This repository has been archived by the owner on May 27, 2022. It is now read-only.
/
Client.m
164 lines (124 loc) · 4.32 KB
/
Client.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
//
// Client.m
// Messagee
//
// Created by Rod Simpson on 12/27/12.
// Copyright (c) 2012 Rod Simpson. All rights reserved.
//
#import "Client.h"
@implementation Client
@synthesize usergridClient, user;
- (id)init
{
self = [super init];
if (self) {
//configure the org and app
NSString * orgName = @"ApigeeOrg";
NSString * appName = @"MessageeApp";
//make new client
usergridClient = [[UGClient alloc]initWithOrganizationId: orgName withApplicationID: appName];
//[usergridClient setLogging:true]; //uncomment to see debug output in console window
}
return self;
}
-(bool)login:(NSString*)username withPassword:(NSString*)password {
//uncomment the below for easy testing (just click login)
//username = @"myuser";
//password = @"mypass";
//then log the user in
//UGClientResponse *response =
[usergridClient logInUser:username password:password];
user = [usergridClient getLoggedInUser];
if (user.username){
return true;
} else {
return false;
}
}
-(bool)createUser:(NSString*)username
withName:(NSString*)name
withEmail:(NSString*)email
withPassword:(NSString*)password{
UGClientResponse *response = [usergridClient addUser:username email:email name:name password:password];
if (response.transactionState == 0) {
return [self login:username withPassword:password];
}
return false;
}
-(NSArray*)getFollowing {
UGQuery *query = [[UGQuery alloc] init];
[query addURLTerm:@"limit" equals:@"30"];
UGClientResponse *response = [usergridClient getEntityConnections: @"users" connectorID:@"me" connectionType:@"following" query:query];
NSArray * following = [response.response objectForKey:@"entities"];
return following;
}
-(bool)followUser:(NSString*)username{
UGClientResponse *response =
[usergridClient connectEntities:@"users"
connectorID:@"me"
connectionType:@"following"
connecteeType:@"users"
connecteeID:username];
if (response.transactionState == 0) {
return true;
}
return false;
}
-(NSArray*)getMessages {
NSString *username = [user username];
UGQuery *query = [[UGQuery alloc] init];
[query addURLTerm:@"limit" equals:@"30"];
UGClientResponse *response = [usergridClient getActivityFeedForUser:username query:query];
NSArray *messages = [response.response objectForKey:@"entities"];
return messages;
}
-(bool)postMessage:(NSString*)message {
/*
//we are trying to build a json object that looks like this:
{
"actor" : {
"displayName" :"myusername",
"uuid" : "myuserid",
"username" : "myusername",
"email" : "myemail",
"picture": "http://path/to/picture",
"image" : {
"duration" : 0,
"height" : 80,
"url" : "http://www.gravatar.com/avatar/",
"width" : 80
},
},
"verb" : "post",
"content" : content,
"lat" : 48.856614,
"lon" : 2.352222
}
*/
NSMutableDictionary *activity = [[NSMutableDictionary alloc] init ];
NSMutableDictionary *actor = [[NSMutableDictionary alloc] init];
NSString *username = [user username];
NSString *email = [user email];
NSString *uuid = [user uuid];
NSString *picture = [user picture];
NSString *lat = @"48.856614"; //todo: get coords from the phone
NSString *lon = @"2.352222f";
// actor
[actor setObject:username forKey:@"displayName"];
[actor setObject:uuid forKey:@"uuid"];
[actor setObject:username forKey:@"username"];
[actor setObject:email forKey:@"email"];
[actor setObject:picture forKey:@"picture"];
// activity
[activity setValue:actor forKey:@"actor"];
[activity setObject:@"post" forKey:@"verb"];
[activity setObject:message forKey:@"content"];
[activity setObject:lat forKey:@"lat"];
[activity setObject:lon forKey:@"lon"];
UGClientResponse *response = [usergridClient postUserActivity:uuid activity:activity];
if (response.transactionState == 0) {
return true;
}
return false;
}
@end