MessageFetcher.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2014-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "MessageFetcher.h"
  21. #define CONVERSATION_PREDICATE [NSPredicate predicateWithFormat:@"conversation == %@", _conversation]
  22. #define CONVERSATION_UNREAD_PREDICATE [NSPredicate predicateWithFormat:@"conversation == %@ AND read == false AND isOwn == false", _conversation]
  23. #define CONVERSATION_MESSAGEID_PREDICATE [NSPredicate predicateWithFormat:@"conversation == %@ AND id == %@", _conversation, _messageId]
  24. @interface MessageFetcher ()
  25. @property Conversation *conversation;
  26. @property NSData *messageId;
  27. @property EntityFetcher *entityFetcher;
  28. @property NSFetchRequest *countFetchRequest;
  29. @property NSFetchRequest *messagesFetchRequest;
  30. @property NSFetchRequest *unreadMessagesFetchRequest;
  31. @property NSFetchRequest *messageIDFetchRequest;
  32. @property NSFetchRequest *allMessagesFetchRequest;
  33. @end
  34. @implementation MessageFetcher
  35. +(instancetype)messageFetcherFor:(Conversation *)conversation withEntityFetcher:(EntityFetcher *)entityFetcher {
  36. return [[MessageFetcher alloc] initWithConversation: conversation withEntityFetcher:entityFetcher];
  37. }
  38. - (instancetype)initWithConversation:(Conversation *)conversation withEntityFetcher:(EntityFetcher *)entityFetcher
  39. {
  40. self = [super init];
  41. if (self) {
  42. _conversation = conversation;
  43. _entityFetcher = entityFetcher;
  44. _orderAscending = YES;
  45. [self setupFetchRequests];
  46. }
  47. return self;
  48. }
  49. - (NSArray *) sortDescriptorsAscending:(BOOL)ascending {
  50. return @[
  51. [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:ascending],
  52. [NSSortDescriptor sortDescriptorWithKey:@"remoteSentDate" ascending:ascending]
  53. ];
  54. }
  55. - (void)setupFetchRequests {
  56. _countFetchRequest = [_entityFetcher fetchRequestForEntity:@"Message"];
  57. _countFetchRequest.predicate = CONVERSATION_PREDICATE;
  58. _messagesFetchRequest = [_entityFetcher fetchRequestForEntity:@"Message"];
  59. _messagesFetchRequest.predicate = CONVERSATION_PREDICATE;
  60. _messagesFetchRequest.sortDescriptors = [self sortDescriptorsAscending:_orderAscending];
  61. _unreadMessagesFetchRequest = [_entityFetcher fetchRequestForEntity:@"Message"];
  62. _unreadMessagesFetchRequest.predicate = CONVERSATION_UNREAD_PREDICATE;
  63. _unreadMessagesFetchRequest.sortDescriptors = [self sortDescriptorsAscending:NO];
  64. _allMessagesFetchRequest = [_entityFetcher fetchRequestForEntity:@"Message"];
  65. _allMessagesFetchRequest.predicate = CONVERSATION_PREDICATE;
  66. _allMessagesFetchRequest.sortDescriptors = [self sortDescriptorsAscending:_orderAscending];
  67. }
  68. - (void)setOrderAscending:(BOOL)orderAscending {
  69. if (_orderAscending != orderAscending) {
  70. _orderAscending = orderAscending;
  71. [self setupFetchRequests];
  72. }
  73. }
  74. - (NSArray *)messagesAtOffset:(NSInteger)offset count:(NSInteger)count {
  75. _messagesFetchRequest.fetchOffset = offset;
  76. _messagesFetchRequest.fetchLimit = count;
  77. return [_entityFetcher executeFetchRequest: _messagesFetchRequest];
  78. }
  79. - (NSArray *)unreadMessages {
  80. return [_entityFetcher executeFetchRequest: _unreadMessagesFetchRequest];
  81. }
  82. - (NSInteger)count {
  83. return [_entityFetcher executeCountFetchRequest:_countFetchRequest];
  84. }
  85. - (NSUInteger)indexForMessage:(NSData *)messageId {
  86. _messageId = messageId;
  87. if (_messageIDFetchRequest == nil) {
  88. _messageIDFetchRequest = [_entityFetcher fetchRequestForEntity:@"Message"];
  89. _messageIDFetchRequest.sortDescriptors = [self sortDescriptorsAscending:_orderAscending];
  90. }
  91. _messageIDFetchRequest.predicate = CONVERSATION_MESSAGEID_PREDICATE;
  92. NSArray *searchMessage = [_entityFetcher executeFetchRequest:_messageIDFetchRequest];
  93. if (searchMessage.count > 0) {
  94. NSArray *allMessages = [_entityFetcher executeFetchRequest:_allMessagesFetchRequest];
  95. return [allMessages indexOfObject:searchMessage.firstObject];
  96. }
  97. return 0;
  98. }
  99. - (BaseMessage *)lastMessage {
  100. NSFetchRequest *fetchRequest = [_entityFetcher fetchRequestForEntity:@"Message"];
  101. fetchRequest.predicate = CONVERSATION_PREDICATE;
  102. fetchRequest.sortDescriptors = [self sortDescriptorsAscending:NO];
  103. fetchRequest.fetchLimit = 1;
  104. NSArray *result = [_entityFetcher executeFetchRequest:fetchRequest];
  105. if (result != nil && [result count] > 0) {
  106. return [result objectAtIndex: 0];
  107. } else {
  108. return nil;
  109. }
  110. }
  111. - (NSArray *)last20Messages {
  112. NSFetchRequest *fetchRequest = [_entityFetcher fetchRequestForEntity:@"Message"];
  113. fetchRequest.predicate = CONVERSATION_PREDICATE;
  114. fetchRequest.sortDescriptors = [self sortDescriptorsAscending:NO];
  115. fetchRequest.fetchLimit = 20;
  116. NSArray *result = [_entityFetcher executeFetchRequest:fetchRequest];
  117. if (result != nil && [result count] > 0) {
  118. return result;
  119. } else {
  120. return nil;
  121. }
  122. }
  123. @end