2020-12-02 06:52:21 +01:00
|
|
|
use std::collections::{BTreeMap, HashMap};
|
|
|
|
|
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-03 12:12:07 +01:00
|
|
|
room::message::MessageEventContent, AnyMessageEventContent,
|
|
|
|
AnyPossiblyRedactedSyncMessageEvent, AnyRoomEvent, AnySyncMessageEvent,
|
2020-12-02 06:52:21 +01:00
|
|
|
},
|
|
|
|
identifiers::RoomId,
|
2020-11-23 07:52:07 +01:00
|
|
|
};
|
|
|
|
|
2020-11-24 09:12:24 +01:00
|
|
|
use crate::matrix;
|
|
|
|
|
2020-12-03 12:46:44 +01:00
|
|
|
/// View for the login prompt
|
|
|
|
#[derive(Debug, Clone, Default)]
|
|
|
|
pub struct PromptView {
|
|
|
|
user_input: text_input::State,
|
|
|
|
password_input: text_input::State,
|
|
|
|
server_input: text_input::State,
|
|
|
|
login_button: iced::button::State,
|
2020-11-23 17:18:05 +01:00
|
|
|
|
2020-12-03 12:46:44 +01:00
|
|
|
user: String,
|
|
|
|
password: String,
|
|
|
|
server: String,
|
|
|
|
action: PromptAction,
|
|
|
|
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())
|
|
|
|
.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(Text::new("Username"))
|
|
|
|
.push(
|
|
|
|
TextInput::new(
|
|
|
|
&mut self.user_input,
|
|
|
|
"Username",
|
|
|
|
&self.user,
|
|
|
|
Message::SetUser,
|
|
|
|
)
|
|
|
|
.padding(5),
|
|
|
|
)
|
|
|
|
.push(Text::new("Password"))
|
|
|
|
.push(
|
|
|
|
TextInput::new(
|
|
|
|
&mut self.password_input,
|
|
|
|
"Password",
|
|
|
|
&self.password,
|
|
|
|
Message::SetPassword,
|
|
|
|
)
|
|
|
|
.password()
|
|
|
|
.padding(5),
|
|
|
|
)
|
|
|
|
.push(Text::new("Homeserver"))
|
|
|
|
.push(
|
|
|
|
TextInput::new(
|
|
|
|
&mut self.server_input,
|
|
|
|
"Server",
|
|
|
|
&self.server,
|
|
|
|
Message::SetServer,
|
|
|
|
)
|
|
|
|
.padding(5),
|
|
|
|
);
|
|
|
|
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")
|
|
|
|
.color([0.2, 0.2, 0.0]),
|
|
|
|
);
|
|
|
|
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-03 12:46:44 +01:00
|
|
|
/// Main view after successful login
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct MainView {
|
|
|
|
client: matrix_sdk::Client,
|
|
|
|
session: matrix::Session,
|
|
|
|
|
|
|
|
rooms: BTreeMap<RoomId, String>,
|
|
|
|
buttons: HashMap<RoomId, iced::button::State>,
|
|
|
|
messages: BTreeMap<RoomId, MessageEventContent>,
|
|
|
|
selected: Option<RoomId>,
|
|
|
|
room_scroll: iced::scrollable::State,
|
|
|
|
message_scroll: iced::scrollable::State,
|
|
|
|
message_input: iced::text_input::State,
|
|
|
|
draft: String,
|
|
|
|
send_button: iced::button::State,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MainView {
|
|
|
|
pub fn new(client: matrix_sdk::Client, session: matrix::Session) -> Self {
|
|
|
|
Self {
|
|
|
|
client,
|
|
|
|
session,
|
|
|
|
rooms: Default::default(),
|
|
|
|
selected: None,
|
|
|
|
room_scroll: Default::default(),
|
|
|
|
message_scroll: Default::default(),
|
|
|
|
message_input: Default::default(),
|
|
|
|
buttons: Default::default(),
|
|
|
|
messages: Default::default(),
|
|
|
|
draft: String::new(),
|
|
|
|
send_button: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn view(&mut self) -> Element<Message> {
|
|
|
|
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 });
|
|
|
|
let mut room_col = Scrollable::new(&mut self.room_scroll)
|
|
|
|
.width(400.into())
|
|
|
|
.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
|
|
|
|
let buttons: Vec<Button<_>> = self
|
|
|
|
.buttons
|
|
|
|
.iter_mut()
|
|
|
|
.map(|(id, state)| {
|
|
|
|
// Get read lock for the room
|
|
|
|
let room =
|
|
|
|
futures::executor::block_on(async { rooms.get(id).unwrap().read().await });
|
|
|
|
Button::new(state, Text::new(room.display_name()))
|
|
|
|
.on_press(Message::SelectRoom(id.to_owned()))
|
|
|
|
.width(400.into())
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
// Then we add them to our room column. What a mess.
|
|
|
|
for button in buttons {
|
|
|
|
room_col = room_col.push(button);
|
|
|
|
}
|
|
|
|
root_row = root_row.push(room_col);
|
|
|
|
|
|
|
|
// Messages.
|
|
|
|
//
|
|
|
|
// Get selected room.
|
|
|
|
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 {
|
|
|
|
let mut col = Column::new()
|
|
|
|
.spacing(5)
|
|
|
|
.padding(5)
|
|
|
|
.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) => {
|
|
|
|
let row = Row::new()
|
|
|
|
.spacing(5)
|
|
|
|
.push(
|
|
|
|
// Render senders disambiguated name or fallback to
|
|
|
|
// mxid
|
|
|
|
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]),
|
|
|
|
)
|
|
|
|
.push(Text::new(&text.body).width(Length::Fill))
|
|
|
|
.push(Text::new(format_systime(room_message.origin_server_ts)));
|
|
|
|
scroll = scroll.push(row);
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
col = col.push(scroll).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(col);
|
|
|
|
} else {
|
|
|
|
root_row = root_row.push(
|
|
|
|
Container::new(Text::new("Select a room to start chatting"))
|
|
|
|
.center_x()
|
|
|
|
.center_y()
|
|
|
|
.width(Length::Fill)
|
|
|
|
.height(Length::Fill),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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-02 06:52:21 +01:00
|
|
|
ResetRooms(BTreeMap<RoomId, String>),
|
|
|
|
SelectRoom(RoomId),
|
|
|
|
Sync(AnyRoomEvent),
|
2020-12-03 12:12:07 +01:00
|
|
|
SetMessage(String),
|
|
|
|
SendMessage,
|
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,
|
|
|
|
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();
|
|
|
|
*self = Retrix::AwaitLogin;
|
2020-11-24 09:12:24 +01:00
|
|
|
return Command::perform(
|
|
|
|
async move { matrix::login(&user, &password, &server).await },
|
|
|
|
|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();
|
|
|
|
*self = Retrix::AwaitLogin;
|
2020-12-03 12:12:07 +01:00
|
|
|
return Command::perform(
|
|
|
|
async move { matrix::signup(&user, &password, &server).await },
|
|
|
|
|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-03 12:46:44 +01:00
|
|
|
*self = Retrix::LoggedIn(MainView::new(client, session));
|
2020-12-03 12:12:07 +01:00
|
|
|
/*let client = client.clone();
|
2020-11-24 21:49:06 +01:00
|
|
|
return Command::perform(
|
2020-11-24 16:11:27 +01:00
|
|
|
async move {
|
2020-12-02 06:52:21 +01:00
|
|
|
let mut rooms = BTreeMap::new();
|
|
|
|
for (id, room) in client.joined_rooms().read().await.iter() {
|
2020-11-24 21:49:06 +01:00
|
|
|
let name = room.read().await.display_name();
|
2020-12-02 06:52:21 +01:00
|
|
|
rooms.insert(id.to_owned(), name);
|
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
|
|
|
},
|
|
|
|
|rooms| Message::ResetRooms(rooms),
|
2020-12-03 12:12:07 +01:00
|
|
|
);*/
|
2020-11-24 16:11:27 +01:00
|
|
|
}
|
|
|
|
_ => (),
|
2020-11-23 17:18:05 +01:00
|
|
|
},
|
2020-12-03 12:46:44 +01:00
|
|
|
Retrix::LoggedIn(view) => match message {
|
|
|
|
Message::ResetRooms(r) => view.rooms = r,
|
|
|
|
Message::SelectRoom(r) => view.selected = Some(r),
|
2020-12-03 12:12:07 +01:00
|
|
|
Message::Sync(event) => match event {
|
|
|
|
AnyRoomEvent::Message(_message) => (),
|
|
|
|
AnyRoomEvent::State(_state) => (),
|
|
|
|
AnyRoomEvent::RedactedMessage(_message) => (),
|
|
|
|
AnyRoomEvent::RedactedState(_state) => (),
|
|
|
|
},
|
2020-12-03 12:46:44 +01:00
|
|
|
Message::SetMessage(m) => view.draft = m,
|
2020-12-03 12:12:07 +01:00
|
|
|
Message::SendMessage => {
|
2020-12-03 12:46:44 +01:00
|
|
|
let selected = view.selected.to_owned();
|
|
|
|
let draft = view.draft.clone();
|
|
|
|
let client = view.client.clone();
|
2020-12-03 12:12:07 +01:00
|
|
|
return Command::perform(
|
|
|
|
async move {
|
|
|
|
client
|
|
|
|
.room_send(
|
|
|
|
&selected.unwrap(),
|
|
|
|
AnyMessageEventContent::RoomMessage(
|
|
|
|
MessageEventContent::text_plain(draft),
|
|
|
|
),
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
},
|
|
|
|
|result| match result {
|
|
|
|
Ok(_) => Message::SetMessage(String::new()),
|
|
|
|
Err(e) => Message::SetMessage(format!("{:?}", e)),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
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(),
|
|
|
|
Retrix::AwaitLogin => Container::new(Text::new(format!("Logging in...")))
|
|
|
|
.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
|
|
|
|
)
|
|
|
|
}
|