split Destination enum into unit

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-08-03 07:16:39 +00:00
parent eb6e509ad8
commit 15ad7523aa
2 changed files with 62 additions and 56 deletions

View file

@ -0,0 +1,56 @@
use std::fmt::Debug;
use conduit::implement;
use ruma::{OwnedServerName, OwnedUserId};
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Destination {
Appservice(String),
Push(OwnedUserId, String), // user and pushkey
Normal(OwnedServerName),
}
#[implement(Destination)]
#[must_use]
pub fn get_prefix(&self) -> Vec<u8> {
match self {
Self::Normal(server) => {
let len = server.as_bytes().len().saturating_add(1);
let mut p = Vec::with_capacity(len);
p.extend_from_slice(server.as_bytes());
p.push(0xFF);
p
},
Self::Appservice(server) => {
let sigil = b"+";
let len = sigil
.len()
.saturating_add(server.as_bytes().len())
.saturating_add(1);
let mut p = Vec::with_capacity(len);
p.extend_from_slice(sigil);
p.extend_from_slice(server.as_bytes());
p.push(0xFF);
p
},
Self::Push(user, pushkey) => {
let sigil = b"$";
let len = sigil
.len()
.saturating_add(user.as_bytes().len())
.saturating_add(1)
.saturating_add(pushkey.as_bytes().len())
.saturating_add(1);
let mut p = Vec::with_capacity(len);
p.extend_from_slice(sigil);
p.extend_from_slice(user.as_bytes());
p.push(0xFF);
p.extend_from_slice(pushkey.as_bytes());
p.push(0xFF);
p
},
}
}

View file

@ -1,5 +1,6 @@
mod appservice; mod appservice;
mod data; mod data;
mod dest;
mod send; mod send;
mod sender; mod sender;
@ -9,16 +10,18 @@ use async_trait::async_trait;
use conduit::{err, warn, Result, Server}; use conduit::{err, warn, Result, Server};
use ruma::{ use ruma::{
api::{appservice::Registration, OutgoingRequest}, api::{appservice::Registration, OutgoingRequest},
OwnedServerName, OwnedUserId, RoomId, ServerName, UserId, OwnedServerName, RoomId, ServerName, UserId,
}; };
use tokio::sync::Mutex; use tokio::sync::Mutex;
use self::data::Data;
pub use self::dest::Destination;
use crate::{account_data, client, globals, presence, pusher, resolver, rooms, users, Dep}; use crate::{account_data, client, globals, presence, pusher, resolver, rooms, users, Dep};
pub struct Service { pub struct Service {
server: Arc<Server>, server: Arc<Server>,
services: Services, services: Services,
pub db: data::Data, pub db: Data,
sender: loole::Sender<Msg>, sender: loole::Sender<Msg>,
receiver: Mutex<loole::Receiver<Msg>>, receiver: Mutex<loole::Receiver<Msg>>,
} }
@ -46,13 +49,6 @@ struct Msg {
queue_id: Vec<u8>, queue_id: Vec<u8>,
} }
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Destination {
Appservice(String),
Push(OwnedUserId, String), // user and pushkey
Normal(OwnedServerName),
}
#[allow(clippy::module_name_repetitions)] #[allow(clippy::module_name_repetitions)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)] #[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum SendingEvent { pub enum SendingEvent {
@ -82,7 +78,7 @@ impl crate::Service for Service {
appservice: args.depend::<crate::appservice::Service>("appservice"), appservice: args.depend::<crate::appservice::Service>("appservice"),
pusher: args.depend::<pusher::Service>("pusher"), pusher: args.depend::<pusher::Service>("pusher"),
}, },
db: data::Data::new(&args), db: Data::new(&args),
sender, sender,
receiver: Mutex::new(receiver), receiver: Mutex::new(receiver),
})) }))
@ -280,49 +276,3 @@ impl Service {
self.sender.send(msg).map_err(|e| err!("{e}")) self.sender.send(msg).map_err(|e| err!("{e}"))
} }
} }
impl Destination {
#[must_use]
pub fn get_prefix(&self) -> Vec<u8> {
match self {
Self::Normal(server) => {
let len = server.as_bytes().len().saturating_add(1);
let mut p = Vec::with_capacity(len);
p.extend_from_slice(server.as_bytes());
p.push(0xFF);
p
},
Self::Appservice(server) => {
let sigil = b"+";
let len = sigil
.len()
.saturating_add(server.as_bytes().len())
.saturating_add(1);
let mut p = Vec::with_capacity(len);
p.extend_from_slice(sigil);
p.extend_from_slice(server.as_bytes());
p.push(0xFF);
p
},
Self::Push(user, pushkey) => {
let sigil = b"$";
let len = sigil
.len()
.saturating_add(user.as_bytes().len())
.saturating_add(1)
.saturating_add(pushkey.as_bytes().len())
.saturating_add(1);
let mut p = Vec::with_capacity(len);
p.extend_from_slice(sigil);
p.extend_from_slice(user.as_bytes());
p.push(0xFF);
p.extend_from_slice(pushkey.as_bytes());
p.push(0xFF);
p
},
}
}
}