IdentityInfoFetcher.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2019-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 "IdentityInfoFetcher.h"
  21. #import "ServerAPIConnector.h"
  22. #ifdef DEBUG
  23. static const DDLogLevel ddLogLevel = DDLogLevelInfo;
  24. #else
  25. static const DDLogLevel ddLogLevel = DDLogLevelWarning;
  26. #endif
  27. @implementation IdentityInfoFetcher {
  28. NSMutableDictionary *prefetchCache;
  29. NSMutableDictionary *pendingFetchCompletionHandlersByIdentity;
  30. NSMutableDictionary *pendingFetchErrorHandlersByIdentity;
  31. dispatch_queue_t queue;
  32. }
  33. + (IdentityInfoFetcher*)sharedIdentityInfoFetcher {
  34. static IdentityInfoFetcher *instance;
  35. @synchronized (self) {
  36. if (!instance)
  37. instance = [[IdentityInfoFetcher alloc] init];
  38. }
  39. return instance;
  40. }
  41. - (id)init
  42. {
  43. self = [super init];
  44. if (self) {
  45. queue = dispatch_queue_create("ch.threema.identityinfofetcher", DISPATCH_QUEUE_SERIAL);
  46. prefetchCache = [NSMutableDictionary dictionary];
  47. pendingFetchCompletionHandlersByIdentity = [NSMutableDictionary dictionary];
  48. pendingFetchErrorHandlersByIdentity = [NSMutableDictionary dictionary];
  49. }
  50. return self;
  51. }
  52. - (void)prefetchIdentityInfo:(NSSet*)identities onCompletion:(void(^)(void))onCompletion onError:(void(^)(NSError *error))onError {
  53. ServerAPIConnector *apiConnector = [[ServerAPIConnector alloc] init];
  54. [apiConnector fetchBulkIdentityInfo:[identities allObjects] onCompletion:^(NSArray *identities, NSArray *publicKeys, NSArray *featureMasks, NSArray *states, NSArray *types) {
  55. dispatch_async(queue, ^{
  56. // Add results to cache
  57. for (int i = 0; i < identities.count; i++) {
  58. [prefetchCache setObject:@{
  59. @"publicKey": publicKeys[i],
  60. @"featureMask": featureMasks[i],
  61. @"state": states[i],
  62. @"type": types[i]
  63. } forKey:identities[i]];
  64. }
  65. onCompletion();
  66. });
  67. } onError:^(NSError *error) {
  68. onError(error);
  69. }];
  70. }
  71. - (void)fetchIdentityInfoFor:(NSString*)identity onCompletion:(void(^)(NSData *publicKey, NSNumber *state, NSNumber *type, NSNumber *featureMask))onCompletion onError:(void(^)(NSError *error))onError {
  72. dispatch_async(queue, ^{
  73. // Check prefetch cache
  74. NSDictionary *cachedIdentityInfo = [prefetchCache objectForKey:identity];
  75. if (cachedIdentityInfo != nil) {
  76. onCompletion(cachedIdentityInfo[@"publicKey"], cachedIdentityInfo[@"state"], cachedIdentityInfo[@"type"], cachedIdentityInfo[@"featureMask"]);
  77. return;
  78. }
  79. // Not in prefetch cache - fetch now
  80. if (pendingFetchCompletionHandlersByIdentity[identity]) {
  81. // A fetch request is already pending for this identity.
  82. // Store the completion/error blocks and call them later to avoid fetching multiple times in parallel.
  83. [pendingFetchCompletionHandlersByIdentity[identity] addObject:[onCompletion copy]];
  84. [pendingFetchErrorHandlersByIdentity[identity] addObject:[onError copy]];
  85. } else {
  86. // No other fetch pending for this identity
  87. pendingFetchCompletionHandlersByIdentity[identity] = [NSMutableArray arrayWithObject:[onCompletion copy]];
  88. pendingFetchErrorHandlersByIdentity[identity] = [NSMutableArray arrayWithObject:[onError copy]];
  89. ServerAPIConnector *apiConnector = [[ServerAPIConnector alloc] init];
  90. DDLogVerbose(@"Fetching identity %@ from server", identity);
  91. [apiConnector fetchIdentityInfo:identity onCompletion:^(NSData *publicKey, NSNumber *state, NSNumber *type, NSNumber *featureMask) {
  92. dispatch_async(queue, ^{
  93. for (void(^completionHandler)(NSData*, NSNumber*, NSNumber*, NSNumber*) in pendingFetchCompletionHandlersByIdentity[identity]) {
  94. completionHandler(publicKey, state, type, featureMask);
  95. }
  96. [pendingFetchCompletionHandlersByIdentity removeObjectForKey:identity];
  97. [pendingFetchErrorHandlersByIdentity removeObjectForKey:identity];
  98. });
  99. } onError:^(NSError *error) {
  100. dispatch_async(queue, ^{
  101. for (void(^errorHandler)(NSError *) in pendingFetchErrorHandlersByIdentity[identity]) {
  102. errorHandler(error);
  103. }
  104. [pendingFetchCompletionHandlersByIdentity removeObjectForKey:identity];
  105. [pendingFetchErrorHandlersByIdentity removeObjectForKey:identity];
  106. });
  107. }];
  108. }
  109. });
  110. }
  111. @end