lamprey-common 0.1.1

yet another chat thing?
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
use serde::{Deserialize, Serialize};

#[cfg(feature = "utoipa")]
use utoipa::{IntoParams, ToSchema};

#[cfg(feature = "validator")]
use validator::Validate;

use crate::v1::types::emoji::Emoji;
use crate::v1::types::moderation::Report;
use crate::v1::types::reaction::ReactionCounts;
use crate::v1::types::util::{some_option, Diff, Time};
use crate::v1::types::{AuditLogEntry, Embed, RoleId, UserId};
use crate::v1::types::{EmojiId, RoomId};

use super::channel::Channel;
use super::EmbedCreate;
use super::{
    media::{Media, MediaRef},
    ChannelId, MessageId, MessageVerId,
};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
#[cfg_attr(feature = "validator", derive(Validate))]
pub struct Message {
    #[serde(flatten)]
    pub message_type: MessageType,
    pub id: MessageId,
    pub channel_id: ChannelId,
    pub version_id: MessageVerId,

    /// unique string sent by the client via idempotency-key to identify this message
    pub nonce: Option<String>,

    /// the id of who sent this message
    pub author_id: UserId,

    pub mentions: Mentions,

    /// exists if this message is pinned
    pub pinned: Option<Pinned>,

    #[serde(default)]
    pub reactions: ReactionCounts,

    pub created_at: Option<Time>,

    /// deleted messages can still be viewed by moderators for a period of time, but otherwise cannot be recovered
    pub deleted_at: Option<Time>,

    /// removed messages are hidden for non moderators. they are recoverable by moderators
    pub removed_at: Option<Time>,

    pub edited_at: Option<Time>,

    /// the associated thread for this message, if one exists.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thread: Option<Box<Channel>>,
}

/// information about a pinned message
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
pub struct Pinned {
    /// when this was pinned
    pub time: Time,

    /// the position of this pin. lower numbers come first.
    pub position: u16,
}

/// reorder pinned messages
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
#[cfg_attr(feature = "validator", derive(Validate))]
pub struct PinsReorder {
    /// the messages to reorder
    #[serde(default)]
    #[validate(length(min = 1, max = 1024))]
    pub messages: Vec<PinsReorderItem>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
#[cfg_attr(feature = "validator", derive(Validate))]
pub struct PinsReorderItem {
    pub id: MessageId,

    #[serde(default, deserialize_with = "some_option")]
    pub position: Option<Option<u16>>,
}

fn true_fn() -> bool {
    true
}

/// what mentions to parse from the message content. mentions will only be parsed if the message content actually contains a mention pattern.
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
pub struct ParseMentions {
    /// only parse mentions for these users. an empty vec disables all mentions, while None allows all mentions.
    pub users: Option<Vec<UserId>>,

    /// only parse mentions for these roles. an empty vec disables all mentions, while None allows all mentions.
    pub roles: Option<Vec<RoleId>>,

    /// whether to parse @everyone mentions from the content
    #[serde(default = "true_fn")]
    pub everyone: bool,
}

/// who/what this message notified on send
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
pub struct Mentions {
    pub users: Vec<UserId>,
    pub roles: Vec<RoleId>,
    pub threads: Vec<ChannelId>,

    #[serde(default)]
    pub emojis: Vec<EmojiId>,

    #[serde(default)]
    pub everyone: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
#[cfg_attr(feature = "validator", derive(Validate))]
pub struct MessageCreate {
    /// the message's content, in either markdown or the new format depending on if use_new_text_formatting is true
    #[cfg_attr(feature = "utoipa", schema(min_length = 1, max_length = 8192))]
    #[cfg_attr(feature = "validator", validate(length(min = 1, max = 8192)))]
    pub content: Option<String>,

    #[cfg_attr(
        feature = "utoipa",
        schema(required = false, min_length = 0, max_length = 32)
    )]
    #[cfg_attr(feature = "validator", validate(length(min = 0, max = 32)))]
    #[serde(default)]
    pub attachments: Vec<MediaRef>,

