Add cache for file transfers

This commit is contained in:
fiaxh 2021-02-17 11:22:19 -06:00
parent 0626bad8e9
commit 80c8e18cea
5 changed files with 69 additions and 23 deletions

View File

@ -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

View File

@ -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();

View File

@ -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;
}

View File

@ -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 {

View File

@ -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;
}
}
}