Roster versioning

This commit is contained in:
fiaxh 2017-05-21 23:30:30 +02:00
parent 4247922e8c
commit bcb96909c9
8 changed files with 295 additions and 121 deletions

View file

@ -30,7 +30,7 @@ public class Dino.Application : Gtk.Application {
CounterpartInteractionManager.start(stream_interaction); CounterpartInteractionManager.start(stream_interaction);
PresenceManager.start(stream_interaction); PresenceManager.start(stream_interaction);
MucManager.start(stream_interaction); MucManager.start(stream_interaction);
RosterManager.start(stream_interaction); RosterManager.start(stream_interaction, db);
ConversationManager.start(stream_interaction, db); ConversationManager.start(stream_interaction, db);
ChatInteraction.start(stream_interaction); ChatInteraction.start(stream_interaction);

View file

@ -115,6 +115,31 @@ public class Database : Qlite.Database {
} }
} }
public class RosterTable : Table {
public Column<int> account_id = new Column.Integer("account_id");
public Column<string> jid = new Column.Text("jid");
public Column<string> name = new Column.Text("name");
public Column<string> subscription = new Column.Text("subscription");
internal RosterTable(Database db) {
base(db, "roster");
init({account_id, jid, name, subscription});
unique({account_id, jid}, "IGNORE");
}
}
public class AccountKeyValueTable : Table {
public Column<int> account_id = new Column.Integer("account_id");
public Column<string> key = new Column.Text("key");
public Column<string> value = new Column.Text("value");
internal AccountKeyValueTable(Database db) {
base(db, "account_key_value");
init({account_id, key, value});
unique({account_id, key}, "IGNORE");
}
}
public AccountTable account { get; private set; } public AccountTable account { get; private set; }
public JidTable jid { get; private set; } public JidTable jid { get; private set; }
public MessageTable message { get; private set; } public MessageTable message { get; private set; }
@ -122,6 +147,8 @@ public class Database : Qlite.Database {
public ConversationTable conversation { get; private set; } public ConversationTable conversation { get; private set; }
public AvatarTable avatar { get; private set; } public AvatarTable avatar { get; private set; }
public EntityFeatureTable entity_feature { get; private set; } public EntityFeatureTable entity_feature { get; private set; }
public RosterTable roster { get; private set; }
public AccountKeyValueTable account_key_value { get; private set; }
public Map<int, string> jid_table_cache = new HashMap<int, string>(); public Map<int, string> jid_table_cache = new HashMap<int, string>();
public Map<string, int> jid_table_reverse = new HashMap<string, int>(); public Map<string, int> jid_table_reverse = new HashMap<string, int>();
@ -136,7 +163,9 @@ public class Database : Qlite.Database {
conversation = new ConversationTable(this); conversation = new ConversationTable(this);
avatar = new AvatarTable(this); avatar = new AvatarTable(this);
entity_feature = new EntityFeatureTable(this); entity_feature = new EntityFeatureTable(this);
init({ account, jid, message, real_jid, conversation, avatar, entity_feature }); roster = new RosterTable(this);
account_key_value = new AccountKeyValueTable(this);
init({ account, jid, message, real_jid, conversation, avatar, entity_feature, roster, account_key_value });
exec("PRAGMA synchronous=0"); exec("PRAGMA synchronous=0");
} }

View file