    /// arbitrary metadata associated with a message
    ///
    /// deprecated: arbitrary metadata is too dubious, sorry. will come up with a better solution later
    #[cfg_attr(feature = "utoipa", schema(deprecated))]
    pub metadata: Option<serde_json::Value>,

    /// the message this message is replying to
    pub reply_id: Option<MessageId>,

    /// override the name of this message's sender
    ///
    /// deprecated: create new puppets for each bridged user instead
    #[cfg_attr(feature = "utoipa", schema(deprecated))]
    #[serde(default)]
    pub override_name: Option<String>,

    #[serde(default)]
    pub embeds: Vec<EmbedCreate>,

    /// custom timestamps (timestamp massaging), for bridge bots
    pub created_at: Option<Time>,

    #[serde(default)]
    pub mentions: ParseMentions,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
#[cfg_attr(feature = "validator", derive(Validate))]
pub struct MessagePatch {
    /// the new message content. whether its markdown/new format depends on the target message's format
    #[cfg_attr(feature = "utoipa", schema(min_length = 1, max_length = 8192))]
    #[cfg_attr(feature = "validator", validate(length(min = 1, max = 8192)))]
    #[serde(default, deserialize_with = "some_option")]
    pub content: Option<Option<String>>,

    #[cfg_attr(
        feature = "utoipa",
        schema(required = false, min_length = 0, max_length = 32)
    )]
    #[cfg_attr(feature = "validator", validate(length(min = 0, max = 32)))]
    pub attachments: Option<Vec<MediaRef>>,

    /// arbitrary metadata associated with a message
    ///
    /// deprecated: arbitrary metadata is too dubious, sorry. will come up with a better solution later
    #[cfg_attr(feature = "utoipa", schema(deprecated))]
    #[serde(default, deserialize_with = "some_option")]
    pub metadata: Option<Option<serde_json::Value>>,

    /// the message this message is replying to
    #[serde(default, deserialize_with = "some_option")]
    pub reply_id: Option<Option<MessageId>>,

    /// override the name of this message's sender
    ///
    /// deprecated: create new puppets for each bridged user instead
    #[cfg_attr(feature = "utoipa", schema(deprecated))]
    pub override_name: Option<Option<String>>,

    pub embeds: Option<Vec<EmbedCreate>>,

    pub edited_at: Option<Time>,
}

// NOTE: utoipa doesnt seem to like #[deprecated] here
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
#[serde(tag = "type")]
pub enum MessageType {
    /// a basic message, using markdown
    DefaultMarkdown(MessageDefaultMarkdown),

    #[cfg(feature = "feat_message_forwarding")]
    /// (TODO) a message copied from somewhere else
    Forward(MessageDefaultTagged),

    /// a message was pinned
    MessagePinned(MessagePin),

    #[cfg(feature = "feat_message_move")]
    /// (TODO) one or more messages were moved
    MessagesMoved(MessagesMoved),

    /// a thread member was added to the thread or group dm
    MemberAdd(MessageMember),

    /// a thread member was removed from the thread or group dm
    MemberRemove(MessageMember),

    /// a room member joined the room
    MemberJoin,

    /// (TODO) call ended in a dm/gdm
    Call(MessageCall),

    /// this thread was renamed
    ThreadRename(MessageThreadRename),

    /// A thread was created from a message
    ThreadCreated(MessageThreadCreated),

    /// (TODO) someone mentioned this thread
    // needs some sort of antispam system. again, see github.
    // doesnt necessarily reference a thread in the same room, but usually should
    ThreadPingback(MessageThreadPingback),
    // /// (TODO) receive announcement threads from this room
    // // but where does this get sent to???
    // RoomFollowed(MessageRoomFollowed),

    // /// (TODO) interact with a bot, uncertain if i'll go this route
    // BotCommand(MessageBotCommand),

    // /// (TODO) implement a reporting system? uncertain (reports are certain, but reports-as-messages vs as-threads idk)
    // // #[deprecated = "reports will be impl'd as threads"]
    // ModerationReport(MessageModerationReport),

    // Nudge,
}

/// Information about a message being pinned
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
pub struct MessagePin {
    pub pinned_message_id: MessageId,
}

