2020-12-10 13:17:41 +01:00
|
|
|
use std::{
|
|
|
|
collections::{BTreeMap, HashMap, VecDeque},
|
|
|
|
time::SystemTime,
|
|
|
|
};
|
2020-12-02 06:52:21 +01:00
|
|
|
|
2020-12-07 17:12:21 +01:00
|
|
|
use futures::executor::block_on;
|
2020-11-23 07:52:07 +01:00
|
|
|
use iced::{
|
|
|
|
text_input::{self, TextInput},
|
2020-12-03 12:12:07 +01:00
|
|
|
Application, Button, Column, Command, Container, Element, Length, Radio, Row, Rule, Scrollable,
|
2020-12-02 06:52:21 +01:00
|
|
|
Subscription, Text,
|
|
|
|
};
|
|
|
|
use matrix_sdk::{
|
|
|
|
events::{
|
2020-12-08 16:40:36 +01:00
|
|
|
key::verification::cancel::CancelCode as VerificationCancelCode,
|
2020-12-03 12:12:07 +01:00
|
|
|
room::message::MessageEventContent, AnyMessageEventContent,
|
2020-12-10 13:17:41 +01:00
|
|
|
AnyPossiblyRedactedSyncMessageEvent, AnyRoomEvent, AnyStateEvent, AnySyncMessageEvent,
|
|
|
|
AnyToDeviceEvent,
|
2020-12-02 06:52:21 +01:00
|
|
|
},
|
2020-12-10 13:17:41 +01:00
|
|
|
identifiers::{RoomAliasId, RoomId, UserId},
|
2020-11-23 07:52:07 +01:00
|
|
|
};
|
|
|
|
|
2020-11-24 09:12:24 +01:00
|
|
|
use crate::matrix;
|
|
|
|
|
2020-12-10 13:17:41 +01:00
|
|
|
pub trait AnyRoomEventExt {
|
|
|
|
fn origin_server_ts(&self) -> SystemTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AnyRoomEventExt for AnyRoomEvent {
|
|
|
|
fn origin_server_ts(&self) -> SystemTime {
|
|
|
|
match self {
|
|
|
|
AnyRoomEvent::Message(e) => e.origin_server_ts(),
|
|
|
|
AnyRoomEvent::State(e) => e.origin_server_ts(),
|
|
|
|
AnyRoomEvent::RedactedMessage(e) => e.origin_server_ts(),
|
|
|
|
AnyRoomEvent::RedactedState(e) => e.origin_server_ts(),
|
|
|
|
}
|
|
|
|
.to_owned()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-03 12:46:44 +01:00
|
|
|
/// View for the login prompt
|
|
|
|
#[derive(Debug, Clone, Default)]
|
|
|
|
pub struct PromptView {
|
2020-12-08 16:40:36 +01:00
|
|
|
/// Username input field
|
2020-12-03 12:46:44 +01:00
|
|
|
user_input: text_input::State,
|
2020-12-08 16:40:36 +01:00
|
|
|
/// Password input field
|
2020-12-03 12:46:44 +01:00
|
|
|
password_input: text_input::State,
|
2020-12-08 16:40:36 +01:00
|
|
|
/// Homeserver input field
|
2020-12-03 12:46:44 +01:00
|
|
|
server_input: text_input::State,
|
2020-12-08 16:40:36 +01:00
|
|
|
/// Device name input field
|
|
|
|
device_input: text_input::State,
|
|
|
|
/// Button to trigger login
|
2020-12-03 12:46:44 +01:00
|
|
|
login_button: iced::button::State,
|
2020-11-23 17:18:05 +01:00
|
|
|
|
2020-12-08 16:40:36 +01:00
|
|
|
/// Username
|
2020-12-03 12:46:44 +01:00
|
|
|
user: String,
|
2020-12-08 16:40:36 +01:00
|
|
|
/// Password
|
2020-12-03 12:46:44 +01:00
|
|
|
password: String,
|
2020-12-08 16:40:36 +01:00
|
|
|
/// Homeserver
|
2020-12-03 12:46:44 +01:00
|
|
|
server: String,
|
2020-12-08 16:40:36 +01:00
|
|
|
/// Device name to create login session under
|
|
|
|
device_name: String,
|
|
|
|
/// Whether to log in or sign up
|
2020-12-03 12:46:44 +01:00
|
|
|
action: PromptAction,
|
2020-12-08 16:40:36 +01:00
|
|
|
/// Error message
|
2020-12-03 12:46:44 +01:00
|
|
|
error: Option<String>,
|
|
|
|
}
|
2020-11-24 16:11:27 +01:00
|
|
|
|
2020-12-03 12:46:44 +01:00
|
|
|
impl PromptView {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn view(&mut self) -> Element<Message> {
|
|
|
|
let mut content = Column::new()
|
|
|
|
.width(500.into())
|
2020-12-08 16:40:36 +01:00
|
|
|
.spacing(5)
|
2020-12-03 12:46:44 +01:00
|
|
|
.push(
|
|
|
|
Row::new()
|
|
|
|
.spacing(15)
|
|
|
|
.push(Radio::new(
|
|
|
|
PromptAction::Login,
|
|
|
|
"Login",
|
|
|
|
Some(self.action),
|
|
|
|
Message::SetAction,
|
|
|
|
))
|
|
|
|
.push(Radio::new(
|
|
|
|
PromptAction::Signup,
|
|
|
|
"Sign up",
|
|
|
|
Some(self.action),
|
|
|
|
Message::SetAction,
|
|
|
|
)),
|
|
|
|
)
|
|
|
|
.push(
|
2020-12-08 16:40:36 +01:00
|
|
|
Column::new().push(Text::new("Username")).push(
|
|
|
|
TextInput::new(
|
|
|
|
&mut self.user_input,
|
|
|
|
"Username",
|
|
|
|
&self.user,
|
|
|
|
Message::SetUser,
|
|
|
|
)
|
|
|
|
.padding(5),
|
|
|
|
),
|
2020-12-03 12:46:44 +01:00
|
|
|
)
|
|
|
|
.push(
|
2020-12-08 16:40:36 +01:00
|
|
|
Column::new().push(Text::new("Password")).push(
|
|
|
|
TextInput::new(
|
|
|
|
&mut self.password_input,
|
|
|
|
"Password",
|
|
|
|
&self.password,
|
|
|
|
Message::SetPassword,
|
|
|
|
)
|
|
|
|
.password()
|
|
|
|
.padding(5),
|
|
|
|
),
|
2020-12-03 12:46:44 +01:00
|
|
|
)
|
|
|
|
.push(
|
2020-12-08 16:40:36 +01:00
|
|
|
Column::new().push(Text::new("Homeserver")).push(
|
|
|
|
TextInput::new(
|
|
|
|
&mut self.server_input,
|
|
|
|
"https://homeserver.com",
|
|
|
|
&self.server,
|
|
|
|
Message::SetServer,
|
|
|
|
)
|
|
|
|
.padding(5),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.push(
|
|
|
|
Column::new().push(Text::new("Device name")).push(
|
|
|
|
TextInput::new(
|
|
|
|
&mut self.device_input,
|
|
|
|
"retrix on my laptop",
|
|
|
|
&self.device_name,
|
|
|
|
Message::SetDeviceName,
|
|
|
|
)
|
|
|
|
.padding(5),
|
|
|
|
),
|
2020-12-03 12:46:44 +01:00
|
|
|
);
|
|
|
|
let button = match self.action {
|
|
|
|
PromptAction::Login => {
|
|
|
|
Button::new(&mut self.login_button, Text::new("Login")).on_press(Message::Login)
|
|
|
|
}
|
|
|
|
PromptAction::Signup => {
|
|
|
|
content = content.push(
|
|
|
|
Text::new("NB: Signup is very naively implemented, and prone to breaking")
|
2020-12-08 16:40:36 +01:00
|
|
|
.color([1.0, 0.5, 0.0]),
|
2020-12-03 12:46:44 +01:00
|
|
|
);
|
|
|
|
Button::new(&mut self.login_button, Text::new("Sign up")).on_press(Message::Signup)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
content = content.push(button);
|
|
|
|
if let Some(ref error) = self.error {
|
|
|
|
content = content.push(Text::new(error).color([1.0, 0.0, 0.0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
Container::new(content)
|
|
|
|
.center_x()
|
|
|
|
.center_y()
|
|
|
|
.width(iced::Length::Fill)
|
|
|
|
.height(iced::Length::Fill)
|
|
|
|
.into()
|
|
|
|
}
|
2020-11-23 07:52:07 +01:00
|
|
|
}
|
|
|
|
|
2020-12-07 17:12:21 +01:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub enum RoomSorting {
|
|
|
|
Recent,
|
|
|
|
Alphabetic,
|
|
|
|
}
|
|
|
|
|
2020-12-10 13:17:41 +01:00
|
|
|
/// Data for en entry in the room list
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct RoomEntry {
|
|
|
|
/// Cached calculated name
|
|
|
|
name: String,
|
|
|
|
/// Room topic
|
|
|
|
topic: String,
|
|
|
|
/// Canonical alias
|
|
|
|
alias: Option<RoomAliasId>,
|
|
|
|
/// Defined display name
|
|
|
|
display_name: Option<String>,
|
|
|
|
/// Person we're in a direct message with
|
|
|
|
direct: Option<UserId>,
|
|
|
|
/// Button to select the room
|
|
|
|
button: iced::button::State,
|
|
|
|
/// Most recent activity in the room
|
|
|
|
updated: std::time::SystemTime,
|
|
|
|
/// Cache of messages
|
|
|
|
messages: MessageBuffer,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RoomEntry {
|
|
|
|
/// Recalculate displayname
|
|
|
|
pub fn update_display_name(&mut self, id: &RoomId) {
|
|
|
|
self.name = if let Some(ref name) = self.display_name {
|
|
|
|
name.to_owned()
|
|
|
|
} else if let Some(ref user) = self.direct {
|
|
|
|
user.to_string()
|
|
|
|
} else if let Some(ref alias) = self.alias {
|
|
|
|
alias.to_string()
|
|
|
|
} else {
|
|
|
|
id.to_string()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for RoomEntry {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
name: Default::default(),
|
|
|
|
topic: String::new(),
|
|
|
|
alias: None,
|
|
|
|
display_name: None,
|
|
|
|
direct: None,
|
|
|
|
button: Default::default(),
|
|
|
|
updated: std::time::SystemTime::UNIX_EPOCH,
|
|
|
|
messages: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RoomEntry {
|
|
|
|
fn update_time(&mut self) {
|
|
|
|
self.updated = self.messages.update_time();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
pub struct MessageBuffer {
|
|
|
|
messages: VecDeque<AnyRoomEvent>,
|
|
|
|
/// Token for the start of the messages we have
|
|
|
|
start: String,
|
|
|
|
/// Token for the end of the messages we have
|
|
|
|
end: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MessageBuffer {
|
|
|
|
/// Sorts the messages by send time
|
|
|
|
fn sort(&mut self) {
|
|
|
|
self.messages
|
|
|
|
.make_contiguous()
|
|
|
|
.sort_unstable_by(|a, b| a.origin_server_ts().cmp(&b.origin_server_ts()).reverse())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the send time of the most recently sent message
|
|
|
|
fn update_time(&self) -> SystemTime {
|
|
|
|
match self.messages.back() {
|
|
|
|
Some(message) => message.origin_server_ts(),
|
|
|
|
None => SystemTime::UNIX_EPOCH,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Insert a message that's probably the most recent
|
|
|
|
pub fn push_back(&mut self, event: AnyRoomEvent) {
|
|
|
|
self.messages.push_back(event);
|
|
|
|
self.sort();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn push_front(&mut self, event: AnyRoomEvent) {
|
|
|
|
self.messages.push_front(event);
|
|
|
|
self.sort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-03 12:46:44 +01:00
|
|
|
/// Main view after successful login
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct MainView {
|
2020-12-08 16:40:36 +01:00
|
|
|
/// Settings view, if open
|
2020-12-07 17:12:21 +01:00
|
|
|
settings_view: Option<SettingsView>,
|
2020-12-08 16:40:36 +01:00
|
|
|
/// The matrix-sdk client
|
2020-12-03 12:46:44 +01:00
|
|
|
client: matrix_sdk::Client,
|
|
|
|
session: matrix::Session,
|
2020-12-08 16:40:36 +01:00
|
|
|
/// Draft of message to send
|
2020-12-07 17:12:21 +01:00
|
|
|
draft: String,
|
2020-12-08 16:40:36 +01:00
|
|
|
/// Potential error message
|
2020-12-08 09:36:29 +01:00
|
|
|
error: Option<(String, iced::button::State)>,
|
2020-12-08 16:40:36 +01:00
|
|
|
/// Selected room
|
2020-12-07 17:12:21 +01:00
|
|
|
selected: Option<RoomId>,
|
2020-12-08 16:40:36 +01:00
|
|
|
/// Potential verification flow
|
2020-12-07 17:12:21 +01:00
|
|
|
sas: Option<matrix_sdk::Sas>,
|
2020-12-08 16:40:36 +01:00
|
|
|
/// Whether to sort rooms alphabetically or by activity
|
2020-12-07 17:12:21 +01:00
|
|
|
sorting: RoomSorting,
|
2020-12-10 13:17:41 +01:00
|
|
|
rooms: BTreeMap<RoomId, RoomEntry>,
|
2020-12-07 17:12:21 +01:00
|
|
|
|
2020-12-08 16:40:36 +01:00
|
|
|
/// Room list entry/button to select room
|
2020-12-07 17:12:21 +01:00
|
|
|
buttons: HashMap<RoomId, iced::button::State>,
|
2020-12-08 16:40:36 +01:00
|
|
|
/// Room list scrollbar state
|
2020-12-03 12:46:44 +01:00
|
|
|
room_scroll: iced::scrollable::State,
|
2020-12-08 16:40:36 +01:00
|
|
|
/// Message view scrollbar state
|
2020-12-03 12:46:44 +01:00
|
|
|
message_scroll: iced::scrollable::State,
|
2020-12-08 16:40:36 +01:00
|
|
|
/// Message draft text input
|
2020-12-03 12:46:44 +01:00
|
|
|
message_input: iced::text_input::State,
|
2020-12-08 16:40:36 +01:00
|
|
|
/// Button to send drafted message
|
2020-12-03 12:46:44 +01:00
|
|
|
send_button: iced::button::State,
|
2020-12-08 16:40:36 +01:00
|
|
|
/// Button to open settings menu
|
2020-12-07 17:12:21 +01:00
|
|
|
settings_button: iced::button::State,
|
2020-12-08 16:40:36 +01:00
|
|
|
/// Button for accepting/continuing verification
|
2020-12-07 17:12:21 +01:00
|
|
|
sas_accept_button: iced::button::State,
|
2020-12-08 16:40:36 +01:00
|
|
|
/// Button for cancelling verification
|
2020-12-07 17:12:21 +01:00
|
|
|
sas_deny_button: iced::button::State,
|
2020-12-03 12:46:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MainView {
|
|
|
|
pub fn new(client: matrix_sdk::Client, session: matrix::Session) -> Self {
|
|
|
|
Self {
|
|
|
|
client,
|
|
|
|
session,
|
2020-12-07 17:12:21 +01:00
|
|
|
settings_view: None,
|
|
|
|
settings_button: Default::default(),
|
2020-12-08 09:36:29 +01:00
|
|
|
error: None,
|
2020-12-07 17:12:21 +01:00
|
|
|
sas: None,
|
2020-12-03 12:46:44 +01:00
|
|
|
rooms: Default::default(),
|
|
|
|
selected: None,
|
|
|
|
room_scroll: Default::default(),
|
|
|
|
message_scroll: Default::default(),
|
|
|
|
message_input: Default::default(),
|
|
|
|
buttons: Default::default(),
|
|
|
|
draft: String::new(),
|
|
|
|
send_button: Default::default(),
|
2020-12-10 13:17:41 +01:00
|
|
|
sorting: RoomSorting::Alphabetic,
|
2020-12-07 17:12:21 +01:00
|
|
|
sas_accept_button: Default::default(),
|
|
|
|
sas_deny_button: Default::default(),
|
2020-12-03 12:46:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn view(&mut self) -> Element<Message> {
|
2020-12-07 17:12:21 +01:00
|
|
|
// If settings view is open, display that instead
|
|
|
|
if let Some(ref mut settings) = self.settings_view {
|
2020-12-08 16:40:36 +01:00
|
|
|
return settings.view(self.sorting);
|
2020-12-07 17:12:21 +01:00
|
|
|
}
|
2020-12-03 12:46:44 +01:00
|
|
|
let mut root_row = Row::new().width(Length::Fill).height(Length::Fill);
|
2020-11-24 21:49:06 +01:00
|
|
|
|
2020-12-03 12:46:44 +01:00
|
|
|
// Room list
|
|
|
|
let joined = self.client.joined_rooms();
|
|
|
|
let rooms = futures::executor::block_on(async { joined.read().await });
|
2020-12-07 17:12:21 +01:00
|
|
|
let mut room_scroll = Scrollable::new(&mut self.room_scroll)
|
2020-12-08 16:40:36 +01:00
|
|
|
.width(300.into())
|
2020-12-03 12:46:44 +01:00
|
|
|
.height(Length::Fill)
|
|
|
|
.scrollbar_width(5);
|
|
|
|
// We have to iterate the buttons map and not the other way around to make the
|
|
|
|
// borrow checker happy. First we make sure there's a button entry for every room
|
|
|
|
// entry, and clean up button entries from removed rooms.
|
|
|
|
for (id, _) in rooms.iter() {
|
|
|
|
self.buttons.entry(id.to_owned()).or_default();
|
2020-11-24 21:49:06 +01:00
|
|
|
}
|
2020-12-03 12:46:44 +01:00
|
|
|
self.buttons.retain(|id, _| rooms.contains_key(id));
|
|
|
|
// Then we make our buttons
|
2020-12-07 17:12:21 +01:00
|
|
|
let mut buttons: HashMap<RoomId, Button<Message>> = self
|
2020-12-03 12:46:44 +01:00
|
|
|
.buttons
|
|
|
|
.iter_mut()
|
|
|
|
.map(|(id, state)| {
|
|
|
|
// Get read lock for the room
|
2020-12-07 17:12:21 +01:00
|
|
|
let room = block_on(async { rooms.get(id).unwrap().read().await });
|
|
|
|
let button = Button::new(state, Text::new(room.display_name()))
|
2020-12-03 12:46:44 +01:00
|
|
|
.on_press(Message::SelectRoom(id.to_owned()))
|
2020-12-07 17:12:21 +01:00
|
|
|
.width(400.into());
|
|
|
|
(id.clone(), button)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
// List of direct message room ids
|
|
|
|
let mut dm_rooms: Vec<&RoomId> = rooms
|
|
|
|
.iter()
|
|
|
|
.filter(|(_, room)| {
|
|
|
|
let read = block_on(async { room.read().await });
|
|
|
|
read.direct_target.is_some()
|
2020-12-03 12:46:44 +01:00
|
|
|
})
|
2020-12-07 17:12:21 +01:00
|
|
|
.map(|(id, _)| id)
|
2020-12-03 12:46:44 +01:00
|
|
|
.collect();
|
2020-12-07 17:12:21 +01:00
|
|
|
// List of non-DM room ids
|
|
|
|
let mut room_rooms = rooms
|
|
|
|
.iter()
|
|
|
|
.filter(|(_, room)| {
|
|
|
|
let read = block_on(async { room.read().await });
|
|
|
|
read.direct_target.is_none()
|
|
|
|
})
|
|
|
|
.map(|(id, _)| id)
|
|
|
|
.collect();
|
|
|
|
for list in [&mut dm_rooms, &mut room_rooms].iter_mut() {
|
|
|
|
match self.sorting {
|
2020-12-08 16:40:36 +01:00
|
|
|
RoomSorting::Recent => list.sort_by_key(|id| {
|
2020-12-07 17:12:21 +01:00
|
|
|
let read = block_on(async { rooms.get(id).unwrap().read().await });
|
2020-12-08 16:40:36 +01:00
|
|
|
let time = read
|
2020-12-07 17:12:21 +01:00
|
|
|
.messages
|
|
|
|
.iter()
|
|
|
|
.map(|msg| match msg {
|
|
|
|
AnyPossiblyRedactedSyncMessageEvent::Regular(m) => m.origin_server_ts(),
|
|
|
|
AnyPossiblyRedactedSyncMessageEvent::Redacted(m) => {
|
|
|
|
m.origin_server_ts()
|
|
|
|
}
|
|
|
|
})
|
2020-12-10 13:17:41 +01:00
|
|
|
.min()
|
2020-12-08 16:40:36 +01:00
|
|
|
.copied();
|
|
|
|
match time {
|
|
|
|
Some(time) => time,
|
2020-12-10 13:17:41 +01:00
|
|
|
None => std::time::SystemTime::now(),
|
2020-12-08 16:40:36 +01:00
|
|
|
}
|
2020-12-07 17:12:21 +01:00
|
|
|
}),
|
|
|
|
RoomSorting::Alphabetic => list.sort_by_cached_key(|id| {
|
|
|
|
let read = block_on(async { rooms.get(id).unwrap().read().await });
|
|
|
|
read.display_name().to_uppercase()
|
|
|
|
}),
|
|
|
|
};
|
2020-12-03 12:46:44 +01:00
|
|
|
}
|
2020-12-07 17:12:21 +01:00
|
|
|
|
|
|
|
// Add buttons to room column
|
|
|
|
room_scroll = room_scroll.push(Text::new("Direct messages"));
|
|
|
|
for button in dm_rooms.iter().map(|id| buttons.remove(id).unwrap()) {
|
|
|
|
room_scroll = room_scroll.push(button);
|
|
|
|
}
|
|
|
|
room_scroll = room_scroll.push(Text::new("Rooms"));
|
|
|
|
for button in room_rooms.iter().map(|id| buttons.remove(id).unwrap()) {
|
|
|
|
room_scroll = room_scroll.push(button);
|
|
|
|
}
|
|
|
|
|
|
|
|
let room_col = Column::new()
|
|
|
|
.push(
|
|
|
|
Button::new(&mut self.settings_button, Text::new("Settings"))
|
|
|
|
.on_press(Message::OpenSettings),
|
|
|
|
)
|
|
|
|
.push(room_scroll);
|
|
|
|
|
2020-12-03 12:46:44 +01:00
|
|
|
root_row = root_row.push(room_col);
|
|
|
|
|
|
|
|
// Messages.
|
|
|
|
//
|
|
|
|
// Get selected room.
|
2020-12-07 17:12:21 +01:00
|
|
|
let mut message_col = Column::new().spacing(5).padding(5);
|
2020-12-03 12:46:44 +01:00
|
|
|
let selected_room = self.selected.as_ref().and_then(|selected| {
|
|
|
|
futures::executor::block_on(async {
|
|
|
|
match rooms.get(selected) {
|
|
|
|
Some(room) => Some(room.read().await),
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
if let Some(room) = selected_room {
|
2020-12-07 17:12:21 +01:00
|
|
|
message_col = message_col
|
2020-12-03 12:46:44 +01:00
|
|
|
.push(Text::new(room.display_name()).size(25))
|
|
|
|
.push(Rule::horizontal(2));
|
|
|
|
let mut scroll = Scrollable::new(&mut self.message_scroll)
|
|
|
|
.scrollbar_width(2)
|
|
|
|
.height(Length::Fill);
|
|
|
|
for message in room.messages.iter() {
|
|
|
|
if let AnyPossiblyRedactedSyncMessageEvent::Regular(event) = message {
|
|
|
|
if let AnySyncMessageEvent::RoomMessage(room_message) = event {
|
|
|
|
match &room_message.content {
|
|
|
|
MessageEventContent::Text(text) => {
|
2020-12-07 17:12:21 +01:00
|
|
|
// Render senders disambiguated name or fallback to mxid
|
|
|
|
let sender = Text::new(
|
|
|
|
room.joined_members
|
|
|
|
.get(&room_message.sender)
|
|
|
|
.map(|sender| sender.disambiguated_name())
|
|
|
|
.unwrap_or(room_message.sender.to_string()),
|
|
|
|
)
|
|
|
|
.color([0.2, 0.2, 1.0]);
|
2020-12-03 12:46:44 +01:00
|
|
|
let row = Row::new()
|
|
|
|
.spacing(5)
|
2020-12-07 17:12:21 +01:00
|
|
|
.push(sender)
|
2020-12-03 12:46:44 +01:00
|
|
|
.push(Text::new(&text.body).width(Length::Fill))
|
|
|
|
.push(Text::new(format_systime(room_message.origin_server_ts)));
|
|
|
|
scroll = scroll.push(row);
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-07 17:12:21 +01:00
|
|
|
message_col = message_col.push(scroll);
|
2020-12-03 12:46:44 +01:00
|
|
|
} else {
|
2020-12-07 17:12:21 +01:00
|
|
|
message_col = message_col.push(
|
2020-12-03 12:46:44 +01:00
|
|
|
Container::new(Text::new("Select a room to start chatting"))
|
|
|
|
.center_x()
|
|
|
|
.center_y()
|
|
|
|
.width(Length::Fill)
|
|
|
|
.height(Length::Fill),
|
|
|
|
);
|
|
|
|
}
|
2020-12-07 17:12:21 +01:00
|
|
|
// Verification info
|
|
|
|
if let Some(ref sas) = self.sas {
|
|
|
|
let device = sas.other_device();
|
|
|
|
let sas_row = match sas.emoji() {
|
2020-12-10 13:17:41 +01:00
|
|
|
_ if sas.is_done() => {
|
|
|
|
Row::new().push(Text::new("Verification complete").width(Length::Fill))
|
|
|
|
}
|
2020-12-07 17:12:21 +01:00
|
|
|
Some(emojis) => {
|
|
|
|
let mut row = Row::new().push(Text::new("Verify emojis match:"));
|
|
|
|
for (emoji, name) in emojis.iter() {
|
|
|
|
row = row.push(
|
|
|
|
Column::new()
|
|
|
|
.align_items(iced::Align::Center)
|
|
|
|
.push(Text::new(*emoji).size(32))
|
|
|
|
.push(Text::new(*name)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
row.push(
|
|
|
|
Button::new(&mut self.sas_accept_button, Text::new("Confirm"))
|
|
|
|
.on_press(Message::VerificationConfirm),
|
|
|
|
)
|
|
|
|
.push(
|
|
|
|
Button::new(&mut self.sas_deny_button, Text::new("Deny"))
|
|
|
|
.on_press(Message::VerificationCancel),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
None => Row::new()
|
|
|
|
.push(
|
|
|
|
Text::new(format!(
|
|
|
|
"Incoming verification request from {}",
|
|
|
|
match device.display_name() {
|
|
|
|
Some(name) => name,
|
|
|
|
None => device.device_id().as_str(),
|
|
|
|
}
|
|
|
|
))
|
|
|
|
.width(Length::Fill),
|
|
|
|
)
|
|
|
|
.push(
|
|
|
|
Button::new(&mut self.sas_accept_button, Text::new("Accept"))
|
|
|
|
.on_press(Message::VerificationAccept),
|
|
|
|
)
|
|
|
|
.push(
|
|
|
|
Button::new(&mut self.sas_deny_button, Text::new("Cancel"))
|
|
|
|
.on_press(Message::VerificationCancel),
|
|
|
|
),
|
|
|
|
};
|
|
|
|
message_col = message_col.push(sas_row);
|
|
|
|
}
|
2020-12-08 16:40:36 +01:00
|
|
|
// Potential error message
|
|
|
|
if let Some((ref error, ref mut button)) = self.error {
|
|
|
|
message_col = message_col.push(
|
|
|
|
Row::new()
|
|
|
|
.push(Text::new(error).width(Length::Fill).color([1.0, 0.0, 0.0]))
|
|
|
|
.push(Button::new(button, Text::new("Close")).on_press(Message::ClearError)),
|
|
|
|
);
|
|
|
|
}
|
2020-12-07 17:12:21 +01:00
|
|
|
// Compose box
|
|
|
|
message_col = message_col.push(
|
|
|
|
Row::new()
|
|
|
|
.push(
|
|
|
|
TextInput::new(
|
|
|
|
&mut self.message_input,
|
|
|
|
"Write a message...",
|
|
|
|
&self.draft,
|
|
|
|
Message::SetMessage,
|
|
|
|
)
|
|
|
|
.width(Length::Fill)
|
|
|
|
.padding(5)
|
|
|
|
.on_submit(Message::SendMessage),
|
|
|
|
)
|
|
|
|
.push(
|
|
|
|
Button::new(&mut self.send_button, Text::new("Send"))
|
|
|
|
.on_press(Message::SendMessage),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
root_row = root_row.push(message_col);
|
2020-12-03 12:46:44 +01:00
|
|
|
|
|
|
|
root_row.into()
|
2020-11-24 21:49:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-03 12:46:44 +01:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum Retrix {
|
|
|
|
Prompt(PromptView),
|
|
|
|
AwaitLogin,
|
|
|
|
LoggedIn(MainView),
|
|
|
|
}
|
|
|
|
|
2020-12-03 12:12:07 +01:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub enum PromptAction {
|
|
|
|
Login,
|
|
|
|
Signup,
|
|
|
|
}
|
|
|
|
|
2020-12-03 12:46:44 +01:00
|
|
|
impl Default for PromptAction {
|
|
|
|
fn default() -> Self {
|
|
|
|
PromptAction::Login
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-23 07:52:07 +01:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum Message {
|
2020-11-24 16:11:27 +01:00
|
|
|
// Login form messages
|
2020-11-23 07:52:07 +01:00
|
|
|
SetUser(String),
|
|
|
|
SetPassword(String),
|
|
|
|
SetServer(String),
|
2020-12-08 16:40:36 +01:00
|
|
|
SetDeviceName(String),
|
2020-12-03 12:12:07 +01:00
|
|
|
SetAction(PromptAction),
|
2020-11-23 17:18:05 +01:00
|
|
|
Login,
|
2020-12-03 12:12:07 +01:00
|
|
|
Signup,
|
|
|
|
// Auth result messages
|
2020-11-24 21:49:06 +01:00
|
|
|
LoggedIn(matrix_sdk::Client, matrix::Session),
|
|
|
|
LoginFailed(String),
|
2020-11-24 16:11:27 +01:00
|
|
|
|
|
|
|
// Main state messages
|
2020-12-10 13:17:41 +01:00
|
|
|
ResetRooms(BTreeMap<RoomId, RoomEntry>),
|
2020-12-02 06:52:21 +01:00
|
|
|
SelectRoom(RoomId),
|
2020-12-08 09:36:29 +01:00
|
|
|
/// Set error message
|
|
|
|
ErrorMessage(String),
|
|
|
|
/// Close error message
|
|
|
|
ClearError,
|
2020-12-08 16:40:36 +01:00
|
|
|
/// Set how the room list is sorted
|
|
|
|
SetSort(RoomSorting),
|
2020-12-07 17:12:21 +01:00
|
|
|
SetVerification(Option<matrix_sdk::Sas>),
|
2020-12-08 09:36:29 +01:00
|
|
|
/// Accept verification flow
|
2020-12-07 17:12:21 +01:00
|
|
|
VerificationAccept,
|
2020-12-08 09:36:29 +01:00
|
|
|
/// Accept sent
|
2020-12-07 17:12:21 +01:00
|
|
|
VerificationAccepted,
|
2020-12-08 09:36:29 +01:00
|
|
|
/// Confirm keys match
|
2020-12-07 17:12:21 +01:00
|
|
|
VerificationConfirm,
|
2020-12-08 09:36:29 +01:00
|
|
|
/// Confirmation sent
|
2020-12-07 17:12:21 +01:00
|
|
|
VerificationConfirmed,
|
2020-12-08 09:36:29 +01:00
|
|
|
/// Cancel verification flow
|
2020-12-07 17:12:21 +01:00
|
|
|
VerificationCancel,
|
2020-12-08 09:36:29 +01:00
|
|
|
/// Verification flow cancelled
|
2020-12-08 16:40:36 +01:00
|
|
|
VerificationCancelled(VerificationCancelCode),
|
2020-12-08 09:36:29 +01:00
|
|
|
/// Matrix event received
|
2020-12-07 17:12:21 +01:00
|
|
|
Sync(matrix::Event),
|
2020-12-08 09:36:29 +01:00
|
|
|
/// Set contents of message compose box
|
2020-12-03 12:12:07 +01:00
|
|
|
SetMessage(String),
|
2020-12-08 09:36:29 +01:00
|
|
|
/// Send the contents of the compose box to the selected room
|
2020-12-03 12:12:07 +01:00
|
|
|
SendMessage,
|
2020-12-07 17:12:21 +01:00
|
|
|
|
|
|
|
// Settings messages
|
2020-12-08 09:36:29 +01:00
|
|
|
/// Open settings menu
|
2020-12-07 17:12:21 +01:00
|
|
|
OpenSettings,
|
2020-12-08 09:36:29 +01:00
|
|
|
/// Close settings menu
|
2020-12-07 17:12:21 +01:00
|
|
|
CloseSettings,
|
2020-12-08 09:36:29 +01:00
|
|
|
/// Set display name input field
|
|
|
|
SetDisplayNameInput(String),
|
|
|
|
/// Save new display name
|
|
|
|
SaveDisplayName,
|
|
|
|
/// New display name saved successfully
|
|
|
|
DisplayNameSaved,
|
2020-12-07 17:12:21 +01:00
|
|
|
/// Set key import path
|
|
|
|
SetKeyPath(String),
|
2020-12-08 09:36:29 +01:00
|
|
|
/// Set password key backup is encrypted with
|
2020-12-07 17:12:21 +01:00
|
|
|
SetKeyPassword(String),
|
|
|
|
/// Import encryption keys
|
|
|
|
ImportKeys,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Default, Debug)]
|
|
|
|
pub struct SettingsView {
|
2020-12-08 09:36:29 +01:00
|
|
|
/// Display name to set
|
|
|
|
display_name: String,
|
2020-12-08 16:40:36 +01:00
|
|
|
/// Are we saving the display name?
|
|
|
|
saving_name: bool,
|
2020-12-08 09:36:29 +01:00
|
|
|
|
|
|
|
/// Display name text input
|
|
|
|
display_name_input: iced::text_input::State,
|
|
|
|
/// Button to set display name
|
|
|
|
display_name_button: iced::button::State,
|
|
|
|
|
2020-12-07 17:12:21 +01:00
|
|
|
/// Path to import encryption keys from
|
|
|
|
key_path: String,
|
|
|
|
/// Password to decrypt the keys with
|
|
|
|
key_password: String,
|
|
|
|
|
|
|
|
/// Encryption key path entry
|
|
|
|
key_path_input: iced::text_input::State,
|
|
|
|
/// Entry for key password
|
|
|
|
key_password_input: iced::text_input::State,
|
|
|
|
/// Button to import keys
|
|
|
|
key_import_button: iced::button::State,
|
|
|
|
/// Button to close settings view
|
|
|
|
close_button: iced::button::State,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SettingsView {
|
|
|
|
fn new() -> Self {
|
|
|
|
Self::default()
|
|
|
|
}
|
|
|
|
|
2020-12-08 16:40:36 +01:00
|
|
|
fn view(&mut self, sort: RoomSorting) -> Element<Message> {
|
2020-12-07 17:12:21 +01:00
|
|
|
let content = Column::new()
|
|
|
|
.width(500.into())
|
2020-12-08 16:40:36 +01:00
|
|
|
.spacing(5)
|
|
|
|
.push(Text::new("Profile").size(25))
|
2020-12-07 17:12:21 +01:00
|
|
|
.push(
|
2020-12-08 09:36:29 +01:00
|
|
|
Column::new().push(Text::new("Display name")).push(
|
|
|
|
Row::new()
|
|
|
|
.push(
|
|
|
|
TextInput::new(
|
|
|
|
&mut self.display_name_input,
|
|
|
|
"Alice",
|
|
|
|
&self.display_name,
|
|
|
|
Message::SetDisplayNameInput,
|
|
|
|
)
|
|
|
|
.width(Length::Fill)
|
|
|
|
.padding(5),
|
|
|
|
)
|
2020-12-08 16:40:36 +01:00
|
|
|
.push(match self.saving_name {
|
|
|
|
false => Button::new(&mut self.display_name_button, Text::new("Save"))
|
2020-12-08 09:36:29 +01:00
|
|
|
.on_press(Message::SaveDisplayName),
|
2020-12-08 16:40:36 +01:00
|
|
|
true => {
|
|
|
|
Button::new(&mut self.display_name_button, Text::new("Saving..."))
|
|
|
|
}
|
|
|
|
}),
|
2020-12-08 09:36:29 +01:00
|
|
|
),
|
2020-12-07 17:12:21 +01:00
|
|
|
)
|
2020-12-08 16:40:36 +01:00
|
|
|
.push(Text::new("Appearance").size(25))
|
|
|
|
.push(Text::new("Sort messages by:"))
|
|
|
|
.push(Radio::new(
|
|
|
|
RoomSorting::Alphabetic,
|
|
|
|
"Name",
|
|
|
|
Some(sort),
|
|
|
|
Message::SetSort,
|
|
|
|
))
|
|
|
|
.push(Radio::new(
|
|
|
|
RoomSorting::Recent,
|
|
|
|
"Activity",
|
|
|
|
Some(sort),
|
|
|
|
Message::SetSort,
|
|
|
|
))
|
|
|
|
.push(Text::new("Encryption").size(25))
|
2020-12-07 17:12:21 +01:00
|
|
|
.push(
|
2020-12-08 09:36:29 +01:00
|
|
|
Column::new()
|
|
|
|
.push(Text::new("Import key (enter path)"))
|
|
|
|
.push(
|
|
|
|
TextInput::new(
|
|
|
|
&mut self.key_path_input,
|
|
|
|
"/home/user/exported_keys.txt",
|
|
|
|
&self.key_path,
|
|
|
|
Message::SetKeyPath,
|
|
|
|
)
|
|
|
|
.padding(5),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.push(
|
|
|
|
Column::new().push(Text::new("Key password")).push(
|
|
|
|
TextInput::new(
|
|
|
|
&mut self.key_password_input,
|
|
|
|
"SecretPassword42",
|
|
|
|
&self.key_password,
|
|
|
|
Message::SetKeyPassword,
|
|
|
|
)
|
|
|
|
.password()
|
|
|
|
.padding(5),
|
|
|
|
),
|
2020-12-07 17:12:21 +01:00
|
|
|
)
|
|
|
|
.push(
|
|
|
|
Button::new(&mut self.key_import_button, Text::new("Import keys"))
|
|
|
|
.on_press(Message::ImportKeys),
|
|
|
|
)
|
|
|
|
.push(
|
2020-12-08 16:40:36 +01:00
|
|
|
Row::new().width(Length::Fill).push(
|
|
|
|
Button::new(&mut self.close_button, Text::new("Close"))
|
|
|
|
.on_press(Message::CloseSettings),
|
|
|
|
),
|
2020-12-07 17:12:21 +01:00
|
|
|
);
|
|
|
|
Container::new(content)
|
|
|
|
.center_x()
|
|
|
|
.center_y()
|
|
|
|
.width(Length::Fill)
|
|
|
|
.height(Length::Fill)
|
|
|
|
.into()
|
|
|
|
}
|
2020-11-23 07:52:07 +01:00
|
|
|
}
|
|
|
|
|
2020-11-24 09:12:24 +01:00
|
|
|
impl Application for Retrix {
|
2020-11-23 07:52:07 +01:00
|
|
|
type Message = Message;
|
2020-11-24 09:12:24 +01:00
|
|
|
type Executor = iced::executor::Default;
|
|
|
|
type Flags = ();
|
2020-11-23 07:52:07 +01:00
|
|
|
|
2020-11-24 09:12:24 +01:00
|
|
|
fn new(_flags: ()) -> (Self, Command<Self::Message>) {
|
2020-11-24 21:49:06 +01:00
|
|
|
// Skip login prompt if we have a session saved
|
|
|
|
match matrix::get_session().ok().flatten() {
|
|
|
|
Some(session) => {
|
|
|
|
let command = Command::perform(
|
|
|
|
async move { matrix::restore_login(session).await },
|
|
|
|
|result| match result {
|
|
|
|
Ok((s, c)) => Message::LoggedIn(s, c),
|
|
|
|
Err(e) => Message::LoginFailed(e.to_string()),
|
|
|
|
},
|
|
|
|
);
|
2020-12-03 12:46:44 +01:00
|
|
|
(Retrix::AwaitLogin, command)
|
2020-11-24 21:49:06 +01:00
|
|
|
}
|
2020-12-03 12:46:44 +01:00
|
|
|
None => (Retrix::Prompt(PromptView::new()), Command::none()),
|
2020-11-24 21:49:06 +01:00
|
|
|
}
|
2020-11-23 07:52:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn title(&self) -> String {
|
|
|
|
String::from("Retrix matrix client")
|
|
|
|
}
|
|
|
|
|
2020-12-02 06:52:21 +01:00
|
|
|
fn subscription(&self) -> Subscription<Self::Message> {
|
|
|
|
match self {
|
2020-12-03 12:46:44 +01:00
|
|
|
Retrix::LoggedIn(view) => {
|
|
|
|
matrix::MatrixSync::subscription(view.client.clone()).map(Message::Sync)
|
2020-12-02 06:52:21 +01:00
|
|
|
}
|
|
|
|
_ => Subscription::none(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-24 09:12:24 +01:00
|
|
|
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
|
2020-12-02 06:52:21 +01:00
|
|
|
match self {
|
2020-12-03 12:46:44 +01:00
|
|
|
Retrix::Prompt(prompt) => match message {
|
|
|
|
Message::SetUser(u) => prompt.user = u,
|
|
|
|
Message::SetPassword(p) => prompt.password = p,
|
|
|
|
Message::SetServer(s) => prompt.server = s,
|
2020-12-08 16:40:36 +01:00
|
|
|
Message::SetDeviceName(n) => prompt.device_name = n,
|
2020-12-03 12:46:44 +01:00
|
|
|
Message::SetAction(a) => prompt.action = a,
|
2020-11-24 09:12:24 +01:00
|
|
|
Message::Login => {
|
2020-12-03 12:46:44 +01:00
|
|
|
let user = prompt.user.clone();
|
|
|
|
let password = prompt.password.clone();
|
|
|
|
let server = prompt.server.clone();
|
2020-12-08 16:40:36 +01:00
|
|
|
let device = prompt.device_name.clone();
|
|
|
|
let device = match device.is_empty() {
|
|
|
|
false => Some(device),
|
|
|
|
true => None,
|
|
|
|
};
|
2020-12-03 12:46:44 +01:00
|
|
|
*self = Retrix::AwaitLogin;
|
2020-11-24 09:12:24 +01:00
|
|
|
return Command::perform(
|
2020-12-08 16:40:36 +01:00
|
|
|
async move { matrix::login(&user, &password, &server, device.as_deref()).await },
|
2020-11-24 09:12:24 +01:00
|
|
|
|result| match result {
|
|
|
|
Ok((c, r)) => Message::LoggedIn(c, r),
|
2020-11-24 21:49:06 +01:00
|
|
|
Err(e) => Message::LoginFailed(e.to_string()),
|
2020-11-24 09:12:24 +01:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2020-12-03 12:12:07 +01:00
|
|
|
Message::Signup => {
|
2020-12-03 12:46:44 +01:00
|
|
|
let user = prompt.user.clone();
|
|
|
|
let password = prompt.password.clone();
|
|
|
|
let server = prompt.server.clone();
|
2020-12-08 16:40:36 +01:00
|
|
|
let device = prompt.device_name.clone();
|
|
|
|
let device = match device.is_empty() {
|
|
|
|
false => Some(device),
|
|
|
|
true => None,
|
|
|
|
};
|
2020-12-03 12:46:44 +01:00
|
|
|
*self = Retrix::AwaitLogin;
|
2020-12-03 12:12:07 +01:00
|
|
|
return Command::perform(
|
2020-12-08 16:40:36 +01:00
|
|
|
async move {
|
|
|
|
matrix::signup(&user, &password, &server, device.as_deref()).await
|
|
|
|
},
|
2020-12-03 12:12:07 +01:00
|
|
|
|result| match result {
|
|
|
|
Ok((client, response)) => Message::LoggedIn(client, response),
|
|
|
|
Err(e) => Message::LoginFailed(e.to_string()),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2020-11-24 21:49:06 +01:00
|
|
|
_ => (),
|
|
|
|
},
|
2020-12-03 12:46:44 +01:00
|
|
|
Retrix::AwaitLogin => match message {
|
2020-11-24 21:49:06 +01:00
|
|
|
Message::LoginFailed(e) => {
|
2020-12-03 12:46:44 +01:00
|
|
|
let mut view = PromptView::default();
|
|
|
|
view.error = Some(e);
|
|
|
|
*self = Retrix::Prompt(view);
|
2020-11-24 21:49:06 +01:00
|
|
|
}
|
2020-11-24 16:11:27 +01:00
|
|
|
Message::LoggedIn(client, session) => {
|
2020-12-10 13:17:41 +01:00
|
|
|
*self = Retrix::LoggedIn(MainView::new(client.clone(), session));
|
2020-11-24 21:49:06 +01:00
|
|
|
return Command::perform(
|
2020-11-24 16:11:27 +01:00
|
|
|
async move {
|
2020-12-10 13:17:41 +01:00
|
|
|
let mut rooms: BTreeMap<RoomId, RoomEntry> = BTreeMap::new();
|
2020-12-02 06:52:21 +01:00
|
|
|
for (id, room) in client.joined_rooms().read().await.iter() {
|
2020-12-10 13:17:41 +01:00
|
|
|
let room = room.read().await;
|
|
|
|
let entry = rooms.entry(id.clone()).or_default();
|
|
|
|
|
|
|
|
entry.direct = room.direct_target.clone();
|
|
|
|
// Display name calculation for DMs is bronk so we're doing it
|
|
|
|
// ourselves
|
|
|
|
match entry.direct {
|
|
|
|
Some(ref direct) => {
|
|
|
|
let request = matrix_sdk::api::r0::profile::get_display_name::Request::new(direct);
|
|
|
|
if let Ok(response) = client.send(request).await {
|
|
|
|
if let Some(name) = response.displayname {
|
|
|
|
entry.name = name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => entry.name = room.display_name(),
|
|
|
|
}
|
|
|
|
let messages = room
|
|
|
|
.messages
|
|
|
|
.iter()
|
|
|
|
.cloned()
|
|
|
|
.map(|event| match event {
|
|
|
|
AnyPossiblyRedactedSyncMessageEvent::Redacted(e) => {
|
|
|
|
AnyRoomEvent::RedactedMessage(
|
|
|
|
e.into_full_event(id.clone()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
AnyPossiblyRedactedSyncMessageEvent::Regular(e) => {
|
|
|
|
AnyRoomEvent::Message(e.into_full_event(id.clone()))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
entry.messages.messages = messages;
|
2020-11-24 16:11:27 +01:00
|
|
|
}
|
2020-12-02 06:52:21 +01:00
|
|
|
rooms
|
2020-11-24 16:11:27 +01:00
|
|
|
},
|
2020-12-10 13:17:41 +01:00
|
|
|
Message::ResetRooms,
|
|
|
|
);
|
2020-11-24 16:11:27 +01:00
|
|
|
}
|
|
|
|
_ => (),
|
2020-11-23 17:18:05 +01:00
|
|
|
},
|
2020-12-07 17:12:21 +01:00
|
|
|
Retrix::LoggedIn(view) => {
|
|
|
|
match message {
|
2020-12-08 09:36:29 +01:00
|
|
|
Message::ErrorMessage(e) => view.error = Some((e, Default::default())),
|
2020-12-08 16:40:36 +01:00
|
|
|
Message::ClearError => view.error = None,
|
|
|
|
Message::SetSort(s) => view.sorting = s,
|
2020-12-07 17:12:21 +01:00
|
|
|
Message::ResetRooms(r) => view.rooms = r,
|
|
|
|
Message::SelectRoom(r) => view.selected = Some(r),
|
|
|
|
Message::Sync(event) => match event {
|
2020-12-10 13:17:41 +01:00
|
|
|
matrix::Event::Room(event) => match event {
|
|
|
|
AnyRoomEvent::Message(event) => {
|
|
|
|
let room = view.rooms.entry(event.room_id().clone()).or_default();
|
|
|
|
room.messages
|
|
|
|
.push_back(AnyRoomEvent::Message(event.clone()));
|
|
|
|
}
|
|
|
|
AnyRoomEvent::State(event) => match event {
|
|
|
|
AnyStateEvent::RoomCanonicalAlias(alias) => {
|
|
|
|
let room = view.rooms.entry(alias.room_id).or_default();
|
|
|
|
room.alias = alias.content.alias;
|
|
|
|
}
|
|
|
|
AnyStateEvent::RoomName(name) => {
|
|
|
|
let room = view.rooms.entry(name.room_id).or_default();
|
|
|
|
room.display_name = name.content.name().map(String::from);
|
|
|
|
}
|
|
|
|
AnyStateEvent::RoomTopic(topic) => {
|
|
|
|
let room = view.rooms.entry(topic.room_id).or_default();
|
|
|
|
}
|
|
|
|
any => {
|
|
|
|
let room = view.rooms.entry(any.room_id().clone()).or_default();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
},
|
2020-12-07 17:12:21 +01:00
|
|
|
matrix::Event::ToDevice(event) => match event {
|
|
|
|
AnyToDeviceEvent::KeyVerificationStart(start) => {
|
|
|
|
let client = view.client.clone();
|
|
|
|
return Command::perform(
|
|
|
|
async move {
|
2020-12-08 16:40:36 +01:00
|
|
|
tokio::time::delay_for(std::time::Duration::from_secs(2))
|
|
|
|
.await;
|
2020-12-07 17:12:21 +01:00
|
|
|
client.get_verification(&start.content.transaction_id).await
|
|
|
|
},
|
|
|
|
Message::SetVerification,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
AnyToDeviceEvent::KeyVerificationCancel(cancel) => {
|
2020-12-08 16:40:36 +01:00
|
|
|
return async {
|
|
|
|
Message::VerificationCancelled(cancel.content.code)
|
|
|
|
}
|
|
|
|
.into();
|
2020-12-07 17:12:21 +01:00
|
|
|
}
|
|
|
|
_ => (),
|
2020-12-03 12:12:07 +01:00
|
|
|
},
|
2020-12-07 17:12:21 +01:00
|
|
|
},
|
|
|
|
Message::SetVerification(v) => view.sas = v,
|
|
|
|
Message::VerificationAccept => {
|
|
|
|
let sas = match &view.sas {
|
|
|
|
Some(sas) => sas.clone(),
|
|
|
|
None => return Command::none(),
|
|
|
|
};
|
|
|
|
return Command::perform(async move { sas.accept().await }, |result| {
|
|
|
|
match result {
|
|
|
|
Ok(()) => Message::VerificationAccepted,
|
2020-12-08 16:40:36 +01:00
|
|
|
Err(e) => Message::ErrorMessage(e.to_string()),
|
2020-12-07 17:12:21 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Message::VerificationConfirm => {
|
|
|
|
let sas = match &view.sas {
|
|
|
|
Some(sas) => sas.clone(),
|
|
|
|
None => return Command::none(),
|
|
|
|
};
|
|
|
|
return Command::perform(async move { sas.confirm().await }, |result| {
|
|
|
|
match result {
|
|
|
|
Ok(()) => Message::VerificationConfirmed,
|
2020-12-08 16:40:36 +01:00
|
|
|
Err(e) => Message::ErrorMessage(e.to_string()),
|
2020-12-07 17:12:21 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Message::VerificationCancel => {
|
|
|
|
let sas = match &view.sas {
|
|
|
|
Some(sas) => sas.clone(),
|
|
|
|
None => return Command::none(),
|
|
|
|
};
|
|
|
|
return Command::perform(async move { sas.cancel().await }, |result| {
|
|
|
|
match result {
|
2020-12-08 16:40:36 +01:00
|
|
|
Ok(()) => {
|
|
|
|
Message::VerificationCancelled(VerificationCancelCode::User)
|
|
|
|
}
|
|
|
|
Err(e) => Message::ErrorMessage(e.to_string()),
|
2020-12-07 17:12:21 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-12-08 16:40:36 +01:00
|
|
|
Message::VerificationCancelled(code) => {
|
2020-12-07 17:12:21 +01:00
|
|
|
view.sas = None;
|
2020-12-08 16:40:36 +01:00
|
|
|
return async move { Message::ErrorMessage(code.as_str().to_owned()) }
|
|
|
|
.into();
|
2020-12-07 17:12:21 +01:00
|
|
|
}
|
|
|
|
Message::SetMessage(m) => view.draft = m,
|
|
|
|
Message::SendMessage => {
|
|
|
|
let selected = match view.selected.clone() {
|
|
|
|
Some(selected) => selected,
|
|
|
|
None => return Command::none(),
|
|
|
|
};
|
|
|
|
let draft = view.draft.clone();
|
|
|
|
let client = view.client.clone();
|
|
|
|
return Command::perform(
|
|
|
|
async move {
|
|
|
|
client
|
|
|
|
.room_send(
|
|
|
|
&selected,
|
|
|
|
AnyMessageEventContent::RoomMessage(
|
|
|
|
MessageEventContent::text_plain(draft),
|
|
|
|
),
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
},
|
|
|
|
|result| match result {
|
|
|
|
Ok(_) => Message::SetMessage(String::new()),
|
2020-12-08 16:40:36 +01:00
|
|
|
Err(e) => Message::ErrorMessage(e.to_string()),
|
2020-12-07 17:12:21 +01:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2020-12-08 16:40:36 +01:00
|
|
|
Message::OpenSettings => {
|
|
|
|
view.settings_view = Some(SettingsView::new());
|
|
|
|
let client = view.client.clone();
|
|
|
|
return Command::perform(
|
|
|
|
async move {
|
|
|
|
client
|
|
|
|
.display_name()
|
|
|
|
.await
|
|
|
|
.unwrap_or_default()
|
|
|
|
.unwrap_or_default()
|
|
|
|
},
|
|
|
|
Message::SetDisplayNameInput,
|
|
|
|
);
|
|
|
|
}
|
2020-12-08 09:36:29 +01:00
|
|
|
Message::SetDisplayNameInput(name) => {
|
|
|
|
if let Some(ref mut settings) = view.settings_view {
|
|
|
|
settings.display_name = name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Message::SaveDisplayName => {
|
|
|
|
if let Some(ref mut settings) = view.settings_view {
|
|
|
|
let client = view.client.clone();
|
|
|
|
let name = settings.display_name.clone();
|
2020-12-08 16:40:36 +01:00
|
|
|
settings.saving_name = true;
|
2020-12-08 09:36:29 +01:00
|
|
|
return Command::perform(
|
|
|
|
async move { client.set_display_name(Some(&name)).await },
|
|
|
|
|result| match result {
|
|
|
|
Ok(()) => Message::DisplayNameSaved,
|
2020-12-08 16:40:36 +01:00
|
|
|
// TODO: set saving to false and report error
|
|
|
|
Err(_) => Message::DisplayNameSaved,
|
2020-12-08 09:36:29 +01:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-12-08 16:40:36 +01:00
|
|
|
Message::DisplayNameSaved => {
|
|
|
|
if let Some(ref mut settings) = view.settings_view {
|
|
|
|
settings.saving_name = false;
|
|
|
|
}
|
|
|
|
}
|
2020-12-07 17:12:21 +01:00
|
|
|
Message::SetKeyPath(p) => {
|
|
|
|
if let Some(ref mut settings) = view.settings_view {
|
|
|
|
settings.key_path = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Message::SetKeyPassword(p) => {
|
|
|
|
if let Some(ref mut settings) = view.settings_view {
|
|
|
|
settings.key_password = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Message::ImportKeys => {
|
|
|
|
if let Some(ref settings) = view.settings_view {
|
|
|
|
let path = std::path::PathBuf::from(&settings.key_path);
|
|
|
|
let password = settings.key_password.clone();
|
|
|
|
let client = view.client.clone();
|
|
|
|
return Command::perform(
|
|
|
|
async move { client.import_keys(path, &password).await },
|
|
|
|
|result| match result {
|
|
|
|
Ok(_) => Message::SetKeyPassword(String::new()),
|
|
|
|
// TODO: Actual error reporting here
|
|
|
|
Err(e) => Message::SetKeyPath(e.to_string()),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Message::CloseSettings => view.settings_view = None,
|
|
|
|
_ => (),
|
2020-12-03 12:12:07 +01:00
|
|
|
}
|
2020-12-07 17:12:21 +01:00
|
|
|
}
|
2020-11-24 16:11:27 +01:00
|
|
|
};
|
2020-11-24 09:12:24 +01:00
|
|
|
Command::none()
|
2020-11-23 17:18:05 +01:00
|
|
|
}
|
2020-11-23 07:52:07 +01:00
|
|
|
|
|
|
|
fn view(&mut self) -> Element<Self::Message> {
|
2020-12-02 06:52:21 +01:00
|
|
|
match self {
|
2020-12-03 12:46:44 +01:00
|
|
|
Retrix::Prompt(prompt) => prompt.view(),
|
2020-12-07 17:12:21 +01:00
|
|
|
Retrix::AwaitLogin => Container::new(Text::new("Logging in..."))
|
2020-12-03 12:46:44 +01:00
|
|
|
.center_x()
|
|
|
|
.center_y()
|
|
|
|
.width(Length::Fill)
|
|
|
|
.height(Length::Fill)
|
|
|
|
.into(),
|
|
|
|
Retrix::LoggedIn(view) => view.view(),
|
2020-11-23 07:52:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-02 06:52:21 +01:00
|
|
|
|
|
|
|
fn format_systime(time: std::time::SystemTime) -> String {
|
|
|
|
let secs = time
|
|
|
|
.duration_since(std::time::SystemTime::UNIX_EPOCH)
|
|
|
|
.unwrap_or_default()
|
|
|
|
.as_secs();
|
|
|
|
format!(
|
|
|
|
"{:02}:{:02}",
|
|
|
|
(secs % (60 * 60 * 24)) / (60 * 60),
|
|
|
|
(secs % (60 * 60)) / 60
|
|
|
|
)
|
|
|
|
}
|