urbit_http_api/apps/
chat.rs

1use crate::error::Result;
2use crate::traits::messaging::{AuthoredMessage, Message, Messaging};
3use crate::Channel;
4use crossbeam::channel::Receiver;
5
6/// A struct that provides an interface for interacting with Urbit chats
7pub struct Chat<'a> {
8    pub channel: &'a mut Channel,
9}
10
11impl<'a> Messaging for Chat<'a> {
12    fn channel(&mut self) -> &mut Channel {
13        self.channel
14    }
15}
16
17impl<'a> Chat<'a> {
18    /// Send a message to an Urbit chat.
19    /// Returns the index of the node that was added to Graph Store.
20    pub fn send_chat_message(
21        &mut self,
22        chat_ship: &str,
23        chat_name: &str,
24        message: &Message,
25    ) -> Result<String> {
26        self.send_message(chat_ship, chat_name, message)
27    }
28
29    /// Extracts chat log automatically into a list of formatted `String`s
30    pub fn export_chat_log(&mut self, chat_ship: &str, chat_name: &str) -> Result<Vec<String>> {
31        self.export_message_log(chat_ship, chat_name)
32    }
33
34    /// Extracts a chat's messages as `AuthoredMessage`s
35    pub fn export_chat_authored_messages(
36        &mut self,
37        chat_ship: &str,
38        chat_name: &str,
39    ) -> Result<Vec<AuthoredMessage>> {
40        self.export_authored_messages(chat_ship, chat_name)
41    }
42
43    /// Subscribe to and watch for messages. This method returns a `Receiver` with the
44    /// `AuthoredMessage`s that are posted after subscribing. Simply call `receiver.try_recv()`
45    /// to read the next `AuthoredMessage` if one has been posted.
46    ///
47    /// Technical Note: This method actually creates a new `Channel` with your Urbit Ship, and spawns a new unix thread
48    /// locally that processes all messages on said channel. This is required due to borrowing mechanisms in Rust, however
49    /// on the plus side this makes it potentially more performant by each subscription having it's own unix thread.
50    pub fn subscribe_to_chat(
51        &mut self,
52        chat_ship: &str,
53        chat_name: &str,
54    ) -> Result<Receiver<AuthoredMessage>> {
55        self.subscribe_to_messages(chat_ship, chat_name)
56    }
57}