@@ -1086,14 +1086,53 @@ internal static SignalConversation UpdateMessageRead(long index, SignalConversat
1086
1086
return dbConversation ;
1087
1087
}
1088
1088
1089
- internal static List < SignalContact > InsertOrUpdateContacts ( List < SignalContact > contactsList )
1089
+ internal static List < SignalConversation > InsertOrUpdateGroups ( IList < ( SignalGroup group , IList < string > members ) > groups )
1090
1090
{
1091
- List < SignalContact > refreshedContacts = new List < SignalContact > ( ) ;
1091
+ List < SignalConversation > refreshedGroups = new List < SignalConversation > ( ) ;
1092
1092
lock ( DBLock )
1093
1093
{
1094
1094
using ( var ctx = new SignalDBContext ( ) )
1095
1095
{
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 )
1097
1136
{
1098
1137
var dbContact = ctx . Contacts
1099
1138
. Where ( c => c . ThreadId == contact . ThreadId )
@@ -1103,6 +1142,7 @@ internal static List<SignalContact> InsertOrUpdateContacts(List<SignalContact> c
1103
1142
refreshedContacts . Add ( dbContact ) ;
1104
1143
dbContact . ThreadDisplayName = contact . ThreadDisplayName ;
1105
1144
dbContact . Color = contact . Color ;
1145
+ dbContact . CanReceive = contact . CanReceive ;
1106
1146
dbContact . ExpiresInSeconds = contact . ExpiresInSeconds ;
1107
1147
}
1108
1148
else
@@ -1275,31 +1315,35 @@ public static List<SignalContact> GetAllContactsLocked()
1275
1315
1276
1316
public static SignalContact GetOrCreateContactLocked ( string username , long timestamp , bool notify = true )
1277
1317
{
1278
- SignalContact contact ;
1279
- bool createdNew = false ;
1280
1318
lock ( DBLock )
1281
1319
{
1282
1320
using ( var ctx = new SignalDBContext ( ) )
1283
1321
{
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 ) ;
1301
1323
}
1302
1324
}
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
+ }
1303
1347
if ( createdNew && notify )
1304
1348
{
1305
1349
SignalLibHandle . Instance . DispatchAddOrUpdateConversation ( contact , null ) ;
0 commit comments