420dedbc1e
- Add IM service modules: audit, channel roles, custom emojis, forum tags, integrations, invitations, repo links, slash commands, stages, voice, webhooks - Add PR service modules: review requests, templates - Add repo service modules: contributors, release assets, git extras (archive, branch rename, commit extras, diff/merge, tag, tree) - Add user service: social (follow/block) - Add internal auth service - Update existing service modules with expanded functionality - Remove deleted IM modules: articles, delivery trace, drafts, follows, messages, polls, presence, reactions, threads
107 lines
2.2 KiB
Rust
107 lines
2.2 KiB
Rust
use std::sync::Arc;
|
|
use std::sync::atomic::{AtomicU64, Ordering};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
use tokio::sync::broadcast;
|
|
use uuid::Uuid;
|
|
|
|
use crate::models::base_info::UserBaseInfo;
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub enum ChannelAction {
|
|
Created,
|
|
Updated,
|
|
Deleted,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct ChannelEvent {
|
|
pub channel_id: Uuid,
|
|
pub action: ChannelAction,
|
|
pub workspace_name: Option<String>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub enum MemberAction {
|
|
Joined,
|
|
Updated,
|
|
Kicked,
|
|
Left,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct MemberEvent {
|
|
pub channel_id: Uuid,
|
|
pub user: UserBaseInfo,
|
|
pub user_id: Uuid,
|
|
pub action: MemberAction,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub enum CategoryAction {
|
|
Created,
|
|
Updated,
|
|
Deleted,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct CategoryEvent {
|
|
pub workspace_name: String,
|
|
pub category_id: Uuid,
|
|
pub action: CategoryAction,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub enum ImEvent {
|
|
Channel {
|
|
request_id: Uuid,
|
|
data: ChannelEvent,
|
|
},
|
|
Member {
|
|
request_id: Uuid,
|
|
data: MemberEvent,
|
|
},
|
|
Category {
|
|
request_id: Uuid,
|
|
data: CategoryEvent,
|
|
},
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct ImEventBus {
|
|
tx: broadcast::Sender<ImEvent>,
|
|
lagged: Arc<AtomicU64>,
|
|
}
|
|
|
|
impl ImEventBus {
|
|
pub fn new(capacity: usize) -> Self {
|
|
let (tx, _) = broadcast::channel(capacity);
|
|
Self {
|
|
tx,
|
|
lagged: Arc::new(AtomicU64::new(0)),
|
|
}
|
|
}
|
|
|
|
pub fn publish(&self, event: ImEvent) -> bool {
|
|
self.tx.send(event).is_ok()
|
|
}
|
|
|
|
pub fn subscribe(&self) -> broadcast::Receiver<ImEvent> {
|
|
self.tx.subscribe()
|
|
}
|
|
|
|
pub fn record_lagged(&self, count: u64) {
|
|
self.lagged.fetch_add(count, Ordering::Relaxed);
|
|
}
|
|
|
|
pub fn lagged_total(&self) -> u64 {
|
|
self.lagged.load(Ordering::Relaxed)
|
|
}
|
|
}
|
|
|
|
impl Default for ImEventBus {
|
|
fn default() -> Self {
|
|
Self::new(1024)
|
|
}
|
|
}
|