Skip to main content

Chats

Trait Chats 

Source
pub trait Chats: MaybeSend + 'static {
    // Required methods
    fn watch(&self, events: Events<ChatEvent>);
    fn send(&self, room: String, text: String, reply: Reply<()>);

    // Provided methods
    fn capabilities(&self) -> ChatCapabilities { ... }
    fn find_users(&self, query: String, reply: Reply<Vec<ChatUser>>) { ... }
    fn create_room(&self, room: NewChatRoom, reply: Reply<String>) { ... }
    fn add_member(&self, room: String, user: String, reply: Reply<()>) { ... }
    fn remove_member(&self, room: String, user: String, reply: Reply<()>) { ... }
}
Expand description

The host’s chat rooms, for the chats panel. The host keeps the account, the rooms and their messages, and moderates; the panel shows what the service pushes and sends what the user writes through send.

A message the user sends appears once the host pushes it back. Choosing recipients, creating rooms and changing their members are optional operations: the host turns each on in capabilities, keeps the accounts and decides whether the signed-in user may do it, and answers a refusal with an error that the panel shows. Methods are called on the UI thread and must return at once; answer and push from any thread, runtime or browser task.

use std::sync::Mutex;

use egui_charts::data::Reply;
use egui_charts::services::{ChatEvent, ChatMessage, Chats, Events};

/// One room that echoes what the user writes. A real host sends it to its chat server.
#[derive(Default)]
struct Echo(Mutex<Option<Events<ChatEvent>>>);

impl Chats for Echo {
    fn watch(&self, events: Events<ChatEvent>) {
        events.push(ChatEvent::Room("General".into()));
        *self.0.lock().unwrap() = Some(events);
    }

    fn send(&self, room: String, text: String, reply: Reply<()>) {
        if let Some(events) = &*self.0.lock().unwrap() {
            let message = ChatMessage::new(room, "You", chrono::Utc::now(), text);
            events.push(ChatEvent::Message(message));
        }
        reply.send(Ok(()));
    }
}

Required Methods§

Source

fn watch(&self, events: Events<ChatEvent>)

Pushes the user’s rooms and their recent messages, oldest first, then every new room and message until events is inactive. Called when the panel starts and again after a lagged stream, each time with a new stream that starts from no rooms.

The stream takes at most 10,000 pushes between two frames: push at most that many recent messages at once, or the stream lags on every call and nothing shows.

Source

fn send(&self, room: String, text: String, reply: Reply<()>)

Posts text to room. Answer Ok once it is posted, and push the message.

Provided Methods§

Source

fn capabilities(&self) -> ChatCapabilities

The optional operations below this host supports. The default supports none, and the panel then offers only to read and post. Read each frame, so it may change with the account.

Source

fn find_users(&self, query: String, reply: Reply<Vec<ChatUser>>)

The users whose name matches query, for choosing recipients and members. The default answers FeedError::NotSupported.

Source

fn create_room(&self, room: NewChatRoom, reply: Reply<String>)

Creates room and answers its identifier once it exists. Push the room, as ChatEvent::Room or ChatEvent::Members, for the panel to show it. The default answers FeedError::NotSupported.

Source

fn add_member(&self, room: String, user: String, reply: Reply<()>)

Adds the user with identifier user to room. Answer Ok once it is done, and push the new ChatEvent::Members. The default answers FeedError::NotSupported.

Source

fn remove_member(&self, room: String, user: String, reply: Reply<()>)

Removes the user with identifier user from room. Answer Ok once it is done, and push the new ChatEvent::Members. The default answers FeedError::NotSupported.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§