aboutsummaryrefslogtreecommitdiff
path: root/libdino/src/service
diff options
context:
space:
mode:
authorfiaxh <git@mx.ax.lt>2017-03-22 17:15:06 +0100
committerfiaxh <git@mx.ax.lt>2017-03-23 14:36:53 +0100
commitfa78573b052693b29350bdd0f7eaf74dc6571e4a (patch)
tree7efcb3eb96e5c3d17c6a1c0604eff533e1244a20 /libdino/src/service
parentec06d5f9ebd7142f4be422e73b46d6702671927d (diff)
downloaddino-fa78573b052693b29350bdd0f7eaf74dc6571e4a.tar.gz
dino-fa78573b052693b29350bdd0f7eaf74dc6571e4a.zip
Move some database interaction into entities
fixes #2
Diffstat (limited to 'libdino/src/service')
-rw-r--r--libdino/src/service/conversation_manager.vala5
-rw-r--r--libdino/src/service/database.vala192
-rw-r--r--libdino/src/service/message_manager.vala12
3 files changed, 17 insertions, 192 deletions
diff --git a/libdino/src/service/conversation_manager.vala b/libdino/src/service/conversation_manager.vala
index 10c6ac51..2d046e83 100644
--- a/libdino/src/service/conversation_manager.vala
+++ b/libdino/src/service/conversation_manager.vala
@@ -92,10 +92,9 @@ public class ConversationManager : StreamInteractionModule, Object {
private void ensure_add_conversation(Jid jid, Account account, Conversation.Type type) {
if (conversations.has_key(account) && !conversations[account].has_key(jid)) {
- Conversation conversation = new Conversation(jid, account);
- conversation.type_ = type;
+ Conversation conversation = new Conversation(jid, account, type);
add_conversation(conversation);
- db.add_conversation(conversation);
+ conversation.persist(db);
}
}
diff --git a/libdino/src/service/database.vala b/libdino/src/service/database.vala
index 29c6f3ee..f631ff1f 100644
--- a/libdino/src/service/database.vala
+++ b/libdino/src/service/database.vala
@@ -137,122 +137,23 @@ public class Database : Qlite.Database {
// new table columns are added, outdated columns are still present
}
- public void add_account(Account new_account) {
- new_account.id = (int) account.insert()
- .value(account.bare_jid, new_account.bare_jid.to_string())
- .value(account.resourcepart, new_account.resourcepart)
- .value(account.password, new_account.password)
- .value(account.alias, new_account.alias)
- .value(account.enabled, new_account.enabled)
- .perform();
- new_account.notify.connect(on_account_update);
- }
-
- private void on_account_update(Object o, ParamSpec sp) {
- Account changed_account = (Account) o;
- account.update().with(account.id, "=", changed_account.id)
- .set(account.bare_jid, changed_account.bare_jid.to_string())
- .set(account.resourcepart, changed_account.resourcepart)
- .set(account.password, changed_account.password)
- .set(account.alias, changed_account.alias)
- .set(account.enabled, changed_account.enabled)
- .perform();
- }
-
- public void remove_account(Account to_delete) {
- account.delete().with(account.bare_jid, "=", to_delete.bare_jid.to_string()).perform();
- }
-
public ArrayList<Account> get_accounts() {
ArrayList<Account> ret = new ArrayList<Account>();
foreach(Row row in account.select()) {
- Account account = get_account_from_row(row);
- account.notify.connect(on_account_update);
+ Account account = new Account.from_row(this, row);
ret.add(account);
}
return ret;
}
- private Account? get_account_by_id(int id) {
+ public Account? get_account_by_id(int id) {
Row? row = account.row_with(account.id, id).inner;
if (row != null) {
- return get_account_from_row(row);
+ return new Account.from_row(this, row);
}
return null;
}
- private Account get_account_from_row(Row row) {
- Account new_account = new Account.from_bare_jid(row[account.bare_jid]);
-
- new_account.id = row[account.id];
- new_account.resourcepart = row[account.resourcepart];
- new_account.password = row[account.password];
- new_account.alias = row[account.alias];
- new_account.enabled = row[account.enabled];
- return new_account;
- }
-
- public void add_message(Message new_message, Account account) {
- InsertBuilder builder = message.insert()
- .value(message.account_id, new_message.account.id)
- .value(message.counterpart_id, get_jid_id(new_message.counterpart))
- .value(message.counterpart_resource, new_message.counterpart.resourcepart)
- .value(message.our_resource, new_message.ourpart.resourcepart)
- .value(message.direction, new_message.direction)
- .value(message.type_, new_message.type_)
- .value(message.time, (long) new_message.time.to_unix())
- .value(message.local_time, (long) new_message.local_time.to_unix())
- .value(message.body, new_message.body)
- .value(message.encryption, new_message.encryption)
- .value(message.marked, new_message.marked);
- if (new_message.stanza_id != null) builder.value(message.stanza_id, new_message.stanza_id);
- new_message.id = (int) builder.perform();
-
- if (new_message.real_jid != null) {
- real_jid.insert()
- .value(real_jid.message_id, new_message.id)
- .value(real_jid.real_jid, new_message.real_jid)
- .perform();
- }
- new_message.notify.connect(on_message_update);
- }
-
- private void on_message_update(Object o, ParamSpec sp) {
- Message changed_message = (Message) o;
- UpdateBuilder update_builder = message.update().with(message.id, "=", changed_message.id);
- switch (sp.get_name()) {
- case "stanza_id":
- update_builder.set(message.stanza_id, changed_message.stanza_id); break;
- case "counterpart":
- update_builder.set(message.counterpart_id, get_jid_id(changed_message.counterpart));
- update_builder.set(message.counterpart_resource, changed_message.counterpart.resourcepart); break;
- case "ourpart":
- update_builder.set(message.our_resource, changed_message.ourpart.resourcepart); break;
- case "direction":
- update_builder.set(message.direction, changed_message.direction); break;
- case "type_":
- update_builder.set(message.type_, changed_message.type_); break;
- case "time":
- update_builder.set(message.time, (long) changed_message.time.to_unix()); break;
- case "local_time":
- update_builder.set(message.local_time, (long) changed_message.local_time.to_unix()); break;
- case "body":
- update_builder.set(message.body, changed_message.body); break;
- case "encryption":
- update_builder.set(message.encryption, changed_message.encryption); break;
- case "marked":
- update_builder.set(message.marked, changed_message.marked); break;
- }
- update_builder.perform();
-
- if (sp.get_name() == "real_jid") {
- real_jid.insert()
- .value(real_jid.message_id, changed_message.id)
- .value(real_jid.real_jid, changed_message.real_jid)
- .perform();
- }
- }
-
public Gee.List<Message> get_messages(Jid jid, Account account, int count, Message? before) {
string jid_id = get_jid_id(jid).to_string();
@@ -267,7 +168,7 @@ public class Database : Qlite.Database {
LinkedList<Message> ret = new LinkedList<Message>();
foreach (Row row in select) {
- ret.insert(0, get_message_from_row(row));
+ ret.insert(0, new Message.from_row(this, row));
}
return ret;
}
@@ -275,7 +176,7 @@ public class Database : Qlite.Database {
public Gee.List<Message> get_unsend_messages(Account account) {
Gee.List<Message> ret = new ArrayList<Message>();
foreach (Row row in message.select().with(message.marked, "=", (int) Message.Marked.UNSENT)) {
- ret.add(get_message_from_row(row));
+ ret.add(new Message.from_row(this, row));
}
return ret;
}
@@ -307,94 +208,19 @@ public class Database : Qlite.Database {
public Message? get_message_by_id(int id) {
Row? row = message.row_with(message.id, id).inner;
if (row != null) {
- return get_message_from_row(row);
+ return new Message.from_row(this, row);
}
return null;
}
- public Message get_message_from_row(Row row) {
- Message new_message = new Message();
-
- new_message.id = row[message.id];
- new_message.stanza_id = row[message.stanza_id];
- string from = get_jid_by_id(row[message.counterpart_id]);
- string from_resource = row[message.counterpart_resource];
- if (from_resource != null) {
- new_message.counterpart = new Jid(from + "/" + from_resource);
- } else {
- new_message.counterpart = new Jid(from);
- }
- new_message.direction = row[message.direction];
- new_message.type_ = (Message.Type) row[message.type_];
- new_message.time = new DateTime.from_unix_utc(row[message.time]);
- new_message.body = row[message.body];
- new_message.account = get_account_by_id(row[message.account_id]); // TODO dont have to generate acc new
- new_message.marked = (Message.Marked) row[message.marked];
- new_message.encryption = (Encryption) row[message.encryption];
- new_message.real_jid = get_real_jid_for_message(new_message);
-
- new_message.notify.connect(on_message_update);
- return new_message;
- }
-
- public string? get_real_jid_for_message(Message message) {
- return real_jid.select({real_jid.real_jid}).with(real_jid.message_id, "=", message.id)[real_jid.real_jid];
- }
-
- public void add_conversation(Conversation new_conversation) {
- var insert = conversation.insert()
- .value(conversation.jid_id, get_jid_id(new_conversation.counterpart))
- .value(conversation.account_id, new_conversation.account.id)
- .value(conversation.type_, new_conversation.type_)
- .value(conversation.encryption, new_conversation.encryption)
- //.value(conversation.read_up_to, new_conversation.read_up_to)
- .value(conversation.active, new_conversation.active);
- if (new_conversation.last_active != null) {
- insert.value(conversation.last_active, (long) new_conversation.last_active.to_unix());
- }
- new_conversation.id = (int) insert.perform();
- new_conversation.notify.connect(on_conversation_update);
- }
-
public ArrayList<Conversation> get_conversations(Account account) {
ArrayList<Conversation> ret = new ArrayList<Conversation>();
foreach (Row row in conversation.select().with(conversation.account_id, "=", account.id)) {
- ret.add(get_conversation_from_row(row));
+ ret.add(new Conversation.from_row(this, row));
}
return ret;
}
- private void on_conversation_update(Object o, ParamSpec sp) {
- Conversation changed_conversation = (Conversation) o;
- var update = conversation.update().with(conversation.jid_id, "=", get_jid_id(changed_conversation.counterpart)).with(conversation.account_id, "=", changed_conversation.account.id)
- .set(conversation.type_, changed_conversation.type_)
- .set(conversation.encryption, changed_conversation.encryption)
- //.set(conversation.read_up_to, changed_conversation.read_up_to)
- .set(conversation.active, changed_conversation.active);
- if (changed_conversation.last_active != null) {
- update.set(conversation.last_active, (long) changed_conversation.last_active.to_unix());
- } else {
- update.set_null(conversation.last_active);
- }
- update.perform();
- }
-
- private Conversation get_conversation_from_row(Row row) {
- Conversation new_conversation = new Conversation(new Jid(get_jid_by_id(row[conversation.jid_id])), get_account_by_id(row[conversation.account_id]));
-
- new_conversation.id = row[conversation.id];
- new_conversation.active = row[conversation.active];
- int64? last_active = row[conversation.last_active];
- if (last_active != null) new_conversation.last_active = new DateTime.from_unix_utc(last_active);
- new_conversation.type_ = (Conversation.Type) row[conversation.type_];
- new_conversation.encryption = (Encryption) row[conversation.encryption];
- int? read_up_to = row[conversation.read_up_to];
- if (read_up_to != null) new_conversation.read_up_to = get_message_by_id(read_up_to);
-
- new_conversation.notify.connect(on_conversation_update);
- return new_conversation;
- }
-
public void set_avatar_hash(Jid jid, string hash, int type) {
avatar.insert().or("REPLACE")
.value(avatar.jid, jid.to_string())
@@ -429,12 +255,12 @@ public class Database : Qlite.Database {
}
- private int get_jid_id(Jid jid_obj) {
+ public int get_jid_id(Jid jid_obj) {
Row? row = jid.row_with(jid.bare_jid, jid_obj.bare_jid.to_string()).inner;
return row != null ? row[jid.id] : add_jid(jid_obj);
}
- private string? get_jid_by_id(int id) {
+ public string? get_jid_by_id(int id) {
return jid.select({jid.bare_jid}).with(jid.id, "=", id)[jid.bare_jid];
}
diff --git a/libdino/src/service/message_manager.vala b/libdino/src/service/message_manager.vala
index e83e82b1..c2d1021e 100644
--- a/libdino/src/service/message_manager.vala
+++ b/libdino/src/service/message_manager.vala
@@ -36,7 +36,7 @@ public class MessageManager : StreamInteractionModule, Object {
public void send_message(string text, Conversation conversation) {
Entities.Message message = create_out_message(text, conversation);
add_message(message, conversation);
- db.add_message(message, conversation.account);
+ message.persist(db);
send_xmpp_message(message, conversation);
message_sent(message, conversation);
}
@@ -88,7 +88,8 @@ public class MessageManager : StreamInteractionModule, Object {
private void on_message_received(Account account, Xmpp.Message.Stanza message) {
if (message.body == null) return;
- Entities.Message new_message = new Entities.Message();
+ Entities.Message.Type type_ = message.type_ == Xmpp.Message.Stanza.TYPE_GROUPCHAT ? Entities.Message.Type.GROUPCHAT : Entities.Message.Type.CHAT;
+ Entities.Message new_message = new Entities.Message(message.body, type_);
new_message.account = account;
new_message.stanza_id = message.id;
Jid from_jid = new Jid(message.from);
@@ -100,9 +101,7 @@ public class MessageManager : StreamInteractionModule, Object {
}
new_message.counterpart = new_message.direction == Entities.Message.DIRECTION_SENT ? new Jid(message.to) : new Jid(message.from);
new_message.ourpart = new_message.direction == Entities.Message.DIRECTION_SENT ? new Jid(message.from) : new Jid(message.to);
- new_message.body = message.body;
new_message.stanza = message;
- new_message.set_type_string(message.type_);
Xep.DelayedDelivery.MessageFlag? deleyed_delivery_flag = Xep.DelayedDelivery.MessageFlag.get_flag(message);
new_message.time = deleyed_delivery_flag != null ? deleyed_delivery_flag.datetime : new DateTime.now_utc();
new_message.local_time = new DateTime.now_utc();
@@ -112,7 +111,7 @@ public class MessageManager : StreamInteractionModule, Object {
bool is_uuid = new_message.stanza_id != null && Regex.match_simple("""[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}""", new_message.stanza_id);
if ((is_uuid && !db.contains_message_by_stanza_id(new_message.stanza_id, conversation.account)) ||
(!is_uuid && !db.contains_message(new_message, conversation.account))) {
- db.add_message(new_message, conversation.account);
+ new_message.persist(db);
add_message(new_message, conversation);
if (new_message.time.difference(conversation.last_active) > 0) {
conversation.last_active = new_message.time;
@@ -133,7 +132,8 @@ public class MessageManager : StreamInteractionModule, Object {
}
private Entities.Message create_out_message(string text, Conversation conversation) {
- Entities.Message message = new Entities.Message();
+ Entities.Message.Type type_ = conversation.type_ == Conversation.Type.GROUPCHAT ? Entities.Message.Type.GROUPCHAT : Entities.Message.Type.CHAT;
+ Entities.Message message = new Entities.Message(text, type_);
message.stanza_id = random_uuid();
message.account = conversation.account;
message.body = text;