Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Index forum support #39

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
66163b7
My awesome changes
hiimjasmine00 Jan 15, 2025
46559a2
These are probably the last ones
hiimjasmine00 Jan 15, 2025
c018573
Fix a few bugs
hiimjasmine00 Jan 15, 2025
af6122c
improved three lines
hiimjasmine00 Jan 15, 2025
72169fc
add empty checks
hiimjasmine00 Jan 16, 2025
65a6fa1
Merge branch 'main' into index-forums
hiimjasmine00 Jan 17, 2025
4bc4062
fix a bit more
hiimjasmine00 Jan 17, 2025
4452dd9
Merge branch 'main' of https://github.com/geode-sdk/server into index…
hiimjasmine00 Jan 18, 2025
1bb6fe4
clone things before refactoring
hiimjasmine00 Jan 18, 2025
03d2914
Merge branch 'main' of https://github.com/geode-sdk/server into index…
hiimjasmine00 Jan 18, 2025
71be8fa
Okay, maybe I'll test these
hiimjasmine00 Jan 18, 2025
12f7ee6
Merge branch 'main' of https://github.com/geode-sdk/server into index…
hiimjasmine00 Jan 18, 2025
e5dab29
Merge branch 'main' of https://github.com/geode-sdk/server into index…
hiimjasmine00 Jan 20, 2025
4b31786
I think this should be it
hiimjasmine00 Jan 20, 2025
9fe2160
remove refs
hiimjasmine00 Jan 20, 2025
419e69a
Simplify parameters
hiimjasmine00 Jan 20, 2025
8e955e0
clean up code
hiimjasmine00 Jan 21, 2025
eed95c0
Merge branch 'main' of https://github.com/geode-sdk/server into index…
hiimjasmine00 Jan 23, 2025
dfe36bd
Merge branch 'main' of https://github.com/geode-sdk/server into index…
hiimjasmine00 Jan 27, 2025
ee4e75e
create pending threads from oldest to newest
hiimjasmine00 Jan 27, 2025
f0d934b
change button ids
hiimjasmine00 Jan 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ GITHUB_CLIENT_SECRET=
# Discord

DISCORD_WEBHOOK_URL=
DISCORD_BOT_TOKEN=
DISCORD_GUILD_ID=
DISCORD_CHANNEL_ID=

# Config

