this repo has no description
1use chrono::Utc;
2use reqwest::StatusCode;
3use serde_json::{Value, json};
4pub use crate::common::*;
5#[allow(dead_code)]
6pub async fn setup_new_user(handle_prefix: &str) -> (String, String) {
7 let client = client();
8 let ts = Utc::now().timestamp_millis();
9 let handle = format!("{}-{}.test", handle_prefix, ts);
10 let email = format!("{}-{}@test.com", handle_prefix, ts);
11 let password = "e2e-password-123";
12 let create_account_payload = json!({
13 "handle": handle,
14 "email": email,
15 "password": password
16 });
17 let create_res = client
18 .post(format!(
19 "{}/xrpc/com.atproto.server.createAccount",
20 base_url().await
21 ))
22 .json(&create_account_payload)
23 .send()
24 .await
25 .expect("setup_new_user: Failed to send createAccount");
26 if create_res.status() != reqwest::StatusCode::OK {
27 panic!(
28 "setup_new_user: Failed to create account: {:?}",
29 create_res.text().await
30 );
31 }
32 let create_body: Value = create_res
33 .json()
34 .await
35 .expect("setup_new_user: createAccount response was not JSON");
36 let new_did = create_body["did"]
37 .as_str()
38 .expect("setup_new_user: Response had no DID")
39 .to_string();
40 let new_jwt = verify_new_account(&client, &new_did).await;
41 (new_did, new_jwt)
42}
43#[allow(dead_code)]
44pub async fn create_post(
45 client: &reqwest::Client,
46 did: &str,
47 jwt: &str,
48 text: &str,
49) -> (String, String) {
50 let collection = "app.bsky.feed.post";
51 let rkey = format!("e2e_social_{}", Utc::now().timestamp_millis());
52 let now = Utc::now().to_rfc3339();
53 let create_payload = json!({
54 "repo": did,
55 "collection": collection,
56 "rkey": rkey,
57 "record": {
58 "$type": collection,
59 "text": text,
60 "createdAt": now
61 }
62 });
63 let create_res = client
64 .post(format!(
65 "{}/xrpc/com.atproto.repo.putRecord",
66 base_url().await
67 ))
68 .bearer_auth(jwt)
69 .json(&create_payload)
70 .send()
71 .await
72 .expect("Failed to send create post request");
73 assert_eq!(
74 create_res.status(),
75 reqwest::StatusCode::OK,
76 "Failed to create post record"
77 );
78 let create_body: Value = create_res
79 .json()
80 .await
81 .expect("create post response was not JSON");
82 let uri = create_body["uri"].as_str().unwrap().to_string();
83 let cid = create_body["cid"].as_str().unwrap().to_string();
84 (uri, cid)
85}
86#[allow(dead_code)]
87pub async fn create_follow(
88 client: &reqwest::Client,
89 follower_did: &str,
90 follower_jwt: &str,
91 followee_did: &str,
92) -> (String, String) {
93 let collection = "app.bsky.graph.follow";
94 let rkey = format!("e2e_follow_{}", Utc::now().timestamp_millis());
95 let now = Utc::now().to_rfc3339();
96 let create_payload = json!({
97 "repo": follower_did,
98 "collection": collection,
99 "rkey": rkey,
100 "record": {
101 "$type": collection,
102 "subject": followee_did,
103 "createdAt": now
104 }
105 });
106 let create_res = client
107 .post(format!(
108 "{}/xrpc/com.atproto.repo.putRecord",
109 base_url().await
110 ))
111 .bearer_auth(follower_jwt)
112 .json(&create_payload)
113 .send()
114 .await
115 .expect("Failed to send create follow request");
116 assert_eq!(
117 create_res.status(),
118 reqwest::StatusCode::OK,
119 "Failed to create follow record"
120 );
121 let create_body: Value = create_res
122 .json()
123 .await
124 .expect("create follow response was not JSON");
125 let uri = create_body["uri"].as_str().unwrap().to_string();
126 let cid = create_body["cid"].as_str().unwrap().to_string();
127 (uri, cid)
128}
129#[allow(dead_code)]
130pub async fn create_like(
131 client: &reqwest::Client,
132 liker_did: &str,
133 liker_jwt: &str,
134 subject_uri: &str,
135 subject_cid: &str,
136) -> (String, String) {
137 let collection = "app.bsky.feed.like";
138 let rkey = format!("e2e_like_{}", Utc::now().timestamp_millis());
139 let now = Utc::now().to_rfc3339();
140 let payload = json!({
141 "repo": liker_did,
142 "collection": collection,
143 "rkey": rkey,
144 "record": {
145 "$type": collection,
146 "subject": {
147 "uri": subject_uri,
148 "cid": subject_cid
149 },
150 "createdAt": now
151 }
152 });
153 let res = client
154 .post(format!(
155 "{}/xrpc/com.atproto.repo.putRecord",
156 base_url().await
157 ))
158 .bearer_auth(liker_jwt)
159 .json(&payload)
160 .send()
161 .await
162 .expect("Failed to create like");
163 assert_eq!(res.status(), StatusCode::OK, "Failed to create like");
164 let body: Value = res.json().await.expect("Like response not JSON");
165 (
166 body["uri"].as_str().unwrap().to_string(),
167 body["cid"].as_str().unwrap().to_string(),
168 )
169}
170#[allow(dead_code)]
171pub async fn create_repost(
172 client: &reqwest::Client,
173 reposter_did: &str,
174 reposter_jwt: &str,
175 subject_uri: &str,
176 subject_cid: &str,
177) -> (String, String) {
178 let collection = "app.bsky.feed.repost";
179 let rkey = format!("e2e_repost_{}", Utc::now().timestamp_millis());
180 let now = Utc::now().to_rfc3339();
181 let payload = json!({
182 "repo": reposter_did,
183 "collection": collection,
184 "rkey": rkey,
185 "record": {
186 "$type": collection,
187 "subject": {
188 "uri": subject_uri,
189 "cid": subject_cid
190 },
191 "createdAt": now
192 }
193 });
194 let res = client
195 .post(format!(
196 "{}/xrpc/com.atproto.repo.putRecord",
197 base_url().await
198 ))
199 .bearer_auth(reposter_jwt)
200 .json(&payload)
201 .send()
202 .await
203 .expect("Failed to create repost");
204 assert_eq!(res.status(), StatusCode::OK, "Failed to create repost");
205 let body: Value = res.json().await.expect("Repost response not JSON");
206 (
207 body["uri"].as_str().unwrap().to_string(),
208 body["cid"].as_str().unwrap().to_string(),
209 )
210}