forked from migrs/QSTwit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
QSTwitterAction.m
121 lines (110 loc) · 4.83 KB
/
QSTwitterAction.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
//
// TwitterAction.m
// Twitter
//
// Created by Joshua Holt on 12/23/09.
// Updated by Masato Igarashi on 07/05/11
#import "QSTwitterAction.h"
#import "QSTwitterUtil.h"
#import "QSTwitterDefines.h"
#import <AddressBook/ABGlobals.h>
#import <AddressBook/ABPerson.h>
#import <AddressBook/ABMultiValue.h>
@implementation QSTwitterAction
-(NSArray*)validActionsForDirectObject:(QSObject *)dObject indirectObject:(QSObject *)iObject {
if ([dObject count] == 1 && [self twitterUsernameForContact:dObject] != nil) {
return @[kDirectMessageAction, kSendMessageAction];
}
return nil;
}
-(NSString *)twitterUsernameForContact:(QSObject *)person {
NSString *usr = nil;
if ([[person primaryType] isEqualToString:NSStringPboardType]) {
usr = [person primaryObject];
if ([usr hasPrefix:@"@"]) {
usr = [usr substringFromIndex:1];
}
}
if (usr) {
return usr;
}
NSArray *people = nil;
/* the QSObject (ContactHandling) protocol (from the Contacts plugin) is not public so -[QSObject ABPerson] throws a warning on build.
This code can *only* be reached if the Contacts plugin is installed though (since it's the only way ABPeopleUIDsPboardType type objects can exist in QS */
ABPerson *pers = [person ABPerson];
if ([NSApplication isMountainLion]) {
people = [pers linkedPeople];
} else {
people = @[pers];
}
for (ABPerson *p in people) {
if (usr) {
break;
}
ABMultiValue *ims = [p valueForProperty:kABSocialProfileProperty];
for (NSUInteger i = 0; i < [ims count]; i++) {
NSDictionary *serv = [ims valueAtIndex:i];
if ([[serv objectForKey:kABSocialProfileServiceKey] isEqualToString:kABSocialProfileServiceTwitter]) {
usr = [serv objectForKey:kABSocialProfileUsernameKey];
break;
}
}
}
return usr;
}
-(NSArray *)validIndirectObjectsForAction:(NSString *)action directObject:(QSObject *)dObject {
if ([action isEqualToString:kDirectMessageToAction] || [action isEqualToString:kSendMessageToAction]) {
NSArray *contacts = [QSLib scoredArrayForType:QSABPersonType];
NSIndexSet *ind = [contacts indexesOfObjectsWithOptions:NSEnumerationConcurrent passingTest:^BOOL(QSObject *obj, NSUInteger idx, BOOL *stop) {
return [self twitterUsernameForContact:obj] != nil;
}];
return [contacts objectsAtIndexes:ind];
}
if ([action isEqualToString:kDirectMessageAction]) {
return [NSArray arrayWithObject:[QSObject textProxyObjectWithDefaultValue:@"Direct Message…"]];
}
if ([action isEqualToString:kSendMessageAction]) {
return [NSArray arrayWithObject:[QSObject textProxyObjectWithDefaultValue:@"Send Message…"]];
}
return nil;
}
-(BOOL)messageIsTooLong:(NSString *)message {
if ([message length] > kMaxTweetLength) {
[[QSTwitterUtil sharedInstance] twitterNotify:[NSString stringWithFormat:NSLocalizedStringFromTableInBundle(@"Message too long (%lu chars)", nil, [NSBundle bundleForClass:[self class]], @"too long message for tweetts"), [message length]]];
return YES;
}
return NO;
}
- (QSObject *)sendTweet:(QSObject *)dObject {
NSString *message = [dObject stringValue];
QSTwitterUtil *tu = [QSTwitterUtil sharedInstance];
if ([self messageIsTooLong:message]) {
return [QSObject textProxyObjectWithDefaultValue:message];
}
return [tu tweet:message toUser:nil];
}
-(QSObject *)sendDirectMessage:(QSObject *)dObject toContact:(QSObject *)iObject {
NSString *username = [self twitterUsernameForContact:iObject];
if (!username || [username length] == 0) {
[[QSTwitterUtil sharedInstance] twitterNotify:NSLocalizedStringFromTableInBundle(@"Invalid Twitter username", nil, [NSBundle bundleForClass:[self class]], @"Invalid username message")];
return nil;
}
NSString *message = [dObject stringValue];
if ([self messageIsTooLong:message]) {
return [QSObject textProxyObjectWithDefaultValue:[dObject stringValue]];
}
return [[QSTwitterUtil sharedInstance] tweet:message toUser:username];
}
-(QSObject*)sendMessage:(QSObject *)dObject toContact:(QSObject *)iObject {
NSString *username = [self twitterUsernameForContact:iObject];
if (!username || [username length] == 0) {
[[QSTwitterUtil sharedInstance] twitterNotify:NSLocalizedStringFromTableInBundle(@"Invalid Twitter username", nil, [NSBundle bundleForClass:[self class]], @"Invalid username message")];
return nil;
}
NSString *message = [NSString stringWithFormat:@"@%@ %@",username,[dObject stringValue]];
if ([self messageIsTooLong:message]) {
return [QSObject textProxyObjectWithDefaultValue:[dObject stringValue]];
}
return [[QSTwitterUtil sharedInstance] tweet:message toUser:nil];
}
@end