Database: Add user settings sync entity.

Run cargo fmt.
This commit is contained in:
Paul
2021-05-26 11:02:16 +01:00
parent 7aad88ad3f
commit 5aa9624d5c
13 changed files with 67 additions and 41 deletions

View File

@@ -1,4 +1,4 @@
use crate::util::variables::{VAPID_PRIVATE_KEY, USE_JANUARY}; use crate::util::variables::{USE_JANUARY, VAPID_PRIVATE_KEY};
use crate::{ use crate::{
database::*, database::*,
notifications::{events::ClientboundNotification, websocket::is_online}, notifications::{events::ClientboundNotification, websocket::is_online},
@@ -297,12 +297,13 @@ impl Message {
let channel = self.channel.clone(); let channel = self.channel.clone();
ClientboundNotification::MessageDelete { ClientboundNotification::MessageDelete {
id: self.id.clone(), id: self.id.clone(),
channel: self.channel.clone() channel: self.channel.clone(),
} }
.publish(channel); .publish(channel);
if let Some(attachments) = &self.attachments { if let Some(attachments) = &self.attachments {
let attachment_ids: Vec<String> = attachments.iter().map(|f| f.id.to_string()).collect(); let attachment_ids: Vec<String> =
attachments.iter().map(|f| f.id.to_string()).collect();
get_collection("attachments") get_collection("attachments")
.update_one( .update_one(
doc! { doc! {

View File

@@ -0,0 +1,2 @@
pub mod autumn;
pub mod january;

View File

@@ -1,9 +1,11 @@
mod autumn; mod microservice;
mod channel; mod channel;
mod january;
mod message; mod message;
mod server; mod server;
mod user; mod user;
mod sync;
use microservice::*;
pub use autumn::*; pub use autumn::*;
pub use channel::*; pub use channel::*;
@@ -11,3 +13,4 @@ pub use january::*;
pub use message::*; pub use message::*;
pub use server::*; pub use server::*;
pub use user::*; pub use user::*;
pub use sync::*;

View File

@@ -0,0 +1,3 @@
use std::collections::HashMap;
pub type UserSettings = HashMap<String, (i64, String)>;

View File

@@ -4,11 +4,11 @@ use mongodb::{
bson::{doc, from_document}, bson::{doc, from_document},
options::FindOptions, options::FindOptions,
}; };
use serde::{Deserialize, Serialize};
use validator::Validate;
use num_enum::TryFromPrimitive; use num_enum::TryFromPrimitive;
use ulid::Ulid; use serde::{Deserialize, Serialize};
use std::ops; use std::ops;
use ulid::Ulid;
use validator::Validate;
use crate::database::permissions::user::UserPermissions; use crate::database::permissions::user::UserPermissions;
use crate::database::*; use crate::database::*;
@@ -65,7 +65,7 @@ pub enum Badges {
Developer = 1, Developer = 1,
Translator = 2, Translator = 2,
Supporter = 4, Supporter = 4,
EarlyAdopter = 256 EarlyAdopter = 256,
} }
impl_op_ex_commutative!(+ |a: &i32, b: &Badges| -> i32 { *a | *b as i32 }); impl_op_ex_commutative!(+ |a: &i32, b: &Badges| -> i32 { *a | *b as i32 });

View File

@@ -1,9 +1,12 @@
use crate::database::{get_collection, get_db}; use crate::database::{get_collection, get_db};
use log::info;
use futures::StreamExt; use futures::StreamExt;
use log::info;
use mongodb::{
bson::{doc, from_document},
options::FindOptions,
};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use mongodb::{bson::{doc, from_document}, options::FindOptions};
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct MigrationInfo { struct MigrationInfo {
@@ -92,34 +95,36 @@ pub async fn run_migrations(revision: i32) -> i32 {
info!("Running migration [revision 3 / 2021-05-25]: Support multiple file uploads, add channel_unreads and user_settings."); info!("Running migration [revision 3 / 2021-05-25]: Support multiple file uploads, add channel_unreads and user_settings.");
let messages = get_collection("messages"); let messages = get_collection("messages");
let mut cursor = messages.find( let mut cursor = messages
doc! { .find(
"attachment": { doc! {
"$exists": 1 "attachment": {
} "$exists": 1
}, }
FindOptions::builder() },
.projection(doc! { FindOptions::builder()
"_id": 1, .projection(doc! {
"attachments": [ "$attachment" ] "_id": 1,
}) "attachments": [ "$attachment" ]
.build() })
) .build(),
.await )
.expect("Failed to fetch messages."); .await
.expect("Failed to fetch messages.");
while let Some(result) = cursor.next().await { while let Some(result) = cursor.next().await {
let doc = result.unwrap(); let doc = result.unwrap();
let id = doc.get_str("_id").unwrap(); let id = doc.get_str("_id").unwrap();
let attachments = doc.get_array("attachments").unwrap(); let attachments = doc.get_array("attachments").unwrap();
messages.update_one( messages
doc! { "_id": id }, .update_one(
doc! { "$unset": { "attachment": 1 }, "$set": { "attachments": attachments } }, doc! { "_id": id },
None doc! { "$unset": { "attachment": 1 }, "$set": { "attachments": attachments } },
) None,
.await )
.unwrap(); .await
.unwrap();
} }
get_db() get_db()

View File

@@ -111,7 +111,10 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
Message::create( Message::create(
"00000000000000000000000000".to_string(), "00000000000000000000000000".to_string(),
id.clone(), id.clone(),
Content::SystemMessage(SystemMessage::ChannelRenamed { name, by: user.id.clone() }), Content::SystemMessage(SystemMessage::ChannelRenamed {
name,
by: user.id.clone(),
}),
) )
.publish(&target) .publish(&target)
.await .await
@@ -122,7 +125,9 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
Message::create( Message::create(
"00000000000000000000000000".to_string(), "00000000000000000000000000".to_string(),
id.clone(), id.clone(),
Content::SystemMessage(SystemMessage::ChannelDescriptionChanged { by: user.id.clone() }), Content::SystemMessage(SystemMessage::ChannelDescriptionChanged {
by: user.id.clone(),
}),
) )
.publish(&target) .publish(&target)
.await .await

View File

@@ -28,7 +28,7 @@ pub struct Options {
#[validate(length(min = 26, max = 26))] #[validate(length(min = 26, max = 26))]
after: Option<String>, after: Option<String>,
sort: Option<Sort>, sort: Option<Sort>,
include_users: Option<bool> include_users: Option<bool>,
} }
#[get("/<target>/messages?<options..>")] #[get("/<target>/messages?<options..>")]

View File

@@ -59,7 +59,9 @@ pub async fn req(user: User, target: Ref, message: Json<Data>) -> Result<JsonVal
let id = Ulid::new().to_string(); let id = Ulid::new().to_string();
let attachments = if let Some(attachment_id) = &message.attachment { let attachments = if let Some(attachment_id) = &message.attachment {
Some(vec![ File::find_and_use(attachment_id, "attachments", "message", &id).await? ]) Some(vec![
File::find_and_use(attachment_id, "attachments", "message", &id).await?,
])
} else { } else {
None None
}; };

View File

@@ -1,6 +1,7 @@
use crate::util::variables::{ use crate::util::variables::{
APP_URL, JANUARY_URL, AUTUMN_URL, DISABLE_REGISTRATION, EXTERNAL_WS_URL, HCAPTCHA_SITEKEY, INVITE_ONLY, APP_URL, AUTUMN_URL, DISABLE_REGISTRATION, EXTERNAL_WS_URL, HCAPTCHA_SITEKEY, INVITE_ONLY,
USE_AUTUMN, USE_JANUARY, USE_EMAIL, USE_HCAPTCHA, USE_VOSO, VAPID_PUBLIC_KEY, VOSO_URL, VOSO_WS_HOST, JANUARY_URL, USE_AUTUMN, USE_EMAIL, USE_HCAPTCHA, USE_JANUARY, USE_VOSO, VAPID_PUBLIC_KEY,
VOSO_URL, VOSO_WS_HOST,
}; };
use mongodb::bson::doc; use mongodb::bson::doc;

View File

@@ -29,13 +29,17 @@ pub enum Error {
CannotEditMessage, CannotEditMessage,
EmptyMessage, EmptyMessage,
CannotRemoveYourself, CannotRemoveYourself,
GroupTooLarge { max: usize }, GroupTooLarge {
max: usize,
},
AlreadyInGroup, AlreadyInGroup,
NotInGroup, NotInGroup,
// ? General errors. // ? General errors.
TooManyIds, TooManyIds,
FailedValidation { error: ValidationErrors }, FailedValidation {
error: ValidationErrors,
},
DatabaseError { DatabaseError {
operation: &'static str, operation: &'static str,
with: &'static str, with: &'static str,