Browse Source

rocket 0.5-rc2 & rust 1.64

master
hole-thu 3 years ago
parent
commit
791d1f526d
  1. 9
      Cargo.toml
  2. 2
      rust-toolchain
  3. 2
      src/api/attention.rs
  4. 3
      src/api/comment.rs
  5. 2
      src/api/mod.rs
  6. 4
      src/api/operation.rs
  7. 10
      src/api/post.rs
  8. 2
      src/api/vote.rs
  9. 2
      src/cache.rs
  10. 10
      src/cors.rs
  11. 2
      src/login.rs
  12. 11
      src/main.rs

9
Cargo.toml

@ -11,14 +11,10 @@ default = ["mastlogin"]
mastlogin = ["reqwest"]
[dependencies]
rocket = { version = "=0.5.0-rc.1", features = ["json"] }
rocket_codegen = "=0.5.0-rc.1"
rocket_http = "=0.5.0-rc.1"
rocket_sync_db_pools_codegen = "=0.1.0-rc.1"
rocket_sync_db_pools = { version = "=0.1.0-rc.1", features = ["diesel_postgres_pool"] }
rocket = { version = "=0.5.0-rc.2", features = ["json"] }
rocket_sync_db_pools = { version = "=0.1.0-rc.2", features = ["diesel_postgres_pool"] }
diesel = { version = "1.4.8", features = ["postgres", "chrono"] }
diesel_migrations = "1.4.0"
tokio = "1.17.0"
redis = { version="0.21.5", features = ["aio", "tokio-comp"] }
chrono = { version="0.4.19", features = ["serde"] }
rand = "0.8.5"
@ -28,5 +24,6 @@ log = "0.4.16"
env_logger = "0.9.0"
web-push = "0.9.2"
url = "2.2.2"
futures = "0.3.24"
reqwest = { version = "0.11.10", features = ["json"], optional = true }

2
rust-toolchain

@ -1 +1 @@
stable
1.64

2
src/api/attention.rs

