-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
queries.lisp
1188 lines (961 loc) · 46.9 KB
/
queries.lisp
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(in-package #:org.shirakumo.tooter)
(defun pagination-url (handle)
(cdr handle))
(defun pagination-decoding-function (handle)
(car handle))
(defun make-pagination-handle (decoding-function url)
(cons decoding-function url))
(defmacro with-pagination-return ((decoding-function) &body body)
(a:with-gensyms (return-body return-headers link-to-next-page link-to-previous-page)
(let ((actual-decoding-function (if (symbolp decoding-function)
`(function ,decoding-function)
decoding-function)))
`(multiple-value-bind (,return-body ,return-headers)
,@body
(multiple-value-bind (,link-to-next-page ,link-to-previous-page)
(tooter-link-header-parser:find-pagination-links ,return-headers)
(values (funcall ,actual-decoding-function ,return-body)
(make-pagination-handle ,actual-decoding-function
,link-to-next-page)
(make-pagination-handle ,actual-decoding-function
,link-to-previous-page)
,return-headers))))))
(defun navigate-page (client handle)
(when (pagination-url handle)
(with-pagination-return ((pagination-decoding-function handle))
(query-url client (pagination-url handle)))))
(defmacro do-pages ((client page &key (direction :next)) start-form &body body)
(assert (member direction '(:next :previous)))
(a:with-gensyms (first-results
previous-results
results-decoded-entity
results-next-handle
results-previous-handle
saved-results
loop-name)
`(flet ((,results-decoded-entity (results)
(first results))
,(if (eq direction :next)
`(,results-next-handle (results)
(second results))
`(,results-previous-handle (results)
(third results))))
(let ((,page nil)
(,first-results :unitialized)
(,previous-results nil))
(loop named ,loop-name do
(let ((,saved-results ,previous-results))
(cond
((eq ,first-results :unitialized)
(setf ,first-results (multiple-value-list ,start-form))
(setf ,previous-results ,first-results))
(t
(setf ,previous-results
,(if (eq direction :next)
`(multiple-value-list
(navigate-page ,client
(,results-next-handle ,previous-results)))
`(multiple-value-list
(navigate-page ,client
(,results-previous-handle ,previous-results)))))))
(setf ,page (,results-decoded-entity ,previous-results))
(cond
(,page
,@body)
(t
(return-from ,loop-name (,results-decoded-entity ,saved-results))))))))))
(defmacro collect-all-pages (client starting-form)
(a:with-gensyms (results page)
`(let ((,results '()))
(do-pages (,client ,page :direction :next)
,starting-form
(setf ,results (nconc ,results ,page)))
,results)))
;; Announcement
(defmethod get-announcements ((client client))
(decode-announcement (query client "/api/v1/announcements")))
(defmethod dismiss-announcement ((client client) (id string))
(submit client (format nil "/api/v1/announcements/~a/dismiss" id)))
(defmethod add-reaction-announcement ((client client) (id string) (name string))
(submit client
(format NIL "/api/v1/announcements/~a/reactions/~a" id name)
:http-method :put))
(defmethod dismiss-reaction-announcement ((client client) (id string) (name string))
(submit client
(format NIL "/api/v1/announcements/~a/reactions/~a" id name)
:http-method :delete))
;; Application
(defmethod verify-app-credentials ((client client))
(decode-application (query client "/api/v1/apps/verify_credentials")))
;;; Accounts
(defmethod find-account ((client client) (id string))
(decode-account (query client (format NIL "/api/v1/accounts/~a" id))))
(defmethod verify-credentials ((client client))
(setf (account client)
(decode-account (query client "/api/v1/accounts/verify_credentials"))))
(defmethod update-credentials ((client client) &key display-name note avatar header (locked NIL l-p) fields)
(check-type display-name (or null string))
(check-type note (or null string))
(check-type avatar (or null pathname))
(check-type header (or null pathname))
(check-type fields list)
(setf (account client)
(decode-account (apply #'submit client "/api/v1/accounts/update_credentials"
:display-name display-name
:note note
:avatar avatar
:header header
:locked (coerce-boolean locked l-p)
(loop for i from 0
for (key . val) in fields
collect (format NIL "fields_attributes[~a][name]" i)
collect key
collect (format NIL "fields_attributes[~a][value]" i)
collect val)))))
(defmethod get-followers ((client client) (id string) &key max-id since-id limit)
(check-type max-id (or null string))
(check-type since-id (or null string))
(check-type limit (or null (integer 0)))
(with-pagination-return (decode-account)
(query client (format NIL "/api/v1/accounts/~a/followers" id)
:max-id max-id
:since-id since-id
:limit limit)))
(defmethod get-followers ((client client) (account account) &rest args)
(apply #'get-followers client (id account) args))
(defmethod get-followers ((client client) (self (eql T)) &rest args)
(apply #'get-followers client (id (account client)) args))
(defmethod get-following ((client client) (id string) &key max-id since-id limit)
(check-type max-id (or null string))
(check-type since-id (or null string))
(check-type limit (or null (integer 0)))
(with-pagination-return (decode-account)
(query client (format NIL "/api/v1/accounts/~a/following" id)
:max-id max-id
:since-id since-id
:limit limit)))
(defmethod get-following ((client client) (account account) &rest args)
(apply #'get-following client (id account) args))
(defmethod get-following ((client client) (self (eql T)) &rest args)
(apply #'get-following client (id (account client)) args))
(defmethod get-statuses ((client client) (id string) &key (only-media NIL o-p) (pinned NIL p-p) (exclude-replies NIL e-p) max-id since-id limit)
(check-type max-id (or null string))
(check-type since-id (or null string))
(check-type limit (or null (integer 0)))
(decode-status (query client (format NIL "/api/v1/accounts/~a/statuses" id)
:only-media (coerce-boolean only-media o-p)
:pinned (coerce-boolean pinned p-p)
:exclude-replies (coerce-boolean exclude-replies e-p)
:max-id max-id
:since-id since-id
:limit limit)))
(defmethod get-statuses ((client client) (account account) &rest args)
(apply #'get-statuses client (id account) args))
(defmethod get-statuses ((client client) (self (eql T)) &rest args)
(apply #'get-statuses client (id (account client)) args))
;;; Bookmarks
(defmethod bookmarks ((client client) &key max-id since-id min-id (limit 20))
(check-type max-id (or null string))
(check-type since-id (or null string))
(check-type min-id (or null string))
(check-type limit (or null (integer 0)))
(with-pagination-return (decode-status)
(query client "/api/v1/bookmarks"
:max-id max-id
:since-id since-id
:min-id min-id
:limit limit)))
(defmethod bookmark ((client client) (id string))
(check-type id string)
(decode-status (submit client (format NIL "/api/v1/statuses/~a/bookmark" id))))
(defmethod bookmark ((client client) (status status))
(bookmark client (id status)))
(defmethod unbookmark ((client client) (id string))
(check-type id string)
(decode-status (submit client (format NIL "/api/v1/statuses/~a/unbookmark" id))))
(defmethod unbookmark ((client client) (status status))
(unbookmark client (id status)))
;;; Filters
(defmethod filters ((client v2:client))
(decode-filter (query client "/api/v2/filters")))
(defmethod filter ((client v2:client) (id string))
(decode-filter (query client (format NIL "/api/v2/filters/~a" id))))
(defmethod filter ((client v2:client) (filter filter))
(filter client (id filter)))
(defun check-filter-action (value)
(assert (member value '("warn" "hide") :test #'string=)))
(defun check-filter-context (value)
(assert (consp value))
(loop for keyword in value do
(assert (member keyword '
("home" "notifications" "public" "thread" "account")
:test #'string=))))
(defun encode-filter-context (context)
(loop for i from 0
for destination in context
collect (format nil "context[~a]" i)
collect destination))
(defmethod create-filter ((client v2:client) title context
&key expires-in
(filter-action "hide")
(fields '()))
(check-filter-context context)
(check-filter-action filter-action)
(assert (stringp title))
(assert (consp context))
(decode-filter (apply #'submit
client
"/api/v2/filters"
:title title
(encode-filter-context context)
:filter-action filter-action
:expires-in expires-in
(loop for i from 0
for (key . val) in fields
collect (format NIL "fields_attributes[~a][keyword]" i)
collect key
collect (format NIL "fields_attributes[~a][whole_word]" i)
collect val))))
(defun make-update-filter-field (id keyword &key (whole-word nil) (destroy nil))
(list id keyword whole-word destroy))
(defmethod update-filter ((client v2:client) id title context
&key (filter-action "hide") expires-in (fields '()))
(assert (stringp id))
(assert (stringp title))
(check-filter-context context)
(check-filter-action filter-action)
(decode-filter (apply #'submit
client
(format NIL "/api/v2/filters/~a" id)
:http-method :put
:id id
:title title
:filter-action filter-action
:expires-in expires-in
(loop for i from 0
for attribute in fields
collect (format NIL "keyword_attributes[~a][keyword]" i)
collect (first attribute)
collect (format NIL "keyword_attributes[~a][whole_word]" i)
collect (second attribute)
collect (format NIL "keyword_attributes[~a][id]" i)
collect (third attribute)
collect (format NIL "keyword_attributes[~a][_destroy]" i)
collect (fourth attribute))
(encode-filter-context context))))
(defmethod delete-filter ((client v2:client) id)
(assert (stringp id))
(submit client
(format NIL "/api/v2/filters/~a" id)
:http-method :delete))
(defmethod find-filter ((client v2:client) id)
(assert (stringp id))
(decode-filter (query client (format NIL "/api/v2/filters/~a" id))))
(defmethod filter-keywords ((client v2:client) filter-id)
(assert (stringp filter-id))
(decode-filter-keyword (query client (format NIL "/api/v2/filters/~a/keywords" filter-id))))
(defmethod add-filter-keyword ((client v2:client) filter-id keyword &key (whole-word NIL w-p))
(assert (stringp filter-id))
(assert (stringp keyword))
(decode-filter-keyword (submit client
(format NIL "/api/v2/filters/~a/keywords" filter-id)
:keyword keyword
"whole_word" (coerce-boolean whole-word w-p))))
(defmethod remove-filter-keyword ((client v2:client) filter-keyword-id)
(assert (stringp filter-keyword-id))
(query client
(format NIL "/api/v2/filters/keywords/~a" filter-keyword-id)
:http-method :delete))
;;; Follows
(defmethod follow ((client client) (id string))
(decode-relationship (submit client (format NIL "/api/v1/accounts/~a/follow" id))))
(defmethod follow ((client client) (account account))
(follow client (id account)))
(defmethod unfollow ((client client) (id string))
(decode-relationship (submit client (format NIL "/api/v1/accounts/~a/unfollow" id))))
(defmethod unfollow ((client client) (account account))
(unfollow client (id account)))
(defmethod block ((client client) (id string))
(decode-relationship (submit client (format NIL "/api/v1/accounts/~a/block" id))))
(defmethod block ((client client) (account account))
(block client (id account)))
(defmethod unblock ((client client) (id string))
(decode-relationship (submit client (format NIL "/api/v1/accounts/~a/unblock" id))))
(defmethod unblock ((client client) (account account))
(unblock client (id account)))
(defmethod mute ((client client) (id string) &key (notifications T n-p))
(decode-relationship (submit client (format NIL "/api/v1/accounts/~a/mute" id)
:notifications (coerce-boolean notifications n-p))))
(defmethod mute ((client client) (account account) &rest args)
(apply #'mute client (id account) args))
(defmethod unmute ((client client) (id string))
(decode-relationship (submit client (format NIL "/api/v1/accounts/~a/unmute" id))))
(defmethod unmute ((client client) (account account))
(unblock client (id account)))
(defmethod get-activity ((client client))
(decode-activity (query client "/api/v1/instance/activity")))
(defmethod relationships ((client client) (ids cons))
(decode-relationship (query client "/api/v1/accounts/relationships"
:id (loop for id in ids
collect (etypecase id
(account (id id))
(string id))))))
(defmethod relationships ((client client) (account account))
(relationships client (list (id account))))
(defmethod search-accounts ((client client) query &key (limit 40) (following NIL f-p) (resolve NIL r-p) (offset 0))
(check-type query string)
(check-type limit (or null (integer 0)))
(decode-account (query client "/api/v1/accounts/search"
:q query
:limit limit
:offset offset
:following (coerce-boolean following f-p)
:resolve (coerce-boolean resolve r-p))))
;;; Blocks
(defmethod blocks ((client client) &key max-id since-id (limit 40))
(check-type max-id (or null string))
(check-type since-id (or null string))
(check-type limit (or null (integer 0)))
(with-pagination-return (decode-account)
(query client "/api/v1/blocks"
:max-id max-id
:since-id since-id
:limit limit)))
(defmethod blocked-domains ((client client) &key max-id since-id (limit 40))
(check-type max-id (or null string))
(check-type since-id (or null string))
(check-type limit (or null (integer 0)))
(with-pagination-return (identity)
(query client "/api/v1/domain_blocks"
:max-id max-id
:since-id since-id
:limit limit)))
(defmethod block ((client client) (domain string))
(submit client "/api/v1/domain_blocks"
:domain domain)
T)
(defmethod unblock ((client client) (domain string))
(submit client "/api/v1/domain_blocks"
:http-method :delete
:domain domain)
T)
;;; Favourites
(defmethod favourites ((client client) &key min-id max-id (limit 20))
(check-type max-id (or null string))
(check-type min-id (or null string))
(check-type limit (or null (integer 0)))
(with-pagination-return (decode-status)
(query client "/api/v1/favourites"
:min-id min-id
:max-id max-id
:limit limit)))
;;; Follow Requests
(defmethod follow-requests ((client client) &key max-id since-id (limit 40))
(check-type max-id (or null string))
(check-type since-id (or null string))
(check-type limit (or null (integer 0)))
(with-pagination-return (decode-account)
(query client "/api/v1/follow_requests"
:max-id max-id
:since-id since-id
:limit limit)))
(defmethod accept-request ((client client) (id string))
(decode-relationship (submit client (format NIL "/api/v1/follow_requests/~a/authorize" id))))
(defmethod accept-request ((client client) (account account))
(accept-request client (id account)))
(defmethod reject-request ((client client) (id string))
(decode-relationship (submit client (format NIL "/api/v1/follow_requests/~a/reject" id))))
(defmethod reject-request ((client client) (account account))
(reject-request client (id account)))
;;; Instances
(defmethod instance ((client client))
(decode-instance (query client "/api/v1/instance")))
(defmethod instance ((client v2:client))
(decode-instance (query client "/api/v2/instance")))
(defmethod peers ((client client))
(query client "/api/v1/instance/peers"))
(defmethod weekly-activity ((client client))
(decode-activity (query client "/api/v1/instance/activity")))
(defmethod emojis ((client client))
(decode-emoji (query client "/api/v1/custom_emojis")))
;;; Lists
(defmethod user-lists ((client client) (id string))
(decode-user-list (query client (format NIL "/api/v1/accounts/~a/lists" id))))
(defmethod user-lists ((client client) (account account))
(user-lists client (id account)))
(defmethod user-lists ((client client) (id (eql T)))
(decode-user-list (query client "/api/v1/lists")))
(defmethod user-list-accounts ((client client) (id string) &key max-id since-id limit)
(check-type max-id (or null string))
(check-type since-id (or null string))
(check-type limit (or null (integer 0)))
(with-pagination-return (decode-account)
(query client (format NIL "/api/v1/lists/~a/accounts" id)
:max-id max-id
:since-id since-id
:limit (or limit 0))))
(defmethod user-list-accounts ((client client) (user-list user-list) &rest args)
(apply #'user-list-accounts client (id user-list) args))
;; Convenience.
(defmethod (setf user-list-accounts) (accounts (client client) (id string))
(let* ((accounts (loop for account in accounts
collect (etypecase account
(string account)
(account (id account)))))
(existing (mapcar #'id (user-list-accounts client id)))
(to-remove (set-difference existing accounts))
(to-add (set-difference accounts existing)))
(when to-remove (remove-user-list-accounts client id to-remove))
(when to-add (add-user-list-accounts client id to-add))
accounts))
(defmethod find-list ((client client) (id string))
(decode-user-list (query client (format NIL "/api/v1/lists/~a" id))))
(defun check-list-replies-policy-to-values (value)
(assert (member value '("followed" "list" "none") :test #'string=)))
(defmethod make-user-list ((client client) title &key (replies-policy :list) (exclusive nil))
(check-list-replies-policy-to-values replies-policy)
(decode-user-list (submit client "/api/v1/lists"
:replies-policy replies-policy
:exclusive exclusive
:title title)))
(defmethod update-user-list ((client client) (id string)
&key title (replies-policy :list))
(check-type title string)
(check-list-replies-policy-to-values replies-policy)
(decode-user-list (submit client (format NIL "/api/v1/lists/~a" id)
:http-method :put
:replies-policy replies-policy
:title title)))
(defmethod update-user-list ((client client) (user-list user-list) &rest args)
(apply #'update-user-list client (id user-list) args))
(defmethod delete-user-list ((client client) (id string))
(submit client (format NIL "/api/v1/lists/~a" id)
:http-method :delete)
T)
(defmethod add-user-list-accounts ((client client) (id string) accounts)
(submit client (format NIL "/api/v1/lists/~a/accounts" id)
:account-ids (loop for account in accounts
collect (etypecase account
(string account)
(account (id account)))))
T)
(defmethod add-user-list-accounts ((client client) (user-list user-list) accounts)
(add-user-list-accounts client (id user-list) accounts))
(defmethod remove-user-list-accounts ((client client) (id string) accounts)
(submit client (format NIL "/api/v1/lists/~a/accounts" id)
:http-method :delete
:account-ids (loop for account in accounts
collect (etypecase account
(string account)
(account (id account)))))
T)
(defmethod remove-user-list-accounts ((client client) (user-list user-list) accounts)
(remove-user-list-accounts client (id user-list) accounts))
;;; Media
(defmethod make-media ((client client) file &key description focus)
(check-type file pathname)
(check-type description (or null string))
(check-type focus (or null cons))
(decode-attachment (submit client "/api/v1/media"
:file file
:description description
:focus (when focus (format NIL "~f,~f" (car focus) (cdr focus))))))
(defmethod update-media ((client client) id &key description focus)
(check-type description (or null string))
(check-type focus (or null cons))
(decode-attachment (submit client (format NIL "/api/v1/media/~a" id)
:description description
:focus (when focus (format NIL "~f,~f" (car focus) (cdr focus))))))
(defmethod update-media ((client client) (attachment attachment) &rest args)
(apply #'update-media client (id attachment) args))
;;; Mutes
(defmethod mutes ((client client) &key max-id since-id (limit 40))
(check-type max-id (or null string))
(check-type since-id (or null string))
(check-type limit (or null (integer 0)))
(with-pagination-return (decode-account)
(query client "/api/v1/mutes"
:max-id max-id
:since-id since-id
:limit limit)))
;;; Notifications
(defun encode-notification-type (encoded-type)
(ecase encoded-type
(:admin.report "admin.report")
(:admin.sign-up "admin.sign_up")
(:favourite "favourite")
(:follow "follow")
(:follow-request "follow_request")
(:mention "mention")
(:move "move")
(:poll "poll")
(:reblog "reblog")
(:severed-relationships "severed_relationships")
(:status "status")
(:update "update")))
(defun encode-grouped-notification-type (encoded-type)
(ecase encoded-type
(:favourite "favourite")
(:follow "follow")
(:reblog "reblog")))
(defmethod notifications ((client client)
&key max-id
min-id
since-id
(limit 15)
exclude-types
types
account-id)
(check-type max-id (or null string))
(check-type since-id (or null string))
(check-type limit (or null (integer 0)))
(check-type exclude-types list)
(with-pagination-return (decode-notification)
(query client "/api/v1/notifications"
:max-id max-id
:min-id min-id
:since-id since-id
:limit limit
:types (loop for type in types
collect (encode-notification-type type))
:exclude-types (loop for type in exclude-types
collect (encode-notification-type type))
:account-id account-id)))
(defmethod find-notification ((client client) (id string))
(decode-notification (query client (format NIL "/api/v1/notifications/~a" id))))
(defmethod delete-notification ((client client) (all (eql T)))
(submit client "/api/v1/notifications/clear")
T)
(defmethod delete-notification ((client client) (id string))
(submit client (format nil "/api/v1/notifications/~a/dismiss" id))
T)
(defmethod delete-notification ((client client) (notification notification))
(delete-notification client (id notification)))
;; grouped notifications
(defmethod grouped-notifications ((client v2:client)
&key max-id
min-id
since-id
(limit 15)
exclude-types
types
account-id
(expand-accounts :full)
grouped-types
include-filtered)
(check-type max-id (or null string))
(check-type since-id (or null string))
(check-type limit (or null (integer 0)))
(check-type exclude-types list)
(assert (member expand-accounts '(:full :partial-avatars)))
(with-pagination-return (decode-grouped-notifications-results)
(query client
"/api/v2/notifications"
:max-id max-id
:min-id min-id
:since-id since-id
:limit limit
:types
(loop for type in types
collect (encode-notification-type type))
:exclude-types
(loop for type in exclude-types
collect (encode-notification-type type))
:account-id account-id
:expand-accounts expand-accounts
:grouped-types
(loop for type in grouped-types
collect
(encode-grouped-notification-type type))
:include-filtered include-filtered)))
(defmethod find-grouped-notification ((client v2:client) (group-key string))
(decode-grouped-notifications-results (query client
(format NIL
"/api/v2/notifications/~a"
group-key))))
(defmethod delete-grouped-notification ((client v2:client) (group-key string))
(submit client (format nil "/api/v2/notifications/~a/dismiss" group-key))
T)
(defmethod get-notifications-requests ((client client) (id string) &key max-id since-id limit)
(check-type max-id (or null string))
(check-type since-id (or null string))
(check-type limit (or null (integer 0)))
(with-pagination-return (decode-account)
(decode-notification-request (query client (format NIL "/api/v1/notifications/requests")
:max-id max-id
:since-id since-id
:limit limit))))
(defmethod get-notification-requests ((client client) (id string))
(decode-notification-request (query client
(format nil "/api/v1/notifications/requests/~a"
id))))
(defun %apply-to-notification-requests (client id action)
(submit client (format nil
"/api/v1/notifications/requests/~a/~a"
id
action))
t)
(defmethod accept-notification-request ((client client) (id string))
(%apply-to-notification-requests client id "accept"))
(defmethod dismiss-notification-request ((client client) (id string))
(%apply-to-notification-requests client id "dismiss"))
(defun %apply-to-notification-requests-multiple (client ids action)
(submit client
(format nil
"/api/v1/notifications/requests/~a"
action)
(loop for id in ids
collect
(format nil "id[]")
collect id))
t)
(defmethod accept-multiple-notification-requests ((client client) (ids list))
(%apply-to-notification-requests-multiple client ids "accept"))
(defmethod dismiss-multiple-notification-requests ((client client) (ids list))
(%apply-to-notification-requests-multiple client ids "dismiss"))
(defmethod notifications-request-merged-p ((client client))
(%decode-check-notification-requests-merged
(query client
"/api/v1/notifications/requests/merged")))
(defmethod fetch-notification-policy ((client v2:client))
(decode-notification-policy (query client "/api/v2/notifications/policy")))
(defmethod update-notification-policy ((client v2:client)
&key
(for-not-following :accept)
(for-not-followers :accept)
(for-new-accounts :accept)
(for-private-mentions :accept)
(for-limited-accounts :accept))
(assert (member for-not-following '(:accept :filter :drop)))
(assert (member for-not-followers '(:accept :filter :drop)))
(assert (member for-new-accounts '(:accept :filter :drop)))
(assert (member for-private-mentions '(:accept :filter :drop)))
(assert (member for-limited-accounts '(:accept :filter :drop)))
(decode-notification-policy(submit client
(format NIL "/api/v2/notifications/policy")
:http-method :patch
:for-not-following for-not-following
:for-not-followers for-not-followers
:for-new-accounts for-new-accounts
:for-private-mentions for-private-mentions
:for-limited-accounts for-limited-accounts)))
(defmethod make-subscription ((client client) endpoint public-key secret &key alerts)
(check-type endpoint string)
(check-type public-key string)
(check-type secret string)
(check-type alerts list)
(decode-push-subscription (apply #'submit client "/api/v1/push/subscription"
"subscription[endpoint]" endpoint
"subscription[keys][p256dh]" public-key
"subscription[keys][auth]" secret
(loop for alert in alerts
collect (ecase alert
(:follows "data[alerts][follow]")
(:favourites "data[alerts][favourite]")
(:reblogs "data[alerts][reblogs]")
(:mentions "data[alerts][mention]")
(:polls "data[alerts][poll]"))
collect "true"))))
(defmethod subscription ((client client))
(decode-push-subscription (query client "/api/v1/push/subscription")))
(defmethod delete-subscription ((client client))
(submit client "/api/v1/push/subscription"
:http-method :delete)
T)
;;; Reports
(defmethod reports ((client client))
(decode-report (query client "/api/v1/reports")))
(defmethod make-report ((client client) (id string) &key statuses comment forward)
(check-type statuses list)
(check-type comment (or null string))
(decode-report (submit client "/api/v1/reports"
:account-id id
:status-ids (loop for status in statuses
collect (etypecase status
(string status)
(status (id status))))
:comment comment
:forward (coerce-boolean forward t))))
(defmethod make-report ((client client) (account account) &rest args)
(apply #'make-report client (id account) args))
;;; Search
(defmethod find-results ((client v2:client) query &key account-id max-id min-id kind (exclude-unreviewed NIL e-p) (resolve NIL r-p) (limit 20) (offset 0) (following NIL f-p))
(check-type account-id (or null string))
(check-type max-id (or null string))
(check-type min-id (or null string))
(check-type kind (or null string))
(assert (member kind '("accounts" "hashtags" "statuses") :test #'string=))
(decode-results (query client "/api/v2/search"
:q query
:account-id account-id
:max-id max-id
:min-id min-id
:type kind
:exclude-unreviewed (coerce-boolean exclude-unreviewed e-p)
:resolve (coerce-boolean resolve r-p)
:limit limit
:offset offset
:following (coerce-boolean following f-p))))
;;; Statuses
(defmethod find-status ((client client) (id string))
(decode-status (query client (format NIL "/api/v1/statuses/~a" id))))
(defmethod context ((client client) (id string))
(decode-context (query client (format NIL "/api/v1/statuses/~a/context" id))))
(defmethod context ((client client) (status status))
(context client (id status)))
(defmethod card ((client client) (id string))
(decode-card (query client (format NIL "/api/v1/statuses/~a/context" id))))
(defmethod card ((client client) (status status))
(card client (id status)))
(defmethod rebloggers ((client client) (id string) &key max-id since-id (limit 40))
(check-type max-id (or null string))
(check-type since-id (or null string))
(check-type limit (or null (integer 0)))
(with-pagination-return (decode-account)
(query client (format NIL "/api/v1/statuses/~a/reblogged_by" id)
:max-id max-id
:since-id since-id
:limit limit)))
(defmethod rebloggers ((client client) (status status) &rest args)
(apply #'rebloggers client (id status) args))
(defmethod favouriters ((client client) (id string) &key max-id since-id (limit 40))
(check-type max-id (or null string))
(check-type since-id (or null string))
(check-type limit (or null (integer 0)))
(with-pagination-return (decode-account)
(query client (format NIL "/api/v1/statuses/~a/favourited_by" id)
:max-id max-id
:since-id since-id
:limit limit)))
(defmethod favouriters ((client client) (status status) &rest args)
(apply #'favouriters client (id status) args))
(defmethod status-ensure-media-id ((client client) (media string))
(declare (ignore client))
media)
(defmethod status-ensure-media-id ((client client) (media attachment))
(declare (ignore client))
(id media))
(defmethod status-ensure-media-id ((client client) (media pathname))
(pathname (id (make-media client media))))
(defmethod status-ensure-media-id ((client client) (media null))
(declare (ignore client media))
nil)
(defmethod status-ensure-media-id ((client client) (media cons))
(mapcar (lambda (a) (status-ensure-media-id client a))
media))
(defmethod status-ensure-media-id ((client client) media)
(list (status-ensure-media-id client media)))
(defun status-ensure-language (language)
(when language
(string language)))
(defmethod make-status ((client client) status &key in-reply-to media (sensitive NIL s-p) spoiler-text visibility language scheduled-at poll-options poll-expire-seconds (poll-multiple NIL m-p) (poll-hide-totals NIL h-p) idempotency-key)
(let ((results-entity (submit client "/api/v1/statuses"
:status status
:in-reply-to-id (etypecase in-reply-to
(string in-reply-to)
(status (id in-reply-to))
(null NIL))
:media-ids (status-ensure-media-id client media)
:sensitive (coerce-boolean sensitive s-p)
:spoiler-text spoiler-text
:visibility (ecase visibility
((NIL) NIL)
(:direct "direct")
(:private "private")
(:unlisted "unlisted")
(:public "public"))
:language (status-ensure-language language)
:scheduled-at scheduled-at
"poll[options]" poll-options
"poll[expires-in]" poll-expire-seconds
"poll[multiple]" (coerce-boolean poll-multiple m-p)
"poll[hide_totals]" (coerce-boolean poll-hide-totals h-p)
:idempotency-key idempotency-key)))
(if scheduled-at
(decode-scheduled-status results-entity)
(decode-status results-entity))))
(defmethod delete-status ((client client) (id string))
(submit client (format NIL "/api/v1/statuses/~a" id)
:http-method :delete)
T)
(defmethod delete-status ((client client) (status status))
(delete-status client (id status)))
(defmethod edit-status ((client client) (status status) (text string) &key media (sensitive NIL s-p) spoiler-text language poll-options poll-expire-seconds (poll-multiple NIL m-p) (poll-hide-totals NIL h-p))
(edit-status client
(id status)
text
:media media
:sensitive (coerce-boolean sensitive s-p)
:spoiler-text spoiler-text
:language language
:poll-options poll-options
:poll-expire-seconds poll-expire-seconds
:poll-multiple (coerce-boolean poll-multiple m-p)
:poll-hide-totals (coerce-boolean poll-hide-totals h-p)))
(defmethod edit-status ((client client) (id string) (text string) &key media (sensitive NIL s-p) spoiler-text language poll-options poll-expire-seconds (poll-multiple NIL m-p) (poll-hide-totals NIL h-p))
(decode-status (submit client
(format nil "/api/v1/statuses/~a" id)
:http-method :put
:status text
:media-ids (status-ensure-media-id client media)
:sensitive (coerce-boolean sensitive s-p)
:spoiler-text spoiler-text
:language (status-ensure-language language)
"poll[options]" poll-options
"poll[expires-in]" poll-expire-seconds
"poll[multiple]" (coerce-boolean poll-multiple m-p)
"poll[hide_totals]" (coerce-boolean poll-hide-totals h-p))))
(defmethod reblog ((client client) (id string))
(decode-status (submit client (format NIL "/api/v1/statuses/~a/reblog" id))))
(defmethod reblog ((client client) (status status))
(reblog client (id status)))
(defmethod unreblog ((client client) (id string))
(decode-status (submit client (format NIL "/api/v1/statuses/~a/unreblog" id))))
(defmethod unreblog ((client client) (status status))
(unreblog client (id status)))
(defmethod favourite ((client client) (id string))
(decode-status (submit client (format NIL "/api/v1/statuses/~a/favourite" id))))
(defmethod favourite ((client client) (status status))
(favourite client (id status)))
(defmethod unfavourite ((client client) (id string))
(decode-status (submit client (format NIL "/api/v1/statuses/~a/unfavourite" id))))
(defmethod unfavourite ((client client) (status status))
(unfavourite client (id status)))
(defmethod endorsements ((client client))
(decode-account (query client "/api/v1/endorsements")))
(defmethod pin ((client client) (id string))
(decode-status (submit client (format NIL "/api/v1/statuses/~a/pin" id))))
(defmethod pin ((client client) (status status))