From af52c24df7749923df897a2dd53c367a9f8ef31f Mon Sep 17 00:00:00 2001 From: fiaxh Date: Sun, 26 Apr 2020 00:20:27 +0200 Subject: Fix nick change in MUC, update bookmark accordingly; remove unused code --- libdino/src/service/conversation_manager.vala | 4 -- libdino/src/service/muc_manager.vala | 28 ++++++++++++-- libdino/src/service/presence_manager.vala | 55 ++++----------------------- 3 files changed, 32 insertions(+), 55 deletions(-) (limited to 'libdino') diff --git a/libdino/src/service/conversation_manager.vala b/libdino/src/service/conversation_manager.vala index 3b48f154..18a362c6 100644 --- a/libdino/src/service/conversation_manager.vala +++ b/libdino/src/service/conversation_manager.vala @@ -62,10 +62,6 @@ public class ConversationManager : StreamInteractionModule, Object { return null; } - public Gee.List get_conversations_for_presence(Show show, Account account) { - return get_conversations(show.jid, account); - } - public Gee.List get_conversations(Jid jid, Account account) { Gee.List ret = new ArrayList(Conversation.equals_func); Conversation? bare_conversation = get_conversation(jid, account); diff --git a/libdino/src/service/muc_manager.vala b/libdino/src/service/muc_manager.vala index 15f32c44..c4941b47 100644 --- a/libdino/src/service/muc_manager.vala +++ b/libdino/src/service/muc_manager.vala @@ -100,9 +100,29 @@ public class MucManager : StreamInteractionModule, Object { if (stream != null) stream.get_module(Xep.Muc.Module.IDENTITY).change_subject(stream, jid.bare_jid, subject); } - public void change_nick(Account account, Jid jid, string new_nick) { - XmppStream? stream = stream_interactor.get_stream(account); - if (stream != null) stream.get_module(Xep.Muc.Module.IDENTITY).change_nick(stream, jid.bare_jid, new_nick); + public async void change_nick(Conversation conversation, string new_nick) { + XmppStream? stream = stream_interactor.get_stream(conversation.account); + if (stream == null) return; + + // Check if this would be a valid nick + try { + Jid jid = conversation.counterpart.with_resource(new_nick); + } catch (InvalidJidError error) { return; } + + stream.get_module(Xep.Muc.Module.IDENTITY).change_nick(stream, conversation.counterpart, new_nick); + + conversation.nickname = new_nick; + + // Update nick in bookmark + Set? conferences = yield bookmarks_provider[conversation.account].get_conferences(stream); + if (conferences == null) return; + foreach (Conference conference in conferences) { + if (conference.jid.equals(conversation.counterpart)) { + Conference new_conference = new Conference() { jid=conversation.counterpart, nick=new_nick, name=conference.name, password=conference.password, autojoin=conference.autojoin }; + bookmarks_provider[conversation.account].replace_conference.begin(stream, conversation.counterpart, new_conference); + break; + } + } } public void invite(Account account, Jid muc, Jid invitee) { @@ -403,7 +423,7 @@ public class MucManager : StreamInteractionModule, Object { foreach (Conference conference in conferences) { if (conference.jid.equals(jid)) { if (!conference.autojoin) { - Conference new_conference = new Conference() { jid=jid, nick=conference.nick, name=conference.name, password=conference.password, autojoin=true }; + Conference new_conference = new Conference() { jid=jid, nick=nick ?? conference.nick, name=conference.name, password=password ?? conference.password, autojoin=true }; bookmarks_provider[account].replace_conference.begin(stream, jid, new_conference); } return; diff --git a/libdino/src/service/presence_manager.vala b/libdino/src/service/presence_manager.vala index f494bb54..a9dc83aa 100644 --- a/libdino/src/service/presence_manager.vala +++ b/libdino/src/service/presence_manager.vala @@ -8,13 +8,12 @@ public class PresenceManager : StreamInteractionModule, Object { public static ModuleIdentity IDENTITY = new ModuleIdentity("presence_manager"); public string id { get { return IDENTITY.id; } } - public signal void show_received(Show show, Jid jid, Account account); + public signal void show_received(Jid jid, Account account); public signal void received_offline_presence(Jid jid, Account account); public signal void received_subscription_request(Jid jid, Account account); public signal void received_subscription_approval(Jid jid, Account account); private StreamInteractor stream_interactor; - private HashMap>> shows = new HashMap>>(Jid.hash_bare_func, Jid.equals_bare_func); private HashMap> resources = new HashMap>(Jid.hash_bare_func, Jid.equals_bare_func); private Gee.List subscription_requests = new ArrayList(Jid.equals_func); @@ -28,19 +27,14 @@ public class PresenceManager : StreamInteractionModule, Object { stream_interactor.account_added.connect(on_account_added); } - public Show get_last_show(Jid jid, Account account) { + public string? get_last_show(Jid jid, Account account) { XmppStream? stream = stream_interactor.get_stream(account); - if (stream != null) { - Xmpp.Presence.Stanza? presence = stream.get_flag(Presence.Flag.IDENTITY).get_presence(jid); - if (presence != null) { - return new Show(jid, presence.show, new DateTime.now_utc()); - } - } - return new Show(jid, Show.OFFLINE, new DateTime.now_utc()); - } + if (stream == null) return null; - public HashMap>? get_shows(Jid jid, Account account) { - return shows[jid]; + Xmpp.Presence.Stanza? presence = stream.get_flag(Presence.Flag.IDENTITY).get_presence(jid); + if (presence == null) return null; + + return presence.show; } public Gee.List? get_full_jids(Jid jid, Account account) { @@ -110,7 +104,7 @@ public class PresenceManager : StreamInteractionModule, Object { resources[jid].add(jid); } } - add_show(account, jid, show); + show_received(jid, account); } private void on_received_unavailable(Account account, Jid jid) { @@ -124,38 +118,5 @@ public class PresenceManager : StreamInteractionModule, Object { } received_offline_presence(jid, account); } - - private void add_show(Account account, Jid jid, string s) { - Show show = new Show(jid, s, new DateTime.now_utc()); - lock (shows) { - if (!shows.has_key(jid)) { - shows[jid] = new HashMap>(); - } - if (!shows[jid].has_key(jid)) { - shows[jid][jid] = new ArrayList(); - } - shows[jid][jid].add(show); - } - show_received(show, jid, account); - } -} - -public class Show : Object { - public const string ONLINE = Xmpp.Presence.Stanza.SHOW_ONLINE; - public const string AWAY = Xmpp.Presence.Stanza.SHOW_AWAY; - public const string CHAT = Xmpp.Presence.Stanza.SHOW_CHAT; - public const string DND = Xmpp.Presence.Stanza.SHOW_DND; - public const string XA = Xmpp.Presence.Stanza.SHOW_XA; - public const string OFFLINE = "offline"; - - public Jid jid; - public string as; - public DateTime datetime; - - public Show(Jid jid, string show, DateTime datetime) { - this.jid = jid; - this.as = show; - this.datetime = datetime; - } } } -- cgit v1.2.3-54-g00ecf