@ -108,7 +108,7 @@ pub async fn set_notification(pid: i32, ni: Form<NotificatinInput>, _user: Curre
.ok_or(UnknownPushEndpoint)?
.to_string();
(url_host.ends_with("googleapis.com") || url_host.ends_with("mozilla.com"))
.then(|| ())
.then_some(())
.ok_or(UnknownPushEndpoint)?;
if ni.enable {

3
src/api/comment.rs

@ -7,9 +7,8 @@ use crate::rds_conn::RdsConn;
use crate::rds_models::*;
use crate::schema;
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl};
use futures::{future, join};
use rocket::form::Form;
use rocket::futures::future;
use rocket::futures::join;
use rocket::serde::{json::json, Serialize};
use std::collections::HashMap;

2
src/api/mod.rs

@ -1,3 +1,5 @@
#![allow(clippy::unnecessary_lazy_evaluations)]
use crate::db_conn::Db;
use crate::libs::diesel_logger::LoggingConnection;
use crate::models::*;

4
src/api/operation.rs

@ -108,10 +108,10 @@ pub async fn report(ri: Form<ReportInput>, user: CurrentUser, db: Db, rconn: Rds
.await?
.unwrap_or(0)
< 10)
.then(|| ())
.then_some(())
.ok_or(NotAllowed)?;
(!ri.reason.is_empty()).then(|| ()).ok_or(NoReason)?;
(!ri.reason.is_empty()).then_some(()).ok_or(NoReason)?;
let mut p = Post::get(&db, &rconn, ri.pid).await?;
if ri.should_hide.is_some() {

10
src/api/post.rs

@ -87,23 +87,23 @@ async fn p2output(p: &Post, user: &CurrentUser, db: &Db, rconn: &RdsConn) -> Api
Ok(PostOutput {
pid: p.id,
room_id: p.room_id,
text: can_view.then(|| p.content.clone()).unwrap_or_default(),
cw: (!p.cw.is_empty()).then(|| p.cw.clone()),
text: can_view.then_some(p.content.clone()).unwrap_or_default(),
cw: (!p.cw.is_empty()).then_some(p.cw.clone()),
n_attentions: p.n_attentions,
n_comments: p.n_comments,
create_time: p.create_time.timestamp(),
last_comment_time: p.last_comment_time.timestamp(),
allow_search: p.allow_search,
author_title: (!p.author_title.is_empty()).then(|| p.author_title.clone()),
author_title: (!p.author_title.is_empty()).then_some(p.author_title.clone()),
is_tmp: p.is_tmp,
is_reported: user.is_admin.then(|| p.is_reported),
is_reported: user.is_admin.then_some(p.is_reported),
comments: OptionFuture::from(
comments.map(|cs| async move { c2output(p, &cs, user, &cached_block_dict).await }),
)
.await,
can_del: p.check_permission(user, "wd").is_ok(),
attention: Attention::init(&user.namehash, rconn).has(p.id).await?,
hot_score: user.is_admin.then(|| p.hot_score),
hot_score: user.is_admin.then_some(p.hot_score),
is_blocked,
/*
blocked_count: if user.is_admin {

2
src/api/vote.rs

@ -18,7 +18,7 @@ pub async fn get_poll_dict(pid: i32, rconn: &RdsConn, namehash: &str) -> Option<
.has(namehash)
.await
.unwrap_or_default()
.then(|| opt)
.then_some(opt)
}))
.await
.into_iter()

2
src/cache.rs

@ -355,7 +355,7 @@ impl BlockDictCache {
let missing: Vec<(String, bool)> =
future::try_join_all(hash_list.iter().filter_map(|hash| {
(!block_dict.contains_key(&hash.to_string())).then(|| async {
(!block_dict.contains_key(&hash.to_string())).then_some(async {
Ok::<(String, bool), RedisError>((
hash.to_string(),
BlockedUsers::check_if_block(&self.rconn, user, hash).await?,

10
src/cors.rs

@ -19,11 +19,11 @@ impl Fairing for Cors {
}
async fn on_response<'r>(&self, request: &'r Request<'_>, response: &mut Response<'r>) {
if let Some(origin) = request
.headers()
.get_one("Origin")
.and_then(|origin| self.whitelist.contains(&origin.to_string()).then(|| origin))
{
if let Some(origin) = request.headers().get_one("Origin").and_then(|origin| {
self.whitelist
.contains(&origin.to_string())
.then_some(origin)
}) {
response.set_header(Header::new("Access-Control-Allow-Origin", origin));
response.set_header(Header::new(
"Access-Control-Allow-Methods",

2
src/login.rs

@ -1,3 +1,5 @@
#![allow(clippy::unused_unit)]
use crate::db_conn::Db;
use crate::models::User;
use rocket::request::{FromRequest, Outcome, Request};

11
src/main.rs

@ -28,17 +28,18 @@ use diesel::Connection;
use random_hasher::RandomHasher;
use rds_conn::{init_rds_client, RdsConn};
use rds_models::clear_outdate_redis_data;
use rocket::tokio;
use rocket::tokio::time::{sleep, Duration};
use std::env;
use tokio::time::{sleep, Duration};
embed_migrations!("migrations/postgres");
#[rocket::main]
async fn main() -> Result<(), rocket::Error> {
async fn main() {
load_env();
if env::args().any(|arg| arg.eq("--init-database")) {
init_database();
return Ok(());
return;
}
env_logger::init();
let rmc = init_rds_client().await;
@ -61,7 +62,7 @@ async fn main() -> Result<(), rocket::Error> {
}
});
rocket::build()
let _ = rocket::build()
.mount(
"/_api/v1",
routes![
@ -119,7 +120,7 @@ async fn main() -> Result<(), rocket::Error> {
.collect::<Vec<String>>(),
})
.launch()
.await
.await;
}
fn load_env() {

Loading…
Cancel
Save