2023-07-06 22:52:16 +00:00
|
|
|
use matrix_sdk::{Client,
|
|
|
|
ruma::{events::{AnyTimelineEvent}},
|
|
|
|
deserialized_responses::TimelineEvent,
|
|
|
|
room::MessagesOptions};
|
|
|
|
use anyhow::{Result, Error};
|
|
|
|
use cli_log::{error, warn, info};
|
2023-07-04 16:32:57 +00:00
|
|
|
|
|
|
|
pub enum State {
|
|
|
|
None,
|
|
|
|
Main,
|
|
|
|
Setup,
|
|
|
|
}
|
|
|
|
|
2023-07-06 22:52:16 +00:00
|
|
|
pub struct Room {
|
|
|
|
matrix_room: matrix_sdk::room::Joined,
|
|
|
|
timeline: Vec<AnyTimelineEvent>,
|
|
|
|
timeline_end: Option<String>,
|
|
|
|
}
|
|
|
|
|
2023-07-04 16:32:57 +00:00
|
|
|
pub struct Status {
|
|
|
|
state: State,
|
|
|
|
account_name: String,
|
|
|
|
account_user_id: String,
|
2023-07-06 18:47:06 +00:00
|
|
|
|
|
|
|
client: Option<Client>,
|
2023-07-06 22:52:16 +00:00
|
|
|
rooms: Vec<Room>,
|
2023-07-06 18:47:06 +00:00
|
|
|
current_room_id: Option<u32>,
|
2023-07-04 16:32:57 +00:00
|
|
|
}
|
|
|
|
|
2023-07-06 22:52:16 +00:00
|
|
|
impl Room {
|
|
|
|
pub fn new (matrix_room: matrix_sdk::room::Joined) -> Self {
|
|
|
|
Self {
|
|
|
|
matrix_room,
|
|
|
|
timeline: Vec::new(),
|
|
|
|
timeline_end: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn poll_old_timeline(&mut self) -> Result<()> {
|
|
|
|
let mut messages_options = MessagesOptions::backward();
|
|
|
|
messages_options = match &self.timeline_end {
|
|
|
|
Some(end) => messages_options.from(end.as_str()),
|
|
|
|
None => messages_options
|
|
|
|
};
|
|
|
|
let events = self.matrix_room.messages(messages_options).await?;
|
|
|
|
self.timeline_end = events.end;
|
|
|
|
|
|
|
|
for event in events.chunk.iter() {
|
|
|
|
self.timeline.insert(0, match event.event.deserialize() {
|
|
|
|
Ok(ev) => ev,
|
|
|
|
Err(err) => {
|
|
|
|
warn!("Failed to deserialize timeline event - {err}");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn timeline_add(&mut self, event: AnyTimelineEvent) {
|
|
|
|
self.timeline.push(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn timeline(&self) -> &Vec<AnyTimelineEvent> { &self.timeline }
|
|
|
|
}
|
|
|
|
|
2023-07-06 18:47:06 +00:00
|
|
|
impl Status {
|
|
|
|
pub fn new(client: Option<Client>) -> Self {
|
|
|
|
let rooms = match &client {
|
2023-07-06 22:52:16 +00:00
|
|
|
Some(c) => {
|
|
|
|
c.joined_rooms()
|
|
|
|
.iter()
|
|
|
|
.map(|r| {
|
|
|
|
Room::new(r.clone())
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
},
|
2023-07-06 18:47:06 +00:00
|
|
|
None => Vec::new(),
|
|
|
|
};
|
|
|
|
|
2023-07-04 16:32:57 +00:00
|
|
|
Self {
|
|
|
|
state: State::None,
|
|
|
|
account_name: "".to_string(),
|
|
|
|
account_user_id: "".to_string(),
|
2023-07-06 18:47:06 +00:00
|
|
|
client,
|
|
|
|
rooms,
|
|
|
|
current_room_id: Some(0),
|
2023-07-04 16:32:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn account_name(&self) -> &String {
|
|
|
|
&self.account_name
|
|
|
|
}
|
|
|
|
|
2023-07-06 15:34:55 +00:00
|
|
|
pub fn set_account_name(&mut self, name: String) {
|
|
|
|
self.account_name = name;
|
|
|
|
}
|
|
|
|
|
2023-07-04 16:32:57 +00:00
|
|
|
pub fn account_user_id(&self) -> &String {
|
|
|
|
&self.account_user_id
|
|
|
|
}
|
|
|
|
|
2023-07-06 15:34:55 +00:00
|
|
|
pub fn set_account_user_id(&mut self, user_id: String) {
|
|
|
|
self.account_user_id = user_id;
|
|
|
|
}
|
|
|
|
|
2023-07-06 22:52:16 +00:00
|
|
|
pub fn room(&self) -> Option<&Room> {
|
2023-07-06 18:47:06 +00:00
|
|
|
self.rooms.get(self.current_room_id? as usize)
|
2023-07-04 16:32:57 +00:00
|
|
|
}
|
2023-07-06 22:52:16 +00:00
|
|
|
|
|
|
|
pub fn rooms(&self) -> &Vec<Room> {
|
|
|
|
&self.rooms
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rooms_mut(&mut self) -> &mut Vec<Room> {
|
|
|
|
&mut self.rooms
|
|
|
|
}
|
2023-07-04 16:32:57 +00:00
|
|
|
|
|
|
|
pub fn state(&self) -> &State {
|
|
|
|
&self.state
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_state(&mut self, state: State) {
|
|
|
|
self.state = state;
|
|
|
|
}
|
|
|
|
}
|