Expand Down
44 changes: 44 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub struct AppData {
front_url: String,
github: GitHubClientData,
webhook_url: String,
discord: DiscordForumData,
disable_downloads: bool,
max_download_mb: u32,
port: u16,
Expand All @@ -17,6 +18,13 @@ pub struct GitHubClientData {
client_secret: String,
}

#[derive(Clone)]
pub struct DiscordForumData {
guild_id: u64,
channel_id: u64,
bot_token: String,
}

pub async fn build_config() -> anyhow::Result<AppData> {
let env_url = dotenvy::var("DATABASE_URL")?;

Expand All @@ -31,6 +39,15 @@ pub async fn build_config() -> anyhow::Result<AppData> {
let github_client = dotenvy::var("GITHUB_CLIENT_ID").unwrap_or("".to_string());
let github_secret = dotenvy::var("GITHUB_CLIENT_SECRET").unwrap_or("".to_string());
let webhook_url = dotenvy::var("DISCORD_WEBHOOK_URL").unwrap_or("".to_string());
let guild_id = dotenvy::var("DISCORD_GUILD_ID")
.unwrap_or("0".to_string())
.parse::<u64>()
.unwrap_or(0);
let channel_id = dotenvy::var("DISCORD_CHANNEL_ID")
.unwrap_or("0".to_string())
.parse::<u64>()
.unwrap_or(0);
let bot_token = dotenvy::var("DISCORD_BOT_TOKEN").unwrap_or("".to_string());
let disable_downloads =
dotenvy::var("DISABLE_DOWNLOAD_COUNTS").unwrap_or("0".to_string()) == "1";
let max_download_mb = dotenvy::var("MAX_MOD_FILESIZE_MB")
Expand All @@ -47,6 +64,11 @@ pub async fn build_config() -> anyhow::Result<AppData> {
client_secret: github_secret,
},
webhook_url,
discord: DiscordForumData {
guild_id,
channel_id,
bot_token,
},
disable_downloads,
max_download_mb,
port,
Expand All @@ -64,6 +86,24 @@ impl GitHubClientData {
}
}

impl DiscordForumData {
pub fn is_valid(&self) -> bool {
self.guild_id != 0 && self.channel_id != 0 && !self.bot_token.is_empty()
}

pub fn guild_id(&self) -> u64 {
self.guild_id
}

pub fn channel_id(&self) -> u64 {
self.channel_id
}

pub fn bot_auth(&self) -> String {
format!("Bot {}", self.bot_token)
}
}

impl AppData {
pub fn db(&self) -> &sqlx::postgres::PgPool {
&self.db
Expand All @@ -85,6 +125,10 @@ impl AppData {
&self.webhook_url
}

pub fn discord(&self) -> &DiscordForumData {
&self.discord
}

pub fn disable_downloads(&self) -> bool {
self.disable_downloads
}
Expand Down
53 changes: 49 additions & 4 deletions src/endpoints/mod_versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ use crate::events::mod_created::{
};
use crate::webhook::discord::DiscordWebhook;
use crate::{
extractors::auth::Auth,
types::{
extractors::auth::Auth, forum::discord::create_or_update_thread, types::{
api::{ApiError, ApiResponse},
mod_json::{split_version_and_compare, ModJson},
models::{
Expand Down Expand Up @@ -316,6 +315,26 @@ pub async fn create_version(
}
.to_discord_webhook()
.send(&data.webhook_url());
} else {
tokio::spawn(async move {
if !data.discord().is_valid() {
log::error!("Discord configuration is not set up. Not creating forum threads.");
return;
}

let version_res = ModVersion::get_one(&path.id, &json.version, true, false, &mut pool).await;
if version_res.is_err() {
return;
}
create_or_update_thread(
None,
&data.discord(),
&fetched_mod.unwrap(),
&version_res.unwrap(),
"",
&data.app_url()
).await;
});
}
Ok(HttpResponse::NoContent())
}
Expand Down Expand Up @@ -396,7 +415,7 @@ pub async fn update_version(
name: version.name.clone(),
version: version.version.clone(),
owner,
verified_by: dev,
verified_by: dev.clone(),
base_url: data.app_url().to_string(),
}
.to_discord_webhook()
Expand All @@ -407,13 +426,39 @@ pub async fn update_version(
name: version.name.clone(),
version: version.version.clone(),
owner,
verified: NewModVersionVerification::Admin(dev),
verified: NewModVersionVerification::Admin(dev.clone()),
base_url: data.app_url().to_string(),
}
.to_discord_webhook()
.send(&data.webhook_url());
}
}

if payload.status == ModVersionStatusEnum::Accepted || payload.status == ModVersionStatusEnum::Rejected {
tokio::spawn(async move {
if !data.discord().is_valid() {
log::error!("Discord configuration is not set up. Not creating forum threads.");
return;
}

let mod_res = Mod::get_one(&path.id, false, &mut pool).await.ok().flatten();
if mod_res.is_none() {
return;
}
let version_res = ModVersion::get_one(&path.id, &path.version, true, false, &mut pool).await;
if version_res.is_err() {
return;
}
create_or_update_thread(
None,
&data.discord(),
&mod_res.unwrap(),
&version_res.unwrap(),
&dev.display_name,
&data.app_url()
).await;
});
}

Ok(HttpResponse::NoContent())
}
26 changes: 26 additions & 0 deletions src/endpoints/mods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ use crate::database::repository::developers;
use crate::database::repository::mods;
use crate::events::mod_feature::ModFeaturedEvent;
use crate::extractors::auth::Auth;
use crate::forum::discord::create_or_update_thread;
use crate::types::api::{create_download_link, ApiError, ApiResponse};
use crate::types::mod_json::ModJson;
use crate::types::models::incompatibility::Incompatibility;
use crate::types::models::mod_entity::{download_geode_file, Mod, ModUpdate};
use crate::types::models::mod_gd_version::{GDVersionEnum, VerPlatform};
use crate::types::models::mod_version::ModVersion;
use crate::types::models::mod_version_status::ModVersionStatusEnum;
use crate::webhook::discord::DiscordWebhook;
use actix_web::{get, post, put, web, HttpResponse, Responder};
Expand Down Expand Up @@ -147,6 +149,30 @@ pub async fn create(
.await
.or(Err(ApiError::TransactionError))?;

tokio::spawn(async move {
if !data.discord().is_valid() {
log::error!("Discord configuration is not set up. Not creating forum threads.");
return;
}

let mod_res = Mod::get_one(&json.id, false, &mut pool).await.ok().flatten();
if mod_res.is_none() {
return;
}
let version_res = ModVersion::get_one(&json.id, &json.version, true, false, &mut pool).await;
if version_res.is_err() {
return;
}
create_or_update_thread(
None,
&data.discord(),
&mod_res.unwrap(),
&version_res.unwrap(),
"",
&data.app_url()
).await;
});

Ok(HttpResponse::NoContent())
}

Expand Down
Loading