1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-12-27 22:18:51 +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.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
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<WsSession>,
pub incognito: bool,
pub conversations: HashSet<ConvID>,
}
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(),
})
}

View File

@ -29,6 +29,8 @@ pub fn get_user_ws_routes() -> Vec<UserWsRoute> {
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)

View File

@ -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<u64> {
fn post_conv_id(&mut self, name: &str) -> ResultBoxError<ConvID> {
let conv_id = self.post_u64(name)?;
if !conversations_helper::does_user_belongs_to(&self.user_id()?, conv_id)? {

View File

@ -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<String>,
pub members: Vec<UserID>,