/
QBChatMessage+QMCustomParameters.m
243 lines (167 loc) · 7.15 KB
/
QBChatMessage+QMCustomParameters.m
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//
// QBChatAbstractMessage+QMCustomParameters.h
// QMServices
//
// Created by Andrey Ivanov on 24.07.14.
// Copyright (c) 2015 Quickblox. All rights reserved.
//
#import "QBChatMessage+QMCustomParameters.h"
#import <objc/runtime.h>
/*Message keys*/
NSString const *kQMCustomParameterSaveToHistory = @"save_to_history";
NSString const *kQMCustomParameterMessageType = @"notification_type";
NSString const *kQMCustomParameterChatMessageID = @"chat_message_id";
NSString const *kQMCustomParameterDateSent = @"date_sent";
NSString const *kQMCustomParameterChatMessageDeliveryStatus = @"message_delivery_status_read";
/*Dialogs keys*/
NSString const *kQMCustomParameterDialogID = @"dialog_id";
NSString const *kQMCustomParameterRoomJID = @"room_jid";
NSString const *kQMCustomParameterDialogRoomName = @"room_name";
NSString const *kQMCustomParameterDialogRoomPhoto = @"room_photo";
NSString const *kQMCustomParameterDialogRoomLastMessageDate = @"room_last_message_date";
NSString const *kQMCustomParameterDialogUpdatedDate = @"dialog_updated_date";
NSString const *kQMCustomParameterDialogType = @"type";
NSString const *kQMCustomParameterDialogOccupantsIDs = @"occupants_ids";
NSString const *kQMCustomParameterDialogDeletedID = @"deleted_id";
NSString const *kQMCustomParameterDialogRoomUpdatedDate = @"room_updated_date";
@interface QBChatMessage (Context)
@property (strong, nonatomic) NSMutableDictionary *context;
@property (strong, nonatomic) QBChatDialog *tDialog;
@end
@implementation QBChatMessage (QMCustomParameters)
/*Message params*/
@dynamic saveToHistory;
@dynamic messageType;
@dynamic chatMessageID;
@dynamic customDateSent;
@dynamic messageDeliveryStatus;
@dynamic dialog;
@dynamic attachmentStatus;
- (NSMutableDictionary *)context {
if (!self.customParameters) {
self.customParameters = [NSMutableDictionary dictionary];
}
return self.customParameters;
}
- (QBChatDialog *)dialog {
if (!self.tDialog) {
self.tDialog = [[QBChatDialog alloc] init];
//Grap custom parameters;
self.tDialog.ID = self.context[kQMCustomParameterDialogID];
self.tDialog.roomJID = self.context[kQMCustomParameterRoomJID];
self.tDialog.type = [self.context[kQMCustomParameterDialogType] intValue];
NSAssert(self.tDialog.type != 0, @"dialog type is undefined");
self.tDialog.name = self.context[kQMCustomParameterDialogRoomName];
NSString *lastMessageDateTimeInterval = self.context[kQMCustomParameterDialogRoomLastMessageDate];
if (lastMessageDateTimeInterval) {
self.tDialog.lastMessageDate = [NSDate dateWithTimeIntervalSince1970:[lastMessageDateTimeInterval floatValue]];
}
NSString * strIDs = self.context[kQMCustomParameterDialogOccupantsIDs];
NSArray *componets = [strIDs componentsSeparatedByString:@","];
NSMutableArray *occupatnsIDs = [NSMutableArray arrayWithCapacity:componets.count];
for (NSString *occupantID in componets) {
[occupatnsIDs addObject:@(occupantID.integerValue)];
}
self.tDialog.occupantIDs = occupatnsIDs;
}
return self.tDialog;
}
- (QBChatDialog *)tDialog {
return objc_getAssociatedObject(self, @selector(tDialog));
}
- (void)setTDialog:(QBChatDialog *)tDialog {
objc_setAssociatedObject(self, @selector(tDialog), tDialog, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (QMMessageAttachmentStatus)attachmentStatus {
return [[self tAttachmentStatus] integerValue];
}
- (void)setAttachmentStatus:(QMMessageAttachmentStatus)attachmentStatus {
[self setTAttachmentStatus:@(attachmentStatus)];
}
- (NSNumber *)tAttachmentStatus {
return objc_getAssociatedObject(self, @selector(tAttachmentStatus));
}
- (void)setTAttachmentStatus:(NSNumber *)attachmentStatusNumber {
objc_setAssociatedObject(self, @selector(tAttachmentStatus), attachmentStatusNumber, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)updateCustomParametersWithDialog:(QBChatDialog *)dialog {
self.tDialog = nil;
self.context[kQMCustomParameterDialogID] = dialog.ID;
self.context[kQMCustomParameterDialogType] = @(dialog.type);
if (dialog.lastMessageDate != nil){
NSTimeInterval lastMessageDateTimeInterval = [dialog.lastMessageDate timeIntervalSince1970];
self.context[kQMCustomParameterDialogRoomLastMessageDate] = [@(lastMessageDateTimeInterval) stringValue];
}
if (dialog.type == QBChatDialogTypeGroup) {
if (dialog.photo != nil) {
self.context[kQMCustomParameterDialogRoomPhoto] = dialog.photo;
}
if (dialog.name != nil) {
self.context[kQMCustomParameterDialogRoomName] = dialog.name;
}
if (dialog.roomJID != nil ){
self.context[kQMCustomParameterRoomJID] = dialog.roomJID;
}
NSTimeInterval nowDateTimeInterval = [[NSDate date] timeIntervalSince1970];
self.context[kQMCustomParameterDialogRoomUpdatedDate] = [@(nowDateTimeInterval) stringValue];
NSString *strIDs = [dialog.occupantIDs componentsJoinedByString:@","];
self.context[kQMCustomParameterDialogOccupantsIDs] = strIDs;
}
}
- (void)setDialogOccupantsIDs:(NSArray *)dialogOccupantsIDs {
NSString *strIDs = [dialogOccupantsIDs componentsJoinedByString:@","];
self.context[kQMCustomParameterDialogOccupantsIDs] = strIDs;
}
- (NSArray *)dialogOccupantsIDs {
NSString *strIDs = self.context[kQMCustomParameterDialogOccupantsIDs];
NSArray *componets = [strIDs componentsSeparatedByString:@","];
NSMutableArray *result = [NSMutableArray arrayWithCapacity:componets.count];
for (NSString *occupantID in componets) {
[result addObject:@(occupantID.integerValue)];
}
return result;
}
#pragma mark - cParamChatMessageID
- (void)setChatMessageID:(NSString *)chatMessageID {
self.context[kQMCustomParameterChatMessageID] = chatMessageID;
}
- (NSString *)chatMessageID {
return self.context[kQMCustomParameterChatMessageID];
}
#pragma mark - dateSent
- (void)setCustomDateSent:(NSNumber *)dateSent {
self.context[kQMCustomParameterDateSent] = dateSent;
}
- (NSNumber *)customDateSent {
return self.context[kQMCustomParameterDateSent];
}
#pragma mark - cParamSaveToHistory
- (void)setSaveToHistory:(NSString *)saveToHistory {
self.context[kQMCustomParameterSaveToHistory] = saveToHistory;
}
- (NSString *)saveToHistory {
return self.context[kQMCustomParameterSaveToHistory];
}
#pragma mark - dialogDeletedID
- (void)setDialogDeletedID:(NSNumber *)dialogDeletedID {
self.context[kQMCustomParameterDialogDeletedID] = dialogDeletedID;
}
- (NSNumber *)dialogDeletedID {
return self.context[kQMCustomParameterDialogDeletedID];
}
#pragma mark - messageType
- (void)setMessageType:(QMMessageType)messageType {
if (messageType != QMMessageTypeText) {
self.context[kQMCustomParameterMessageType] = @(messageType);
}
}
- (QMMessageType)messageType {
return [self.context[kQMCustomParameterMessageType] integerValue];
}
- (BOOL)isNotificatonMessage {
return self.messageType != QMMessageTypeText;
}
- (BOOL)isMediaMessage {
return self.attachments.count > 0;
}
@end