Apply patreon sharing across web/bot
This commit is contained in:
		@@ -233,39 +233,6 @@ pub async fn initialize(
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub async fn check_subscription(cache_http: impl CacheHttp, user_id: impl Into<UserId>) -> bool {
 | 
			
		||||
    offline!(true);
 | 
			
		||||
 | 
			
		||||
    if let Some(subscription_guild) = *CNC_GUILD {
 | 
			
		||||
        let guild_member = GuildId::new(subscription_guild).member(cache_http, user_id).await;
 | 
			
		||||
 | 
			
		||||
        if let Ok(member) = guild_member {
 | 
			
		||||
            for role in member.roles {
 | 
			
		||||
                if SUBSCRIPTION_ROLES.contains(&role.get()) {
 | 
			
		||||
                    return true;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        false
 | 
			
		||||
    } else {
 | 
			
		||||
        true
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub async fn check_guild_subscription(
 | 
			
		||||
    cache_http: impl CacheHttp,
 | 
			
		||||
    guild_id: impl Into<GuildId>,
 | 
			
		||||
) -> bool {
 | 
			
		||||
    offline!(true);
 | 
			
		||||
 | 
			
		||||
    if let Some(owner) = cache_http.cache().unwrap().guild(guild_id).map(|guild| guild.owner_id) {
 | 
			
		||||
        check_subscription(&cache_http, owner).await
 | 
			
		||||
    } else {
 | 
			
		||||
        false
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub async fn check_authorization(
 | 
			
		||||
    cookies: &CookieJar<'_>,
 | 
			
		||||
    ctx: &Context,
 | 
			
		||||
 
 | 
			
		||||
@@ -7,38 +7,43 @@ pub mod todos;
 | 
			
		||||
 | 
			
		||||
use std::env;
 | 
			
		||||
 | 
			
		||||
use crate::utils::check_subscription;
 | 
			
		||||
use crate::web::guards::transaction::Transaction;
 | 
			
		||||
use crate::web::{check_authorization, routes::JsonResult};
 | 
			
		||||
pub use channels::get_guild_channels;
 | 
			
		||||
pub use emojis::get_guild_emojis;
 | 
			
		||||
pub use reminders::*;
 | 
			
		||||
use rocket::{get, http::CookieJar, serde::json::json, State};
 | 
			
		||||
pub use roles::get_guild_roles;
 | 
			
		||||
use serenity::all::UserId;
 | 
			
		||||
use serenity::{
 | 
			
		||||
    client::Context,
 | 
			
		||||
    model::id::{GuildId, RoleId},
 | 
			
		||||
};
 | 
			
		||||
pub use templates::*;
 | 
			
		||||
 | 
			
		||||
use crate::web::{check_authorization, routes::JsonResult};
 | 
			
		||||
 | 
			
		||||
#[get("/api/guild/<id>")]
 | 
			
		||||
pub async fn get_guild_info(id: u64, cookies: &CookieJar<'_>, ctx: &State<Context>) -> JsonResult {
 | 
			
		||||
pub async fn get_guild_info(
 | 
			
		||||
    id: u64,
 | 
			
		||||
    cookies: &CookieJar<'_>,
 | 
			
		||||
    ctx: &State<Context>,
 | 
			
		||||
    mut transaction: Transaction<'_>,
 | 
			
		||||
) -> JsonResult {
 | 
			
		||||
    offline!(Ok(json!({ "patreon": true, "name": "Guild" })));
 | 
			
		||||
    check_authorization(cookies, ctx.inner(), id).await?;
 | 
			
		||||
 | 
			
		||||
    match GuildId::new(id)
 | 
			
		||||
        .to_guild_cached(ctx.inner())
 | 
			
		||||
        .map(|guild| (guild.owner_id, guild.name.clone()))
 | 
			
		||||
    {
 | 
			
		||||
        Some((owner_id, name)) => {
 | 
			
		||||
            let member_res = GuildId::new(env::var("PATREON_GUILD_ID").unwrap().parse().unwrap())
 | 
			
		||||
                .member(&ctx.inner(), owner_id)
 | 
			
		||||
                .await;
 | 
			
		||||
    let user_id =
 | 
			
		||||
        cookies.get_private("userid").map(|c| c.value().parse::<u64>().ok()).flatten().unwrap();
 | 
			
		||||
 | 
			
		||||
            let patreon = member_res.map_or(false, |member| {
 | 
			
		||||
                member
 | 
			
		||||
                    .roles
 | 
			
		||||
                    .contains(&RoleId::new(env::var("PATREON_ROLE_ID").unwrap().parse().unwrap()))
 | 
			
		||||
            });
 | 
			
		||||
    match GuildId::new(id).to_guild_cached(ctx.inner()).map(|guild| guild.name.clone()) {
 | 
			
		||||
        Some(name) => {
 | 
			
		||||
            let patreon = check_subscription(
 | 
			
		||||
                ctx.inner(),
 | 
			
		||||
                transaction.executor(),
 | 
			
		||||
                UserId::from(user_id),
 | 
			
		||||
                Some(GuildId::from(id)),
 | 
			
		||||
            )
 | 
			
		||||
            .await;
 | 
			
		||||
 | 
			
		||||
            Ok(json!({ "patreon": patreon, "name": name }))
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -12,8 +12,9 @@ use serenity::{
 | 
			
		||||
};
 | 
			
		||||
use sqlx::{MySql, Pool};
 | 
			
		||||
 | 
			
		||||
use crate::utils::check_subscription;
 | 
			
		||||
use crate::web::{
 | 
			
		||||
    check_authorization, check_guild_subscription, check_subscription,
 | 
			
		||||
    check_authorization,
 | 
			
		||||
    consts::MIN_INTERVAL,
 | 
			
		||||
    guards::transaction::Transaction,
 | 
			
		||||
    routes::{
 | 
			
		||||
@@ -186,8 +187,13 @@ pub async fn edit_reminder(
 | 
			
		||||
        || reminder.interval_months.flatten().is_some()
 | 
			
		||||
        || reminder.interval_seconds.flatten().is_some()
 | 
			
		||||
    {
 | 
			
		||||
        if check_guild_subscription(&ctx.inner(), id).await
 | 
			
		||||
            || check_subscription(&ctx.inner(), user_id).await
 | 
			
		||||
        if check_subscription(
 | 
			
		||||
            ctx.inner(),
 | 
			
		||||
            transaction.executor(),
 | 
			
		||||
            UserId::from(user_id),
 | 
			
		||||
            Some(GuildId::from(id)),
 | 
			
		||||
        )
 | 
			
		||||
        .await
 | 
			
		||||
        {
 | 
			
		||||
            let new_interval_length = match reminder.interval_days {
 | 
			
		||||
                Some(interval) => interval.unwrap_or(0),
 | 
			
		||||
 
 | 
			
		||||
@@ -6,8 +6,8 @@ use serde::{Deserialize, Serialize};
 | 
			
		||||
use serenity::{client::Context, model::id::UserId};
 | 
			
		||||
use sqlx::types::Json;
 | 
			
		||||
 | 
			
		||||
use crate::utils::check_subscription;
 | 
			
		||||
use crate::web::{
 | 
			
		||||
    check_subscription,
 | 
			
		||||
    consts::{
 | 
			
		||||
        DAY, MAX_CONTENT_LENGTH, MAX_EMBED_AUTHOR_LENGTH, MAX_EMBED_DESCRIPTION_LENGTH,
 | 
			
		||||
        MAX_EMBED_FIELDS, MAX_EMBED_FIELD_TITLE_LENGTH, MAX_EMBED_FIELD_VALUE_LENGTH,
 | 
			
		||||
@@ -131,7 +131,7 @@ pub async fn create_reminder(
 | 
			
		||||
        || reminder.interval_days.is_some()
 | 
			
		||||
        || reminder.interval_months.is_some()
 | 
			
		||||
    {
 | 
			
		||||
        if !check_subscription(&ctx, user_id).await {
 | 
			
		||||
        if !check_subscription(&ctx, transaction.executor(), user_id, None).await {
 | 
			
		||||
            return Err(json!({"error": "Patreon is required to set intervals"}));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -9,8 +9,8 @@ use rocket::{
 | 
			
		||||
use serenity::{client::Context, model::id::UserId};
 | 
			
		||||
use sqlx::{MySql, Pool};
 | 
			
		||||
 | 
			
		||||
use crate::utils::check_subscription;
 | 
			
		||||
use crate::web::{
 | 
			
		||||
    check_subscription,
 | 
			
		||||
    guards::transaction::Transaction,
 | 
			
		||||
    routes::{
 | 
			
		||||
        dashboard::{
 | 
			
		||||
@@ -162,7 +162,9 @@ pub async fn edit_reminder(
 | 
			
		||||
        || reminder.interval_months.flatten().is_some()
 | 
			
		||||
        || reminder.interval_seconds.flatten().is_some()
 | 
			
		||||
    {
 | 
			
		||||
        if check_subscription(&ctx.inner(), user_id).await {
 | 
			
		||||
        if check_subscription(&ctx.inner(), transaction.executor(), UserId::from(user_id), None)
 | 
			
		||||
            .await
 | 
			
		||||
        {
 | 
			
		||||
            let new_interval_length = match reminder.interval_days {
 | 
			
		||||
                Some(interval) => interval.unwrap_or(0),
 | 
			
		||||
                None => sqlx::query!(
 | 
			
		||||
 
 | 
			
		||||
@@ -20,9 +20,9 @@ use serenity::{
 | 
			
		||||
use sqlx::types::Json;
 | 
			
		||||
use sqlx::FromRow;
 | 
			
		||||
 | 
			
		||||
use crate::utils::check_subscription;
 | 
			
		||||
use crate::web::{
 | 
			
		||||
    catchers::internal_server_error,
 | 
			
		||||
    check_guild_subscription, check_subscription,
 | 
			
		||||
    consts::{
 | 
			
		||||
        CHARACTERS, DAY, DEFAULT_AVATAR, MAX_CONTENT_LENGTH, MAX_EMBED_AUTHOR_LENGTH,
 | 
			
		||||
        MAX_EMBED_DESCRIPTION_LENGTH, MAX_EMBED_FIELDS, MAX_EMBED_FIELD_TITLE_LENGTH,
 | 
			
		||||
@@ -477,9 +477,7 @@ pub(crate) async fn create_reminder(
 | 
			
		||||
        || reminder.interval_days.is_some()
 | 
			
		||||
        || reminder.interval_months.is_some()
 | 
			
		||||
    {
 | 
			
		||||
        if !check_guild_subscription(&ctx, guild_id).await
 | 
			
		||||
            && !check_subscription(&ctx, user_id).await
 | 
			
		||||
        {
 | 
			
		||||
        if !check_subscription(&ctx, transaction.executor(), user_id, Some(guild_id)).await {
 | 
			
		||||
            return Err(json!({"error": "Patreon is required to set intervals"}));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user