mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 05:26:59 +00:00
Remove old notif code; uncommited changes.
This commit is contained in:
@@ -170,7 +170,7 @@ impl<'r> FromParam<'r> for Channel {
|
||||
}
|
||||
}
|
||||
|
||||
use crate::notifications::events::Notification;
|
||||
/*use crate::notifications::events::Notification;
|
||||
|
||||
pub fn process_event(event: &Notification) {
|
||||
match event {
|
||||
@@ -212,4 +212,4 @@ pub fn process_event(event: &Notification) {
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
@@ -306,7 +306,7 @@ pub fn get_invite<U: Into<Option<String>>>(
|
||||
}
|
||||
}
|
||||
|
||||
use crate::notifications::events::Notification;
|
||||
/*use crate::notifications::events::Notification;
|
||||
|
||||
pub fn process_event(event: &Notification) {
|
||||
match event {
|
||||
@@ -335,4 +335,4 @@ pub fn process_event(event: &Notification) {
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
use super::get_collection;
|
||||
use crate::database::channel::Channel;
|
||||
use crate::notifications;
|
||||
use crate::notifications::events::message::Create;
|
||||
use crate::notifications::events::Notification;
|
||||
use crate::pubsub::hive;
|
||||
use crate::routes::channel::ChannelType;
|
||||
|
||||
@@ -43,7 +40,7 @@ impl Message {
|
||||
.insert_one(to_bson(&self).unwrap().as_document().unwrap().clone(), None)
|
||||
.is_ok()
|
||||
{
|
||||
notifications::send_message_given_channel(
|
||||
/*notifications::send_message_given_channel(
|
||||
Notification::message_create(Create {
|
||||
id: self.id.clone(),
|
||||
nonce: self.nonce.clone(),
|
||||
@@ -52,7 +49,7 @@ impl Message {
|
||||
content: self.content.clone(),
|
||||
}),
|
||||
&target,
|
||||
);
|
||||
);*/
|
||||
|
||||
if hive::publish(
|
||||
&target.id,
|
||||
|
||||
@@ -272,9 +272,7 @@ impl<'r> FromParam<'r> for User {
|
||||
}
|
||||
}
|
||||
|
||||
use crate::notifications::events::Notification;
|
||||
|
||||
pub fn process_event(event: &Notification) {
|
||||
/*pub fn process_event(event: &Notification) {
|
||||
match event {
|
||||
Notification::user_friend_status(ev) => {
|
||||
let mut cache = CACHE.lock().unwrap();
|
||||
@@ -297,4 +295,4 @@ pub fn process_event(event: &Notification) {
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
15
src/main.rs
15
src/main.rs
@@ -10,14 +10,12 @@ extern crate bitfield;
|
||||
extern crate lazy_static;
|
||||
|
||||
pub mod database;
|
||||
pub mod notifications;
|
||||
pub mod pubsub;
|
||||
pub mod routes;
|
||||
pub mod util;
|
||||
|
||||
use log::info;
|
||||
use rocket_cors::AllowedOrigins;
|
||||
use std::thread;
|
||||
|
||||
fn main() {
|
||||
dotenv::dotenv().ok();
|
||||
@@ -28,19 +26,6 @@ fn main() {
|
||||
util::variables::preflight_checks();
|
||||
database::connect();
|
||||
|
||||
// ! START OLD NOTIF CODE
|
||||
notifications::start_worker();
|
||||
|
||||
thread::spawn(|| {
|
||||
notifications::pubsub::launch_subscriber();
|
||||
});
|
||||
|
||||
notifications::state::init();
|
||||
/*thread::spawn(|| {
|
||||
notifications::ws::launch_server();
|
||||
});*/
|
||||
// ! END OLD NOTIF CODE
|
||||
|
||||
pubsub::hive::init_hive();
|
||||
pubsub::websocket::launch_server();
|
||||
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
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,
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
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,
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct Create {
|
||||
pub id: String,
|
||||
pub nonce: Option<String>,
|
||||
pub channel: String,
|
||||
pub author: String,
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct Edit {
|
||||
pub id: String,
|
||||
pub channel: String,
|
||||
pub author: String,
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct Delete {
|
||||
pub id: String,
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{json, Value};
|
||||
|
||||
pub mod groups;
|
||||
pub mod guilds;
|
||||
pub mod message;
|
||||
pub mod users;
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub enum Notification {
|
||||
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 {
|
||||
pub fn serialize(self) -> String {
|
||||
if let Value::Object(obj) = json!(self) {
|
||||
let (key, value) = obj.iter().next().unwrap();
|
||||
|
||||
if let Value::Object(data) = value {
|
||||
let mut data = data.clone();
|
||||
data.insert("type".to_string(), Value::String(key.to_string()));
|
||||
json!(data).to_string()
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push_to_cache(&self) {
|
||||
crate::database::channel::process_event(&self);
|
||||
crate::database::guild::process_event(&self);
|
||||
crate::database::user::process_event(&self);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct FriendStatus {
|
||||
pub id: String,
|
||||
pub user: String,
|
||||
pub status: i32,
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
use crate::database::channel::Channel;
|
||||
|
||||
use once_cell::sync::OnceCell;
|
||||
use std::sync::mpsc::{channel, Sender};
|
||||
use std::thread;
|
||||
|
||||
pub mod events;
|
||||
pub mod pubsub;
|
||||
pub mod state;
|
||||
pub mod ws;
|
||||
|
||||
pub fn send_message<U: Into<Option<Vec<String>>>, G: Into<Option<String>>>(
|
||||
users: U,
|
||||
guild: G,
|
||||
data: events::Notification,
|
||||
) -> bool {
|
||||
let users = users.into();
|
||||
let guild = guild.into();
|
||||
|
||||
data.push_to_cache();
|
||||
|
||||
if pubsub::send_message(users.clone(), guild.clone(), data.clone()) {
|
||||
state::send_message(users, guild, data.serialize());
|
||||
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
struct NotificationArguments {
|
||||
users: Option<Vec<String>>,
|
||||
guild: Option<String>,
|
||||
data: events::Notification,
|
||||
}
|
||||
|
||||
static mut SENDER: OnceCell<Sender<NotificationArguments>> = OnceCell::new();
|
||||
|
||||
pub fn start_worker() {
|
||||
let (sender, receiver) = channel();
|
||||
unsafe {
|
||||
SENDER.set(sender).unwrap();
|
||||
}
|
||||
|
||||
thread::spawn(move || {
|
||||
while let Ok(data) = receiver.recv() {
|
||||
send_message(data.users, data.guild, data.data);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn send_message_threaded<U: Into<Option<Vec<String>>>, G: Into<Option<String>>>(
|
||||
users: U,
|
||||
guild: G,
|
||||
data: events::Notification,
|
||||
) -> bool {
|
||||
unsafe {
|
||||
SENDER
|
||||
.get()
|
||||
.unwrap()
|
||||
.send(NotificationArguments {
|
||||
users: users.into(),
|
||||
guild: guild.into(),
|
||||
data,
|
||||
})
|
||||
.is_ok()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send_message_given_channel(data: events::Notification, channel: &Channel) {
|
||||
match channel.channel_type {
|
||||
0..=1 => send_message_threaded(channel.recipients.clone(), None, data),
|
||||
2 => send_message_threaded(None, channel.guild.clone(), data),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
}
|
||||
@@ -1,102 +0,0 @@
|
||||
use super::events::Notification;
|
||||
use crate::database::get_collection;
|
||||
|
||||
use mongodb::bson::{doc, from_bson, to_bson, Bson};
|
||||
use mongodb::options::{CursorType, FindOneOptions, FindOptions};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::time::Duration;
|
||||
use ulid::Ulid;
|
||||
|
||||
use once_cell::sync::OnceCell;
|
||||
static SOURCEID: OnceCell<String> = OnceCell::new();
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct PubSubMessage {
|
||||
#[serde(rename = "_id")]
|
||||
id: String,
|
||||
source: String,
|
||||
|
||||
user_recipients: Option<Vec<String>>,
|
||||
target_guild: Option<String>,
|
||||
data: Notification,
|
||||
}
|
||||
|
||||
pub fn send_message(users: Option<Vec<String>>, guild: Option<String>, data: Notification) -> bool {
|
||||
let message = PubSubMessage {
|
||||
id: Ulid::new().to_string(),
|
||||
source: SOURCEID.get().unwrap().to_string(),
|
||||
user_recipients: users.into(),
|
||||
target_guild: guild.into(),
|
||||
data,
|
||||
};
|
||||
|
||||
if get_collection("pubsub")
|
||||
.insert_one(
|
||||
to_bson(&message)
|
||||
.expect("Failed to serialize pubsub message.")
|
||||
.as_document()
|
||||
.expect("Failed to convert to a document.")
|
||||
.clone(),
|
||||
None,
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn launch_subscriber() {
|
||||
let source = Ulid::new().to_string();
|
||||
SOURCEID
|
||||
.set(source.clone())
|
||||
.expect("Failed to create and set source ID.");
|
||||
|
||||
let pubsub = get_collection("pubsub");
|
||||
if let Ok(result) = pubsub.find_one(
|
||||
doc! {},
|
||||
FindOneOptions::builder().sort(doc! { "_id": -1 }).build(),
|
||||
) {
|
||||
let query = if let Some(doc) = result {
|
||||
doc! { "_id": { "$gt": doc.get_str("_id").unwrap() } }
|
||||
} else {
|
||||
doc! {}
|
||||
};
|
||||
|
||||
if let Ok(mut cursor) = pubsub.find(
|
||||
query,
|
||||
FindOptions::builder()
|
||||
.cursor_type(CursorType::TailableAwait)
|
||||
.no_cursor_timeout(true)
|
||||
.max_await_time(Duration::from_secs(1200))
|
||||
.build(),
|
||||
) {
|
||||
loop {
|
||||
while let Some(item) = cursor.next() {
|
||||
if let Ok(doc) = item {
|
||||
if let Ok(message) =
|
||||
from_bson(Bson::Document(doc)) as Result<PubSubMessage, _>
|
||||
{
|
||||
if &message.source != &source {
|
||||
super::state::send_message(
|
||||
message.user_recipients,
|
||||
message.target_guild,
|
||||
message.data.serialize(),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
eprintln!("Failed to deserialize pubsub message.");
|
||||
}
|
||||
} else {
|
||||
eprintln!("Failed to unwrap a document from pubsub.");
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
eprintln!("Failed to open subscriber cursor.");
|
||||
}
|
||||
} else {
|
||||
eprintln!("Failed to fetch latest document from pubsub collection.");
|
||||
}
|
||||
}
|
||||
@@ -1,199 +0,0 @@
|
||||
use crate::database;
|
||||
use crate::util::vec_to_set;
|
||||
|
||||
use hashbrown::{HashMap, HashSet};
|
||||
use mongodb::bson::doc;
|
||||
use mongodb::options::FindOneOptions;
|
||||
use once_cell::sync::OnceCell;
|
||||
use std::sync::RwLock;
|
||||
use ws::Sender;
|
||||
|
||||
pub enum StateResult {
|
||||
DatabaseError,
|
||||
InvalidToken,
|
||||
Success(String),
|
||||
}
|
||||
|
||||
static CONNECTIONS: OnceCell<RwLock<HashMap<String, Sender>>> = OnceCell::new();
|
||||
|
||||
pub fn add_connection(id: String, sender: Sender) {
|
||||
CONNECTIONS
|
||||
.get()
|
||||
.unwrap()
|
||||
.write()
|
||||
.unwrap()
|
||||
.insert(id, sender);
|
||||
}
|
||||
|
||||
pub struct User {
|
||||
connections: HashSet<String>,
|
||||
guilds: HashSet<String>,
|
||||
}
|
||||
|
||||
impl User {
|
||||
pub fn new() -> User {
|
||||
User {
|
||||
connections: HashSet::new(),
|
||||
guilds: HashSet::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Guild {
|
||||
users: HashSet<String>,
|
||||
}
|
||||
|
||||
impl Guild {
|
||||
pub fn new() -> Guild {
|
||||
Guild {
|
||||
users: HashSet::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct GlobalState {
|
||||
users: HashMap<String, User>,
|
||||
guilds: HashMap<String, Guild>,
|
||||
}
|
||||
|
||||
impl GlobalState {
|
||||
pub fn new() -> GlobalState {
|
||||
GlobalState {
|
||||
users: HashMap::new(),
|
||||
guilds: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push_to_guild(&mut self, guild: String, user: String) {
|
||||
if !self.guilds.contains_key(&guild) {
|
||||
self.guilds.insert(guild.clone(), Guild::new());
|
||||
}
|
||||
|
||||
self.guilds.get_mut(&guild).unwrap().users.insert(user);
|
||||
}
|
||||
|
||||
pub fn try_authenticate(&mut self, connection: String, access_token: String) -> StateResult {
|
||||
if let Ok(result) = database::get_collection("users").find_one(
|
||||
doc! {
|
||||
"access_token": access_token,
|
||||
},
|
||||
FindOneOptions::builder()
|
||||
.projection(doc! { "_id": 1 })
|
||||
.build(),
|
||||
) {
|
||||
if let Some(user) = result {
|
||||
let user_id = user.get_str("_id").unwrap();
|
||||
|
||||
if self.users.contains_key(user_id) {
|
||||
self.users
|
||||
.get_mut(user_id)
|
||||
.unwrap()
|
||||
.connections
|
||||
.insert(connection);
|
||||
|
||||
return StateResult::Success(user_id.to_string());
|
||||
}
|
||||
|
||||
if let Ok(results) =
|
||||
database::get_collection("members").find(doc! { "_id.user": &user_id }, None)
|
||||
{
|
||||
let mut guilds = vec![];
|
||||
for result in results {
|
||||
if let Ok(entry) = result {
|
||||
guilds.push(
|
||||
entry
|
||||
.get_document("_id")
|
||||
.unwrap()
|
||||
.get_str("guild")
|
||||
.unwrap()
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let mut user = User::new();
|
||||
for guild in guilds {
|
||||
user.guilds.insert(guild.clone());
|
||||
self.push_to_guild(guild, user_id.to_string());
|
||||
}
|
||||
|
||||
user.connections.insert(connection);
|
||||
self.users.insert(user_id.to_string(), user);
|
||||
|
||||
StateResult::Success(user_id.to_string())
|
||||
} else {
|
||||
StateResult::DatabaseError
|
||||
}
|
||||
} else {
|
||||
StateResult::InvalidToken
|
||||
}
|
||||
} else {
|
||||
StateResult::DatabaseError
|
||||
}
|
||||
}
|
||||
|
||||
pub fn disconnect<U: Into<Option<String>>>(&mut self, user_id: U, connection: String) {
|
||||
if let Some(user_id) = user_id.into() {
|
||||
let user = self.users.get_mut(&user_id).unwrap();
|
||||
user.connections.remove(&connection);
|
||||
|
||||
if user.connections.len() == 0 {
|
||||
for guild in &user.guilds {
|
||||
self.guilds.get_mut(guild).unwrap().users.remove(&user_id);
|
||||
}
|
||||
|
||||
self.users.remove(&user_id);
|
||||
}
|
||||
}
|
||||
|
||||
CONNECTIONS
|
||||
.get()
|
||||
.unwrap()
|
||||
.write()
|
||||
.unwrap()
|
||||
.remove(&connection);
|
||||
}
|
||||
}
|
||||
|
||||
pub static DATA: OnceCell<RwLock<GlobalState>> = OnceCell::new();
|
||||
|
||||
pub fn init() {
|
||||
if CONNECTIONS.set(RwLock::new(HashMap::new())).is_err() {
|
||||
panic!("Failed to set global connections map.");
|
||||
}
|
||||
|
||||
if DATA.set(RwLock::new(GlobalState::new())).is_err() {
|
||||
panic!("Failed to set global state.");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send_message(users: Option<Vec<String>>, guild: Option<String>, data: String) {
|
||||
let state = DATA.get().unwrap().read().unwrap();
|
||||
let mut connections = HashSet::new();
|
||||
|
||||
let mut users = vec_to_set(&users.unwrap_or(vec![]));
|
||||
if let Some(guild) = guild {
|
||||
if let Some(entry) = state.guilds.get(&guild) {
|
||||
for user in &entry.users {
|
||||
users.insert(user.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for user in users {
|
||||
if let Some(entry) = state.users.get(&user) {
|
||||
for connection in &entry.connections {
|
||||
connections.insert(connection.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let targets = CONNECTIONS.get().unwrap().read().unwrap();
|
||||
for conn in connections {
|
||||
if let Some(sender) = targets.get(&conn) {
|
||||
if sender.send(data.clone()).is_err() {
|
||||
eprintln!("Failed to send a notification to a websocket. [{}]", &conn);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,135 +0,0 @@
|
||||
use super::state::{self, StateResult};
|
||||
use crate::util::variables::WS_HOST;
|
||||
|
||||
use serde_json::{from_str, json, Value};
|
||||
use ulid::Ulid;
|
||||
use ws::{listen, CloseCode, Error, Handler, Handshake, Message, Result, Sender};
|
||||
|
||||
struct Server {
|
||||
sender: Sender,
|
||||
user_id: Option<String>,
|
||||
id: String,
|
||||
}
|
||||
|
||||
impl Handler for Server {
|
||||
fn on_open(&mut self, _: Handshake) -> Result<()> {
|
||||
state::add_connection(self.id.clone(), self.sender.clone());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn on_message(&mut self, msg: Message) -> Result<()> {
|
||||
if let Message::Text(text) = msg {
|
||||
if let Ok(data) = from_str(&text) as std::result::Result<Value, _> {
|
||||
if let Value::String(packet_type) = &data["type"] {
|
||||
if packet_type == "authenticate" {
|
||||
if self.user_id.is_some() {
|
||||
self.sender.send(
|
||||
json!({
|
||||
"type": "authenticate",
|
||||
"success": false,
|
||||
"error": "Already authenticated!"
|
||||
})
|
||||
.to_string(),
|
||||
)
|
||||
} else if let Value::String(token) = &data["token"] {
|
||||
let mut state = unsafe { state::DATA.get().unwrap().write().unwrap() };
|
||||
|
||||
match state.try_authenticate(self.id.clone(), token.to_string()) {
|
||||
StateResult::Success(user_id) => {
|
||||
let user = crate::database::user::fetch_user(&user_id)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
self.user_id = Some(user_id);
|
||||
|
||||
self.sender.send(
|
||||
json!({
|
||||
"type": "authenticate",
|
||||
"success": true,
|
||||
})
|
||||
.to_string(),
|
||||
)?;
|
||||
|
||||
if let Ok(payload) = user.create_payload() {
|
||||
self.sender.send(
|
||||
json!({
|
||||
"type": "ready",
|
||||
"data": payload
|
||||
})
|
||||
.to_string(),
|
||||
)
|
||||
} else {
|
||||
// ! TODO: FIXME: ALL THE NOTIFICATIONS CODE NEEDS TO BE
|
||||
// ! RESTRUCTURED, IT IS UTTER GARBAGE. :)))))
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
StateResult::DatabaseError => self.sender.send(
|
||||
json!({
|
||||
"type": "authenticate",
|
||||
"success": false,
|
||||
"error": "Had database error."
|
||||
})
|
||||
.to_string(),
|
||||
),
|
||||
StateResult::InvalidToken => self.sender.send(
|
||||
json!({
|
||||
"type": "authenticate",
|
||||
"success": false,
|
||||
"error": "Invalid token."
|
||||
})
|
||||
.to_string(),
|
||||
),
|
||||
}
|
||||
} else {
|
||||
self.sender.send(
|
||||
json!({
|
||||
"type": "authenticate",
|
||||
"success": false,
|
||||
"error": "Token not present."
|
||||
})
|
||||
.to_string(),
|
||||
)
|
||||
}
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn on_close(&mut self, _code: CloseCode, _reason: &str) {
|
||||
unsafe {
|
||||
state::DATA
|
||||
.get()
|
||||
.unwrap()
|
||||
.write()
|
||||
.unwrap()
|
||||
.disconnect(self.user_id.clone(), self.id.clone());
|
||||
}
|
||||
|
||||
println!("User disconnected. [{}]", self.id);
|
||||
}
|
||||
|
||||
fn on_error(&mut self, err: Error) {
|
||||
println!("The server encountered an error: {:?}", err);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn launch_server() {
|
||||
state::init();
|
||||
|
||||
listen(WS_HOST.to_string(), |sender| Server {
|
||||
sender,
|
||||
user_id: None,
|
||||
id: Ulid::new().to_string(),
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
@@ -7,7 +7,7 @@ pub mod users;
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
#[serde(tag = "type", content = "data")]
|
||||
#[serde(tag = "type")]
|
||||
pub enum Notification {
|
||||
message_create(message::Create),
|
||||
message_edit(message::Edit),
|
||||
|
||||
@@ -104,6 +104,7 @@ impl Handler for Client {
|
||||
}
|
||||
|
||||
if let Err(err) = hive::subscribe(self.user_id.as_ref().unwrap().clone(), ids) {
|
||||
error!("Failed to subscribe someone to the Hive! {}", err);
|
||||
self.sender.send(
|
||||
json!({
|
||||
"type": "warn",
|
||||
|
||||
@@ -3,10 +3,6 @@ use crate::database::{
|
||||
self, channel::Channel, get_relationship, get_relationship_internal, message::Message,
|
||||
user::User, Permission, PermissionCalculator, Relationship,
|
||||
};
|
||||
use crate::notifications::{
|
||||
self,
|
||||
events::{groups::*, guilds::ChannelDelete, message::*, Notification},
|
||||
};
|
||||
use crate::util::vec_to_set;
|
||||
|
||||
use chrono::prelude::*;
|
||||
@@ -180,13 +176,13 @@ pub fn add_member(user: User, target: Channel, member: User) -> Option<Response>
|
||||
})
|
||||
.send(&target)
|
||||
{
|
||||
notifications::send_message_given_channel(
|
||||
/*notifications::send_message_given_channel(
|
||||
Notification::group_user_join(UserJoin {
|
||||
id: target.id.clone(),
|
||||
user: member.id.clone(),
|
||||
}),
|
||||
&target,
|
||||
);
|
||||
&target, FIXME
|
||||
);*/
|
||||
|
||||
Some(Response::Result(super::Status::Ok))
|
||||
} else {
|
||||
@@ -255,13 +251,13 @@ pub fn remove_member(user: User, target: Channel, member: User) -> Option<Respon
|
||||
})
|
||||
.send(&target)
|
||||
{
|
||||
notifications::send_message_given_channel(
|
||||
/*notifications::send_message_given_channel(
|
||||
Notification::group_user_leave(UserLeave {
|
||||
id: target.id.clone(),
|
||||
user: member.id.clone(),
|
||||
}),
|
||||
&target,
|
||||
);
|
||||
&target, FIXME
|
||||
);*/
|
||||
|
||||
Some(Response::Result(super::Status::Ok))
|
||||
} else {
|
||||
@@ -373,13 +369,13 @@ pub fn delete(user: User, target: Channel) -> Option<Response> {
|
||||
})
|
||||
.send(&target)
|
||||
{
|
||||
notifications::send_message_given_channel(
|
||||
/*notifications::send_message_given_channel(
|
||||
Notification::group_user_leave(UserLeave {
|
||||
id: target.id.clone(),
|
||||
user: user.id.clone(),
|
||||
}),
|
||||
&target,
|
||||
);
|
||||
&target, FIXME
|
||||
);*/
|
||||
|
||||
Some(Response::Result(super::Status::Ok))
|
||||
} else {
|
||||
@@ -411,14 +407,14 @@ pub fn delete(user: User, target: Channel) -> Option<Response> {
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
notifications::send_message_threaded(
|
||||
/*notifications::send_message_threaded(
|
||||
None,
|
||||
guild_id.clone(),
|
||||
Notification::guild_channel_delete(ChannelDelete {
|
||||
id: guild_id.clone(),
|
||||
channel: target.id.clone(),
|
||||
}),
|
||||
);
|
||||
}), FIXME
|
||||
);*/
|
||||
|
||||
try_delete()
|
||||
} else {
|
||||
@@ -630,15 +626,15 @@ pub fn edit_message(
|
||||
None,
|
||||
) {
|
||||
Ok(_) => {
|
||||
notifications::send_message_given_channel(
|
||||
/*notifications::send_message_given_channel(
|
||||
Notification::message_edit(Edit {
|
||||
id: message.id.clone(),
|
||||
channel: target.id.clone(),
|
||||
author: message.author.clone(),
|
||||
content: edit.content.clone(),
|
||||
}),
|
||||
&target,
|
||||
);
|
||||
&target, FIXME
|
||||
);*/
|
||||
|
||||
Some(Response::Result(super::Status::Ok))
|
||||
}
|
||||
@@ -661,12 +657,12 @@ pub fn delete_message(user: User, target: Channel, message: Message) -> Option<R
|
||||
|
||||
match col.delete_one(doc! { "_id": &message.id }, None) {
|
||||
Ok(_) => {
|
||||
notifications::send_message_given_channel(
|
||||
/*notifications::send_message_given_channel(
|
||||
Notification::message_delete(Delete {
|
||||
id: message.id.clone(),
|
||||
}),
|
||||
&target,
|
||||
);
|
||||
&target, FIXME
|
||||
);*/
|
||||
|
||||
Some(Response::Result(super::Status::Ok))
|
||||
}
|
||||
|
||||
@@ -5,10 +5,6 @@ use crate::database::{
|
||||
self, channel::fetch_channel, channel::Channel, guild::serialise_guilds_with_channels,
|
||||
user::User, Permission, PermissionCalculator,
|
||||
};
|
||||
use crate::notifications::{
|
||||
self,
|
||||
events::{guilds::*, Notification},
|
||||
};
|
||||
use crate::util::gen_token;
|
||||
|
||||
use mongodb::bson::{doc, Bson};
|
||||
@@ -122,13 +118,13 @@ pub fn remove_guild(user: User, target: Guild) -> Option<Response> {
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
notifications::send_message_threaded(
|
||||
/*notifications::send_message_threaded(
|
||||
None,
|
||||
target.id.clone(),
|
||||
Notification::guild_delete(Delete {
|
||||
id: target.id.clone(),
|
||||
}),
|
||||
);
|
||||
}), FIXME
|
||||
);*/
|
||||
|
||||
Some(Response::Result(super::Status::Ok))
|
||||
} else {
|
||||
@@ -166,15 +162,15 @@ pub fn remove_guild(user: User, target: Guild) -> Option<Response> {
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
notifications::send_message_threaded(
|
||||
/*notifications::send_message_threaded(
|
||||
None,
|
||||
target.id.clone(),
|
||||
Notification::guild_user_leave(UserLeave {
|
||||
id: target.id.clone(),
|
||||
user: user.id.clone(),
|
||||
banned: false,
|
||||
}),
|
||||
);
|
||||
}), FIXME
|
||||
);*/
|
||||
|
||||
Some(Response::Result(super::Status::Ok))
|
||||
} else {
|
||||
@@ -248,7 +244,7 @@ pub fn create_channel(user: User, target: Guild, info: Json<CreateChannel>) -> O
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
notifications::send_message_threaded(
|
||||
/*notifications::send_message_threaded(
|
||||
None,
|
||||
target.id.clone(),
|
||||
Notification::guild_channel_create(ChannelCreate {
|
||||
@@ -256,8 +252,8 @@ pub fn create_channel(user: User, target: Guild, info: Json<CreateChannel>) -> O
|
||||
channel: id.clone(),
|
||||
name: name.clone(),
|
||||
description: description.clone(),
|
||||
}),
|
||||
);
|
||||
}), FIXME
|
||||
);*/
|
||||
|
||||
Some(Response::Success(json!({ "id": &id })))
|
||||
} else {
|
||||
@@ -426,14 +422,14 @@ pub fn use_invite(user: User, code: String) -> Response {
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
notifications::send_message_threaded(
|
||||
/*notifications::send_message_threaded(
|
||||
None,
|
||||
guild_id.clone(),
|
||||
Notification::guild_user_join(UserJoin {
|
||||
id: guild_id.clone(),
|
||||
user: user.id.clone(),
|
||||
}),
|
||||
);
|
||||
}), FIXME
|
||||
);*/
|
||||
|
||||
Response::Success(json!({
|
||||
"guild": &guild_id,
|
||||
@@ -642,15 +638,15 @@ pub fn kick_member(user: User, target: Guild, other: String) -> Option<Response>
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
notifications::send_message_threaded(
|
||||
/*notifications::send_message_threaded(
|
||||
None,
|
||||
target.id.clone(),
|
||||
Notification::guild_user_leave(UserLeave {
|
||||
id: target.id.clone(),
|
||||
user: other.clone(),
|
||||
banned: false,
|
||||
}),
|
||||
);
|
||||
}), FIXME
|
||||
);*/
|
||||
|
||||
Some(Response::Result(super::Status::Ok))
|
||||
} else {
|
||||
@@ -734,15 +730,15 @@ pub fn ban_member(
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
notifications::send_message_threaded(
|
||||
/*notifications::send_message_threaded(
|
||||
None,
|
||||
target.id.clone(),
|
||||
Notification::guild_user_leave(UserLeave {
|
||||
id: target.id.clone(),
|
||||
user: other.clone(),
|
||||
banned: true,
|
||||
}),
|
||||
);
|
||||
}), FIXME
|
||||
);*/
|
||||
|
||||
Some(Response::Result(super::Status::Ok))
|
||||
} else {
|
||||
|
||||
@@ -2,10 +2,6 @@ use super::Response;
|
||||
use crate::database::{
|
||||
self, get_relationship, get_relationship_internal, user::User, Relationship,
|
||||
};
|
||||
use crate::notifications::{
|
||||
self,
|
||||
events::{users::*, Notification},
|
||||
};
|
||||
use crate::routes::channel;
|
||||
|
||||
use mongodb::bson::doc;
|
||||
@@ -251,7 +247,7 @@ pub fn add_friend(user: User, target: User) -> Response {
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
notifications::send_message_threaded(
|
||||
/*notifications::send_message_threaded(
|
||||
vec![target.id.clone()],
|
||||
None,
|
||||
Notification::user_friend_status(FriendStatus {
|
||||
@@ -268,8 +264,8 @@ pub fn add_friend(user: User, target: User) -> Response {
|
||||
id: user.id.clone(),
|
||||
user: target.id.clone(),
|
||||
status: Relationship::Friend as i32,
|
||||
}),
|
||||
);
|
||||
}), FIXME
|
||||
);*/
|
||||
|
||||
Response::Success(json!({ "status": Relationship::Friend as i32 }))
|
||||
} else {
|
||||
@@ -324,7 +320,7 @@ pub fn add_friend(user: User, target: User) -> Response {
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
notifications::send_message_threaded(
|
||||
/*notifications::send_message_threaded(
|
||||
vec![user.id.clone()],
|
||||
None,
|
||||
Notification::user_friend_status(FriendStatus {
|
||||
@@ -341,8 +337,8 @@ pub fn add_friend(user: User, target: User) -> Response {
|
||||
id: target.id.clone(),
|
||||
user: user.id.clone(),
|
||||
status: Relationship::Incoming as i32,
|
||||
}),
|
||||
);
|
||||
}), FIXME
|
||||
);*/
|
||||
|
||||
Response::Success(json!({ "status": Relationship::Outgoing as i32 }))
|
||||
} else {
|
||||
@@ -401,7 +397,7 @@ pub fn remove_friend(user: User, target: User) -> Response {
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
notifications::send_message_threaded(
|
||||
/*notifications::send_message_threaded(
|
||||
vec![user.id.clone()],
|
||||
None,
|
||||
Notification::user_friend_status(FriendStatus {
|
||||
@@ -418,8 +414,8 @@ pub fn remove_friend(user: User, target: User) -> Response {
|
||||
id: target.id.clone(),
|
||||
user: user.id.clone(),
|
||||
status: Relationship::NONE as i32,
|
||||
}),
|
||||
);
|
||||
}), FIXME
|
||||
);*/
|
||||
|
||||
Response::Success(json!({ "status": Relationship::NONE as i32 }))
|
||||
} else {
|
||||
@@ -477,7 +473,7 @@ pub fn block_user(user: User, target: User) -> Response {
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
notifications::send_message_threaded(
|
||||
/*notifications::send_message_threaded(
|
||||
vec![user.id.clone()],
|
||||
None,
|
||||
Notification::user_friend_status(FriendStatus {
|
||||
@@ -494,8 +490,8 @@ pub fn block_user(user: User, target: User) -> Response {
|
||||
id: target.id.clone(),
|
||||
user: user.id.clone(),
|
||||
status: Relationship::BlockedOther as i32,
|
||||
}),
|
||||
);
|
||||
}), FIXME
|
||||
);*/
|
||||
|
||||
Response::Success(json!({ "status": Relationship::Blocked as i32 }))
|
||||
} else {
|
||||
@@ -545,7 +541,7 @@ pub fn block_user(user: User, target: User) -> Response {
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
notifications::send_message_threaded(
|
||||
/*notifications::send_message_threaded(
|
||||
vec![user.id.clone()],
|
||||
None,
|
||||
Notification::user_friend_status(FriendStatus {
|
||||
@@ -562,8 +558,8 @@ pub fn block_user(user: User, target: User) -> Response {
|
||||
id: target.id.clone(),
|
||||
user: user.id.clone(),
|
||||
status: Relationship::BlockedOther as i32,
|
||||
}),
|
||||
);
|
||||
}), FIXME
|
||||
);*/
|
||||
|
||||
Response::Success(json!({ "status": Relationship::Blocked as i32 }))
|
||||
} else {
|
||||
@@ -596,15 +592,15 @@ pub fn block_user(user: User, target: User) -> Response {
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
notifications::send_message_threaded(
|
||||
/*notifications::send_message_threaded(
|
||||
vec![user.id.clone()],
|
||||
None,
|
||||
Notification::user_friend_status(FriendStatus {
|
||||
id: user.id.clone(),
|
||||
user: target.id.clone(),
|
||||
status: Relationship::Blocked as i32,
|
||||
}),
|
||||
);
|
||||
}), FIXME
|
||||
);*/
|
||||
|
||||
Response::Success(json!({ "status": Relationship::Blocked as i32 }))
|
||||
} else {
|
||||
@@ -640,15 +636,15 @@ pub fn unblock_user(user: User, target: User) -> Response {
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
notifications::send_message_threaded(
|
||||
/*notifications::send_message_threaded(
|
||||
vec![user.id.clone()],
|
||||
None,
|
||||
Notification::user_friend_status(FriendStatus {
|
||||
id: user.id.clone(),
|
||||
user: target.id.clone(),
|
||||
status: Relationship::BlockedOther as i32,
|
||||
}),
|
||||
);
|
||||
}), FIXME
|
||||
);*/
|
||||
|
||||
Response::Success(json!({ "status": Relationship::BlockedOther as i32 }))
|
||||
} else {
|
||||
@@ -690,7 +686,7 @@ pub fn unblock_user(user: User, target: User) -> Response {
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
notifications::send_message_threaded(
|
||||
/*notifications::send_message_threaded(
|
||||
vec![user.id.clone()],
|
||||
None,
|
||||
Notification::user_friend_status(FriendStatus {
|
||||
@@ -707,8 +703,8 @@ pub fn unblock_user(user: User, target: User) -> Response {
|
||||
id: target.id.clone(),
|
||||
user: user.id.clone(),
|
||||
status: Relationship::NONE as i32,
|
||||
}),
|
||||
);
|
||||
}), FIXME
|
||||
);*/
|
||||
|
||||
Response::Success(json!({ "status": Relationship::NONE as i32 }))
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user