opt: use macro for sql updates, and merge updates
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
use crate::api::post::ps2outputs;
|
||||
use crate::api::{CurrentUser, JsonAPI, PolicyError::*, UGC};
|
||||
use crate::db_conn::Db;
|
||||
use crate::libs::diesel_logger::LoggingConnection;
|
||||
use crate::models::*;
|
||||
use crate::rds_conn::RdsConn;
|
||||
use crate::rds_models::*;
|
||||
use crate::schema;
|
||||
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl};
|
||||
use rocket::form::Form;
|
||||
use rocket::serde::json::json;
|
||||
|
||||
@@ -37,10 +40,15 @@ pub async fn attention_post(
|
||||
att.remove(ai.pid).await?;
|
||||
delta = -1;
|
||||
}
|
||||
p.change_n_attentions(&db, delta).await?;
|
||||
p.change_hot_score(&db, delta * 2).await?;
|
||||
update!(
|
||||
p,
|
||||
posts,
|
||||
&db,
|
||||
{ n_attentions, add delta },
|
||||
{ hot_score, add delta * 2 }
|
||||
);
|
||||
if switch_to && user.is_admin {
|
||||
p.set_is_reported(&db, false).await?;
|
||||
update!(p, posts, &db, { is_reported, to false });
|
||||
}
|
||||
p.refresh_cache(&rconn, false).await;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
use crate::api::{APIError, CurrentUser, JsonAPI, PolicyError::*, UGC};
|
||||
use crate::db_conn::Db;
|
||||
use crate::libs::diesel_logger::LoggingConnection;
|
||||
use crate::models::*;
|
||||
use crate::rds_conn::RdsConn;
|
||||
use crate::rds_models::*;
|
||||
use crate::schema;
|
||||
use chrono::{offset::Utc, DateTime};
|
||||
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl};
|
||||
use rocket::form::Form;
|
||||
use rocket::futures::{future::TryFutureExt, join, try_join};
|
||||
use rocket::futures::join;
|
||||
use rocket::serde::{json::json, Serialize};
|
||||
use std::collections::HashMap;
|
||||
|
||||
@@ -100,26 +103,30 @@ pub async fn add_comment(
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
p.change_n_comments(&db, 1).await?;
|
||||
p.set_last_comment_time(&db, c.create_time).await?;
|
||||
// auto attention after comment
|
||||
let mut att = Attention::init(&user.namehash, &rconn);
|
||||
|
||||
let mut hs_delta = 1;
|
||||
let mut att = Attention::init(&user.namehash, &rconn);
|
||||
let hs_delta;
|
||||
let at_delta;
|
||||
|
||||
if !att.has(p.id).await? {
|
||||
hs_delta += 2;
|
||||
try_join!(
|
||||
att.add(p.id).err_into::<APIError>(),
|
||||
async {
|
||||
p.change_n_attentions(&db, 1).await?;
|
||||
Ok::<(), APIError>(())
|
||||
}
|
||||
.err_into::<APIError>(),
|
||||
)?;
|
||||
hs_delta = 3;
|
||||
at_delta = 1;
|
||||
att.add(p.id).await?;
|
||||
} else {
|
||||
hs_delta = 1;
|
||||
at_delta = 0;
|
||||
}
|
||||
|
||||
p.change_hot_score(&db, hs_delta).await?;
|
||||
update!(
|
||||
p,
|
||||
posts,
|
||||
&db,
|
||||
{ n_comments, add 1 },
|
||||
{ last_comment_time, to c.create_time },
|
||||
{ n_attentions, add at_delta },
|
||||
{ hot_score, add hs_delta }
|
||||
);
|
||||
|
||||
join!(
|
||||
p.refresh_cache(&rconn, false),
|
||||
p.clear_comments_cache(&rconn),
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
use crate::db_conn::Db;
|
||||
use crate::libs::diesel_logger::LoggingConnection;
|
||||
use crate::models::*;
|
||||
use crate::random_hasher::RandomHasher;
|
||||
use crate::rds_conn::RdsConn;
|
||||
use crate::rds_models::BannedUsers;
|
||||
use crate::schema;
|
||||
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl};
|
||||
use rocket::http::Status;
|
||||
use rocket::outcome::try_outcome;
|
||||
use rocket::request::{FromRequest, Outcome, Request};
|
||||
@@ -180,7 +183,8 @@ impl UGC for Post {
|
||||
self.n_comments == 0
|
||||
}
|
||||
async fn do_set_deleted(&mut self, db: &Db) -> API<()> {
|
||||
self.set_is_deleted(db, true).await.map_err(From::from)
|
||||
update!(*self, posts, db, { is_deleted, to true });
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -199,7 +203,8 @@ impl UGC for Comment {
|
||||
true
|
||||
}
|
||||
async fn do_set_deleted(&mut self, db: &Db) -> API<()> {
|
||||
self.set_is_deleted(db, true).await.map_err(From::from)
|
||||
update!(*self, comments, db, { is_deleted, to true });
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,9 @@ use crate::rds_models::*;
|
||||
use chrono::offset::Local;
|
||||
use rocket::form::Form;
|
||||
use rocket::serde::json::json;
|
||||
use crate::libs::diesel_logger::LoggingConnection;
|
||||
use crate::schema;
|
||||
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl};
|
||||
|
||||
#[derive(FromForm)]
|
||||
pub struct DeleteInput {
|
||||
@@ -25,8 +28,13 @@ pub async fn delete(di: Form<DeleteInput>, user: CurrentUser, db: Db, rconn: Rds
|
||||
c = Comment::get(&db, di.id).await?;
|
||||
c.soft_delete(&user, &db).await?;
|
||||
p = Post::get(&db, &rconn, c.post_id).await?;
|
||||
p.change_n_comments(&db, -1).await?;
|
||||
p.change_hot_score(&db, -1).await?;
|
||||
update!(
|
||||
p,
|
||||
posts,
|
||||
&db,
|
||||
{ n_comments, add -1 },
|
||||
{ hot_score, add -1 }
|
||||
);
|
||||
|
||||
p.refresh_cache(&rconn, false).await;
|
||||
p.clear_comments_cache(&rconn).await;
|
||||
@@ -86,7 +94,7 @@ pub async fn report(ri: Form<ReportInput>, user: CurrentUser, db: Db, rconn: Rds
|
||||
user.id.ok_or_else(|| NotAllowed)?;
|
||||
|
||||
let mut p = Post::get(&db, &rconn, ri.pid).await?;
|
||||
p.set_is_reported(&db, true).await?;
|
||||
update!(p, posts, &db, { is_reported, to true });
|
||||
p.refresh_cache(&rconn, false).await;
|
||||
Systemlog {
|
||||
user_hash: user.namehash,
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
use crate::api::comment::{c2output, CommentOutput};
|
||||
use crate::api::{CurrentUser, JsonAPI, UGC};
|
||||
use crate::db_conn::Db;
|
||||
use crate::libs::diesel_logger::LoggingConnection;
|
||||
use crate::models::*;
|
||||
use crate::rds_conn::RdsConn;
|
||||
use crate::rds_models::*;
|
||||
use crate::schema;
|
||||
use chrono::{offset::Utc, DateTime};
|
||||
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl};
|
||||
use rocket::form::Form;
|
||||
use rocket::futures::future;
|
||||
use rocket::serde::{json::json, Serialize};
|
||||
@@ -159,7 +162,7 @@ pub async fn publish_post(
|
||||
pub async fn edit_cw(cwi: Form<CwInput>, user: CurrentUser, db: Db, rconn: RdsConn) -> JsonAPI {
|
||||
let mut p = Post::get(&db, &rconn, cwi.pid).await?;
|
||||
p.check_permission(&user, "w")?;
|
||||
p.set_cw(&db, cwi.cw.to_string()).await?;
|
||||
update!(p, posts, &db, { cw, to cwi.cw.to_string() });
|
||||
p.refresh_cache(&rconn, false).await;
|
||||
Ok(json!({"code": 0}))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user