-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SlackContactPhotoProvider.m
52 lines (44 loc) · 2.29 KB
/
SlackContactPhotoProvider.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
#import <sqlite3.h>
#import "FolderFinder.h"
#import "SlackContactPhotoProvider.h"
@implementation SlackContactPhotoProvider
- (DDNotificationContactPhotoPromiseOffer *)contactPhotoPromiseOfferForNotification:(DDUserNotification *)notification {
NSDictionary *alert = [notification applicationUserInfo];
NSString *body = alert[@"aps"][@"alert"][@"body"];
NSString *threadId = alert[@"aps"][@"thread-id"];
NSString *messageType = [threadId substringToIndex:1];
NSString *teamId = [notification.applicationUserInfo valueForKeyPath:@"team_id"];
NSString *username;
if ([messageType isEqualToString:@"D"]){
username = [body componentsSeparatedByString:@":"][0];
} else if([messageType isEqualToString:@"C"]){
username = [body componentsSeparatedByString:@":"][0];
username = [body componentsSeparatedByString:@" "][1];
}
username = [username stringByReplacingOccurrencesOfString:@":" withString:@""];
username = [username stringByReplacingOccurrencesOfString:@"@" withString:@""];
NSString *containerPath = [FolderFinder findDataFolder:@"com.tinyspeck.chatlyio"];
NSString *databasePath = [NSString stringWithFormat:@"%@/Library/Application Support/Slack/%@/Database/main_db", containerPath, teamId];
NSString *imageURLStr;
const char *dbpath = [databasePath UTF8String];
sqlite3 *_slackdb;
if (sqlite3_open(dbpath, &_slackdb) == SQLITE_OK) {
const char *stmt = [[NSString stringWithFormat:@"SELECT 'https://ca.slack-edge.com/' || ZTEAMID || '-' || ZTSID || '-' || ZAVATARHASH || '-512' as url FROM ZSLKCOREDATAUSER WHERE ZNAME = '%@';", username] UTF8String];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(_slackdb, stmt, -1, &statement, NULL) == SQLITE_OK) {
if (sqlite3_step(statement) == SQLITE_ROW) {
const unsigned char *result = sqlite3_column_text(statement, 0);
imageURLStr = [NSString stringWithUTF8String:(char *)result];
}
sqlite3_finalize(statement);
}
sqlite3_close(_slackdb);
}
if (imageURLStr) {
NSURL *imageURL = [NSURL URLWithString:imageURLStr];
return [NSClassFromString(@"DDNotificationContactPhotoPromiseOffer") offerDownloadingPromiseWithPhotoIdentifier:imageURLStr fromURL:imageURL];
} else {
return nil;
}
}
@end