-
Notifications
You must be signed in to change notification settings - Fork 1
/
message.gleam
162 lines (144 loc) · 3.98 KB
/
message.gleam
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
import gleam/dynamic.{type Decoder}
import gleam/option.{type Option}
import gleam/result
import glitch/types/cheermote.{type Cheermote}
import glitch/types/emote.{type Emote}
pub type Message {
Message(text: String, fragments: List(Fragment))
}
pub fn decoder() -> Decoder(Message) {
dynamic.decode2(
Message,
dynamic.field("text", dynamic.string),
dynamic.field("fragments", dynamic.list(of: message_fragment_decoder())),
)
}
pub type MessageType {
Text
ChannelPointsHighlighted
ChannelPointsSubOnly
UserIntro
}
pub fn messsage_type_to_string(messsage_type: MessageType) -> String {
case messsage_type {
Text -> "text"
ChannelPointsHighlighted -> "channel_point_highlighted"
ChannelPointsSubOnly -> "channel_points_sub_only"
UserIntro -> "user_intro"
}
}
pub fn messsage_type_from_string(string: String) -> Result(MessageType, Nil) {
case string {
"text" -> Ok(Text)
"channel_point_highlighted" -> Ok(ChannelPointsHighlighted)
"channel_points_sub_only" -> Ok(ChannelPointsSubOnly)
"user_intro" -> Ok(UserIntro)
_ -> Error(Nil)
}
}
pub fn messsage_type_decoder() -> Decoder(MessageType) {
fn(data: dynamic.Dynamic) {
use string <- result.try(dynamic.string(data))
string
|> messsage_type_from_string
|> result.replace_error([
dynamic.DecodeError(
expected: "MessageType",
found: "String(" <> string <> ")",
path: [],
),
])
}
}
pub type Fragment {
Fragment(
fragment_type: FragmentType,
text: String,
cheermote: Option(Cheermote),
emote: Option(Emote),
mention: Option(Mention),
)
}
fn message_fragment_decoder() -> Decoder(Fragment) {
dynamic.decode5(
Fragment,
dynamic.field("type", fragment_type_decoder()),
dynamic.field("text", dynamic.string),
dynamic.optional_field("cheermote", cheermote.decoder()),
dynamic.optional_field("emote", emote.decoder()),
dynamic.optional_field("mention", mention_decoder()),
)
}
pub type FragmentType {
TextFragment
CheermoteFragment
EmoteFragment
MentionFragment
}
pub fn fragment_type_to_string(fragment_type: FragmentType) -> String {
case fragment_type {
TextFragment -> "text"
CheermoteFragment -> "cheermote"
EmoteFragment -> "emote"
MentionFragment -> "mentiond"
}
}
pub fn fragment_type_from_string(string: String) -> Result(FragmentType, Nil) {
case string {
"text" -> Ok(TextFragment)
"cheermote" -> Ok(CheermoteFragment)
"emote" -> Ok(EmoteFragment)
"mentiond" -> Ok(MentionFragment)
_ -> Error(Nil)
}
}
pub fn fragment_type_decoder() -> Decoder(FragmentType) {
fn(data: dynamic.Dynamic) {
use string <- result.try(dynamic.string(data))
string
|> fragment_type_from_string
|> result.replace_error([
dynamic.DecodeError(
expected: "FragmentType",
found: "String(" <> string <> ")",
path: [],
),
])
}
}
pub type Reply {
Reply(
parent_message_id: String,
parent_message_body: String,
parent_user_id: String,
parent_user_name: String,
thread_message_id: String,
thread_user_id: String,
thread_user_name: String,
thread_user_login: String,
)
}
pub fn reply_decoder() -> Decoder(Reply) {
dynamic.decode8(
Reply,
dynamic.field("parent_message_id", dynamic.string),
dynamic.field("parent_message_body", dynamic.string),
dynamic.field("parent_message_body", dynamic.string),
dynamic.field("parent_user_name", dynamic.string),
dynamic.field("parent_user_name", dynamic.string),
dynamic.field("thread_user_id", dynamic.string),
dynamic.field("thread_user_id", dynamic.string),
dynamic.field("thread_user_login", dynamic.string),
)
}
pub type Mention {
Mention(user_id: String, user_name: String, user_login: String)
}
pub fn mention_decoder() -> Decoder(Mention) {
dynamic.decode3(
Mention,
dynamic.field("user_id", dynamic.string),
dynamic.field("user_name", dynamic.string),
dynamic.field("user_login", dynamic.string),
)
}