-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
OTRDatabaseView.m
418 lines (366 loc) · 20.2 KB
/
OTRDatabaseView.m
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//
// OTRDatabaseView.m
// Off the Record
//
// Created by David Chiles on 3/31/14.
// Copyright (c) 2014 Chris Ballinger. All rights reserved.
//
#import "OTRDatabaseView.h"
@import YapDatabase;
#import "OTRDatabaseManager.h"
#import "OTRBuddy.h"
#import "OTRAccount.h"
#import "OTRIncomingMessage.h"
#import "OTRLog.h"
#import "OTROutgoingMessage.h"
#import "ChatSecureCoreCompat-Swift.h"
NSString *OTRArchiveFilteredConversationsName = @"OTRFilteredConversationsName";
NSString *OTRBuddyFilteredConversationsName = @"OTRBuddyFilteredConversationsName";
NSString *OTRConversationGroup = @"Conversation";
NSString *OTRConversationDatabaseViewExtensionName = @"OTRConversationDatabaseViewExtensionName";
NSString *OTRChatDatabaseViewExtensionName = @"OTRChatDatabaseViewExtensionName";
NSString *OTRFilteredChatDatabaseViewExtensionName = @"OTRFilteredChatDatabaseViewExtensionName";
NSString *OTRAllBuddiesDatabaseViewExtensionName = @"OTRAllBuddiesDatabaseViewExtensionName";
NSString *OTRArchiveFilteredBuddiesName = @"OTRFilteredBuddiesName";
NSString *OTRAllSubscriptionRequestsViewExtensionName = @"AllSubscriptionRequestsViewExtensionName";
NSString *OTRAllPushAccountInfoViewExtensionName = @"OTRAllPushAccountInfoViewExtensionName";
NSString *OTRAllAccountGroup = @"All Accounts";
NSString *OTRAllAccountDatabaseViewExtensionName = @"OTRAllAccountDatabaseViewExtensionName";
NSString *OTRChatMessageGroup = @"Messages";
NSString *OTRBuddyGroup = @"Buddy";
NSString *OTRAllPresenceSubscriptionRequestGroup = @"OTRAllPresenceSubscriptionRequestGroup";
NSString *OTRUnreadMessageGroup = @"Unread Messages";
NSString *OTRPushTokenGroup = @"Tokens";
NSString *OTRPushDeviceGroup = @"Devices";
NSString *OTRPushAccountGroup = @"Account";
@implementation OTRDatabaseView
+ (BOOL)registerArchiveFilteredConversationsViewWithDatabase:(YapDatabase *)database {
YapDatabaseFilteredView *filteredView = [database registeredExtension:OTRArchiveFilteredConversationsName];
if (filteredView) {
return YES;
}
YapDatabaseView *conversationView = [database registeredExtension:OTRConversationDatabaseViewExtensionName];
if (!conversationView) {
return NO;
}
YapDatabaseViewOptions *options = [[YapDatabaseViewOptions alloc] init];
options.isPersistent = NO;
BOOL showArchived = NO;
YapDatabaseViewFiltering *filtering = [YapDatabaseViewFiltering withObjectBlock:^BOOL(YapDatabaseReadTransaction * _Nonnull transaction, NSString * _Nonnull group, NSString * _Nonnull collection, NSString * _Nonnull key, id _Nonnull object) {
if ([object conformsToProtocol:@protocol(OTRThreadOwner)]) {
id<OTRThreadOwner> threadOwner = object;
BOOL isArchived = threadOwner.isArchived;
return showArchived == isArchived;
}
return YES;
}];
filteredView = [[YapDatabaseFilteredView alloc] initWithParentViewName:OTRConversationDatabaseViewExtensionName filtering:filtering versionTag:[NSUUID UUID].UUIDString options:options];
return [database registerExtension:filteredView withName:OTRArchiveFilteredConversationsName];
}
+ (BOOL)registerBuddyFilteredConversationsViewWithDatabase:(YapDatabase *)database {
YapDatabaseFilteredView *filteredView = [database registeredExtension:OTRBuddyFilteredConversationsName];
if (filteredView) {
return YES;
}
YapDatabaseView *conversationView = [database registeredExtension:OTRConversationDatabaseViewExtensionName];
if (!conversationView) {
return NO;
}
YapDatabaseViewOptions *options = [[YapDatabaseViewOptions alloc] init];
options.isPersistent = YES;
NSSet *whiteListSet = [NSSet setWithObjects:[OTRBuddy collection], nil];
options.allowedCollections = [[YapWhitelistBlacklist alloc] initWithWhitelist:whiteListSet];
YapDatabaseViewFiltering *filtering = [YapDatabaseViewFiltering withObjectBlock:^BOOL(YapDatabaseReadTransaction * _Nonnull transaction, NSString * _Nonnull group, NSString * _Nonnull collection, NSString * _Nonnull key, id _Nonnull object) {
id<OTRThreadOwner> thread = (id<OTRThreadOwner>)object;
if (![thread conformsToProtocol:@protocol(OTRThreadOwner)]) {
return NO;
}
id<OTRMessageProtocol> lastMessage = [thread lastMessageWithTransaction:transaction];
if (!lastMessage) {
return NO;
}
return YES;
}];
filteredView = [[YapDatabaseFilteredView alloc] initWithParentViewName:OTRConversationDatabaseViewExtensionName filtering:filtering versionTag:@"1" options:options];
return [database registerExtension:filteredView withName:OTRBuddyFilteredConversationsName];
}
+ (BOOL)registerConversationDatabaseViewWithDatabase:(YapDatabase *)database
{
YapDatabaseView *conversationView = [database registeredExtension:OTRConversationDatabaseViewExtensionName];
if (conversationView) {
return YES;
}
YapDatabaseViewGrouping *viewGrouping = [YapDatabaseViewGrouping withObjectBlock:^NSString *(YapDatabaseReadTransaction *transaction, NSString *collection, NSString *key, id object) {
if ([object conformsToProtocol:@protocol(OTRThreadOwner)]) {
if ([object isKindOfClass:[OTRXMPPBuddy class]] && ((OTRXMPPBuddy *)object).askingForApproval) {
return OTRAllPresenceSubscriptionRequestGroup;
}
if ([object isKindOfClass:[OTRBuddy class]])
{
OTRBuddy *buddy = (OTRBuddy *)object;
if (!buddy.username.length) {
return nil;
}
// Hack to show "placeholder" items in list
if (buddy.lastMessageId && buddy.lastMessageId.length == 0) {
return OTRConversationGroup;
}
id <OTRMessageProtocol> lastMessage = [buddy lastMessageWithTransaction:transaction];
if (lastMessage) {
return OTRConversationGroup;
}
} else {
return OTRConversationGroup;
}
}
return nil; // exclude from view
}];
YapDatabaseViewSorting *viewSorting = [YapDatabaseViewSorting withObjectBlock:^NSComparisonResult(YapDatabaseReadTransaction *transaction, NSString *group, NSString *collection1, NSString *key1, id object1, NSString *collection2, NSString *key2, id object2) {
if ([group isEqualToString:OTRConversationGroup]) {
if ([object1 conformsToProtocol:@protocol(OTRThreadOwner)] && [object2 conformsToProtocol:@protocol(OTRThreadOwner)]) {
id <OTRThreadOwner> thread1 = object1;
id <OTRThreadOwner> thread2 = object2;
id <OTRMessageProtocol> message1 = [thread1 lastMessageWithTransaction:transaction];
id <OTRMessageProtocol> message2 = [thread2 lastMessageWithTransaction:transaction];
// Assume nil dates indicate a lastMessageId of ""
// indicating that we want to force to the top
NSDate *date1 = [message1 messageDate];
if (!date1) {
if (thread1.lastMessageIdentifier && thread1.lastMessageIdentifier.length == 0) {
date1 = [NSDate date];
} else {
date1 = [NSDate distantPast];
}
}
NSDate *date2 = [message2 messageDate];
if (!date2) {
if (thread2.lastMessageIdentifier && thread2.lastMessageIdentifier.length == 0) {
date2 = [NSDate date];
} else {
date2 = [NSDate distantPast];
}
}
return [date2 compare:date1];
}
} else if ([group isEqualToString:OTRAllPresenceSubscriptionRequestGroup]) {
if ([object1 isKindOfClass:[OTRXMPPBuddy class]] && [object2 isKindOfClass:[OTRXMPPBuddy class]]) {
OTRXMPPBuddy *request1 = object1;
OTRXMPPBuddy *request2 = object2;
return [request2.displayName compare:request1.displayName];
}
}
return NSOrderedSame;
}];
YapDatabaseViewOptions *options = [[YapDatabaseViewOptions alloc] init];
options.isPersistent = YES;
NSSet *whiteListSet = [NSSet setWithObjects:[OTRBuddy collection],[OTRXMPPRoom collection], nil];
options.allowedCollections = [[YapWhitelistBlacklist alloc] initWithWhitelist:whiteListSet];
YapDatabaseAutoView *databaseView = [[YapDatabaseAutoView alloc] initWithGrouping:viewGrouping
sorting:viewSorting
versionTag:@"9"
options:options];
BOOL result = [database registerExtension:databaseView withName:OTRConversationDatabaseViewExtensionName];
if (result) {
result = [self registerArchiveFilteredConversationsViewWithDatabase:database];
}
if (result) {
result = [self registerBuddyFilteredConversationsViewWithDatabase:database];
}
return result;
}
+ (BOOL)registerAllAccountsDatabaseViewWithDatabase:(YapDatabase *)database
{
YapDatabaseView *accountView = [database registeredExtension:OTRAllAccountDatabaseViewExtensionName];
if (accountView) {
return YES;
}
[YapDatabaseViewGrouping withObjectBlock:^NSString * _Nullable(YapDatabaseReadTransaction * _Nonnull transaction, NSString * _Nonnull collection, NSString * _Nonnull key, id _Nonnull object) {
if ([collection isEqualToString:[OTRAccount collection]] && [object isKindOfClass:[OTRAccount class]])
{
OTRAccount *account = object;
if (!account.username.length) {
return nil;
}
return OTRAllAccountGroup;
}
return nil;
}];
YapDatabaseViewGrouping *viewGrouping = [YapDatabaseViewGrouping withKeyBlock:^NSString *(YapDatabaseReadTransaction *transaction, NSString *collection, NSString *key) {
if ([collection isEqualToString:[OTRAccount collection]])
{
return OTRAllAccountGroup;
}
return nil;
}];
YapDatabaseViewSorting *viewSorting = [YapDatabaseViewSorting withObjectBlock:^NSComparisonResult(YapDatabaseReadTransaction *transaction, NSString *group, NSString *collection1, NSString *key1, id object1, NSString *collection2, NSString *key2, id object2) {
if ([group isEqualToString:OTRAllAccountGroup]) {
if ([object1 isKindOfClass:[OTRAccount class]] && [object2 isKindOfClass:[OTRAccount class]]) {
OTRAccount *account1 = (OTRAccount *)object1;
OTRAccount *account2 = (OTRAccount *)object2;
return [account1.displayName compare:account2.displayName options:NSCaseInsensitiveSearch];
}
}
return NSOrderedSame;
}];
YapDatabaseViewOptions *options = [[YapDatabaseViewOptions alloc] init];
options.isPersistent = YES;
options.allowedCollections = [[YapWhitelistBlacklist alloc] initWithWhitelist:[NSSet setWithObject:[OTRAccount collection]]];
YapDatabaseAutoView *databaseView = [[YapDatabaseAutoView alloc] initWithGrouping:viewGrouping
sorting:viewSorting
versionTag:@"2"
options:options];
return [database registerExtension:databaseView withName:OTRAllAccountDatabaseViewExtensionName];
}
+ (BOOL)registerFilteredChatViewWithDatabase:(YapDatabase *)database {
YapDatabaseFilteredView *filteredView = [database registeredExtension:OTRFilteredChatDatabaseViewExtensionName];
if (filteredView) {
return YES;
}
YapDatabaseView *chatView = [database registeredExtension:OTRChatDatabaseViewExtensionName];
if (!chatView) {
return NO;
}
YapDatabaseViewOptions *options = [[YapDatabaseViewOptions alloc] init];
YapDatabaseViewFiltering *filtering = [YapDatabaseViewFiltering withObjectBlock:^BOOL(YapDatabaseReadTransaction * _Nonnull transaction, NSString * _Nonnull group, NSString * _Nonnull collection, NSString * _Nonnull key, id _Nonnull object) {
if ([object conformsToProtocol:@protocol(OTRMessageProtocol)]) {
id<OTRMessageProtocol> message = object;
BOOL shouldDisplay = [FileTransferManager shouldDisplayMessage:message transaction:transaction];
return shouldDisplay;
}
return YES;
}];
filteredView = [[YapDatabaseFilteredView alloc] initWithParentViewName:OTRChatDatabaseViewExtensionName filtering:filtering versionTag:@"6" options:options];
return [database registerExtension:filteredView withName:OTRFilteredChatDatabaseViewExtensionName];
}
+ (BOOL)registerChatDatabaseViewWithDatabase:(YapDatabase *)database
{
if ([database registeredExtension:OTRChatDatabaseViewExtensionName]) {
return YES;
}
YapDatabaseViewGrouping *viewGrouping = [YapDatabaseViewGrouping withObjectBlock:^NSString *(YapDatabaseReadTransaction *transaction, NSString *collection, NSString *key, id object) {
if ([object conformsToProtocol:@protocol(OTRMessageProtocol)])
{
id <OTRMessageProtocol> message = object;
NSString *threadId = [message threadId];
if (!threadId) {
DDLogError(@"Message has no threadId! %@", message);
return nil;
} else {
return threadId;
}
} else {
DDLogError(@"Object in view does not conform to OTRMessageProtocol! %@", object);
return nil;
}
}];
YapDatabaseViewSorting *viewSorting = [YapDatabaseViewSorting withObjectBlock:^NSComparisonResult(YapDatabaseReadTransaction *transaction, NSString *group, NSString *collection1, NSString *key1, id object1, NSString *collection2, NSString *key2, id object2) {
if ([object1 conformsToProtocol:@protocol(OTRMessageProtocol)] && [object2 conformsToProtocol:@protocol(OTRMessageProtocol)]) {
id <OTRMessageProtocol> message1 = (id <OTRMessageProtocol>)object1;
id <OTRMessageProtocol> message2 = (id <OTRMessageProtocol>)object2;
return [[message1 messageDate] compare:[message2 messageDate]];
}
return NSOrderedSame;
}];
YapDatabaseViewOptions *options = [[YapDatabaseViewOptions alloc] init];
options.isPersistent = YES;
NSSet *whitelist = [NSSet setWithObjects:[OTRBaseMessage collection],[OTRXMPPRoomMessage collection], nil];
options.allowedCollections = [[YapWhitelistBlacklist alloc] initWithWhitelist:whitelist];
YapDatabaseAutoView *view = [[YapDatabaseAutoView alloc] initWithGrouping:viewGrouping
sorting:viewSorting
versionTag:@"1"
options:options];
return [database registerExtension:view withName:OTRChatDatabaseViewExtensionName] && [self registerFilteredChatViewWithDatabase:database];
}
+ (BOOL)registerAllBuddiesDatabaseViewWithDatabase:(YapDatabase *)database
{
if ([database registeredExtension:OTRAllBuddiesDatabaseViewExtensionName]) {
return YES;
}
YapDatabaseViewGrouping *viewGrouping = [YapDatabaseViewGrouping withObjectBlock:^NSString *(YapDatabaseReadTransaction *transaction, NSString *collection, NSString *key, id object) {
if ([object isKindOfClass:[OTRBuddy class]]) {
//Checking to see if the buddy username is equal to the account username in order to remove 'self' buddy
OTRBuddy *buddy = (OTRBuddy *)object;
OTRAccount *account = [buddy accountWithTransaction:transaction];
// Hack fix for buddies created without an account
// There must be a race condition in the roster popualtion
if (!account) {
return nil;
}
// Filter out buddies with no username
if (!buddy.username.length) {
return nil;
}
if (![account.username isEqualToString:buddy.username]) {
// Filter out buddies that are not really on our roster
if ([buddy isKindOfClass:[OTRXMPPBuddy class]]) {
OTRXMPPBuddy *xmppBuddy = (OTRXMPPBuddy *)buddy;
if (xmppBuddy.trustLevel != BuddyTrustLevelRoster && !xmppBuddy.pendingApproval) {
return nil;
}
}
return OTRBuddyGroup;
}
}
return nil;
}];
YapDatabaseViewSorting *viewSorting = [YapDatabaseViewSorting withObjectBlock:^NSComparisonResult(YapDatabaseReadTransaction *transaction, NSString *group, NSString *collection1, NSString *key1, id object1, NSString *collection2, NSString *key2, id object2) {
OTRBuddy *buddy1 = (OTRBuddy *)object1;
OTRBuddy *buddy2 = (OTRBuddy *)object2;
NSComparisonResult result = NSOrderedSame;
if (buddy1.currentStatus == buddy2.currentStatus) {
NSString *buddy1String = buddy1.username;
NSString *buddy2String = buddy2.username;
if ([buddy1.displayName length]) {
buddy1String = buddy1.displayName;
}
if ([buddy2.displayName length]) {
buddy2String = buddy2.displayName;
}
result = [buddy1String compare:buddy2String options:NSCaseInsensitiveSearch];
}
else if (buddy1.currentStatus < buddy2.currentStatus) {
result = NSOrderedAscending;
}
else {
result = NSOrderedDescending;
}
NSComparisonResult archiveSort = [@(buddy1.isArchived) compare:@(buddy2.isArchived)];
if (archiveSort == NSOrderedSame) {
return result;
} else {
return archiveSort;
}
}];
YapDatabaseViewOptions *options = [[YapDatabaseViewOptions alloc] init];
options.isPersistent = YES;
options.allowedCollections = [[YapWhitelistBlacklist alloc] initWithWhitelist:[NSSet setWithObject:[OTRBuddy collection]]];
YapDatabaseAutoView *view = [[YapDatabaseAutoView alloc] initWithGrouping:viewGrouping
sorting:viewSorting
versionTag:@"9"
options:options];
return [database registerExtension:view withName:OTRAllBuddiesDatabaseViewExtensionName] && [self registerFilteredBuddiesViewWithDatabase:database];
}
+ (BOOL)registerFilteredBuddiesViewWithDatabase:(YapDatabase *)database {
YapDatabaseFilteredView *filteredView = [database registeredExtension:OTRArchiveFilteredBuddiesName];
if (filteredView) {
return YES;
}
YapDatabaseView *buddiesView = [database registeredExtension:OTRAllBuddiesDatabaseViewExtensionName];
if (!buddiesView) {
return NO;
}
YapDatabaseViewOptions *options = [[YapDatabaseViewOptions alloc] init];
options.isPersistent = NO;
BOOL showArchived = NO;
YapDatabaseViewFiltering *filtering = [YapDatabaseViewFiltering withObjectBlock:^BOOL(YapDatabaseReadTransaction * _Nonnull transaction, NSString * _Nonnull group, NSString * _Nonnull collection, NSString * _Nonnull key, id _Nonnull object) {
if ([object conformsToProtocol:@protocol(OTRThreadOwner)]) {
id<OTRThreadOwner> threadOwner = object;
BOOL isArchived = threadOwner.isArchived;
return showArchived == isArchived;
}
return YES;
}];
filteredView = [[YapDatabaseFilteredView alloc] initWithParentViewName:OTRAllBuddiesDatabaseViewExtensionName filtering:filtering versionTag:[NSUUID UUID].UUIDString options:options];
return [database registerExtension:filteredView withName:OTRArchiveFilteredBuddiesName];
}
@end