lamprey-bridge 0.1.0

yet another chat thing?
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
use anyhow::Result;
use async_trait::async_trait;
use common::v1::types::{ChannelId, MediaId, MessageId, RoomId};
use serenity::all::{
    AttachmentId as DcAttachmentId, ChannelId as DcChannelId, MessageId as DcMessageId,
};
use sqlx::{query, query_as};
use uuid::Uuid;

use crate::common::{Globals, PortalConfig, RealmConfig};

struct PortalConfigRow {
    pub lamprey_thread_id: String,
    pub lamprey_room_id: String,
    pub discord_guild_id: String,
    pub discord_channel_id: String,
    pub discord_thread_id: Option<String>,
    pub discord_webhook: String,
}

impl TryFrom<PortalConfigRow> for PortalConfig {
    type Error = anyhow::Error;

    fn try_from(value: PortalConfigRow) -> Result<Self, Self::Error> {
        Ok(Self {
            lamprey_thread_id: value.lamprey_thread_id.parse()?,
            lamprey_room_id: value.lamprey_room_id.parse()?,
            discord_guild_id: value.discord_guild_id.parse()?,
            discord_channel_id: value.discord_channel_id.parse()?,
            discord_thread_id: value.discord_thread_id.map(|v| v.parse()).transpose()?,
            discord_webhook: value.discord_webhook,
        })
    }
}

impl From<PortalConfig> for PortalConfigRow {
    fn from(value: PortalConfig) -> Self {
        Self {
            lamprey_thread_id: value.lamprey_thread_id.to_string(),
            lamprey_room_id: value.lamprey_room_id.to_string(),
            discord_guild_id: value.discord_guild_id.to_string(),
            discord_channel_id: value.discord_channel_id.to_string(),
            discord_thread_id: value.discord_thread_id.map(|v| v.to_string()),
            discord_webhook: value.discord_webhook,
        }
    }
}

pub struct MessageMetadata {
    pub chat_id: MessageId,
    pub chat_thread_id: ChannelId,
    pub discord_id: DcMessageId,
    /// the THREAD id, falling back to channel id
    pub discord_channel_id: DcChannelId,
}

struct MessageMetadataRow {
    chat_id: String,
    chat_thread_id: String,
    discord_id: String,
    discord_channel_id: String,
}

pub struct AttachmentMetadata {
    pub chat_id: MediaId,
    pub discord_id: DcAttachmentId,
}

struct AttachmentMetadataRow {
    chat_id: String,
    discord_id: String,
}

#[derive(Debug)]
pub struct Puppet {
    pub id: Uuid,
    pub ext_platform: String,
    pub ext_id: String,
    pub ext_avatar: Option<String>,
    pub ext_banner: Option<String>,
    pub name: String,
    pub avatar: Option<String>,
    pub banner: Option<String>,
    // TODO: remove Option
    pub bot: Option<bool>,
}

struct RealmConfigRow {
    lamprey_room_id: String,
    discord_guild_id: String,
    continuous: bool,
}

impl TryFrom<RealmConfigRow> for RealmConfig {
    type Error = anyhow::Error;

    fn try_from(value: RealmConfigRow) -> Result<Self> {
        Ok(Self {
            lamprey_room_id: value.lamprey_room_id.parse()?,
            discord_guild_id: value.discord_guild_id.parse()?,
            continuous: value.continuous,
        })
    }
}

impl From<RealmConfig> for RealmConfigRow {
    fn from(value: RealmConfig) -> Self {
        Self {
            lamprey_room_id: value.lamprey_room_id.to_string(),
            discord_guild_id: value.discord_guild_id.to_string(),
            continuous: value.continuous,
        }
    }
}

impl TryFrom<MessageMetadataRow> for MessageMetadata {
    type Error = anyhow::Error;

