etwin_core 0.12.3

Core crate for Eternal-Twin
Documentation
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
use crate::core::{HtmlFragment, Instant, Listing, ListingCount, LocaleId};
use crate::oauth::{OauthClientIdRef, ShortOauthClient};
use crate::types::AnyError;
use crate::user::{ShortUser, UserIdRef, UserRef};
use async_trait::async_trait;
use auto_impl::auto_impl;
#[cfg(feature = "serde")]
use etwin_serde_tools::{Deserialize, Serialize};
use std::str::FromStr;
use thiserror::Error;

pub type MarktwinText = String;

declare_new_uuid! {
  pub struct ForumSectionId(Uuid);
  pub type ParseError = ForumSectionIdParseError;
  const SQL_NAME = "forum_section_id";
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type", rename = "ForumSection"))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ForumSectionIdRef {
  pub id: ForumSectionId,
}

impl ForumSectionIdRef {
  pub const fn new(id: ForumSectionId) -> Self {
    Self { id }
  }
}

impl From<ForumSectionId> for ForumSectionIdRef {
  fn from(id: ForumSectionId) -> Self {
    Self::new(id)
  }
}

declare_new_string! {
  pub struct ForumSectionKey(String);
  pub type ParseError = ForumSectionKeyParseError;
  const PATTERN = r"^[_a-z][_a-z0-9]{0,31}$";
  const SQL_NAME = "forum_section_key";
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type", rename = "ForumSection"))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ForumSectionKeyRef {
  pub key: ForumSectionKey,
}

impl From<ForumSectionKey> for ForumSectionKeyRef {
  fn from(key: ForumSectionKey) -> Self {
    Self { key }
  }
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(untagged))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ForumSectionRef {
  Id(ForumSectionIdRef),
  Key(ForumSectionKeyRef),
}

impl ForumSectionRef {
  pub const fn split(&self) -> (Option<ForumSectionIdRef>, Option<&ForumSectionKeyRef>) {
    let mut id: Option<ForumSectionIdRef> = None;
    let mut key: Option<&ForumSectionKeyRef> = None;
    match self {
      Self::Id(r) => id = Some(*r),
      Self::Key(r) => key = Some(r),
    };
    (id, key)
  }

  pub const fn split_deref(&self) -> (Option<ForumSectionId>, Option<&ForumSectionKey>) {
    let mut id: Option<ForumSectionId> = None;
    let mut key: Option<&ForumSectionKey> = None;
    match self {
      Self::Id(r) => id = Some(r.id),
      Self::Key(r) => key = Some(&r.key),
    };
    (id, key)
  }
}

impl From<&'_ ForumSection> for ForumSectionRef {
  fn from(section: &ForumSection) -> Self {
    Self::Id(section.as_ref())
  }
}

impl From<ForumSectionIdRef> for ForumSectionRef {
  fn from(id: ForumSectionIdRef) -> Self {
    Self::Id(id)
  }
}

impl From<ForumSectionId> for ForumSectionRef {
  fn from(id: ForumSectionId) -> Self {
    Self::Id(id.into())
  }
}

impl From<ForumSectionKeyRef> for ForumSectionRef {
  fn from(key: ForumSectionKeyRef) -> Self {
    Self::Key(key)
  }
}

impl From<ForumSectionKey> for ForumSectionRef {
  fn from(key: ForumSectionKey) -> Self {
    Self::Key(key.into())
  }
}

#[derive(Debug, Error)]
#[error("invalid forum section id ({id}) or key ({key})")]
pub struct ForumSectionRefParseError {
  pub id: ForumSectionIdParseError,
  pub key: ForumSectionKeyParseError,
}

impl FromStr for ForumSectionRef {
  type Err = ForumSectionRefParseError;

  fn from_str(s: &str) -> Result<Self, Self::Err> {
    match ForumSectionId::from_str(s) {
      Ok(r) => Ok(r.into()),
      Err(id) => match ForumSectionKey::from_str(s) {
        Ok(r) => Ok(r.into()),
        Err(key) => Err(ForumSectionRefParseError { id, key }),
      },
    }
  }
}