@ -13,32 +13,30 @@ public class RosterManager : StreamInteractionModule, Object {
public signal void updated_roster_item(Account account, Jid jid, Roster.Item roster_item); public signal void updated_roster_item(Account account, Jid jid, Roster.Item roster_item);
private StreamInteractor stream_interactor; private StreamInteractor stream_interactor;
private Database db;
private Gee.Map<Account, RosterStoreImpl> roster_stores = new HashMap<Account, RosterStoreImpl>(Account.hash_func, Account.equals_func);
public static void start(StreamInteractor stream_interactor) { public static void start(StreamInteractor stream_interactor, Database db) {
RosterManager m = new RosterManager(stream_interactor); RosterManager m = new RosterManager(stream_interactor, db);
stream_interactor.add_module(m); stream_interactor.add_module(m);
} }
public RosterManager(StreamInteractor stream_interactor) { public RosterManager(StreamInteractor stream_interactor, Database db) {
this.stream_interactor = stream_interactor; this.stream_interactor = stream_interactor;
this.db = db;
stream_interactor.account_added.connect(on_account_added); stream_interactor.account_added.connect(on_account_added);
stream_interactor.module_manager.initialize_account_modules.connect((account, modules) => {
if (!roster_stores.has_key(account)) roster_stores[account] = new RosterStoreImpl(account, db);
modules.add(new Roster.VersioningModule(roster_stores[account]));
});
} }
public ArrayList<Roster.Item> get_roster(Account account) { public Collection<Roster.Item> get_roster(Account account) {
Core.XmppStream? stream = stream_interactor.get_stream(account); return roster_stores[account].get_roster();
ArrayList<Roster.Item> ret = new ArrayList<Roster.Item>();
if (stream != null) {
ret.add_all(stream.get_flag(Roster.Flag.IDENTITY).get_roster());
}
return ret;
} }
public Roster.Item? get_roster_item(Account account, Jid jid) { public Roster.Item? get_roster_item(Account account, Jid jid) {
Core.XmppStream? stream = stream_interactor.get_stream(account); return roster_stores[account].get_item(jid);
if (stream == null) return null;
Xmpp.Roster.Flag? flag = stream.get_flag(Xmpp.Roster.Flag.IDENTITY);
if (flag == null) return null;
return flag.get_item(jid.bare_jid.to_string());
} }
public void remove_jid(Account account, Jid jid) { public void remove_jid(Account account, Jid jid) {
@ -53,7 +51,9 @@ public class RosterManager : StreamInteractionModule, Object {
private void on_account_added(Account account) { private void on_account_added(Account account) {
stream_interactor.module_manager.get_module(account, Roster.Module.IDENTITY).received_roster.connect( (stream, roster) => { stream_interactor.module_manager.get_module(account, Roster.Module.IDENTITY).received_roster.connect( (stream, roster) => {
on_roster_received(account, roster); foreach (Roster.Item roster_item in roster) {
on_roster_item_updated(account, roster_item);
}
}); });
stream_interactor.module_manager.get_module(account, Roster.Module.IDENTITY).item_removed.connect( (stream, roster_item) => { stream_interactor.module_manager.get_module(account, Roster.Module.IDENTITY).item_removed.connect( (stream, roster_item) => {
removed_roster_item(account, new Jid(roster_item.jid), roster_item); removed_roster_item(account, new Jid(roster_item.jid), roster_item);
@ -63,15 +63,83 @@ public class RosterManager : StreamInteractionModule, Object {
}); });
} }
private void on_roster_received(Account account, Collection<Roster.Item> roster_items) {
foreach (Roster.Item roster_item in roster_items) {
on_roster_item_updated(account, roster_item);
}
}
private void on_roster_item_updated(Account account, Roster.Item roster_item) { private void on_roster_item_updated(Account account, Roster.Item roster_item) {
updated_roster_item(account, new Jid(roster_item.jid), roster_item); updated_roster_item(account, new Jid(roster_item.jid), roster_item);
} }
} }
public class RosterStoreImpl : Roster.Storage, Object {
private Account account;
private Database db;
private string version = "";
private HashMap<string, Roster.Item> items = new HashMap<string, Roster.Item>();
public class RosterStoreImpl(Account account, Database db) {
this.account = account;
this.db = db;
version = db_get_roster_version() ?? "";
foreach (Qlite.Row row in db.roster.select().with(db.roster.account_id, "=", account.id)) {
Roster.Item item = new Roster.Item();
item.jid = row[db.roster.jid];
item.name = row[db.roster.name];
item.subscription = row[db.roster.subscription];
items[item.jid] = item;
}
}
public string? get_roster_version() {
return version;
}
public Collection<Roster.Item> get_roster() {
return items.values;
}
public Roster.Item? get_item(Jid jid) {
return items.has_key(jid.bare_jid.to_string()) ? items[jid.bare_jid.to_string()] : null;
}
public void set_roster_version(string version) {
db.account_key_value.insert().or("REPLACE")
.value(db.account_key_value.account_id, account.id)
.value(db.account_key_value.key, "roster_version")
.value(db.account_key_value.value, version)
.perform();
}
public void set_roster(Collection<Roster.Item> items) {
db.roster.delete().with(db.roster.account_id, "=", account.id).perform();
foreach (Roster.Item item in items) {
set_item(item);
}
}
public void set_item(Roster.Item item) {
items[item.jid] = item;
db.roster.insert().or("REPLACE")
.value(db.roster.account_id, account.id)
.value(db.roster.jid, item.jid)
.value(db.roster.name, item.name)
.value(db.roster.subscription, item.subscription)
.perform();
}
public void remove_item(Roster.Item item) {
items.unset(item.jid);
db.roster.delete()
.with(db.roster.account_id, "=", account.id)
.with(db.roster.jid, "=", item.jid);
}
private string? db_get_roster_version() {
Qlite.Row? row = db.account_key_value.select()
.with(db.account_key_value.account_id, "=", account.id)
.with(db.account_key_value.key, "=", "roster_version").iterator().get_next();
if (row != null) return row[db.account_key_value.value];
return null;
}
}
} }

View file

@ -26,6 +26,7 @@ SOURCES
"src/module/roster/flag.vala" "src/module/roster/flag.vala"
"src/module/roster/item.vala" "src/module/roster/item.vala"
"src/module/roster/module.vala" "src/module/roster/module.vala"
"src/module/roster/versioning_module.vala"
"src/module/sasl.vala" "src/module/sasl.vala"
"src/module/stanza.vala" "src/module/stanza.vala"
"src/module/stanza_error.vala" "src/module/stanza_error.vala"

View file

@ -25,12 +25,12 @@ public class Item {
public string? name { public string? name {
get { return stanza_node.get_attribute(NODE_NAME); } get { return stanza_node.get_attribute(NODE_NAME); }
set { stanza_node.set_attribute(NODE_NAME, value); } set { if (value != null) stanza_node.set_attribute(NODE_NAME, value); }
} }
public string? subscription { public string? subscription {
get { return stanza_node.get_attribute(NODE_SUBSCRIPTION); } get { return stanza_node.get_attribute(NODE_SUBSCRIPTION); }
set { stanza_node.set_attribute(NODE_SUBSCRIPTION, value); } set { if (value != null) stanza_node.set_attribute(NODE_SUBSCRIPTION, value); }
} }
public Item() { public Item() {

View file

@ -3,20 +3,19 @@ using Gee;
using Xmpp.Core; using Xmpp.Core;
namespace Xmpp.Roster { namespace Xmpp.Roster {
private const string NS_URI = "jabber:iq:roster"; private const string NS_URI = "jabber:iq:roster";
public class Module : XmppStreamModule, Iq.Handler { public class Module : XmppStreamModule, Iq.Handler {
public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "roster_module"); public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "roster_module");
public signal void received_roster(XmppStream stream, Collection<Item> roster); public signal void received_roster(XmppStream stream, Collection<Item> roster, Iq.Stanza stanza);
public signal void item_removed(XmppStream stream, Item roster_item); public signal void pre_get_roster(XmppStream stream, Iq.Stanza iq);
public signal void item_updated(XmppStream stream, Item roster_item); public signal void item_removed(XmppStream stream, Item item, Iq.Stanza iq);
public signal void item_updated(XmppStream stream, Item item, Iq.Stanza iq);
public bool interested_resource = true; public bool interested_resource = true;
/**
* Add a jid to the roster
*/
public void add_jid(XmppStream stream, string jid, string? handle = null) { public void add_jid(XmppStream stream, string jid, string? handle = null) {
Item roster_item = new Item(); Item roster_item = new Item();
roster_item.jid = jid; roster_item.jid = jid;
@ -26,9 +25,6 @@ namespace Xmpp.Roster {
roster_set(stream, roster_item); roster_set(stream, roster_item);
} }
/**
* Remove a jid from the roster
*/
public void remove_jid(XmppStream stream, string jid) { public void remove_jid(XmppStream stream, string jid) {
Item roster_item = new Item(); Item roster_item = new Item();
roster_item.jid = jid; roster_item.jid = jid;
@ -60,11 +56,11 @@ namespace Xmpp.Roster {
switch (item.subscription) { switch (item.subscription) {
case Item.SUBSCRIPTION_REMOVE: case Item.SUBSCRIPTION_REMOVE:
flag.roster_items.unset(item.jid); flag.roster_items.unset(item.jid);
item_removed(stream, item); item_removed(stream, item, iq);
break; break;
default: default:
flag.roster_items[item.jid] = item; flag.roster_items[item.jid] = item;
item_updated(stream, item); item_updated(stream, item, iq);
break; break;
} }
} }
@ -94,6 +90,8 @@ namespace Xmpp.Roster {
stream.get_flag(Flag.IDENTITY).iq_id = random_uuid(); stream.get_flag(Flag.IDENTITY).iq_id = random_uuid();
StanzaNode query_node = new StanzaNode.build("query", NS_URI).add_self_xmlns(); StanzaNode query_node = new StanzaNode.build("query", NS_URI).add_self_xmlns();
Iq.Stanza iq = new Iq.Stanza.get(query_node, stream.get_flag(Flag.IDENTITY).iq_id); Iq.Stanza iq = new Iq.Stanza.get(query_node, stream.get_flag(Flag.IDENTITY).iq_id);
pre_get_roster(stream, iq);
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, on_roster_get_received); stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, on_roster_get_received);
} }
@ -101,11 +99,13 @@ namespace Xmpp.Roster {
Flag flag = stream.get_flag(Flag.IDENTITY); Flag flag = stream.get_flag(Flag.IDENTITY);
if (iq.id == flag.iq_id) { if (iq.id == flag.iq_id) {
StanzaNode? query_node = iq.stanza.get_subnode("query", NS_URI); StanzaNode? query_node = iq.stanza.get_subnode("query", NS_URI);
if (query_node != null) {
foreach (StanzaNode item_node in query_node.sub_nodes) { foreach (StanzaNode item_node in query_node.sub_nodes) {
Item item = new Item.from_stanza_node(item_node); Item item = new Item.from_stanza_node(item_node);
flag.roster_items[item.jid] = item; flag.roster_items[item.jid] = item;
} }
stream.get_module(Module.IDENTITY).received_roster(stream, flag.roster_items.values); }
stream.get_module(Module.IDENTITY).received_roster(stream, flag.roster_items.values, iq);
} }
} }
@ -116,4 +116,5 @@ namespace Xmpp.Roster {
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq); stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq);
} }
} }
} }

