summaryrefslogtreecommitdiff
path: root/qkbox/toot.scm
blob: b3379d1849f25cbe796a495536e620a979900cd4 (plain)
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
;;; Qkbox --- TojoQK's toybox
;;; Copyright © 2020 Masaya Tojo <masaya@tojo.tokyo>
;;;
;;; This file is part of Qkbox.
;;;
;;; Qkbox is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; Qkbox is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with Qkbox.  If not, see <http://www.gnu.org/licenses/>.

(define-module (qkbox toot)
  #:use-module (ice-9 format)
  #:use-module (web response)
  #:use-module (web client)
  #:use-module (json builder)
  #:use-module (json parser)
  #:use-module (rnrs bytevectors)
  #:use-module (ice-9 binary-ports)
  #:use-module (ice-9 receive)
  #:use-module (ice-9 regex)
  #:use-module (srfi srfi-9)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-9 gnu)
  #:use-module (pict)
  #:use-module ((sxml simple)
                #:select (xml->sxml))
  #:use-module ((sxml fold)
                #:select (foldt))
  #:use-module ((sxml xpath)
                #:select (sxpath)))

(define current-mastodon-host
  (make-parameter (getenv "MASTODON_HOST")))

(define current-mastodon-access-token
  (make-parameter (getenv "MASTODON_ACCESS_TOKEN")))

(define*-public (post text
                      #:key
                      spoiler-text
                      visibility
                      sensitive?
                      reply-to)
  (receive (res body)
      (/api/v1/statuses #:status text
                        #:spoiler-text spoiler-text
                        #:visibility visibility
                        #:sensitive? sensitive?
                        #:reply-to reply-to)
    (case (response-code res)
      ((200)
       (assoc-ref body "id"))
      (else
       (error "post: failed" res body)))))

(define* (/api/v1/statuses #:key
                           status
                           spoiler-text
                           visibility
                           sensitive?
                           reply-to
                           media-ids)
  (request
   'POST
   "/api/v1/statuses"
   #:json
   `(,@(if status
           `((status . ,(if (string? status)
                            status
                            (format #f "~y" status))))
           '())
     ,@(if spoiler-text
           `((spoiler_text . ,spoiler-text))
           '())
     ,@(if visibility
           (case visibility
             ((public unlisted private direct)
              `((visibility . ,visibility)))
             (else
              (error "post: invalid visibility (must be one of: public unlisted private direct)")))
           '())
     ,@(if sensitive? `((sensitive . (if sensitive? #t #f)))
           '())
     ,@(if reply-to
           `((in_reply_to_id . ,reply-to))
           '())
     ,@(if media-ids
           `((media_ids . ,(list->vector (map number->string media-ids))))
           '()))
   #:authorization? #t))

(define-record-type <status>
  (make-status json)
  status?
  (json status-json))

(set-record-type-printer! <status>
  (lambda (status port)
    (format port
            "#<status id: ~s ...>"
            (status-id status))))

(define-record-type <account>
  (make-account json)
  account?
  (json account-json))

(set-record-type-printer! <account>
  (lambda (account port)
    (format port "#<account id: ~s acct: ~s ...>"
            (account-id account)
            (account-acct account))))

(define-public (status-public? status)
  (eq? 'public (status-visibility status)))

(define*-public (display-status status
                                #:key
                                (port (current-output-port))
                                display-cw?)
  (let ((account (status-account status)))
    (format port "~s ~a ~a ~s~@[ ~:@(~a~)~]~%"
            (fetch-avatar-static (account-avatar-static account))
            (account-display-name account)
            (account-acct account)
            (status-id status)
            (if (eq? 'public (status-visibility status))
                #f
                (status-visibility status)))
    (cond
     ((status-spoiler-text status)
      => (lambda (spoiler-text)
           (format port "[CW]: ~a~%" spoiler-text)
           (when display-cw?
             (display-content (status-content status)
                              port))))
     (else
      (display-content (status-content status)
                       port)))))

(define-public (status-id status)
  (assoc-ref (status-json status) "id"))

(define-public (status-account status)
  (make-account (assoc-ref (status-json status) "account")))

(define-public (status-reblog status)
  (let ((reblog/json (assoc-ref (status-json status) "reblog")))
    (if (eq? reblog/json 'null)
        #f
        (make-status reblog/json))))

(define-public (status-content status)
  (assoc-ref (status-json status) "content"))

(define (content->sxml content)
  (xml->sxml
   (string-append
    "<status>"
    (regexp-substitute/global #f "<br>"
                              content
                              'pre "<br/>" 'post)
    "</status>")))

(define*-public (display-content content #:optional (port (current-output-port)))
  (for-each (lambda (x) (display x port))
            (flatten
             (foldt (lambda (xs)
                      (case (car xs)
                        ((*TOP* span status p a) (cdr xs))
                        ((br) #\newline)
                        ((@ class target rel href) '())
                        (else
                         (format #f "~%[DEBUG] UNEXPECTED ~a~%" xs))))
                    (lambda (obj) obj)
                    (content->sxml content))))
  (newline port))

(define (flatten x)
  (cond
   ((list? x)
    (append-map flatten x))
   (else (list x))))

(define-public (status-visibility status)
  (string->symbol (assoc-ref (status-json status) "visibility")))

(define-public (status-spoiler-text status)
  (let ((s (assoc-ref (status-json status) "spoiler_text")))
    (if (zero? (string-length s))
        #f
        s)))

(define-public (status-media-attachments status)
  (let ((v (assoc-ref (status-json status) "media_attachments")))
    (if (zero? (vector-length v))
        #f
        (vector->list v))))

(define-public (account-id account)
  (assoc-ref (account-json account) "id"))

(define-public (account-avatar-static account)
  (assoc-ref (account-json account) "avatar_static"))

(define-public (account-acct account)
  (assoc-ref (account-json account) "acct"))

(define-public (account-display-name account)
  (assoc-ref (account-json account) "display_name"))

(define avatar-static-hash (make-hash-table))

(define* (fetch-pict url #:key resize)
  (receive (res body)
      (http-get url)
    (let ((tmp (tmpnam)))
      (let ((in (open tmp (logior O_WRONLY O_CREAT O_EXCL))))
        (put-bytevector in body)
        (close in))
      (let ((pict (and (zero? (apply system* "convert"
                                     `(,@(if resize
                                             `("-resize"
                                               ,(format #f "~d!x~d!"
                                                        (car resize)
                                                        (cadr resize)))
                                             '())
                                       ,(string-append tmp "[0]")
                                       ,(string-append "png:" tmp))))
                       (pict-from-file tmp))))
        (delete-file tmp)
        pict))))

(define* (fetch-avatar-static url)
  (cond
   ((hash-ref avatar-static-hash url) => identity)
   (else
    (let ((pict (fetch-pict url #:resize '(32 32))))
      (hash-set! avatar-static-hash url pict)
      pict))))

(define*-public (status id #:key (authorization? #t))
  (receive (res body)
      (/api/v1/statuses/:id id #:authorization? authorization?)
    (case (response-code res)
      ((200) (make-status body))
      (else
       (error "status: failed" res body)))))

(define* (/api/v1/statuses/:id id #:key (authorization? #t))
  (request
   'GET
   (format #f "/api/v1/statuses/~a" id)
   #:authorization? authorization?))

(define*-public (timeline #:key max-id since-id min-id limit local?)
  (receive (res body)
    (/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 make-status (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"
   #:json
   `(,@(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-public (favourite id)
  (define-values (res body)
    (/api/v1/statuses/:id/favourite id))
  (case (response-code res)
    ((200) #t)
    (else
     (error "favourite: failed" res body))))

(define (/api/v1/statuses/:id/favourite id)
  (request
   'POST
   (format #f "/api/v1/statuses/~a/favourite" id)
   #:authorization? #t))

(define-public (unfavourite id)
  (define-values (res body)
    (/api/v1/statuses/:id/unfavourite id))
  (case (response-code res)
    ((200) #t)
    (else
     (error "unfavourite: failed" res body))))

(define (/api/v1/statuses/:id/unfavourite id)
  (request
   'POST
   (format #f "/api/v1/statuses/~a/unfavourite" id)
   #:authorization? #t))

(define-public (reblog id)
  (define-values (res body)
    (/api/v1/statuses/:id/reblog id))
  (case (response-code res)
    ((200) #t)
    (else
     (error "reblog: failed" res body))))

(define (/api/v1/statuses/:id/reblog id)
  (request
   'POST
   (format #f "/api/v1/statuses/~a/reblog" id)
   #:authorization? #t))

(define-public (unreblog id)
  (define-values (res body)
    (/api/v1/statuses/:id/unreblog id))
  (case (response-code res)
    ((200) #t)
    (else
     (error "unreblog: failed" res body))))

(define (/api/v1/statuses/:id/unreblog id)
  (request
   'POST
   (format #f "/api/v1/statuses/~a/unreblog" id)
   #:authorization? #t))

(define* (raw-request method path #:key (headers '()) (body "") authorization?)
  (receive (res body)
      (http-request (string-append "https://"
                                   (current-mastodon-host)
                                   path)
                    #:method method
                    #:headers `(,@headers
                                ,@(if authorization?
                                      `((authorization
                                         ,(string->symbol
                                           (string-append
                                            "Bearer "
                                            (current-mastodon-access-token)))))
                                      '()))
                    #:decode-body? #f
                    #:body body)
    (values res body)))

(define* (request method path
                  #:key
                  json
                  authorization?)
  (receive (res body)
      (raw-request method path
                   #:body (if json
                              (scm->json-string json)
                              #f)
                   #:headers (if json
                                 '((content-type application/json))
                                 '())
                   #:authorization? authorization?)
 (if (= 200 (response-code res))
        (values res (json-string->scm (utf8->string body)))
        (values res body))))

(define*-public (account id)
  (receive (res body)
      (/api/v1/accounts/:id id)
    (case (response-code res)
      ((200) (make-account body))
      ((401)
       (error "account: Unauthorized" id))
      ((404)
       (error "account: Not Found" id))
      ((410)
       (error "account: Account is suspended" id))
      (else
       (error "account: failed"
              (response-code res)
              (utf8->string body))))))

(define* (/api/v1/accounts/:id id)
  (request
   'GET
   (format #f "/api/v1/accounts/~a" id)))