From cf6063feefe75c727dd2735bbddb24823145415f Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 6 Feb 2021 08:55:14 +0100 Subject: [PATCH] Can register / unregister a conversation --- src/controllers/user_ws_actions.rs | 14 ++++++++++++++ src/controllers/user_ws_controller.rs | 6 +++++- src/controllers/user_ws_routes.rs | 2 ++ src/data/base_request_handler.rs | 3 ++- src/data/conversation.rs | 4 +++- 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/controllers/user_ws_actions.rs b/src/controllers/user_ws_actions.rs index 3f72bd9..df7c6d4 100644 --- a/src/controllers/user_ws_actions.rs +++ b/src/controllers/user_ws_actions.rs @@ -12,4 +12,18 @@ pub fn set_incognito(r: &mut UserWsRequestHandler) -> Res { r.update_conn(|c| c.incognito = new_state)?; r.success("Updated.") +} + +/// Register a conversation +pub fn register_conv(r: &mut UserWsRequestHandler) -> Res { + let conv_id = r.post_conv_id("convID")?; + r.update_conn(|c| { c.conversations.insert(conv_id); })?; + r.success("ok") +} + +/// Un-register a conversation +pub fn unregister_conv(r: &mut UserWsRequestHandler) -> Res { + let conv_id = r.post_u64("convID")?; + r.update_conn(|c| { c.conversations.remove(&conv_id); })?; + r.success("ok") } \ No newline at end of file diff --git a/src/controllers/user_ws_controller.rs b/src/controllers/user_ws_controller.rs index 41a9f21..263b4b1 100644 --- a/src/controllers/user_ws_controller.rs +++ b/src/controllers/user_ws_controller.rs @@ -2,7 +2,7 @@ //! //! Handles the WebSocket offered to the users -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use std::time::{Duration, Instant}; use actix::{Actor, ActorContext, Addr, AsyncContext, Handler, Running, StreamHandler}; @@ -99,12 +99,14 @@ mod ws_tokens_list { /// WebSocket connections list mod ws_connections_list { + use std::collections::HashSet; use std::sync::Arc; use std::sync::Mutex; use actix::Addr; use crate::controllers::user_ws_controller::WsSession; + use crate::data::conversation::ConvID; use crate::data::user::UserID; /// This structure contains information about an active connection @@ -115,6 +117,7 @@ mod ws_connections_list { pub remote_ip: String, pub session: actix::Addr, pub incognito: bool, + pub conversations: HashSet, } impl WsConnection { @@ -326,6 +329,7 @@ impl Actor for WsSession { remote_ip: self.remote_ip.clone(), session: ctx.address(), incognito: self.incognito, + conversations: HashSet::new(), }) } diff --git a/src/controllers/user_ws_routes.rs b/src/controllers/user_ws_routes.rs index f718e5e..0c65398 100644 --- a/src/controllers/user_ws_routes.rs +++ b/src/controllers/user_ws_routes.rs @@ -29,6 +29,8 @@ pub fn get_user_ws_routes() -> Vec { vec![ // Main controller UserWsRoute::new("$main/set_incognito", user_ws_actions::set_incognito), + UserWsRoute::new("$main/register_conv", user_ws_actions::register_conv), + UserWsRoute::new("$main/unregister_conv", user_ws_actions::unregister_conv), // Likes controller UserWsRoute::new("likes/update", likes_controller::update) diff --git a/src/data/base_request_handler.rs b/src/data/base_request_handler.rs index 90a9cdc..91e0e07 100644 --- a/src/data/base_request_handler.rs +++ b/src/data/base_request_handler.rs @@ -13,6 +13,7 @@ use crate::api_data::http_error::HttpError; use crate::constants::PASSWORD_MIN_LENGTH; use crate::controllers::routes::RequestResult; use crate::data::comment::Comment; +use crate::data::conversation::ConvID; use crate::data::custom_emoji::CustomEmoji; use crate::data::error::{ExecError, ResultBoxError}; use crate::data::group::GroupAccessLevel; @@ -424,7 +425,7 @@ pub trait BaseRequestHandler { } /// Get & return the ID of the conversation included in the POST request - fn post_conv_id(&mut self, name: &str) -> ResultBoxError { + fn post_conv_id(&mut self, name: &str) -> ResultBoxError { let conv_id = self.post_u64(name)?; if !conversations_helper::does_user_belongs_to(&self.user_id()?, conv_id)? { diff --git a/src/data/conversation.rs b/src/data/conversation.rs index 9396b47..0af2868 100644 --- a/src/data/conversation.rs +++ b/src/data/conversation.rs @@ -4,9 +4,11 @@ use crate::data::user::UserID; +pub type ConvID = u64; + #[derive(Debug, PartialEq, Eq)] pub struct Conversation { - pub id: u64, + pub id: ConvID, pub owner_id: UserID, pub name: Option, pub members: Vec,