declare_new_uuid! {
  pub struct ForumThreadId(Uuid);
  pub type ParseError = ForumThreadIdParseError;
  const SQL_NAME = "forum_thread_id";
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type", rename = "ForumThread"))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ForumThreadIdRef {
  pub id: ForumThreadId,
}

impl ForumThreadIdRef {
  pub const fn new(id: ForumThreadId) -> Self {
    Self { id }
  }
}

impl From<ForumThreadId> for ForumThreadIdRef {
  fn from(id: ForumThreadId) -> Self {
    Self::new(id)
  }
}

declare_new_string! {
  pub struct ForumThreadKey(String);
  pub type ParseError = ForumThreadKeyParseError;
  const PATTERN = r"^[_a-z][_a-z0-9]{0,31}$";
  const SQL_NAME = "forum_thread_key";
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type", rename = "ForumThread"))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ForumThreadKeyRef {
  pub key: ForumThreadKey,
}

impl From<ForumThreadKey> for ForumThreadKeyRef {
  fn from(key: ForumThreadKey) -> Self {
    Self { key }
  }
}

#[derive(Debug, Error)]
#[error("invalid forum thread id ({id}) or key ({key})")]
pub struct ForumThreadRefParseError {
  pub id: ForumThreadIdParseError,
  pub key: ForumThreadKeyParseError,
}

impl FromStr for ForumThreadRef {
  type Err = ForumThreadRefParseError;

  fn from_str(s: &str) -> Result<Self, Self::Err> {
    match ForumThreadId::from_str(s) {
      Ok(r) => Ok(r.into()),
      Err(id) => match ForumThreadKey::from_str(s) {
        Ok(r) => Ok(r.into()),
        Err(key) => Err(ForumThreadRefParseError { id, key }),
      },
    }
  }
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(untagged))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ForumThreadRef {
  Id(ForumThreadIdRef),
  Key(ForumThreadKeyRef),
}

impl ForumThreadRef {
  pub const fn split(&self) -> (Option<ForumThreadIdRef>, Option<&ForumThreadKeyRef>) {
    let mut id: Option<ForumThreadIdRef> = None;
    let mut key: Option<&ForumThreadKeyRef> = None;
    match self {
      Self::Id(r) => id = Some(*r),
      Self::Key(r) => key = Some(r),
    };
    (id, key)
  }

  pub const fn split_deref(&self) -> (Option<ForumThreadId>, Option<&ForumThreadKey>) {
    let mut id: Option<ForumThreadId> = None;
    let mut key: Option<&ForumThreadKey> = None;
    match self {
      Self::Id(r) => id = Some(r.id),
      Self::Key(r) => key = Some(&r.key),
    };
    (id, key)
  }
}

impl From<ForumThreadIdRef> for ForumThreadRef {
  fn from(id: ForumThreadIdRef) -> Self {
    Self::Id(id)
  }
}

impl From<ForumThreadId> for ForumThreadRef {
  fn from(id: ForumThreadId) -> Self {
    Self::Id(id.into())
  }
}

impl From<ForumThreadKeyRef> for ForumThreadRef {
  fn from(key: ForumThreadKeyRef) -> Self {
    Self::Key(key)
  }
}

impl From<ForumThreadKey> for ForumThreadRef {
  fn from(key: ForumThreadKey) -> Self {
    Self::Key(key.into())
  }
}

declare_new_enum!(
  pub enum ForumRole {
    #[str("Administrator")]
    Administrator,
    #[str("Moderator")]
    Moderator,
  }
  pub type ParseError = ForumRoleParseError;
  const SQL_NAME = "forum_role";
);

declare_new_string! {
  pub struct ForumSectionDisplayName(String);
  pub type ParseError = ForumSectionDisplayNameParseError;
  const PATTERN = r"^\S.{0,62}\S$";
  const SQL_NAME = "forum_section_display_name";
}

declare_new_string! {
  pub struct ForumThreadTitle(String);
  pub type ParseError = ForumThreadTitleParseError;
  const PATTERN = r"^\S.{0,62}\S$";
  const SQL_NAME = "forum_thread_title";
}

declare_new_uuid! {
  pub struct ForumPostId(Uuid);
  pub type ParseError = ForumPostIdParseError;
  const SQL_NAME = "forum_post_id";
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type", rename = "ForumThread"))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ForumPostIdRef {
  pub id: ForumPostId,
}

