summaryrefslogtreecommitdiff
path: root/mastoapi.c
diff options
context:
space:
mode:
Diffstat (limited to 'mastoapi.c')
-rw-r--r--mastoapi.c68
1 files changed, 50 insertions, 18 deletions
diff --git a/mastoapi.c b/mastoapi.c
index 8b62397..926edfa 100644
--- a/mastoapi.c
+++ b/mastoapi.c
@@ -366,6 +366,30 @@ int oauth_post_handler(const xs_dict *req, const char *q_path,
366} 366}
367 367
368 368
369xs_str *mastoapi_id(const xs_dict *msg)
370/* returns a somewhat Mastodon-compatible status id */
371{
372 char tmp[256] = "";
373 int n = 0;
374 const char *id = xs_dict_get(msg, "id");
375 const char *published = xs_dict_get(msg, "published");
376
377 if (!xs_is_null(published)) {
378 /* transfer all numbers from the published date */
379 while (*published && n < sizeof(tmp) - 1) {
380 if (*published >= '0' && *published <= '9')
381 tmp[n++] = *published;
382 published++;
383 }
384 tmp[n] = '\0';
385 }
386
387 xs *md5 = xs_md5_hex(id, strlen(id));
388
389 return xs_str_cat(xs_str_new(tmp), md5);
390}
391
392
369int mastoapi_get_handler(const xs_dict *req, const char *q_path, 393int mastoapi_get_handler(const xs_dict *req, const char *q_path,
370 char **body, int *b_size, char **ctype) 394 char **body, int *b_size, char **ctype)
371{ 395{
@@ -441,7 +465,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
441 if (logged_in) { 465 if (logged_in) {
442 const char *max_id = xs_dict_get(args, "max_id"); 466 const char *max_id = xs_dict_get(args, "max_id");
443 const char *since_id = xs_dict_get(args, "since_id"); 467 const char *since_id = xs_dict_get(args, "since_id");
444// const char *min_id = xs_dict_get(args, "min_id"); 468 const char *min_id = xs_dict_get(args, "min_id");
445 const char *limit_s = xs_dict_get(args, "limit"); 469 const char *limit_s = xs_dict_get(args, "limit");
446 int limit = 0; 470 int limit = 0;
447 int cnt = 0; 471 int cnt = 0;
@@ -475,6 +499,13 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
475 break; 499 break;
476 } 500 }
477 501
502 /* only returns entries newer than min_id */
503 /* what does really "Return results immediately newer than ID" mean? */
504 if (min_id) {
505 if (strcmp(v, min_id) == 0)
506 break;
507 }
508
478 /* get the entry */ 509 /* get the entry */
479 if (!valid_status(timeline_get_by_md5(&snac, v, &msg))) 510 if (!valid_status(timeline_get_by_md5(&snac, v, &msg)))
480 continue; 511 continue;
@@ -531,10 +562,11 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
531 562
532 char *tmp; 563 char *tmp;
533 id = xs_dict_get(msg, "id"); 564 id = xs_dict_get(msg, "id");
565 xs *mid = mastoapi_id(msg);
534 566
535 xs *st = xs_dict_new(); 567 xs *st = xs_dict_new();
536 568
537 st = xs_dict_append(st, "id", v); 569 st = xs_dict_append(st, "id", mid);
538 st = xs_dict_append(st, "uri", id); 570 st = xs_dict_append(st, "uri", id);
539 st = xs_dict_append(st, "url", id); 571 st = xs_dict_append(st, "url", id);
540 st = xs_dict_append(st, "created_at", xs_dict_get(msg, "published")); 572 st = xs_dict_append(st, "created_at", xs_dict_get(msg, "published"));
@@ -586,24 +618,24 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
586 618
587 st = xs_dict_append(st, "replies_count", ixc); 619 st = xs_dict_append(st, "replies_count", ixc);
588 620
589 tmp = xs_dict_get(msg, "inReplyTo"); 621 /* default in_reply_to values */
590 if (xs_is_null(tmp)) { 622 st = xs_dict_append(st, "in_reply_to_id", n);
591 st = xs_dict_append(st, "in_reply_to_id", n); 623 st = xs_dict_append(st, "in_reply_to_account_id", n);
592 st = xs_dict_append(st, "in_reply_to_account_id", n);
593 }
594 else {
595 xs *irt_md5 = xs_md5_hex(tmp, strlen(tmp));
596 st = xs_dict_append(st, "in_reply_to_id", irt_md5);
597 624
625 tmp = xs_dict_get(msg, "inReplyTo");
626 if (!xs_is_null(tmp)) {
598 xs *irto = NULL; 627 xs *irto = NULL;
599 char *at = NULL; 628
600 if (valid_status(object_get(tmp, &irto)) && 629 if (valid_status(object_get(tmp, &irto))) {
601 !xs_is_null(at = xs_dict_get(irto, "attributedTo"))) { 630 xs *irt_mid = mastoapi_id(irto);
602 xs *at_md5 = xs_md5_hex(at, strlen(at)); 631 st = xs_dict_set(st, "in_reply_to_id", irt_mid);
603 st = xs_dict_append(st, "in_reply_to_account_id", at_md5); 632
633 char *at = NULL;
634 if (!xs_is_null(at = xs_dict_get(irto, "attributedTo"))) {
635 xs *at_md5 = xs_md5_hex(at, strlen(at));
636 st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
637 }
604 } 638 }
605 else
606 st = xs_dict_append(st, "in_reply_to_account_id", n);
607 } 639 }
608 640
609 st = xs_dict_append(st, "reblog", n); 641 st = xs_dict_append(st, "reblog", n);
@@ -632,7 +664,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
632 *ctype = "application/json"; 664 *ctype = "application/json";
633 status = 200; 665 status = 200;
634 666
635// printf("%s\n", *body); 667 srv_debug(0, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
636 } 668 }
637 else { 669 else {
638 status = 401; // unauthorized 670 status = 401; // unauthorized