    fn try_from(row: MessageMetadataRow) -> Result<Self> {
        Ok(MessageMetadata {
            chat_id: row.chat_id.parse()?,
            chat_thread_id: row.chat_thread_id.parse()?,
            discord_id: row.discord_id.parse()?,
            discord_channel_id: row.discord_channel_id.parse()?,
        })
    }
}

impl From<MessageMetadata> for MessageMetadataRow {
    fn from(value: MessageMetadata) -> Self {
        MessageMetadataRow {
            chat_id: value.chat_id.to_string(),
            chat_thread_id: value.chat_thread_id.to_string(),
            discord_id: value.discord_id.to_string(),
            discord_channel_id: value.discord_channel_id.to_string(),
        }
    }
}

impl TryFrom<AttachmentMetadataRow> for AttachmentMetadata {
    type Error = anyhow::Error;

    fn try_from(row: AttachmentMetadataRow) -> Result<Self> {
        Ok(Self {
            chat_id: row.chat_id.parse()?,
            discord_id: row.discord_id.parse()?,
        })
    }
}

impl From<AttachmentMetadata> for AttachmentMetadataRow {
    fn from(value: AttachmentMetadata) -> Self {
        Self {
            chat_id: value.chat_id.to_string(),
            discord_id: value.discord_id.to_string(),
        }
    }
}

#[async_trait]
pub trait Data {
    async fn get_portals(&self) -> Result<Vec<PortalConfig>>;
    async fn get_portal_by_thread_id(&self, id: ChannelId) -> Result<Option<PortalConfig>>;
    async fn get_portal_by_discord_channel(&self, id: DcChannelId) -> Result<Option<PortalConfig>>;
    async fn insert_portal(&self, portal: PortalConfig) -> Result<()>;
    async fn delete_portal(&self, lamprey_thread_id: ChannelId) -> Result<()>;
    async fn get_message(&self, message_id: MessageId) -> Result<Option<MessageMetadata>>;
    async fn get_message_dc(&self, message_id: DcMessageId) -> Result<Option<MessageMetadata>>;
    async fn get_attachment(&self, media_id: MediaId) -> Result<Option<AttachmentMetadata>>;
    async fn get_attachment_dc(
        &self,
        attachment_id: DcAttachmentId,
    ) -> Result<Option<AttachmentMetadata>>;
    async fn get_last_message_ch(&self, thread_id: ChannelId) -> Result<Option<MessageMetadata>>;
    async fn get_last_message_dc(&self, channel_id: DcChannelId)
        -> Result<Option<MessageMetadata>>;
    async fn insert_message(&self, meta: MessageMetadata) -> Result<()>;
    async fn insert_attachment(&self, meta: AttachmentMetadata) -> Result<()>;
    async fn delete_message(&self, message_id: MessageId) -> Result<()>;
    async fn delete_message_dc(&self, message_id: DcMessageId) -> Result<()>;
    async fn get_puppet(&self, ext_platform: &str, ext_id: &str) -> Result<Option<Puppet>>;
    async fn insert_puppet(&self, data: Puppet) -> Result<()>;
    async fn get_realms(&self) -> Result<Vec<RealmConfig>>;
    async fn insert_realm(&self, config: RealmConfig) -> Result<()>;
    async fn delete_realm(&self, lamprey_room_id: RoomId) -> Result<()>;
}

#[async_trait]
impl Data for Globals {
    async fn get_portals(&self) -> Result<Vec<PortalConfig>> {
        let rows = query_as!(PortalConfigRow, "SELECT * FROM portal")
            .fetch_all(&self.pool)
            .await?;
        rows.into_iter().map(|r| r.try_into()).collect()
    }

    async fn get_portal_by_thread_id(&self, id: ChannelId) -> Result<Option<PortalConfig>> {
        let id = id.to_string();
        let row = query_as!(
            PortalConfigRow,
            "SELECT * FROM portal WHERE lamprey_thread_id = ?",
            id
        )
        .fetch_optional(&self.pool)
        .await?;
        row.map(|r| r.try_into()).transpose()
    }

