[fractal] API: manage room module from app directly
- From: Daniel Garcia Moreno <danigm src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [fractal] API: manage room module from app directly
- Date: Tue, 30 Jun 2020 07:43:04 +0000 (UTC)
commit f67d943ab78cfa5c64abd0797ba3442f1d881c5f
Author: Alejandro DomÃnguez <adomu net-c com>
Date: Wed Jun 17 03:21:27 2020 +0200
API: manage room module from app directly
fractal-gtk/src/app/backend_loop.rs | 29 ++++------
fractal-gtk/src/appop/message.rs | 95 +++++++++++++++++++++++++++++----
fractal-matrix-api/src/backend/mod.rs | 6 ---
fractal-matrix-api/src/backend/room.rs | 73 +------------------------
fractal-matrix-api/src/backend/types.rs | 5 +-
5 files changed, 98 insertions(+), 110 deletions(-)
---
diff --git a/fractal-gtk/src/app/backend_loop.rs b/fractal-gtk/src/app/backend_loop.rs
index 304b1270..03a08342 100644
--- a/fractal-gtk/src/app/backend_loop.rs
+++ b/fractal-gtk/src/app/backend_loop.rs
@@ -45,12 +45,6 @@ pub fn backend_loop(rx: Receiver<BKResponse>) {
BKResponse::RoomMessages(Ok(msgs)) => {
APPOP!(show_room_messages, (msgs));
}
- BKResponse::SentMsg(Ok((txid, evid))) => {
- APPOP!(msg_sent, (txid, evid));
- let initial = false;
- let number_tries = 0;
- APPOP!(sync, (initial, number_tries));
- }
BKResponse::RemoveMessage(Ok((room, msg))) => {
APPOP!(remove_message, (room, msg));
@@ -72,9 +66,6 @@ pub fn backend_loop(rx: Receiver<BKResponse>) {
BKResponse::RoomMemberEvent(ev) => {
APPOP!(room_member_event, (ev));
}
- BKResponse::AttachedFile(Ok(msg)) => {
- APPOP!(attached_file, (msg));
- }
// errors
BKResponse::AccountDestructionError(err) => {
@@ -178,7 +169,7 @@ pub fn backend_loop(rx: Receiver<BKResponse>) {
APPOP!(logout);
APPOP!(set_state, (st));
}
- BKResponse::AttachedFile(Err(err)) => {
+ BKResponse::AttachedFileError(err) => {
let err_str = format!("{:?}", err);
error!(
"attaching {}: retrying send",
@@ -186,16 +177,14 @@ pub fn backend_loop(rx: Receiver<BKResponse>) {
);
APPOP!(retry_send);
}
- BKResponse::SentMsg(Err(err)) => match err {
- Error::SendMsgError(txid) => {
- error!("sending {}: retrying send", txid);
- APPOP!(retry_send);
- }
- _ => {
- let error = i18n("Error sending message");
- APPOP!(show_error, (error));
- }
- },
+ BKResponse::SentMsgError(Error::SendMsgError(txid)) => {
+ error!("sending {}: retrying send", txid);
+ APPOP!(retry_send);
+ }
+ BKResponse::SentMsgError(_) => {
+ let error = i18n("Error sending message");
+ APPOP!(show_error, (error));
+ }
BKResponse::SentMsgRedactionError(_) => {
let error = i18n("Error deleting message");
APPOP!(show_error, (error));
diff --git a/fractal-gtk/src/appop/message.rs b/fractal-gtk/src/appop/message.rs
index 3f2ff3f5..e7881cc1 100644
--- a/fractal-gtk/src/appop/message.rs
+++ b/fractal-gtk/src/appop/message.rs
@@ -1,6 +1,10 @@
use comrak::{markdown_to_html, ComrakOptions};
use fractal_api::backend::room;
+use fractal_api::clone;
use fractal_api::identifiers::{EventId, RoomId};
+use fractal_api::r0::AccessToken;
+use fractal_api::types::ExtraContent;
+use fractal_api::url::Url;
use fractal_api::util::ResultExpectLog;
use gdk_pixbuf::Pixbuf;
use gio::prelude::FileExt;
@@ -14,6 +18,7 @@ use serde_json::Value as JsonValue;
use std::env::temp_dir;
use std::fs;
use std::path::PathBuf;
+use std::sync::mpsc::Sender;
use std::thread;
use crate::appop::room::Force;
@@ -214,18 +219,14 @@ impl AppOp {
self.sending_message = true;
if let Some(next) = self.msg_queue.last() {
let msg = next.msg.clone();
+ let tx = self.backend.clone();
match &next.msg.mtype[..] {
"m.image" | "m.file" | "m.audio" | "m.video" => {
- self.backend
- .send(BKCommand::AttachFile(
- login_data.server_url,
- login_data.access_token,
- msg,
- ))
- .unwrap();
+ thread::spawn(move || {
+ attach_file(tx, login_data.server_url, login_data.access_token, msg)
+ });
}
_ => {
- let tx = self.backend.clone();
thread::spawn(move || {
match room::send_msg(login_data.server_url, login_data.access_token, msg) {
Ok((txid, evid)) => {
@@ -235,7 +236,7 @@ impl AppOp {
APPOP!(sync, (initial, number_tries));
}
Err(err) => {
- tx.send(BKCommand::SendBKResponse(BKResponse::SentMsg(Err(err))))
+ tx.send(BKCommand::SendBKResponse(BKResponse::SentMsgError(err)))
.expect_log("Connection closed");
}
}
@@ -640,3 +641,79 @@ fn get_file_media_info(file: &str, mimetype: &str) -> Option<JsonValue> {
Some(info)
}
+
+fn attach_file(tx: Sender<BKCommand>, baseu: Url, tk: AccessToken, mut msg: Message) {
+ let fname = msg.url.clone().unwrap_or_default();
+ let mut extra_content: Option<ExtraContent> = msg
+ .clone()
+ .extra_content
+ .and_then(|c| serde_json::from_value(c).ok());
+
+ let thumb = extra_content
+ .clone()
+ .and_then(|c| c.info.thumbnail_url)
+ .unwrap_or_default();
+
+ if fname.starts_with("mxc://") && thumb.starts_with("mxc://") {
+ send_msg_and_manage(tx, baseu, tk, msg);
+
+ return;
+ }
+
+ if !thumb.is_empty() {
+ match room::upload_file(baseu.clone(), tk.clone(), &thumb) {
+ Ok(response) => {
+ let thumb_uri = response.content_uri.to_string();
+ msg.thumb = Some(thumb_uri.clone());
+ if let Some(ref mut xctx) = extra_content {
+ xctx.info.thumbnail_url = Some(thumb_uri);
+ }
+ msg.extra_content = serde_json::to_value(&extra_content).ok();
+ }
+ Err(err) => {
+ tx.send(BKCommand::SendBKResponse(BKResponse::AttachedFileError(
+ err,
+ )))
+ .expect_log("Connection closed");
+ }
+ }
+
+ if let Err(_e) = std::fs::remove_file(&thumb) {
+ error!("Can't remove thumbnail: {}", thumb);
+ }
+ }
+
+ let query = room::upload_file(baseu.clone(), tk.clone(), &fname).map(|response| {
+ msg.url = Some(response.content_uri.to_string());
+ thread::spawn(clone!(msg, tx => move || send_msg_and_manage(tx, baseu, tk, msg)));
+
+ msg
+ });
+
+ match query {
+ Ok(msg) => {
+ APPOP!(attached_file, (msg));
+ }
+ Err(err) => {
+ tx.send(BKCommand::SendBKResponse(BKResponse::AttachedFileError(
+ err,
+ )))
+ .expect_log("Connection closed");
+ }
+ };
+}
+
+fn send_msg_and_manage(tx: Sender<BKCommand>, baseu: Url, tk: AccessToken, msg: Message) {
+ match room::send_msg(baseu, tk, msg) {
+ Ok((txid, evid)) => {
+ APPOP!(msg_sent, (txid, evid));
+ let initial = false;
+ let number_tries = 0;
+ APPOP!(sync, (initial, number_tries));
+ }
+ Err(err) => {
+ tx.send(BKCommand::SendBKResponse(BKResponse::SentMsgError(err)))
+ .expect_log("Connection closed");
+ }
+ };
+}
diff --git a/fractal-matrix-api/src/backend/mod.rs b/fractal-matrix-api/src/backend/mod.rs
index 16079225..966059d4 100644
--- a/fractal-matrix-api/src/backend/mod.rs
+++ b/fractal-matrix-api/src/backend/mod.rs
@@ -55,12 +55,6 @@ impl Backend {
)
}
- // Room module
- Ok(BKCommand::AttachFile(server, access_token, msg)) => {
- let r = room::attach_file(self, server, access_token, msg);
- bkerror!(r, tx, BKResponse::AttachedFile);
- }
-
// Internal commands
Ok(BKCommand::SendBKResponse(response)) => {
tx.send(response).expect_log("Connection closed");
diff --git a/fractal-matrix-api/src/backend/room.rs b/fractal-matrix-api/src/backend/room.rs
index 42e7f98c..c29e98f0 100644
--- a/fractal-matrix-api/src/backend/room.rs
+++ b/fractal-matrix-api/src/backend/room.rs
@@ -11,17 +11,13 @@ use std::time::Duration;
use crate::error::Error;
use crate::globals;
-use std::thread;
use crate::util::cache_dir_path;
use crate::util::dw_media;
use crate::util::get_prev_batch_from;
use crate::util::ContentType;
-use crate::util::ResultExpectLog;
use crate::util::HTTP_CLIENT;
-use crate::backend::types::BKResponse;
-use crate::backend::types::Backend;
use crate::backend::types::RoomType;
use crate::r0::config::get_global_account_data::request as get_global_account_data;
@@ -78,7 +74,6 @@ use crate::r0::typing::request as send_typing_notification;
use crate::r0::typing::Body as TypingNotificationBody;
use crate::r0::typing::Parameters as TypingNotificationParameters;
use crate::r0::AccessToken;
-use crate::types::ExtraContent;
use crate::types::Member;
use crate::types::Message;
use crate::types::{Room, RoomMembership, RoomTag};
@@ -386,73 +381,7 @@ pub fn set_room_avatar(
Ok(())
}
-pub fn attach_file(
- bk: &Backend,
- baseu: Url,
- tk: AccessToken,
- mut msg: Message,
-) -> Result<(), Error> {
- let fname = msg.url.clone().unwrap_or_default();
- let mut extra_content: Option<ExtraContent> = msg
- .clone()
- .extra_content
- .and_then(|c| serde_json::from_value(c).ok());
-
- let thumb = extra_content
- .clone()
- .and_then(|c| c.info.thumbnail_url)
- .unwrap_or_default();
-
- let tx = bk.tx.clone();
-
- if fname.starts_with("mxc://") && thumb.starts_with("mxc://") {
- tx.send(BKResponse::SentMsg(send_msg(baseu, tk, msg)))
- .expect_log("Connection closed");
-
- return Ok(());
- }
-
- thread::spawn(move || {
- if !thumb.is_empty() {
- match upload_file(baseu.clone(), tk.clone(), &thumb) {
- Err(err) => {
- tx.send(BKResponse::AttachedFile(Err(err)))
- .expect_log("Connection closed");
- }
- Ok(response) => {
- let thumb_uri = response.content_uri.to_string();
- msg.thumb = Some(thumb_uri.clone());
- if let Some(ref mut xctx) = extra_content {
- xctx.info.thumbnail_url = Some(thumb_uri);
- }
- msg.extra_content = serde_json::to_value(&extra_content).ok();
- }
- }
-
- if let Err(_e) = std::fs::remove_file(&thumb) {
- error!("Can't remove thumbnail: {}", thumb);
- }
- }
-
- let query = upload_file(baseu.clone(), tk.clone(), &fname).map(|response| {
- msg.url = Some(response.content_uri.to_string());
- thread::spawn(clone!(msg, tx => move || {
- let query = send_msg(baseu, tk, msg);
- tx.send(BKResponse::SentMsg(query))
- .expect_log("Connection closed");
- }));
-
- msg
- });
-
- tx.send(BKResponse::AttachedFile(query))
- .expect_log("Connection closed");
- });
-
- Ok(())
-}
-
-fn upload_file(
+pub fn upload_file(
base: Url,
access_token: AccessToken,
fname: &str,
diff --git a/fractal-matrix-api/src/backend/types.rs b/fractal-matrix-api/src/backend/types.rs
index 18462470..4dee0669 100644
--- a/fractal-matrix-api/src/backend/types.rs
+++ b/fractal-matrix-api/src/backend/types.rs
@@ -24,7 +24,6 @@ pub enum BKCommand {
u64,
),
ShutDown,
- AttachFile(Url, AccessToken, Message),
SendBKResponse(BKResponse),
}
@@ -39,12 +38,10 @@ pub enum BKResponse {
RoomMemberEvent(Event),
RoomMessages(Result<Vec<Message>, Error>),
RoomMessagesInit(Vec<Message>),
- SentMsg(Result<(String, Option<EventId>), Error>),
RemoveMessage(Result<(RoomId, EventId), Error>),
RoomName(RoomId, String),
RoomTopic(RoomId, String),
MediaUrl(Url),
- AttachedFile(Result<Message, Error>),
RoomNotifications(RoomId, i32, i32),
//errors
@@ -84,6 +81,8 @@ pub enum BKResponse {
NewRoomError(Error, RoomId),
RoomDetailError(Error),
RoomAvatarError(Error),
+ SentMsgError(Error),
+ AttachedFileError(Error),
}
#[derive(Debug, Clone, Copy)]
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]