Improve entity identity getter

This commit is contained in:
fiaxh 2020-11-19 18:33:03 +01:00
parent f40730c780
commit 881b9eec9d
2 changed files with 33 additions and 26 deletions

View file

@ -42,32 +42,35 @@ public class EntityInfo : StreamInteractionModule, Object {
stream_interactor.module_manager.initialize_account_modules.connect(initialize_modules); stream_interactor.module_manager.initialize_account_modules.connect(initialize_modules);
} }
public async Identity? get_identity(Account account, Jid jid) { public async Gee.Set<Identity>? get_identities(Account account, Jid jid) {
Gee.Set<ServiceDiscovery.Identity>? identities = null;
if (jid_identity.has_key(jid)) { if (jid_identity.has_key(jid)) {
identities = jid_identity[jid]; return jid_identity[jid];
} }
if (identities == null) {
string? hash = entity_caps_hashes[jid]; string? hash = entity_caps_hashes[jid];
if (hash != null) { if (hash != null) {
identities = get_identities(hash); Gee.Set<Identity>? identities = get_stored_identities(hash);
if (identities != null) return identities;
} }
if (identities == null) {
ServiceDiscovery.InfoResult? info_result = yield get_info_result(account, jid, hash); ServiceDiscovery.InfoResult? info_result = yield get_info_result(account, jid, hash);
identities = info_result.identities; if (info_result != null) {
} return info_result.identities;
} }
if (identities != null) { return null;
}
public async Identity? get_identity(Account account, Jid jid) {
Gee.Set<ServiceDiscovery.Identity>? identities = yield get_identities(account, jid);
if (identities == null) return null;
foreach (var identity in identities) { foreach (var identity in identities) {
if (identity.category == Identity.CATEGORY_CLIENT) { if (identity.category == Identity.CATEGORY_CLIENT) {
return identity; return identity;
} }
} }
}
return null; return null;
} }
@ -78,7 +81,7 @@ public class EntityInfo : StreamInteractionModule, Object {
string? hash = entity_caps_hashes[jid]; string? hash = entity_caps_hashes[jid];
if (hash != null) { if (hash != null) {
Gee.List<string>? features = get_features(hash); Gee.List<string>? features = get_stored_features(hash);
if (features != null) { if (features != null) {
return features.contains(feature); return features.contains(feature);
} }
@ -132,9 +135,10 @@ public class EntityInfo : StreamInteractionModule, Object {
.value(db.entity_identity.entity_name, identity.name) .value(db.entity_identity.entity_name, identity.name)
.perform(); .perform();
} }
entity_identity[entity] = identities;
} }
private Gee.List<string>? get_features(string entity) { private Gee.List<string>? get_stored_features(string entity) {
Gee.List<string>? features = entity_features[entity]; Gee.List<string>? features = entity_features[entity];
if (features != null) { if (features != null) {
return features; return features;
@ -152,7 +156,7 @@ public class EntityInfo : StreamInteractionModule, Object {
return features; return features;
} }
private Gee.Set<Identity> get_identities(string entity) { private Gee.Set<Identity>? get_stored_identities(string entity) {
Gee.Set<Identity>? identities = entity_identity[entity]; Gee.Set<Identity>? identities = entity_identity[entity];
if (identities != null) { if (identities != null) {
return identities; return identities;
@ -164,6 +168,11 @@ public class EntityInfo : StreamInteractionModule, Object {
var identity = new Identity(row[db.entity_identity.category], row[db.entity_identity.type], row[db.entity_identity.entity_name]); var identity = new Identity(row[db.entity_identity.category], row[db.entity_identity.type], row[db.entity_identity.entity_name]);
identities.add(identity); identities.add(identity);
} }
if (entity_identity.size == 0) {
return null;
}
entity_identity[entity] = identities;
return identities; return identities;
} }
@ -209,9 +218,7 @@ public class CapsCacheImpl : CapsCache, Object {
} }
public async Gee.Set<Identity> get_entity_identities(Jid jid) { public async Gee.Set<Identity> get_entity_identities(Jid jid) {
var ret = new HashSet<Identity>(Identity.hash_func, Identity.equals_func); return yield entity_info.get_identities(account, jid);
ret.add(yield entity_info.get_identity(account, jid));
return ret;
} }
} }
} }

View file

@ -37,7 +37,7 @@ public class BadMessagesPopulator : Plugins.ConversationItemPopulator, Plugins.C
} }
private void init_state() { private void init_state() {
if (current_conversation == null) return;
if (current_conversation.type_ == Conversation.Type.GROUPCHAT_PM) return; if (current_conversation.type_ == Conversation.Type.GROUPCHAT_PM) return;
var qry = db.identity_meta.select() var qry = db.identity_meta.select()