tangled
alpha
login
or
join now
bad-example.com
/
hacktober-bot
6
fork
atom
announcing good-first-issue tags added on @tangled.sh (not affiliated with tangled!)
6
fork
atom
overview
issues
1
pulls
pipelines
extract link posting to a helper
bad-example.com
5 months ago
f2d874b5
5d26ae77
+42
-35
1 changed file
expand all
collapse all
unified
split
src
main.rs
+42
-35
src/main.rs
···
9
9
server::create_session::CreateSession,
10
10
repo::create_record::CreateRecord,
11
11
},
12
12
-
client::{AuthenticatedClient, Session, XrpcClient},
12
12
+
client::{HttpClient, AuthenticatedClient, Session, XrpcClient},
13
13
types::{
14
14
datetime::Datetime,
15
15
ident::AtIdentifier,
···
36
36
app_password: String,
37
37
}
38
38
39
39
-
#[tokio::main]
40
40
-
async fn main() -> Result<()> {
41
41
-
env_logger::init();
42
42
-
let args = Args::parse();
43
43
-
44
44
-
45
45
-
// Create HTTP client
46
46
-
let pds_uri = args.pds.as_str().trim_end_matches('/').to_string().into();
47
47
-
let mut client = AuthenticatedClient::new(reqwest::Client::new(), pds_uri);
48
48
-
49
49
-
// Create session
50
50
-
let session = Session::from(
51
51
-
client
52
52
-
.send(
53
53
-
CreateSession::new()
54
54
-
.identifier(&args.identifier)
55
55
-
.password(args.app_password)
56
56
-
.build(),
57
57
-
)
58
58
-
.await?
59
59
-
.into_output()?,
60
60
-
);
61
61
-
62
62
-
println!("logged in as {} ({})", session.handle, session.did);
63
63
-
client.set_session(session);
64
64
-
65
65
-
let text = "hello from tangled.org/@bad-example.com/hacktober-bot/";
39
39
+
async fn post_link<C: HttpClient>(client: &AuthenticatedClient<C>, identifier: &AtIdentifier<'_>, pre_text: &str, link: Url) -> Result<()> {
40
40
+
let link_text = link.to_string();
41
41
+
let text = format!("{pre_text} {link_text}");
66
42
let link_feature = serde_json::json!({
67
43
"$type": "app.bsky.richtext.facet#link",
68
68
-
"uri": "https://tangled.org/@bad-example.com/hacktober-bot/",
44
44
+
"uri": link_text,
69
45
});
70
46
let link_facet = Facet {
71
47
features: vec![Data::from_json(&link_feature)?],
72
48
index: ByteSlice {
73
73
-
byte_start: 11,
74
74
-
byte_end: 54,
49
49
+
byte_start: (pre_text.len() + 1) as i64,
50
50
+
byte_end: text.len() as i64,
75
51
extra_data: Default::default(),
76
52
},
77
53
extra_data: Default::default(),
···
93
69
94
70
let json = serde_json::to_value(post)?;
95
71
let data = Data::from_json(&json)?;
96
96
-
let identifier = AtIdentifier::new(&args.identifier)?;
97
72
98
73
println!("\nposting...");
99
99
-
let wat = client
74
74
+
client
100
75
.send(CreateRecord::new()
101
101
-
.repo(identifier)
76
76
+
.repo(identifier.clone())
102
77
.collection(Post::nsid())
103
78
.record(data)
104
79
.build())
105
80
.await?
106
81
.into_output()?;
107
82
108
108
-
println!("\ngot: {wat:?}");
83
83
+
Ok(())
84
84
+
}
85
85
+
86
86
+
#[tokio::main]
87
87
+
async fn main() -> Result<()> {
88
88
+
env_logger::init();
89
89
+
let args = Args::parse();
90
90
+
91
91
+
92
92
+
// Create HTTP client
93
93
+
let pds_uri = args.pds.as_str().trim_end_matches('/').to_string().into();
94
94
+
let mut client = AuthenticatedClient::new(reqwest::Client::new(), pds_uri);
95
95
+
96
96
+
// Create session
97
97
+
let session = Session::from(
98
98
+
client
99
99
+
.send(
100
100
+
CreateSession::new()
101
101
+
.identifier(&args.identifier)
102
102
+
.password(args.app_password)
103
103
+
.build(),
104
104
+
)
105
105
+
.await?
106
106
+
.into_output()?,
107
107
+
);
108
108
+
109
109
+
println!("logged in as {} ({})", session.handle, session.did);
110
110
+
client.set_session(session);
111
111
+
112
112
+
let identifier = AtIdentifier::new(&args.identifier)?;
113
113
+
114
114
+
let u2: Url = "https://bad-example.com".parse()?;
115
115
+
post_link(&client, &identifier, "link test 2: ", u2).await?;
109
116
110
117
Ok(())
111
118
}