@@ -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) ]
260250mod 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]
0 commit comments