1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-02-16 22:12:39 +00:00

Can register / unregister a conversation

This commit is contained in:
Pierre HUBERT 2021-02-06 08:55:14 +01:00
parent fad21381ed
commit cf6063feef
5 changed files with 26 additions and 3 deletions

View File

@ -12,4 +12,18 @@ pub fn set_incognito(r: &mut UserWsRequestHandler) -> Res {
r.update_conn(|c| c.incognito = new_state)?; r.update_conn(|c| c.incognito = new_state)?;
r.success("Updated.") 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")
} }

View File

@ -2,7 +2,7 @@
//! //!
//! Handles the WebSocket offered to the users //! Handles the WebSocket offered to the users
use std::collections::HashMap; use std::collections::{HashMap, HashSet};
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use actix::{Actor, ActorContext, Addr, AsyncContext, Handler, Running, StreamHandler}; use actix::{Actor, ActorContext, Addr, AsyncContext, Handler, Running, StreamHandler};
@ -99,12 +99,14 @@ mod ws_tokens_list {
/// WebSocket connections list /// WebSocket connections list
mod ws_connections_list { mod ws_connections_list {
use std::collections::HashSet;
use std::sync::Arc; use std::sync::Arc;
use std::sync::Mutex; use std::sync::Mutex;
use actix::Addr; use actix::Addr;
use crate::controllers::user_ws_controller::WsSession; use crate::controllers::user_ws_controller::WsSession;
use crate::data::conversation::ConvID;
use crate::data::user::UserID; use crate::data::user::UserID;
/// This structure contains information about an active connection /// This structure contains information about an active connection
@ -115,6 +117,7 @@ mod ws_connections_list {
pub remote_ip: String, pub remote_ip: String,
pub session: actix::Addr<WsSession>, pub session: actix::Addr<WsSession>,
pub incognito: bool, pub incognito: bool,
pub conversations: HashSet<ConvID>,
} }
impl WsConnection { impl WsConnection {
@ -326,6 +329,7 @@ impl Actor for WsSession {
remote_ip: self.remote_ip.clone(), remote_ip: self.remote_ip.clone(),
session: ctx.address(), session: ctx.address(),
incognito: self.incognito, incognito: self.incognito,
conversations: HashSet::new(),
}) })
} }

View File

@ -29,6 +29,8 @@ pub fn get_user_ws_routes() -> Vec<UserWsRoute> {
vec![ vec![
// Main controller // Main controller
UserWsRoute::new("$main/set_incognito", user_ws_actions::set_incognito), 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 // Likes controller
UserWsRoute::new("likes/update", likes_controller::update) UserWsRoute::new("likes/update", likes_controller::update)

View File

@ -13,6 +13,7 @@ use crate::api_data::http_error::HttpError;
use crate::constants::PASSWORD_MIN_LENGTH; use crate::constants::PASSWORD_MIN_LENGTH;
use crate::controllers::routes::RequestResult; use crate::controllers::routes::RequestResult;
use crate::data::comment::Comment; use crate::data::comment::Comment;
use crate::data::conversation::ConvID;
use crate::data::custom_emoji::CustomEmoji; use crate::data::custom_emoji::CustomEmoji;
use crate::data::error::{ExecError, ResultBoxError}; use crate::data::error::{ExecError, ResultBoxError};
use crate::data::group::GroupAccessLevel; use crate::data::group::GroupAccessLevel;
@ -424,7 +425,7 @@ pub trait BaseRequestHandler {
} }
/// Get & return the ID of the conversation included in the POST request /// Get & return the ID of the conversation included in the POST request
fn post_conv_id(&mut self, name: &str) -> ResultBoxError<u64> { fn post_conv_id(&mut self, name: &str) -> ResultBoxError<ConvID> {
let conv_id = self.post_u64(name)?; let conv_id = self.post_u64(name)?;
if !conversations_helper::does_user_belongs_to(&self.user_id()?, conv_id)? { if !conversations_helper::does_user_belongs_to(&self.user_id()?, conv_id)? {

View File

@ -4,9 +4,11 @@
use crate::data::user::UserID; use crate::data::user::UserID;
pub type ConvID = u64;
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub struct Conversation { pub struct Conversation {
pub id: u64, pub id: ConvID,
pub owner_id: UserID, pub owner_id: UserID,
pub name: Option<String>, pub name: Option<String>,
pub members: Vec<UserID>, pub members: Vec<UserID>,