Skip to content

Commit

Permalink
Add hidden column to content_item table
Browse files Browse the repository at this point in the history
  • Loading branch information
fiaxh committed Aug 31, 2018
1 parent ecb18af commit 0e41fb3
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 50 deletions.
72 changes: 45 additions & 27 deletions libdino/src/service/content_item_store.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -43,8 +42,8 @@ public class ContentItemStore : StreamInteractionModule, Object {
Gee.TreeSet<ContentItem> items = new Gee.TreeSet<ContentItem>(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();
Expand All @@ -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;
}
Expand All @@ -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<ContentItem> item = get_items_from_query(select, conversation);

return item.size > 0 ? item[0] : null;
}

public Gee.List<ContentItem> 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);
Expand All @@ -87,11 +97,12 @@ public class ContentItemStore : StreamInteractionModule, Object {
public Gee.List<ContentItem> 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);
Expand All @@ -100,11 +111,12 @@ public class ContentItemStore : StreamInteractionModule, Object {
public Gee.List<ContentItem> 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);
Expand All @@ -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);
}
Expand All @@ -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);
Expand All @@ -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)) {
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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;

Expand Down
53 changes: 35 additions & 18 deletions libdino/src/service/database.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> id = new Column.Integer("id") { primary_key = true, auto_increment = true };
Expand Down Expand Up @@ -34,17 +34,19 @@ public class Database : Qlite.Database {
}
}

public class ContentTable : Table {
public class ContentItemTable : Table {
public Column<int> id = new Column.Integer("id") { primary_key = true, auto_increment = true };
public Column<int> conversation_id = new Column.Integer("conversation_id") { not_null = true };
public Column<long> time = new Column.Long("time") { not_null = true };
public Column<long> local_time = new Column.Long("local_time") { not_null = true };
public Column<int> content_type = new Column.Integer("content_type") { not_null = true };
public Column<int> foreign_id = new Column.Integer("foreign_id") { not_null = true };
public Column<bool> 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");
}
}
Expand Down Expand Up @@ -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; }
Expand All @@ -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);
Expand All @@ -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) { }
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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<Account> get_accounts() {
Expand All @@ -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();
}

Expand Down
1 change: 1 addition & 0 deletions libdino/src/service/message_storage.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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<Message> get_messages(Conversation conversation, int count = 50) {
Expand Down
6 changes: 3 additions & 3 deletions libdino/src/service/search_processor.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down
10 changes: 8 additions & 2 deletions plugins/http-files/src/file_provider.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});
Expand Down
Loading

0 comments on commit 0e41fb3

Please sign in to comment.