Skip to content

Commit 122fde9

Browse files
committed
Handle contact and group sync messages
1 parent d1101b3 commit 122fde9

File tree

3 files changed

+80
-23
lines changed

3 files changed

+80
-23
lines changed

Signal-Windows.Lib/IncomingMessages.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ private void HandleMessage(SignalServiceEnvelope envelope)
193193
var tmpFile = LibUtils.CreateTmpFile("groups_sync");
194194
var plaintextStream = MessageReceiver.RetrieveAttachment(groups.AsPointer(), tmpFile, 10000, null);
195195
var deviceGroupsStream = new DeviceGroupsInputStream(plaintextStream);
196+
var groupsList = new List<(SignalGroup, IList<string>)>();
196197
DeviceGroup g;
197198
while ((g = deviceGroupsStream.Read()) != null)
198199
{
@@ -204,7 +205,18 @@ private void HandleMessage(SignalServiceEnvelope envelope)
204205

205206
}
206207
}
208+
var group = new SignalGroup()
209+
{
210+
ThreadDisplayName = g.Name,
211+
ThreadId = Base64.EncodeBytes(g.Id),
212+
GroupMemberships = new List<GroupMembership>(),
213+
CanReceive = true,
214+
ExpiresInSeconds = g.ExpirationTimer != null ? g.ExpirationTimer.Value : 0
215+
};
216+
groupsList.Add((group, g.Members));
207217
}
218+
List<SignalConversation> newConversations = SignalDBContext.InsertOrUpdateGroups(groupsList);
219+
SignalLibHandle.Instance.DispatchAddOrUpdateConversations(newConversations);
208220
}
209221
else if (content.SynchronizeMessage.Contacts != null && content.SynchronizeMessage.Contacts.Complete) //TODO incomplete updates
210222
{
@@ -232,6 +244,7 @@ private void HandleMessage(SignalServiceEnvelope envelope)
232244
ThreadDisplayName = c.Name,
233245
ThreadId = c.Number,
234246
Color = c.Color,
247+
CanReceive = true,
235248
ExpiresInSeconds = c.ExpirationTimer != null ? c.ExpirationTimer.Value : 0
236249
};
237250
contactsList.Add(contact);

Signal-Windows.Lib/SignalLibHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ internal void DispatchHandleIdentityKeyChange(LinkedList<SignalMessage> messages
476476
Task.WaitAll(operations.ToArray());
477477
}
478478

