forked from Awful/Awful.app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAwfulPrivateMessage.m
More file actions
82 lines (75 loc) · 2.75 KB
/
Copy pathAwfulPrivateMessage.m
File metadata and controls
82 lines (75 loc) · 2.75 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
//
// AwfulPrivateMessage.m
// Awful
//
// Created by Nolan Waite on 13-02-22.
// Copyright (c) 2013 Regular Berry Software LLC. All rights reserved.
//
#import "AwfulPrivateMessage.h"
#import "AwfulDataStack.h"
#import "AwfulParsing.h"
#import "AwfulUser.h"
#import "NSManagedObject+Awful.h"
@implementation AwfulPrivateMessage
- (NSString *)firstIconName
{
NSString *basename = [[self.messageIconImageURL lastPathComponent]
stringByDeletingPathExtension];
return [basename stringByAppendingPathExtension:@"png"];
}
+ (instancetype)privateMessageWithParsedInfo:(PrivateMessageParsedInfo *)info
{
AwfulPrivateMessage *message = [self firstMatchingPredicate:@"messageID = %@", info.messageID];
if (!message) {
message = [AwfulPrivateMessage insertNew];
}
[info applyToObject:message];
if (info.from) {
AwfulUser *from;
if (info.from.userID) {
from = [AwfulUser firstMatchingPredicate:@"userID = %@", info.from.userID];
}
if (!from && info.from.username) {
from = [AwfulUser firstMatchingPredicate:@"username = %@", info.from.username];
}
if (!from) {
from = [AwfulUser insertNew];
}
[info.from applyToObject:from];
message.from = from;
}
if (info.to) {
AwfulUser *to = [AwfulUser firstMatchingPredicate:@"userID = %@", info.to.username];
if (!to) {
to = [AwfulUser insertNew];
}
[info.to applyToObject:to];
message.to = to;
}
[[AwfulDataStack sharedDataStack] save];
return message;
}
+ (NSArray *)privateMessagesWithFolderParsedInfo:(PrivateMessageFolderParsedInfo *)info
{
NSMutableDictionary *existingPMs = [NSMutableDictionary new];
NSArray *messageIDs = [info.privateMessages valueForKey:@"messageID"];
for (AwfulPrivateMessage *msg in [self fetchAllMatchingPredicate:@"messageID IN %@", messageIDs]) {
existingPMs[msg.messageID] = msg;
}
NSMutableDictionary *existingUsers = [NSMutableDictionary new];
NSArray *usernames = [info.privateMessages valueForKeyPath:@"from.username"];
for (AwfulUser *user in [AwfulUser fetchAllMatchingPredicate:@"username IN %@", usernames]) {
existingUsers[user.username] = user;
}
for (PrivateMessageParsedInfo *pmInfo in info.privateMessages) {
AwfulPrivateMessage *msg = existingPMs[pmInfo.messageID] ?: [AwfulPrivateMessage insertNew];
[pmInfo applyToObject:msg];
if (!msg.from) msg.from = [AwfulUser insertNew];
[pmInfo.from applyToObject:msg.from];
existingUsers[msg.from.username] = msg.from;
existingPMs[msg.messageID] = msg;
}
[[AwfulDataStack sharedDataStack] save];
return [existingPMs allValues];
}
@end