From 0e41fb3294271fc271ac8d24c1871fbea27e2ba4 Mon Sep 17 00:00:00 2001 From: fiaxh Date: Mon, 27 Aug 2018 14:57:02 +0200 Subject: [PATCH] Add `hidden` column to content_item table --- libdino/src/service/content_item_store.vala | 72 +++++++++++++-------- libdino/src/service/database.vala | 53 +++++++++------ libdino/src/service/message_storage.vala | 1 + libdino/src/service/search_processor.vala | 6 +- plugins/http-files/src/file_provider.vala | 10 ++- plugins/http-files/src/manager.vala | 5 ++ 6 files changed, 97 insertions(+), 50 deletions(-) diff --git a/libdino/src/service/content_item_store.vala b/libdino/src/service/content_item_store.vala index 83ba1da0..8c4f4665 100644 --- a/libdino/src/service/content_item_store.vala +++ b/libdino/src/service/content_item_store.vala @@ -26,8 +26,7 @@ public class ContentItemStore : StreamInteractionModule, Object { this.stream_interactor = stream_interactor; this.db = db; - stream_interactor.get_module(MessageProcessor.IDENTITY).message_received.connect(on_new_message); - stream_interactor.get_module(MessageProcessor.IDENTITY).message_sent.connect(on_new_message); + stream_interactor.get_module(MessageProcessor.IDENTITY).message_sent.connect((message, conversation) => insert_message(message, conversation)); stream_interactor.get_module(FileManager.IDENTITY).received_file.connect(insert_file_transfer); } @@ -43,8 +42,8 @@ public class ContentItemStore : StreamInteractionModule, Object { Gee.TreeSet items = new Gee.TreeSet(ContentItem.compare); foreach (var row in select) { - int provider = row[db.content.content_type]; - int foreign_id = row[db.content.foreign_id]; + int provider = row[db.content_item.content_type]; + int foreign_id = row[db.content_item.foreign_id]; switch (provider) { case 1: RowOption row_option = db.message.select().with(db.message.id, "=", foreign_id).row(); @@ -53,15 +52,15 @@ public class ContentItemStore : StreamInteractionModule, Object { if (message == null) { message = new Message.from_row(db, row_option.inner); } - items.add(new MessageItem(message, conversation, row[db.content.id])); + items.add(new MessageItem(message, conversation, row[db.content_item.id])); } break; case 2: RowOption row_option = db.file_transfer.select().with(db.file_transfer.id, "=", foreign_id).row(); if (row_option.is_present()) { - string storage_dir = stream_interactor.get_module(FileManager.IDENTITY).get_storage_dir(); + string storage_dir = FileManager.get_storage_dir(); FileTransfer file_transfer = new FileTransfer.from_row(db, row_option.inner, storage_dir); - items.add(new FileItem(file_transfer, row[db.content.id])); + items.add(new FileItem(file_transfer, row[db.content_item.id])); } break; } @@ -74,11 +73,22 @@ public class ContentItemStore : StreamInteractionModule, Object { return ret; } + public ContentItem? get_item(Conversation conversation, int type, int foreign_id) { + QueryBuilder select = db.content_item.select() + .with(db.content_item.content_type, "=", type) + .with(db.content_item.foreign_id, "=", foreign_id); + + Gee.List item = get_items_from_query(select, conversation); + + return item.size > 0 ? item[0] : null; + } + public Gee.List get_latest(Conversation conversation, int count) { - QueryBuilder select = db.content.select() - .with(db.content.conversation_id, "=", conversation.id) - .order_by(db.content.local_time, "DESC") - .order_by(db.content.time, "DESC") + QueryBuilder select = db.content_item.select() + .with(db.content_item.conversation_id, "=", conversation.id) + .with(db.content_item.hide, "=", false) + .order_by(db.content_item.local_time, "DESC") + .order_by(db.content_item.time, "DESC") .limit(count); return get_items_from_query(select, conversation); @@ -87,11 +97,12 @@ public class ContentItemStore : StreamInteractionModule, Object { public Gee.List get_before(Conversation conversation, ContentItem item, int count) { long local_time = (long) item.sort_time.to_unix(); long time = (long) item.display_time.to_unix(); - QueryBuilder select = db.content.select() + QueryBuilder select = db.content_item.select() .where(@"local_time < ? OR (local_time = ? AND time < ?) OR (local_time = ? AND time = ? AND id < ?)", { local_time.to_string(), local_time.to_string(), time.to_string(), local_time.to_string(), time.to_string(), item.id.to_string() }) - .with(db.content.conversation_id, "=", conversation.id) - .order_by(db.content.local_time, "DESC") - .order_by(db.content.time, "DESC") + .with(db.content_item.conversation_id, "=", conversation.id) + .with(db.content_item.hide, "=", false) + .order_by(db.content_item.local_time, "DESC") + .order_by(db.content_item.time, "DESC") .limit(count); return get_items_from_query(select, conversation); @@ -100,11 +111,12 @@ public class ContentItemStore : StreamInteractionModule, Object { public Gee.List get_after(Conversation conversation, ContentItem item, int count) { long local_time = (long) item.sort_time.to_unix(); long time = (long) item.display_time.to_unix(); - QueryBuilder select = db.content.select() + QueryBuilder select = db.content_item.select() .where(@"local_time > ? OR (local_time = ? AND time > ?) OR (local_time = ? AND time = ? AND id > ?)", { local_time.to_string(), local_time.to_string(), time.to_string(), local_time.to_string(), time.to_string(), item.id.to_string() }) - .with(db.content.conversation_id, "=", conversation.id) - .order_by(db.content.local_time, "ASC") - .order_by(db.content.time, "ASC") + .with(db.content_item.conversation_id, "=", conversation.id) + .with(db.content_item.hide, "=", false) + .order_by(db.content_item.local_time, "ASC") + .order_by(db.content_item.time, "ASC") .limit(count); return get_items_from_query(select, conversation); @@ -114,11 +126,10 @@ public class ContentItemStore : StreamInteractionModule, Object { filters.add(content_filter); } - private void on_new_message(Message message, Conversation conversation) { + public void insert_message(Message message, Conversation conversation, bool hide = false) { MessageItem item = new MessageItem(message, conversation, -1); + item.id = db.add_content_item(conversation, message.time, message.local_time, 1, message.id, hide); if (!discard(item)) { - item.id = db.add_content_item(conversation, message.time, message.local_time, 1, message.id); - if (collection_conversations.has_key(conversation)) { collection_conversations.get(conversation).insert_item(item); } @@ -129,7 +140,7 @@ public class ContentItemStore : StreamInteractionModule, Object { private void insert_file_transfer(FileTransfer file_transfer, Conversation conversation) { FileItem item = new FileItem(file_transfer, -1); if (!discard(item)) { - item.id = db.add_content_item(conversation, file_transfer.time, file_transfer.local_time, 2, file_transfer.id); + item.id = db.add_content_item(conversation, file_transfer.time, file_transfer.local_time, 2, file_transfer.id, false); if (collection_conversations.has_key(conversation)) { collection_conversations.get(conversation).insert_item(item); @@ -138,6 +149,13 @@ public class ContentItemStore : StreamInteractionModule, Object { } } + public void set_item_hide(ContentItem content_item, bool hide) { + db.content_item.update() + .with(db.content_item.id, "=", content_item.id) + .set(db.content_item.hide, hide) + .perform(); + } + private bool discard(ContentItem content_item) { foreach (ContentFilter filter in filters) { if (filter.discard(content_item)) { @@ -167,12 +185,12 @@ public abstract class ContentItem : Object { public Encryption? encryption { get; set; default=null; } public Entities.Message.Marked? mark { get; set; default=null; } - public ContentItem(int id, string ty, Jid jid, DateTime sort_time, double seccondary_sort_indicator, DateTime display_time, Encryption encryption, Entities.Message.Marked mark) { + public ContentItem(int id, string ty, Jid jid, DateTime sort_time, DateTime display_time, Encryption encryption, Entities.Message.Marked mark) { this.id = id; this.type_ = ty; this.jid = jid; this.sort_time = sort_time; - this.seccondary_sort_indicator = seccondary_sort_indicator; + this.seccondary_sort_indicator = id; this.display_time = display_time; this.encryption = encryption; this.mark = mark; @@ -197,7 +215,7 @@ public class MessageItem : ContentItem { public Conversation conversation; public MessageItem(Message message, Conversation conversation, int id) { - base(id, TYPE, message.from, message.local_time, message.id + 0.0845, message.time, message.encryption, message.marked); + base(id, TYPE, message.from, message.local_time, message.time, message.encryption, message.marked); this.message = message; this.conversation = conversation; @@ -218,7 +236,7 @@ public class FileItem : ContentItem { public FileItem(FileTransfer file_transfer, int id) { Jid jid = file_transfer.direction == FileTransfer.DIRECTION_SENT ? file_transfer.account.bare_jid.with_resource(file_transfer.account.resourcepart) : file_transfer.counterpart; - base(id, TYPE, jid, file_transfer.local_time, file_transfer.id + 0.0845, file_transfer.time, file_transfer.encryption, file_to_message_state(file_transfer.state)); + base(id, TYPE, jid, file_transfer.local_time, file_transfer.time, file_transfer.encryption, file_to_message_state(file_transfer.state)); this.file_transfer = file_transfer; diff --git a/libdino/src/service/database.vala b/libdino/src/service/database.vala index 8a470d12..e5ddd0f2 100644 --- a/libdino/src/service/database.vala +++ b/libdino/src/service/database.vala @@ -6,7 +6,7 @@ using Dino.Entities; namespace Dino { public class Database : Qlite.Database { - private const int VERSION = 8; + private const int VERSION = 9; public class AccountTable : Table { public Column id = new Column.Integer("id") { primary_key = true, auto_increment = true }; @@ -34,17 +34,19 @@ public class Database : Qlite.Database { } } - public class ContentTable : Table { + public class ContentItemTable : Table { public Column id = new Column.Integer("id") { primary_key = true, auto_increment = true }; public Column conversation_id = new Column.Integer("conversation_id") { not_null = true }; public Column time = new Column.Long("time") { not_null = true }; public Column local_time = new Column.Long("local_time") { not_null = true }; public Column content_type = new Column.Integer("content_type") { not_null = true }; public Column foreign_id = new Column.Integer("foreign_id") { not_null = true }; + public Column hide = new Column.BoolInt("hide") { default = "0", not_null = true, min_version = 9 }; - internal ContentTable(Database db) { + internal ContentItemTable(Database db) { base(db, "content_item"); - init({id, conversation_id, time, local_time, content_type, foreign_id}); + init({id, conversation_id, time, local_time, content_type, foreign_id, hide}); + index("contentitem_localtime_counterpart_idx", {local_time, conversation_id}); unique({content_type, foreign_id}, "IGNORE"); } } @@ -189,7 +191,7 @@ public class Database : Qlite.Database { public AccountTable account { get; private set; } public JidTable jid { get; private set; } - public ContentTable content { get; private set; } + public ContentItemTable content_item { get; private set; } public MessageTable message { get; private set; } public RealJidTable real_jid { get; private set; } public FileTransferTable file_transfer { get; private set; } @@ -207,7 +209,7 @@ public class Database : Qlite.Database { base(fileName, VERSION); account = new AccountTable(this); jid = new JidTable(this); - content = new ContentTable(this); + content_item = new ContentItemTable(this); message = new MessageTable(this); real_jid = new RealJidTable(this); file_transfer = new FileTransferTable(this); @@ -216,7 +218,7 @@ public class Database : Qlite.Database { entity_feature = new EntityFeatureTable(this); roster = new RosterTable(this); settings = new SettingsTable(this); - init({ account, jid, content, message, real_jid, file_transfer, conversation, avatar, entity_feature, roster, settings }); + init({ account, jid, content_item, message, real_jid, file_transfer, conversation, avatar, entity_feature, roster, settings }); try { exec("PRAGMA synchronous=0"); } catch (Error e) { } @@ -226,10 +228,11 @@ public class Database : Qlite.Database { // new table columns are added, outdated columns are still present if (oldVersion < 7) { message.fts_rebuild(); - } else if (oldVersion < 8) { + } + if (oldVersion < 8) { exec(""" - insert into content_item (conversation_id, time, local_time, content_type, foreign_id) - select conversation.id, message.time, message.local_time, 1, message.id + insert into content_item (conversation_id, time, local_time, content_type, foreign_id, hide) + select conversation.id, message.time, message.local_time, 1, message.id, 0 from message join conversation on message.account_id=conversation.account_id and message.counterpart_id=conversation.jid_id and @@ -239,7 +242,7 @@ public class Database : Qlite.Database { message.body not in (select info from file_transfer where info not null) and message.id not in (select info from file_transfer where info not null) union - select conversation.id, message.time, message.local_time, 2, file_transfer.id + select conversation.id, message.time, message.local_time, 2, file_transfer.id, 0 from file_transfer join message on file_transfer.info=message.id @@ -249,6 +252,19 @@ public class Database : Qlite.Database { message.type=conversation.type+1 and (message.counterpart_resource=conversation.resource or message.type != 3)"""); } + if (oldVersion < 9) { + exec(""" + insert into content_item (conversation_id, time, local_time, content_type, foreign_id, hide) + select conversation.id, message.time, message.local_time, 1, message.id, 1 + from message join conversation on + message.account_id=conversation.account_id and + message.counterpart_id=conversation.jid_id and + message.type=conversation.type+1 and + (message.counterpart_resource=conversation.resource or message.type != 3) + where + message.body in (select info from file_transfer where info not null) or + message.id in (select info from file_transfer where info not null)"""); + } } public ArrayList get_accounts() { @@ -275,13 +291,14 @@ public class Database : Qlite.Database { } } - public int add_content_item(Conversation conversation, DateTime time, DateTime local_time, int content_type, int foreign_id) { - return (int) content.insert() - .value(content.conversation_id, conversation.id) - .value(content.local_time, (long) local_time.to_unix()) - .value(content.time, (long) time.to_unix()) - .value(content.content_type, content_type) - .value(content.foreign_id, foreign_id) + public int add_content_item(Conversation conversation, DateTime time, DateTime local_time, int content_type, int foreign_id, bool hide) { + return (int) content_item.insert() + .value(content_item.conversation_id, conversation.id) + .value(content_item.local_time, (long) local_time.to_unix()) + .value(content_item.time, (long) time.to_unix()) + .value(content_item.content_type, content_type) + .value(content_item.foreign_id, foreign_id) + .value(content_item.hide, hide) .perform(); } diff --git a/libdino/src/service/message_storage.vala b/libdino/src/service/message_storage.vala index 50fc94b3..9c077109 100644 --- a/libdino/src/service/message_storage.vala +++ b/libdino/src/service/message_storage.vala @@ -28,6 +28,7 @@ public class MessageStorage : StreamInteractionModule, Object { message.persist(db); init_conversation(conversation); messages[conversation].add(message); + stream_interactor.get_module(ContentItemStore.IDENTITY).insert_message(message, conversation); } public Gee.List get_messages(Conversation conversation, int count = 50) { diff --git a/libdino/src/service/search_processor.vala b/libdino/src/service/search_processor.vala index 5a14dfe7..6a08d6b8 100644 --- a/libdino/src/service/search_processor.vala +++ b/libdino/src/service/search_processor.vala @@ -63,8 +63,8 @@ public class SearchProcessor : StreamInteractionModule, Object { .outer_join_with(db.real_jid, db.real_jid.message_id, db.message.id) .with(db.account.enabled, "=", true); if (join_content) { - rows.join_on(db.content, "message.id=content_item.foreign_id AND content_item.content_type=1") - .with(db.content.content_type, "=", 1); + rows.join_on(db.content_item, "message.id=content_item.foreign_id AND content_item.content_type=1") + .with(db.content_item.content_type, "=", 1); } if (with != null) { if (with.index_of("/") > 0) { @@ -233,7 +233,7 @@ public class SearchProcessor : StreamInteractionModule, Object { foreach (Row row in rows) { Message message = new Message.from_row(db, row); Conversation? conversation = stream_interactor.get_module(ConversationManager.IDENTITY).get_conversation_for_message(message); - ret.add(new MessageItem(message, conversation, row[db.content.id])); + ret.add(new MessageItem(message, conversation, row[db.content_item.id])); } return ret; } diff --git a/plugins/http-files/src/file_provider.vala b/plugins/http-files/src/file_provider.vala index 7616d535..6f4b19f4 100644 --- a/plugins/http-files/src/file_provider.vala +++ b/plugins/http-files/src/file_provider.vala @@ -26,8 +26,8 @@ public class FileProvider : Dino.FileProvider, Object { private class ReceivedMessageListener : MessageListener { - public string[] after_actions_const = new string[]{ }; - public override string action_group { get { return "DECRYPT"; } } + public string[] after_actions_const = new string[]{ "STORE" }; + public override string action_group { get { return ""; } } public override string[] after_actions { get { return after_actions_const; } } private FileProvider outer; @@ -91,6 +91,12 @@ public class FileProvider : Dino.FileProvider, Object { file_transfer.provider = 0; file_transfer.info = message.id.to_string(); file_incoming(file_transfer, conversation); + + ContentItem? content_item = stream_interactor.get_module(ContentItemStore.IDENTITY).get_item(conversation, 1, message.id); + if (content_item != null) { + stream_interactor.get_module(ContentItemStore.IDENTITY).set_item_hide(content_item, true); + } + success = true; Idle.add((owned)callback); }); diff --git a/plugins/http-files/src/manager.vala b/plugins/http-files/src/manager.vala index 9faa8933..78697f9c 100644 --- a/plugins/http-files/src/manager.vala +++ b/plugins/http-files/src/manager.vala @@ -40,6 +40,11 @@ public class Manager : StreamInteractionModule, FileSender, Object { message.encryption = Encryption.NONE; stream_interactor.get_module(MessageProcessor.IDENTITY).send_message(message, conversation); file_transfer.info = message.id.to_string(); + + ContentItem? content_item = stream_interactor.get_module(ContentItemStore.IDENTITY).get_item(conversation, 1, message.id); + if (content_item != null) { + stream_interactor.get_module(ContentItemStore.IDENTITY).set_item_hide(content_item, true); + } }, (stream, error_str) => { print(@"Failed getting upload url + $error_str\n");