    async fn get_portal_by_discord_channel(&self, id: DcChannelId) -> Result<Option<PortalConfig>> {
        let id = id.to_string();
        let row = query_as!(
            PortalConfigRow,
            "SELECT * FROM portal WHERE discord_channel_id = ? OR discord_thread_id = ?",
            id,
            id
        )
        .fetch_optional(&self.pool)
        .await?;
        row.map(|r| r.try_into()).transpose()
    }

    async fn insert_portal(&self, portal: PortalConfig) -> Result<()> {
        let row: PortalConfigRow = portal.into();
        query!(
            r#"
            INSERT OR IGNORE INTO portal
            (lamprey_thread_id, lamprey_room_id, discord_guild_id, discord_channel_id, discord_thread_id, discord_webhook)
            VALUES (?, ?, ?, ?, ?, ?)
            "#,
            row.lamprey_thread_id,
            row.lamprey_room_id,
            row.discord_guild_id,
            row.discord_channel_id,
            row.discord_thread_id,
            row.discord_webhook
        )
        .execute(&self.pool)
        .await?;
        Ok(())
    }

    async fn delete_portal(&self, lamprey_thread_id: ChannelId) -> Result<()> {
        let id = lamprey_thread_id.to_string();
        query!("DELETE FROM portal WHERE lamprey_thread_id = ?", id)
            .execute(&self.pool)
            .await?;
        Ok(())
    }

    async fn get_message(&self, message_id: MessageId) -> Result<Option<MessageMetadata>> {
        let b1 = message_id.to_string();
        let row = query_as!(
            MessageMetadataRow,
            "SELECT * FROM message WHERE chat_id = ?",
            b1
        )
        .fetch_optional(&self.pool)
        .await?;
        let meta = match row {
            Some(row) => Some(row.try_into()?),
            None => None,
        };
        Ok(meta)
    }

    async fn get_message_dc(&self, message_id: DcMessageId) -> Result<Option<MessageMetadata>> {
        let b1 = message_id.to_string();
        let row = query_as!(
            MessageMetadataRow,
            "SELECT * FROM message WHERE discord_id = ?",
            b1
        )
        .fetch_optional(&self.pool)
        .await?;
        let meta = match row {
            Some(row) => Some(row.try_into()?),
            None => None,
        };
        Ok(meta)
    }

    async fn get_attachment_dc(
        &self,
        attachment_id: DcAttachmentId,
    ) -> Result<Option<AttachmentMetadata>> {
        let b1 = attachment_id.to_string();
        let row = query_as!(
            AttachmentMetadataRow,
            "SELECT * FROM attachment WHERE discord_id = ?",
            b1
        )
        .fetch_optional(&self.pool)
        .await?;
        let meta = match row {
            Some(row) => Some(row.try_into()?),
            None => None,
        };
        Ok(meta)
    }

    async fn get_attachment(&self, media_id: MediaId) -> Result<Option<AttachmentMetadata>> {
        let b1 = media_id.to_string();
        let row = query_as!(
            AttachmentMetadataRow,
            "SELECT * FROM attachment WHERE chat_id = ?",
            b1
        )
        .fetch_optional(&self.pool)
        .await?;
        let meta = match row {
            Some(row) => Some(row.try_into()?),
            None => None,
        };
        Ok(meta)
    }

    async fn insert_message(&self, meta: MessageMetadata) -> Result<()> {
        let row: MessageMetadataRow = meta.into();
        query!(
            "INSERT OR IGNORE INTO message (chat_id, chat_thread_id, discord_id, discord_channel_id) VALUES (?, ?, ?, ?)",
            row.chat_id,
            row.chat_thread_id,
            row.discord_id,
            row.discord_channel_id,
        )
            .execute(&self.pool)
            .await?;
        Ok(())
    }

