From 80c8e18cea4f243ffa7caa921f8bf6d9baca01bd Mon Sep 17 00:00:00 2001 From: fiaxh Date: Wed, 17 Feb 2021 11:22:19 -0600 Subject: Add cache for file transfers --- libdino/CMakeLists.txt | 1 + libdino/src/application.vala | 1 + libdino/src/service/content_item_store.vala | 25 ++--------- libdino/src/service/file_manager.vala | 4 +- libdino/src/service/file_transfer_storage.vala | 61 ++++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 23 deletions(-) create mode 100644 libdino/src/service/file_transfer_storage.vala (limited to 'libdino') diff --git a/libdino/CMakeLists.txt b/libdino/CMakeLists.txt index b3293a68..90efcc73 100644 --- a/libdino/CMakeLists.txt +++ b/libdino/CMakeLists.txt @@ -36,6 +36,7 @@ SOURCES src/service/entity_capabilities_storage.vala src/service/entity_info.vala src/service/file_manager.vala + src/service/file_transfer_storage.vala src/service/jingle_file_transfers.vala src/service/message_correction.vala src/service/message_processor.vala diff --git a/libdino/src/application.vala b/libdino/src/application.vala index 80af3d3b..c1fd7e39 100644 --- a/libdino/src/application.vala +++ b/libdino/src/application.vala @@ -46,6 +46,7 @@ public interface Application : GLib.Application { Register.start(stream_interactor, db); EntityInfo.start(stream_interactor, db); MessageCorrection.start(stream_interactor, db); + FileTransferStorage.start(stream_interactor, db); create_actions(); diff --git a/libdino/src/service/content_item_store.vala b/libdino/src/service/content_item_store.vala index a6d79267..2de804a1 100644 --- a/libdino/src/service/content_item_store.vala +++ b/libdino/src/service/content_item_store.vala @@ -56,27 +56,10 @@ public class ContentItemStore : StreamInteractionModule, Object { } break; case 2: - RowOption row_option = db.file_transfer.select().with(db.file_transfer.id, "=", foreign_id).row(); - if (row_option.is_present()) { - try { - string storage_dir = FileManager.get_storage_dir(); - FileTransfer file_transfer = new FileTransfer.from_row(db, row_option.inner, storage_dir); - if (conversation.type_.is_muc_semantic()) { - try { - // resourcepart wasn't set before, so we pick nickname instead (which isn't accurate if nickname is changed) - file_transfer.ourpart = conversation.counterpart.with_resource(file_transfer.ourpart.resourcepart ?? conversation.nickname); - } catch (InvalidJidError e) { - warning("Failed setting file transfer Jid: %s", e.message); - } - } - Message? message = null; - if (file_transfer.provider == 0 && file_transfer.info != null) { - message = stream_interactor.get_module(MessageStorage.IDENTITY).get_message_by_id(int.parse(file_transfer.info), conversation); - } - items.add(new FileItem(file_transfer, conversation, row[db.content_item.id], message)); - } catch (InvalidJidError e) { - warning("Ignoring file transfer with invalid Jid: %s", e.message); - } + FileTransfer? file_transfer = stream_interactor.get_module(FileTransferStorage.IDENTITY).get_call_by_id(foreign_id); + if (file_transfer != null) { + var file_item = new FileItem(file_transfer, conversation, row[db.content_item.id]); + items.add(file_item); } break; } diff --git a/libdino/src/service/file_manager.vala b/libdino/src/service/file_manager.vala index f3256601..19d51b44 100644 --- a/libdino/src/service/file_manager.vala +++ b/libdino/src/service/file_manager.vala @@ -69,7 +69,7 @@ public class FileManager : StreamInteractionModule, Object { yield save_file(file_transfer); - file_transfer.persist(db); + stream_interactor.get_module(FileTransferStorage.IDENTITY).add_file(file_transfer); conversation.last_active = file_transfer.time; received_file(file_transfer, conversation); } catch (Error e) { @@ -281,7 +281,7 @@ public class FileManager : StreamInteractionModule, Object { } } - file_transfer.persist(db); + stream_interactor.get_module(FileTransferStorage.IDENTITY).add_file(file_transfer); if (is_sender_trustworthy(file_transfer, conversation)) { try { diff --git a/libdino/src/service/file_transfer_storage.vala b/libdino/src/service/file_transfer_storage.vala new file mode 100644 index 00000000..b4876c34 --- /dev/null +++ b/libdino/src/service/file_transfer_storage.vala @@ -0,0 +1,61 @@ +using Xmpp; +using Gee; +using Qlite; + +using Dino.Entities; + +namespace Dino { + + public class FileTransferStorage : StreamInteractionModule, Object { + public static ModuleIdentity IDENTITY = new ModuleIdentity("file_store"); + public string id { get { return IDENTITY.id; } } + + private StreamInteractor stream_interactor; + private Database db; + + private WeakMap files_by_db_id = new WeakMap(); + + public static void start(StreamInteractor stream_interactor, Database db) { + FileTransferStorage m = new FileTransferStorage(stream_interactor, db); + stream_interactor.add_module(m); + } + + private FileTransferStorage(StreamInteractor stream_interactor, Database db) { + this.stream_interactor = stream_interactor; + this.db = db; + } + + public void add_file(FileTransfer file_transfer) { + file_transfer.persist(db); + cache_call(file_transfer); + } + + public FileTransfer? get_call_by_id(int id) { + FileTransfer? file_transfer = files_by_db_id[id]; + if (file_transfer != null) { + return file_transfer; + } + + RowOption row_option = db.file_transfer.select().with(db.file_transfer.id, "=", id).row(); + + return create_call_from_row_opt(row_option); + } + + private FileTransfer? create_call_from_row_opt(RowOption row_opt) { + if (!row_opt.is_present()) return null; + + try { + FileTransfer file_transfer = new FileTransfer.from_row(db, row_opt.inner, FileManager.get_storage_dir()); + cache_call(file_transfer); + return file_transfer; + } catch (InvalidJidError e) { + warning("Got file transfer with invalid Jid: %s", e.message); + } + return null; + } + + private void cache_call(FileTransfer file_transfer) { + files_by_db_id[file_transfer.id] = file_transfer; + } + } +} \ No newline at end of file -- cgit v1.2.3-70-g09d2