diff options
Diffstat (limited to 'mastoapi.c')
| -rw-r--r-- | mastoapi.c | 70 |
1 files changed, 70 insertions, 0 deletions
| @@ -1528,4 +1528,74 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path, | |||
| 1528 | return status; | 1528 | return status; |
| 1529 | } | 1529 | } |
| 1530 | 1530 | ||
| 1531 | |||
| 1532 | int mastoapi_put_handler(const xs_dict *req, const char *q_path, | ||
| 1533 | const char *payload, int p_size, | ||
| 1534 | char **body, int *b_size, char **ctype) | ||
| 1535 | { | ||
| 1536 | if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/")) | ||
| 1537 | return 0; | ||
| 1538 | |||
| 1539 | srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path)); | ||
| 1540 | /* { | ||
| 1541 | xs *j = xs_json_dumps_pp(req, 4); | ||
| 1542 | printf("mastoapi put:\n%s\n", j); | ||
| 1543 | }*/ | ||
| 1544 | |||
| 1545 | int status = 404; | ||
| 1546 | xs *args = NULL; | ||
| 1547 | char *i_ctype = xs_dict_get(req, "content-type"); | ||
| 1548 | |||
| 1549 | if (i_ctype && xs_startswith(i_ctype, "application/json")) | ||
| 1550 | args = xs_json_loads(payload); | ||
| 1551 | else | ||
| 1552 | args = xs_dup(xs_dict_get(req, "p_vars")); | ||
| 1553 | |||
| 1554 | if (args == NULL) | ||
| 1555 | return 400; | ||
| 1556 | |||
| 1557 | xs *cmd = xs_replace(q_path, "/api", ""); | ||
| 1558 | |||
| 1559 | snac snac = {0}; | ||
| 1560 | int logged_in = process_auth_token(&snac, req); | ||
| 1561 | |||
| 1562 | if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) { | ||
| 1563 | if (logged_in) { | ||
| 1564 | xs *l = xs_split(cmd, "/"); | ||
| 1565 | const char *stid = xs_list_get(l, 3); | ||
| 1566 | |||
| 1567 | if (!xs_is_null(stid)) { | ||
| 1568 | const char *desc = xs_dict_get(args, "description"); | ||
| 1569 | |||
| 1570 | /* set the image metadata */ | ||
| 1571 | static_put_meta(&snac, stid, desc); | ||
| 1572 | |||
| 1573 | /* prepare a response */ | ||
| 1574 | xs *rsp = xs_dict_new(); | ||
| 1575 | xs *url = xs_fmt("%s/s/%s", snac.actor, stid); | ||
| 1576 | |||
| 1577 | rsp = xs_dict_append(rsp, "id", stid); | ||
| 1578 | rsp = xs_dict_append(rsp, "type", "image"); | ||
| 1579 | rsp = xs_dict_append(rsp, "url", url); | ||
| 1580 | rsp = xs_dict_append(rsp, "preview_url", url); | ||
| 1581 | rsp = xs_dict_append(rsp, "remote_url", url); | ||
| 1582 | rsp = xs_dict_append(rsp, "description", desc); | ||
| 1583 | |||
| 1584 | *body = xs_json_dumps_pp(rsp, 4); | ||
| 1585 | *ctype = "application/json"; | ||
| 1586 | status = 200; | ||
| 1587 | } | ||
| 1588 | } | ||
| 1589 | else | ||
| 1590 | status = 401; | ||
| 1591 | } | ||
| 1592 | |||
| 1593 | /* user cleanup */ | ||
| 1594 | if (logged_in) | ||
| 1595 | user_free(&snac); | ||
| 1596 | |||
| 1597 | return status; | ||
| 1598 | } | ||
| 1599 | |||
| 1600 | |||
| 1531 | #endif /* #ifndef NO_MASTODON_API */ | 1601 | #endif /* #ifndef NO_MASTODON_API */ |