impl ForumPostIdRef {
  pub const fn new(id: ForumPostId) -> Self {
    Self { id }
  }
}

impl From<ForumPostId> for ForumPostIdRef {
  fn from(id: ForumPostId) -> Self {
    Self::new(id)
  }
}

declare_new_uuid! {
  pub struct ForumPostRevisionId(Uuid);
  pub type ParseError = ForumPostRevisionIdParseError;
  const SQL_NAME = "forum_post_revision_id";
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type", rename = "ForumSection"))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ForumSection {
  pub id: ForumSectionId,
  pub key: Option<ForumSectionKey>,
  pub display_name: ForumSectionDisplayName,
  pub ctime: Instant,
  pub locale: Option<LocaleId>,
  pub threads: ForumThreadListing,
  pub role_grants: Vec<ForumRoleGrant>,
  #[cfg_attr(feature = "serde", serde(rename = "self"))]
  pub this: ForumSectionSelf,
}

impl ForumSection {
  pub fn as_ref(&self) -> ForumSectionIdRef {
    ForumSectionIdRef { id: self.id }
  }
}

pub type ForumSectionListing = Listing<ForumSectionMeta>;

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type", rename = "ForumSection"))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ForumSectionMeta {
  pub id: ForumSectionId,
  pub key: Option<ForumSectionKey>,
  pub display_name: ForumSectionDisplayName,
  pub ctime: Instant,
  pub locale: Option<LocaleId>,
  pub threads: ListingCount,
  #[cfg_attr(feature = "serde", serde(rename = "self"))]
  pub this: ForumSectionSelf,
}

impl ForumSectionMeta {
  pub const fn as_ref(&self) -> ForumSectionIdRef {
    ForumSectionIdRef::new(self.id)
  }
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type", rename = "ForumSection"))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawForumSectionMeta {
  pub id: ForumSectionId,
  pub key: Option<ForumSectionKey>,
  pub display_name: ForumSectionDisplayName,
  pub ctime: Instant,
  pub locale: Option<LocaleId>,
  pub threads: ListingCount,
  pub role_grants: Vec<RawForumRoleGrant>,
}

