store own affiliation and role to disk

This commit is contained in:
Daniel Gultsch 2018-07-08 20:52:55 +02:00
parent 3014b7f857
commit 7b64a031cd
3 changed files with 51 additions and 66 deletions

View File

@ -1,12 +1,13 @@
package eu.siacs.conversations.entities; package eu.siacs.conversations.entities;
import android.annotation.SuppressLint;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Set; import java.util.Set;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
@ -14,14 +15,12 @@ import eu.siacs.conversations.R;
import eu.siacs.conversations.services.MessageArchiveService; import eu.siacs.conversations.services.MessageArchiveService;
import eu.siacs.conversations.utils.JidHelper; import eu.siacs.conversations.utils.JidHelper;
import eu.siacs.conversations.utils.UIHelper; import eu.siacs.conversations.utils.UIHelper;
import eu.siacs.conversations.xml.Namespace;
import eu.siacs.conversations.xmpp.chatstate.ChatState; import eu.siacs.conversations.xmpp.chatstate.ChatState;
import eu.siacs.conversations.xmpp.forms.Data; import eu.siacs.conversations.xmpp.forms.Data;
import eu.siacs.conversations.xmpp.forms.Field; import eu.siacs.conversations.xmpp.forms.Field;
import eu.siacs.conversations.xmpp.pep.Avatar; import eu.siacs.conversations.xmpp.pep.Avatar;
import rocks.xmpp.addr.Jid; import rocks.xmpp.addr.Jid;
@SuppressLint("DefaultLocale")
public class MucOptions { public class MucOptions {
private boolean mAutoPushConfiguration = true; private boolean mAutoPushConfiguration = true;
@ -30,8 +29,11 @@ public class MucOptions {
return this.conversation.getAccount(); return this.conversation.getAccount();
} }
public void setSelf(User user) { public boolean setSelf(User user) {
this.self = 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) { public void changeAffiliation(Jid jid, Affiliation affiliation) {
@ -72,19 +74,17 @@ public class MucOptions {
} }
public enum Affiliation { public enum Affiliation {
OWNER("owner", 4, R.string.owner), OWNER(4, R.string.owner),
ADMIN("admin", 3, R.string.admin), ADMIN(3, R.string.admin),
MEMBER("member", 2, R.string.member), MEMBER(2, R.string.member),
OUTCAST("outcast", 0, R.string.outcast), OUTCAST(0, R.string.outcast),
NONE("none", 1, R.string.no_affiliation); NONE(1, R.string.no_affiliation);
Affiliation(String string, int rank, int resId) { Affiliation(int rank, int resId) {
this.string = string;
this.resId = resId; this.resId = resId;
this.rank = rank; this.rank = rank;
} }
private String string;
private int resId; private int resId;
private int rank; private int rank;
@ -94,7 +94,7 @@ public class MucOptions {
@Override @Override
public String toString() { public String toString() {
return this.string; return name().toLowerCase(Locale.US);
} }
public boolean outranks(Affiliation affiliation) { public boolean outranks(Affiliation affiliation) {
@ -104,21 +104,30 @@ public class MucOptions {
public boolean ranks(Affiliation affiliation) { public boolean ranks(Affiliation affiliation) {
return rank >= affiliation.rank; 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 { public enum Role {
MODERATOR("moderator", R.string.moderator, 3), MODERATOR(R.string.moderator, 3),
VISITOR("visitor", R.string.visitor, 1), VISITOR(R.string.visitor, 1),
PARTICIPANT("participant", R.string.participant, 2), PARTICIPANT(R.string.participant, 2),
NONE("none", R.string.no_role, 0); NONE(R.string.no_role, 0);
Role(String string, int resId, int rank) { Role(int resId, int rank) {
this.string = string;
this.resId = resId; this.resId = resId;
this.rank = rank; this.rank = rank;
} }
private String string;
private int resId; private int resId;
private int rank; private int rank;
@ -128,12 +137,23 @@ public class MucOptions {
@Override @Override
public String toString() { public String toString() {
return this.string; return name().toLowerCase(Locale.US);
} }
public boolean ranks(Role role) { public boolean ranks(Role role) {
return rank >= role.rank; 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 { public enum Error {
@ -198,25 +218,7 @@ public class MucOptions {
} }
public void setRole(String role) { public void setRole(String role) {
if (role == null) { this.role = Role.of(role);
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;
}
} }
public Affiliation getAffiliation() { public Affiliation getAffiliation() {
@ -224,27 +226,7 @@ public class MucOptions {
} }
public void setAffiliation(String affiliation) { public void setAffiliation(String affiliation) {
if (affiliation == null) { this.affiliation = Affiliation.of(affiliation);
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;
}
} }
public void setPgpKeyId(long id) { public void setPgpKeyId(long id) {
@ -382,6 +364,8 @@ public class MucOptions {
this.account = conversation.getAccount(); this.account = conversation.getAccount();
this.conversation = conversation; this.conversation = conversation;
this.self = new User(this, createJoinJid(getProposedNick())); 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) { public boolean updateConfiguration(ServiceDiscoveryResult serviceDiscoveryResult) {
@ -452,9 +436,7 @@ public class MucOptions {
} }
public boolean participating() { public boolean participating() {
return !online() return self.getRole().ranks(Role.PARTICIPANT) || !moderated();
|| self.getRole().ranks(Role.PARTICIPANT)
|| hasFeature("muc_unmoderated");
} }
public boolean membersOnly() { public boolean membersOnly() {

View File

@ -325,7 +325,7 @@ public class IqGenerator extends AbstractGenerator {
Element query = packet.query("http://jabber.org/protocol/muc#admin"); Element query = packet.query("http://jabber.org/protocol/muc#admin");
for (Jid jid : jids) { for (Jid jid : jids) {
Element item = query.addChild("item"); Element item = query.addChild("item");
item.setAttribute("jid", jid.toString()); item.setAttribute("jid", jid.toEscapedString());
item.setAttribute("affiliation", affiliation); item.setAttribute("affiliation", affiliation);
} }
return packet; return packet;

View File

@ -74,7 +74,10 @@ public class PresenceParser extends AbstractParser implements
if (mucOptions.setOnline()) { if (mucOptions.setOnline()) {
mXmppConnectionService.getAvatarService().clear(mucOptions); 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); mXmppConnectionService.persistSelfNick(user);
invokeRenameListener(mucOptions, true); invokeRenameListener(mucOptions, true);