diff --git a/server/.sqlx/query-0c85cedeaa4596b2c9502fb1117e5b4395166143a20bed6d776392d8a1d91a41.json b/server/.sqlx/query-0c85cedeaa4596b2c9502fb1117e5b4395166143a20bed6d776392d8a1d91a41.json new file mode 100644 index 0000000..5483d18 --- /dev/null +++ b/server/.sqlx/query-0c85cedeaa4596b2c9502fb1117e5b4395166143a20bed6d776392d8a1d91a41.json @@ -0,0 +1,12 @@ +{ + "db_name": "MySQL", + "query": "DELETE FROM `word_excluded_users` WHERE `word_id` = ?", + "describe": { + "columns": [], + "parameters": { + "Right": 1 + }, + "nullable": [] + }, + "hash": "0c85cedeaa4596b2c9502fb1117e5b4395166143a20bed6d776392d8a1d91a41" +} diff --git a/server/.sqlx/query-165481c1b0d0ae827417789f98718f781f3ee0a2396e35ee623ffe620b1b5d2e.json b/server/.sqlx/query-165481c1b0d0ae827417789f98718f781f3ee0a2396e35ee623ffe620b1b5d2e.json deleted file mode 100644 index 5574f42..0000000 --- a/server/.sqlx/query-165481c1b0d0ae827417789f98718f781f3ee0a2396e35ee623ffe620b1b5d2e.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "db_name": "MySQL", - "query": "DELETE FROM `excluded_users` WHERE `word_id` = ?", - "describe": { - "columns": [], - "parameters": { - "Right": 1 - }, - "nullable": [] - }, - "hash": "165481c1b0d0ae827417789f98718f781f3ee0a2396e35ee623ffe620b1b5d2e" -} diff --git a/server/.sqlx/query-240b60be424754d335427a5e4f39d3fc49994a6eb1615f1443d09de92b956f09.json b/server/.sqlx/query-16e7e237d8fae0f937ad54173b379d476bbc9d4afc52708eb9de6a87a619d66c.json similarity index 74% rename from server/.sqlx/query-240b60be424754d335427a5e4f39d3fc49994a6eb1615f1443d09de92b956f09.json rename to server/.sqlx/query-16e7e237d8fae0f937ad54173b379d476bbc9d4afc52708eb9de6a87a619d66c.json index 5652c3a..3097d04 100644 --- a/server/.sqlx/query-240b60be424754d335427a5e4f39d3fc49994a6eb1615f1443d09de92b956f09.json +++ b/server/.sqlx/query-16e7e237d8fae0f937ad54173b379d476bbc9d4afc52708eb9de6a87a619d66c.json @@ -1,6 +1,6 @@ { "db_name": "MySQL", - "query": "SELECT\n `word`,\n `word_uuid` AS `id`,\n `register_time` AS `time`,\n `excluded_users`.`trap_id` AS `excluded_users`\n FROM `words` JOIN `excluded_users` ON `words`.`word_id` = `excluded_users`.`word_id`\n WHERE `words`.`trap_id`=?", + "query": "SELECT\n `word`,\n `word_uuid` AS `id`,\n `register_time` AS `time`,\n `word_excluded_users`.`trap_id` AS `excluded_users`\n FROM\n `words`\n JOIN\n `word_excluded_users`\n ON\n `words`.`word_id` = `word_excluded_users`.`word_id`\n WHERE\n `words`.`trap_id`=?", "describe": { "columns": [ { @@ -50,5 +50,5 @@ false ] }, - "hash": "240b60be424754d335427a5e4f39d3fc49994a6eb1615f1443d09de92b956f09" + "hash": "16e7e237d8fae0f937ad54173b379d476bbc9d4afc52708eb9de6a87a619d66c" } diff --git a/server/.sqlx/query-950a4cfda9cc104e4e3b2b2a8dfbac70b945a38eead1f0728b2db11378f08fe4.json b/server/.sqlx/query-950a4cfda9cc104e4e3b2b2a8dfbac70b945a38eead1f0728b2db11378f08fe4.json new file mode 100644 index 0000000..d9accb6 --- /dev/null +++ b/server/.sqlx/query-950a4cfda9cc104e4e3b2b2a8dfbac70b945a38eead1f0728b2db11378f08fe4.json @@ -0,0 +1,34 @@ +{ + "db_name": "MySQL", + "query": "SELECT\n `word`,\n `traq_uuid`\n FROM\n `words`\n JOIN\n `word_excluded_users`\n ON\n `words`.`word_id` = `word_excluded_users`.`word_id`\n JOIN\n `users`\n ON\n `words`.`trap_id` = `users`.`trap_id`\n ", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "word", + "type_info": { + "type": "VarString", + "flags": "NOT_NULL | NO_DEFAULT_VALUE", + "max_size": 200 + } + }, + { + "ordinal": 1, + "name": "traq_uuid", + "type_info": { + "type": "String", + "flags": "NOT_NULL | NO_DEFAULT_VALUE", + "max_size": 144 + } + } + ], + "parameters": { + "Right": 0 + }, + "nullable": [ + false, + false + ] + }, + "hash": "950a4cfda9cc104e4e3b2b2a8dfbac70b945a38eead1f0728b2db11378f08fe4" +} diff --git a/server/app/src/repo.rs b/server/app/src/repo.rs index 7059059..5a28f43 100644 --- a/server/app/src/repo.rs +++ b/server/app/src/repo.rs @@ -7,6 +7,7 @@ use sqlx::{ }; mod polling; +mod traq_message; mod words; pub mod constant { diff --git a/server/app/src/repo/traq_message.rs b/server/app/src/repo/traq_message.rs new file mode 100644 index 0000000..a6f4e89 --- /dev/null +++ b/server/app/src/repo/traq_message.rs @@ -0,0 +1,58 @@ +use anyhow::Result; +use sqlx::query; +use uuid::Uuid; + +use super::Repository; +use crate::traq::message::{StampNotify, WordNotify, A}; + +impl Repository { + pub async fn a(&self, a: &A) -> Result { + let _aa = query!( + "SELECT + `word`, + `traq_uuid` + FROM + `words` + JOIN + `word_excluded_users` + ON + `words`.`word_id` = `word_excluded_users`.`word_id` + JOIN + `users` + ON + `words`.`trap_id` = `users`.`trap_id` + " + ); + + Ok(WordNotify { + words: vec!["".to_string()], + target_traq_uuid: Uuid::new_v4(), + message_uuid: a.message_uuid, + }) + } + + pub async fn b(&self, a: &A) -> Result { + let _aa = query!( + "SELECT + `word`, + `traq_uuid` + FROM + `words` + JOIN + `word_excluded_users` + ON + `words`.`word_id` = `word_excluded_users`.`word_id` + JOIN + `users` + ON + `words`.`trap_id` = `users`.`trap_id` + " + ); + + Ok(StampNotify { + stamps: vec!["".to_string()], + target_traq_uuid: Uuid::new_v4(), + message_uuid: a.message_uuid, + }) + } +} diff --git a/server/app/src/traq/message.rs b/server/app/src/traq/message.rs index 8084d01..9d20fe0 100644 --- a/server/app/src/traq/message.rs +++ b/server/app/src/traq/message.rs @@ -1,12 +1,18 @@ use anyhow::Result; +use axum::async_trait; use chrono::{DateTime, SecondsFormat, Utc}; use log::{debug, error, info}; -use traq::apis::{configuration::Configuration, message_api::search_messages}; +use traq::{ + apis::{ + configuration::Configuration, message_api::search_messages, user_api::post_direct_message, + }, + models::{Message, PostMessageRequest}, +}; +use uuid::Uuid; +use super::{CONFIG, MESSAGE_LIMIT}; use crate::repo::Repository; -use super::MESSAGE_LIMIT; - pub(super) async fn collect( repo: &Repository, config: &Configuration, @@ -54,6 +60,12 @@ pub(super) async fn collect( let hit_messages = result.hits; info!("Collected {} messages", hit_messages.len()); + let m = hit_messages.clone(); + let r = repo.clone(); + tokio::spawn(async move { + let _ = process(m, &r); + }); + // check whether all messages are retrieved if MESSAGE_LIMIT * (page + 1) >= result.total_hits as i32 { if hit_messages.is_empty() { @@ -73,3 +85,129 @@ pub(super) async fn collect( Ok(()) } + +async fn process(messages: Vec, repo: &Repository) -> Result<()> { + let messages = convert(messages); + + let mut notifies = Vec::>::new(); + + for message in messages { + notifies.push(message.match_word(repo).await); + notifies.push(message.match_stamp(repo).await); + } + + // These Vectors no longer need to be edited + let ns: Vec> = notifies; + + info!("Sending {} DMs...", ns.len()); + for n in ns { + let _ = n.send_dm(); + } + info!("Correctly finished sending DMs!"); + + Ok(()) +} + +pub struct A { + pub message_uuid: Uuid, + pub user_id: Uuid, + pub content: String, + pub stamps: Vec, +} + +struct Stamp { + stamp_id: Uuid, + user_id: Uuid, +} + +pub struct WordNotify { + pub words: Vec, + pub target_traq_uuid: Uuid, + pub message_uuid: Uuid, +} + +pub struct StampNotify { + pub stamps: Vec, + pub target_traq_uuid: Uuid, + pub message_uuid: Uuid, +} + +fn convert(messages: Vec) -> Vec { + let mut a = Vec::::new(); + + for i in messages { + let mut c = Vec::::new(); + for j in i.stamps { + c.push(Stamp { + user_id: j.user_id, + stamp_id: j.stamp_id, + }); + } + + a.push(A { + message_uuid: i.id, + user_id: i.user_id, + content: i.content, + stamps: c, + }); + } + + a +} + +impl A { + async fn match_word(&self, repo: &Repository) -> Box { + let _ = repo.a(self).await; + unimplemented!() + } + + async fn match_stamp(&self, repo: &Repository) -> Box { + let _ = repo.b(self).await; + unimplemented!() + } +} + +#[async_trait] +trait Notify { + async fn send_dm(&self) -> Result<()>; +} + +#[async_trait] +impl Notify for WordNotify { + async fn send_dm(&self) -> Result<()> { + let _a = post_direct_message( + &CONFIG, + &self.target_traq_uuid.to_string(), + Some(PostMessageRequest { + content: format!("{:?}\n{}", self.words, self.message_uuid), + embed: None, + }), + ) + .await; + + Ok(()) + } +} + +#[async_trait] +impl Notify for StampNotify { + async fn send_dm(&self) -> Result<()> { + let _a = post_direct_message( + &CONFIG, + &self.target_traq_uuid.to_string(), + Some(PostMessageRequest { + content: format!("{:?}\n{}", self.stamps, self.message_uuid), + embed: None, + }), + ) + .await; + + let _ = match _a { + Ok(_) => return Ok(()), + Err(_) => { + error!(""); + return Ok(()); + } + }; + } +}