NCUser.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. #import "NCUser.h"
  6. NSString * const kParticipantTypeUser = @"users";
  7. NSString * const kParticipantTypeGroup = @"groups";
  8. NSString * const kParticipantTypeEmail = @"emails";
  9. NSString * const kParticipantTypeCircle = @"circles";
  10. NSString * const kParticipantTypeFederated = @"federated_users";
  11. @implementation NCUser
  12. + (instancetype)userWithDictionary:(NSDictionary *)userDict
  13. {
  14. if (!userDict) {
  15. return nil;
  16. }
  17. NCUser *user = [[NCUser alloc] init];
  18. id userId = [userDict objectForKey:@"id"];
  19. if ([userId isKindOfClass:[NSString class]]) {
  20. user.userId = userId;
  21. } else {
  22. user.userId = [userId stringValue];
  23. }
  24. id name = [userDict objectForKey:@"label"];
  25. if ([name isKindOfClass:[NSString class]]) {
  26. user.name = name;
  27. } else {
  28. user.name = [name stringValue];
  29. }
  30. id source = [userDict objectForKey:@"source"];
  31. if ([source isKindOfClass:[NSString class]]) {
  32. user.source = source;
  33. } else {
  34. user.source = [source stringValue];
  35. }
  36. if ([user.source isEqualToString:@"remotes"]) {
  37. user.source = kParticipantTypeFederated;
  38. }
  39. return user;
  40. }
  41. + (instancetype)userFromNCContact:(NCContact *)contact
  42. {
  43. if (!contact) {
  44. return nil;
  45. }
  46. NCUser *user = [[NCUser alloc] init];
  47. user.name = contact.name;
  48. user.userId = contact.userId;
  49. user.source = kParticipantTypeUser;
  50. return user;
  51. }
  52. + (NSDictionary<NSString *, NSArray<NCUser *> *> *)indexedUsersFromUsersArray:(NSArray *)users
  53. {
  54. NSMutableDictionary *indexedUsers = [[NSMutableDictionary alloc] init];
  55. for (NCUser *user in users) {
  56. NSString *index = [[user.name substringToIndex:1] uppercaseString];
  57. NSRange first = [user.name rangeOfComposedCharacterSequenceAtIndex:0];
  58. NSRange match = [user.name rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet] options:0 range:first];
  59. if (match.location == NSNotFound) {
  60. index = @"#";
  61. }
  62. NSMutableArray *usersForIndex = [indexedUsers valueForKey:index];
  63. if (usersForIndex == nil) {
  64. usersForIndex = [[NSMutableArray alloc] init];
  65. }
  66. [usersForIndex addObject:user];
  67. [indexedUsers setObject:usersForIndex forKey:index];
  68. }
  69. return indexedUsers;
  70. }
  71. + (NSArray<NCUser *> *)combineUsersArray:(NSArray *)firstArray withUsersArray:(NSArray *)secondArray
  72. {
  73. // Add first array of users
  74. NSMutableArray *combinedUserArray = [[NSMutableArray alloc] initWithArray:firstArray];
  75. // Remove first array users from second array
  76. NSMutableArray *filteredSecondUserArray = [[NSMutableArray alloc] init];
  77. for (NCUser *secondArrayUser in secondArray) {
  78. BOOL duplicate = NO;
  79. for (NCUser *user in combinedUserArray) {
  80. if ([secondArrayUser.userId isEqualToString:user.userId] && [secondArrayUser.source isEqualToString:kParticipantTypeUser]) {
  81. duplicate = YES;
  82. break;
  83. }
  84. }
  85. if (!duplicate) {
  86. [filteredSecondUserArray addObject:secondArrayUser];
  87. }
  88. }
  89. // Combine both arrays
  90. [combinedUserArray addObjectsFromArray:filteredSecondUserArray];
  91. return combinedUserArray;
  92. }
  93. @end