    async fn insert_attachment(&self, meta: AttachmentMetadata) -> Result<()> {
        let row: AttachmentMetadataRow = meta.into();
        query!(
            "INSERT OR IGNORE INTO attachment (chat_id, discord_id) VALUES ($1, $2)",
            row.chat_id,
            row.discord_id
        )
        .execute(&self.pool)
        .await?;
        Ok(())
    }

    async fn get_last_message_ch(&self, thread_id: ChannelId) -> Result<Option<MessageMetadata>> {
        let b1 = thread_id.to_string();
        let row = query_as!(
            MessageMetadataRow,
            "SELECT * FROM message WHERE chat_thread_id = ? ORDER BY chat_id DESC LIMIT 1",
            b1
        )
        .fetch_optional(&self.pool)
        .await?;
        Ok(row.map(|r| r.try_into()).transpose()?)
    }

    async fn get_last_message_dc(
        &self,
        channel_id: DcChannelId,
    ) -> Result<Option<MessageMetadata>> {
        let id = channel_id.to_string();
        let row = query_as!(
            MessageMetadataRow,
            "SELECT * FROM message WHERE discord_channel_id = ? ORDER BY discord_id DESC LIMIT 1",
            id
        )
        .fetch_optional(&self.pool)
        .await?;
        Ok(row.map(|r| r.try_into()).transpose()?)
    }

    async fn delete_message(&self, message_id: MessageId) -> Result<()> {
        let b1 = message_id.to_string();
        query!("DELETE FROM message WHERE chat_id = ?", b1)
            .execute(&self.pool)
            .await?;
        Ok(())
    }

    async fn delete_message_dc(&self, message_id: DcMessageId) -> Result<()> {
        let b1 = message_id.to_string();
        query!("DELETE FROM message WHERE discord_id = ?", b1)
            .execute(&self.pool)
            .await?;
        Ok(())
    }

    async fn get_puppet(&self, ext_platform: &str, ext_id: &str) -> Result<Option<Puppet>> {
        let row = query_as!(
            Puppet,
            r#"
            SELECT id AS "id!: Uuid", ext_platform, ext_id, ext_avatar, ext_banner, name, avatar, banner, bot
            FROM puppet WHERE ext_platform = ? AND ext_id = ?
            "#,
            ext_platform,
            ext_id,
        )
        .fetch_optional(&self.pool)
        .await?;
        Ok(row)
    }

    async fn insert_puppet(&self, data: Puppet) -> Result<()> {
        query!(
            r#"
            INSERT OR REPLACE INTO puppet (id, ext_platform, ext_id, ext_avatar, ext_banner, name, avatar, banner, bot)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
            "#,
            data.id,
            data.ext_platform,
            data.ext_id,
            data.ext_avatar,
            data.ext_banner,
            data.name,
            data.avatar,
            data.banner,
            data.bot,
        )
        .execute(&self.pool)
        .await?;
        Ok(())
    }

    async fn get_realms(&self) -> Result<Vec<RealmConfig>> {
        let rows = query_as!(
            RealmConfigRow,
            r#"
            SELECT
              lamprey_room_id AS "lamprey_room_id!: String",
              discord_guild_id AS "discord_guild_id!: String",
              continuous
            FROM realm
            "#,
        )
        .fetch_all(&self.pool)
        .await?;
        rows.into_iter().map(|r| r.try_into()).collect()
    }

    async fn insert_realm(&self, config: RealmConfig) -> Result<()> {
        let row: RealmConfigRow = config.into();
        query!(
            r#"
             INSERT OR REPLACE INTO realm (lamprey_room_id, discord_guild_id, continuous)
             VALUES (?, ?, ?)
             "#,
            row.lamprey_room_id,
            row.discord_guild_id,
            row.continuous
        )
        .execute(&self.pool)
        .await?;
        Ok(())
    }

    async fn delete_realm(&self, lamprey_room_id: RoomId) -> Result<()> {
        let id = lamprey_room_id.to_string();
        query!("DELETE FROM realm WHERE lamprey_room_id = ?", id)
            .execute(&self.pool)
            .await?;
        Ok(())
    }
}