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
use std::sync::Arc;

use anyhow::{Error, Result};
use common::v1::types::{
    self,
    misc::UserIdReq,
    pagination::{PaginationQuery, PaginationResponse},
    user_status, Channel, ChannelId, ChannelType, Media, MediaCreate, MediaCreateSource,
    MessageCreate, MessageId, MessageSync, RoomId, Session, User, UserId,
};
use sdk::{Client, EventHandler, Http};
use tokio::sync::{mpsc, oneshot};
use tracing::{debug, error, info};

use crate::{
    bridge::BridgeMessage,
    common::{Globals, GlobalsTrait},
    data::Data,
    portal::PortalMessage,
};

pub struct Lamprey {
    recv: mpsc::Receiver<LampreyMessage>,
    client: Client,
    globals: Arc<Globals>,
}

pub enum LampreyMessage {
    Handle {
        response: oneshot::Sender<LampreyHandle>,
    },
}

struct Handle {
    globals: Arc<Globals>,
}

impl EventHandler for Handle {
    type Error = Error;

    async fn ready(&mut self, _user: Option<User>, _session: Session) -> Result<()> {
        Ok(())
    }

    async fn error(&mut self, err: String) -> Result<()> {
        error!("lamprey sync error: {err}");
        Ok(())
    }

    async fn sync(&mut self, msg: MessageSync) -> Result<()> {
        match msg {
            MessageSync::ChannelCreate { channel: thread } => {
                info!("chat upsert thread");
                let Ok(realms) = self.globals.get_realms().await else {
                    return Ok(());
                };

                let Some(realm_config) = realms
                    .iter()
                    .find(|c| Some(c.lamprey_room_id) == thread.room_id)
                else {
                    return Ok(());
                };

                if !realm_config.continuous {
                    return Ok(());
                }

                if self
                    .globals
                    .get_portal_by_thread_id(thread.id)
                    .await?
                    .is_some()
                {
                    return Ok(());
                }

                if let Err(e) = self
                    .globals
                    .bridge_chan
                    .send(BridgeMessage::LampreyThreadCreate {
                        thread: *thread,
                        discord_guild_id: realm_config.discord_guild_id,
                    })
                {
                    error!("failed to send lamprey thread create message: {e}");
                }
            }
            MessageSync::MessageCreate { message } => {
                info!("lamprey message create");
                self.globals
                    .portal_send(
                        message.channel_id,
                        PortalMessage::LampreyMessageCreate { message },
                    )
                    .await;
            }
            MessageSync::MessageUpdate { message } => {
                info!("lamprey message update");
                self.globals
                    .portal_send(
                        message.channel_id,
                        PortalMessage::LampreyMessageUpdate { message },
                    )
                    .await;
            }
            MessageSync::MessageDelete {
                channel_id: thread_id,
                message_id,
            } => {
                info!("lamprey message delete");
                self.globals
                    .portal_send(
                        thread_id,
                        PortalMessage::LampreyMessageDelete { message_id },
                    )
                    .await;
            }
            _ => {}
        }
        Ok(())
    }
}

impl Lamprey {
    pub fn new(globals: Arc<Globals>, recv: mpsc::Receiver<LampreyMessage>) -> Self {
        let token = globals.config.lamprey_token.clone();
        let base_url = globals.config.lamprey_base_url.clone();
        let ws_url = globals.config.lamprey_ws_url.clone();
        let handle = Handle {
            globals: globals.clone(),
        };
        let mut client = Client::new(token.clone().into()).with_handler(Box::new(handle));
        client.http = if let Some(base_url) = base_url {
            client.http.with_base_url(base_url.parse().unwrap())
        } else {
            client.http
        };
        client.syncer = if let Some(ws_url) = ws_url {
            client.syncer.with_base_url(ws_url.parse().unwrap())
        } else {
            client.syncer
        };
        Self {
            client,
            recv,
            globals,
        }
    }

    pub async fn connect(mut self) -> Result<()> {
        tokio::spawn(async move {
            while let Some(msg) = self.recv.recv().await {
                info!("got msg");
                match handle(self.globals.clone(), msg, &self.client.http).await {
                    Ok(_) => {}
                    Err(err) => error!("{err}"),
                };
            }
        });

        let _ = self.client.syncer.connect().await;
        Ok(())
    }
}

async fn handle(globals: Arc<Globals>, msg: LampreyMessage, http: &Http) -> Result<()> {
    match msg {
        LampreyMessage::Handle { response } => {
            let _ = response.send(LampreyHandle {
                globals,
                http: http.clone(),
            });
        }
    }
    Ok(())
}

pub struct LampreyHandle {
    http: Http,
    globals: Arc<Globals>,
}

impl LampreyHandle {
    pub async fn media_upload(
        &self,
        filename: String,
        bytes: Vec<u8>,
        user_id: UserId,
    ) -> Result<Media> {
        let req = MediaCreate {
            alt: None,
            source: MediaCreateSource::Upload {
                filename,
                size: bytes.len() as u64,
            },
        };
        let upload = self.http.for_puppet(user_id).media_create(&req).await?;
        let media = self
            .http
            .for_puppet(user_id)
            .media_upload(&upload, bytes)
            .await?;
        media.ok_or(anyhow::anyhow!("failed to upload"))
    }

