Files
zhenyi 821537186e refactor(tests): reformat code and update dependency management
- 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
2026-06-11 12:11:05 +08:00

98 lines
2.9 KiB
Rust

//! Pin event handlers on `MessageService`.
use std::sync::Arc;
use uuid::Uuid;
use crate::ImksError;
use crate::ImksResult;
use crate::socket::socket::Socket;
use super::message::MessageService;
impl MessageService {
/// Handle `pin:add` — pin a message, then broadcast to the channel room.
pub async fn pin_message(
&self,
socket: Arc<Socket>,
data: &serde_json::Value,
) -> ImksResult<()> {
let user_id = self.user_id(&socket)?;
let (channel_id, message_id) = self.parse_pin_payload(data)?;
self.ensure_member(&channel_id.to_string(), &user_id.to_string())
.await?;
self.repo
.pin_message(channel_id, message_id, user_id)
.await?;
if let Some(ns) = self.namespaces.get_namespace(&socket.namespace) {
let ns = ns.clone();
let cid = channel_id.to_string();
let mid = message_id.to_string();
tokio::spawn(async move {
ns.emit_to_room(
&cid,
"pin:added",
serde_json::json!({
"channel_id": cid,
"message_id": mid,
"pinned_by": user_id.to_string(),
}),
)
.await;
});
}
tracing::info!(%channel_id, %message_id, %user_id, "Message pinned");
Ok(())
}
/// Handle `pin:remove` — unpin a message.
pub async fn unpin_message(
&self,
socket: Arc<Socket>,
data: &serde_json::Value,
) -> ImksResult<()> {
let user_id = self.user_id(&socket)?;
let (channel_id, message_id) = self.parse_pin_payload(data)?;
self.ensure_member(&channel_id.to_string(), &user_id.to_string())
.await?;
self.repo.unpin_message(channel_id, message_id).await?;
if let Some(ns) = self.namespaces.get_namespace(&socket.namespace) {
let ns = ns.clone();
let cid = channel_id.to_string();
let mid = message_id.to_string();
tokio::spawn(async move {
ns.emit_to_room(
&cid,
"pin:removed",
serde_json::json!({
"channel_id": cid,
"message_id": mid,
}),
)
.await;
});
}
tracing::info!(%channel_id, %message_id, %user_id, "Message unpinned");
Ok(())
}
fn parse_pin_payload(&self, data: &serde_json::Value) -> ImksResult<(Uuid, Uuid)> {
let arr = data
.as_array()
.and_then(|a| a.first())
.ok_or_else(|| ImksError::InvalidInput("Expected [payload] array".into()))?;
Ok((
Self::parse_field(arr, "channel_id")?,
Self::parse_field(arr, "message_id")?,
))
}
}