NCNotification.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. #import "NCNotification.h"
  6. #import "NextcloudTalk-Swift.h"
  7. @implementation NCNotification
  8. + (instancetype)notificationWithDictionary:(NSDictionary *)notificationDict
  9. {
  10. if (!notificationDict || ![notificationDict isKindOfClass:[NSDictionary class]]) {
  11. return nil;
  12. }
  13. NCNotification *notification = [[NCNotification alloc] init];
  14. notification.notificationId = [[notificationDict objectForKey:@"notification_id"] integerValue];
  15. notification.app = [notificationDict objectForKey:@"app"];
  16. notification.objectId = [notificationDict objectForKey:@"object_id"];
  17. notification.objectType = [notificationDict objectForKey:@"object_type"];
  18. notification.subject = [notificationDict objectForKey:@"subject"];
  19. notification.subjectRich = [notificationDict objectForKey:@"subjectRich"];
  20. notification.subjectRichParameters = [notificationDict objectForKey:@"subjectRichParameters"];
  21. notification.message = [notificationDict objectForKey:@"message"];
  22. notification.messageRich = [notificationDict objectForKey:@"messageRich"];
  23. notification.messageRichParameters = [notificationDict objectForKey:@"messageRichParameters"];
  24. notification.actions = [notificationDict objectForKey:@"actions"];
  25. NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init];
  26. notification.datetime = [formatter dateFromString:[notificationDict objectForKey:@"datetime"]];
  27. if (![notification.subjectRichParameters isKindOfClass:[NSDictionary class]]) {
  28. notification.subjectRichParameters = @{};
  29. }
  30. if (![notification.messageRichParameters isKindOfClass:[NSDictionary class]]) {
  31. notification.messageRichParameters = @{};
  32. }
  33. return notification;
  34. }
  35. - (NCNotificationType)notificationType
  36. {
  37. NCNotificationType type = kNCNotificationTypeRoom;
  38. if ([_objectType isEqualToString:@"chat"]) {
  39. type = kNCNotificationTypeChat;
  40. } else if ([_objectType isEqualToString:@"recording"]) {
  41. type = kNCNotificationTypeRecording;
  42. } else if ([_objectType isEqualToString:@"call"]) {
  43. type = kNCNotificationTypeCall;
  44. } else if ([_objectType isEqualToString:@"remote_talk_share"]) {
  45. type = kNCNotificationTypeFederation;
  46. }
  47. return type;
  48. }
  49. - (NSString *)roomToken
  50. {
  51. // Starting with NC 24 objectId additionally contains the messageId: "{roomToken}/{messageId}"
  52. if ([_objectId containsString:@"/"]) {
  53. NSArray *objectIdComponents = [_objectId componentsSeparatedByString:@"/"];
  54. return objectIdComponents[0];
  55. }
  56. return _objectId;
  57. }
  58. - (NSString *)chatMessageAuthor
  59. {
  60. NSString *author = [[_subjectRichParameters objectForKey:@"user"] objectForKey:@"name"];
  61. NSString *guest = [[_subjectRichParameters objectForKey:@"guest"] objectForKey:@"name"];
  62. if (guest) {
  63. author = [NSString stringWithFormat:@"%@ (%@)", guest, NSLocalizedString(@"guest", nil)];
  64. }
  65. return author ? author : NSLocalizedString(@"Guest", nil);
  66. }
  67. - (NSString *)chatMessageTitle
  68. {
  69. NSString *title = [self chatMessageAuthor];
  70. // Check if the room has a name
  71. NSArray *parameters = [self getParametersFromRichText:_subjectRich];
  72. for (int i = 0; i < parameters.count; i++) {
  73. NSTextCheckingResult *match = [parameters objectAtIndex:i];
  74. NSString* parameter = [_subjectRich substringWithRange:match.range];
  75. NSString *parameterKey = [[parameter stringByReplacingOccurrencesOfString:@"{" withString:@""]
  76. stringByReplacingOccurrencesOfString:@"}" withString:@""];
  77. if ([parameterKey isEqualToString:@"reaction"]) {
  78. return _subject;
  79. }
  80. if ([parameterKey isEqualToString:@"call"]) {
  81. NSString *inString = NSLocalizedString(@"in", nil);
  82. title = [title stringByAppendingString:[NSString stringWithFormat:@" %@ %@", inString, [[_subjectRichParameters objectForKey:@"call"] objectForKey:@"name"]]];
  83. }
  84. }
  85. return title;
  86. }
  87. - (NSArray *)getParametersFromRichText:(NSString *)text
  88. {
  89. NSError *error = nil;
  90. NSRegularExpression *parameterRegex = [NSRegularExpression regularExpressionWithPattern:@"\\{([^}]+)\\}" options:NSRegularExpressionCaseInsensitive error:&error];
  91. return [parameterRegex matchesInString:text options:0 range:NSMakeRange(0, [text length])];
  92. }
  93. - (NSArray *)notificationActions
  94. {
  95. if (!self.actions) {
  96. return nil;
  97. }
  98. NSMutableArray *resultActions = [[NSMutableArray alloc] init];
  99. for (NSDictionary *dict in self.actions) {
  100. NCNotificationAction *notificationAction = [[NCNotificationAction alloc] initWithDictionary:dict];
  101. [resultActions addObject:notificationAction];
  102. }
  103. return resultActions;
  104. }
  105. @end