MucOptions.java

  1package eu.siacs.conversations.entities;
  2
  3import android.text.TextUtils;
  4
  5import androidx.annotation.NonNull;
  6import androidx.annotation.Nullable;
  7
  8import java.util.ArrayList;
  9import java.util.Collections;
 10import java.util.HashSet;
 11import java.util.List;
 12import java.util.Locale;
 13import java.util.Set;
 14
 15import io.ipfs.cid.Cid;
 16
 17import eu.siacs.conversations.Config;
 18import eu.siacs.conversations.R;
 19import eu.siacs.conversations.services.AvatarService;
 20import eu.siacs.conversations.services.MessageArchiveService;
 21import eu.siacs.conversations.utils.JidHelper;
 22import eu.siacs.conversations.utils.UIHelper;
 23import eu.siacs.conversations.xmpp.Jid;
 24import eu.siacs.conversations.xmpp.chatstate.ChatState;
 25import eu.siacs.conversations.xmpp.forms.Data;
 26import eu.siacs.conversations.xmpp.forms.Field;
 27import eu.siacs.conversations.xmpp.pep.Avatar;
 28
 29public class MucOptions {
 30
 31    public static final String STATUS_CODE_SELF_PRESENCE = "110";
 32    public static final String STATUS_CODE_ROOM_CREATED = "201";
 33    public static final String STATUS_CODE_BANNED = "301";
 34    public static final String STATUS_CODE_CHANGED_NICK = "303";
 35    public static final String STATUS_CODE_KICKED = "307";
 36    public static final String STATUS_CODE_AFFILIATION_CHANGE = "321";
 37    public static final String STATUS_CODE_LOST_MEMBERSHIP = "322";
 38    public static final String STATUS_CODE_SHUTDOWN = "332";
 39    public static final String STATUS_CODE_TECHNICAL_REASONS = "333";
 40    private final Set<User> users = new HashSet<>();
 41    private final Conversation conversation;
 42    public OnRenameListener onRenameListener = null;
 43    private boolean mAutoPushConfiguration = true;
 44    private final Account account;
 45    private ServiceDiscoveryResult serviceDiscoveryResult;
 46    private boolean isOnline = false;
 47    private Error error = Error.NONE;
 48    private User self;
 49    private String password = null;
 50
 51    private boolean tookProposedNickFromBookmark = false;
 52
 53    public MucOptions(Conversation conversation) {
 54        this.account = conversation.getAccount();
 55        this.conversation = conversation;
 56        this.self = new User(this, createJoinJid(getProposedNick()));
 57        this.self.affiliation = Affiliation.of(conversation.getAttribute("affiliation"));
 58        this.self.role = Role.of(conversation.getAttribute("role"));
 59    }
 60
 61    public Account getAccount() {
 62        return this.conversation.getAccount();
 63    }
 64
 65    public boolean setSelf(User user) {
 66        this.self = user;
 67        final boolean roleChanged = this.conversation.setAttribute("role", user.role.toString());
 68        final boolean affiliationChanged = this.conversation.setAttribute("affiliation", user.affiliation.toString());
 69        return roleChanged || affiliationChanged;
 70    }
 71
 72    public void changeAffiliation(Jid jid, Affiliation affiliation) {
 73        User user = findUserByRealJid(jid);
 74        synchronized (users) {
 75            if (user != null && user.getRole() == Role.NONE) {
 76                users.remove(user);
 77                if (affiliation.ranks(Affiliation.MEMBER)) {
 78                    user.affiliation = affiliation;
 79                    users.add(user);
 80                }
 81            }
 82        }
 83    }
 84
 85    public void flagNoAutoPushConfiguration() {
 86        mAutoPushConfiguration = false;
 87    }
 88
 89    public boolean autoPushConfiguration() {
 90        return mAutoPushConfiguration;
 91    }
 92
 93    public boolean isSelf(Jid counterpart) {
 94        return counterpart.equals(self.getFullJid());
 95    }
 96
 97    public void resetChatState() {
 98        synchronized (users) {
 99            for (User user : users) {
100                user.chatState = Config.DEFAULT_CHAT_STATE;
101            }
102        }
103    }
104
105    public boolean isTookProposedNickFromBookmark() {
106        return tookProposedNickFromBookmark;
107    }
108
109    void notifyOfBookmarkNick(final String nick) {
110        final String normalized = normalize(account.getJid(),nick);
111        if (normalized != null && normalized.equals(getSelf().getFullJid().getResource())) {
112            this.tookProposedNickFromBookmark = true;
113        }
114    }
115
116    public boolean mamSupport() {
117        return MessageArchiveService.Version.has(getFeatures());
118    }
119
120    public boolean updateConfiguration(ServiceDiscoveryResult serviceDiscoveryResult) {
121        this.serviceDiscoveryResult = serviceDiscoveryResult;
122        String name;
123        Field roomConfigName = getRoomInfoForm().getFieldByName("muc#roomconfig_roomname");
124        if (roomConfigName != null) {
125            name = roomConfigName.getValue();
126        } else {
127            List<ServiceDiscoveryResult.Identity> identities = serviceDiscoveryResult.getIdentities();
128            String identityName = identities.size() > 0 ? identities.get(0).getName() : null;
129            final Jid jid = conversation.getJid();
130            if (identityName != null && !identityName.equals(jid == null ? null : jid.getEscapedLocal())) {
131                name = identityName;
132            } else {
133                name = null;
134            }
135        }
136        boolean changed = conversation.setAttribute("muc_name", name);
137        changed |= conversation.setAttribute(Conversation.ATTRIBUTE_MEMBERS_ONLY, this.hasFeature("muc_membersonly"));
138        changed |= conversation.setAttribute(Conversation.ATTRIBUTE_MODERATED, this.hasFeature("muc_moderated"));
139        changed |= conversation.setAttribute(Conversation.ATTRIBUTE_NON_ANONYMOUS, this.hasFeature("muc_nonanonymous"));
140        return changed;
141    }
142
143    private Data getRoomInfoForm() {
144        final List<Data> forms = serviceDiscoveryResult == null ? Collections.emptyList() : serviceDiscoveryResult.forms;
145        return forms.size() == 0 ? new Data() : forms.get(0);
146    }
147
148    public String getAvatar() {
149        return account.getRoster().getContact(conversation.getJid()).getAvatarFilename();
150    }
151
152    public boolean hasFeature(String feature) {
153        return this.serviceDiscoveryResult != null && this.serviceDiscoveryResult.features.contains(feature);
154    }
155
156    public boolean hasVCards() {
157        return hasFeature("vcard-temp");
158    }
159
160    public boolean canInvite() {
161        final boolean hasPermission = !membersOnly() || self.getRole().ranks(Role.MODERATOR) || allowInvites();
162        return hasPermission && online();
163    }
164
165    public boolean allowInvites() {
166        final Field field = getRoomInfoForm().getFieldByName("muc#roomconfig_allowinvites");
167        return field != null && "1".equals(field.getValue());
168    }
169
170    public boolean canChangeSubject() {
171        return self.getRole().ranks(Role.MODERATOR) || participantsCanChangeSubject();
172    }
173
174    public boolean participantsCanChangeSubject() {
175        final Field field = getRoomInfoForm().getFieldByName("muc#roominfo_changesubject");
176        return field != null && "1".equals(field.getValue());
177    }
178
179    public boolean allowPm() {
180        final Field field = getRoomInfoForm().getFieldByName("muc#roomconfig_allowpm");
181        if (field == null) {
182            return true; //fall back if field does not exists
183        }
184        if ("anyone".equals(field.getValue())) {
185            return true;
186        } else if ("participants".equals(field.getValue())) {
187            return self.getRole().ranks(Role.PARTICIPANT);
188        } else if ("moderators".equals(field.getValue())) {
189            return self.getRole().ranks(Role.MODERATOR);
190        } else {
191            return false;
192        }
193    }
194
195    public boolean participating() {
196        return self.getRole().ranks(Role.PARTICIPANT) || !moderated();
197    }
198
199    public boolean membersOnly() {
200        return conversation.getBooleanAttribute(Conversation.ATTRIBUTE_MEMBERS_ONLY, false);
201    }
202
203    public List<String> getFeatures() {
204        return this.serviceDiscoveryResult != null ? this.serviceDiscoveryResult.features : Collections.emptyList();
205    }
206
207    public boolean nonanonymous() {
208        return conversation.getBooleanAttribute(Conversation.ATTRIBUTE_NON_ANONYMOUS, false);
209    }
210
211    public boolean isPrivateAndNonAnonymous() {
212        return membersOnly() && nonanonymous();
213    }
214
215    public boolean moderated() {
216        return conversation.getBooleanAttribute(Conversation.ATTRIBUTE_MODERATED, false);
217    }
218
219    public boolean stableId() {
220        return getFeatures().contains("http://jabber.org/protocol/muc#stable_id");
221    }
222
223    public User deleteUser(Jid jid) {
224        User user = findUserByFullJid(jid);
225        if (user != null) {
226            synchronized (users) {
227                users.remove(user);
228                boolean realJidInMuc = false;
229                for (User u : users) {
230                    if (user.realJid != null && user.realJid.equals(u.realJid)) {
231                        realJidInMuc = true;
232                        break;
233                    }
234                }
235                boolean self = user.realJid != null && user.realJid.equals(account.getJid().asBareJid());
236                if (membersOnly()
237                        && nonanonymous()
238                        && user.affiliation.ranks(Affiliation.MEMBER)
239                        && user.realJid != null
240                        && !realJidInMuc
241                        && !self) {
242                    user.role = Role.NONE;
243                    user.avatar = null;
244                    user.fullJid = null;
245                    users.add(user);
246                }
247            }
248        }
249        return user;
250    }
251
252    //returns true if real jid was new;
253    public boolean updateUser(User user) {
254        User old;
255        boolean realJidFound = false;
256        if (user.fullJid == null && user.realJid != null) {
257            old = findUserByRealJid(user.realJid);
258            realJidFound = old != null;
259            if (old != null) {
260                if (old.fullJid != null) {
261                    return false; //don't add. user already exists
262                } else {
263                    synchronized (users) {
264                        users.remove(old);
265                    }
266                }
267            }
268        } else if (user.realJid != null) {
269            old = findUserByRealJid(user.realJid);
270            realJidFound = old != null;
271            synchronized (users) {
272                if (old != null && (old.fullJid == null || old.role == Role.NONE)) {
273                    users.remove(old);
274                }
275            }
276        }
277        old = findUserByFullJid(user.getFullJid());
278
279        synchronized (this.users) {
280            if (old != null) {
281                users.remove(old);
282            }
283            boolean fullJidIsSelf = isOnline && user.getFullJid() != null && user.getFullJid().equals(self.getFullJid());
284            if ((!membersOnly() || user.getAffiliation().ranks(Affiliation.MEMBER))
285                    && user.getAffiliation().outranks(Affiliation.OUTCAST)
286                    && !fullJidIsSelf) {
287                this.users.add(user);
288                return !realJidFound && user.realJid != null;
289            }
290        }
291        return false;
292    }
293
294    public User findUserByFullJid(Jid jid) {
295        if (jid == null) {
296            return null;
297        }
298        synchronized (users) {
299            for (User user : users) {
300                if (jid.equals(user.getFullJid())) {
301                    return user;
302                }
303            }
304        }
305        return null;
306    }
307
308    public User findUserByRealJid(Jid jid) {
309        if (jid == null) {
310            return null;
311        }
312        synchronized (users) {
313            for (User user : users) {
314                if (jid.equals(user.realJid)) {
315                    return user;
316                }
317            }
318        }
319        return null;
320    }
321
322    public User findOrCreateUserByRealJid(Jid jid, Jid fullJid) {
323        User user = findUserByRealJid(jid);
324        if (user == null) {
325            user = new User(this, fullJid);
326            user.setRealJid(jid);
327        }
328        return user;
329    }
330
331    public User findUser(ReadByMarker readByMarker) {
332        if (readByMarker.getRealJid() != null) {
333            return findOrCreateUserByRealJid(readByMarker.getRealJid().asBareJid(), readByMarker.getFullJid());
334        } else if (readByMarker.getFullJid() != null) {
335            return findUserByFullJid(readByMarker.getFullJid());
336        } else {
337            return null;
338        }
339    }
340
341    public boolean isContactInRoom(Contact contact) {
342        return contact != null && findUserByRealJid(contact.getJid().asBareJid()) != null;
343    }
344
345    public boolean isUserInRoom(Jid jid) {
346        return findUserByFullJid(jid) != null;
347    }
348
349    public boolean setOnline() {
350        boolean before = this.isOnline;
351        this.isOnline = true;
352        return !before;
353    }
354
355    public ArrayList<User> getUsers() {
356        return getUsers(true);
357    }
358
359    public ArrayList<User> getUsers(boolean includeOffline) {
360        synchronized (users) {
361            ArrayList<User> users = new ArrayList<>();
362            for (User user : this.users) {
363                if (!user.isDomain() && (includeOffline || user.getRole().ranks(Role.PARTICIPANT))) {
364                    users.add(user);
365                }
366            }
367            return users;
368        }
369    }
370
371    public ArrayList<User> getUsersWithChatState(ChatState state, int max) {
372        synchronized (users) {
373            ArrayList<User> list = new ArrayList<>();
374            for (User user : users) {
375                if (user.chatState == state) {
376                    list.add(user);
377                    if (list.size() >= max) {
378                        break;
379                    }
380                }
381            }
382            return list;
383        }
384    }
385
386    public List<User> getUsers(int max) {
387        ArrayList<User> subset = new ArrayList<>();
388        HashSet<Jid> jids = new HashSet<>();
389        jids.add(account.getJid().asBareJid());
390        synchronized (users) {
391            for (User user : users) {
392                if (user.getRealJid() == null || (user.getRealJid().getLocal() != null && jids.add(user.getRealJid()))) {
393                    subset.add(user);
394                }
395                if (subset.size() >= max) {
396                    break;
397                }
398            }
399        }
400        return subset;
401    }
402
403    public static List<User> sub(List<User> users, int max) {
404        ArrayList<User> subset = new ArrayList<>();
405        HashSet<Jid> jids = new HashSet<>();
406        for (User user : users) {
407            jids.add(user.getAccount().getJid().asBareJid());
408            if (user.getRealJid() == null || (user.getRealJid().getLocal() != null && jids.add(user.getRealJid()))) {
409                subset.add(user);
410            }
411            if (subset.size() >= max) {
412                break;
413            }
414        }
415        return subset;
416    }
417
418    public int getUserCount() {
419        synchronized (users) {
420            return users.size();
421        }
422    }
423
424    public String getProposedNick() {
425        final Bookmark bookmark = this.conversation.getBookmark();
426        final String bookmarkedNick = normalize(account.getJid(), bookmark == null ? null : bookmark.getNick());
427        if (bookmarkedNick != null) {
428            this.tookProposedNickFromBookmark = true;
429            return bookmarkedNick;
430        } else if (!conversation.getJid().isBareJid()) {
431            return conversation.getJid().getResource();
432        } else {
433            return defaultNick(account);
434        }
435    }
436
437    public static String defaultNick(final Account account) {
438        final String displayName = normalize(account.getJid(), account.getDisplayName());
439        if (displayName == null) {
440            return JidHelper.localPartOrFallback(account.getJid());
441        } else {
442            return displayName;
443        }
444    }
445
446    private static String normalize(Jid account, String nick) {
447        if (account == null || TextUtils.isEmpty(nick)) {
448            return null;
449        }
450        try {
451            return account.withResource(nick).getResource();
452        } catch (IllegalArgumentException e) {
453            return null;
454        }
455
456    }
457
458    public String getActualNick() {
459        if (this.self.getName() != null) {
460            return this.self.getName();
461        } else {
462            return this.getProposedNick();
463        }
464    }
465
466    public boolean online() {
467        return this.isOnline;
468    }
469
470    public Error getError() {
471        return this.error;
472    }
473
474    public void setError(Error error) {
475        this.isOnline = isOnline && error == Error.NONE;
476        this.error = error;
477    }
478
479    public void setOnRenameListener(OnRenameListener listener) {
480        this.onRenameListener = listener;
481    }
482
483    public void setOffline() {
484        synchronized (users) {
485            this.users.clear();
486        }
487        this.error = Error.NO_RESPONSE;
488        this.isOnline = false;
489    }
490
491    public User getSelf() {
492        return self;
493    }
494
495    public boolean setSubject(String subject) {
496        return this.conversation.setAttribute("subject", subject);
497    }
498
499    public String getSubject() {
500        return this.conversation.getAttribute("subject");
501    }
502
503    public String getName() {
504        return this.conversation.getAttribute("muc_name");
505    }
506
507    private List<User> getFallbackUsersFromCryptoTargets() {
508        List<User> users = new ArrayList<>();
509        for (Jid jid : conversation.getAcceptedCryptoTargets()) {
510            User user = new User(this, null);
511            user.setRealJid(jid);
512            users.add(user);
513        }
514        return users;
515    }
516
517    public List<User> getUsersRelevantForNameAndAvatar() {
518        final List<User> users;
519        if (isOnline) {
520            users = getUsers(5);
521        } else {
522            users = getFallbackUsersFromCryptoTargets();
523        }
524        return users;
525    }
526
527    String createNameFromParticipants() {
528        List<User> users = getUsersRelevantForNameAndAvatar();
529        if (users.size() >= 2) {
530            StringBuilder builder = new StringBuilder();
531            for (User user : users) {
532                if (builder.length() != 0) {
533                    builder.append(", ");
534                }
535                String name = UIHelper.getDisplayName(user);
536                if (name != null) {
537                    builder.append(name.split("\\s+")[0]);
538                }
539            }
540            return builder.toString();
541        } else {
542            return null;
543        }
544    }
545
546    public long[] getPgpKeyIds() {
547        List<Long> ids = new ArrayList<>();
548        for (User user : this.users) {
549            if (user.getPgpKeyId() != 0) {
550                ids.add(user.getPgpKeyId());
551            }
552        }
553        ids.add(account.getPgpId());
554        long[] primitiveLongArray = new long[ids.size()];
555        for (int i = 0; i < ids.size(); ++i) {
556            primitiveLongArray[i] = ids.get(i);
557        }
558        return primitiveLongArray;
559    }
560
561    public boolean pgpKeysInUse() {
562        synchronized (users) {
563            for (User user : users) {
564                if (user.getPgpKeyId() != 0) {
565                    return true;
566                }
567            }
568        }
569        return false;
570    }
571
572    public boolean everybodyHasKeys() {
573        synchronized (users) {
574            for (User user : users) {
575                if (user.getPgpKeyId() == 0) {
576                    return false;
577                }
578            }
579        }
580        return true;
581    }
582
583    public Jid createJoinJid(String nick) {
584        try {
585            return conversation.getJid().withResource(nick);
586        } catch (final IllegalArgumentException e) {
587            return null;
588        }
589    }
590
591    public Jid getTrueCounterpart(Jid jid) {
592        if (jid.equals(getSelf().getFullJid())) {
593            return account.getJid().asBareJid();
594        }
595        User user = findUserByFullJid(jid);
596        return user == null ? null : user.realJid;
597    }
598
599    public String getPassword() {
600        this.password = conversation.getAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD);
601        if (this.password == null && conversation.getBookmark() != null
602                && conversation.getBookmark().getPassword() != null) {
603            return conversation.getBookmark().getPassword();
604        } else {
605            return this.password;
606        }
607    }
608
609    public void setPassword(String password) {
610        if (conversation.getBookmark() != null) {
611            conversation.getBookmark().setPassword(password);
612        } else {
613            this.password = password;
614        }
615        conversation.setAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD, password);
616    }
617
618    public Conversation getConversation() {
619        return this.conversation;
620    }
621
622    public List<Jid> getMembers(final boolean includeDomains) {
623        ArrayList<Jid> members = new ArrayList<>();
624        synchronized (users) {
625            for (User user : users) {
626                if (user.affiliation.ranks(Affiliation.MEMBER) && user.realJid != null && !user.realJid.asBareJid().equals(conversation.account.getJid().asBareJid()) && (!user.isDomain() || includeDomains)) {
627                    members.add(user.realJid);
628                }
629            }
630        }
631        return members;
632    }
633
634    public enum Affiliation {
635        OWNER(4, R.string.owner),
636        ADMIN(3, R.string.admin),
637        MEMBER(2, R.string.member),
638        OUTCAST(0, R.string.outcast),
639        NONE(1, R.string.no_affiliation);
640
641        private final int resId;
642        private final int rank;
643
644        Affiliation(int rank, int resId) {
645            this.resId = resId;
646            this.rank = rank;
647        }
648
649        public static Affiliation of(@Nullable String value) {
650            if (value == null) {
651                return NONE;
652            }
653            try {
654                return Affiliation.valueOf(value.toUpperCase(Locale.US));
655            } catch (IllegalArgumentException e) {
656                return NONE;
657            }
658        }
659
660        public int getResId() {
661            return resId;
662        }
663
664        @Override
665        public String toString() {
666            return name().toLowerCase(Locale.US);
667        }
668
669        public boolean outranks(Affiliation affiliation) {
670            return rank > affiliation.rank;
671        }
672
673        public boolean ranks(Affiliation affiliation) {
674            return rank >= affiliation.rank;
675        }
676    }
677
678    public enum Role {
679        MODERATOR(R.string.moderator, 3),
680        VISITOR(R.string.visitor, 1),
681        PARTICIPANT(R.string.participant, 2),
682        NONE(R.string.no_role, 0);
683
684        private final int resId;
685        private final int rank;
686
687        Role(int resId, int rank) {
688            this.resId = resId;
689            this.rank = rank;
690        }
691
692        public static Role of(@Nullable String value) {
693            if (value == null) {
694                return NONE;
695            }
696            try {
697                return Role.valueOf(value.toUpperCase(Locale.US));
698            } catch (IllegalArgumentException e) {
699                return NONE;
700            }
701        }
702
703        public int getResId() {
704            return resId;
705        }
706
707        @Override
708        public String toString() {
709            return name().toLowerCase(Locale.US);
710        }
711
712        public boolean ranks(Role role) {
713            return rank >= role.rank;
714        }
715    }
716
717    public enum Error {
718        NO_RESPONSE,
719        SERVER_NOT_FOUND,
720        REMOTE_SERVER_TIMEOUT,
721        NONE,
722        NICK_IN_USE,
723        PASSWORD_REQUIRED,
724        BANNED,
725        MEMBERS_ONLY,
726        RESOURCE_CONSTRAINT,
727        KICKED,
728        SHUTDOWN,
729        DESTROYED,
730        INVALID_NICK,
731        TECHNICAL_PROBLEMS,
732        UNKNOWN,
733        NON_ANONYMOUS
734    }
735
736    private interface OnEventListener {
737        void onSuccess();
738
739        void onFailure();
740    }
741
742    public interface OnRenameListener extends OnEventListener {
743
744    }
745
746    public static class User implements Comparable<User>, AvatarService.Avatarable {
747        private Role role = Role.NONE;
748        private Affiliation affiliation = Affiliation.NONE;
749        private Jid realJid;
750        private Jid fullJid;
751        private long pgpKeyId = 0;
752        private Avatar avatar;
753        private final MucOptions options;
754        private ChatState chatState = Config.DEFAULT_CHAT_STATE;
755
756        public User(MucOptions options, Jid fullJid) {
757            this.options = options;
758            this.fullJid = fullJid;
759        }
760
761        public String getName() {
762            return fullJid == null ? null : fullJid.getResource();
763        }
764
765        public Role getRole() {
766            return this.role;
767        }
768
769        public void setRole(String role) {
770            this.role = Role.of(role);
771        }
772
773        public Affiliation getAffiliation() {
774            return this.affiliation;
775        }
776
777        public void setAffiliation(String affiliation) {
778            this.affiliation = Affiliation.of(affiliation);
779        }
780
781        public long getPgpKeyId() {
782            if (this.pgpKeyId != 0) {
783                return this.pgpKeyId;
784            } else if (realJid != null) {
785                return getAccount().getRoster().getContact(realJid).getPgpKeyId();
786            } else {
787                return 0;
788            }
789        }
790
791        public void setPgpKeyId(long id) {
792            this.pgpKeyId = id;
793        }
794
795        public Contact getContact() {
796            if (fullJid != null) {
797                return getAccount().getRoster().getContactFromContactList(realJid);
798            } else if (realJid != null) {
799                return getAccount().getRoster().getContact(realJid);
800            } else {
801                return null;
802            }
803        }
804
805        public boolean setAvatar(Avatar avatar) {
806            if (this.avatar != null && this.avatar.equals(avatar)) {
807                return false;
808            } else {
809                this.avatar = avatar;
810                return true;
811            }
812        }
813
814        public String getAvatar() {
815            if (avatar != null) {
816                return avatar.getFilename();
817            }
818            Avatar avatar = realJid != null ? getAccount().getRoster().getContact(realJid).getAvatar() : null;
819            return avatar == null ? null : avatar.getFilename();
820        }
821
822        public Cid getAvatarCid() {
823            if (avatar != null) {
824                return avatar.cid();
825            }
826            Avatar avatar = realJid != null ? getAccount().getRoster().getContact(realJid).getAvatar() : null;
827            return avatar == null ? null : avatar.cid();
828        }
829
830        public Account getAccount() {
831            return options.getAccount();
832        }
833
834        public Conversation getConversation() {
835            return options.getConversation();
836        }
837
838        public Jid getFullJid() {
839            return fullJid;
840        }
841
842        @Override
843        public boolean equals(Object o) {
844            if (this == o) return true;
845            if (o == null || getClass() != o.getClass()) return false;
846
847            User user = (User) o;
848
849            if (role != user.role) return false;
850            if (affiliation != user.affiliation) return false;
851            if (realJid != null ? !realJid.equals(user.realJid) : user.realJid != null)
852                return false;
853            return fullJid != null ? fullJid.equals(user.fullJid) : user.fullJid == null;
854
855        }
856
857        public boolean isDomain() {
858            return realJid != null && realJid.getLocal() == null && role == Role.NONE;
859        }
860
861        @Override
862        public int hashCode() {
863            int result = role != null ? role.hashCode() : 0;
864            result = 31 * result + (affiliation != null ? affiliation.hashCode() : 0);
865            result = 31 * result + (realJid != null ? realJid.hashCode() : 0);
866            result = 31 * result + (fullJid != null ? fullJid.hashCode() : 0);
867            return result;
868        }
869
870        @Override
871        public String toString() {
872            return "[fulljid:" + fullJid + ",realjid:" + realJid + ",affiliation" + affiliation.toString() + "]";
873        }
874
875        public boolean realJidMatchesAccount() {
876            return realJid != null && realJid.equals(options.account.getJid().asBareJid());
877        }
878
879        @Override
880        public int compareTo(@NonNull User another) {
881            if (another.getAffiliation().outranks(getAffiliation())) {
882                return 1;
883            } else if (getAffiliation().outranks(another.getAffiliation())) {
884                return -1;
885            } else {
886                return getComparableName().compareToIgnoreCase(another.getComparableName());
887            }
888        }
889
890        public String getComparableName() {
891            Contact contact = getContact();
892            if (contact != null) {
893                return contact.getDisplayName();
894            } else {
895                String name = getName();
896                return name == null ? "" : name;
897            }
898        }
899
900        public Jid getRealJid() {
901            return realJid;
902        }
903
904        public void setRealJid(Jid jid) {
905            this.realJid = jid != null ? jid.asBareJid() : null;
906        }
907
908        public boolean setChatState(ChatState chatState) {
909            if (this.chatState == chatState) {
910                return false;
911            }
912            this.chatState = chatState;
913            return true;
914        }
915
916        @Override
917        public int getAvatarBackgroundColor() {
918            final String seed = realJid != null ? realJid.asBareJid().toString() : null;
919            return UIHelper.getColorForName(seed == null ? getName() : seed);
920        }
921
922        @Override
923        public String getAvatarName() {
924            return getConversation().getName().toString();
925        }
926    }
927}