Initial work on site moderation improvements (#410)

This commit is contained in:
wafflecoffee
2022-08-03 02:31:56 -04:00
committed by GitHub
parent 916da16523
commit 411b8e3cb6
6 changed files with 105 additions and 3 deletions

View File

@@ -2,7 +2,7 @@ use super::ApiError;
use crate::database;
use crate::models::projects::ProjectStatus;
use crate::util::auth::check_is_moderator_from_headers;
use actix_web::{get, web, HttpRequest, HttpResponse};
use actix_web::{delete, get, web, HttpRequest, HttpResponse};
use serde::Deserialize;
use sqlx::PgPool;
@@ -54,3 +54,38 @@ pub async fn get_projects(
Ok(HttpResponse::Ok().json(projects))
}
#[derive(Deserialize)]
pub struct BanUser {
pub id: i64,
}
#[get("ban")]
pub async fn ban_user(
req: HttpRequest,
pool: web::Data<PgPool>,
id: web::Query<BanUser>,
) -> Result<HttpResponse, ApiError> {
check_is_moderator_from_headers(req.headers(), &**pool).await?;
sqlx::query!("INSERT INTO banned_users (github_id) VALUES ($1);", id.id)
.execute(&**pool)
.await?;
Ok(HttpResponse::NoContent().body(""))
}
#[delete("ban")]
pub async fn unban_user(
req: HttpRequest,
pool: web::Data<PgPool>,
id: web::Query<BanUser>,
) -> Result<HttpResponse, ApiError> {
check_is_moderator_from_headers(req.headers(), &**pool).await?;
sqlx::query!("DELETE FROM banned_users WHERE github_id = $1;", id.id)
.execute(&**pool)
.await?;
Ok(HttpResponse::NoContent().body(""))
}