NCContact.m 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. #import "NCContact.h"
  6. #import "ABContact.h"
  7. #import "NCUser.h"
  8. @implementation NCContact
  9. + (instancetype)contactWithIdentifier:(NSString *)identifier cloudId:(NSString *)cloudId lastUpdate:(NSInteger)lastUpdate andAccountId:(NSString *)accountId
  10. {
  11. NCContact *contact = [[NCContact alloc] init];
  12. contact.identifier = identifier;
  13. contact.cloudId = cloudId;
  14. contact.lastUpdate = lastUpdate;
  15. contact.accountId = accountId;
  16. contact.internalId = [NSString stringWithFormat:@"%@@%@", contact.accountId, contact.identifier];
  17. return contact;
  18. }
  19. + (void)updateContact:(NCContact *)managedContact withContact:(NCContact *)contact
  20. {
  21. managedContact.cloudId = contact.cloudId;
  22. managedContact.lastUpdate = contact.lastUpdate;
  23. }
  24. - (NSString *)userId
  25. {
  26. if (self.cloudId) {
  27. NSArray *components = [self.cloudId componentsSeparatedByString:@"@"];
  28. if (components.count > 1) {
  29. NSString *userId = components[0];
  30. // If there are more than 2 components grab everything as userId until last separator.
  31. if (components.count > 2) {
  32. for (NSInteger i = 1; i <= components.count - 2; i++) {
  33. userId = [userId stringByAppendingString:[NSString stringWithFormat:@"@%@", components[i]]];
  34. }
  35. }
  36. return userId;
  37. }
  38. }
  39. return nil;
  40. }
  41. - (NSString *)name
  42. {
  43. if (self.identifier) {
  44. ABContact *unmanagedABContact = nil;
  45. ABContact *managedABContact = [ABContact objectsWhere:@"identifier = %@", self.identifier].firstObject;
  46. if (managedABContact) {
  47. unmanagedABContact = [[ABContact alloc] initWithValue:managedABContact];
  48. }
  49. NSString *contactDisplayName = unmanagedABContact.name;
  50. if (!contactDisplayName || [contactDisplayName isEqualToString:@""]) {
  51. // If the address book contact was stored without name return
  52. // the first phone number (it should have at least one phone number) as display name.
  53. contactDisplayName = unmanagedABContact.phoneNumbers.firstObject;
  54. }
  55. return contactDisplayName;
  56. }
  57. return nil;
  58. }
  59. + (NSArray<NCUser *> *)contactsForAccountId:(NSString *)accountId contains:(NSString *)searchString
  60. {
  61. RLMResults *managedContacts = [NCContact objectsWhere:@"accountId = %@", accountId];
  62. NSMutableArray *filteredContacts = nil;
  63. // Create an unmanaged copy of the stored contacts
  64. NSMutableArray *contacts = [NSMutableArray new];
  65. for (NCContact *managedContact in managedContacts) {
  66. NCContact *contact = [[NCContact alloc] initWithValue:managedContact];
  67. NCUser *user = [NCUser userFromNCContact:contact];
  68. [contacts addObject:user];
  69. }
  70. filteredContacts = contacts;
  71. if (searchString && ![searchString isEqualToString:@""]) {
  72. NSString *filter = @"%K CONTAINS[cd] %@ || %K CONTAINS[cd] %@";
  73. NSArray* args = @[@"name", searchString, @"userId", searchString];
  74. NSPredicate* predicate = [NSPredicate predicateWithFormat:filter argumentArray:args];
  75. filteredContacts = [[NSMutableArray alloc] initWithArray:[contacts filteredArrayUsingPredicate:predicate]];
  76. }
  77. return filteredContacts;
  78. }
  79. @end