    pub async fn message_get(
        &self,
        thread_id: ChannelId,
        message_id: MessageId,
    ) -> Result<types::Message> {
        let res = self.http.message_get(thread_id, message_id).await?;
        Ok(res)
    }

    pub async fn message_list(
        &self,
        thread_id: ChannelId,
        query: &PaginationQuery<MessageId>,
    ) -> Result<PaginationResponse<types::Message>> {
        let res = self.http.message_list(thread_id, query).await?;
        Ok(res)
    }

    pub async fn message_create(
        &self,
        thread_id: ChannelId,
        user_id: UserId,
        req: MessageCreate,
    ) -> Result<types::Message> {
        let res = self
            .http
            .for_puppet(user_id)
            .message_create(thread_id, &req)
            .await?;
        Ok(res)
    }

    pub async fn message_update(
        &self,
        thread_id: ChannelId,
        message_id: MessageId,
        user_id: UserId,
        req: types::MessagePatch,
    ) -> Result<types::Message> {
        let res = self
            .http
            .for_puppet(user_id)
            .message_update(thread_id, message_id, &req)
            .await?;
        Ok(res)
    }

    pub async fn message_delete(
        &self,
        thread_id: ChannelId,
        message_id: MessageId,
        user_id: UserId,
    ) -> Result<()> {
        self.http
            .for_puppet(user_id)
            .message_delete(thread_id, message_id)
            .await?;
        Ok(())
    }

    pub async fn message_react(
        &self,
        thread_id: ChannelId,
        message_id: MessageId,
        user_id: UserId,
        reaction: String,
    ) -> Result<()> {
        self.http
            .for_puppet(user_id)
            .message_react(thread_id, message_id, reaction)
            .await?;
        Ok(())
    }

    pub async fn message_unreact(
        &self,
        thread_id: ChannelId,
        message_id: MessageId,
        user_id: UserId,
        reaction: String,
    ) -> Result<()> {
        self.http
            .for_puppet(user_id)
            .message_unreact(thread_id, message_id, reaction)
            .await?;
        Ok(())
    }

    pub async fn typing_start(&self, thread_id: ChannelId, user_id: UserId) -> Result<()> {
        self.http
            .for_puppet(user_id)
            .typing_start(thread_id)
            .await?;
        Ok(())
    }

    pub async fn puppet_ensure(
        &self,
        name: String,
        key: String,
        room_id: RoomId,
        bot: bool,
    ) -> Result<User> {
        let app_id = self.globals.config.lamprey_application_id;
        let user = self
            .http
            .puppet_ensure(
                app_id,
                key,
                &types::PuppetCreate {
                    name,
                    description: None,
                    bot,
                    system: false,
                },
            )
            .await?;
        debug!("ensured user");
        self.http.room_member_put(room_id, user.id).await?;
        debug!("ensured room member");
        Ok(user)
    }

    pub async fn user_fetch(&self, user_id: UserId) -> Result<User> {
        let res = self.http.user_get(user_id).await?;
        Ok(res)
    }

    pub async fn user_update(&self, user_id: UserId, patch: &types::UserPatch) -> Result<User> {
        let res = self
            .http
            .for_puppet(user_id)
            .user_update(UserIdReq::UserId(user_id), patch)
            .await?;
        Ok(res)
    }

    pub async fn user_set_status(
        &self,
        user_id: UserId,
        patch: &user_status::StatusPatch,
    ) -> Result<()> {
        self.http
            .for_puppet(user_id)
            .user_set_status(UserIdReq::UserId(user_id), patch)
            .await?;
        Ok(())
    }

    pub async fn room_member_patch(
        &self,
        room_id: RoomId,
        user_id: UserId,
        patch: &types::RoomMemberPatch,
    ) -> Result<types::RoomMember> {
        let res = self
            .http
            .room_member_patch(room_id, UserIdReq::UserId(user_id), patch)
            .await?;
        Ok(res)
    }

    pub async fn room_threads(&self, room_id: RoomId) -> Result<Vec<Channel>> {
        let mut all_threads = Vec::new();
        let mut query = PaginationQuery::default();
        loop {
            info!("get room threads");
            let res = self.http.thread_list(room_id, &query).await?;
            debug!("threads: {res:?}");
            all_threads.extend(res.items);
            if let Some(cursor) = res.cursor {
                query.from = Some(cursor.parse()?);
            } else {
                break;
            }
            if !res.has_more {
                break;
            }
        }
        Ok(all_threads)
    }

    pub async fn create_thread(
        &self,
        room_id: RoomId,
        name: String,
        topic: Option<String>,
        ty: ChannelType,
        parent_id: Option<ChannelId>,
    ) -> Result<Channel> {
        let res = self
            .http
            .channel_create(
                room_id,
                &types::ChannelCreate {
                    name,
                    description: topic,
                    ty,
                    tags: None,
                    nsfw: false,
                    recipients: None,
                    bitrate: None,
                    user_limit: None,
                    parent_id,
                    icon: None,
                },
            )
            .await?;
        Ok(res)
    }
}