impl RawForumSectionMeta {
  pub const fn as_ref(&self) -> ForumSectionIdRef {
    ForumSectionIdRef::new(self.id)
  }
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ForumSectionSelf {
  pub roles: Vec<ForumRole>,
}

pub type ForumThreadListing = Listing<ForumThreadMeta>;

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ForumRoleGrant {
  pub role: ForumRole,
  pub user: ShortUser,
  pub start_time: Instant,
  pub granted_by: ShortUser,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawForumRoleGrant {
  pub role: ForumRole,
  pub user: UserIdRef,
  pub start_time: Instant,
  pub granted_by: UserIdRef,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type", rename = "ForumThread"))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ForumThread {
  pub id: ForumThreadId,
  pub key: Option<ForumThreadKey>,
  pub title: ForumThreadTitle,
  pub ctime: Instant,
  pub section: ForumSectionMeta,
  pub posts: ForumPostListing,
  pub is_pinned: bool,
  pub is_locked: bool,
}

impl ForumThread {
  pub fn as_ref(&self) -> ForumThreadIdRef {
    self.id.into()
  }
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type", rename = "ForumThread"))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ForumThreadMeta {
  pub id: ForumThreadId,
  pub key: Option<ForumThreadKey>,
  pub title: ForumThreadTitle,
  pub ctime: Instant,
  pub is_pinned: bool,
  pub is_locked: bool,
  pub posts: ListingCount,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type", rename = "ForumThread"))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawForumThreadMeta {
  pub id: ForumThreadId,
  pub key: Option<ForumThreadKey>,
  pub title: ForumThreadTitle,
  pub section: ForumSectionIdRef,
  pub ctime: Instant,
  pub is_pinned: bool,
  pub is_locked: bool,
  pub posts: ListingCount,
}

impl RawForumThreadMeta {
  pub fn as_ref(&self) -> ForumThreadIdRef {
    self.id.into()
  }
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type", rename = "ForumSection"))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawCreateForumThreadResult {
  pub id: ForumThreadId,
  pub key: Option<ForumThreadKey>,
  pub title: ForumThreadTitle,
  pub section: ForumSectionIdRef,
  pub ctime: Instant,
  pub is_pinned: bool,
  pub is_locked: bool,
  pub post_id: ForumPostId,
  pub post_revision: RawForumPostRevision,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type", rename = "ForumThread"))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ForumThreadMetaWithSection {
  pub id: ForumThreadId,
  pub key: Option<ForumThreadKey>,
  pub title: ForumThreadTitle,
  pub ctime: Instant,
  pub is_pinned: bool,
  pub is_locked: bool,
  pub posts: ListingCount,
  pub section: ForumSectionMeta,
}

pub type ForumPostListing = Listing<ShortForumPost>;

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type", rename = "ForumPost"))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ForumPost {
  pub id: ForumPostId,
  pub ctime: Instant,
  pub author: ForumActor,
  pub revisions: ForumPostRevisionListing,
  pub thread: ForumThreadMetaWithSection,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type", rename = "ForumPost"))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ShortForumPost {
  pub id: ForumPostId,
  pub ctime: Instant,
  pub author: ForumActor,
  pub revisions: LatestForumPostRevisionListing,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type", rename = "ForumPost"))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawShortForumPost {
  pub id: ForumPostId,
  pub ctime: Instant,
  pub author: RawForumActor,
  pub revisions: RawLatestForumPostRevisionListing,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type", rename = "ForumPost"))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawForumPost {
  pub id: ForumPostId,
  pub ctime: Instant,
  pub author: RawForumActor,
  pub revisions: RawForumPostRevisionListing,
  pub thread: RawForumThreadMeta,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct LatestForumPostRevisionListing {
  pub count: u32,
  pub last: ForumPostRevision,
}

pub type ForumPostRevisionListing = Listing<ForumPostRevision>;
pub type RawForumPostRevisionListing = Listing<RawForumPostRevision>;

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawLatestForumPostRevisionListing {
  pub count: u32,
  pub last: RawForumPostRevision,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type", rename = "ForumPostRevision"))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ForumPostRevision {
  pub id: ForumPostRevisionId,
  pub time: Instant,
  pub author: ForumActor,
  pub content: Option<ForumPostRevisionContent>,
  pub moderation: Option<ForumPostRevisionContent>,
  pub comment: Option<ForumPostRevisionComment>,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type", rename = "ForumPostRevision"))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawForumPostRevision {
  pub id: ForumPostRevisionId,
  pub time: Instant,
  pub author: RawForumActor,
  pub content: Option<ForumPostRevisionContent>,
  pub moderation: Option<ForumPostRevisionContent>,
  pub comment: Option<ForumPostRevisionComment>,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ForumPostRevisionContent {
  pub marktwin: MarktwinText,
  pub html: HtmlFragment,
}

declare_new_string! {
  pub struct ForumPostRevisionComment(String);
  pub type ParseError = ForumPostRevisionCommentParseError;
  const PATTERN = r".";
  const SQL_NAME = "forum_post_revision_comment";
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(untagged))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ForumActor {
  ClientForumActor(ClientForumActor),
  RoleForumActor(RoleForumActor),
  UserForumActor(UserForumActor),
}

impl ForumActor {
  pub fn to_raw(&self) -> RawForumActor {
    match self {
      Self::ClientForumActor(a) => RawForumActor::ClientForumActor(a.to_raw()),
      Self::RoleForumActor(a) => RawForumActor::RoleForumActor(a.to_raw()),
      Self::UserForumActor(a) => RawForumActor::UserForumActor(a.to_raw()),
    }
  }
}

#[cfg_attr(
  feature = "serde",
  derive(Serialize, Deserialize),
  serde(tag = "type", rename = "ClientForumActor")
)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ClientForumActor {
  pub client: ShortOauthClient,
}

impl ClientForumActor {
  pub fn to_raw(&self) -> RawClientForumActor {
    RawClientForumActor {
      client: self.client.id.into(),
    }
  }
}

#[cfg_attr(
  feature = "serde",
  derive(Serialize, Deserialize),
  serde(tag = "type", rename = "RoleForumActor")
)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RoleForumActor {
  pub role: ForumRole,
  #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
  pub user: Option<ShortUser>,
}

impl RoleForumActor {
  pub fn to_raw(&self) -> RawRoleForumActor {
    RawRoleForumActor {
      role: self.role,
      user: self.user.as_ref().map(ShortUser::as_ref),
    }
  }
}

#[cfg_attr(
  feature = "serde",
  derive(Serialize, Deserialize),
  serde(tag = "type", rename = "UserForumActor")
)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct UserForumActor {
  #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
  pub role: Option<ForumRole>,
  pub user: ShortUser,
}