/// Information about a thread being renamed
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
pub struct MessageThreadRename {
    #[serde(alias = "new")]
    pub name_new: String,

    #[serde(alias = "old")]
    pub name_old: String,
}

/// Information about a thread being created
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
pub struct MessageThreadCreated {
    /// the message this thread was created from
    pub source_message_id: Option<MessageId>,
}

/// Information about the pingback
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
pub struct MessageThreadPingback {
    pub source_room_id: RoomId,
    pub source_channel_id: ChannelId,
    pub source_user_id: UserId,
}

#[cfg(feature = "feat_message_move")]
/// Information about one or more messages being moved between threads
/// probably want this being sent in both the source and target threads, maybe
/// with a bit of different styling depending on whether its source/target
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
pub struct MessagesMoved {
    // do messages keep their ids when being moved?
    pub start_id: MessageId,
    pub end_id: MessageId,
    pub source_id: ChannelId,
    pub target_id: ChannelId,
    pub reason: Option<String>,
}

/// Information about a member being added or removed from a thread
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
pub struct MessageMember {
    pub target_user_id: UserId,
}

/// Following a room and will receive announcement posts from it
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
pub struct MessageRoomFollowed {
    pub thread_id: ChannelId,
    pub reason: Option<String>,
}

/// audit log entries as a message (builtin moderation logging?)
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
pub struct MessageModerationLog {
    pub audit_log_entry: AuditLogEntry,
}

/// a report that moderators should look at
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
pub struct MessageModerationReport {
    pub report: Report,
}

/// a bot command
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
pub struct MessageBotCommand {
    pub command_id: String,
}

/// a basic message, written using markdown
///
/// NOTE: new message features won't be backported here!
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
#[cfg_attr(feature = "validator", derive(Validate))]
pub struct MessageDefaultMarkdown {
    /// the message's content in markdown
    #[cfg_attr(feature = "utoipa", schema(min_length = 1, max_length = 8192))]
    #[cfg_attr(feature = "validator", validate(length(min = 1, max = 8192)))]
    pub content: Option<String>,

    // TODO(#325): use MediaRef here during create
    #[cfg_attr(feature = "utoipa", schema(min_length = 1, max_length = 32))]
    #[cfg_attr(feature = "validator", validate(length(min = 1, max = 32), nested))]
    pub attachments: Vec<Media>,

    /// arbitrary metadata associated with a message
    ///
    /// deprecated: arbitrary metadata is too dubious, sorry. will come up with a better solution later
    #[cfg_attr(feature = "utoipa", schema(deprecated))]
    pub metadata: Option<serde_json::Value>,

    /// the message this message is replying to
    pub reply_id: Option<MessageId>,

    #[cfg_attr(feature = "utoipa", schema(min_length = 1, max_length = 32))]
    #[cfg_attr(feature = "validator", validate(length(min = 1, max = 32), nested))]
    pub embeds: Vec<Embed>,

    /// override the name of this message's sender
    ///
    /// deprecated: create new puppets for each bridged user instead
    #[cfg_attr(feature = "utoipa", schema(deprecated))]
    pub override_name: Option<String>,
    // // experimental! don't touch yet.
    // #[cfg(feature = "feat_interaction")]
    // #[cfg_attr(feature = "utoipa", schema(ignore))]
    // #[serde(default)]
    // pub interactions: Interactions,
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
#[cfg_attr(feature = "validator", derive(Validate))]
pub struct MessageCall {
    /// when the call ended. is None if the call is still going.
    pub ended_at: Option<Time>,

    /// the people who joined the call
    pub participants: Vec<UserId>,
}

/// ways to interact with a message
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
#[cfg_attr(feature = "validator", derive(Validate))]
pub struct Interactions {
    #[cfg(feature = "feat_interaction_reaction")]
    /// show placeholder reactions (they appear with zero total reactions) for these emoji
    pub reactions_default: Option<Vec<Emoji>>,

    // yet another rabbit hole. not worth it for now.
    #[cfg(feature = "feat_interaction_status")]
    #[serde(flatten)]
    pub status: Option<InteractionStatus>,
}

/// the current status
#[cfg(feature = "feat_interaction_reaction")]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
pub enum InteractionStatus {
    /// This message is still loading, or the action it represents is in progress
    ///
    /// - Will switch to Failed after 5 minutes or 30 seconds without edit
    /// - Can edit without creating message history entry
    /// - Intended for dynamic/streaming responses
    Loading,

