From 182195e8e0b278e5fc32e48280b3f33ab4dc3ecd Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Mon, 9 Jul 2018 22:33:14 +0200 Subject: store own affiliation and role to disk --- .../de/pixart/messenger/entities/MucOptions.java | 109 +++++++++------------ .../de/pixart/messenger/generator/IqGenerator.java | 2 +- .../de/pixart/messenger/parser/PresenceParser.java | 5 +- 3 files changed, 52 insertions(+), 64 deletions(-) (limited to 'src') diff --git a/src/main/java/de/pixart/messenger/entities/MucOptions.java b/src/main/java/de/pixart/messenger/entities/MucOptions.java index 1b60a8c97..cf12a9533 100644 --- a/src/main/java/de/pixart/messenger/entities/MucOptions.java +++ b/src/main/java/de/pixart/messenger/entities/MucOptions.java @@ -2,11 +2,13 @@ package de.pixart.messenger.entities; import android.annotation.SuppressLint; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; +import java.util.Locale; import java.util.Set; import de.pixart.messenger.Config; @@ -29,8 +31,11 @@ public class MucOptions { return this.conversation.getAccount(); } - public void setSelf(User user) { + public boolean setSelf(User user) { this.self = user; + final boolean roleChanged = this.conversation.setAttribute("role", user.role.toString()); + final boolean affiliationChanged = this.conversation.setAttribute("affiliation", user.affiliation.toString()); + return roleChanged || affiliationChanged; } public void changeAffiliation(Jid jid, Affiliation affiliation) { @@ -71,19 +76,17 @@ public class MucOptions { } public enum Affiliation { - OWNER("owner", 4, R.string.owner), - ADMIN("admin", 3, R.string.admin), - MEMBER("member", 2, R.string.member), - OUTCAST("outcast", 0, R.string.outcast), - NONE("none", 1, R.string.no_affiliation); - - Affiliation(String string, int rank, int resId) { - this.string = string; + OWNER(4, R.string.owner), + ADMIN(3, R.string.admin), + MEMBER(2, R.string.member), + OUTCAST(0, R.string.outcast), + NONE(1, R.string.no_affiliation); + + Affiliation(int rank, int resId) { this.resId = resId; this.rank = rank; } - private String string; private int resId; private int rank; @@ -93,7 +96,7 @@ public class MucOptions { @Override public String toString() { - return this.string; + return name().toLowerCase(Locale.US); } public boolean outranks(Affiliation affiliation) { @@ -103,21 +106,30 @@ public class MucOptions { public boolean ranks(Affiliation affiliation) { return rank >= affiliation.rank; } + + public static Affiliation of(@Nullable String value) { + if (value == null) { + return NONE; + } + try { + return Affiliation.valueOf(value.toUpperCase(Locale.US)); + } catch (IllegalArgumentException e) { + return NONE; + } + } } public enum Role { - MODERATOR("moderator", R.string.moderator, 3), - VISITOR("visitor", R.string.visitor, 1), - PARTICIPANT("participant", R.string.participant, 2), - NONE("none", R.string.no_role, 0); + MODERATOR(R.string.moderator, 3), + VISITOR(R.string.visitor, 1), + PARTICIPANT(R.string.participant, 2), + NONE(R.string.no_role, 0); - Role(String string, int resId, int rank) { - this.string = string; + Role(int resId, int rank) { this.resId = resId; this.rank = rank; } - private String string; private int resId; private int rank; @@ -127,12 +139,23 @@ public class MucOptions { @Override public String toString() { - return this.string; + return name().toLowerCase(Locale.US); } public boolean ranks(Role role) { return rank >= role.rank; } + + public static Role of(@Nullable String value) { + if (value == null) { + return NONE; + } + try { + return Role.valueOf(value.toUpperCase(Locale.US)); + } catch (IllegalArgumentException e) { + return NONE; + } + } } public enum Error { @@ -197,25 +220,7 @@ public class MucOptions { } public void setRole(String role) { - if (role == null) { - this.role = Role.NONE; - return; - } - role = role.toLowerCase(); - switch (role) { - case "moderator": - this.role = Role.MODERATOR; - break; - case "participant": - this.role = Role.PARTICIPANT; - break; - case "visitor": - this.role = Role.VISITOR; - break; - default: - this.role = Role.NONE; - break; - } + this.role = Role.of(role); } public Affiliation getAffiliation() { @@ -223,27 +228,7 @@ public class MucOptions { } public void setAffiliation(String affiliation) { - if (affiliation == null) { - this.affiliation = Affiliation.NONE; - return; - } - affiliation = affiliation.toLowerCase(); - switch (affiliation) { - case "admin": - this.affiliation = Affiliation.ADMIN; - break; - case "owner": - this.affiliation = Affiliation.OWNER; - break; - case "member": - this.affiliation = Affiliation.MEMBER; - break; - case "outcast": - this.affiliation = Affiliation.OUTCAST; - break; - default: - this.affiliation = Affiliation.NONE; - } + this.affiliation = Affiliation.of(affiliation); } public void setPgpKeyId(long id) { @@ -381,6 +366,8 @@ public class MucOptions { this.account = conversation.getAccount(); this.conversation = conversation; this.self = new User(this, createJoinJid(getProposedNick())); + this.self.affiliation = Affiliation.of(conversation.getAttribute("affiliation")); + this.self.role = Role.of(conversation.getAttribute("role")); } public boolean updateConfiguration(ServiceDiscoveryResult serviceDiscoveryResult) { @@ -450,9 +437,7 @@ public class MucOptions { } public boolean participating() { - return !online() - || self.getRole().ranks(Role.PARTICIPANT) - || hasFeature("muc_unmoderated"); + return self.getRole().ranks(Role.PARTICIPANT) || !moderated(); } public boolean membersOnly() { diff --git a/src/main/java/de/pixart/messenger/generator/IqGenerator.java b/src/main/java/de/pixart/messenger/generator/IqGenerator.java index 27d1592ae..918c95e6f 100644 --- a/src/main/java/de/pixart/messenger/generator/IqGenerator.java +++ b/src/main/java/de/pixart/messenger/generator/IqGenerator.java @@ -325,7 +325,7 @@ public class IqGenerator extends AbstractGenerator { Element query = packet.query("http://jabber.org/protocol/muc#admin"); for (Jid jid : jids) { Element item = query.addChild("item"); - item.setAttribute("jid", jid.toString()); + item.setAttribute("jid", jid.toEscapedString()); item.setAttribute("affiliation", affiliation); } return packet; diff --git a/src/main/java/de/pixart/messenger/parser/PresenceParser.java b/src/main/java/de/pixart/messenger/parser/PresenceParser.java index 3b6b83fd4..4091e34cd 100644 --- a/src/main/java/de/pixart/messenger/parser/PresenceParser.java +++ b/src/main/java/de/pixart/messenger/parser/PresenceParser.java @@ -71,7 +71,10 @@ public class PresenceParser extends AbstractParser implements if (mucOptions.setOnline()) { mXmppConnectionService.getAvatarService().clear(mucOptions); } - mucOptions.setSelf(user); + if (mucOptions.setSelf(user)) { + Log.d(Config.LOGTAG, "role or affiliation changed"); + mXmppConnectionService.databaseBackend.updateConversation(conversation); + } mXmppConnectionService.persistSelfNick(user); invokeRenameListener(mucOptions, true); } -- cgit v1.2.3