forked from jmug/stoatchat
Add all possible notifications.
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "revolt"
|
name = "revolt"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
authors = ["Paul Makles <paulmakles@gmail.com>"]
|
authors = ["Paul Makles <paulmakles@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use super::get_collection;
|
|||||||
use crate::guards::channel::ChannelRef;
|
use crate::guards::channel::ChannelRef;
|
||||||
use crate::notifications;
|
use crate::notifications;
|
||||||
use crate::notifications::events::message::Create;
|
use crate::notifications::events::message::Create;
|
||||||
use crate::notifications::events::Notification::MessageCreate;
|
use crate::notifications::events::Notification;
|
||||||
use crate::routes::channel::ChannelType;
|
use crate::routes::channel::ChannelType;
|
||||||
|
|
||||||
use bson::{doc, to_bson, UtcDateTime};
|
use bson::{doc, to_bson, UtcDateTime};
|
||||||
@@ -25,7 +25,7 @@ pub struct Message {
|
|||||||
pub content: String,
|
pub content: String,
|
||||||
pub edited: Option<UtcDateTime>,
|
pub edited: Option<UtcDateTime>,
|
||||||
|
|
||||||
pub previous_content: Option<Vec<PreviousEntry>>,
|
pub previous_content: Vec<PreviousEntry>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// ? TODO: write global send message
|
// ? TODO: write global send message
|
||||||
@@ -37,19 +37,16 @@ impl Message {
|
|||||||
.insert_one(to_bson(&self).unwrap().as_document().unwrap().clone(), None)
|
.insert_one(to_bson(&self).unwrap().as_document().unwrap().clone(), None)
|
||||||
.is_ok()
|
.is_ok()
|
||||||
{
|
{
|
||||||
let data = MessageCreate(Create {
|
notifications::send_message_given_channel(
|
||||||
id: self.id.clone(),
|
Notification::message_create(Create {
|
||||||
nonce: self.nonce.clone(),
|
id: self.id.clone(),
|
||||||
channel: self.channel.clone(),
|
nonce: self.nonce.clone(),
|
||||||
author: self.author.clone(),
|
channel: self.channel.clone(),
|
||||||
content: self.content.clone(),
|
author: self.author.clone(),
|
||||||
});
|
content: self.content.clone(),
|
||||||
|
}),
|
||||||
match target.channel_type {
|
&target,
|
||||||
0..=1 => notifications::send_message_threaded(target.recipients.clone(), None, data),
|
);
|
||||||
2 => notifications::send_message_threaded(None, target.guild.clone(), data),
|
|
||||||
_ => unreachable!(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let short_content: String = self.content.chars().take(24).collect();
|
let short_content: String = self.content.chars().take(24).collect();
|
||||||
|
|
||||||
|
|||||||
13
src/notifications/events/groups.rs
Normal file
13
src/notifications/events/groups.rs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
|
pub struct UserJoin {
|
||||||
|
pub id: String,
|
||||||
|
pub user: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
|
pub struct UserLeave {
|
||||||
|
pub id: String,
|
||||||
|
pub user: String,
|
||||||
|
}
|
||||||
33
src/notifications/events/guilds.rs
Normal file
33
src/notifications/events/guilds.rs
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
|
pub struct UserJoin {
|
||||||
|
pub id: String,
|
||||||
|
pub user: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
|
pub struct UserLeave {
|
||||||
|
pub id: String,
|
||||||
|
pub user: String,
|
||||||
|
pub banned: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
|
pub struct ChannelCreate {
|
||||||
|
pub id: String,
|
||||||
|
pub channel: String,
|
||||||
|
pub name: String,
|
||||||
|
pub description: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
|
pub struct ChannelDelete {
|
||||||
|
pub id: String,
|
||||||
|
pub channel: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
|
pub struct Delete {
|
||||||
|
pub id: String,
|
||||||
|
}
|
||||||
@@ -8,3 +8,14 @@ pub struct Create {
|
|||||||
pub author: String,
|
pub author: String,
|
||||||
pub content: String,
|
pub content: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
|
pub struct Edit {
|
||||||
|
pub id: String,
|
||||||
|
pub content: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
|
pub struct Delete {
|
||||||
|
pub id: String,
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,11 +1,25 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::{json, Value};
|
use serde_json::{json, Value};
|
||||||
|
|
||||||
|
pub mod groups;
|
||||||
|
pub mod guilds;
|
||||||
pub mod message;
|
pub mod message;
|
||||||
|
pub mod users;
|
||||||
|
|
||||||
|
#[allow(non_camel_case_types)]
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
pub enum Notification {
|
pub enum Notification {
|
||||||
MessageCreate(message::Create),
|
message_create(message::Create),
|
||||||
|
message_edit(message::Edit),
|
||||||
|
message_delete(message::Delete),
|
||||||
|
group_user_join(groups::UserJoin),
|
||||||
|
group_user_leave(groups::UserLeave),
|
||||||
|
guild_user_join(guilds::UserJoin),
|
||||||
|
guild_user_leave(guilds::UserLeave),
|
||||||
|
guild_channel_create(guilds::ChannelCreate),
|
||||||
|
guild_channel_delete(guilds::ChannelDelete),
|
||||||
|
guild_delete(guilds::Delete),
|
||||||
|
user_friend_status(users::FriendStatus),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Notification {
|
impl Notification {
|
||||||
|
|||||||
7
src/notifications/events/users.rs
Normal file
7
src/notifications/events/users.rs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
|
pub struct FriendStatus {
|
||||||
|
pub id: String,
|
||||||
|
pub status: i32,
|
||||||
|
}
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use crate::guards::channel::ChannelRef;
|
||||||
|
|
||||||
use once_cell::sync::OnceCell;
|
use once_cell::sync::OnceCell;
|
||||||
use std::sync::mpsc::{channel, Sender};
|
use std::sync::mpsc::{channel, Sender};
|
||||||
use std::thread;
|
use std::thread;
|
||||||
@@ -62,3 +64,11 @@ pub fn send_message_threaded<U: Into<Option<Vec<String>>>, G: Into<Option<String
|
|||||||
.is_ok()
|
.is_ok()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn send_message_given_channel(data: events::Notification, channel: &ChannelRef) {
|
||||||
|
match channel.channel_type {
|
||||||
|
0..=1 => send_message_threaded(channel.recipients.clone(), None, data),
|
||||||
|
2 => send_message_threaded(None, channel.guild.clone(), data),
|
||||||
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
@@ -18,8 +18,6 @@ pub struct PubSubMessage {
|
|||||||
|
|
||||||
user_recipients: Option<Vec<String>>,
|
user_recipients: Option<Vec<String>>,
|
||||||
target_guild: Option<String>,
|
target_guild: Option<String>,
|
||||||
|
|
||||||
notification_type: String,
|
|
||||||
data: Notification,
|
data: Notification,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,10 +27,6 @@ pub fn send_message(users: Option<Vec<String>>, guild: Option<String>, data: Not
|
|||||||
source: SOURCEID.get().unwrap().to_string(),
|
source: SOURCEID.get().unwrap().to_string(),
|
||||||
user_recipients: users.into(),
|
user_recipients: users.into(),
|
||||||
target_guild: guild.into(),
|
target_guild: guild.into(),
|
||||||
notification_type: match data {
|
|
||||||
Notification::MessageCreate(_) => "message_create",
|
|
||||||
}
|
|
||||||
.to_string(),
|
|
||||||
data,
|
data,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,10 @@ use crate::database::{
|
|||||||
};
|
};
|
||||||
use crate::guards::auth::UserRef;
|
use crate::guards::auth::UserRef;
|
||||||
use crate::guards::channel::ChannelRef;
|
use crate::guards::channel::ChannelRef;
|
||||||
|
use crate::notifications::{
|
||||||
|
self,
|
||||||
|
events::{groups::*, guilds::ChannelDelete, message::*, Notification},
|
||||||
|
};
|
||||||
use crate::util::vec_to_set;
|
use crate::util::vec_to_set;
|
||||||
|
|
||||||
use bson::{doc, from_bson, Bson, Bson::UtcDatetime};
|
use bson::{doc, from_bson, Bson, Bson::UtcDatetime};
|
||||||
@@ -218,10 +222,18 @@ pub fn add_member(user: UserRef, target: ChannelRef, member: UserRef) -> Option<
|
|||||||
author: "system".to_string(),
|
author: "system".to_string(),
|
||||||
content: format!("<@{}> added <@{}> to the group.", &user.id, &member.id),
|
content: format!("<@{}> added <@{}> to the group.", &user.id, &member.id),
|
||||||
edited: None,
|
edited: None,
|
||||||
previous_content: None,
|
previous_content: vec![],
|
||||||
})
|
})
|
||||||
.send(&target)
|
.send(&target)
|
||||||
{
|
{
|
||||||
|
notifications::send_message_given_channel(
|
||||||
|
Notification::group_user_join(UserJoin {
|
||||||
|
id: target.id.clone(),
|
||||||
|
user: member.id.clone(),
|
||||||
|
}),
|
||||||
|
&target,
|
||||||
|
);
|
||||||
|
|
||||||
Some(Response::Result(super::Status::Ok))
|
Some(Response::Result(super::Status::Ok))
|
||||||
} else {
|
} else {
|
||||||
Some(Response::PartialStatus(
|
Some(Response::PartialStatus(
|
||||||
@@ -285,10 +297,18 @@ pub fn remove_member(user: UserRef, target: ChannelRef, member: UserRef) -> Opti
|
|||||||
author: "system".to_string(),
|
author: "system".to_string(),
|
||||||
content: format!("<@{}> removed <@{}> from the group.", &user.id, &member.id),
|
content: format!("<@{}> removed <@{}> from the group.", &user.id, &member.id),
|
||||||
edited: None,
|
edited: None,
|
||||||
previous_content: None,
|
previous_content: vec![],
|
||||||
})
|
})
|
||||||
.send(&target)
|
.send(&target)
|
||||||
{
|
{
|
||||||
|
notifications::send_message_given_channel(
|
||||||
|
Notification::group_user_leave(UserLeave {
|
||||||
|
id: target.id.clone(),
|
||||||
|
user: member.id.clone(),
|
||||||
|
}),
|
||||||
|
&target,
|
||||||
|
);
|
||||||
|
|
||||||
Some(Response::Result(super::Status::Ok))
|
Some(Response::Result(super::Status::Ok))
|
||||||
} else {
|
} else {
|
||||||
Some(Response::PartialStatus(
|
Some(Response::PartialStatus(
|
||||||
@@ -327,7 +347,7 @@ pub fn delete(user: UserRef, target: ChannelRef) -> Option<Response> {
|
|||||||
Some(Response::Result(super::Status::Ok))
|
Some(Response::Result(super::Status::Ok))
|
||||||
} else {
|
} else {
|
||||||
Some(Response::InternalServerError(
|
Some(Response::InternalServerError(
|
||||||
json!({ "error": "Failed to delete group." }),
|
json!({ "error": "Failed to delete channel." }),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -395,10 +415,18 @@ pub fn delete(user: UserRef, target: ChannelRef) -> Option<Response> {
|
|||||||
author: "system".to_string(),
|
author: "system".to_string(),
|
||||||
content: format!("<@{}> left the group.", &user.id),
|
content: format!("<@{}> left the group.", &user.id),
|
||||||
edited: None,
|
edited: None,
|
||||||
previous_content: None,
|
previous_content: vec![],
|
||||||
})
|
})
|
||||||
.send(&target)
|
.send(&target)
|
||||||
{
|
{
|
||||||
|
notifications::send_message_given_channel(
|
||||||
|
Notification::group_user_leave(UserLeave {
|
||||||
|
id: target.id.clone(),
|
||||||
|
user: user.id.clone(),
|
||||||
|
}),
|
||||||
|
&target,
|
||||||
|
);
|
||||||
|
|
||||||
Some(Response::Result(super::Status::Ok))
|
Some(Response::Result(super::Status::Ok))
|
||||||
} else {
|
} else {
|
||||||
Some(Response::PartialStatus(
|
Some(Response::PartialStatus(
|
||||||
@@ -413,9 +441,10 @@ pub fn delete(user: UserRef, target: ChannelRef) -> Option<Response> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
2 => {
|
2 => {
|
||||||
|
let guild_id = target.guild.unwrap();
|
||||||
if database::get_collection("guilds")
|
if database::get_collection("guilds")
|
||||||
.update_one(
|
.update_one(
|
||||||
doc! { "_id": target.guild.unwrap() },
|
doc! { "_id": &guild_id },
|
||||||
doc! {
|
doc! {
|
||||||
"$pull": {
|
"$pull": {
|
||||||
"invites": {
|
"invites": {
|
||||||
@@ -427,6 +456,15 @@ pub fn delete(user: UserRef, target: ChannelRef) -> Option<Response> {
|
|||||||
)
|
)
|
||||||
.is_ok()
|
.is_ok()
|
||||||
{
|
{
|
||||||
|
notifications::send_message_threaded(
|
||||||
|
None,
|
||||||
|
guild_id.clone(),
|
||||||
|
Notification::guild_channel_delete(ChannelDelete {
|
||||||
|
id: guild_id.clone(),
|
||||||
|
channel: target.id.clone(),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
try_delete()
|
try_delete()
|
||||||
} else {
|
} else {
|
||||||
Some(Response::InternalServerError(
|
Some(Response::InternalServerError(
|
||||||
@@ -512,7 +550,7 @@ pub fn send_message(
|
|||||||
author: user.id,
|
author: user.id,
|
||||||
content,
|
content,
|
||||||
edited: None,
|
edited: None,
|
||||||
previous_content: None,
|
previous_content: vec![],
|
||||||
};
|
};
|
||||||
|
|
||||||
if message.send(&target) {
|
if message.send(&target) {
|
||||||
@@ -533,28 +571,21 @@ pub fn get_message(user: UserRef, target: ChannelRef, message: Message) -> Optio
|
|||||||
return Some(Response::LackingPermission(Permission::ReadMessages));
|
return Some(Response::LackingPermission(Permission::ReadMessages));
|
||||||
}
|
}
|
||||||
|
|
||||||
let prev =
|
// ! CHECK IF USER HAS PERMISSION TO VIEW EDITS OF MESSAGES
|
||||||
// ! CHECK IF USER HAS PERMISSION TO VIEW EDITS OF MESSAGES
|
let mut entries = vec![];
|
||||||
if let Some(previous) = message.previous_content {
|
for entry in message.previous_content {
|
||||||
let mut entries = vec![];
|
entries.push(json!({
|
||||||
for entry in previous {
|
"content": entry.content,
|
||||||
entries.push(json!({
|
"time": entry.time.timestamp(),
|
||||||
"content": entry.content,
|
}));
|
||||||
"time": entry.time.timestamp(),
|
}
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
Some(entries)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
Some(Response::Success(json!({
|
Some(Response::Success(json!({
|
||||||
"id": message.id,
|
"id": message.id,
|
||||||
"author": message.author,
|
"author": message.author,
|
||||||
"content": message.content,
|
"content": message.content,
|
||||||
"edited": if let Some(t) = message.edited { Some(t.timestamp()) } else { None },
|
"edited": if let Some(t) = message.edited { Some(t.timestamp()) } else { None },
|
||||||
"previous_content": prev,
|
"previous_content": entries,
|
||||||
})))
|
})))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -596,7 +627,7 @@ pub fn edit_message(
|
|||||||
},
|
},
|
||||||
"$push": {
|
"$push": {
|
||||||
"previous_content": {
|
"previous_content": {
|
||||||
"content": message.content,
|
"content": &message.content,
|
||||||
"time": time,
|
"time": time,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -604,19 +635,13 @@ pub fn edit_message(
|
|||||||
None,
|
None,
|
||||||
) {
|
) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
/*websocket::queue_message(
|
notifications::send_message_given_channel(
|
||||||
get_recipients(&target),
|
Notification::message_edit(Edit {
|
||||||
json!({
|
id: message.id.clone(),
|
||||||
"type": "message_update",
|
content: message.content,
|
||||||
"data": {
|
}),
|
||||||
"id": message.id,
|
&target,
|
||||||
"channel": target.id,
|
);
|
||||||
"content": edit.content.clone(),
|
|
||||||
"edited": edited.timestamp()
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.to_string(),
|
|
||||||
);*/
|
|
||||||
|
|
||||||
Some(Response::Result(super::Status::Ok))
|
Some(Response::Result(super::Status::Ok))
|
||||||
}
|
}
|
||||||
@@ -641,17 +666,12 @@ pub fn delete_message(user: UserRef, target: ChannelRef, message: Message) -> Op
|
|||||||
|
|
||||||
match col.delete_one(doc! { "_id": &message.id }, None) {
|
match col.delete_one(doc! { "_id": &message.id }, None) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
/*websocket::queue_message(
|
notifications::send_message_given_channel(
|
||||||
get_recipients(&target),
|
Notification::message_delete(Delete {
|
||||||
json!({
|
id: message.id.clone(),
|
||||||
"type": "message_delete",
|
}),
|
||||||
"data": {
|
&target,
|
||||||
"id": message.id,
|
);
|
||||||
"channel": target.id
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.to_string(),
|
|
||||||
);*/
|
|
||||||
|
|
||||||
Some(Response::Result(super::Status::Ok))
|
Some(Response::Result(super::Status::Ok))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,10 @@ use crate::database::{self, channel::Channel, Permission, PermissionCalculator};
|
|||||||
use crate::guards::auth::UserRef;
|
use crate::guards::auth::UserRef;
|
||||||
use crate::guards::channel::ChannelRef;
|
use crate::guards::channel::ChannelRef;
|
||||||
use crate::guards::guild::{get_invite, get_member, GuildRef};
|
use crate::guards::guild::{get_invite, get_member, GuildRef};
|
||||||
|
use crate::notifications::{
|
||||||
|
self,
|
||||||
|
events::{guilds::*, Notification},
|
||||||
|
};
|
||||||
use crate::util::gen_token;
|
use crate::util::gen_token;
|
||||||
|
|
||||||
use bson::{doc, from_bson, Bson};
|
use bson::{doc, from_bson, Bson};
|
||||||
@@ -180,6 +184,14 @@ pub fn remove_guild(user: UserRef, target: GuildRef) -> Option<Response> {
|
|||||||
)
|
)
|
||||||
.is_ok()
|
.is_ok()
|
||||||
{
|
{
|
||||||
|
notifications::send_message_threaded(
|
||||||
|
None,
|
||||||
|
target.id.clone(),
|
||||||
|
Notification::guild_delete(Delete {
|
||||||
|
id: target.id.clone(),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
Some(Response::Result(super::Status::Ok))
|
Some(Response::Result(super::Status::Ok))
|
||||||
} else {
|
} else {
|
||||||
Some(Response::InternalServerError(
|
Some(Response::InternalServerError(
|
||||||
@@ -212,6 +224,16 @@ pub fn remove_guild(user: UserRef, target: GuildRef) -> Option<Response> {
|
|||||||
)
|
)
|
||||||
.is_ok()
|
.is_ok()
|
||||||
{
|
{
|
||||||
|
notifications::send_message_threaded(
|
||||||
|
None,
|
||||||
|
target.id.clone(),
|
||||||
|
Notification::guild_user_leave(UserLeave {
|
||||||
|
id: target.id.clone(),
|
||||||
|
user: user.id.clone(),
|
||||||
|
banned: false,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
Some(Response::Result(super::Status::Ok))
|
Some(Response::Result(super::Status::Ok))
|
||||||
} else {
|
} else {
|
||||||
Some(Response::InternalServerError(
|
Some(Response::InternalServerError(
|
||||||
@@ -268,13 +290,24 @@ pub fn create_channel(
|
|||||||
"nonce": &nonce,
|
"nonce": &nonce,
|
||||||
"type": 2,
|
"type": 2,
|
||||||
"guild": &target.id,
|
"guild": &target.id,
|
||||||
"name": name,
|
"name": &name,
|
||||||
"description": description,
|
"description": &description,
|
||||||
},
|
},
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
.is_ok()
|
.is_ok()
|
||||||
{
|
{
|
||||||
|
notifications::send_message_threaded(
|
||||||
|
None,
|
||||||
|
target.id.clone(),
|
||||||
|
Notification::guild_channel_create(ChannelCreate {
|
||||||
|
id: target.id.clone(),
|
||||||
|
channel: id.clone(),
|
||||||
|
name: name.clone(),
|
||||||
|
description: description.clone(),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
Some(Response::Success(json!({ "id": &id })))
|
Some(Response::Success(json!({ "id": &id })))
|
||||||
} else {
|
} else {
|
||||||
Some(Response::BadRequest(
|
Some(Response::BadRequest(
|
||||||
@@ -442,6 +475,15 @@ pub fn use_invite(user: UserRef, code: String) -> Response {
|
|||||||
)
|
)
|
||||||
.is_ok()
|
.is_ok()
|
||||||
{
|
{
|
||||||
|
notifications::send_message_threaded(
|
||||||
|
None,
|
||||||
|
guild_id.clone(),
|
||||||
|
Notification::guild_user_join(UserJoin {
|
||||||
|
id: guild_id.clone(),
|
||||||
|
user: user.id.clone(),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
Response::Success(json!({
|
Response::Success(json!({
|
||||||
"guild": &guild_id,
|
"guild": &guild_id,
|
||||||
"channel": &invite.channel,
|
"channel": &invite.channel,
|
||||||
@@ -636,6 +678,16 @@ pub fn kick_member(user: UserRef, target: GuildRef, other: String) -> Option<Res
|
|||||||
)
|
)
|
||||||
.is_ok()
|
.is_ok()
|
||||||
{
|
{
|
||||||
|
notifications::send_message_threaded(
|
||||||
|
None,
|
||||||
|
target.id.clone(),
|
||||||
|
Notification::guild_user_leave(UserLeave {
|
||||||
|
id: target.id.clone(),
|
||||||
|
user: other.clone(),
|
||||||
|
banned: false,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
Some(Response::Result(super::Status::Ok))
|
Some(Response::Result(super::Status::Ok))
|
||||||
} else {
|
} else {
|
||||||
Some(Response::InternalServerError(
|
Some(Response::InternalServerError(
|
||||||
@@ -712,6 +764,16 @@ pub fn ban_member(
|
|||||||
)
|
)
|
||||||
.is_ok()
|
.is_ok()
|
||||||
{
|
{
|
||||||
|
notifications::send_message_threaded(
|
||||||
|
None,
|
||||||
|
target.id.clone(),
|
||||||
|
Notification::guild_user_leave(UserLeave {
|
||||||
|
id: target.id.clone(),
|
||||||
|
user: other.clone(),
|
||||||
|
banned: true,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
Some(Response::Result(super::Status::Ok))
|
Some(Response::Result(super::Status::Ok))
|
||||||
} else {
|
} else {
|
||||||
Some(Response::InternalServerError(
|
Some(Response::InternalServerError(
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
use super::Response;
|
use super::Response;
|
||||||
use crate::database::{self, get_relationship, get_relationship_internal, mutual, Relationship};
|
use crate::database::{self, get_relationship, get_relationship_internal, mutual, Relationship};
|
||||||
use crate::guards::auth::UserRef;
|
use crate::guards::auth::UserRef;
|
||||||
|
use crate::notifications::{
|
||||||
|
self,
|
||||||
|
events::{users::*, Notification},
|
||||||
|
};
|
||||||
use crate::routes::channel;
|
use crate::routes::channel;
|
||||||
|
|
||||||
use bson::doc;
|
use bson::doc;
|
||||||
@@ -32,7 +36,7 @@ pub fn user(user: UserRef, target: UserRef) -> Response {
|
|||||||
Response::Success(json!({
|
Response::Success(json!({
|
||||||
"id": target.id,
|
"id": target.id,
|
||||||
"username": target.username,
|
"username": target.username,
|
||||||
"relationship": get_relationship(&user, &target) as u8,
|
"relationship": get_relationship(&user, &target) as i32,
|
||||||
"mutual": {
|
"mutual": {
|
||||||
"guilds": mutual::find_mutual_guilds(&user.id, &target.id),
|
"guilds": mutual::find_mutual_guilds(&user.id, &target.id),
|
||||||
"friends": mutual::find_mutual_friends(&user.id, &target.id),
|
"friends": mutual::find_mutual_friends(&user.id, &target.id),
|
||||||
@@ -67,7 +71,7 @@ pub fn lookup(user: UserRef, query: Json<LookupQuery>) -> Response {
|
|||||||
results.push(json!({
|
results.push(json!({
|
||||||
"id": id,
|
"id": id,
|
||||||
"username": doc.get_str("username").unwrap(),
|
"username": doc.get_str("username").unwrap(),
|
||||||
"relationship": get_relationship_internal(&user.id, &id, &relationships) as u8
|
"relationship": get_relationship_internal(&user.id, &id, &relationships) as i32
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -186,7 +190,7 @@ pub fn get_friends(user: UserRef) -> Response {
|
|||||||
/// retrieve friend status with user
|
/// retrieve friend status with user
|
||||||
#[get("/<target>/friend")]
|
#[get("/<target>/friend")]
|
||||||
pub fn get_friend(user: UserRef, target: UserRef) -> Response {
|
pub fn get_friend(user: UserRef, target: UserRef) -> Response {
|
||||||
Response::Success(json!({ "status": get_relationship(&user, &target) as u8 }))
|
Response::Success(json!({ "status": get_relationship(&user, &target) as i32 }))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// create or accept a friend request
|
/// create or accept a friend request
|
||||||
@@ -218,8 +222,8 @@ pub fn add_friend(user: UserRef, target: UserRef) -> Response {
|
|||||||
if col
|
if col
|
||||||
.update_one(
|
.update_one(
|
||||||
doc! {
|
doc! {
|
||||||
"_id": target.id,
|
"_id": target.id.clone(),
|
||||||
"relations.id": user.id
|
"relations.id": user.id.clone()
|
||||||
},
|
},
|
||||||
doc! {
|
doc! {
|
||||||
"$set": {
|
"$set": {
|
||||||
@@ -230,7 +234,25 @@ pub fn add_friend(user: UserRef, target: UserRef) -> Response {
|
|||||||
)
|
)
|
||||||
.is_ok()
|
.is_ok()
|
||||||
{
|
{
|
||||||
Response::Success(json!({ "status": Relationship::Friend as u8 }))
|
notifications::send_message_threaded(
|
||||||
|
vec![target.id.clone()],
|
||||||
|
None,
|
||||||
|
Notification::user_friend_status(FriendStatus {
|
||||||
|
id: user.id.clone(),
|
||||||
|
status: Relationship::Friend as i32,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
notifications::send_message_threaded(
|
||||||
|
vec![user.id.clone()],
|
||||||
|
None,
|
||||||
|
Notification::user_friend_status(FriendStatus {
|
||||||
|
id: target.id.clone(),
|
||||||
|
status: Relationship::Friend as i32,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
Response::Success(json!({ "status": Relationship::Friend as i32 }))
|
||||||
} else {
|
} else {
|
||||||
Response::InternalServerError(
|
Response::InternalServerError(
|
||||||
json!({ "error": "Failed to commit! Try re-adding them as a friend." }),
|
json!({ "error": "Failed to commit! Try re-adding them as a friend." }),
|
||||||
@@ -269,12 +291,12 @@ pub fn add_friend(user: UserRef, target: UserRef) -> Response {
|
|||||||
if col
|
if col
|
||||||
.update_one(
|
.update_one(
|
||||||
doc! {
|
doc! {
|
||||||
"_id": target.id
|
"_id": target.id.clone()
|
||||||
},
|
},
|
||||||
doc! {
|
doc! {
|
||||||
"$push": {
|
"$push": {
|
||||||
"relations": {
|
"relations": {
|
||||||
"id": user.id,
|
"id": user.id.clone(),
|
||||||
"status": Relationship::Incoming as i32
|
"status": Relationship::Incoming as i32
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -283,7 +305,25 @@ pub fn add_friend(user: UserRef, target: UserRef) -> Response {
|
|||||||
)
|
)
|
||||||
.is_ok()
|
.is_ok()
|
||||||
{
|
{
|
||||||
Response::Success(json!({ "status": Relationship::Outgoing as u8 }))
|
notifications::send_message_threaded(
|
||||||
|
vec![user.id.clone()],
|
||||||
|
None,
|
||||||
|
Notification::user_friend_status(FriendStatus {
|
||||||
|
id: target.id.clone(),
|
||||||
|
status: Relationship::Outgoing as i32,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
notifications::send_message_threaded(
|
||||||
|
vec![target.id.clone()],
|
||||||
|
None,
|
||||||
|
Notification::user_friend_status(FriendStatus {
|
||||||
|
id: user.id.clone(),
|
||||||
|
status: Relationship::Incoming as i32,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
Response::Success(json!({ "status": Relationship::Outgoing as i32 }))
|
||||||
} else {
|
} else {
|
||||||
Response::InternalServerError(
|
Response::InternalServerError(
|
||||||
json!({ "error": "Failed to commit! Try re-adding them as a friend." }),
|
json!({ "error": "Failed to commit! Try re-adding them as a friend." }),
|
||||||
@@ -327,12 +367,12 @@ pub fn remove_friend(user: UserRef, target: UserRef) -> Response {
|
|||||||
if col
|
if col
|
||||||
.update_one(
|
.update_one(
|
||||||
doc! {
|
doc! {
|
||||||
"_id": target.id
|
"_id": target.id.clone()
|
||||||
},
|
},
|
||||||
doc! {
|
doc! {
|
||||||
"$pull": {
|
"$pull": {
|
||||||
"relations": {
|
"relations": {
|
||||||
"id": user.id
|
"id": user.id.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -340,7 +380,25 @@ pub fn remove_friend(user: UserRef, target: UserRef) -> Response {
|
|||||||
)
|
)
|
||||||
.is_ok()
|
.is_ok()
|
||||||
{
|
{
|
||||||
Response::Success(json!({ "status": Relationship::NONE as u8 }))
|
notifications::send_message_threaded(
|
||||||
|
vec![user.id.clone()],
|
||||||
|
None,
|
||||||
|
Notification::user_friend_status(FriendStatus {
|
||||||
|
id: target.id.clone(),
|
||||||
|
status: Relationship::NONE as i32,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
notifications::send_message_threaded(
|
||||||
|
vec![target.id.clone()],
|
||||||
|
None,
|
||||||
|
Notification::user_friend_status(FriendStatus {
|
||||||
|
id: user.id.clone(),
|
||||||
|
status: Relationship::NONE as i32,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
Response::Success(json!({ "status": Relationship::NONE as i32 }))
|
||||||
} else {
|
} else {
|
||||||
Response::InternalServerError(
|
Response::InternalServerError(
|
||||||
json!({ "error": "Failed to commit! Target remains in same state." }),
|
json!({ "error": "Failed to commit! Target remains in same state." }),
|
||||||
@@ -365,7 +423,9 @@ pub fn block_user(user: UserRef, target: UserRef) -> Response {
|
|||||||
let col = database::get_collection("users");
|
let col = database::get_collection("users");
|
||||||
|
|
||||||
match get_relationship(&user, &target) {
|
match get_relationship(&user, &target) {
|
||||||
Relationship::Friend | Relationship::Incoming | Relationship::Outgoing => {
|
Relationship::Friend
|
||||||
|
| Relationship::Incoming
|
||||||
|
| Relationship::Outgoing => {
|
||||||
if col
|
if col
|
||||||
.update_one(
|
.update_one(
|
||||||
doc! {
|
doc! {
|
||||||
@@ -384,8 +444,8 @@ pub fn block_user(user: UserRef, target: UserRef) -> Response {
|
|||||||
if col
|
if col
|
||||||
.update_one(
|
.update_one(
|
||||||
doc! {
|
doc! {
|
||||||
"_id": target.id,
|
"_id": target.id.clone(),
|
||||||
"relations.id": user.id
|
"relations.id": user.id.clone()
|
||||||
},
|
},
|
||||||
doc! {
|
doc! {
|
||||||
"$set": {
|
"$set": {
|
||||||
@@ -396,7 +456,91 @@ pub fn block_user(user: UserRef, target: UserRef) -> Response {
|
|||||||
)
|
)
|
||||||
.is_ok()
|
.is_ok()
|
||||||
{
|
{
|
||||||
Response::Success(json!({ "status": Relationship::Blocked as u8 }))
|
notifications::send_message_threaded(
|
||||||
|
vec![user.id.clone()],
|
||||||
|
None,
|
||||||
|
Notification::user_friend_status(FriendStatus {
|
||||||
|
id: target.id.clone(),
|
||||||
|
status: Relationship::Blocked as i32,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
notifications::send_message_threaded(
|
||||||
|
vec![target.id.clone()],
|
||||||
|
None,
|
||||||
|
Notification::user_friend_status(FriendStatus {
|
||||||
|
id: user.id.clone(),
|
||||||
|
status: Relationship::BlockedOther as i32,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
Response::Success(json!({ "status": Relationship::Blocked as i32 }))
|
||||||
|
} else {
|
||||||
|
Response::InternalServerError(
|
||||||
|
json!({ "error": "Failed to commit! Try blocking the user again, remove it first." }),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Response::InternalServerError(
|
||||||
|
json!({ "error": "Failed to commit to database, try again." }),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Relationship::NONE => {
|
||||||
|
if col
|
||||||
|
.update_one(
|
||||||
|
doc! {
|
||||||
|
"_id": user.id.clone(),
|
||||||
|
},
|
||||||
|
doc! {
|
||||||
|
"$push": {
|
||||||
|
"relations": {
|
||||||
|
"id": target.id.clone(),
|
||||||
|
"status": Relationship::Blocked as i32,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.is_ok()
|
||||||
|
{
|
||||||
|
if col
|
||||||
|
.update_one(
|
||||||
|
doc! {
|
||||||
|
"_id": target.id.clone(),
|
||||||
|
},
|
||||||
|
doc! {
|
||||||
|
"$push": {
|
||||||
|
"relations": {
|
||||||
|
"id": user.id.clone(),
|
||||||
|
"status": Relationship::BlockedOther as i32,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.is_ok()
|
||||||
|
{
|
||||||
|
notifications::send_message_threaded(
|
||||||
|
vec![user.id.clone()],
|
||||||
|
None,
|
||||||
|
Notification::user_friend_status(FriendStatus {
|
||||||
|
id: target.id.clone(),
|
||||||
|
status: Relationship::Blocked as i32,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
notifications::send_message_threaded(
|
||||||
|
vec![target.id.clone()],
|
||||||
|
None,
|
||||||
|
Notification::user_friend_status(FriendStatus {
|
||||||
|
id: user.id.clone(),
|
||||||
|
status: Relationship::BlockedOther as i32,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
Response::Success(json!({ "status": Relationship::Blocked as i32 }))
|
||||||
} else {
|
} else {
|
||||||
Response::InternalServerError(
|
Response::InternalServerError(
|
||||||
json!({ "error": "Failed to commit! Try blocking the user again, remove it first." }),
|
json!({ "error": "Failed to commit! Try blocking the user again, remove it first." }),
|
||||||
@@ -427,16 +571,23 @@ pub fn block_user(user: UserRef, target: UserRef) -> Response {
|
|||||||
)
|
)
|
||||||
.is_ok()
|
.is_ok()
|
||||||
{
|
{
|
||||||
Response::Success(json!({ "status": Relationship::Blocked as u8 }))
|
notifications::send_message_threaded(
|
||||||
|
vec![user.id.clone()],
|
||||||
|
None,
|
||||||
|
Notification::user_friend_status(FriendStatus {
|
||||||
|
id: target.id.clone(),
|
||||||
|
status: Relationship::Blocked as i32,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
Response::Success(json!({ "status": Relationship::Blocked as i32 }))
|
||||||
} else {
|
} else {
|
||||||
Response::InternalServerError(
|
Response::InternalServerError(
|
||||||
json!({ "error": "Failed to commit to database, try again." }),
|
json!({ "error": "Failed to commit to database, try again." }),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Relationship::SELF | Relationship::NONE => {
|
Relationship::SELF => Response::BadRequest(json!({ "error": "This has no effect." })),
|
||||||
Response::BadRequest(json!({ "error": "This has no effect." }))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -463,7 +614,16 @@ pub fn unblock_user(user: UserRef, target: UserRef) -> Response {
|
|||||||
)
|
)
|
||||||
.is_ok()
|
.is_ok()
|
||||||
{
|
{
|
||||||
Response::Success(json!({ "status": Relationship::BlockedOther as u8 }))
|
notifications::send_message_threaded(
|
||||||
|
vec![user.id.clone()],
|
||||||
|
None,
|
||||||
|
Notification::user_friend_status(FriendStatus {
|
||||||
|
id: target.id.clone(),
|
||||||
|
status: Relationship::BlockedOther as i32,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
Response::Success(json!({ "status": Relationship::BlockedOther as i32 }))
|
||||||
} else {
|
} else {
|
||||||
Response::InternalServerError(
|
Response::InternalServerError(
|
||||||
json!({ "error": "Failed to commit to database, try again." }),
|
json!({ "error": "Failed to commit to database, try again." }),
|
||||||
@@ -490,12 +650,12 @@ pub fn unblock_user(user: UserRef, target: UserRef) -> Response {
|
|||||||
if col
|
if col
|
||||||
.update_one(
|
.update_one(
|
||||||
doc! {
|
doc! {
|
||||||
"_id": target.id
|
"_id": target.id.clone()
|
||||||
},
|
},
|
||||||
doc! {
|
doc! {
|
||||||
"$pull": {
|
"$pull": {
|
||||||
"relations": {
|
"relations": {
|
||||||
"id": user.id
|
"id": user.id.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -503,7 +663,25 @@ pub fn unblock_user(user: UserRef, target: UserRef) -> Response {
|
|||||||
)
|
)
|
||||||
.is_ok()
|
.is_ok()
|
||||||
{
|
{
|
||||||
Response::Success(json!({ "status": Relationship::NONE as u8 }))
|
notifications::send_message_threaded(
|
||||||
|
vec![user.id.clone()],
|
||||||
|
None,
|
||||||
|
Notification::user_friend_status(FriendStatus {
|
||||||
|
id: target.id.clone(),
|
||||||
|
status: Relationship::NONE as i32,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
notifications::send_message_threaded(
|
||||||
|
vec![target.id.clone()],
|
||||||
|
None,
|
||||||
|
Notification::user_friend_status(FriendStatus {
|
||||||
|
id: user.id.clone(),
|
||||||
|
status: Relationship::NONE as i32,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
Response::Success(json!({ "status": Relationship::NONE as i32 }))
|
||||||
} else {
|
} else {
|
||||||
Response::InternalServerError(
|
Response::InternalServerError(
|
||||||
json!({ "error": "Failed to commit! Target remains in same state." }),
|
json!({ "error": "Failed to commit! Target remains in same state." }),
|
||||||
|
|||||||
Reference in New Issue
Block a user