-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMessage.cs
267 lines (230 loc) · 8.47 KB
/
Message.cs
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
using System;
using System.Collections;
using System.Collections.Generic;
namespace Cisco.Spark
{
/// <summary>
/// A single message in a group Room or directly to a Person.
/// </summary>
public class Message : SparkObject
{
/// <summary>
/// SparkType the implementation represents.
/// </summary>
internal override SparkType SparkType
{
get { return SparkType.Message; }
}
/// <summary>
/// The Room the message belongs to if it is a general message.
/// </summary>
public Room Room { get; set; }
/// <summary>
/// The recipient if the message is a 1:1 room / direct message.
/// </summary>
public Person Recipient { get; set; }
/// <summary>
/// The author of the message.
/// </summary>
public Person Author { get; set; }
/// <summary>
/// The plain text of the message.
/// </summary>
public string Text { get; set; }
/// <summary>
/// The message in Markdown form.
/// </summary>
public string Markdown { get; set; }
/// <summary>
/// The message in HTML form.
/// </summary>
public string Html { get; set; }
/// <summary>
/// List of uploaded files attached with the message.
/// </summary>
public List<SparkFile> Files { get; set; }
/// <summary>
/// List of people mentioned in the message.
/// </summary>
public List<Person> Mentions { get; set; }
/// <summary>
/// Creates a representation of existing Spark-side Message.
/// Use <see cref="Load"/> to populate rest of properties from Spark.
/// </summary>
/// <param name="id">Spark UID of the Message.</param>
public static Message FromId(string id)
{
return (Message)SparkObjectFactory.Make(id, SparkType.Message);
}
public Message() { }
/// <summary>
/// Creates a new Message posted into a given Room.
/// </summary>
/// <param name="room">The Room to post the Message into.</param>
public Message(Room room)
{
Room = room;
Author = Person.AuthenticatedUser;
}
/// <summary>
/// Creates a new Message sent to a given Person via a 1:1 / Direct Room.
/// </summary>
/// <param name="person">The Person to send the Message to.</param>
public Message(Person person)
{
Recipient = person;
Author = Person.AuthenticatedUser;
}
protected override Dictionary<string, object> ToDict(List<string> fields)
{
var data = base.ToDict();
// Destination.
if (Room != null)
{
data["roomId"] = Room.Id;
}
else if (Recipient != null)
{
data["toPersonId"] = Recipient.Id;
}
else
{
throw new Exception("Message must have a destination");
}
// Author.
data["personId"] = Author.Id;
// Message Content.
// Content check.
if ((Files == null || Files.Count == 0) && (Text == null || Text == "") && (Markdown == null || Markdown == "") && (Html == null || Html == null))
{
throw new Exception("A message must have content (text and/or files)");
}
data["text"] = Text;
data["markdown"] = Markdown;
// Uploaded Files.
if (Files != null && Files.Count > 0)
{
var fileUrls = new List<string>();
foreach (var file in Files)
{
fileUrls.Add(file.UploadUrl.AbsoluteUri);
}
data["files"] = fileUrls;
}
// Mentions.
if (Mentions != null && Mentions.Count > 0)
{
var personIds = new List<string>();
foreach (var person in Mentions)
{
personIds.Add(person.Id);
}
data["mentionedPeople"] = personIds;
}
return CleanDict(data, fields);
}
protected override void LoadDict(Dictionary<string, object> data)
{
base.LoadDict(data);
// Destination.
if (data.ContainsKey("roomId"))
{
Room = Room.FromId(data["roomId"] as string);
}
else if (data.ContainsKey("toPersonId"))
{
Recipient = Person.FromId(data["toPersonId"] as string);
}
// Author.
Author = Person.FromId(data["personId"] as string);
// Message Content (can be blank when uploading files).
object text;
if (data.TryGetValue("text", out text))
{
Text = text as string;
}
object markdown;
if (data.TryGetValue("markdown", out markdown))
{
Markdown = markdown as string;
}
object html;
if (data.TryGetValue("html", out html))
{
Html = html as string;
}
// Uploaded Files.
object files;
if (data.TryGetValue("files", out files))
{
var extractedFiles = files as List<object>;
var tempFiles = new List<SparkFile>();
foreach (var urlString in extractedFiles)
{
var url = new Uri(urlString as string);
tempFiles.Add(new SparkFile(url));
}
Files = tempFiles;
}
// Mentions
object mentions;
if (data.TryGetValue("mentionedPeople", out mentions))
{
var extractedIds = mentions as List<object>;
var tempPeople = new List<Person>();
foreach (var id in extractedIds)
{
var stringId = id as string;
tempPeople.Add(Person.FromId(stringId));
}
Mentions = tempPeople;
}
}
/// <summary>
/// Lists all Message matching given criteria.
/// </summary>
/// <param name="room">The room to pull from.</param>
/// <param name="error">Error from Spark, if any.</param>
/// <param name="results">Callback for the list of messages.</param>
/// <param name="mentionedPeople">Will only return messages where these people are mentioned.</param>
/// <param name="before">Will only return messages sent before this DateTime.</param>
/// <param name="beforeMessage">Will only return messages from before this Message.</param>
/// <param name="max">Maximum number of messages to return.</param>
public static IEnumerator ListMessages(Room room, Action<SparkMessage> error, Action<List<Message>> results, List<Person> mentionedPeople = null, DateTime? before = null, Message beforeMessage = null, int max = 0)
{
// Room must exist on Spark.
if (room.Id == null)
{
throw new Exception("Must pass a valid Room to ListMessages");
}
// Search constraints.
var constraints = new Dictionary<string, string>();
constraints["roomId"] = room.Id;
if (mentionedPeople != null)
{
var serialisedString = new List<string>();
foreach (var person in mentionedPeople)
{
serialisedString.Add(person.Id);
}
var queryString = Json.Serialize(serialisedString);
constraints["mentionedPeople"] = queryString;
}
if (before != null)
{
constraints["before"] = before.ToString();
}
if (beforeMessage != null)
{
constraints["beforeMessage"] = beforeMessage.Id;
}
if (max > 0)
{
constraints["max"] = max.ToString();
}
// Make request.
var listObjects = ListObjects(constraints, SparkType.Message, error, results);
yield return Request.Instance.StartCoroutine(listObjects);
}
}
}