-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor bot metadata handling and add votes table (#113)
- Loading branch information
Showing
15 changed files
with
293 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// build.rs | ||
use std::env; | ||
use std::fs; | ||
use std::path::Path; | ||
|
||
fn main() { | ||
let version = env::var("CARGO_PKG_VERSION").unwrap(); | ||
let out_dir = env::var("OUT_DIR").unwrap(); | ||
let dest_path = Path::new(&out_dir).join("version.rs"); | ||
fs::write( | ||
dest_path, | ||
format!("pub const VERSION: &str = \"{}\";", version), | ||
) | ||
.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- This file should undo anything in `up.sql` | ||
DROP TABLE IF EXISTS bot_votes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CREATE TABLE bot_votes | ||
( | ||
bot_id VARCHAR NOT NULL REFERENCES bots (id) ON DELETE CASCADE, | ||
date DATE DEFAULT CURRENT_DATE NOT NULL, | ||
votes INTEGER DEFAULT 1 NOT NULL, | ||
PRIMARY KEY (bot_id, date) | ||
); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod manage; | ||
pub mod metadata; | ||
pub mod owners; | ||
pub mod votes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,20 @@ | ||
use crate::{ | ||
app::AppState, | ||
models::Bot, | ||
task::spawn_blocking, | ||
util::errors::{bot_not_found, AppResult}, | ||
views::EncodableBotOwner, | ||
}; | ||
use crate::{app::AppState, models::Bot, util::errors::AppResult, views::EncodableBotOwner}; | ||
use axum::{extract::Path, Json}; | ||
use diesel::OptionalExtension; | ||
use diesel_async::async_connection_wrapper::AsyncConnectionWrapper; | ||
use serde_json::Value; | ||
|
||
/// Handles `GET /bots/:bot_id/owners` requests. | ||
pub async fn owners(state: AppState, Path(id): Path<String>) -> AppResult<Json<Value>> { | ||
let conn = state.db_read().await?; | ||
spawn_blocking(move || { | ||
let conn: &mut AsyncConnectionWrapper<_> = &mut conn.into(); | ||
let id = id.as_str(); | ||
let mut conn = state.db_read().await?; | ||
let id = id.as_str(); | ||
|
||
let bot: Bot = Bot::find(conn, id) | ||
.optional()? | ||
.ok_or_else(|| bot_not_found(id))?; | ||
let bot: Bot = Bot::find(&mut conn, id).await?; | ||
|
||
let owners = bot | ||
.owners(conn)? | ||
.into_iter() | ||
.map(|owner| owner.into()) | ||
.collect::<Vec<EncodableBotOwner>>(); | ||
let owners = bot | ||
.owners(&mut conn) | ||
.await? | ||
.into_iter() | ||
.map(|owner| owner.into()) | ||
.collect::<Vec<EncodableBotOwner>>(); | ||
|
||
Ok(Json(json!({ "users": owners }))) | ||
}) | ||
.await | ||
Ok(Json(json!({ "users": owners }))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use crate::auth::AuthCheck; | ||
use crate::models::vote::NewBotVote; | ||
use crate::task::spawn_blocking; | ||
use crate::util::errors::bot_not_found; | ||
use crate::views::EncodableBotVote; | ||
use crate::{ | ||
app::AppState, | ||
models::{Bot, BotVote}, | ||
schema::bot_votes, | ||
util::errors::AppResult, | ||
}; | ||
use axum::extract::Path; | ||
use axum::http::request::Parts; | ||
use axum::Json; | ||
use diesel_async::async_connection_wrapper::AsyncConnectionWrapper; | ||
use serde_json::Value; | ||
|
||
/// Handles the `GET /bots/:bot_id/votes` route. | ||
pub async fn votes(app: AppState, Path(bot_id): Path<String>) -> AppResult<Json<Value>> { | ||
let mut conn = app.db_read().await?; | ||
|
||
use diesel::dsl::*; | ||
use diesel::prelude::*; | ||
use diesel::sql_types::BigInt; | ||
use diesel_async::RunQueryDsl; | ||
|
||
let bot = Bot::find(&mut conn, bot_id.as_str()).await?; | ||
|
||
// last 90 votes | ||
let votes = BotVote::belonging_to(&bot) | ||
.filter(bot_votes::date.gt(date(now - 90.days()))) | ||
.order((bot_votes::date.asc(), bot_votes::bot_id.desc())) | ||
.load(&mut conn) | ||
.await? | ||
.into_iter() | ||
.map(BotVote::into) | ||
.collect::<Vec<EncodableBotVote>>(); | ||
|
||
let sum_votes = sql::<BigInt>("SUM(bot_votes.votes)"); | ||
let total_votes: i64 = BotVote::belonging_to(&bot) | ||
.select(sum_votes) | ||
.get_result(&mut conn) | ||
.await?; | ||
|
||
Ok(Json(json!({ | ||
"bot_votes": votes, | ||
"extra": { | ||
"total_votes": total_votes, | ||
} | ||
}))) | ||
} | ||
|
||
// TODO: 12h user ratelimit | ||
/// Handles the `POST /bots/:bot_id/votes` route. | ||
pub async fn vote( | ||
app: AppState, | ||
Path(bot_id): Path<String>, | ||
req: Parts, | ||
) -> AppResult<Json<EncodableBotVote>> { | ||
let conn = app.db_write().await?; | ||
use diesel::OptionalExtension; | ||
use diesel::RunQueryDsl; | ||
|
||
spawn_blocking(move || { | ||
let conn: &mut AsyncConnectionWrapper<_> = &mut conn.into(); | ||
|
||
// Make sure user is logged in | ||
let _ = AuthCheck::only_cookie().check(&req, conn)?.user_id(); | ||
let bot_id = bot_id.as_str(); | ||
|
||
// Check if bot exists | ||
let _: Bot = Bot::by_id(bot_id) | ||
.first(conn) | ||
.optional()? | ||
.ok_or_else(|| bot_not_found(bot_id))?; | ||
|
||
let new_vote = NewBotVote::new(bot_id); | ||
let vote = new_vote.create(conn)?; | ||
|
||
Ok(Json(EncodableBotVote::from(vote))) | ||
}) | ||
.await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use crate::diesel::ExpressionMethods; | ||
use crate::models::util::diesel::Conn; | ||
use crate::models::Bot; | ||
use crate::schema::bot_votes; | ||
use chrono::NaiveDate; | ||
use diesel::RunQueryDsl; | ||
use diesel::{QueryResult, SelectableHelper}; | ||
|
||
#[derive(Queryable, Identifiable, Associations, Selectable, Debug, Clone)] | ||
#[diesel(primary_key(bot_id, date), belongs_to(Bot))] | ||
pub struct BotVote { | ||
pub bot_id: String, | ||
pub date: NaiveDate, | ||
pub votes: i32, | ||
} | ||
|
||
#[derive(Insertable, Debug, Clone)] | ||
#[diesel( | ||
table_name = bot_votes, | ||
check_for_backend(diesel::pg::Pg), | ||
)] | ||
pub struct NewBotVote<'a> { | ||
pub bot_id: &'a str, | ||
} | ||
|
||
impl<'a> NewBotVote<'a> { | ||
pub fn new(bot_id: &'a str) -> NewBotVote { | ||
Self { bot_id } | ||
} | ||
|
||
pub fn create(&self, conn: &mut impl Conn) -> QueryResult<BotVote> { | ||
conn.transaction(|conn| { | ||
use crate::schema::bot_votes::dsl::*; | ||
|
||
let vote: BotVote = diesel::insert_into(bot_votes) | ||
.values(self) | ||
.on_conflict((bot_id, date)) | ||
.do_update() | ||
.set(votes.eq(votes + 1)) | ||
.returning(BotVote::as_returning()) | ||
.get_result(conn)?; | ||
|
||
Ok(vote) | ||
}) | ||
} | ||
} |
Oops, something went wrong.