lemmy/crates/api_common/src/websocket/handlers/online_users.rs
Dessalines 63f54a3103
Making the chat server an actor. (#2793)
* Making the chat server an actor.

- Fixes #2778
- #2787

* Forgot to add handlers folder.

* Some cleanup.

* Forgot to remove a comment.

* Address PR comments.

* Using ToString for enum operations.
2023-04-13 06:53:55 -04:00

56 lines
1.4 KiB
Rust

use crate::websocket::chat_server::ChatServer;
use actix::{Context, Handler, Message};
use lemmy_db_schema::newtypes::{CommunityId, PostId};
/// Getting the number of online connections
#[derive(Message)]
#[rtype(usize)]
pub struct GetUsersOnline;
/// Handler for Disconnect message.
impl Handler<GetUsersOnline> for ChatServer {
type Result = usize;
fn handle(&mut self, _msg: GetUsersOnline, _: &mut Context<Self>) -> Self::Result {
self.sessions.len()
}
}
/// Getting the number of post users online
#[derive(Message)]
#[rtype(usize)]
pub struct GetPostUsersOnline {
pub post_id: PostId,
}
/// Handler for Disconnect message.
impl Handler<GetPostUsersOnline> for ChatServer {
type Result = usize;
fn handle(&mut self, msg: GetPostUsersOnline, _: &mut Context<Self>) -> Self::Result {
self
.post_rooms
.get(&msg.post_id)
.map_or(1, std::collections::HashSet::len)
}
}
/// Getting the number of post users online
#[derive(Message)]
#[rtype(usize)]
pub struct GetCommunityUsersOnline {
pub community_id: CommunityId,
}
/// Handler for Disconnect message.
impl Handler<GetCommunityUsersOnline> for ChatServer {
type Result = usize;
fn handle(&mut self, msg: GetCommunityUsersOnline, _: &mut Context<Self>) -> Self::Result {
self
.community_rooms
.get(&msg.community_id)
.map_or(1, std::collections::HashSet::len)
}
}