Skip to content

Commit 9595861

Browse files
committed
insert special messages for group updates, session resets and disappearing messages updates
fixes #55
1 parent a213718 commit 9595861

File tree

1 file changed

+157
-20
lines changed

1 file changed

+157
-20
lines changed

Signal-Windows/Signal/IncomingMessages.cs

Lines changed: 157 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,33 +86,21 @@ private void HandleMessage(SignalServiceEnvelope envelope)
8686
SignalServiceDataMessage message = content.Message;
8787
if (message.EndSession)
8888
{
89-
LibsignalDBContext.DeleteAllSessions(envelope.getSource());
89+
HandleSessionResetMessage(envelope, content, message, false, timestamp);
9090
}
9191
else if (message.IsGroupUpdate())
9292
{
9393
if (message.Group.Type == SignalServiceGroup.GroupType.UPDATE)
9494
{
95-
HandleGroupUpdateMessage(envelope, content, message, timestamp);
95+
HandleGroupUpdateMessage(envelope, content, message, false, timestamp);
9696
}
9797
}
9898
else if (message.ExpirationUpdate)
9999
{
100-
string threadid;
101-
if (message.Group != null)
102-
{
103-
threadid = Base64.encodeBytes(message.Group.GroupId);
104-
SignalDBContext.GetOrCreateGroupLocked(threadid, timestamp, this);
105-
}
106-
else
107-
{
108-
threadid = envelope.getSource();
109-
SignalDBContext.GetOrCreateContactLocked(threadid, timestamp, this);
110-
}
111-
SignalDBContext.UpdateExpiresInLocked(new SignalConversation() { ThreadId = threadid }, (uint)message.ExpiresInSeconds);
100+
HandleExpirationUpdateMessage(envelope, content, message, false, timestamp);
112101
}
113102
else
114103
{
115-
//TODO check both the db for duplicates
116104
HandleSignalMessage(envelope, content, message, false, timestamp);
117105
}
118106
}
@@ -122,14 +110,22 @@ private void HandleMessage(SignalServiceEnvelope envelope)
122110
{
123111
var syncMessage = content.SynchronizeMessage.getSent().ForceGetValue();
124112
var dataMessage = syncMessage.getMessage();
125-
//TODO check both the db for duplicates
126-
if (dataMessage.IsGroupUpdate())
113+
114+
if (dataMessage.EndSession)
115+
{
116+
HandleSessionResetMessage(envelope, content, dataMessage, true, timestamp);
117+
}
118+
else if (dataMessage.IsGroupUpdate())
127119
{
128120
if (dataMessage.Group.Type == SignalServiceGroup.GroupType.UPDATE)
129121
{
130-
HandleGroupUpdateMessage(envelope, content, dataMessage, timestamp);
122+
HandleGroupUpdateMessage(envelope, content, dataMessage, true, timestamp);
131123
}
132124
}
125+
else if (dataMessage.ExpirationUpdate)
126+
{
127+
HandleExpirationUpdateMessage(envelope, content, dataMessage, true, timestamp);
128+
}
133129
else
134130
{
135131
HandleSignalMessage(envelope, content, dataMessage, true, timestamp);
@@ -150,26 +146,167 @@ private void HandleMessage(SignalServiceEnvelope envelope)
150146
}
151147
}
152148

