From c1c2e93f6ea0238444ebaf46f19f13387ac9f99a Mon Sep 17 00:00:00 2001 From: Masaya Tojo Date: Wed, 8 Jul 2020 10:37:08 +0900 Subject: qkbox: toot: Add timeline procedure. --- qkbox/toot.scm | 66 ++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 16 deletions(-) diff --git a/qkbox/toot.scm b/qkbox/toot.scm index a90389b..3bbc856 100644 --- a/qkbox/toot.scm +++ b/qkbox/toot.scm @@ -5,7 +5,7 @@ #:use-module (json builder) #:use-module (json parser) #:use-module (rnrs bytevectors) - #:export (post)) + #:export (post timeline)) (define current-mastodon-host (make-parameter (getenv "MASTODON_HOST"))) @@ -38,7 +38,8 @@ sensitive? reply-to media-ids) - (post-json + (request + 'POST "/api/v1/statuses" `(,@(if status `((status . ,(if (string? status) @@ -65,21 +66,54 @@ '())) #:authorization? #t)) -(define* (post-json path json #:key authorization?) +(define* (timeline #:key max-id since-id min-id limit local?) (define-values (res body) - (http-post (string-append "https://" - (current-mastodon-host) - path) - #:headers `((content-type application/json) - ,@(if authorization? - `((authorization - ,(string->symbol - (string-append - "Bearer " - (current-mastodon-access-token))))) - '())) - #:decode-body? #t - #:body (scm->json-string json))) + (/api/v1/timelines/home #:max-id max-id + #:since-id since-id + #:min-id min-id + #:limit limit + #:local? local?)) + (case (response-code res) + ((200) + (map (lambda (status) + `((id . ,(assoc-ref status "id")) + (name . ,(assoc-ref (assoc-ref status "account") + "display_name")) + (url . ,(assoc-ref status "url")) + (content . ,(assoc-ref status "content")))) + (vector->list body))) + ((206) + (error "timeline: Home feed is regenerating")) + (else + (error "timeline: failed" res body)))) + +(define* (/api/v1/timelines/home #:key max-id since-id min-id limit local?) + (request + 'GET + "/api/v1/timelines/home" + `(,@(if max-id `((max-id . ,max-id)) '()) + ,@(if since-id `((since-id . ,since-id)) '()) + ,@(if min-id `((min-id . ,min-id)) '()) + ,@(if limit `((limit . ,limit)) '()) + ,@(if local? `((local . ,local?)) '())) + #:authorization? #t)) + +(define* (request method path json #:key authorization?) + (define-values (res body) + (http-request (string-append "https://" + (current-mastodon-host) + path) + #:method method + #:headers `((content-type application/json) + ,@(if authorization? + `((authorization + ,(string->symbol + (string-append + "Bearer " + (current-mastodon-access-token))))) + '())) + #:decode-body? #t + #:body (scm->json-string json))) (if (= 200 (response-code res)) (values res (json-string->scm (utf8->string body))) (values res body))) -- cgit v1.2.3