Skip to content

Commit 589bf08

Browse files
authored
Merge pull request #45 from Quantus-Network/feat/simplify-raid-submission
feat: simplify raid submission
2 parents 394f76f + 00ab665 commit 589bf08

5 files changed

Lines changed: 123 additions & 66 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE raid_submissions
2+
ALTER COLUMN target_id DROP NOT NULL;

src/handlers/raid_quest.rs

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::{
1818
raid_leaderboard::RaidLeaderboard,
1919
raid_quest::{CreateRaidQuest, RaidQuest, RaidQuestFilter, RaidQuestSortColumn},
2020
raid_submission::{CreateRaidSubmission, RaidSubmissionInput, RaiderSubmissions},
21+
x_association::XAssociation,
2122
},
2223
utils::x_url::{build_x_status_url, parse_x_status_url},
2324
AppError,
@@ -158,16 +159,7 @@ pub async fn handle_get_active_raid_raider_submissions(
158159
State(state): State<AppState>,
159160
Extension(user): Extension<Address>,
160161
) -> Result<Json<SuccessResponse<RaiderSubmissions>>, AppError> {
161-
let Some(current_active_raid) = state.db.raid_quests.find_active().await? else {
162-
return Err(AppError::Database(DbError::RecordNotFound(format!(
163-
"No active raid is found"
164-
))));
165-
};
166-
let Some(user_x) = state.db.x_associations.find_by_address(&user.quan_address).await? else {
167-
return Err(AppError::Database(DbError::RecordNotFound(format!(
168-
"User doesn't have X association"
169-
))));
170-
};
162+
let (current_active_raid, user_x) = get_active_raid_and_x_association(&state, &user).await?;
171163

172164
let submissions = state
173165
.db
@@ -190,31 +182,13 @@ pub async fn handle_create_raid_submission(
190182
Extension(user): Extension<Address>,
191183
extract::Json(payload): Json<RaidSubmissionInput>,
192184
) -> Result<(StatusCode, Json<SuccessResponse<String>>), AppError> {
193-
let Some((_target_username, target_id)) = parse_x_status_url(&payload.target_tweet_link) else {
194-
return Err(AppError::Handler(HandlerError::InvalidBody(format!(
195-
"Couldn't parse target tweet link"
196-
))));
197-
};
198-
let Some(_) = state.db.relevant_tweets.find_by_id(&target_id).await? else {
199-
return Err(AppError::Database(DbError::RecordNotFound(format!(
200-
"Not a valid target tweet"
201-
))));
202-
};
185+
let (current_active_raid, user_x) = get_active_raid_and_x_association(&state, &user).await?;
186+
203187
let Some((reply_username, reply_id)) = parse_x_status_url(&payload.tweet_reply_link) else {
204188
return Err(AppError::Handler(HandlerError::InvalidBody(format!(
205189
"Couldn't parse tweet reply link"
206190
))));
207191
};
208-
let Some(current_active_raid) = state.db.raid_quests.find_active().await? else {
209-
return Err(AppError::Database(DbError::RecordNotFound(format!(
210-
"No active raid is found"
211-
))));
212-
};
213-
let Some(user_x) = state.db.x_associations.find_by_address(&user.quan_address).await? else {
214-
return Err(AppError::Database(DbError::RecordNotFound(format!(
215-
"User doesn't have X association"
216-
))));
217-
};
218192
if user_x.username != reply_username {
219193
return Err(AppError::Handler(HandlerError::Auth(AuthHandlerError::Unauthorized(
220194
format!("Only tweet reply author is eligible to submit"),
@@ -225,7 +199,6 @@ pub async fn handle_create_raid_submission(
225199
id: reply_id,
226200
raid_id: current_active_raid.id,
227201
raider_id: user.quan_address.0,
228-
target_id: target_id,
229202
};
230203

231204
let created_id = state.db.raid_submissions.create(&new_raid_submission).await?;
@@ -256,6 +229,23 @@ pub async fn handle_delete_raid_submission(
256229
Ok(NoContent)
257230
}
258231

232+
async fn get_active_raid_and_x_association(
233+
state: &AppState,
234+
user: &Address,
235+
) -> Result<(RaidQuest, XAssociation), AppError> {
236+
let Some(current_active_raid) = state.db.raid_quests.find_active().await? else {
237+
return Err(AppError::Database(DbError::RecordNotFound(format!(
238+
"No active raid is found"
239+
))));
240+
};
241+
let Some(user_x) = state.db.x_associations.find_by_address(&user.quan_address).await? else {
242+
return Err(AppError::Database(DbError::RecordNotFound(format!(
243+
"User doesn't have X association"
244+
))));
245+
};
246+
Ok((current_active_raid, user_x))
247+
}
248+
259249
#[cfg(test)]
260250
mod tests {
261251
use axum::{
@@ -570,10 +560,8 @@ mod tests {
570560
.with_state(state.clone());
571561

572562
// 5. Payload
573-
// Target Link -> ID 1868000000000000000
574563
// Reply Link -> ID 999999999, Username "me"
575564
let payload = RaidSubmissionInput {
576-
target_tweet_link: format!("https://x.com/someone/status/{}", target_tweet_id),
577565
tweet_reply_link: "https://x.com/me/status/999999999".to_string(),
578566
};
579567

@@ -596,7 +584,8 @@ mod tests {
596584
assert!(sub.is_some());
597585
let sub = sub.unwrap();
598586
assert_eq!(sub.raid_id, raid_id);
599-
assert_eq!(sub.target_id, target_tweet_id);
587+
assert_eq!(&sub.id, "999999999");
588+
assert!(sub.target_id.is_none());
600589
}
601590

602591
#[tokio::test]
@@ -621,7 +610,6 @@ mod tests {
621610
.with_state(state);
622611

623612
let payload = RaidSubmissionInput {
624-
target_tweet_link: "https://x.com/a/status/100".into(),
625613
tweet_reply_link: "https://x.com/b/status/200".into(),
626614
};
627615

@@ -660,8 +648,7 @@ mod tests {
660648
.with_state(state);
661649

662650
let payload = RaidSubmissionInput {
663-
target_tweet_link: "not_a_valid_url".into(),
664-
tweet_reply_link: "https://x.com/b/status/200".into(),
651+
tweet_reply_link: "https://x.com/b/dwdwdwt/dwdwd".into(),
665652
};
666653

667654
let response = router
@@ -677,7 +664,7 @@ mod tests {
677664
.unwrap();
678665

679666
// 400 Bad Request / Handler Error
680-
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
667+
assert_eq!(response.status(), StatusCode::NOT_FOUND);
681668
}
682669

683670
#[tokio::test]

src/models/raid_submission.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::models::raid_quest::RaidQuest;
99
pub struct RaidSubmission {
1010
pub id: String,
1111
pub raid_id: i32,
12-
pub target_id: String,
12+
pub target_id: Option<String>,
1313
pub raider_id: String,
1414
pub impression_count: i32,
1515
pub reply_count: i32,
@@ -54,13 +54,11 @@ impl<'r> FromRow<'r, PgRow> for RaidSubmission {
5454
pub struct CreateRaidSubmission {
5555
pub id: String,
5656
pub raid_id: i32,
57-
pub target_id: String,
5857
pub raider_id: String,
5958
}
6059

6160
#[derive(Debug, Clone, Deserialize, Serialize)]
6261
pub struct RaidSubmissionInput {
63-
pub target_tweet_link: String,
6462
pub tweet_reply_link: String,
6563
}
6664

src/repositories/raid_submission.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,14 @@ impl RaidSubmissionRepository {
2424
let created_id = sqlx::query_scalar::<_, String>(
2525
"
2626
INSERT INTO raid_submissions (
27-
id, raid_id, target_id, raider_id
27+
id, raid_id, raider_id
2828
)
29-
VALUES ($1, $2, $3, $4)
29+
VALUES ($1, $2, $3)
3030
RETURNING id
3131
",
3232
)
3333
.bind(&submission.id)
3434
.bind(submission.raid_id)
35-
.bind(&submission.target_id)
3635
.bind(&submission.raider_id)
3736
.fetch_optional(&self.pool)
3837
.await?;
@@ -186,7 +185,6 @@ mod tests {
186185
struct SeedData {
187186
raid_id: i32,
188187
raider_id: String,
189-
target_id: String,
190188
}
191189

192190
// Helper to satisfy the strict Foreign Key chain:
@@ -232,18 +230,13 @@ mod tests {
232230
.await
233231
.expect("Failed to seed relevant tweet");
234232

235-
SeedData {
236-
raid_id,
237-
raider_id,
238-
target_id,
239-
}
233+
SeedData { raid_id, raider_id }
240234
}
241235

242236
fn create_mock_submission_input(seed: &SeedData) -> CreateRaidSubmission {
243237
CreateRaidSubmission {
244238
id: Uuid::new_v4().to_string(),
245239
raid_id: seed.raid_id,
246-
target_id: seed.target_id.clone(),
247240
raider_id: seed.raider_id.clone(),
248241
}
249242
}
@@ -423,7 +416,6 @@ mod tests {
423416
let input = CreateRaidSubmission {
424417
id: Uuid::new_v4().to_string(),
425418
raid_id: 9999, // Non-existent Raid
426-
target_id: "fake_tweet".to_string(),
427419
raider_id: "fake_user".to_string(),
428420
};
429421

0 commit comments

Comments
 (0)