    /// The (inter)action this message represents failed
    Failed {
        reason: String,
        // code: InteractionStatusKnownErrorCode,
        can_retry: bool,
    },
}

// enum InteractionStatusKnownErrorCode {
//     Forbidden,
//     Timeout,
//     BadInput,
//     Missing,
//     Conflict,
//     Gone,
//     TooLarge,
//     Cancelled,
//     Ratelimit,
// }

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
#[cfg_attr(feature = "validator", derive(Validate))]
pub struct MessageMigrate {
    /// which messages to move
    #[serde(default)]
    #[cfg_attr(feature = "validator", validate(length(min = 1, max = 128)))]
    pub message_ids: Vec<MessageId>,

    /// must be in same room (for now...)
    pub target_id: ChannelId,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
#[cfg_attr(feature = "validator", derive(Validate))]
pub struct MessageModerate {
    /// which messages to delete
    #[serde(default)]
    #[cfg_attr(feature = "validator", validate(length(max = 128)))]
    pub delete: Vec<MessageId>,

    /// which messages to remove
    #[serde(default)]
    #[cfg_attr(feature = "validator", validate(length(max = 128)))]
    pub remove: Vec<MessageId>,

    /// which messages to restore
    #[serde(default)]
    #[cfg_attr(feature = "validator", validate(length(max = 128)))]
    pub restore: Vec<MessageId>,
}

#[derive(Debug, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema, IntoParams))]
#[cfg_attr(feature = "validator", derive(Validate))]
pub struct RepliesQuery {
    /// how deeply to fetch replies
    #[serde(default = "fn_one")]
    #[cfg_attr(feature = "validator", validate(range(min = 1, max = 8)))]
    pub depth: u16,

    /// how many replies to fetch per branch
    pub breadth: Option<u16>,
}

/// always returns one
fn fn_one() -> u16 {
    1
}

#[derive(Debug, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema, IntoParams))]
pub struct ContextQuery {
    pub to_start: Option<MessageId>,
    pub to_end: Option<MessageId>,
    pub limit: Option<u16>,
}

#[derive(Debug, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
pub struct ContextResponse {
    pub items: Vec<Message>,
    pub total: u64,
    pub has_after: bool,
    pub has_before: bool,
}

impl Diff<Message> for MessagePatch {
    fn changes(&self, other: &Message) -> bool {
        match &other.message_type {
            MessageType::DefaultMarkdown(m) => {
                self.content.changes(&m.content)
                    || self.metadata.changes(&m.metadata)
                    || self.reply_id.changes(&m.reply_id)
                    || self.override_name.changes(&m.override_name)
                    || self.embeds.is_some()
                    || self.attachments.as_ref().is_some_and(|a| {
                        a.len() != m.attachments.len()
                            || a.iter().zip(&m.attachments).any(|(a, b)| a.id != b.id)
                    })
            }
            // this edit is invalid!
            _ => false,
        }
    }
}

impl MessageType {
    pub fn is_deletable(&self) -> bool {
        match self {
            MessageType::DefaultMarkdown(_) => true,
            #[cfg(feature = "feat_message_forwarding")]
            MessageType::Forward(_) => true,
            MessageType::MessagePinned(_) => true,
            MessageType::MemberAdd(_) => false,
            MessageType::MemberRemove(_) => false,
            MessageType::MemberJoin => true,
            MessageType::ThreadRename(_) => false,
            MessageType::ThreadPingback(_) => true,
            #[cfg(feature = "feat_message_move")]
            MessageType::MessagesMoved(_) => false,
            MessageType::Call(_) => false,
            MessageType::ThreadCreated(_) => false,
        }
    }

    pub fn is_editable(&self) -> bool {
        matches!(self, MessageType::DefaultMarkdown(_))
    }

    pub fn is_movable(&self) -> bool {
        matches!(self, MessageType::DefaultMarkdown(_))
    }
}