2020-12-19 15:00:11 +00:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
convert::TryFrom,
|
2021-01-27 02:54:35 +00:00
|
|
|
fmt::{Debug, Display, Formatter},
|
2020-12-19 15:00:11 +00:00
|
|
|
sync::Arc,
|
|
|
|
time::{Duration, Instant, SystemTime},
|
|
|
|
};
|
2020-09-15 15:13:54 +01:00
|
|
|
|
2021-01-27 02:54:35 +00:00
|
|
|
use crate::{appservice_server, server_server, utils, Database, Error, PduEvent, Result};
|
2020-09-23 14:23:29 +01:00
|
|
|
use federation::transactions::send_transaction_message;
|
2020-12-31 20:07:05 +00:00
|
|
|
use log::info;
|
2020-09-15 15:13:54 +01:00
|
|
|
use rocket::futures::stream::{FuturesUnordered, StreamExt};
|
2020-12-08 09:33:44 +00:00
|
|
|
use ruma::{
|
2020-12-19 15:00:11 +00:00
|
|
|
api::{appservice, federation, OutgoingRequest},
|
2021-01-27 02:54:35 +00:00
|
|
|
events::{push_rules, EventType},
|
2021-01-29 15:14:09 +00:00
|
|
|
uint, ServerName, UInt,
|
2020-12-08 09:33:44 +00:00
|
|
|
};
|
2020-09-23 14:23:29 +01:00
|
|
|
use sled::IVec;
|
2021-01-27 02:54:35 +00:00
|
|
|
use tokio::{select, sync::Semaphore};
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
|
|
|
pub enum OutgoingKind {
|
|
|
|
Appservice(Box<ServerName>),
|
|
|
|
Push(Vec<u8>),
|
|
|
|
Normal(Box<ServerName>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for OutgoingKind {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
OutgoingKind::Appservice(name) => f.write_str(name.as_str()),
|
|
|
|
OutgoingKind::Normal(name) => f.write_str(name.as_str()),
|
|
|
|
OutgoingKind::Push(_) => f.write_str("Push notification TODO"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 15:13:54 +01:00
|
|
|
|
2020-11-09 11:21:04 +00:00
|
|
|
#[derive(Clone)]
|
2020-09-15 15:13:54 +01:00
|
|
|
pub struct Sending {
|
|
|
|
/// The state for a given state hash.
|
2021-01-27 02:54:35 +00:00
|
|
|
pub(super) servernamepduids: sled::Tree, // ServernamePduId = (+ / $)ServerName / UserId + PduId
|
|
|
|
pub(super) servercurrentpdus: sled::Tree, // ServerCurrentPdus = (+ / $)ServerName / UserId + PduId (pduid can be empty for reservation)
|
2020-12-19 15:00:11 +00:00
|
|
|
pub(super) maximum_requests: Arc<Semaphore>,
|
2020-09-15 15:13:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Sending {
|
2021-01-27 02:54:35 +00:00
|
|
|
pub fn start_handler(&self, db: &Database) {
|
2020-10-21 15:08:54 +01:00
|
|
|
let servernamepduids = self.servernamepduids.clone();
|
|
|
|
let servercurrentpdus = self.servercurrentpdus.clone();
|
2021-01-29 15:14:09 +00:00
|
|
|
let db = db.clone();
|
2020-09-15 15:13:54 +01:00
|
|
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
let mut futures = FuturesUnordered::new();
|
2020-09-23 14:23:29 +01:00
|
|
|
|
2020-10-21 15:08:54 +01:00
|
|
|
// Retry requests we could not finish yet
|
|
|
|
let mut current_transactions = HashMap::new();
|
|
|
|
|
2021-01-27 02:54:35 +00:00
|
|
|
for (outgoing_kind, pdu) in servercurrentpdus
|
2020-10-21 15:08:54 +01:00
|
|
|
.iter()
|
|
|
|
.filter_map(|r| r.ok())
|
2020-12-19 15:00:11 +00:00
|
|
|
.filter_map(|(key, _)| Self::parse_servercurrentpdus(key).ok())
|
2021-01-27 02:54:35 +00:00
|
|
|
.filter(|(_, pdu)| !pdu.is_empty()) // Skip reservation key
|
2020-11-03 20:20:35 +00:00
|
|
|
.take(50)
|
|
|
|
// This should not contain more than 50 anyway
|
2020-10-21 15:08:54 +01:00
|
|
|
{
|
2020-11-03 20:20:35 +00:00
|
|
|
current_transactions
|
2021-01-27 02:54:35 +00:00
|
|
|
.entry(outgoing_kind)
|
2020-11-03 20:20:35 +00:00
|
|
|
.or_insert_with(Vec::new)
|
|
|
|
.push(pdu);
|
2020-10-21 15:08:54 +01:00
|
|
|
}
|
|
|
|
|
2021-01-27 02:54:35 +00:00
|
|
|
for (outgoing_kind, pdus) in current_transactions {
|
2021-01-29 15:14:09 +00:00
|
|
|
futures.push(Self::handle_event(outgoing_kind, pdus, &db));
|
2020-10-21 15:08:54 +01:00
|
|
|
}
|
|
|
|
|
2021-01-27 02:54:35 +00:00
|
|
|
let mut last_failed_try: HashMap<OutgoingKind, (u32, Instant)> = HashMap::new();
|
2020-12-19 15:00:11 +00:00
|
|
|
|
2020-10-21 15:08:54 +01:00
|
|
|
let mut subscriber = servernamepduids.watch_prefix(b"");
|
2020-09-15 15:13:54 +01:00
|
|
|
loop {
|
|
|
|
select! {
|
2020-12-08 09:33:44 +00:00
|
|
|
Some(response) = futures.next() => {
|
|
|
|
match response {
|
2021-01-27 02:54:35 +00:00
|
|
|
Ok(outgoing_kind) => {
|
|
|
|
let mut prefix = match &outgoing_kind {
|
|
|
|
OutgoingKind::Appservice(server) => {
|
|
|
|
let mut p = b"+".to_vec();
|
|
|
|
p.extend_from_slice(server.as_bytes());
|
|
|
|
p
|
|
|
|
}
|
|
|
|
OutgoingKind::Push(id) => {
|
|
|
|
let mut p = b"$".to_vec();
|
|
|
|
p.extend_from_slice(&id);
|
|
|
|
p
|
|
|
|
},
|
|
|
|
OutgoingKind::Normal(server) => {
|
|
|
|
let mut p = vec![];
|
|
|
|
p.extend_from_slice(server.as_bytes());
|
|
|
|
p
|
|
|
|
},
|
2020-12-08 09:33:44 +00:00
|
|
|
};
|
2020-10-21 15:08:54 +01:00
|
|
|
prefix.push(0xff);
|
|
|
|
|
|
|
|
for key in servercurrentpdus
|
|
|
|
.scan_prefix(&prefix)
|
|
|
|
.keys()
|
|
|
|
.filter_map(|r| r.ok())
|
|
|
|
{
|
|
|
|
// Don't remove reservation yet
|
|
|
|
if prefix.len() != key.len() {
|
|
|
|
servercurrentpdus.remove(key).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find events that have been added since starting the last request
|
|
|
|
let new_pdus = servernamepduids
|
|
|
|
.scan_prefix(&prefix)
|
|
|
|
.keys()
|
|
|
|
.filter_map(|r| r.ok())
|
|
|
|
.map(|k| {
|
|
|
|
k.subslice(prefix.len(), k.len() - prefix.len())
|
2020-11-03 20:20:35 +00:00
|
|
|
})
|
|
|
|
.take(50)
|
|
|
|
.collect::<Vec<_>>();
|
2020-10-21 15:08:54 +01:00
|
|
|
|
|
|
|
if !new_pdus.is_empty() {
|
|
|
|
for pdu_id in &new_pdus {
|
|
|
|
let mut current_key = prefix.clone();
|
|
|
|
current_key.extend_from_slice(pdu_id);
|
|
|
|
servercurrentpdus.insert(¤t_key, &[]).unwrap();
|
|
|
|
servernamepduids.remove(¤t_key).unwrap();
|
|
|
|
}
|
|
|
|
|
2021-01-27 02:54:35 +00:00
|
|
|
futures.push(
|
|
|
|
Self::handle_event(
|
|
|
|
outgoing_kind.clone(),
|
|
|
|
new_pdus,
|
2021-01-29 15:14:09 +00:00
|
|
|
&db,
|
2021-01-27 02:54:35 +00:00
|
|
|
)
|
|
|
|
);
|
2020-10-21 15:08:54 +01:00
|
|
|
} else {
|
|
|
|
servercurrentpdus.remove(&prefix).unwrap();
|
2020-11-03 20:20:35 +00:00
|
|
|
// servercurrentpdus with the prefix should be empty now
|
2020-10-21 15:08:54 +01:00
|
|
|
}
|
2020-09-23 14:23:29 +01:00
|
|
|
}
|
2021-01-27 02:54:35 +00:00
|
|
|
Err((outgoing_kind, e)) => {
|
|
|
|
info!("Couldn't send transaction to {}\n{}", outgoing_kind, e);
|
|
|
|
let mut prefix = match &outgoing_kind {
|
|
|
|
OutgoingKind::Appservice(serv) => {
|
|
|
|
let mut p = b"+".to_vec();
|
|
|
|
p.extend_from_slice(serv.as_bytes());
|
|
|
|
p
|
|
|
|
},
|
|
|
|
OutgoingKind::Push(id) => {
|
|
|
|
let mut p = b"$".to_vec();
|
|
|
|
p.extend_from_slice(&id);
|
|
|
|
p
|
|
|
|
},
|
|
|
|
OutgoingKind::Normal(serv) => {
|
|
|
|
let mut p = vec![];
|
|
|
|
p.extend_from_slice(serv.as_bytes());
|
|
|
|
p
|
|
|
|
},
|
2020-12-19 15:00:11 +00:00
|
|
|
};
|
2021-01-27 02:54:35 +00:00
|
|
|
|
2020-12-19 15:00:11 +00:00
|
|
|
prefix.push(0xff);
|
2021-01-27 02:54:35 +00:00
|
|
|
|
|
|
|
last_failed_try.insert(outgoing_kind.clone(), match last_failed_try.get(&outgoing_kind) {
|
2020-12-19 15:00:11 +00:00
|
|
|
Some(last_failed) => {
|
|
|
|
(last_failed.0+1, Instant::now())
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
(1, Instant::now())
|
|
|
|
}
|
|
|
|
});
|
|
|
|
servercurrentpdus.remove(&prefix).unwrap();
|
2020-09-23 14:23:29 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
2020-09-15 15:13:54 +01:00
|
|
|
Some(event) = &mut subscriber => {
|
2020-09-23 14:23:29 +01:00
|
|
|
if let sled::Event::Insert { key, .. } = event {
|
2020-10-21 15:08:54 +01:00
|
|
|
let servernamepduid = key.clone();
|
|
|
|
let mut parts = servernamepduid.splitn(2, |&b| b == 0xff);
|
2020-09-15 15:13:54 +01:00
|
|
|
|
2021-01-15 16:05:57 +00:00
|
|
|
let exponential_backoff = |(tries, instant): &(u32, Instant)| {
|
|
|
|
// Fail if a request has failed recently (exponential backoff)
|
|
|
|
let mut min_elapsed_duration = Duration::from_secs(60) * (*tries) * (*tries);
|
|
|
|
if min_elapsed_duration > Duration::from_secs(60*60*24) {
|
|
|
|
min_elapsed_duration = Duration::from_secs(60*60*24);
|
|
|
|
}
|
|
|
|
|
|
|
|
instant.elapsed() < min_elapsed_duration
|
|
|
|
};
|
2021-01-27 02:54:35 +00:00
|
|
|
if let Some((outgoing_kind, pdu_id)) = utils::string_from_bytes(
|
2020-09-23 14:23:29 +01:00
|
|
|
parts
|
|
|
|
.next()
|
|
|
|
.expect("splitn will always return 1 or more elements"),
|
|
|
|
)
|
2021-01-27 02:54:35 +00:00
|
|
|
.map_err(|_| Error::bad_database("[Utf8] ServerName in servernamepduid bytes are invalid."))
|
|
|
|
.and_then(|ident_str| {
|
2020-12-08 09:33:44 +00:00
|
|
|
// Appservices start with a plus
|
2021-01-27 02:54:35 +00:00
|
|
|
Ok(if ident_str.starts_with('+') {
|
|
|
|
OutgoingKind::Appservice(
|
|
|
|
Box::<ServerName>::try_from(&ident_str[1..])
|
|
|
|
.map_err(|_| Error::bad_database("ServerName in servernamepduid is invalid."))?
|
|
|
|
)
|
|
|
|
} else if ident_str.starts_with('$') {
|
|
|
|
OutgoingKind::Push(ident_str[1..].as_bytes().to_vec())
|
2020-12-08 09:33:44 +00:00
|
|
|
} else {
|
2021-01-27 02:54:35 +00:00
|
|
|
OutgoingKind::Normal(
|
|
|
|
Box::<ServerName>::try_from(ident_str)
|
|
|
|
.map_err(|_| Error::bad_database("ServerName in servernamepduid is invalid."))?
|
|
|
|
)
|
|
|
|
})
|
2020-12-08 09:33:44 +00:00
|
|
|
})
|
2021-01-27 02:54:35 +00:00
|
|
|
.and_then(|outgoing_kind| parts
|
2020-10-21 15:08:54 +01:00
|
|
|
.next()
|
|
|
|
.ok_or_else(|| Error::bad_database("Invalid servernamepduid in db."))
|
2021-01-27 02:54:35 +00:00
|
|
|
.map(|pdu_id| (outgoing_kind, pdu_id))
|
2020-10-21 15:08:54 +01:00
|
|
|
)
|
2021-01-27 02:54:35 +00:00
|
|
|
.ok()
|
|
|
|
.filter(|(outgoing_kind, _)| {
|
|
|
|
if last_failed_try.get(outgoing_kind).map_or(false, exponential_backoff) {
|
2020-12-19 15:00:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-27 02:54:35 +00:00
|
|
|
let mut prefix = match outgoing_kind {
|
|
|
|
OutgoingKind::Appservice(serv) => {
|
|
|
|
let mut p = b"+".to_vec();
|
|
|
|
p.extend_from_slice(serv.as_bytes());
|
|
|
|
p
|
|
|
|
},
|
|
|
|
OutgoingKind::Push(id) => {
|
|
|
|
let mut p = b"$".to_vec();
|
|
|
|
p.extend_from_slice(&id);
|
|
|
|
p
|
|
|
|
},
|
|
|
|
OutgoingKind::Normal(serv) => {
|
|
|
|
let mut p = vec![];
|
|
|
|
p.extend_from_slice(serv.as_bytes());
|
|
|
|
p
|
|
|
|
},
|
2020-12-08 09:33:44 +00:00
|
|
|
};
|
2020-10-21 15:08:54 +01:00
|
|
|
prefix.push(0xff);
|
|
|
|
|
|
|
|
servercurrentpdus
|
|
|
|
.compare_and_swap(prefix, Option::<&[u8]>::None, Some(&[])) // Try to reserve
|
|
|
|
== Ok(Ok(()))
|
|
|
|
})
|
2020-09-23 14:23:29 +01:00
|
|
|
{
|
2020-10-21 15:08:54 +01:00
|
|
|
servercurrentpdus.insert(&key, &[]).unwrap();
|
|
|
|
servernamepduids.remove(&key).unwrap();
|
|
|
|
|
2021-01-27 02:54:35 +00:00
|
|
|
futures.push(
|
|
|
|
Self::handle_event(
|
|
|
|
outgoing_kind,
|
|
|
|
vec![pdu_id.into()],
|
2021-01-29 15:14:09 +00:00
|
|
|
&db,
|
2021-01-27 02:54:35 +00:00
|
|
|
)
|
|
|
|
);
|
2020-09-15 15:13:54 +01:00
|
|
|
}
|
2020-09-23 14:23:29 +01:00
|
|
|
}
|
|
|
|
}
|
2020-09-15 15:13:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-27 02:54:35 +00:00
|
|
|
pub fn send_push_pdu(&self, pdu_id: &[u8]) -> Result<()> {
|
|
|
|
// Make sure we don't cause utf8 errors when parsing to a String...
|
|
|
|
let pduid = String::from_utf8_lossy(pdu_id).as_bytes().to_vec();
|
|
|
|
|
|
|
|
// these are valid ServerName chars
|
|
|
|
// (byte.is_ascii_alphanumeric() || byte == b'-' || byte == b'.')
|
|
|
|
let mut key = b"$".to_vec();
|
|
|
|
// keep each pdu push unique
|
|
|
|
key.extend_from_slice(pduid.as_slice());
|
|
|
|
key.push(0xff);
|
|
|
|
key.extend_from_slice(pdu_id);
|
|
|
|
self.servernamepduids.insert(key, b"")?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-11-08 19:45:52 +00:00
|
|
|
pub fn send_pdu(&self, server: &ServerName, pdu_id: &[u8]) -> Result<()> {
|
2020-09-15 15:13:54 +01:00
|
|
|
let mut key = server.as_bytes().to_vec();
|
|
|
|
key.push(0xff);
|
|
|
|
key.extend_from_slice(pdu_id);
|
2020-10-21 15:08:54 +01:00
|
|
|
self.servernamepduids.insert(key, b"")?;
|
2020-09-15 15:13:54 +01:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-09-23 14:23:29 +01:00
|
|
|
|
2020-12-08 09:33:44 +00:00
|
|
|
pub fn send_pdu_appservice(&self, appservice_id: &str, pdu_id: &[u8]) -> Result<()> {
|
2021-01-05 14:21:41 +00:00
|
|
|
let mut key = b"+".to_vec();
|
2020-12-08 09:33:44 +00:00
|
|
|
key.extend_from_slice(appservice_id.as_bytes());
|
|
|
|
key.push(0xff);
|
|
|
|
key.extend_from_slice(pdu_id);
|
|
|
|
self.servernamepduids.insert(key, b"")?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:14:09 +00:00
|
|
|
// TODO this is the whole DB but is it better to clone smaller parts than the whole thing??
|
2020-09-23 14:23:29 +01:00
|
|
|
async fn handle_event(
|
2021-01-27 02:54:35 +00:00
|
|
|
kind: OutgoingKind,
|
2020-10-21 15:08:54 +01:00
|
|
|
pdu_ids: Vec<IVec>,
|
2021-01-29 15:14:09 +00:00
|
|
|
db: &Database,
|
2021-01-27 02:54:35 +00:00
|
|
|
) -> std::result::Result<OutgoingKind, (OutgoingKind, Error)> {
|
|
|
|
match kind {
|
|
|
|
OutgoingKind::Appservice(server) => {
|
|
|
|
let pdu_jsons = pdu_ids
|
|
|
|
.iter()
|
|
|
|
.map(|pdu_id| {
|
|
|
|
Ok::<_, (Box<ServerName>, Error)>(
|
2021-01-29 15:14:09 +00:00
|
|
|
db.rooms
|
2021-01-27 02:54:35 +00:00
|
|
|
.get_pdu_from_id(pdu_id)
|
|
|
|
.map_err(|e| (server.clone(), e))?
|
|
|
|
.ok_or_else(|| {
|
|
|
|
(
|
|
|
|
server.clone(),
|
|
|
|
Error::bad_database(
|
|
|
|
"[Appservice] Event in servernamepduids not found in ",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
})?
|
|
|
|
.to_any_event(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.filter_map(|r| r.ok())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
appservice_server::send_request(
|
2021-01-29 15:14:09 +00:00
|
|
|
&db.globals,
|
|
|
|
db.appservice
|
2021-01-27 02:54:35 +00:00
|
|
|
.get_registration(server.as_str())
|
|
|
|
.unwrap()
|
|
|
|
.unwrap(), // TODO: handle error
|
|
|
|
appservice::event::push_events::v1::Request {
|
|
|
|
events: &pdu_jsons,
|
|
|
|
txn_id: &utils::random_string(16),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.map(|_response| OutgoingKind::Appservice(server.clone()))
|
|
|
|
.map_err(|e| (OutgoingKind::Appservice(server.clone()), e))
|
|
|
|
}
|
|
|
|
OutgoingKind::Push(id) => {
|
|
|
|
let pdus = pdu_ids
|
|
|
|
.iter()
|
|
|
|
.map(|pdu_id| {
|
|
|
|
Ok::<_, (Vec<u8>, Error)>(
|
2021-01-29 15:14:09 +00:00
|
|
|
db.rooms
|
2021-01-27 02:54:35 +00:00
|
|
|
.get_pdu_from_id(pdu_id)
|
|
|
|
.map_err(|e| (id.clone(), e))?
|
|
|
|
.ok_or_else(|| {
|
|
|
|
(
|
|
|
|
id.clone(),
|
|
|
|
Error::bad_database(
|
|
|
|
"[Push] Event in servernamepduids not found in db.",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
})?,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.filter_map(|r| r.ok())
|
|
|
|
.collect::<Vec<_>>();
|
2021-01-29 15:14:09 +00:00
|
|
|
|
2021-01-27 02:54:35 +00:00
|
|
|
dbg!(&pdus);
|
2021-01-29 15:14:09 +00:00
|
|
|
|
2021-01-27 02:54:35 +00:00
|
|
|
for pdu in &pdus {
|
2021-01-29 15:14:09 +00:00
|
|
|
// Redacted events are not notification targets (we don't send push for them)
|
|
|
|
if pdu.unsigned.get("redacted_because").is_some() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for user in db.rooms.room_members(&pdu.room_id) {
|
2021-01-27 02:54:35 +00:00
|
|
|
dbg!(&user);
|
2021-01-29 15:14:09 +00:00
|
|
|
|
2021-01-27 02:54:35 +00:00
|
|
|
let user = user.map_err(|e| (OutgoingKind::Push(id.clone()), e))?;
|
2021-01-29 15:14:09 +00:00
|
|
|
let pushers = db
|
|
|
|
.pusher
|
2021-01-27 02:54:35 +00:00
|
|
|
.get_pusher(&user)
|
2021-01-29 15:14:09 +00:00
|
|
|
.map_err(|e| (OutgoingKind::Push(id.clone()), e))?;
|
|
|
|
|
|
|
|
let rules_for_user = db
|
|
|
|
.account_data
|
|
|
|
.get::<push_rules::PushRulesEvent>(None, &user, EventType::PushRules)
|
|
|
|
.map_err(|e| (OutgoingKind::Push(id.clone()), e))?
|
|
|
|
.map(|ev| ev.content.global)
|
|
|
|
.unwrap_or_else(|| crate::push_rules::default_pushrules(&user));
|
|
|
|
|
|
|
|
let unread: UInt = if let Some(last_read) = db
|
|
|
|
.rooms
|
|
|
|
.edus
|
|
|
|
.private_read_get(&pdu.room_id, &user)
|
2021-01-27 02:54:35 +00:00
|
|
|
.map_err(|e| (OutgoingKind::Push(id.clone()), e))?
|
|
|
|
{
|
2021-01-29 15:14:09 +00:00
|
|
|
(db.rooms
|
|
|
|
.pdus_since(&user, &pdu.room_id, last_read)
|
2021-01-27 02:54:35 +00:00
|
|
|
.map_err(|e| (OutgoingKind::Push(id.clone()), e))?
|
2021-01-29 15:14:09 +00:00
|
|
|
.filter_map(|pdu| pdu.ok()) // Filter out buggy events
|
|
|
|
.filter(|(_, pdu)| {
|
|
|
|
matches!(
|
|
|
|
pdu.kind.clone(),
|
|
|
|
EventType::RoomMessage | EventType::RoomEncrypted
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.count() as u32)
|
|
|
|
.into()
|
|
|
|
} else {
|
|
|
|
// Just return zero unread messages
|
|
|
|
uint!(0)
|
|
|
|
};
|
|
|
|
|
|
|
|
dbg!(&pushers);
|
|
|
|
|
|
|
|
// dbg!(&rules_for_user);
|
|
|
|
|
|
|
|
crate::database::pusher::send_push_notice(
|
|
|
|
&user,
|
|
|
|
unread,
|
|
|
|
&pushers,
|
|
|
|
rules_for_user,
|
|
|
|
pdu,
|
|
|
|
db,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.map_err(|e| (OutgoingKind::Push(id.clone()), e))?;
|
2021-01-27 02:54:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(OutgoingKind::Push(id))
|
|
|
|
}
|
|
|
|
OutgoingKind::Normal(server) => {
|
|
|
|
let pdu_jsons = pdu_ids
|
|
|
|
.iter()
|
|
|
|
.map(|pdu_id| {
|
|
|
|
Ok::<_, (OutgoingKind, Error)>(
|
|
|
|
// TODO: check room version and remove event_id if needed
|
|
|
|
serde_json::from_str(
|
|
|
|
PduEvent::convert_to_outgoing_federation_event(
|
2021-01-29 15:14:09 +00:00
|
|
|
db.rooms
|
2021-01-27 02:54:35 +00:00
|
|
|
.get_pdu_json_from_id(pdu_id)
|
|
|
|
.map_err(|e| (OutgoingKind::Normal(server.clone()), e))?
|
|
|
|
.ok_or_else(|| {
|
|
|
|
(
|
|
|
|
OutgoingKind::Normal(server.clone()),
|
|
|
|
Error::bad_database(
|
|
|
|
"[Normal] Event in servernamepduids not found in db.",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
})?,
|
|
|
|
)
|
|
|
|
.json()
|
|
|
|
.get(),
|
|
|
|
)
|
|
|
|
.expect("Raw<..> is always valid"),
|
2020-11-03 20:20:35 +00:00
|
|
|
)
|
2021-01-27 02:54:35 +00:00
|
|
|
})
|
|
|
|
.filter_map(|r| r.ok())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
server_server::send_request(
|
2021-01-29 15:14:09 +00:00
|
|
|
&db.globals,
|
2021-01-27 02:54:35 +00:00
|
|
|
&*server,
|
|
|
|
send_transaction_message::v1::Request {
|
2021-01-29 15:14:09 +00:00
|
|
|
origin: db.globals.server_name(),
|
2021-01-27 02:54:35 +00:00
|
|
|
pdus: &pdu_jsons,
|
|
|
|
edus: &[],
|
|
|
|
origin_server_ts: SystemTime::now(),
|
|
|
|
transaction_id: &utils::random_string(16),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.map(|_response| OutgoingKind::Normal(server.clone()))
|
|
|
|
.map_err(|e| (OutgoingKind::Normal(server.clone()), e))
|
|
|
|
}
|
2020-12-08 09:33:44 +00:00
|
|
|
}
|
2020-09-23 14:23:29 +01:00
|
|
|
}
|
2020-12-19 15:00:11 +00:00
|
|
|
|
2021-01-27 02:54:35 +00:00
|
|
|
fn parse_servercurrentpdus(key: IVec) -> Result<(OutgoingKind, IVec)> {
|
2020-12-19 15:00:11 +00:00
|
|
|
let mut parts = key.splitn(2, |&b| b == 0xff);
|
|
|
|
let server = parts.next().expect("splitn always returns one element");
|
|
|
|
let pdu = parts
|
|
|
|
.next()
|
|
|
|
.ok_or_else(|| Error::bad_database("Invalid bytes in servercurrentpdus."))?;
|
|
|
|
|
|
|
|
let server = utils::string_from_bytes(&server).map_err(|_| {
|
|
|
|
Error::bad_database("Invalid server bytes in server_currenttransaction")
|
|
|
|
})?;
|
|
|
|
|
|
|
|
// Appservices start with a plus
|
2021-01-27 02:54:35 +00:00
|
|
|
Ok::<_, Error>(if server.starts_with('+') {
|
|
|
|
(
|
|
|
|
OutgoingKind::Appservice(Box::<ServerName>::try_from(server).map_err(|_| {
|
|
|
|
Error::bad_database("Invalid server string in server_currenttransaction")
|
|
|
|
})?),
|
|
|
|
IVec::from(pdu),
|
|
|
|
)
|
|
|
|
} else if server.starts_with('$') {
|
|
|
|
(
|
|
|
|
OutgoingKind::Push(server.as_bytes().to_vec()),
|
|
|
|
IVec::from(pdu),
|
|
|
|
)
|
2020-12-19 15:00:11 +00:00
|
|
|
} else {
|
2021-01-27 02:54:35 +00:00
|
|
|
(
|
|
|
|
OutgoingKind::Normal(Box::<ServerName>::try_from(server).map_err(|_| {
|
|
|
|
Error::bad_database("Invalid server string in server_currenttransaction")
|
|
|
|
})?),
|
|
|
|
IVec::from(pdu),
|
|
|
|
)
|
|
|
|
})
|
2020-12-19 15:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn send_federation_request<T: OutgoingRequest>(
|
|
|
|
&self,
|
|
|
|
globals: &crate::database::globals::Globals,
|
2021-01-14 19:39:56 +00:00
|
|
|
destination: &ServerName,
|
2020-12-19 15:00:11 +00:00
|
|
|
request: T,
|
|
|
|
) -> Result<T::IncomingResponse>
|
|
|
|
where
|
|
|
|
T: Debug,
|
|
|
|
{
|
|
|
|
let permit = self.maximum_requests.acquire().await;
|
|
|
|
let response = server_server::send_request(globals, destination, request).await;
|
|
|
|
drop(permit);
|
|
|
|
|
|
|
|
response
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn send_appservice_request<T: OutgoingRequest>(
|
|
|
|
&self,
|
|
|
|
globals: &crate::database::globals::Globals,
|
|
|
|
registration: serde_yaml::Value,
|
|
|
|
request: T,
|
|
|
|
) -> Result<T::IncomingResponse>
|
|
|
|
where
|
|
|
|
T: Debug,
|
|
|
|
{
|
|
|
|
let permit = self.maximum_requests.acquire().await;
|
|
|
|
let response = appservice_server::send_request(globals, registration, request).await;
|
|
|
|
drop(permit);
|
|
|
|
|
|
|
|
response
|
|
|
|
}
|
2020-09-15 15:13:54 +01:00
|
|
|
}
|