View file

@ -0,0 +1,75 @@
using Gee;
using Xmpp.Core;
namespace Xmpp.Roster {
public class VersioningModule : XmppStreamModule {
private const string NS_URI_FEATURE = "urn:xmpp:features:rosterver";
public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "roster_versioning");
private Storage storage;
public VersioningModule(Storage storage) {
this.storage = storage;
}
public override void attach(XmppStream stream) {
Module.require(stream);
stream.get_module(Module.IDENTITY).pre_get_roster.connect(on_pre_get_roster);
stream.get_module(Module.IDENTITY).received_roster.connect(on_received_roster);
stream.get_module(Module.IDENTITY).item_updated.connect(on_item_updated);
stream.get_module(Module.IDENTITY).item_removed.connect(on_item_removed);
}
public override void detach(XmppStream stream) {
stream.get_module(Module.IDENTITY).pre_get_roster.disconnect(on_pre_get_roster);
}
internal override string get_ns() { return NS_URI; }
internal override string get_id() { return IDENTITY.id; }
private void on_pre_get_roster(XmppStream stream, Iq.Stanza iq) {
StanzaNode? ver_feature = stream.features.get_subnode("ver", NS_URI_FEATURE);
if (ver_feature != null) {
iq.stanza.get_subnode("query", NS_URI).set_attribute("ver", storage.get_roster_version() ?? "");
}
}
private void on_received_roster(XmppStream stream, Collection<Item> roster, Iq.Stanza iq) {
string? ver = iq.stanza.get_deep_attribute(NS_URI + ":query", NS_URI + ":ver");
if (ver != null) storage.set_roster_version(ver);
if (iq.stanza.get_subnode("query", NS_URI) != null) {
storage.set_roster(roster);
} else {
Flag flag = stream.get_flag(Flag.IDENTITY);
foreach (Item item in storage.get_roster()) {
flag.roster_items[item.jid] = item;
}
}
}
private void on_item_updated(XmppStream stream, Item item, Iq.Stanza iq) {
string? ver = iq.stanza.get_deep_attribute(NS_URI + ":query", NS_URI + ":ver");
if (ver != null) storage.set_roster_version(ver);
storage.set_item(item);
}
private void on_item_removed(XmppStream stream, Item item, Iq.Stanza iq) {
string? ver = iq.stanza.get_deep_attribute(NS_URI + ":query", NS_URI + ":ver");
if (ver != null) storage.set_roster_version(ver);
storage.remove_item(item);
}
}
public interface Storage : Object {
public abstract string? get_roster_version();
public abstract Collection<Roster.Item> get_roster();
public abstract void set_roster_version(string version);
public abstract void set_roster(Collection<Roster.Item> items);
public abstract void set_item(Roster.Item item);
public abstract void remove_item(Roster.Item item);
}
}