@@ -221,214 +221,6 @@ - (nullable TSInteraction *)firstInteractionAtOrAroundSortId:(uint64_t)sortId
221221 transaction: transaction];
222222}
223223
224- - (void )updateWithInsertedMessage : (TSInteraction *)message transaction : (DBWriteTransaction *)transaction
225- {
226- [self updateWithMessage: message wasMessageInserted: YES transaction: transaction];
227- }
228-
229- - (void )updateWithUpdatedMessage : (TSInteraction *)message transaction : (DBWriteTransaction *)transaction
230- {
231- [self updateWithMessage: message wasMessageInserted: NO transaction: transaction];
232- }
233-
234- - (uint64_t )messageSortIdForMessage : (TSInteraction *)message
235- {
236- if (message.grdbId == nil ) {
237- OWSFailDebug (@" Missing messageSortId." );
238- } else if (message.grdbId .unsignedLongLongValue == 0 ) {
239- OWSFailDebug (@" Invalid messageSortId." );
240- } else {
241- return message.grdbId .unsignedLongLongValue ;
242- }
243- return 0 ;
244- }
245-
246- - (void )updateWithMessage : (TSInteraction *)message
247- wasMessageInserted : (BOOL )wasMessageInserted
248- transaction : (DBWriteTransaction *)transaction
249- {
250- OWSAssertDebug (message != nil );
251- OWSAssertDebug (transaction != nil );
252-
253- BOOL hasLastVisibleInteraction = [self hasLastVisibleInteractionWithTransaction: transaction];
254- BOOL needsToClearLastVisibleSortId = hasLastVisibleInteraction && wasMessageInserted;
255-
256- if (![message shouldAppearInInboxWithTransaction: transaction]) {
257- // We want to clear the last visible sort ID on any new message,
258- // even if the message doesn't appear in the inbox view.
259- if (needsToClearLastVisibleSortId) {
260- [self clearLastVisibleInteractionWithTransaction: transaction];
261- }
262- [self scheduleTouchFinalizationWithTransaction: transaction];
263- return ;
264- }
265-
266- uint64_t messageSortId = [self messageSortIdForMessage: message];
267- BOOL needsToMarkAsVisible = !self.shouldThreadBeVisible ;
268-
269- ThreadAssociatedData *associatedData = [ThreadAssociatedData fetchOrDefaultForThread: self transaction: transaction];
270-
271- BOOL needsToClearArchived = [self shouldClearArchivedStatusWhenUpdatingWithMessage: message
272- wasMessageInserted: wasMessageInserted
273- threadAssociatedData: associatedData
274- transaction: transaction];
275-
276- BOOL needsToUpdateLastInteractionRowId = messageSortId > self.lastInteractionRowId ;
277-
278- BOOL needsToClearIsMarkedUnread = associatedData.isMarkedUnread && wasMessageInserted;
279-
280- if (needsToMarkAsVisible || needsToClearArchived || needsToUpdateLastInteractionRowId
281- || needsToClearLastVisibleSortId || needsToClearIsMarkedUnread) {
282- [self anyUpdateWithTransaction: transaction
283- block: ^(TSThread *thread) {
284- thread.shouldThreadBeVisible = YES ;
285- thread.lastInteractionRowId = MAX (thread.lastInteractionRowId , messageSortId);
286- }];
287- [associatedData clearIsArchived: needsToClearArchived
288- clearIsMarkedUnread: needsToClearIsMarkedUnread
289- updateStorageService: YES
290- transaction: transaction];
291- if (needsToMarkAsVisible) {
292- // Non-visible threads don't get indexed, so if we're becoming visible for the first time...
293- [SSKEnvironment.shared.databaseStorageRef touchWithThread: self
294- shouldReindex: YES
295- shouldUpdateChatListUi: YES
296- tx: transaction];
297- }
298- if (needsToClearLastVisibleSortId) {
299- [self clearLastVisibleInteractionWithTransaction: transaction];
300- }
301- } else {
302- [self scheduleTouchFinalizationWithTransaction: transaction];
303- }
304- }
305-
306- - (BOOL )shouldClearArchivedStatusWhenUpdatingWithMessage : (TSInteraction *)message
307- wasMessageInserted : (BOOL )wasMessageInserted
308- threadAssociatedData : (ThreadAssociatedData *)threadAssociatedData
309- transaction : (DBReadTransaction *)transaction
310- {
311- BOOL needsToClearArchived = threadAssociatedData.isArchived && wasMessageInserted;
312-
313- // Shouldn't clear archived during migrations.
314- if (!AppContextObjCBridge.shared .isRunningTests && !AppReadinessObjcBridge.isAppReady ) {
315- needsToClearArchived = NO ;
316- }
317-
318- if ([message isKindOfClass: TSInfoMessage.class]) {
319- switch (((TSInfoMessage *)message).messageType ) {
320- case TSInfoMessageSyncedThread: // Shouldn't clear archived during thread import.
321- case TSInfoMessageThreadMerge:
322- needsToClearArchived = NO ;
323- break ;
324- case TSInfoMessageTypeLocalUserEndedSession:
325- case TSInfoMessageTypeRemoteUserEndedSession:
326- case TSInfoMessageUserNotRegistered:
327- case TSInfoMessageTypeUnsupportedMessage:
328- case TSInfoMessageTypeGroupUpdate:
329- case TSInfoMessageTypeGroupQuit:
330- case TSInfoMessageTypeDisappearingMessagesUpdate:
331- case TSInfoMessageAddToContactsOffer:
332- case TSInfoMessageVerificationStateChange:
333- case TSInfoMessageAddUserToProfileWhitelistOffer:
334- case TSInfoMessageAddGroupToProfileWhitelistOffer:
335- case TSInfoMessageUnknownProtocolVersion:
336- case TSInfoMessageUserJoinedSignal:
337- case TSInfoMessageProfileUpdate:
338- case TSInfoMessagePhoneNumberChange:
339- case TSInfoMessageRecipientHidden:
340- case TSInfoMessagePaymentsActivationRequest:
341- case TSInfoMessagePaymentsActivated:
342- case TSInfoMessageSessionSwitchover:
343- case TSInfoMessageReportedSpam:
344- case TSInfoMessageLearnedProfileName:
345- case TSInfoMessageBlockedOtherUser:
346- case TSInfoMessageBlockedGroup:
347- case TSInfoMessageUnblockedOtherUser:
348- case TSInfoMessageUnblockedGroup:
349- case TSInfoMessageAcceptedMessageRequest:
350- break ;
351- }
352- }
353-
354- // Shouldn't clear archived if:
355- // - The thread is muted.
356- // - The user has requested we keep muted chats archived.
357- // - The message was sent by someone other than the current user. (If the
358- // current user sent the message, we should clear archived.)
359- {
360- BOOL threadIsMuted = threadAssociatedData.isMuted ;
361- BOOL shouldKeepMutedChatsArchived = [SSKPreferences shouldKeepMutedChatsArchivedWithTransaction: transaction];
362- BOOL wasMessageSentByUs = [message isKindOfClass: [TSOutgoingMessage class ]];
363- if (threadIsMuted && shouldKeepMutedChatsArchived && !wasMessageSentByUs) {
364- needsToClearArchived = NO ;
365- }
366- }
367-
368- return needsToClearArchived;
369- }
370-
371- - (void )updateWithRemovedMessage : (TSInteraction *)message transaction : (DBWriteTransaction *)transaction
372- {
373- OWSAssertDebug (message != nil );
374- OWSAssertDebug (transaction != nil );
375-
376- uint64_t messageSortId = [self messageSortIdForMessage: message];
377- BOOL needsToUpdateLastInteractionRowId = messageSortId == self.lastInteractionRowId ;
378-
379- NSNumber *_Nullable lastVisibleSortId = [self lastVisibleSortIdWithTransaction: transaction];
380- BOOL needsToUpdateLastVisibleSortId
381- = (lastVisibleSortId != nil && lastVisibleSortId.unsignedLongLongValue == messageSortId);
382-
383- [self updateOnInteractionsRemovedWithNeedsToUpdateLastInteractionRowId: needsToUpdateLastInteractionRowId
384- needsToUpdateLastVisibleSortId: needsToUpdateLastVisibleSortId
385- lastVisibleSortId: lastVisibleSortId
386- transaction: transaction];
387- }
388-
389- - (void )updateOnInteractionsRemovedWithNeedsToUpdateLastInteractionRowId : (BOOL )needsToUpdateLastInteractionRowId
390- needsToUpdateLastVisibleSortId : (BOOL )needsToUpdateLastVisibleSortId
391- transaction : (DBWriteTransaction *)transaction
392- {
393- NSNumber *_Nullable lastVisibleSortId = [self lastVisibleSortIdWithTransaction: transaction];
394-
395- [self updateOnInteractionsRemovedWithNeedsToUpdateLastInteractionRowId: needsToUpdateLastInteractionRowId
396- needsToUpdateLastVisibleSortId: needsToUpdateLastVisibleSortId
397- lastVisibleSortId: lastVisibleSortId
398- transaction: transaction];
399- }
400-
401- - (void )updateOnInteractionsRemovedWithNeedsToUpdateLastInteractionRowId : (BOOL )needsToUpdateLastInteractionRowId
402- needsToUpdateLastVisibleSortId : (BOOL )needsToUpdateLastVisibleSortId
403- lastVisibleSortId : (nullable NSNumber *)lastVisibleSortId
404- transaction : (DBWriteTransaction *)transaction
405- {
406- if (needsToUpdateLastInteractionRowId || needsToUpdateLastVisibleSortId) {
407- [self anyUpdateWithTransaction: transaction
408- block: ^(TSThread *thread) {
409- if (needsToUpdateLastInteractionRowId) {
410- TSInteraction *_Nullable latestInteraction =
411- [thread lastInteractionForInboxWithTransaction: transaction];
412- thread.lastInteractionRowId = latestInteraction ? latestInteraction.sortId : 0 ;
413- }
414- }];
415-
416- if (needsToUpdateLastVisibleSortId) {
417- TSInteraction *_Nullable messageBeforeDeletedMessage =
418- [self firstInteractionAtOrAroundSortId: lastVisibleSortId.unsignedLongLongValue transaction: transaction];
419- if (messageBeforeDeletedMessage != nil ) {
420- [self setLastVisibleInteractionWithSortId: messageBeforeDeletedMessage.sortId
421- onScreenPercentage: 1
422- transaction: transaction];
423- } else {
424- [self clearLastVisibleInteractionWithTransaction: transaction];
425- }
426- }
427- } else {
428- [self scheduleTouchFinalizationWithTransaction: transaction];
429- }
430- }
431-
432224#pragma mark - Archival
433225
434226+ (BOOL )legacyIsArchivedWithLastMessageDate : (nullable NSDate *)lastMessageDate
0 commit comments