diff options
author | fiaxh <git@lightrise.org> | 2021-02-17 11:22:19 -0600 |
---|---|---|
committer | fiaxh <git@lightrise.org> | 2021-02-17 15:25:55 -0600 |
commit | 80c8e18cea4f243ffa7caa921f8bf6d9baca01bd (patch) | |
tree | 5ec9cd032e0d899d6b99fa3340bb636d5d6c1b80 /libdino/src/service | |
parent | 0626bad8e9c2f7dfd4d8eccc86ec1e8bec6c7308 (diff) | |
download | dino-80c8e18cea4f243ffa7caa921f8bf6d9baca01bd.tar.gz dino-80c8e18cea4f243ffa7caa921f8bf6d9baca01bd.zip |
Add cache for file transfers
Diffstat (limited to 'libdino/src/service')
-rw-r--r-- | libdino/src/service/content_item_store.vala | 25 | ||||
-rw-r--r-- | libdino/src/service/file_manager.vala | 4 | ||||
-rw-r--r-- | libdino/src/service/file_transfer_storage.vala | 61 |
3 files changed, 67 insertions, 23 deletions
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<FileTransferStorage> IDENTITY = new ModuleIdentity<FileTransferStorage>("file_store"); + public string id { get { return IDENTITY.id; } } + + private StreamInteractor stream_interactor; + private Database db; + + private WeakMap<int, FileTransfer> files_by_db_id = new WeakMap<int, FileTransfer>(); + + 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 |