-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiTypes.fs
More file actions
1721 lines (1249 loc) · 65 KB
/
ApiTypes.fs
File metadata and controls
1721 lines (1249 loc) · 65 KB
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
// TODO: Generate this file from the source code of misskey-js, seriously.
module Misskey.Net.ApiTypes
open System.Text.Json.Nodes
module internal Utils =
let getNode (key: string) (json: JsonNode) =
match json.[key] with
| null -> failwithf "key %s not found" key
| node -> node
let tryGetNode (key: string) (json: JsonNode) =
try
getNode key json |> Some
with _ ->
None
//
let getWith (modifier: JsonNode -> 'T) (key: string) (json: JsonNode) = getNode key json |> modifier
let tryGetWith (modifier: JsonNode -> 'T) (key: string) (json: JsonNode) =
tryGetNode key json |> Option.map modifier
//
let private asValue (json: JsonNode) =
match json with
| :? JsonValue as value -> value
| _ -> failwith "invalid type: expected JsonValue"
let getValue: string -> JsonNode -> JsonValue = getWith asValue
let tryGetValue: string -> JsonNode -> JsonValue option = tryGetWith asValue
//
let private asArray (json: JsonNode) =
match json with
| :? JsonArray as array -> array
| _ -> failwith "invalid type: expected JsonArray"
let getArray: string -> JsonNode -> JsonArray = getWith asArray
let tryGetArray: string -> JsonNode -> JsonArray option = tryGetWith asArray
let getList key = getArray key >> Seq.toList
let tryGetList key =
tryGetArray key >> Option.map Seq.toList
let getListWith modifier key json =
getArray key json |> Seq.map modifier |> Seq.toList
let tryGetListWith modifier key json =
tryGetArray key json |> Option.map (Seq.map modifier >> Seq.toList)
//
let getType<'T> = getWith <| fun json -> json.GetValue<'T>()
let tryGetType<'T> = tryGetWith <| fun json -> json.GetValue<'T>()
let getTypeList<'T> = getListWith <| fun json -> json.GetValue<'T>()
let tryGetTypeList<'T> = tryGetListWith <| fun json -> json.GetValue<'T>()
let getString: string -> JsonNode -> string = getType
let tryGetString: string -> JsonNode -> string option = tryGetType
let getInt: string -> JsonNode -> int = getType
let tryGetInt: string -> JsonNode -> int option = tryGetType
let getBool: string -> JsonNode -> bool = getType
let tryGetBool: string -> JsonNode -> bool option = tryGetType
let getStringList: string -> JsonNode -> string list = getTypeList
let tryGetStringList: string -> JsonNode -> string list option = tryGetTypeList
let getStringListList: string -> JsonNode -> string list list =
getListWith (fun json ->
match json with
| :? JsonArray as array -> array |> Seq.toList |> List.map (fun json -> json.GetValue<string>())
| _ -> failwith "invalid type: expected JsonArray")
//
let toMap (json: JsonNode) =
match json with
| :? JsonObject as map -> map |> Seq.map (fun kv -> kv.Key, kv.Value) |> Map.ofSeq
| _ -> failwith "invalid type: expected JsonObject"
let getMap key json = getWith toMap key json
let tryGetMap key json = tryGetWith toMap key json
//
//
open Utils
let private checkNull (json: JsonNode) =
if json = null then
failwith "given json node is null"
/// <summary>
/// A class that wraps a JSON node. \
/// JSON ノードをラップするクラス.
/// </summary>
/// <param name="json">The JSON node. JSON ノード.</param>
/// <exception cref="System.Exception">
/// Thrown when the given JSON node is null. \
/// 与えられた JSON ノードが null のときにスローされます.
/// </exception>
type Data(json: JsonNode) =
do checkNull json
/// <summary>
/// Get an item of the JSON node. \
/// JSON ノードの項目を取得します.
/// </summary>
/// <param name="key">The key of the item. 項目のキー.</param>
/// <returns>The item of the JSON node. JSON ノードの項目.</returns>
/// <remarks>
/// This method is equivalent to `json[key]`. If the key is not found or the value is null, this method returns `null`. \
/// このメソッドは `json[key]` と等価です.キーが見つからないか値が null のとき,このメソッドは `null` を返します.
/// </remarks>
member __.Item
with get (key: string) = getNode key json
override __.ToString() = json.ToString()
/// <summary>
/// The JSON node. \
/// JSON ノード.
/// </summary>
member __.Json: JsonNode = json
member __.Get<'T>(key: string) =
getWith (fun json -> json.GetValue<'T>()) key json
member __.TryGet<'T>(key: string) =
tryGetWith (fun json -> json.GetValue<'T>()) key json
/// <summary>
/// Get an item of the JSON node. \
/// JSON ノードの項目を取得します.
/// </summary>
/// <param name="key">The key of the item. 項目のキー.</param>
/// <returns>The item of the JSON node. JSON ノードの項目.</returns>
/// <exception cref="System.Exception">
/// Thrown when the key is not found. キーが見つからなかったときにスローされます.
/// </exception>
member __.GetNode(key: string) = getNode key json
/// <summary>
/// If the key is found, get an item of the JSON node, otherwise return `None`. \
/// キーが見つかった場合,JSON ノードの項目を取得します.そうでない場合,`None` を返します.
/// </summary>
/// <param name="key">The key of the item. 項目のキー.</param>
/// <returns>The item of the JSON node or `None`. JSON ノードの項目または `None`.</returns>
member __.TryGetNode(key: string) = tryGetNode key json
// TODO: Write documentation for the rest of the members.
member __.GetValue(key: string) = getValue key json
member __.TryGetValue(key: string) = tryGetValue key json
member __.GetArray(key: string) = getArray key json
member __.TryGetArray(key: string) = tryGetArray key json
member __.GetList(key: string) = getList key json
member __.TryGetList(key: string) = tryGetList key json
member __.GetString(key: string) = getString key json
member __.TryGetString(key: string) = tryGetString key json
member __.GetInt(key: string) = getInt key json
member __.TryGetInt(key: string) = tryGetInt key json
member __.GetBool(key: string) = getBool key json
member __.TryGetBool(key: string) = tryGetBool key json
member __.GetStringList(key: string) = getStringList key json
member __.TryGetStringList(key: string) = tryGetStringList key json
member __.GetMap(key: string) = getMap key json
member __.TryGetMap(key: string) = tryGetMap key json
member __.GetListWith<'T> (modifier: JsonNode -> 'T) (key: string) = getListWith modifier key json
member __.TryGetListWith<'T> (modifier: JsonNode -> 'T) (key: string) = tryGetListWith modifier key json
//
type ID = string
type DateString = string
//
type Visibility =
| Public
| Home
| Followers
| Specified
//
type DriveFile(json: JsonNode) =
inherit Data(json)
new(data: Data) = DriveFile(data.Json)
static member get (key: string) (json: JsonNode) = json |> getNode key |> DriveFile
static member tryGet (key: string) (json: JsonNode) =
json |> tryGetNode key |> Option.map DriveFile
member this.Id: ID = this.GetString "id"
member this.CreatedAt: DateString = this.GetString "createdAt"
member this.IsSensitive: bool = this.GetBool "isSensitive"
member this.Name: string = this.GetString "name"
member this.ThumbnailUrl: string = this.GetString "thumbnailUrl"
member this.Url: string = this.GetString "url"
member this.Type: string = this.GetString "type"
member this.Size: int = this.GetInt "size"
member this.Md5: string = this.GetString "md5"
member this.Blurhash: string = this.GetString "blurhash"
member this.Comment: string option = this.TryGetString "comment"
//
type Instance(json: JsonNode) =
inherit Data(json)
new(data: Data) = Instance(data.Json)
static member get (key: string) (json: JsonNode) = json |> getNode key |> Instance
static member tryGet (key: string) (json: JsonNode) =
json |> tryGetNode key |> Option.map Instance
member this.Id: ID = this.GetString "id"
member this.CaughtAt: DateString = this.GetString "caughtAt"
member this.Host: string = this.GetString "host"
member this.UsersCount: int = this.GetInt "usersCount"
member this.NotesCount: int = this.GetInt "notesCount"
member this.FollowingCount: int = this.GetInt "followingCount"
member this.FollowersCount: int = this.GetInt "followersCount"
member this.DriveUsage: int = this.GetInt "driveUsage"
member this.DriveFiles: int = this.GetInt "driveFiles"
member this.LatestRequestSentAt: DateString option =
this.TryGetString "latestRequestSentAt"
member this.LatestStatus: int option = this.TryGetInt "latestStatus"
member this.LatestRequestReceivedAt: DateString option =
this.TryGetString "latestRequestReceivedAt"
member this.LastCommunicatedAt: DateString = this.GetString "lastCommunicatedAt"
member this.IsNotResponding: bool = this.GetBool "isNotResponding"
member this.IsSuspended: bool = this.GetBool "isSuspended"
member this.SoftwareName: string option = this.TryGetString "softwareName"
member this.SoftwareVersion: string option = this.TryGetString "softwareVersion"
member this.OpenRegistrations: bool option = this.TryGetBool "openRegistrations"
member this.Name: string option = this.TryGetString "name"
member this.Description: string option = this.TryGetString "description"
member this.MaintainerName: string option = this.TryGetString "maintainerName"
member this.MaintainerEmail: string option = this.TryGetString "maintainerEmail"
member this.IconUrl: string option = this.TryGetString "iconUrl"
member this.FaviconUrl: string option = this.TryGetString "faviconUrl"
member this.ThemeColor: string option = this.TryGetString "themeColor"
member this.InfoUpdatedAt: DateString option = this.TryGetString "infoUpdatedAt"
//
[<RequireQualifiedAccess; CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module User =
type Instance(json: JsonNode) =
inherit Data(json)
new(data: Data) = Instance(data.Json)
static member get = getWith Instance
static member tryGet = tryGetWith Instance
member this.Name: string option = this.TryGetString "name" // REMARK A name of a user is typed as string in the API document, but it is observed that it can be null.
member this.SoftwareName: string option = this.TryGetString "softwareName"
member this.SoftwareVersion: string option = this.TryGetString "softwareVersion"
member this.IconUrl: string option = this.TryGetString "iconUrl"
member this.FaviconUrl: string option = this.TryGetString "faviconUrl"
member this.ThemeColor: string option = this.TryGetString "themeColor"
type OnlineStatus =
| Online
| Active
| Offline
| Unknown
static member ofString(str: string) =
match str with
| "online" -> Online
| "active" -> Active
| "offline" -> Offline
| "unknown" -> Unknown
| _ -> failwithf "unknown online status: %s" str
type Emoji(json: JsonNode) =
inherit Data(json)
new(data: Data) = Emoji(data.Json)
static member get = getWith Emoji
static member tryGet = tryGetWith Emoji
member this.Name: string = this.GetString "name"
member this.Url: string = this.GetString "url"
type FfVisibility =
| Public
| Followers
| Private
static member ofString(str: string) =
match str with
| "public" -> Public
| "followers" -> Followers
| "private" -> Private
| _ -> failwithf "unknown ff visibility: %s" str
type Field(json: JsonNode) =
inherit Data(json)
new(data: Data) = Field(data.Json)
static member get = getWith Field
static member tryGet = tryGetWith Field
member this.Name: string = this.GetString "name"
member this.Value: string = this.GetString "value"
//
[<RequireQualifiedAccess; CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module Note =
type Choice =
{ isVoted: bool
text: string
votes: int }
type Poll =
{ expiresAt: DateString option
multiple: bool
choices: Choice list }
type Emoji = { name: string; url: string }
//
type Note(json: JsonNode) =
inherit Data(json)
new(data: Data) = Note(data.Json)
static member get (key: string) (json: JsonNode) = json |> getNode key |> Note
static member tryGet (key: string) (json: JsonNode) =
json |> tryGetNode key |> Option.map Note
member this.Id: ID = this.GetString "id"
member this.CreatedAt: DateString = this.GetString "createdAt"
member this.Text: string option = this.TryGetString "text"
member this.Cw: string option = this.TryGetString "cw"
member this.User: UserLite = this.Json |> UserLite.get "user"
member this.UserId: ID = this.GetString "userId"
member this.Reply: Note option = this.Json |> Note.tryGet "reply"
member this.ReplyId: ID = this.GetString "replyId"
member this.Renote: Note option = this.Json |> Note.tryGet "renote"
member this.RenoteId: ID = this.GetString "renoteId"
member this.Files: DriveFile list = this.GetListWith DriveFile "files"
and UserLite(json: JsonNode) =
inherit Data(json)
new(data: Data) = UserLite(data.Json)
static member get = getWith UserLite
static member tryGet = tryGetWith UserLite
member this.Id: ID = this.GetString "id"
member this.Username: string = this.GetString "username"
member this.Host: string option = this.TryGetString "host"
member this.Name: string option = this.TryGetString "name"
member this.OnlineStatus: User.OnlineStatus =
this.GetString "onlineStatus" |> User.OnlineStatus.ofString
member this.AvatarUrl: string = this.GetString "avatarUrl"
member this.AvatarBlurhash: string = this.GetString "avatarBlurhash"
member this.Emojis: User.Emoji list = this.GetListWith User.Emoji "emojis"
member this.Instance: User.Instance = User.Instance(this.GetNode "instance")
and UserDetailed(json: JsonNode) =
inherit UserLite(json)
static member get = getWith UserDetailed
static member tryGet = tryGetWith UserDetailed
member this.AlsoKnownAs: string list = this.GetStringList "alsoKnownAs"
member this.BannerBlurhash: string option = this.TryGetString "bannerBlurhash"
member this.BannerColor: string option = this.TryGetString "bannerColor"
member this.BannerUrl: string option = this.TryGetString "bannerUrl"
member this.Birthday: DateString option = this.TryGetString "birthday"
member this.CreatedAt: DateString = this.GetString "createdAt"
member this.Description: string option = this.TryGetString "description"
member this.FfVisibility: User.FfVisibility =
this.GetString "ffVisibility" |> User.FfVisibility.ofString
member this.Fields: User.Field list = this.GetListWith User.Field "fields"
member this.FollowersCount: int = this.GetInt "followersCount"
member this.FollowingCount: int = this.GetInt "followingCount"
member this.HasPendingFollowRequestFromYou: bool =
this.GetBool "hasPendingFollowRequestFromYou"
member this.HasPendingFollowRequestToYou: bool =
this.GetBool "hasPendingFollowRequestToYou"
member this.IsAdmin: bool = this.GetBool "isAdmin"
member this.IsBlocked: bool = this.GetBool "isBlocked"
member this.IsBlocking: bool = this.GetBool "isBlocking"
member this.IsBot: bool = this.GetBool "isBot"
member this.IsCat: bool = this.GetBool "isCat"
member this.IsFollowed: bool = this.GetBool "isFollowed"
member this.IsFollowing: bool = this.GetBool "isFollowing"
member this.IsLocked: bool = this.GetBool "isLocked"
member this.IsModerator: bool = this.GetBool "isModerator"
member this.IsMuted: bool = this.GetBool "isMuted"
member this.IsSilenced: bool = this.GetBool "isSilenced"
member this.IsSuspended: bool = this.GetBool "isSuspended"
member this.Lang: string option = this.TryGetString "lang"
member this.LastFetchedAt: DateString option = this.TryGetString "lastFetchedAt"
member this.Location: string option = this.TryGetString "location"
member this.MovedTo: string = this.GetString "movedTo"
member this.NotesCount: int = this.GetInt "notesCount"
member this.PinnedNoteIds: ID list = this.GetStringList "pinnedNoteIds"
member this.PinnedNotes: Note list = this.GetList "pinnedNotes" |> List.map Note
member this.PinnedPage: Page option = this.Json |> Page.tryGet "pinnedPage"
member this.PinnedPageId: string option = this.TryGetString "pinnedPageId"
member this.PublicReactions: bool = this.GetBool "publicReactions"
member this.SecurityKeys: bool = this.GetBool "securityKeys"
member this.TwoFactorEnabled: bool = this.GetBool "twoFactorEnabled"
member this.UpdatedAt: DateString option = this.TryGetString "updatedAt"
member this.Uri: string option = this.TryGetString "uri"
member this.Url: string option = this.TryGetString "url"
and User =
| Lite of UserLite
| Detailed of UserDetailed
static member fromJson(json: JsonNode) =
try
json |> UserDetailed |> Detailed
with _ ->
json |> UserLite |> Lite
static member from(data: Data) = User.fromJson data.Json
static member get (key: string) (json: JsonNode) =
try
json |> UserDetailed.get key |> Detailed
with _ ->
json |> UserLite.get key |> Lite
static member tryGet (key: string) (json: JsonNode) =
try
Some(User.get key json)
with _ ->
None
member this.AsUserLite: UserLite =
match this with
| Lite user -> user
| Detailed user -> user
member this.Id: ID = this.AsUserLite.Id
member this.Username: string = this.AsUserLite.Username
member this.Host: string option = this.AsUserLite.Host
member this.Name: string option = this.AsUserLite.Name
member this.OnlineStatus: User.OnlineStatus = this.AsUserLite.OnlineStatus
member this.AvatarUrl: string = this.AsUserLite.AvatarUrl
member this.AvatarBlurhash: string = this.AsUserLite.AvatarBlurhash
member this.Emojis: User.Emoji list = this.AsUserLite.Emojis
member this.Instance: User.Instance = this.AsUserLite.Instance
and Page(json: JsonNode) =
inherit Data(json)
new(data: Data) = Page(data.Json)
static member get (key: string) (json: JsonNode) = json |> getNode key |> Page
static member tryGet (key: string) (json: JsonNode) =
json |> tryGetNode key |> Option.map Page
member this.Id: ID = this.GetString "id"
member this.CreatedAt: DateString = this.GetString "createdAt"
member this.UpdatedAt: DateString = this.GetString "updatedAt"
member this.UserId: string = this.GetString "userId"
member this.User: User = this.Json |> User.get "user"
member this.Content: Map<string, JsonNode> list = this.GetListWith toMap "content"
member this.Variables: Map<string, JsonNode> list = this.GetListWith toMap "variables"
member this.Title: string = this.GetString "title"
member this.Name: string = this.GetString "name"
member this.Summary: string option = this.TryGetString "summary"
member this.HideTitleWhenPinned: bool = this.GetBool "hideTitleWhenPinned"
member this.AlignCenter: bool = this.GetBool "alignCenter"
member this.Font: string = this.GetString "font"
member this.Script: string = this.GetString "script"
member this.EyeCatchingImageId: ID option = this.TryGetString "eyeCatchingImageId"
member this.EyeCatchingImage: DriveFile option =
this.Json |> DriveFile.tryGet "eyeCatchingImage"
member this.AttachedFiles: JsonNode list = this.GetList "attachedFiles"
member this.LikedCount: int = this.GetInt "likedCount"
member this.IsLiked: bool = this.GetBool "isLiked"
//
// REMARK: Untyped in the API document.
type UserGroup(json) =
inherit Data(json)
new(data: Data) = UserGroup(data.Json)
static member get (key: string) (json: JsonNode) = json |> getNode key |> UserGroup
static member tryGet (key: string) (json: JsonNode) =
json |> tryGetNode key |> Option.map UserGroup
member this.Id: ID = this.GetString "id"
[<RequireQualifiedAccess; CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module Notification =
type Reaction(json: JsonNode) =
inherit Data(json)
new(data: Data) = Reaction(data.Json)
static member get (key: string) (json: JsonNode) = json |> getNode key |> Reaction
static member tryGet (key: string) (json: JsonNode) =
json |> tryGetNode key |> Option.map Reaction
member this.Reaction: string = this.GetString "reaction"
member this.User: User = this.Json |> User.get "user"
member this.UserId: ID = this.GetString "userId"
member this.Note: Note = this.Json |> Note.get "note"
type Reply(json: JsonNode) =
inherit Data(json)
new(data: Data) = Reply(data.Json)
static member get (key: string) (json: JsonNode) = json |> getNode key |> Reply
static member tryGet (key: string) (json: JsonNode) =
json |> tryGetNode key |> Option.map Reply
member this.User: User = this.Json |> User.get "user"
member this.UserId: ID = this.GetString "userId"
member this.Note: Note = this.Json |> Note.get "note"
type Renote(json: JsonNode) =
inherit Data(json)
new(data: Data) = Renote(data.Json)
static member get (key: string) (json: JsonNode) = json |> getNode key |> Renote
static member tryGet (key: string) (json: JsonNode) =
json |> tryGetNode key |> Option.map Renote
member this.User: User = this.Json |> User.get "user"
member this.UserId: ID = this.GetString "userId"
member this.Note: Note = this.Json |> Note.get "note"
type Quote(json: JsonNode) =
inherit Data(json)
new(data: Data) = Quote(data.Json)
static member get (key: string) (json: JsonNode) = json |> getNode key |> Quote
static member tryGet (key: string) (json: JsonNode) =
json |> tryGetNode key |> Option.map Quote
member this.User: User = this.Json |> User.get "user"
member this.UserId: ID = this.GetString "userId"
member this.Note: Note = this.Json |> Note.get "note"
type Mention(json: JsonNode) =
inherit Data(json)
new(data: Data) = Mention(data.Json)
static member get (key: string) (json: JsonNode) = json |> getNode key |> Mention
static member tryGet (key: string) (json: JsonNode) =
json |> tryGetNode key |> Option.map Mention
member this.User: User = this.Json |> User.get "user"
member this.UserId: ID = this.GetString "userId"
member this.Note: Note = this.Json |> Note.get "note"
type PollVote(json: JsonNode) =
inherit Data(json)
new(data: Data) = PollVote(data.Json)
static member get (key: string) (json: JsonNode) = json |> getNode key |> PollVote
member this.User: User = this.Json |> User.get "user"
member this.UserId: ID = this.GetString "userId"
member this.Note: Note = this.Json |> Note.get "note"
type Follow(json: JsonNode) =
inherit Data(json)
new(data: Data) = Follow(data.Json)
static member get (key: string) (json: JsonNode) = json |> getNode key |> Follow
member this.User: User = this.Json |> User.get "user"
member this.UserId: ID = this.GetString "userId"
type FollowRequestAccepted(json: JsonNode) =
inherit Data(json)
new(data: Data) = FollowRequestAccepted(data.Json)
static member get (key: string) (json: JsonNode) =
json |> getNode key |> FollowRequestAccepted
static member tryGet (key: string) (json: JsonNode) =
json |> tryGetNode key |> Option.map FollowRequestAccepted
member this.User: User = this.Json |> User.get "user"
member this.UserId: ID = this.GetString "userId"
type ReceiveFollowRequest(json: JsonNode) =
inherit Data(json)
new(data: Data) = ReceiveFollowRequest(data.Json)
static member get (key: string) (json: JsonNode) =
json |> getNode key |> ReceiveFollowRequest
static member tryGet (key: string) (json: JsonNode) =
json |> tryGetNode key |> Option.map ReceiveFollowRequest
member this.User: User = this.Json |> User.get "user"
member this.UserId: ID = this.GetString "userId"
type GroupInvited(json: JsonNode) =
inherit Data(json)
new(data: Data) = GroupInvited(data.Json)
static member get (key: string) (json: JsonNode) = json |> getNode key |> GroupInvited
static member tryGet (key: string) (json: JsonNode) =
json |> tryGetNode key |> Option.map GroupInvited
member this.Invitation: UserGroup = this.Json |> UserGroup.get "invitation"
member this.User: User = this.Json |> User.get "user"
member this.UserId: ID = this.GetString "userId"
type App(json: JsonNode) =
inherit Data(json)
new(data: Data) = App(data.Json)
static member get (key: string) (json: JsonNode) = json |> getNode key |> App
static member tryGet (key: string) (json: JsonNode) =
json |> tryGetNode key |> Option.map App
member this.Header: string option = this.TryGetString "header"
member this.Body: string = this.GetString "body"
member this.Icon: string option = this.TryGetString "icon"
[<RequireQualifiedAccess>]
type Body =
| OfReaction of Reaction
| OfReply of Reply
| OfRenote of Renote
| OfQuote of Quote
| OfMention of Mention
| OfPollVote of PollVote
| OfFollow of Follow
| OfFollowRequestAccepted of FollowRequestAccepted
| OfReceiveFollowRequest of ReceiveFollowRequest
| OfGroupInvited of GroupInvited
| OfApp of App
| Other of body: Data
type Notification(json: JsonNode) =
inherit Data(json)
new(data: Data) = Notification(data.Json)
static member get (key: string) (json: JsonNode) = json |> getNode key |> Notification
static member tryGet (key: string) (json: JsonNode) =
json |> tryGetNode key |> Option.map Notification
member this.Id: ID = this.GetString "id"
member this.CreatedAt: DateString = this.GetString "createdAt"
member this.IsRead: bool = this.GetBool "isRead"
member this.Type: string = this.GetString "type"
member this.Body =
match this.Type with
| "reaction" -> Notification.Body.OfReaction(Notification.Reaction(this.Json))
| "reply" -> Notification.Body.OfReply(Notification.Reply(this.Json))
| "renote" -> Notification.Body.OfRenote(Notification.Renote(this.Json))
| "quote" -> Notification.Body.OfQuote(Notification.Quote(this.Json))
| "mention" -> Notification.Body.OfMention(Notification.Mention(this.Json))
| "pollVote" -> Notification.Body.OfPollVote(Notification.PollVote(this.Json))
| "follow" -> Notification.Body.OfFollow(Notification.Follow(this.Json))
| "followRequestAccepted" ->
Notification.Body.OfFollowRequestAccepted(Notification.FollowRequestAccepted(this.Json))
| "receiveFollowRequest" ->
Notification.Body.OfReceiveFollowRequest(Notification.ReceiveFollowRequest(this.Json))
| "groupInvited" -> Notification.Body.OfGroupInvited(Notification.GroupInvited(this.Json))
| "app" -> Notification.Body.OfApp(Notification.App(this.Json))
| _ -> Notification.Body.Other(Data(this.Json))
//
type MessagingMessage(json: JsonNode) =
inherit Data(json)
new(data: Data) = MessagingMessage(data.Json)
static member get (key: string) (json: JsonNode) = json |> getNode key |> MessagingMessage
static member tryGet (key: string) (json: JsonNode) =
json |> tryGetNode key |> Option.map MessagingMessage
member this.Id: ID = this.GetString "id"
member this.CreatedAt: DateString = this.GetString "createdAt"
member this.File: DriveFile option = this.Json |> DriveFile.tryGet "file"
member this.FileId: ID option = this.TryGetString "fileId"
member this.IsRead: bool = this.GetBool "isRead"
member this.Reads: ID list = this.GetStringList "reads"
member this.Text: string option = this.TryGetString "text"
member this.User: User = this.Json |> User.get "user"
member this.UserId: ID = this.GetString "userId"
member this.Recipient: User option = this.Json |> User.tryGet "recipient"
member this.RecipientId: ID option = this.TryGetString "recipientId"
member this.Group: UserGroup option = this.Json |> UserGroup.tryGet "group"
member this.GroupId: ID option = this.TryGetString "groupId"
//
[<RequireQualifiedAccess>]
type ChannelMessageBody =
| Note of body: Note
| Notification of body: Notification
| Mention of body: Note
| Reply of body: Note
| Renote of body: Note
| Follow of body: User
| Followed of body: User
| Unfollow of body: User
| MessagingMessage of body: MessagingMessage
| ReadAllNotifications
| UnreadNotification
| UnreadMention
| ReadAllUnreadMentions
| UnreadSpecifiedNote
| ReadAllUnreadSpecifiedNotes
| UnreadMessagingMessage
| ReadAllMessagingMessages
| Other of self: Data
//
[<RequireQualifiedAccess>]
type StreamMessageType =
| Channel
| NoteUpdated
| Connected
| Other of ``type``: string
type IStreamMessage =
abstract member Type: StreamMessageType
type ChannelMessage(json: JsonNode) =
inherit Data(json)
new(data: Data) = ChannelMessage(data.Json)
static member get (key: string) (json: JsonNode) = json |> getNode key |> ChannelMessage
static member tryGet (key: string) (json: JsonNode) =
json |> tryGetNode key |> Option.map ChannelMessage
interface IStreamMessage with
member __.Type = StreamMessageType.Channel
member this.Id: ID = this.GetString "id"
member this.Type: string = this.GetString "type"
member this.BodyData: Data = Data(this.GetNode "body")
member this.Body: ChannelMessageBody =
match this.Type with
| "note" -> this.BodyData |> Note |> ChannelMessageBody.Note
| "notification" -> this.BodyData |> Notification |> ChannelMessageBody.Notification
| "mention" -> this.BodyData |> Note |> ChannelMessageBody.Mention
| "reply" -> this.BodyData |> Note |> ChannelMessageBody.Reply
| "renote" -> this.BodyData |> Note |> ChannelMessageBody.Renote
| "follow" -> this.BodyData |> User.from |> ChannelMessageBody.Follow
| "followed" -> this.BodyData |> User.from |> ChannelMessageBody.Followed
| "unfollow" -> this.BodyData |> User.from |> ChannelMessageBody.Unfollow
| "messagingMessage" -> this.BodyData |> MessagingMessage |> ChannelMessageBody.MessagingMessage
| "readAllNotifications" -> ChannelMessageBody.ReadAllNotifications
| "unreadNotification" -> ChannelMessageBody.UnreadNotification
| "unreadMention" -> ChannelMessageBody.UnreadMention
| "readAllUnreadMentions" -> ChannelMessageBody.ReadAllUnreadMentions
| "unreadSpecifiedNote" -> ChannelMessageBody.UnreadSpecifiedNote
| "readAllUnreadSpecifiedNotes" -> ChannelMessageBody.ReadAllUnreadSpecifiedNotes
| "unreadMessagingMessage" -> ChannelMessageBody.UnreadMessagingMessage
| "readAllMessagingMessages" -> ChannelMessageBody.ReadAllMessagingMessages
| _ -> ChannelMessageBody.Other(Data(json))
type NoteUpdatedMessage(json: JsonNode) =
inherit Data(json)
new(data: Data) = NoteUpdatedMessage(data.Json)
static member get (key: string) (json: JsonNode) =
json |> getNode key |> NoteUpdatedMessage
static member tryGet (key: string) (json: JsonNode) =
json |> tryGetNode key |> Option.map NoteUpdatedMessage
interface IStreamMessage with
member __.Type = StreamMessageType.NoteUpdated
member this.Id: ID = this.GetString "id"
member this.Type: string = this.GetString "type"
member this.BodyData: Data = Data(this.GetNode "body")
type ConnectedMessage(json: JsonNode) =
inherit Data(json)
new(data: Data) = ConnectedMessage(data.Json)
static member get (key: string) (json: JsonNode) = json |> getNode key |> ConnectedMessage
interface IStreamMessage with
member __.Type = StreamMessageType.Connected
member this.Id: ID = this.GetString "id"
type OtherMessage(json: JsonNode) =
inherit Data(json)
new(data: Data) = OtherMessage(data.Json)
static member get (key: string) (json: JsonNode) = json |> getNode key |> OtherMessage
static member tryGet (key: string) (json: JsonNode) =
json |> tryGetNode key |> Option.map OtherMessage
interface IStreamMessage with
member this.Type = StreamMessageType.Other(this.Type)
member this.Type: string = this.GetString "type"
[<RequireQualifiedAccess>]
type StreamMessage =
| Channel of body: ChannelMessage
| NoteUpdated of body: NoteUpdatedMessage
| Connected of body: ConnectedMessage
| Other of body: OtherMessage
static member get (key: string) (json: JsonNode) =
json |> getNode key |> StreamMessage.ofJson
static member tryGet (key: string) (json: JsonNode) =
json |> tryGetNode key |> Option.bind StreamMessage.tryOfJson
static member ofJson(json: JsonNode) =
match json with
| :? JsonObject as map ->
let typeNode = map.["type"]
if typeNode = null then
failwith "type not found"
let ``type`` = typeNode.GetValue<string>()
match ``type`` with
| "channel" -> Channel(ChannelMessage(map.["body"]))
| "noteUpdated" -> NoteUpdated(NoteUpdatedMessage(map.["body"]))
| "connected" -> Connected(ConnectedMessage(map.["body"]))
| _ -> Other(OtherMessage(map.["body"]))
| _ -> failwith "invalid type: expected JsonObject"
static member tryOfJson(json: JsonNode) =
try
Some(StreamMessage.ofJson json)
with _ ->
None
member this.Type =
match this with
| Channel _ -> StreamMessageType.Channel
| NoteUpdated _ -> StreamMessageType.NoteUpdated
| Connected _ -> StreamMessageType.Connected
| Other other -> StreamMessageType.Other other.Type
//
// Miscellaneous json types.
type Acct(json: JsonNode) =
inherit Data(json)
new(data: Data) = Acct(data.Json)
static member get (key: string) (json: JsonNode) = json |> getNode key |> Acct
static member tryGet (key: string) (json: JsonNode) =
json |> tryGetNode key |> Option.map Acct
member this.Username: string = this.GetString "username"
member this.Host: string option = this.TryGetString "host"
type Ad(json: JsonNode) =
// REMARK: Untyped in the API document.
inherit Data(json)
new(data: Data) = Ad(data.Json)
static member get (key: string) (json: JsonNode) = json |> getNode key |> Ad
static member tryGet (key: string) (json: JsonNode) = json |> tryGetNode key |> Option.map Ad
[<RequireQualifiedAccess; CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module APIError =
type Kind =
| Client
| Server
static member ofString(str: string) =
match str with
| "client" -> Client
| "server" -> Server
| _ -> failwithf "unknown error kind: %s" str
type APIError(json: JsonNode) =
inherit Data(json)
new(data: Data) = APIError(data.Json)
static member get (key: string) (json: JsonNode) = json |> getNode key |> APIError
static member tryGet (key: string) (json: JsonNode) =
json |> tryGetNode key |> Option.map APIError
member this.Id: ID = this.GetString "id"
member this.Code: string = this.GetString "code"
member this.Message: string = this.GetString "message"
member this.Kind: APIError.Kind = this.GetString "kind" |> APIError.Kind.ofString
member this.Info: Map<string, JsonNode> = this.GetMap "info"
type Announcement(json: JsonNode) =
inherit Data(json)
new(data: Data) = Announcement(data.Json)
static member get (key: string) (json: JsonNode) = json |> getNode key |> Announcement
static member tryGet (key: string) (json: JsonNode) =
json |> tryGetNode key |> Option.map Announcement
member this.Id: ID = this.GetString "id"
member this.CreatedAt: DateString = this.GetString "createdAt"