aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/entities/ReadByMarker.java
blob: 3162c26c656b826e6ffb18f2ac08ccee73f8acf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package de.pixart.messenger.entities;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import de.pixart.messenger.xmpp.jid.InvalidJidException;
import de.pixart.messenger.xmpp.jid.Jid;

public class ReadByMarker {

    private ReadByMarker() {

    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ReadByMarker marker = (ReadByMarker) o;

        if (fullJid != null ? !fullJid.equals(marker.fullJid) : marker.fullJid != null)
            return false;
        return realJid != null ? realJid.equals(marker.realJid) : marker.realJid == null;

    }

    @Override
    public int hashCode() {
        int result = fullJid != null ? fullJid.hashCode() : 0;
        result = 31 * result + (realJid != null ? realJid.hashCode() : 0);
        return result;
    }

    private Jid fullJid;
    private Jid realJid;

    public Jid getFullJid() {
        return fullJid;
    }

    public Jid getRealJid() {
        return realJid;
    }

    public JSONObject toJson() {
        JSONObject jsonObject = new JSONObject();
        if (fullJid != null) {
            try {
                jsonObject.put("fullJid", fullJid.toPreppedString());
            } catch (JSONException e) {
                //ignore
            }
        }
        if (realJid != null) {
            try {
                jsonObject.put("realJid", realJid.toPreppedString());
            } catch (JSONException e) {
                //ignore
            }
        }
        return jsonObject;
    }

    public static Set<ReadByMarker> fromJson(JSONArray jsonArray) {
        HashSet<ReadByMarker> readByMarkers = new HashSet<>();
        for (int i = 0; i < jsonArray.length(); ++i) {
            try {
                readByMarkers.add(fromJson(jsonArray.getJSONObject(i)));
            } catch (JSONException e) {
                //ignored
            }
        }
        return readByMarkers;
    }

    public static ReadByMarker from(Jid fullJid, Jid realJid) {
        final ReadByMarker marker = new ReadByMarker();
        marker.fullJid = fullJid;
        marker.realJid = realJid == null ? null : realJid.toBareJid();
        return marker;
    }

    public static ReadByMarker from(Message message) {
        final ReadByMarker marker = new ReadByMarker();
        marker.fullJid = message.getCounterpart();
        marker.realJid = message.getTrueCounterpart();
        return marker;
    }

    public static ReadByMarker from(MucOptions.User user) {
        final ReadByMarker marker = new ReadByMarker();
        marker.fullJid = user.getFullJid();
        marker.realJid = user.getRealJid();
        return marker;
    }

    public static Set<ReadByMarker> from(Collection<MucOptions.User> users) {
        final HashSet<ReadByMarker> markers = new HashSet<>();
        for (MucOptions.User user : users) {
            markers.add(from(user));
        }
        return markers;
    }

    public static ReadByMarker fromJson(JSONObject jsonObject) {
        ReadByMarker marker = new ReadByMarker();
        try {
            marker.fullJid = Jid.fromString(jsonObject.getString("fullJid"), true);
        } catch (JSONException | InvalidJidException e) {
            marker.fullJid = null;
        }
        try {
            marker.realJid = Jid.fromString(jsonObject.getString("realJid"), true);
        } catch (JSONException | InvalidJidException e) {
            marker.realJid = null;
        }
        return marker;
    }

    public static Set<ReadByMarker> fromJsonString(String json) {
        try {
            return fromJson(new JSONArray(json));
        } catch (JSONException | NullPointerException e) {
            return new HashSet<>();
        }
    }

    public static JSONArray toJson(Set<ReadByMarker> readByMarkers) {
        JSONArray jsonArray = new JSONArray();
        for (ReadByMarker marker : readByMarkers) {
            jsonArray.put(marker.toJson());
        }
        return jsonArray;
    }

    public static boolean contains(ReadByMarker needle, Set<ReadByMarker> readByMarkers) {
        for(ReadByMarker marker : readByMarkers) {
            if (marker.realJid != null && needle.realJid != null) {
                if (marker.realJid.toBareJid().equals(needle.realJid.toBareJid())) {
                    return true;
                }
            } else if (marker.fullJid != null && needle.fullJid != null) {
                if (marker.fullJid.equals(needle.fullJid)) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean allUsersRepresented(Collection<MucOptions.User> users, Set<ReadByMarker> markers) {
        for(MucOptions.User user : users) {
            if (!contains(from(user),markers)) {
                return false;
            }
        }
        return true;
    }

}