aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/entities/Edit.java
blob: ab96949a0a6c9390bc8cb15a19cb4f8248b10cf5 (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
package de.pixart.messenger.entities;

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

import java.util.ArrayList;
import java.util.List;

public class Edit {

    private final String editedId;
    private final String serverMsgId;

    Edit(String editedId, String serverMsgId) {
        this.editedId = editedId;
        this.serverMsgId = serverMsgId;
    }

    static String toJson(List<Edit> edits) throws JSONException {
        JSONArray jsonArray = new JSONArray();
        for (Edit edit : edits) {
            jsonArray.put(edit.toJson());
        }
        return jsonArray.toString();
    }

    static boolean wasPreviouslyEditedRemoteMsgId(List<Edit> edits, String remoteMsgId) {
        for (Edit edit : edits) {
            if (edit.editedId != null && edit.editedId.equals(remoteMsgId)) {
                return true;
            }
        }
        return false;
    }

    static boolean wasPreviouslyEditedServerMsgId(List<Edit> edits, String serverMsgId) {
        for (Edit edit : edits) {
            if (edit.serverMsgId != null && edit.serverMsgId.equals(serverMsgId)) {
                return true;
            }
        }
        return false;
    }

    private static Edit fromJson(JSONObject jsonObject) throws JSONException {
        String edited = jsonObject.has("edited_id") ? jsonObject.getString("edited_id") : null;
        String serverMsgId = jsonObject.has("server_msg_id") ? jsonObject.getString("server_msg_id") : null;
        return new Edit(edited, serverMsgId);
    }

    static List<Edit> fromJson(String input) {
        final ArrayList<Edit> list = new ArrayList<>();
        if (input == null) {
            return list;
        }
        try {
            final JSONArray jsonArray = new JSONArray(input);
            for (int i = 0; i < jsonArray.length(); ++i) {
                list.add(fromJson(jsonArray.getJSONObject(i)));
            }
            return list;
        } catch (JSONException e) {
            return list;
        }
    }

    private JSONObject toJson() throws JSONException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("edited_id", editedId);
        jsonObject.put("server_msg_id", serverMsgId);
        return jsonObject;
    }

    String getEditedId() {
        return editedId;
    }

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

        Edit edit = (Edit) o;

        if (editedId != null ? !editedId.equals(edit.editedId) : edit.editedId != null)
            return false;
        return serverMsgId != null ? serverMsgId.equals(edit.serverMsgId) : edit.serverMsgId == null;
    }

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