New Feature: Add server-side channel unreads.
This commit is contained in:
53
src/routes/channels/channel_ack.rs
Normal file
53
src/routes/channels/channel_ack.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
use crate::notifications::events::ClientboundNotification;
|
||||
use crate::util::result::{Error, Result};
|
||||
use crate::database::*;
|
||||
|
||||
use mongodb::bson::doc;
|
||||
use mongodb::options::UpdateOptions;
|
||||
|
||||
#[put("/<target>/ack/<message>")]
|
||||
pub async fn req(user: User, target: Ref, message: Ref) -> Result<()> {
|
||||
let target = target.fetch_channel().await?;
|
||||
|
||||
let perm = permissions::PermissionCalculator::new(&user)
|
||||
.with_channel(&target)
|
||||
.for_channel()
|
||||
.await?;
|
||||
|
||||
if !perm.get_view() {
|
||||
Err(Error::MissingPermission)?
|
||||
}
|
||||
|
||||
let id = target.id();
|
||||
get_collection("channel_unreads")
|
||||
.update_one(
|
||||
doc! {
|
||||
"_id.channel": id,
|
||||
"_id.user": &user.id
|
||||
},
|
||||
doc! {
|
||||
"$unset": {
|
||||
"mentions": 1
|
||||
},
|
||||
"$set": {
|
||||
"last_id": &message.id
|
||||
}
|
||||
},
|
||||
UpdateOptions::builder()
|
||||
.upsert(true)
|
||||
.build()
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "update_one",
|
||||
with: "channel_unreads",
|
||||
})?;
|
||||
|
||||
ClientboundNotification::ChannelAck {
|
||||
id: id.to_string(),
|
||||
user: user.id.clone(),
|
||||
message_id: message.id
|
||||
}.publish(user.id);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
use rocket::Route;
|
||||
|
||||
mod channel_ack;
|
||||
mod delete_channel;
|
||||
mod edit_channel;
|
||||
mod fetch_channel;
|
||||
@@ -18,6 +19,7 @@ mod message_send;
|
||||
|
||||
pub fn routes() -> Vec<Route> {
|
||||
routes![
|
||||
channel_ack::req,
|
||||
fetch_channel::req,
|
||||
fetch_members::req,
|
||||
delete_channel::req,
|
||||
|
||||
10
src/routes/sync/get_unreads.rs
Normal file
10
src/routes/sync/get_unreads.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
use crate::database::*;
|
||||
use crate::util::result::Result;
|
||||
|
||||
use rocket_contrib::json::JsonValue;
|
||||
use mongodb::bson::doc;
|
||||
|
||||
#[get("/unreads")]
|
||||
pub async fn req(user: User) -> Result<JsonValue> {
|
||||
Ok(json!(user.fetch_unreads().await?))
|
||||
}
|
||||
@@ -2,7 +2,8 @@ use rocket::Route;
|
||||
|
||||
mod get_settings;
|
||||
mod set_settings;
|
||||
mod get_unreads;
|
||||
|
||||
pub fn routes() -> Vec<Route> {
|
||||
routes![get_settings::req, set_settings::req]
|
||||
routes![get_settings::req, set_settings::req, get_unreads::req]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user