-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathOSOutcomeEventsRepository.m
More file actions
165 lines (130 loc) · 7.47 KB
/
OSOutcomeEventsRepository.m
File metadata and controls
165 lines (130 loc) · 7.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
Modified MIT License
Copyright 2020 OneSignal
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
1. The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2. All copies of substantial portions of the Software may only be used in connection
with services provided by OneSignal.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#import <Foundation/Foundation.h>
#import "OSCachedUniqueOutcome.h"
#import "OSOutcomeEventsRepository.h"
#import "OSMacros.h"
@implementation OSOutcomeEventsRepository
- (id)initWithCache:(OSOutcomeEventsCache *)outcomeEventsCache {
self = [super init];
if (self) {
_outcomeEventsCache = outcomeEventsCache;
}
return self;
}
- (void)requestMeasureOutcomeEventWithAppId:(NSString *)appId
deviceType:(NSNumber *)deviceType
event:(OSOutcomeEventParams *)event
onSuccess:(OSResultSuccessBlock)successBlock
onFailure:(OSFailureBlock)failureBlock {
mustOverride();
}
/*
1. Iterate over all notifications and find the ones in the set that don't exist (ex. "<name> + "_" + <notificationId>"
2. Create an NSArray of these notificationIds and return it
3. If the array has notifications send the request for only these ids
*/
- (NSArray *)getNotCachedUniqueInfluencesForOutcome:(NSString *)name influences:(NSArray *)influences {
NSArray * attributedUniqueOutcomeEventSent = [self getAttributedUniqueOutcomeEventSent];
NSMutableArray *uniqueInfluences = [NSMutableArray new];
for (OSInfluence *influence in influences) {
NSMutableArray *availableInfluenceIds = [NSMutableArray new];
NSArray *influenceIds = influence.ids;
if (influenceIds == nil)
continue;
for (NSString *indentifier in influenceIds) {
OSCachedUniqueOutcome *uniqueOutcome = [[OSCachedUniqueOutcome new] initWithParamsName:name uniqueId:indentifier channel:influence.influenceChannel];
if (attributedUniqueOutcomeEventSent == nil || attributedUniqueOutcomeEventSent.count == 0) {
[availableInfluenceIds addObject:uniqueOutcome.uniqueId];
continue;
}
BOOL found = NO;
for (OSCachedUniqueOutcome *uniqueOutcomeSent in attributedUniqueOutcomeEventSent) {
if ([uniqueOutcome isEqual:uniqueOutcomeSent]) {
found = YES;
break;
}
}
// If the outcome hasn't been sent with this influence, then it should be included in the returned NSArray
if (!found)
[availableInfluenceIds addObject:uniqueOutcome.uniqueId];
}
if (availableInfluenceIds.count > 0) {
OSInfluence *newInfluence = [influence copy];
newInfluence.ids = availableInfluenceIds;
[uniqueInfluences addObject:newInfluence];
}
}
return uniqueInfluences;
}
- (NSArray<OSCachedUniqueOutcome *> *)getCachedUniqueOutcomesByChannel:(OSInfluenceChannel)channel
channelIds:(NSArray<NSString *> *)channelIds
outcomeName:(NSString *)outcomeName {
if (channelIds != nil) {
NSMutableArray *cachedUniqueOutcomes = [NSMutableArray new];
NSNumber *timestamp = [NSNumber numberWithDouble:[[NSDate date] timeIntervalSince1970]];
for (NSString * influenceId in channelIds) {
OSCachedUniqueOutcome * cachedUniqueOutcome = [[OSCachedUniqueOutcome alloc] initWithParamsName:outcomeName uniqueId:influenceId timestamp:timestamp channel:channel];
[cachedUniqueOutcomes addObject:cachedUniqueOutcome];
}
return cachedUniqueOutcomes;
}
return [NSArray new];
}
- (NSArray<OSCachedUniqueOutcome *> *)getCachedUniqueOutcomesFromSourceBody:(OSOutcomeSourceBody *)sourceBody
outcomeName:(NSString *)outcomeName {
if (sourceBody != nil) {
NSArray *iamIds = sourceBody.inAppMessagesIds;
NSArray *notificationIds = sourceBody.notificationIds;
NSArray<OSCachedUniqueOutcome *> * iamUniqueOutcomes = [self getCachedUniqueOutcomesByChannel:IN_APP_MESSAGE channelIds:iamIds outcomeName:outcomeName];
NSArray<OSCachedUniqueOutcome *> * notificationUniqueOutcomes = [self getCachedUniqueOutcomesByChannel:NOTIFICATION channelIds:notificationIds outcomeName:outcomeName];
return [iamUniqueOutcomes arrayByAddingObjectsFromArray:notificationUniqueOutcomes];
}
return [NSArray new];
}
- (void)saveUniqueOutcomeEventParams:(OSOutcomeEventParams *)eventParams {
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:[NSString stringWithFormat:@"OSOutcomeEventsRepository saveUniqueOutcomeEventParams: %@", eventParams.description]];
if (eventParams.outcomeSource == nil)
return;
NSString *outcomeName = eventParams.outcomeId;
OSOutcomeSourceBody *directBody = eventParams.outcomeSource.directBody;
OSOutcomeSourceBody *indirectBody = eventParams.outcomeSource.indirectBody;
NSArray<OSCachedUniqueOutcome *> *directIds = [self getCachedUniqueOutcomesFromSourceBody:directBody outcomeName:outcomeName];
NSArray<OSCachedUniqueOutcome *> *indirectIds = [self getCachedUniqueOutcomesFromSourceBody:indirectBody outcomeName:outcomeName];
NSArray<OSCachedUniqueOutcome *> *newAttributedIds = [directIds arrayByAddingObjectsFromArray:indirectIds];
NSArray<OSCachedUniqueOutcome *> *attributedUniqueOutcomeEventSent = [self getAttributedUniqueOutcomeEventSent];
NSArray *finalAttributedUniqueOutcomeEventSent = [attributedUniqueOutcomeEventSent arrayByAddingObjectsFromArray:newAttributedIds];
[self saveAttributedUniqueOutcomeEventNotificationIds:finalAttributedUniqueOutcomeEventSent];
}
- (NSSet *)getUnattributedUniqueOutcomeEventsSent {
return [_outcomeEventsCache getUnattributedUniqueOutcomeEventsSent];
}
- (void)saveUnattributedUniqueOutcomeEventsSent:(NSSet *)unattributedUniqueOutcomeEventsSentSet {
[_outcomeEventsCache saveUnattributedUniqueOutcomeEventsSent:unattributedUniqueOutcomeEventsSentSet];
}
- (NSArray *)getAttributedUniqueOutcomeEventSent {
return [_outcomeEventsCache getAttributedUniqueOutcomeEventSent];
}
- (void)saveAttributedUniqueOutcomeEventNotificationIds:(NSArray *)attributedUniqueOutcomeEventNotificationIdsSent {
[_outcomeEventsCache saveAttributedUniqueOutcomeEventNotificationIds:attributedUniqueOutcomeEventNotificationIdsSent];
}
@end