1// Copyright 2018-present 650 Industries. All rights reserved.
2
3#import "EXScopedNotificationSerializer.h"
4
5NS_ASSUME_NONNULL_BEGIN
6
7@implementation EXScopedNotificationSerializer
8
9+ (NSDictionary *)serializedNotificationResponse:(UNNotificationResponse *)response
10{
11  NSDictionary *serializedResponse = [super serializedNotificationResponse:response];
12  NSMutableDictionary *serializedResponseMutable = [serializedResponse mutableCopy];
13  serializedResponseMutable[@"notification"] = [self serializedNotification:response.notification];
14
15  return [serializedResponseMutable copy];
16}
17
18+ (NSDictionary *)serializedNotification:(UNNotification *)notification
19{
20  NSDictionary *serializedNotification = [super serializedNotification:notification];
21  NSMutableDictionary *serializedNotificationMutable = [serializedNotification mutableCopy];
22  serializedNotificationMutable[@"request"] = [self serializedNotificationRequest:notification.request];
23
24  return [serializedNotificationMutable copy];
25}
26
27+ (NSDictionary *)serializedNotificationRequest:(UNNotificationRequest *)request
28{
29  NSDictionary *serializedRequest = [super serializedNotificationRequest:request];
30  NSMutableDictionary *serializedRequestMutable = [serializedRequest mutableCopy];
31  serializedRequestMutable[@"content"] = [self serializedNotificationContent:request.content isRemote:[request.trigger isKindOfClass:[UNPushNotificationTrigger class]]];
32
33  return [serializedRequestMutable copy];
34}
35
36+ (NSDictionary *)serializedNotificationContent:(UNNotificationContent *)content isRemote:(BOOL)isRemote
37{
38  NSDictionary *serializedContent = [super serializedNotificationContent:content isRemote:isRemote];
39  NSMutableDictionary *serializedContentMutable = [serializedContent mutableCopy];
40  serializedContentMutable[@"categoryIdentifier"] = content.categoryIdentifier ? [self removeCategoryIdentifierPrefix: content.categoryIdentifier userInfo:content.userInfo] : [NSNull null];
41
42  return [serializedContentMutable copy];
43}
44
45+ (NSString *)removeCategoryIdentifierPrefix:(NSString *)identifier userInfo:(NSDictionary *)userInfo
46{
47  NSString *experienceId = userInfo[@"experienceId"] ?: [NSNull null];
48  if (experienceId) {
49    NSString *scopedCategoryPrefix = [NSString stringWithFormat:@"%@-", experienceId];
50    return [identifier stringByReplacingOccurrencesOfString:scopedCategoryPrefix withString:@""];
51  }
52  return identifier;
53}
54
55@end
56
57NS_ASSUME_NONNULL_END
58