forked from wuman/PlurkVala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plurk-client.vala
601 lines (542 loc) · 22.7 KB
/
plurk-client.vala
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
using Soup;
using Json;
public class Roguso.PlurkClient : GLib.Object {
private static const string PLURKVALA_USER_AGENT = "PlurkVala 0.2";
private static const int SESSION_MAX_CONNS = 1;
private PlurkApi plurk_api;
private Profile profile;
private string user_agent = PLURKVALA_USER_AGENT;
private Session session;
private CookieJar cookies;
public signal void authenticate(Error? e, bool is_logged_in);
public signal void plurk_received(Error? e, Plurk? plurk);
public signal void timeline_complete(Error? e, PlurkList? plurks);
public signal void response_received(Error? e, Response? response);
public signal void responses_complete(Error? e, string? plurk_id, int from_response, ResponseList? responses);
public signal void plurks_muted(Error? e, string[] plurk_ids);
public signal void plurks_unmuted(Error? e, string[] plurk_ids);
public signal void plurks_favorited(Error? e, string[] plurk_ids);
public signal void plurks_unfavorited(Error? e, string[] plurk_ids);
public signal void plurk_deleted(Error? e, string plurk_id);
public signal void response_deleted(Error? e, string plurk_id, string response_id);
public signal void karma_received(Error? e, double karma);
public signal void profile_received(Error? e, Profile? profile);
public signal void cliques_received(Error? e, string[] cliques);
public signal void clique_users_received(Error? e, string clique_name, UserList? users);
public signal void clique_created(Error? e, string clique_name);
public signal void clique_renamed(Error? e, string clique_name, string new_name);
public signal void user_added_to_clique(Error? e, string clique_name, string user_id);
public signal void user_removed_from_clique(Error? e, string clique_name, string user_id);
private static enum Action {
LOGIN,
LOGOUT,
GET_KARMA_STATS,
GET_OWN_PROFILE,
GET_PUBLIC_PROFILE,
GET_TIMELINE,
GET_UNREAD_TIMELINE,
GET_PLURK,
GET_RESPONSES,
MUTE_PLURKS,
UNMUTE_PLURKS,
FAVORITE_PLURKS,
UNFAVORITE_PLURKS,
ADD_PLURK,
EDIT_PLURK,
DELETE_PLURK,
ADD_RESPONSE,
DELETE_RESPONSE,
GET_CLIQUES,
GET_CLIQUE_USERS,
CREATE_CLIQUE,
RENAME_CLIQUE,
ADD_USER_TO_CLIQUE,
REMOVE_USER_FROM_CLIQUE;
}
/*
* Constructors
*/
public PlurkClient(string api_key) {
plurk_api = new PlurkApi(api_key);
prepare_session();
}
private void prepare_session() {
cookies = new CookieJar();
session = new SessionAsync.with_options(
"user-agent", user_agent,
Soup.SESSION_MAX_CONNS, SESSION_MAX_CONNS,
null);
session.add_feature = cookies;
}
public void login(string username, string password, bool no_data = false) {
Message msg = plurk_api.login(username, password, no_data);
session.queue_message(msg, (s, m) => {
bool success;
Error error = on_request_responded_cb(s, m, Action.LOGIN, out success);
authenticate(error, success);
if ( success ) {
HashTable<string, string> form_data = Soup.form_decode(m.uri.query);
string nodata = form_data.lookup(PlurkApi.PARAM_NO_DATA);
if ( nodata == null || !nodata.to_bool() ) {
profile = new Profile.from_data(m.response_body.flatten().data);
profile_received(null, profile);
}
}
});
}
public void logout() {
Message msg = plurk_api.logout();
session.queue_message(msg, (s, m) => {
bool success;
Error error = on_request_responded_cb(s, m, Action.LOGOUT, out success);
authenticate(error, !success);
});
}
public void mute_plurks(string[] ids) {
if ( ids.length <= 0 ) {
return;
}
Message msg = plurk_api.mute_plurks(ids);
session.queue_message(msg, (s, m) => {
bool success;
Error error = on_request_responded_cb(s, m, Action.MUTE_PLURKS, out success);
if ( success ) {
on_mute_plurks_cb(s, m);
} else {
plurks_muted(error, new string[0]);
}
});
}
public void unmute_plurks(string[] ids) {
if ( ids.length <= 0 ) {
return;
}
Message msg = plurk_api.unmute_plurks(ids);
session.queue_message(msg, (s, m) => {
bool success;
Error error = on_request_responded_cb(s, m, Action.UNMUTE_PLURKS, out success);
if ( success ) {
on_unmute_plurks_cb(s, m);
} else {
plurks_unmuted(error, new string[0]);
}
});
}
public void favorite_plurks(string[] ids) {
if ( ids.length <= 0 ) {
return;
}
Message msg = plurk_api.favorite_plurks(ids);
session.queue_message(msg, (s, m) => {
bool success;
Error error = on_request_responded_cb(s, m, Action.FAVORITE_PLURKS, out success);
if ( success ) {
on_favorite_plurks_cb(s, m);
} else {
plurks_favorited(error, new string[0]);
}
});
}
public void unfavorite_plurks(string[] ids) {
if ( ids.length <= 0 ) {
return;
}
Message msg = plurk_api.unfavorite_plurks(ids);
session.queue_message(msg, (s, m) => {
bool success;
Error error = on_request_responded_cb(s, m, Action.UNFAVORITE_PLURKS, out success);
if ( success ) {
on_unfavorite_plurks_cb(s, m);
} else {
plurks_unfavorited(error, new string[0]);
}
});
}
public void get_timeline(Soup.Date? offset, int limit = 0, PlurkApi.PlurkFilterType filter = PlurkApi.PlurkFilterType.ALL) {
Message msg = plurk_api.timeline_get_plurks(offset, limit, filter);
session.queue_message(msg, (s, m) => {
bool success;
Error error = on_request_responded_cb(s, m, Action.GET_TIMELINE, out success);
if ( success ) {
on_get_timeline_cb(s, m);
} else {
timeline_complete(error, null);
}
});
}
public void get_unread_timeline(Soup.Date? offset, int limit = 0) {
Message msg = plurk_api.timeline_get_unread_plurks(offset, limit);
session.queue_message(msg, (s, m) => {
bool success;
Error error = on_request_responded_cb(s, m, Action.GET_UNREAD_TIMELINE, out success);
if ( success ) {
on_get_timeline_cb(s, m);
} else {
timeline_complete(error, null);
}
});
}
public void get_plurk(string plurk_id) {
Message msg = plurk_api.timeline_get_plurk(plurk_id);
session.queue_message(msg, (s, m) => {
bool success;
Error error = on_request_responded_cb(s, m, Action.GET_PLURK, out success);
if ( success ) {
on_get_plurk_cb(s, m);
} else {
plurk_received(error, null);
}
});
}
public void get_responses(string plurk_id, int from_response = 0) {
Message msg = plurk_api.get_responses(plurk_id, from_response);
session.queue_message(msg, (s, m) => {
bool success;
Error error = on_request_responded_cb(s, m, Action.GET_RESPONSES, out success);
if ( success ) {
on_get_responses_cb(s, m);
} else {
responses_complete(error, null, 0, null);
}
});
}
public void add_plurk(string content,
Plurk.Qualifier qualifier = Plurk.Qualifier.DEFAULT,
string[]? limited_to,
Plurk.NoCommentsValue no_comments = Plurk.NoCommentsValue.RESPONSES_ENABLED_FOR_ALL,
PlurkApi.Language language = PlurkApi.Language.EN) {
Message msg = plurk_api.add_plurk(content, qualifier, limited_to, no_comments, language);
session.queue_message(msg, (s, m) => {
bool success;
Error error = on_request_responded_cb(s, m, Action.ADD_PLURK, out success);
if ( success ) {
on_get_plurk_cb(s, m);
} else {
plurk_received(error, null);
}
});
}
public void edit_plurk(string plurk_id, string content) {
Message msg = plurk_api.edit_plurk(plurk_id, content);
session.queue_message(msg, (s, m) => {
bool success;
Error error = on_request_responded_cb(s, m, Action.EDIT_PLURK, out success);
if ( success ) {
on_get_plurk_cb(s, m);
} else {
plurk_received(error, null);
}
});
}
public void delete_plurk(string plurk_id) {
Message msg = plurk_api.delete_plurk(plurk_id);
session.queue_message(msg, (s, m) => {
bool success;
Error error = on_request_responded_cb(s, m, Action.DELETE_PLURK, out success);
on_delete_plurk_cb(s, m, success, error);
});
}
public void add_response(string plurk_id, string content, Plurk.Qualifier qualifier = Plurk.Qualifier.DEFAULT) {
Message msg = plurk_api.add_response(plurk_id, content, qualifier);
session.queue_message(msg, (s, m) => {
bool success;
Error error = on_request_responded_cb(s, m, Action.ADD_RESPONSE, out success);
if ( success ) {
on_get_response_cb(s, m);
} else {
response_received(error, null);
}
});
}
public void delete_response(string plurk_id, string response_id) {
Message msg = plurk_api.delete_response(plurk_id, response_id);
session.queue_message(msg, (s, m) => {
bool success;
Error error = on_request_responded_cb(s, m, Action.DELETE_RESPONSE, out success);
on_delete_response_cb(s, m, success, error);
});
}
public void get_karma() {
Message msg = plurk_api.get_karma_stats();
session.queue_message(msg, (s, m) => {
bool success;
Error error = on_request_responded_cb(s, m, Action.GET_KARMA_STATS, out success);
if ( success ) {
on_get_karma_stats_cb(s, m);
} else {
karma_received(error, 0.0);
}
});
}
public void get_cliques() {
Message msg = plurk_api.get_cliques();
session.queue_message(msg, (s, m) => {
bool success;
Error error = on_request_responded_cb(s, m, Action.GET_CLIQUES, out success);
if ( success ) {
on_get_cliques_cb(s, m);
} else {
cliques_received(error, new string[0]);
}
});
}
public void get_clique_users(string clique_name) {
Message msg = plurk_api.get_clique_users(clique_name);
session.queue_message(msg, (s, m) => {
bool success;
Error error = on_request_responded_cb(s, m, Action.GET_CLIQUE_USERS, out success);
if ( success ) {
on_get_clique_users_cb(s, m);
} else {
HashTable<string, string> form_data = Soup.form_decode(m.uri.query);
string cliquename = form_data.lookup(PlurkApi.PARAM_CLIQUE_NAME);
clique_users_received(error, cliquename, null);
}
});
}
public void create_clique(string clique_name) {
Message msg = plurk_api.create_clique(clique_name);
session.queue_message(msg, (s, m) => {
bool success;
Error error = on_request_responded_cb(s, m, Action.CREATE_CLIQUE, out success);
HashTable<string, string> form_data = Soup.form_decode(m.uri.query);
string cliquename = form_data.lookup(PlurkApi.PARAM_CLIQUE_NAME);
clique_created(success ? null : error, cliquename);
});
}
public void rename_clique(string clique_name, string new_name) {
Message msg = plurk_api.rename_clique(clique_name, new_name);
session.queue_message(msg, (s, m) => {
bool success;
Error error = on_request_responded_cb(s, m, Action.RENAME_CLIQUE, out success);
HashTable<string, string> form_data = Soup.form_decode(m.uri.query);
string cliquename = form_data.lookup(PlurkApi.PARAM_CLIQUE_NAME);
string newname = form_data.lookup(PlurkApi.PARAM_NEW_NAME);
clique_renamed(success ? null : error, cliquename, newname);
});
}
public void add_user_to_clique(string clique_name, string user_id) {
Message msg = plurk_api.add_user_to_clique(clique_name, user_id);
session.queue_message(msg, (s, m) => {
bool success;
Error error = on_request_responded_cb(s, m, Action.ADD_USER_TO_CLIQUE, out success);
HashTable<string, string> form_data = Soup.form_decode(m.uri.query);
string cliquename = form_data.lookup(PlurkApi.PARAM_CLIQUE_NAME);
string userid = form_data.lookup(PlurkApi.PARAM_USER_ID);
user_added_to_clique(success ? null : error, cliquename, userid);
});
}
public void remove_user_from_clique(string clique_name, string user_id) {
Message msg = plurk_api.remove_user_from_clique(clique_name, user_id);
session.queue_message(msg, (s, m) => {
bool success;
Error error = on_request_responded_cb(s, m, Action.REMOVE_USER_FROM_CLIQUE, out success);
HashTable<string, string> form_data = Soup.form_decode(m.uri.query);
string cliquename = form_data.lookup(PlurkApi.PARAM_CLIQUE_NAME);
string userid = form_data.lookup(PlurkApi.PARAM_USER_ID);
user_removed_from_clique(success ? null : error, cliquename, userid);
});
}
public void get_own_profile() {
Message msg = plurk_api.get_own_profile();
session.queue_message(msg, (s, m) => {
bool success;
Error error = on_request_responded_cb(s, m, Action.GET_OWN_PROFILE, out success);
if ( success ) {
on_get_profile_cb(s, m);
} else {
profile_received(error, null);
}
});
}
public void get_public_profile(string user_id) {
Message msg = plurk_api.get_public_profile(user_id);
session.queue_message(msg, (s, m) => {
bool success;
Error error = on_request_responded_cb(s, m, Action.GET_PUBLIC_PROFILE, out success);
if ( success ) {
on_get_profile_cb(s, m);
} else {
profile_received(error, null);
}
});
}
private static Error on_request_responded_cb(Session session, Message msg, Action action, out bool is_successful) {
Error error;
string error_msg = null;
if ( msg.status_code >= 200 && msg.status_code < 300 ) {
is_successful = true;
error = null;
return error;
}
is_successful = false;
try {
string buffer = msg.response_body.flatten().data;
Parser parser = new Parser();
parser.load_from_data(buffer);
unowned Json.Object object = parser.get_root().get_object();
unowned string error_text = object.get_string_member("error_text");
error_msg = error_text.dup();
} catch ( Error e ) {
error = new Error(PLURK_ERROR_DOMAIN, PlurkError.JSON_PARSING_ERROR, "%d: %s", e.code, e.message);
return error;
}
switch ( action ) {
case Action.LOGIN:
case Action.LOGOUT:
case Action.GET_KARMA_STATS:
case Action.GET_OWN_PROFILE:
case Action.GET_PUBLIC_PROFILE:
case Action.GET_TIMELINE:
case Action.GET_UNREAD_TIMELINE:
case Action.GET_PLURK:
case Action.ADD_PLURK:
case Action.EDIT_PLURK:
case Action.DELETE_PLURK:
case Action.GET_RESPONSES:
case Action.DELETE_RESPONSE:
case Action.ADD_RESPONSE:
case Action.MUTE_PLURKS:
case Action.UNMUTE_PLURKS:
case Action.FAVORITE_PLURKS:
case Action.UNFAVORITE_PLURKS:
case Action.GET_CLIQUES:
case Action.GET_CLIQUE_USERS:
case Action.CREATE_CLIQUE:
case Action.RENAME_CLIQUE:
case Action.ADD_USER_TO_CLIQUE:
case Action.REMOVE_USER_FROM_CLIQUE:
int code = (int) PlurkError.from_msg(error_msg);
error = new Error(PLURK_ERROR_DOMAIN, code, "%s", error_msg);
break;
default:
error = new Error(PLURK_ERROR_DOMAIN, (int) PlurkError.UNKNOWN, "%s", "Unknown error");
break;
}
return error;
}
private void on_get_timeline_cb(Session session, Message msg) {
string buffer = msg.response_body.flatten().data;
PlurkList plurks = new PlurkList.from_data(buffer);
uint index = 0;
Idle.add(() => {
if ( index >= plurks.get_count() ) {
timeline_complete(null, plurks);
return false;
}
Plurk plurk = plurks.get_pos(index++);
plurk_received(null, plurk);
return true;
});
}
private void on_get_plurk_cb(Session session, Message msg) {
string buffer = msg.response_body.flatten().data;
Plurk plurk = new Plurk.from_data(buffer);
Idle.add(() => {
plurk_received(null, plurk);
return false;
});
}
private void on_get_response_cb(Session session, Message msg) {
string buffer = msg.response_body.flatten().data;
Response response = new Response.from_data(buffer);
Idle.add(() => {
response_received(null, response);
return false;
});
}
private void on_get_responses_cb(Session session, Message msg) {
HashTable<string, string> form_data = Soup.form_decode(msg.uri.query);
string plurk_id = form_data.lookup(PlurkApi.PARAM_PLURK_ID);
int from_response = form_data.lookup(PlurkApi.PARAM_FROM_RESPONSE).to_int();
string buffer = msg.response_body.flatten().data;
ResponseList responses = new ResponseList.from_data(buffer);
uint index = 0;
Idle.add(() => {
if ( index >= responses.get_count() ) {
responses_complete(null, plurk_id, from_response, responses);
return false;
}
Response response = responses.get_pos(index++);
response_received(null, response);
return true;
});
}
private void on_get_profile_cb(Session session, Message msg) {
string buffer = msg.response_body.flatten().data;
Profile profile = new Profile.from_data(buffer);
profile_received(null, profile);
}
private void on_mute_plurks_cb(Session session, Message msg) {
HashTable<string, string> form_data = Soup.form_decode(msg.uri.query);
string ids = form_data.lookup(PlurkApi.PARAM_IDS);
string[] plurk_ids = ids[2:ids.length-2].split(", ");
plurks_muted(null, plurk_ids);
}
private void on_unmute_plurks_cb(Session session, Message msg) {
HashTable<string, string> form_data = Soup.form_decode(msg.uri.query);
string ids = form_data.lookup(PlurkApi.PARAM_IDS);
string[] plurk_ids = ids[2:ids.length-2].split(", ");
plurks_unmuted(null, plurk_ids);
}
private void on_favorite_plurks_cb(Session session, Message msg) {
HashTable<string, string> form_data = Soup.form_decode(msg.uri.query);
string ids = form_data.lookup(PlurkApi.PARAM_IDS);
string[] plurk_ids = ids[2:ids.length-2].split(", ");
plurks_favorited(null, plurk_ids);
}
private void on_unfavorite_plurks_cb(Session session, Message msg) {
HashTable<string, string> form_data = Soup.form_decode(msg.uri.query);
string ids = form_data.lookup(PlurkApi.PARAM_IDS);
string[] plurk_ids = ids[2:ids.length-2].split(", ");
plurks_unfavorited(null, plurk_ids);
}
private void on_delete_plurk_cb(Session session, Message msg, bool success, Error? e) {
HashTable<string, string> form_data = Soup.form_decode(msg.uri.query);
string plurk_id = form_data.lookup(PlurkApi.PARAM_PLURK_ID);
plurk_deleted(success ? null : e, plurk_id);
}
private void on_delete_response_cb(Session session, Message msg, bool success, Error? e) {
HashTable<string, string> form_data = Soup.form_decode(msg.uri.query);
string plurk_id = form_data.lookup(PlurkApi.PARAM_PLURK_ID);
string response_id = form_data.lookup(PlurkApi.PARAM_RESPONSE_ID);
response_deleted(success ? null : e, plurk_id, response_id);
}
private void on_get_karma_stats_cb(Session session, Message msg) {
string buffer = msg.response_body.flatten().data;
try {
Parser parser = new Parser();
parser.load_from_data(buffer);
unowned Json.Object object = parser.get_root().get_object();
double karma = object.get_double_member("current_karma");
karma_received(null, karma);
} catch ( Error e ) {
Error error = new Error(PLURK_ERROR_DOMAIN, PlurkError.JSON_PARSING_ERROR, "%d: %s", e.code, e.message);
karma_received(error, 0.0);
}
}
private void on_get_cliques_cb(Session session, Message msg) {
string buffer = msg.response_body.flatten().data;
try {
Parser parser = new Parser();
parser.load_from_data(buffer);
unowned Json.Array array = parser.get_root().get_array();
uint length = array.get_length();
string[] cliques = new string[length];
for ( int i = 0; i < length; i++ ) {
cliques[i] = array.get_string_element(i).dup();
}
cliques_received(null, cliques);
} catch ( Error e ) {
Error error = new Error(PLURK_ERROR_DOMAIN, PlurkError.JSON_PARSING_ERROR, "%d: %s", e.code, e.message);
cliques_received(error, new string[0]);
}
}
private void on_get_clique_users_cb(Session session, Message msg) {
string buffer = msg.response_body.flatten().data;
HashTable<string, string> form_data = Soup.form_decode(msg.uri.query);
string clique_name = form_data.lookup(PlurkApi.PARAM_CLIQUE_NAME);
UserList users = new UserList.from_data(buffer);
clique_users_received(null, clique_name, users);
}
}