Skip to content

Commit

Permalink
Feature/save all broadcasts (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
fatfingers23 committed Apr 20, 2024
1 parent 8b0a7e7 commit 603621b
Show file tree
Hide file tree
Showing 24 changed files with 482 additions and 79 deletions.
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ services:
DISCORD_TOKEN: '${DISCORD_TOKEN}'
TRACKSCAPE_API_BASE: '${TRACKSCAPE_API_BASE}'
REDIS_ADDR: '${REDIS_ADDR}'
WOM_API_KEY: '${WOM_API_KEY}'
depends_on:
- mongo
- redis
Expand Down
20 changes: 12 additions & 8 deletions trackscape-discord-api/src/controllers/chat_controller.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::cache::Cache;
use crate::services::osrs_broadcast_handler::OSRSBroadcastHandler;
use crate::websocket_server::DiscordToClanChatMessage;
use crate::{handler, ChatServerHandle};
use actix_web::web::Data;
Expand All @@ -19,7 +18,9 @@ use trackscape_discord_shared::jobs::CeleryJobQueue;
use trackscape_discord_shared::osrs_broadcast_extractor::osrs_broadcast_extractor::{
get_wiki_clan_rank_image_url, ClanMessage,
};
use trackscape_discord_shared::osrs_broadcast_handler::OSRSBroadcastHandler;
use trackscape_discord_shared::wiki_api::wiki_api::WikiQuest;
use web::Json;

#[derive(Debug)]
struct MyError {
Expand All @@ -30,7 +31,7 @@ struct MyError {
async fn new_discord_message(
req: HttpRequest,
chat_server: web::Data<ChatServerHandle>,
new_chat: web::Json<DiscordToClanChatMessage>,
new_chat: Json<DiscordToClanChatMessage>,
mongodb: web::Data<BotMongoDb>,
) -> actix_web::Result<String> {
let possible_verification_code = req.headers().get("verification-code");
Expand Down Expand Up @@ -77,11 +78,11 @@ async fn new_discord_message(
#[post("/new-clan-chat")]
async fn new_clan_chats(
req: HttpRequest,
discord_http_client: web::Data<Http>,
cache: web::Data<Cache>,
new_chat: web::Json<Vec<ClanMessage>>,
mongodb: web::Data<BotMongoDb>,
persist: web::Data<PersistInstance>,
discord_http_client: Data<Http>,
cache: Data<Cache>,
new_chat: Json<Vec<ClanMessage>>,
mongodb: Data<BotMongoDb>,
persist: Data<PersistInstance>,
celery: Data<Arc<Celery>>,
) -> actix_web::Result<String> {
let possible_verification_code = req.headers().get("verification-code");
Expand Down Expand Up @@ -227,7 +228,10 @@ async fn new_clan_chats(
Some(broadcast) => {
info!("Broadcast: {:?}", broadcast);
info!("{}\n", chat.message.clone());

let _ = mongodb
.broadcasts
.create_broadcast(registered_guild.guild_id, broadcast.clone())
.await;
let mut broadcast_embed = CreateEmbed::new()
.title(broadcast.title.clone())
.description(broadcast.message.clone())
Expand Down
47 changes: 45 additions & 2 deletions trackscape-discord-api/src/controllers/clan_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use actix_web::{get, web, Error, HttpResponse, Scope};
use log::{error, info};
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use tokio::time::sleep;
use trackscape_discord_shared::database::clan_mate_collection_log_totals::ClanMateCollectionLogTotals;
use trackscape_discord_shared::database::clan_mates::{ClanMateModel, ClanMates};
use trackscape_discord_shared::database::BotMongoDb;
Expand Down Expand Up @@ -106,7 +105,6 @@ async fn collection_log(
mongodb: Data<BotMongoDb>,
path: web::Path<(String,)>,
) -> Result<HttpResponse, Error> {
sleep(std::time::Duration::from_secs(1)).await;
let id = path.into_inner().0;
let possible_parsed_id = bson::oid::ObjectId::from_str(id.as_str());
let id = match possible_parsed_id {
Expand Down Expand Up @@ -148,9 +146,54 @@ async fn collection_log(
}
}

#[derive(Deserialize)]
struct BroadcastRequest {
id: String,
limit: i64,
}

#[get("/{id}/broadcasts/{limit}")]
async fn broadcasts(
mongodb: Data<BotMongoDb>,
path: web::Path<BroadcastRequest>,
) -> Result<HttpResponse, Error> {
let possible_parsed_id = bson::oid::ObjectId::from_str(path.id.as_str());
let id = match possible_parsed_id {
Ok(parsed_id) => parsed_id,
Err(_) => {
return Ok(HttpResponse::BadRequest().body("Invalid id format."));
}
};
let registered_guild_query = mongodb.guilds.get_by_id(id).await;
match registered_guild_query {
Ok(possible_guild) => match possible_guild {
Some(guild) => {
let limit_to_use = if path.limit > 100 { 100 } else { path.limit };
let broadcasts = mongodb
.broadcasts
.get_latest_broadcasts(guild.guild_id, limit_to_use)
.await;
match broadcasts {
Ok(broadcasts) => Ok(HttpResponse::Ok().json(broadcasts)),
Err(err) => {
error!("Failed to get broadcasts: {}", err);
Ok(HttpResponse::BadRequest().body("There was an issue with the request"))
}
}
}
None => Ok(HttpResponse::BadRequest().body("There is not a clan with that id")),
},
Err(err) => {
error!("Failed to get clan by id: {}", err);
Ok(HttpResponse::BadRequest().body("There was an issue with the request"))
}
}
}

pub fn clan_controller() -> Scope {
web::scope("/clans")
.service(list_clans)
.service(detail)
.service(collection_log)
.service(broadcasts)
}
1 change: 0 additions & 1 deletion trackscape-discord-api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ extern crate dotenv;
mod cache;
mod controllers;
mod handler;
mod services;
mod websocket_server;

use crate::cache::Cache;
Expand Down
1 change: 0 additions & 1 deletion trackscape-discord-api/src/services/mod.rs

This file was deleted.

1 change: 1 addition & 0 deletions trackscape-discord-shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"

[dependencies]
async-recursion = "1.0.5"
bson = { version = "2.10.0", features = ["chrono-0_4"] }
tracing = "0.1.37"
num-format = "0.4.4"
futures = "0.3.28"
Expand Down
66 changes: 66 additions & 0 deletions trackscape-discord-shared/src/database/broadcasts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use crate::database::BroadcastsDb;
use crate::osrs_broadcast_handler::BroadcastMessageToDiscord;
use bson::DateTime;
use futures::TryStreamExt;
use mockall::predicate::*;
use mongodb::bson::doc;
use mongodb::{bson, Database};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BroadcastModel {
#[serde(rename = "_id")]
pub id: bson::oid::ObjectId,
pub guild_id: u64,
pub broadcast: BroadcastMessageToDiscord,
#[serde(serialize_with = "bson::serde_helpers::serialize_bson_datetime_as_rfc3339_string")]
#[serde(
deserialize_with = "bson::serde_helpers::deserialize_bson_datetime_from_rfc3339_string"
)]
pub created_at: DateTime,
}

impl BroadcastsDb {
pub const COLLECTION_NAME: &'static str = "broadcasts";
pub fn new_instance(mongodb: Database) -> Self {
Self { db: mongodb }
}

pub async fn create_broadcast(
&self,
guild_id: u64,
broadcast: BroadcastMessageToDiscord,
) -> Result<(), anyhow::Error> {
let collection = self.db.collection(Self::COLLECTION_NAME);
let model = BroadcastModel {
id: bson::oid::ObjectId::new(),
guild_id,
broadcast,
created_at: DateTime::now(),
};
collection.insert_one(model, None).await?;
Ok(())
}

pub async fn get_latest_broadcasts(
&self,
guild_id: u64,
limit: i64,
) -> Result<Vec<BroadcastModel>, anyhow::Error> {
let collection = self.db.collection(Self::COLLECTION_NAME);
let filter = doc! { "guild_id": bson::to_bson(&guild_id).unwrap() };
let options = mongodb::options::FindOptions::builder()
.sort(doc! { "created_at": -1 })
.limit(limit)
.build();
let cursor = collection.find(filter, options).await?;
let broadcasts: Vec<BroadcastModel> = cursor.try_collect().await?;
Ok(broadcasts)
}
}

impl BroadcastsDb {
pub fn new(mongodb: Database) -> Self {
Self { db: mongodb }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ pub struct ClanMateCollectionLogTotalModel {
pub total: i64,
pub created_at: DateTime,
}
pub const COLLECTION_LOG_COLLECTION_NAME: &'static str = "clan_mate_collection_log_totals";

impl ClanMateCollectionLogTotalModel {
pub const COLLECTION_NAME: &'static str = "clan_mate_collection_log_totals";

pub fn new(guild_id: u64, player_id: bson::oid::ObjectId, total: i64) -> Self {
Self {
guild_id,
Expand Down Expand Up @@ -65,9 +64,9 @@ impl ClanMateCollectionLogTotals for ClanMateCollectionLogTotalsDb {
player_id: bson::oid::ObjectId,
total: i64,
) -> Result<(), Error> {
let collection = self.db.collection::<ClanMateCollectionLogTotalModel>(
ClanMateCollectionLogTotalModel::COLLECTION_NAME,
);
let collection = self
.db
.collection::<ClanMateCollectionLogTotalModel>(COLLECTION_LOG_COLLECTION_NAME);

let filter = doc! {
"guild_id": bson::to_bson(&guild_id).unwrap(),
Expand Down Expand Up @@ -101,9 +100,9 @@ impl ClanMateCollectionLogTotals for ClanMateCollectionLogTotalsDb {
&self,
guild_id: u64,
) -> Result<Vec<ClanMateCollectionLogTotalsView>, anyhow::Error> {
let collection = self.db.collection::<ClanMateCollectionLogTotalModel>(
ClanMateCollectionLogTotalModel::COLLECTION_NAME,
);
let collection = self
.db
.collection::<ClanMateCollectionLogTotalModel>(COLLECTION_LOG_COLLECTION_NAME);

let filter = doc! {
"guild_id": bson::to_bson(&guild_id).unwrap(),
Expand Down
35 changes: 35 additions & 0 deletions trackscape-discord-shared/src/database/clan_mates.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::database::clan_mate_collection_log_totals::COLLECTION_LOG_COLLECTION_NAME;
use crate::database::ClanMatesDb;
use anyhow::Error;
use async_trait::async_trait;
Expand Down Expand Up @@ -196,9 +197,43 @@ impl ClanMates for ClanMatesDb {

async fn remove_clan_mate(&self, guild_id: u64, player_name: String) -> Result<(), Error> {
//TODO add a bit to clean up other collections too

let collection = self
.db
.collection::<ClanMateModel>(ClanMateModel::COLLECTION_NAME);

let possible_player = self.find_by_current_name(player_name.clone()).await?;
if possible_player.is_none() {
return Err(anyhow::anyhow!(format!(
"Failed to find clan mate: {}",
player_name
)));
}
let player = possible_player.unwrap();

//Removes other data from db may move else where
let collection_log_collection = self
.db
.collection::<ClanMateModel>(COLLECTION_LOG_COLLECTION_NAME);

let filter = doc! {
"guild_id": bson::to_bson(&guild_id).unwrap(),
"player_id": player.id.clone(),
};
let collection_log_result = collection_log_collection.delete_many(filter, None).await;
if collection_log_result.is_err() {
println!(
"Failed to remove collection log totals for clan mate: {}",
player_name
);
println!("Error: {:?}", collection_log_result.err());
return Err(anyhow::anyhow!(format!(
"Failed to remove collection log totals for clan mate: {}",
player_name
)));
}
//End of removing from other collections

let filter = doc! {
"guild_id":bson::to_bson(&guild_id).unwrap(),
"player_name": bson::to_bson(&player_name.replace(" ", "\u{a0}")).unwrap()
Expand Down
12 changes: 11 additions & 1 deletion trackscape-discord-shared/src/database/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use mongodb::bson::doc;
use mongodb::options::ClientOptions;
use mongodb::Database;
pub mod broadcasts;
pub mod clan_mate_collection_log_totals;
pub mod clan_mates;
pub mod drop_logs_db;
Expand All @@ -24,6 +25,7 @@ pub struct BotMongoDb {
pub drop_logs: DropLogsDb,
pub clan_mates: ClanMatesDb,
pub clan_mate_collection_log_totals: ClanMateCollectionLogTotalsDb,
pub broadcasts: BroadcastsDb,
}

#[derive(Clone)]
Expand All @@ -46,6 +48,11 @@ pub struct ClanMateCollectionLogTotalsDb {
db: Database,
}

#[derive(Clone)]
pub struct BroadcastsDb {
db: Database,
}

#[async_trait]
impl MongoDb for BotMongoDb {
async fn new_db_instance(db_url: String) -> Self {
Expand All @@ -60,7 +67,10 @@ impl MongoDb for BotMongoDb {
guilds: GuildsDb::new(db.clone()),
drop_logs: DropLogsDb::new_instance(db.clone()),
clan_mates: ClanMatesDb::new_instance(db.clone()),
clan_mate_collection_log_totals: ClanMateCollectionLogTotalsDb::new_instance(db),
clan_mate_collection_log_totals: ClanMateCollectionLogTotalsDb::new_instance(
db.clone(),
),
broadcasts: BroadcastsDb::new_instance(db),
}
}
}
1 change: 1 addition & 0 deletions trackscape-discord-shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ pub mod ge_api;
pub mod helpers;
pub mod jobs;
pub mod osrs_broadcast_extractor;
pub mod osrs_broadcast_handler;
pub mod wiki_api;
pub mod wom;
Loading

0 comments on commit 603621b

Please sign in to comment.