821537186e
- Reorganized import statements in adapter tests for better readability - Replaced or_insert_with(Vec::new) with or_default() in test closures - Updated Cargo.lock with new dependency versions and checksums - Added TLS features to tonic dependency configuration - Included sqlx, chrono, and uuid dependencies with specific features - Added jsonwebtoken and arc-swap as project dependencies - Reformatted assertion statements to comply with line length limits - Adjusted base64 import order in engine codec module - Updated protobuf include statement formatting
76 lines
2.3 KiB
Rust
76 lines
2.3 KiB
Rust
//! Bookmark event handlers on `MessageService`.
|
|
|
|
use std::sync::Arc;
|
|
|
|
use uuid::Uuid;
|
|
|
|
use crate::ImksError;
|
|
use crate::socket::socket::Socket;
|
|
|
|
use super::message::MessageService;
|
|
|
|
impl MessageService {
|
|
/// Handle `bookmark:add` — toggle (add/update) a bookmark.
|
|
pub async fn add_bookmark(
|
|
&self,
|
|
socket: Arc<Socket>,
|
|
data: &serde_json::Value,
|
|
) -> crate::ImksResult<()> {
|
|
let user_id = self.user_id(&socket)?;
|
|
let arr = data
|
|
.as_array()
|
|
.and_then(|a| a.first())
|
|
.ok_or_else(|| ImksError::InvalidInput("Expected [payload] array".into()))?;
|
|
|
|
let message_id: Uuid = Self::parse_field(arr, "message_id")?;
|
|
let channel_id: Uuid = Self::parse_field(arr, "channel_id")?;
|
|
let note: Option<String> = Self::parse_optional(arr, "note")?;
|
|
|
|
self.repo
|
|
.add_bookmark(message_id, channel_id, user_id, note.as_deref())
|
|
.await?;
|
|
Ok(())
|
|
}
|
|
|
|
/// Handle `bookmark:remove` — remove a bookmark.
|
|
pub async fn remove_bookmark(
|
|
&self,
|
|
socket: Arc<Socket>,
|
|
data: &serde_json::Value,
|
|
) -> crate::ImksResult<()> {
|
|
let user_id = self.user_id(&socket)?;
|
|
let arr = data
|
|
.as_array()
|
|
.and_then(|a| a.first())
|
|
.ok_or_else(|| ImksError::InvalidInput("Expected [payload] array".into()))?;
|
|
|
|
let message_id: Uuid = Self::parse_field(arr, "message_id")?;
|
|
|
|
self.repo.remove_bookmark(message_id, user_id).await?;
|
|
Ok(())
|
|
}
|
|
|
|
/// Handle `bookmark:list` — list a user's bookmarks.
|
|
pub async fn list_bookmarks(
|
|
&self,
|
|
socket: Arc<Socket>,
|
|
data: &serde_json::Value,
|
|
) -> crate::ImksResult<()> {
|
|
let user_id = self.user_id(&socket)?;
|
|
let arr = data
|
|
.as_array()
|
|
.and_then(|a| a.first())
|
|
.ok_or_else(|| ImksError::InvalidInput("Expected [payload] array".into()))?;
|
|
|
|
let before: Option<Uuid> = Self::parse_optional(arr, "before")?;
|
|
let limit: Option<i64> = Self::parse_optional(arr, "limit")?;
|
|
|
|
let page = self.repo.list_bookmarks(user_id, before, limit).await?;
|
|
let _ = socket.emit(
|
|
"bookmark:loaded",
|
|
serde_json::to_value(&page).unwrap_or_default(),
|
|
);
|
|
Ok(())
|
|
}
|
|
}
|