/
EncodeDecode.elm
2574 lines (2085 loc) · 74.9 KB
/
EncodeDecode.elm
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
----------------------------------------------------------------------
--
-- EncodeDecode.elm
-- JSON Encoders and Decoders
-- Copyright (c) 2019 Bill St. Clair <billstclair@gmail.com>
-- Some rights reserved.
-- Distributed under the MIT License
-- See LICENSE
--
----------------------------------------------------------------------
module Mastodon.EncodeDecode exposing
( encodeEntity, entityDecoder, entityValue
, accountDecoder, encodeAccount
, fieldDecoder, encodeField
, appDecoder, encodeApp
, sourceDecoder, encodeSource
, tokenDecoder, encodeToken
, applicationDecoder, encodeApplication
, attachmentDecoder, encodeAttachment
, cardDecoder, encodeCard
, contextDecoder, encodeContext
, visibilityDecoder, encodeVisibility
, emojiDecoder, encodeEmoji
, encodeStatus, statusDecoder
, encodeStatusSource, statusSourceDecoder
, rawStatusDecoder, simpleStatusDecoder, canFailStatusDecoder
, encodeHistoryStatus, historyStatusDecoder
, encodeError, errorDecoder
, encodePoll, pollDecoder
, encodePollDefinition, pollDefinitionDecoder
, encodeFilter, filterDecoder
, encodeFilterContext, filterContextDecoder
, encodeInstance, instanceDecoder, defaultedInstance
, encodeActivity, activityDecoder
, encodeListEntity, listEntityDecoder
, encodeNotification, notificationDecoder
, encodeNotificationType, notificationTypeDecoder
, encodePushSubscription, pushSubscriptionDecoder
, encodeRelationship, relationshipDecoder
, encodeResults, resultsDecoder
, encodeScheduledStatus, scheduledStatusDecoder
, encodeConversation, conversationDecoder
, encodeGroup, groupDecoder
, encodeGroupRelationship, groupRelationshipDecoder
, encodeTag, tagDecoder
, translationDecoder, encodeTranslation
, encodeAuthorization, authorizationDecoder
, encodeMaybe, encodePropertyAsList
, privacyToString, notificationTypeToString, filterContextToString
)
{-| Encoders and Decoders for JSON that goes over the wire.
You will rarely use any of these. Encoding and decoding will usually
be done for you by by `Mastodon.Requests.requestToRawRequest`, which
your code will call indirectly via `Mastodon.Requests.serverRequest`.
# For the one type to rule them all
@docs encodeEntity, entityDecoder, entityValue
# For the individual entity types
@docs accountDecoder, encodeAccount
@docs fieldDecoder, encodeField
@docs appDecoder, encodeApp
@docs sourceDecoder, encodeSource
@docs tokenDecoder, encodeToken
@docs applicationDecoder, encodeApplication
@docs attachmentDecoder, encodeAttachment
@docs cardDecoder, encodeCard
@docs contextDecoder, encodeContext
@docs visibilityDecoder, encodeVisibility
@docs emojiDecoder, encodeEmoji
@docs encodeStatus, statusDecoder
@docs encodeStatusSource, statusSourceDecoder
@docs rawStatusDecoder, simpleStatusDecoder, canFailStatusDecoder
@docs encodeHistoryStatus, historyStatusDecoder
@docs encodeError, errorDecoder
@docs encodePoll, pollDecoder
@docs encodePollDefinition, pollDefinitionDecoder
@docs encodeFilter, filterDecoder
@docs encodeFilterContext, filterContextDecoder
@docs encodeInstance, instanceDecoder, defaultedInstance
@docs encodeActivity, activityDecoder
@docs encodeListEntity, listEntityDecoder
@docs encodeNotification, notificationDecoder
@docs encodeNotificationType, notificationTypeDecoder
@docs encodePushSubscription, pushSubscriptionDecoder
@docs encodeRelationship, relationshipDecoder
@docs encodeResults, resultsDecoder
@docs encodeScheduledStatus, scheduledStatusDecoder
@docs encodeConversation, conversationDecoder
@docs encodeGroup, groupDecoder
@docs encodeGroupRelationship, groupRelationshipDecoder
@docs encodeTag, tagDecoder
@docs translationDecoder, encodeTranslation
# Encoder and decoder for the login parameters.
@docs encodeAuthorization, authorizationDecoder
# Utilities
@docs encodeMaybe, encodePropertyAsList
@docs privacyToString, notificationTypeToString, filterContextToString
-}
import Json.Decode as JD exposing (Decoder)
import Json.Decode.Pipeline as DP exposing (custom, hardcoded, optional, required)
import Json.Encode as JE exposing (Value)
import Mastodon.Entity as Entity
exposing
( Account
, Activity
, App
, Application
, Attachment
, AttachmentType(..)
, Authorization
, Card
, CardType(..)
, Context
, Conversation
, Emoji
, Entity(..)
, Error
, Field
, Filter
, FilterContext(..)
, Focus
, Group
, GroupRelationship
, History
, HistoryStatus
, ImageMetaFields
, ImageMetaInfo
, Instance
, ListEntity
, Mention
, Meta(..)
, Notification
, NotificationType(..)
, PleromaStatusContent
, Poll
, PollDefinition
, PollOption
, Privacy(..)
, PushSubscription
, RawStatus
, Relationship
, Results
, ScheduledStatus
, Source
, Stats
, Status
, StatusParams
, StatusSource
, Tag
, Token
, Translation
, URLs
, VideoMetaFields
, VideoMetaInfo
, Visibility(..)
, WrappedAccount(..)
, WrappedStatus(..)
)
import Url exposing (Protocol(..))
{-| Encode an `Entity` into a `Value`.
-}
encodeEntity : Entity -> Value
encodeEntity entity =
case entity of
NoEntity ->
JE.null
AccountEntity account ->
encodeAccount account
AppEntity app ->
encodeApp app
AccountListEntity accounts ->
JE.list encodeAccount accounts
SourceEntity source ->
encodeSource source
TokenEntity token ->
encodeToken token
ApplicationEntity application ->
encodeApplication application
CardEntity card ->
encodeCard card
ContextEntity context ->
encodeContext context
EmojiEntity emoji ->
encodeEmoji emoji
EmojiListEntity emojis ->
JE.list encodeEmoji emojis
ErrorEntity error ->
encodeError error
StatusEntity status ->
encodeStatus status
StatusSourceEntity source ->
encodeStatusSource source
PollEntity poll ->
encodePoll poll
StatusListEntity statuses ->
JE.list encodeStatus statuses
HistoryStatusListEntity statuses ->
JE.list encodeHistoryStatus statuses
FilterEntity filter ->
encodeFilter filter
FilterListEntity filters ->
JE.list encodeFilter filters
InstanceEntity instance ->
encodeInstance instance
ActivityEntity activity ->
encodeActivity activity
ActivityListEntity activities ->
JE.list encodeActivity activities
PeersEntity peers ->
JE.list JE.string peers
ListEntityEntity list ->
encodeListEntity list
ListEntityListEntity list ->
JE.list encodeListEntity list
AttachmentEntity attachment ->
encodeAttachment attachment
AttachmentListEntity attachments ->
JE.list encodeAttachment attachments
NotificationEntity notification ->
encodeNotification notification
NotificationListEntity notifications ->
JE.list encodeNotification notifications
PushSubscriptionEntity pushSubscription ->
encodePushSubscription pushSubscription
RelationshipEntity relationship ->
encodeRelationship relationship
RelationshipListEntity relationships ->
JE.list encodeRelationship relationships
ResultsEntity results ->
encodeResults results
ScheduledStatusEntity scheduledStatus ->
encodeScheduledStatus scheduledStatus
ScheduledStatusListEntity scheduledStatuses ->
JE.list encodeScheduledStatus scheduledStatuses
ConversationEntity conversation ->
encodeConversation conversation
ConversationListEntity conversations ->
JE.list encodeConversation conversations
GroupEntity group ->
encodeGroup group
GroupListEntity groups ->
JE.list encodeGroup groups
GroupRelationshipEntity relationship ->
encodeGroupRelationship relationship
GroupRelationshipListEntity relationships ->
JE.list encodeGroupRelationship relationships
TagListEntity tags ->
JE.list encodeTag tags
StringListEntity stringList ->
JE.list JE.string stringList
TranslationEntity translation ->
encodeTranslation translation
ValueEntity value ->
value
{-| Decode an `Entity`.
You'll usually know which entity you're looking for, and will use
its decoder explicitly. In case you don't...
The urlString is used only to decode an Instance.
It is used to default fields when the server returns {}
(which it shouldn't, but some do).
-}
entityDecoder : String -> Decoder Entity
entityDecoder urlString =
JD.oneOf
[ JD.null NoEntity
, translationDecoder |> JD.map TranslationEntity
, accountDecoder |> JD.map AccountEntity
, appDecoder |> JD.map AppEntity
, JD.list accountDecoder |> JD.map AccountListEntity
, sourceDecoder |> JD.map SourceEntity
, tokenDecoder |> JD.map TokenEntity
, applicationDecoder |> JD.map ApplicationEntity
, cardDecoder |> JD.map CardEntity
, contextDecoder |> JD.map ContextEntity
, emojiDecoder |> JD.map EmojiEntity
, JD.list emojiDecoder |> JD.map EmojiListEntity
, notificationDecoder |> JD.map NotificationEntity --must be before StatusEntity, don't know why
, canFailStatusDecoder |> JD.map StatusEntity
, statusSourceDecoder |> JD.map StatusSourceEntity
, pollDecoder |> JD.map PollEntity
, JD.list canFailStatusDecoder |> JD.map StatusListEntity
, JD.list historyStatusDecoder |> JD.map HistoryStatusListEntity
, filterDecoder |> JD.map FilterEntity
, JD.list filterDecoder |> JD.map FilterListEntity
, activityDecoder |> JD.map ActivityEntity
, JD.list activityDecoder |> JD.map ActivityListEntity
, groupDecoder |> JD.map GroupEntity -- Must come before ListEntity
, JD.list groupDecoder |> JD.map GroupListEntity
, groupRelationshipDecoder |> JD.map GroupRelationshipEntity
, JD.list groupRelationshipDecoder |> JD.map GroupRelationshipListEntity
, listEntityDecoder |> JD.map ListEntityEntity
, JD.list listEntityDecoder |> JD.map ListEntityListEntity
, attachmentDecoder |> JD.map AttachmentEntity
, JD.list attachmentDecoder |> JD.map AttachmentListEntity
, pushSubscriptionDecoder |> JD.map PushSubscriptionEntity
, relationshipDecoder |> JD.map RelationshipEntity
, JD.list relationshipDecoder |> JD.map RelationshipListEntity
, resultsDecoder |> JD.map ResultsEntity
, scheduledStatusDecoder |> JD.map ScheduledStatusEntity
, JD.list scheduledStatusDecoder |> JD.map ScheduledStatusListEntity
, conversationDecoder |> JD.map ConversationEntity
, JD.list tagDecoder |> JD.map TagListEntity
, JD.list JD.string |> JD.map StringListEntity
, instanceDecoder urlString |> JD.map InstanceEntity
]
{-| Similar to `encodeEntity`, but returns the `v` field, if it is non-null,
giving you the raw data that came over the wire, if this entity DID come over
the wire, and is not one of the few that don't have a `v` field.
-}
entityValue : Entity -> Value
entityValue entity =
case entity of
NoEntity ->
JE.null
AccountEntity account ->
if account.v == JE.null then
encodeAccount account
else
account.v
ActivityEntity activity ->
if activity.v == JE.null then
encodeActivity activity
else
activity.v
AccountListEntity accounts ->
JE.list entityValue (List.map AccountEntity accounts)
SourceEntity source ->
if source.v == JE.null then
encodeSource source
else
source.v
TokenEntity token ->
if token.v == JE.null then
encodeToken token
else
token.v
ApplicationEntity application ->
if application.v == JE.null then
encodeApplication application
else
application.v
CardEntity card ->
if card.v == JE.null then
encodeCard card
else
card.v
ContextEntity context ->
encodeContext context
EmojiEntity emoji ->
encodeEmoji emoji
EmojiListEntity emojis ->
JE.list encodeEmoji emojis
ErrorEntity error ->
encodeError error
StatusEntity status ->
if status.v == JE.null then
encodeStatus status
else
status.v
StatusSourceEntity source ->
if source.v == JE.null then
encodeStatusSource source
else
source.v
PollEntity poll ->
if poll.v == JE.null then
encodePoll poll
else
poll.v
StatusListEntity statuses ->
JE.list entityValue (List.map StatusEntity statuses)
HistoryStatusListEntity statuses ->
JE.list encodeHistoryStatus statuses
FilterEntity filter ->
if filter.v == JE.null then
encodeFilter filter
else
filter.v
FilterListEntity filters ->
JE.list entityValue (List.map FilterEntity filters)
InstanceEntity instance ->
if instance.v == JE.null then
encodeInstance instance
else
instance.v
ActivityListEntity activities ->
JE.list entityValue (List.map ActivityEntity activities)
AppEntity app ->
if app.v == JE.null then
encodeApp app
else
app.v
PeersEntity peers ->
JE.list JE.string peers
ListEntityEntity list ->
encodeListEntity list
ListEntityListEntity lists ->
JE.list encodeListEntity lists
AttachmentEntity attachment ->
if attachment.v == JE.null then
encodeAttachment attachment
else
attachment.v
AttachmentListEntity attachments ->
JE.list entityValue (List.map AttachmentEntity attachments)
NotificationEntity notification ->
if notification.v == JE.null then
encodeNotification notification
else
notification.v
NotificationListEntity notifications ->
JE.list entityValue (List.map NotificationEntity notifications)
PushSubscriptionEntity pushSubscription ->
if pushSubscription.v == JE.null then
encodePushSubscription pushSubscription
else
pushSubscription.v
RelationshipEntity relationship ->
if relationship.v == JE.null then
encodeRelationship relationship
else
relationship.v
RelationshipListEntity relationships ->
JE.list entityValue (List.map RelationshipEntity relationships)
ResultsEntity results ->
if results.v == JE.null then
encodeResults results
else
results.v
ScheduledStatusEntity scheduledStatus ->
if scheduledStatus.v == JE.null then
encodeScheduledStatus scheduledStatus
else
scheduledStatus.v
ScheduledStatusListEntity scheduledStatuses ->
JE.list entityValue (List.map ScheduledStatusEntity scheduledStatuses)
ConversationEntity conversation ->
if conversation.v == JE.null then
encodeConversation conversation
else
conversation.v
ConversationListEntity conversations ->
JE.list entityValue (List.map ConversationEntity conversations)
GroupEntity group ->
if group.v == JE.null then
encodeGroup group
else
group.v
GroupListEntity groups ->
JE.list entityValue (List.map GroupEntity groups)
GroupRelationshipEntity relationship ->
if relationship.v == JE.null then
encodeGroupRelationship relationship
else
relationship.v
GroupRelationshipListEntity relationships ->
JE.list entityValue (List.map GroupRelationshipEntity relationships)
TagListEntity tags ->
JE.list encodeTag tags
StringListEntity stringList ->
JE.list JE.string stringList
TranslationEntity translation ->
encodeTranslation translation
ValueEntity value ->
value
{-| Encode `Maybe x` to `Json.Encode.null` if it's Nothing,
or with the encoder otherwise.
-}
encodeMaybe : (x -> Value) -> Maybe x -> Value
encodeMaybe encoder mx =
case mx of
Nothing ->
JE.null
Just x ->
encoder x
{-| Useful for omitting encoded properties with a default value.
encodePropertyAsList name property encode default
Returns `[]`, if `property == default`.
Otherwise, returns:
[ ( name, encoder property ) ]
-}
encodePropertyAsList : String -> property -> (property -> Value) -> property -> List ( String, Value )
encodePropertyAsList name property encoder default =
if default == property then
[]
else
[ ( name, encoder property ) ]
unwrapAccount : WrappedAccount -> Account
unwrapAccount (WrappedAccount account) =
account
unwrapStatus : WrappedStatus -> Status
unwrapStatus (WrappedStatus status) =
status
{-| Encode an `Account`.
-}
encodeAccount : Account -> Value
encodeAccount account =
JE.object <|
List.concat
[ [ ( "id", JE.string account.id )
, ( "username", JE.string account.username )
, ( "acct", JE.string account.acct )
, ( "display_name", JE.string account.display_name )
, ( "locked", JE.bool account.locked )
, ( "created_at", JE.string account.created_at )
, ( "followers_count", JE.int account.followers_count )
, ( "following_count", JE.int account.following_count )
, ( "statuses_count", JE.int account.statuses_count )
, ( "note", JE.string account.note )
, ( "url", JE.string account.url )
, ( "avatar", JE.string account.avatar )
, ( "avatar_static", JE.string account.avatar_static )
, ( "header", JE.string account.header )
, ( "header_static", JE.string account.header_static )
, ( "emojis", JE.list encodeEmoji account.emojis )
, ( "moved", encodeMaybe (unwrapAccount >> encodeAccount) account.moved )
, ( "fields", JE.list encodeField account.fields )
, ( "bot", JE.bool account.bot )
]
, case account.source of
Nothing ->
[]
Just source ->
[ ( "source", encodeSource source ) ]
, if
account.is_pro
|| account.is_verified
|| account.is_donor
|| account.is_investor
then
[ ( "is_pro", JE.bool account.is_pro )
, ( "is_verified", JE.bool account.is_verified )
, ( "is_donor", JE.bool account.is_donor )
, ( "is_investor", JE.bool account.is_investor )
]
else
[]
]
jsIntDecoder : Decoder Int
jsIntDecoder =
JD.oneOf
[ JD.int
, JD.string
|> JD.andThen
(\s ->
case String.toInt s of
Nothing ->
JD.fail <| "Not an integer string: " ++ s
Just i ->
JD.succeed i
)
]
{-| Decode an `Account`.
-}
accountDecoder : Decoder Account
accountDecoder =
JD.succeed Account
|> required "id" JD.string
|> required "username" JD.string
|> required "acct" JD.string
|> required "display_name" JD.string
|> required "locked" JD.bool
|> required "created_at" JD.string
|> required "followers_count" jsIntDecoder
|> required "following_count" jsIntDecoder
|> required "statuses_count" jsIntDecoder
|> required "note" JD.string
|> required "url" JD.string
|> required "avatar" JD.string
|> required "avatar_static" JD.string
|> required "header" JD.string
|> required "header_static" JD.string
|> required "emojis" (JD.list emojiDecoder)
|> optional "moved"
(JD.lazy
(\() ->
JD.nullable
(accountDecoder |> JD.map WrappedAccount)
)
)
Nothing
|> optional "fields"
(JD.list fieldDecoder)
[]
|> optional "bot" JD.bool False
|> optional "source" (JD.nullable sourceDecoder) Nothing
|> optional "is_pro" optionalBoolDecoder False
|> optional "is_verified" optionalBoolDecoder False
|> optional "is_donor" optionalBoolDecoder False
|> optional "is_investor" optionalBoolDecoder False
|> custom JD.value
{-| Encode an `App`.
-}
encodeApp : App -> Value
encodeApp app =
JE.object
[ ( "id", JE.string app.id )
, ( "name", JE.string app.name )
, ( "website", encodeMaybe JE.string app.website )
, ( "redirect_uri", JE.string app.redirect_uri )
, ( "client_id", JE.string app.client_id )
, ( "client_secret", JE.string app.client_secret )
, ( "vapid_key", encodeMaybe JE.string app.vapid_key )
]
{-| Decode an `App`.
-}
appDecoder : Decoder App
appDecoder =
JD.succeed App
|> required "id" JD.string
|> required "name" JD.string
|> optional "website" (JD.nullable JD.string) Nothing
|> required "redirect_uri" JD.string
|> required "client_id" JD.string
|> required "client_secret" JD.string
|> optional "vapid_key" (JD.nullable JD.string) Nothing
|> custom JD.value
{-| Encode an `Emoji`.
-}
encodeEmoji : Emoji -> Value
encodeEmoji emoji =
JE.object <|
List.concat
[ [ ( "shortcode", JE.string emoji.shortcode )
, ( "static_url", JE.string emoji.static_url )
, ( "url", JE.string emoji.url )
, ( "visible_in_picker", JE.bool emoji.visible_in_picker )
]
, case emoji.category of
Nothing ->
[]
Just category ->
[ ( "category", JE.string category ) ]
]
{-| Decode an `Emoji`.
-}
emojiDecoder : Decoder Emoji
emojiDecoder =
JD.succeed Emoji
|> required "shortcode" JD.string
|> required "static_url" JD.string
|> required "url" JD.string
|> required "visible_in_picker" JD.bool
|> optional "category" (JD.nullable JD.string) Nothing
{-| Encode a `Field`.
-}
encodeField : Field -> Value
encodeField field =
JE.object
[ ( "name", JE.string field.name )
, ( "value", JE.string field.value )
, ( "verified_at", encodeMaybe JE.string field.verified_at )
]
{-| Decode a `Field`.
-}
fieldDecoder : Decoder Field
fieldDecoder =
JD.succeed Field
|> required "name" JD.string
|> required "value" JD.string
|> optional "verified_at" (JD.nullable JD.string) Nothing
{-| Convert `Privacy` to a `String`.
-}
privacyToString : Privacy -> String
privacyToString privacy =
case privacy of
PublicPrivacy ->
"public"
UnlistedPrivacy ->
"unlisted"
PrivatePrivacy ->
"private"
encodePrivacy : Privacy -> Value
encodePrivacy privacy =
privacyToString privacy
|> JE.string
privacyDecoder : Decoder Privacy
privacyDecoder =
JD.string
|> JD.andThen
(\s ->
case s of
"public" ->
JD.succeed PublicPrivacy
"unlisted" ->
JD.succeed UnlistedPrivacy
"private" ->
JD.succeed PrivatePrivacy
_ ->
JD.fail <| "Unknown privacy: " ++ s
)
{-| Encode a `Source`.
-}
encodeSource : Source -> Value
encodeSource source =
JE.object
[ ( "privacy", encodePrivacy source.privacy )
, ( "sensitive", JE.bool source.sensitive )
, ( "language", encodeMaybe JE.string source.language )
, ( "note", JE.string source.note )
, ( "fields", JE.list encodeField source.fields )
]
{-| Decode a `Source`.
-}
sourceDecoder : Decoder Source
sourceDecoder =
JD.succeed Source
|> optional "privacy"
(JD.oneOf
[ JD.null PublicPrivacy
, privacyDecoder
]
)
PublicPrivacy
|> optional "sensitive" JD.bool False
|> optional "language" (JD.nullable JD.string) Nothing
|> required "note" JD.string
|> optional "fields" (JD.list fieldDecoder) []
|> custom JD.value
{-| Encode a `Token`.
-}
encodeToken : Token -> Value
encodeToken token =
JE.object
[ ( "access_token", JE.string token.access_token )
, ( "token_type", JE.string token.token_type )
, ( "scope", JE.string token.scope )
, ( "created_at", JE.int token.created_at )
]
{-| Decode a `Token`.
-}
tokenDecoder : Decoder Token
tokenDecoder =
JD.succeed Token
|> required "access_token" JD.string
|> required "token_type" JD.string
|> required "scope" JD.string
|> required "created_at" jsIntDecoder
|> custom JD.value
{-| Encode an `Application`.
-}
encodeApplication : Application -> Value
encodeApplication application =
JE.object
[ ( "name", JE.string application.name )
, ( "website", encodeMaybe JE.string application.website )
]
{-| Decode an `Application`.
-}
applicationDecoder : Decoder Application
applicationDecoder =
JD.succeed Application
|> required "name" JD.string
|> optional "website" (JD.nullable JD.string) Nothing
|> custom JD.value
encodeAttachmentType : AttachmentType -> Value
encodeAttachmentType attachmentType =
case attachmentType of
UnknownAttachment ->
JE.string "unknown"
ImageAttachment ->
JE.string "image"
GifvAttachment ->
JE.string "gifv"
VideoAttachment ->
JE.string "video"
AudioAttachment ->
JE.string "audio"
UnrecognizedAttachment t ->
JE.string t
attachmentTypeDecoder : Decoder AttachmentType
attachmentTypeDecoder =
JD.string
|> JD.andThen
(\s ->
case s of
"unknown" ->
JD.succeed UnknownAttachment
"image" ->
JD.succeed ImageAttachment
"gifv" ->
JD.succeed GifvAttachment
"video" ->
JD.succeed VideoAttachment
"audio" ->
JD.succeed AudioAttachment
_ ->