153-
private void HandleGroupUpdateMessage(SignalServiceEnvelope envelope, SignalServiceContent content, SignalServiceDataMessage dataMessage, long timestamp)
149+
private void HandleExpirationUpdateMessage(SignalServiceEnvelope envelope, SignalServiceContent content, SignalServiceDataMessage message, bool isSync, long timestamp)
150+
{
151+
SignalMessageDirection type;
152+
SignalContact author;
153+
SignalMessageStatus status;
154+
string prefix;
155+
string conversationId;
156+
long composedTimestamp;
157+
158+
if (isSync)
159+
{
160+
var sent = content.SynchronizeMessage.getSent().ForceGetValue();
161+
type = SignalMessageDirection.Synced;
162+
status = SignalMessageStatus.Confirmed;
163+
composedTimestamp = sent.getTimestamp();
164+
author = null;
165+
prefix = "You have";
166+
conversationId = sent.getDestination().ForceGetValue();
167+
}
168+
else
169+
{
170+
status = 0;
171+
type = SignalMessageDirection.Incoming;
172+
author = SignalDBContext.GetOrCreateContactLocked(envelope.getSource(), timestamp, this);
173+
prefix = $"{author.ThreadDisplayName} has";
174+
composedTimestamp = envelope.getTimestamp();
175+
conversationId = envelope.getSource();
176+
}
177+
SignalDBContext.UpdateExpiresInLocked(new SignalConversation() { ThreadId = conversationId }, (uint)message.ExpiresInSeconds);
178+
SignalMessage sm = new SignalMessage()
179+
{
180+
Direction = type,
181+
Type = SignalMessageType.ExpireUpdate,
182+
Status = status,
183+
Author = author,
184+
Content = new SignalMessageContent() { Content = $"{prefix} set the expiration timer to {message.ExpiresInSeconds} seconds." },
185+
ThreadId = conversationId,
186+
DeviceId = (uint)envelope.getSourceDevice(),
187+
Receipts = 0,
188+
ComposedTimestamp = composedTimestamp,
189+
ReceivedTimestamp = timestamp,
190+
};
191+
Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
192+
{
193+
await UIHandleIncomingMessage(sm);
194+
}).AsTask().Wait();
195+
}
196+
197+
private void HandleSessionResetMessage(SignalServiceEnvelope envelope, SignalServiceContent content, SignalServiceDataMessage dataMessage, bool isSync, long timestamp)
198+
{
199+
SignalMessageDirection type;
200+
SignalContact author;
201+
SignalMessageStatus status;
202+
string prefix;
203+
string conversationId;
204+
long composedTimestamp;
205+
206+
if (isSync)
207+
{
208+
var sent = content.SynchronizeMessage.getSent().ForceGetValue();
209+
type = SignalMessageDirection.Synced;
210+
status = SignalMessageStatus.Confirmed;
211+
composedTimestamp = sent.getTimestamp();
212+
author = null;
213+
prefix = "You have";
214+
conversationId = sent.getDestination().ForceGetValue();
215+
}
216+
else
217+
{
218+
status = 0;
219+
type = SignalMessageDirection.Incoming;
220+
author = SignalDBContext.GetOrCreateContactLocked(envelope.getSource(), timestamp, this);
221+
prefix = $"{author.ThreadDisplayName} has";
222+
composedTimestamp = envelope.getTimestamp();
223+
conversationId = envelope.getSource();
224+
}
225+
LibsignalDBContext.DeleteAllSessions(conversationId);
226+
227+
SignalMessage sm = new SignalMessage()
228+
{
229+
Direction = type,
230+
Type = SignalMessageType.SessionReset,
231+
Status = status,
232+
Author = author,
233+
Content = new SignalMessageContent() { Content = $"{prefix} reset the session." },
234+
ThreadId = conversationId,
235+
DeviceId = (uint)envelope.getSourceDevice(),
236+
Receipts = 0,
237+
ComposedTimestamp = composedTimestamp,
238+
ReceivedTimestamp = timestamp,
239+
};
240+
Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
241+
{
242+
await UIHandleIncomingMessage(sm);
243+
}).AsTask().Wait();
244+
}
245+
246+
private void HandleGroupUpdateMessage(SignalServiceEnvelope envelope, SignalServiceContent content, SignalServiceDataMessage dataMessage, bool isSync, long timestamp)
154247
{
155248
if (dataMessage.Group != null) //TODO check signal droid: group messages have different types!
156249
{
157250
SignalServiceGroup group = dataMessage.Group;
251+
string groupid = Base64.encodeBytes(group.GroupId);
158252
SignalGroup g = new SignalGroup();
159253
string displayname = "Unknown group";
160254
string avatarfile = null;
161255
if (group.Name != null)
162256
{
163257
displayname = group.Name;
164258
}
165-
var dbgroup = SignalDBContext.InsertOrUpdateGroupLocked(Base64.encodeBytes(group.GroupId), displayname, avatarfile, true, timestamp, this);
259+
var dbgroup = SignalDBContext.InsertOrUpdateGroupLocked(groupid, displayname, avatarfile, true, timestamp, this);
166260
if (group.Members != null)
167261
{
168262
foreach (var member in group.Members)
169263
{
170264
SignalDBContext.InsertOrUpdateGroupMembershipLocked(dbgroup.Id, SignalDBContext.GetOrCreateContactLocked(member, 0, this).Id);
171265
}
172266
}
267+
268+
/* insert message into conversation */
269+
SignalMessageDirection type;
270+
SignalContact author;
271+
SignalMessageStatus status;
272+
string prefix;
273+
long composedTimestamp;
274+
275+
if (isSync)
276+
{
277+
var sent = content.SynchronizeMessage.getSent().ForceGetValue();
278+
type = SignalMessageDirection.Synced;
279+
status = SignalMessageStatus.Confirmed;
280+
composedTimestamp = sent.getTimestamp();
281+
author = null;
282+
prefix = "You have";
283+
}
284+
else
285+
{
286+
status = 0;
287+
type = SignalMessageDirection.Incoming;
288+
author = SignalDBContext.GetOrCreateContactLocked(envelope.getSource(), timestamp, this);
289+
prefix = $"{author.ThreadDisplayName} has";
290+
composedTimestamp = envelope.getTimestamp();
291+
}
292+
293+
SignalMessage sm = new SignalMessage()
294+
{
295+
Direction = type,
296+
Type = SignalMessageType.GroupUpdate,
297+
Status = status,
298+
Author = author,
299+
Content = new SignalMessageContent() { Content = $"{prefix} updated the group." },
300+
ThreadId = groupid,
301+
DeviceId = (uint)envelope.getSourceDevice(),
302+
Receipts = 0,
303+
ComposedTimestamp = composedTimestamp,
304+
ReceivedTimestamp = timestamp,
305+
};
306+
Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
307+
{
308+
await UIHandleIncomingMessage(sm);
309+
}).AsTask().Wait();
173310
}
174311
else
175312
{

0 commit comments

Comments
 (0)