-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.java
More file actions
131 lines (117 loc) · 5.39 KB
/
Message.java
File metadata and controls
131 lines (117 loc) · 5.39 KB
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
package com.etiaro.facebook;
import android.support.annotation.NonNull;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
/**
* Created by jakub on 21.03.18.
*/
public class Message implements Comparator<Message>, Comparable<Message>{
public String text, senderID, __typename, message_id, offline_threading_id, conversation_id, sender_email;
public int ttl;
public Long timestamp_precise;
public boolean sent = false, delivered = false, unread, is_sponsored; //Delivered and sent are supported not by counstructor
public ArrayList<Attachment> attachments = new ArrayList<>();
public Message(JSONObject json) throws JSONException {
update(json);
}
public void update(JSONObject json) throws JSONException {
if(json.has("irisSeqId")){
text = json.has("body") ?
json.getString("body") : "";
senderID = json.getJSONObject("messageMetadata").getString("actorFbId");
message_id = json.getJSONObject("messageMetadata").getString("messageId");
offline_threading_id = json.getJSONObject("messageMetadata").getString("offlineThreadingId");
if(json.getJSONObject("messageMetadata").getJSONObject("threadKey").has("threadFbId"))
conversation_id = json.getJSONObject("messageMetadata").getJSONObject("threadKey").getString("threadFbId");
else if(json.getJSONObject("messageMetadata").getJSONObject("threadKey").has("otherUserFbId"))
conversation_id = json.getJSONObject("messageMetadata").getJSONObject("threadKey").getString("otherUserFbId");
timestamp_precise = Long.valueOf(json.getJSONObject("messageMetadata").getString("timestamp"));
unread = true;
return;
}
if(!json.has("message_id")) {
text = json.getString("snippet");
senderID = json.getJSONObject("message_sender").getJSONObject("messaging_actor").getString("id");
}else{
if(json.has("__typename"))
__typename = json.getString("__typename");
message_id = json.getString("message_id");
offline_threading_id = json.getString("offline_threading_id"); //???
senderID = json.getJSONObject("message_sender").getString("id");
if(json.getJSONObject("message_sender").has("email"))
sender_email = json.getJSONObject("message_sender").getString("email");
if(json.has("ttl"))
ttl = json.getInt("ttl");
unread = json.getBoolean("unread");
if(json.has("is_sponsored"))
is_sponsored = json.getBoolean("is_sponsored");
if(json.has("message") && json.getJSONObject("message").has("text"))
text = json.getJSONObject("message").getString("text");
}
if(json.has("blob_attachments") && json.getJSONArray("blob_attachments").length() > 0) {
for(int i = 0; i < json.getJSONArray("blob_attachments").length(); i++){
attachments.add(new Attachment(json.getJSONArray("blob_attachments").getJSONObject(i)));
}
}
if(json.has("sent") && json.has("delivered")) {
sent = json.getBoolean("sent");
delivered = json.getBoolean("delivered");
}
timestamp_precise = Long.valueOf(json.getString("timestamp_precise"));
}
public JSONObject toJSON(){
JSONObject obj = null;
try {
obj = new JSONObject().put("message", new JSONObject().put("text", text)).put("senderID", senderID)
.put("__typename", __typename).put("message_sender", new JSONObject()
.put("email", sender_email).put("id", senderID)
.put("messaging_actor", new JSONObject()
.put("id", senderID)))
.put("message_id", message_id)
.put("offline_threading_id", offline_threading_id).put("ttl", ttl)
.put("sent", sent).put("delivered", delivered).put("unread", unread).put("is_sponsored", is_sponsored)
.put("timestamp_precise", timestamp_precise).put("snippet", text)
.put("messageMetadata", new JSONObject().put("threadKey", new JSONObject().put("threadFbId", conversation_id)));
JSONArray arr = new JSONArray();
Iterator<Attachment> it = attachments.iterator();
while(it.hasNext())
arr.put(it.next().toJSON());
obj.put("blob_attachments", arr);
} catch (JSONException e) {
e.printStackTrace();
}
return obj;
}
public String toString(){
return toJSON().toString();
}
@Override
public int compare(Message m1, Message m2) {
return (int)(m1.timestamp_precise - m2.timestamp_precise);
}
@Override
public int compareTo(@NonNull Message msg) {
return (int)(this.timestamp_precise - msg.timestamp_precise);
}
}
/*{
"commerce_message_type":null,
"customizations":[],
"tags_list":[
"source:messenger:web",
"hot_emoji_size:small",
"inbox"
],
"platform_xmd_encoded":null,
"message_source_data":null,
"montage_reply_data":null,
TODO "message_reactions":[], <-------
"message":{"ranges":[]},
"extensible_attachment":null,
"sticker":null,
}*/