aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firebase/Messaging/FIRMessagingRmq2PersistentStore.h
blob: 09f1d4468d29c8603063dc5733d1ec24fa536f2a (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * Copyright 2017 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import <Foundation/Foundation.h>

@class FIRMessagingPersistentSyncMessage;

// table data handlers
/**
 *  Handle message stored in the outgoing RMQ messages table.
 *
 *  @param rmqId The rmqID of the message.
 *  @param tag   The message tag.
 *  @param data  The data stored in the message.
 */
typedef void(^FCMOutgoingRmqMessagesTableHandler)(int64_t rmqId, int8_t tag, NSData *data);

/// Outgoing messages RMQ table
extern NSString *const kTableOutgoingRmqMessages;
/// Server to device RMQ table
extern NSString *const kTableS2DRmqIds;

@interface FIRMessagingRmq2PersistentStore : NSObject

/**
 *  Initialize and open the RMQ database on the client.
 *
 *  @param databaseName The name for RMQ database.
 *
 *  @return A store used to persist messages on the client.
 */
- (instancetype)initWithDatabaseName:(NSString *)databaseName;

/**
 *  Save outgoing message in RMQ.
 *
 *  @param rmqId The rmqID for the message.
 *  @param tag   The tag of the message proto.
 *  @param data  The data being sent in the message.
 *  @param error The error if any while saving the message to the persistent store.
 *
 *  @return YES if the message was successfully saved to the persistent store else NO.
 */
- (BOOL)saveMessageWithRmqId:(int64_t)rmqId
                         tag:(int8_t)tag
                        data:(NSData *)data
                       error:(NSError **)error;

/**
 *  Add unacked server to device message with a given rmqID to the persistent store.
 *
 *  @param rmqId The rmqID of the message that was not acked by the cient.
 *
 *  @return YES if the save was successful else NO.
 */
- (BOOL)saveUnackedS2dMessageWithRmqId:(NSString *)rmqId;

/**
 *  Update the last RMQ ID that was sent by the client.
 *
 *  @param rmqID The latest rmqID sent by the device.
 *
 *  @return YES if the last rmqID was successfully saved else NO.
 */
- (BOOL)updateLastOutgoingRmqId:(int64_t)rmqID;

#pragma mark - Query

/**
 *  Query the highest rmqID saved in the Outgoing messages table.
 *
 *  @return The highest rmqID amongst all the messages in the Outgoing RMQ table. If no message
 *          was ever persisted return 0.
 */
- (int64_t)queryHighestRmqId;

/**
 *  The last rmqID that was saved on the client.
 *
 *  @return The last rmqID that was saved. If no rmqID was ever persisted return 0.
 */
- (int64_t)queryLastRmqId;

/**
 *  Get a list of all unacked server to device messages stored on the client.
 *
 *  @return List of all unacked s2d messages in the persistent store.
 */
- (NSArray *)unackedS2dRmqIds;

/**
 *  Iterate over all outgoing messages in the RMQ table.
 *
 *  @param handler The handler invoked with each message in the outgoing RMQ table.
 */
- (void)scanOutgoingRmqMessagesWithHandler:(FCMOutgoingRmqMessagesTableHandler)handler;

#pragma mark - Delete

/**
 *  Delete messages with given rmqID's from a table.
 *
 *  @param tableName The table name from which to delete the rmq messages.
 *  @param rmqIds    The rmqID's of the messages to be deleted.
 *
 *  @return The number of messages that were successfully deleted.
 */
- (int)deleteMessagesFromTable:(NSString *)tableName
                    withRmqIds:(NSArray *)rmqIds;

/**
 *  Remove database from the device.
 *
 *  @param dbName The database name to be deleted.
 */
+ (void)removeDatabase:(NSString *)dbName;

#pragma mark - Sync Messages

/**
 *  Save sync message to persistent store to check for duplicates.
 *
 *  @param rmqID          The rmqID of the message to save.
 *  @param expirationTime The expiration time of the message to save.
 *  @param apnsReceived   YES if the message was received via APNS else NO.
 *  @param mcsReceived    YES if the message was received via MCS else NO.
 *  @param error          The error if any while saving the message to store.
 *
 *  @return YES if the message was saved successfully else NO.
 */
- (BOOL)saveSyncMessageWithRmqID:(NSString *)rmqID
                  expirationTime:(int64_t)expirationTime
                    apnsReceived:(BOOL)apnsReceived
                     mcsReceived:(BOOL)mcsReceived
                           error:(NSError **)error;

/**
 *  Update sync message received via APNS.
 *
 *  @param rmqID The rmqID of the sync message.
 *  @param error The error if any while updating the sync message in persistence.
 *
 *  @return YES if the update was successful else NO.
 */
- (BOOL)updateSyncMessageViaAPNSWithRmqID:(NSString *)rmqID
                                    error:(NSError **)error;

/**
 *  Update sync message received via MCS.
 *
 *  @param rmqID The rmqID of the sync message.
 *  @param error The error if any while updating the sync message in persistence.
 *
 *  @return YES if the update was successful else NO.
 */
- (BOOL)updateSyncMessageViaMCSWithRmqID:(NSString *)rmqID
                                   error:(NSError **)error;

/**
 *  Query sync message table for a given rmqID.
 *
 *  @param rmqID The rmqID to search for in SYNC_RMQ.
 *
 *  @return The sync message that was persisted with `rmqID`. If no such message was persisted
 *          return nil.
 */
- (FIRMessagingPersistentSyncMessage *)querySyncMessageWithRmqID:(NSString *)rmqID;

/**
 *  Delete sync message with rmqID.
 *
 *  @param rmqID The rmqID of the message to delete.
 *
 *  @return YES if a sync message with rmqID was found and deleted successfully else NO.
 */
- (BOOL)deleteSyncMessageWithRmqID:(NSString *)rmqID;

/**
 *  Delete the expired sync messages from persisten store. Also deletes messages that have been
 *  delivered both via APNS and MCS.
 *
 *  @param error The error if any while deleting the messages.
 *
 *  @return The total number of messages that were deleted from the persistent store.
 */
- (int)deleteExpiredOrFinishedSyncMessages:(NSError **)error;

@end