479-
internal void DispatchAddOrUpdateConversations(List<SignalContact> newConversations)
479+
internal void DispatchAddOrUpdateConversations(List<SignalConversation> newConversations)
480480
{
481481
List<Task> operations = new List<Task>();
482482
foreach (var dispatcher in Frames.Keys)

Signal-Windows.Lib/Storage/DB.cs

Lines changed: 66 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,14 +1086,53 @@ internal static SignalConversation UpdateMessageRead(long index, SignalConversat
10861086
return dbConversation;
10871087
}
10881088

1089-
internal static List<SignalContact> InsertOrUpdateContacts(List<SignalContact> contactsList)
1089+
internal static List<SignalConversation> InsertOrUpdateGroups(IList<(SignalGroup group, IList<string> members)> groups)
10901090
{
1091-
List<SignalContact> refreshedContacts = new List<SignalContact>();
1091+
List<SignalConversation> refreshedGroups = new List<SignalConversation>();
10921092
lock (DBLock)
10931093
{
10941094
using (var ctx = new SignalDBContext())
10951095
{
1096-
foreach (var contact in contactsList)
1096+
foreach (var receivedGroup in groups)
1097+
{
1098+
var dbGroup = ctx.Groups
1099+
.Where(g => g.ThreadId == receivedGroup.group.ThreadId)
1100+
.Include(g => g.GroupMemberships)
1101+
.SingleOrDefault();
1102+
if (dbGroup != null)
1103+
{
1104+
dbGroup.GroupMemberships.Clear();
1105+
dbGroup.ThreadDisplayName = receivedGroup.group.ThreadDisplayName;
1106+
dbGroup.CanReceive = receivedGroup.group.CanReceive;
1107+
dbGroup.ExpiresInSeconds = receivedGroup.group.ExpiresInSeconds;
1108+
}
1109+
else
1110+
{
1111+
dbGroup = receivedGroup.group;
1112+
ctx.Groups.Add(dbGroup);
1113+
}
1114+
foreach (var member in receivedGroup.members)
1115+
{
1116+
dbGroup.GroupMemberships.Add(new GroupMembership()
1117+
{
1118+
Contact = GetOrCreateContact(ctx, member, 0),
1119+
Group = dbGroup
1120+
});
1121+
}
1122+
}
1123+
}
1124+
}
1125+
return refreshedGroups;
1126+
}
1127+
1128+
internal static List<SignalConversation> InsertOrUpdateContacts(IList<SignalContact> contacts)
1129+
{
1130+
List<SignalConversation> refreshedContacts = new List<SignalConversation>();
1131+
lock (DBLock)
1132+
{
1133+
using (var ctx = new SignalDBContext())
1134+
{
1135+
foreach (var contact in contacts)
10971136
{
10981137
var dbContact = ctx.Contacts
10991138
.Where(c => c.ThreadId == contact.ThreadId)
@@ -1103,6 +1142,7 @@ internal static List<SignalContact> InsertOrUpdateContacts(List<SignalContact> c
11031142
refreshedContacts.Add(dbContact);
11041143
dbContact.ThreadDisplayName = contact.ThreadDisplayName;
11051144
dbContact.Color = contact.Color;
1145+
dbContact.CanReceive = contact.CanReceive;
11061146
dbContact.ExpiresInSeconds = contact.ExpiresInSeconds;
11071147
}
11081148
else
@@ -1275,31 +1315,35 @@ public static List<SignalContact> GetAllContactsLocked()
12751315

12761316
public static SignalContact GetOrCreateContactLocked(string username, long timestamp, bool notify = true)
12771317
{
1278-
SignalContact contact;
1279-
bool createdNew = false;
12801318
lock (DBLock)
12811319
{
12821320
using (var ctx = new SignalDBContext())
12831321
{
1284-
contact = ctx.Contacts
1285-
.Where(c => c.ThreadId == username)
1286-
.SingleOrDefault();
1287-
if (contact == null)
1288-
{
1289-
contact = new SignalContact()
1290-
{
1291-
ThreadId = username,
1292-
ThreadDisplayName = username,
1293-
CanReceive = true,
1294-
LastActiveTimestamp = timestamp,
1295-
Color = null //Utils.CalculateDefaultColor(username)
1296-
};
1297-
ctx.Contacts.Add(contact);
1298-
ctx.SaveChanges();
1299-
createdNew = true;
1300-
}
1322+
return GetOrCreateContact(ctx, username, timestamp, notify);
13011323
}
13021324
}
1325+
}
1326+
1327+
private static SignalContact GetOrCreateContact(SignalDBContext ctx, string username, long timestamp, bool notify = true)
1328+
{
1329+
bool createdNew = false;
1330+
SignalContact contact = contact = ctx.Contacts
1331+
.Where(c => c.ThreadId == username)
1332+
.SingleOrDefault();
1333+
if (contact == null)
1334+
{
1335+
contact = new SignalContact()
1336+
{
1337+
ThreadId = username,
1338+
ThreadDisplayName = username,
1339+
CanReceive = true,
1340+
LastActiveTimestamp = timestamp,
1341+
Color = null //Utils.CalculateDefaultColor(username)
1342+
};
1343+
ctx.Contacts.Add(contact);
1344+
ctx.SaveChanges();
1345+
createdNew = true;
1346+
}
13031347
if (createdNew && notify)
13041348
{
13051349
SignalLibHandle.Instance.DispatchAddOrUpdateConversation(contact, null);

0 commit comments

Comments
 (0)