impl UserForumActor {
  pub fn to_raw(&self) -> RawUserForumActor {
    RawUserForumActor {
      role: self.role,
      user: self.user.as_ref(),
    }
  }
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum RawForumActor {
  ClientForumActor(RawClientForumActor),
  RoleForumActor(RawRoleForumActor),
  UserForumActor(RawUserForumActor),
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawClientForumActor {
  pub client: OauthClientIdRef,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawRoleForumActor {
  pub role: ForumRole,
  pub user: Option<UserIdRef>,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawUserForumActor {
  pub role: Option<ForumRole>,
  pub user: UserIdRef,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AddModeratorOptions {
  pub section: ForumSectionRef,
  pub user: UserRef,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawAddModeratorOptions {
  pub section: ForumSectionRef,
  pub target: UserIdRef,
  pub granter: UserIdRef,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DeleteModeratorOptions {
  pub section: ForumSectionRef,
  pub user: UserRef,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawDeleteModeratorOptions {
  pub section: ForumSectionRef,
  // Moderator to remove
  pub target: UserIdRef,
  // User who triggered the revocation
  pub revoker: UserIdRef,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CreateThreadOptions {
  pub section: ForumSectionRef,
  pub title: ForumThreadTitle,
  pub body: MarktwinText,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawCreateThreadsOptions {
  pub actor: RawForumActor,
  pub section: ForumSectionRef,
  pub title: ForumThreadTitle,
  pub body_mkt: MarktwinText,
  pub body_html: HtmlFragment,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct UpsertSystemSectionOptions {
  pub key: ForumSectionKey,
  pub display_name: ForumSectionDisplayName,
  pub locale: Option<LocaleId>,
}

#[derive(Error, Debug)]
pub enum UpsertSystemSectionError {
  #[error(transparent)]
  Other(AnyError),
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct GetForumSectionOptions {
  pub section: ForumSectionRef,
  pub thread_offset: u32,
  pub thread_limit: u32,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct GetForumSectionMetaOptions {
  pub section: ForumSectionRef,
}

#[derive(Error, Debug)]
pub enum GetSectionMetaError {
  #[error("section not found")]
  NotFound,
  #[error(transparent)]
  Other(AnyError),
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawGetThreadsOptions {
  pub section: ForumSectionRef,
  /// Thread offset
  pub offset: u32,
  /// Thread limit
  pub limit: u32,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawGetForumThreadOptions {
  pub thread: ForumThreadRef,
  /// Post offset
  pub offset: u32,
  /// Post limit
  pub limit: u32,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawGetForumThreadMetaOptions {
  pub thread: ForumThreadRef,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawGetSectionsOptions {
  pub offset: u32,
  pub limit: u32,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawGetRoleGrantsOptions {
  pub section: ForumSectionIdRef,
  pub user: Option<UserIdRef>,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CreatePostOptions {
  pub thread: ForumThreadRef,
  pub body: MarktwinText,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawGetPostsOptions {
  pub thread: ForumThreadRef,
  /// Post offset
  pub offset: u32,
  /// Post limit
  pub limit: u32,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawGetForumPostOptions {
  pub post: ForumPostIdRef,
  /// Revision offset
  pub offset: u32,
  /// Revision limit
  pub limit: u32,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawCreatePostOptions {
  pub actor: RawForumActor,
  pub thread: ForumThreadRef,
  pub body: ForumPostRevisionContent,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type", rename = "ForumSection"))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawCreateForumPostResult {
  pub id: ForumPostId,
  pub thread: ForumThreadIdRef,
  pub section: ForumSectionIdRef,
  pub revision: RawForumPostRevision,
}

#[derive(Error, Debug)]
pub enum CreatePostError {
  #[error("thread not found")]
  ThreadNotFound,
  #[error("current actor does not have the permission to create a post in this thread")]
  Forbidden,
  #[error("failed to parse provided body")]
  FailedToParseBody,
  #[error("failed to render provided body")]
  FailedToRenderBody,
  #[error(transparent)]
  Other(AnyError),
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct UpdatePostOptions {
  pub post: ForumPostId,
  pub revision: ForumPostRevisionId,
  pub content: Option<Option<MarktwinText>>,
  pub moderation: Option<Option<MarktwinText>>,
  pub comment: Option<ForumPostRevisionComment>,
}

#[derive(Error, Debug)]
pub enum UpdatePostError {
  #[error("post not found")]
  PostNotFound,
  #[error("current actor does not have the permission to update this post")]
  Forbidden,
  #[error("failed to parse provided body")]
  FailedToParseBody,
  #[error("failed to render provided body")]
  FailedToRenderBody,
  #[error(transparent)]
  Other(AnyError),
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawCreatePostRevisionOptions {
  pub actor: RawForumActor,
  pub post: ForumPostIdRef,
  pub body: Option<ForumPostRevisionContent>,
  pub mod_body: Option<ForumPostRevisionContent>,
  pub comment: Option<ForumPostRevisionComment>,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type", rename = "ForumSection"))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawCreateForumPostRevisionResult {
  pub revision: RawForumPostRevision,
  pub thread: ForumThreadIdRef,
  pub section: ForumSectionIdRef,
  pub post: ForumPostIdRef,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DeletePostOptions {
  pub post: ForumPostId,
  pub revision: ForumPostRevisionId,
  pub comment: Option<ForumPostRevisionComment>,
}

#[derive(Error, Debug)]
pub enum DeletePostError {
  #[error(transparent)]
  Other(AnyError),
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct GetThreadOptions {
  pub thread: ForumThreadRef,
  pub post_offset: u32,
  pub post_limit: u32,
}

#[derive(Error, Debug)]
pub enum GetThreadError {
  #[error(transparent)]
  Other(AnyError),
}

#[derive(Error, Debug)]
pub enum GetThreadMetaError {
  #[error("thread not found")]
  NotFound,
  #[error(transparent)]
  Other(AnyError),
}

#[async_trait]
#[auto_impl(&, Arc)]
pub trait ForumStore: Send + Sync {
  async fn add_moderator(&self, options: &RawAddModeratorOptions) -> Result<(), AnyError>;

  async fn delete_moderator(&self, options: &RawDeleteModeratorOptions) -> Result<(), AnyError>;

  async fn get_sections(&self, options: &RawGetSectionsOptions) -> Result<Listing<RawForumSectionMeta>, AnyError>;

  async fn get_section_meta(
    &self,
    options: &GetForumSectionMetaOptions,
  ) -> Result<RawForumSectionMeta, GetSectionMetaError>;

  async fn get_threads(&self, options: &RawGetThreadsOptions) -> Result<Listing<RawForumThreadMeta>, AnyError>;

  async fn get_thread_meta(
    &self,
    options: &RawGetForumThreadMetaOptions,
  ) -> Result<RawForumThreadMeta, GetThreadMetaError>;

  async fn create_thread(&self, options: &RawCreateThreadsOptions) -> Result<RawCreateForumThreadResult, AnyError>;

  async fn get_posts(&self, options: &RawGetPostsOptions) -> Result<Listing<RawShortForumPost>, AnyError>;

  async fn create_post(&self, options: &RawCreatePostOptions) -> Result<RawCreateForumPostResult, AnyError>;

  async fn get_post(&self, options: &RawGetForumPostOptions) -> Result<RawForumPost, AnyError>;

  async fn get_role_grants(&self, options: &RawGetRoleGrantsOptions) -> Result<Vec<RawForumRoleGrant>, AnyError>;

  async fn create_post_revision(
    &self,
    options: &RawCreatePostRevisionOptions,
  ) -> Result<RawCreateForumPostRevisionResult, AnyError>;

  async fn upsert_system_section(
    &self,
    options: &UpsertSystemSectionOptions,
  ) -> Result<ForumSection, UpsertSystemSectionError>;
}