2020-12-19 15:00:11 +00:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
2021-03-24 10:52:10 +00:00
|
|
|
convert::TryFrom,
|
2021-02-11 12:16:14 +00:00
|
|
|
fmt::Debug,
|
2020-12-19 15:00:11 +00:00
|
|
|
sync::Arc,
|
|
|
|
time::{Duration, Instant, SystemTime},
|
|
|
|
};
|
2020-09-15 15:13:54 +01:00
|
|
|
|
2021-03-15 08:48:19 +00:00
|
|
|
use crate::{
|
|
|
|
appservice_server, database::pusher, server_server, utils, Database, Error, PduEvent, Result,
|
|
|
|
};
|
2020-09-23 14:23:29 +01:00
|
|
|
use federation::transactions::send_transaction_message;
|
2021-03-26 10:10:45 +00:00
|
|
|
use log::warn;
|
2021-03-02 13:36:48 +00:00
|
|
|
use ring::digest;
|
2020-09-15 15:13:54 +01:00
|
|
|
use rocket::futures::stream::{FuturesUnordered, StreamExt};
|
2020-12-08 09:33:44 +00:00
|
|
|
use ruma::{
|
2021-03-24 10:52:10 +00:00
|
|
|
api::{appservice, federation, OutgoingRequest},
|
2021-01-27 02:54:35 +00:00
|
|
|
events::{push_rules, EventType},
|
2021-04-05 20:25:10 +01:00
|
|
|
push, uint, ServerName, UInt, UserId,
|
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>),
|
2021-03-22 13:04:11 +00:00
|
|
|
Push(Vec<u8>, Vec<u8>), // user and pushkey
|
2021-01-27 02:54:35 +00:00
|
|
|
Normal(Box<ServerName>),
|
|
|
|
}
|
|
|
|
|
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-03-22 13:04:11 +00:00
|
|
|
pub(super) servernamepduids: sled::Tree, // ServernamePduId = (+ / $)SenderKey / ServerName / UserId + PduId
|
2021-01-27 02:54:35 +00:00
|
|
|
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-03-15 08:48:19 +00:00
|
|
|
|
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
|
2021-04-09 20:38:16 +01:00
|
|
|
let mut current_transactions = HashMap::<OutgoingKind, Vec<Vec<u8>>>::new();
|
2020-10-21 15:08:54 +01:00
|
|
|
|
2021-03-15 08:48:19 +00:00
|
|
|
for (key, outgoing_kind, pdu) in servercurrentpdus
|
2020-10-21 15:08:54 +01:00
|
|
|
.iter()
|
|
|
|
.filter_map(|r| r.ok())
|
2021-03-15 08:48:19 +00:00
|
|
|
.filter_map(|(key, _)| {
|
|
|
|
Self::parse_servercurrentpdus(&key)
|
|
|
|
.ok()
|
2021-04-09 20:38:16 +01:00
|
|
|
.map(|(k, p)| (key, k, p.to_vec()))
|
2021-03-15 08:48:19 +00:00
|
|
|
})
|
2020-10-21 15:08:54 +01:00
|
|
|
{
|
2021-02-26 12:24:07 +00:00
|
|
|
if pdu.is_empty() {
|
|
|
|
// Remove old reservation key
|
|
|
|
servercurrentpdus.remove(key).unwrap();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let entry = current_transactions
|
2021-01-27 02:54:35 +00:00
|
|
|
.entry(outgoing_kind)
|
2021-02-26 12:24:07 +00:00
|
|
|
.or_insert_with(Vec::new);
|
|
|
|
|
|
|
|
if entry.len() > 30 {
|
|
|
|
warn!("Dropping some current pdus because too many were queued. This should not happen.");
|
|
|
|
servercurrentpdus.remove(key).unwrap();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry.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-02-26 12:24:07 +00:00
|
|
|
// Create new reservation
|
2021-03-15 08:48:19 +00:00
|
|
|
let mut prefix = match &outgoing_kind {
|
|
|
|
OutgoingKind::Appservice(server) => {
|
|
|
|
let mut p = b"+".to_vec();
|
|
|
|
p.extend_from_slice(server.as_bytes());
|
|
|
|
p
|
|
|
|
}
|
2021-03-22 13:04:11 +00:00
|
|
|
OutgoingKind::Push(user, pushkey) => {
|
2021-03-15 08:48:19 +00:00
|
|
|
let mut p = b"$".to_vec();
|
2021-03-22 13:04:11 +00:00
|
|
|
p.extend_from_slice(&user);
|
|
|
|
p.push(0xff);
|
|
|
|
p.extend_from_slice(&pushkey);
|
2021-03-15 08:48:19 +00:00
|
|
|
p
|
|
|
|
}
|
|
|
|
OutgoingKind::Normal(server) => {
|
|
|
|
let mut p = Vec::new();
|
|
|
|
p.extend_from_slice(server.as_bytes());
|
|
|
|
p
|
|
|
|
}
|
2021-02-26 12:24:07 +00:00
|
|
|
};
|
|
|
|
prefix.push(0xff);
|
|
|
|
servercurrentpdus.insert(prefix, &[]).unwrap();
|
|
|
|
|
2021-03-15 08:48:19 +00:00
|
|
|
futures.push(Self::handle_event(outgoing_kind.clone(), 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
|
|
|
|
}
|
2021-03-22 13:04:11 +00:00
|
|
|
OutgoingKind::Push(user, pushkey) => {
|
2021-01-27 02:54:35 +00:00
|
|
|
let mut p = b"$".to_vec();
|
2021-03-22 13:04:11 +00:00
|
|
|
p.extend_from_slice(&user);
|
|
|
|
p.push(0xff);
|
|
|
|
p.extend_from_slice(&pushkey);
|
2021-01-27 02:54:35 +00:00
|
|
|
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| {
|
2021-04-09 20:38:16 +01:00
|
|
|
k[prefix.len()..].to_vec()
|
2020-11-03 20:20:35 +00:00
|
|
|
})
|
2021-02-26 12:24:07 +00:00
|
|
|
.take(30)
|
2020-11-03 20:20:35 +00:00
|
|
|
.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-03-25 22:55:40 +00:00
|
|
|
Err((outgoing_kind, _)) => {
|
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
|
|
|
|
},
|
2021-03-22 13:04:11 +00:00
|
|
|
OutgoingKind::Push(user, pushkey) => {
|
2021-01-27 02:54:35 +00:00
|
|
|
let mut p = b"$".to_vec();
|
2021-03-22 13:04:11 +00:00
|
|
|
p.extend_from_slice(&user);
|
|
|
|
p.push(0xff);
|
|
|
|
p.extend_from_slice(&pushkey);
|
2021-01-27 02:54:35 +00:00
|
|
|
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 => {
|
2021-04-09 20:38:16 +01:00
|
|
|
for (_tree, key, value_opt) in &event {
|
|
|
|
if value_opt.is_none() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-10-21 15:08:54 +01:00
|
|
|
let servernamepduid = key.clone();
|
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)
|
2021-03-25 22:55:40 +00:00
|
|
|
let mut min_elapsed_duration = Duration::from_secs(30) * (*tries) * (*tries);
|
2021-01-15 16:05:57 +00:00
|
|
|
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-03-22 13:04:11 +00:00
|
|
|
|
|
|
|
if let Some((outgoing_kind, pdu_id)) = Self::parse_servercurrentpdus(&servernamepduid)
|
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
|
|
|
|
},
|
2021-03-22 13:04:11 +00:00
|
|
|
OutgoingKind::Push(user, pushkey) => {
|
2021-01-27 02:54:35 +00:00
|
|
|
let mut p = b"$".to_vec();
|
2021-03-22 13:04:11 +00:00
|
|
|
p.extend_from_slice(&user);
|
|
|
|
p.push(0xff);
|
|
|
|
p.extend_from_slice(&pushkey);
|
2021-01-27 02:54:35 +00:00
|
|
|
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-03-25 22:55:40 +00:00
|
|
|
last_failed_try.remove(&outgoing_kind);
|
|
|
|
|
2021-01-27 02:54:35 +00:00
|
|
|
futures.push(
|
|
|
|
Self::handle_event(
|
|
|
|
outgoing_kind,
|
2021-04-09 20:38:16 +01:00
|
|
|
vec![pdu_id.to_vec()],
|
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-03-15 08:48:19 +00:00
|
|
|
#[tracing::instrument(skip(self))]
|
2021-03-22 13:04:11 +00:00
|
|
|
pub fn send_push_pdu(&self, pdu_id: &[u8], senderkey: IVec) -> Result<()> {
|
2021-01-27 02:54:35 +00:00
|
|
|
let mut key = b"$".to_vec();
|
2021-03-22 13:04:11 +00:00
|
|
|
key.extend_from_slice(&senderkey);
|
2021-01-27 02:54:35 +00:00
|
|
|
key.push(0xff);
|
|
|
|
key.extend_from_slice(pdu_id);
|
|
|
|
self.servernamepduids.insert(key, b"")?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(self))]
|
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
|
|
|
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(self))]
|
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-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument]
|
2021-04-09 20:38:16 +01:00
|
|
|
fn calculate_hash(keys: &[Vec<u8>]) -> Vec<u8> {
|
2021-03-02 13:36:48 +00:00
|
|
|
// We only hash the pdu's event ids, not the whole pdu
|
|
|
|
let bytes = keys.join(&0xff);
|
|
|
|
let hash = digest::digest(&digest::SHA256, &bytes);
|
|
|
|
hash.as_ref().to_owned()
|
|
|
|
}
|
|
|
|
|
2021-03-15 08:48:19 +00:00
|
|
|
#[tracing::instrument(skip(db))]
|
2020-09-23 14:23:29 +01:00
|
|
|
async fn handle_event(
|
2021-01-27 02:54:35 +00:00
|
|
|
kind: OutgoingKind,
|
2021-04-09 20:38:16 +01:00
|
|
|
pdu_ids: Vec<Vec<u8>>,
|
2021-01-29 15:14:09 +00:00
|
|
|
db: &Database,
|
2021-01-27 02:54:35 +00:00
|
|
|
) -> std::result::Result<OutgoingKind, (OutgoingKind, Error)> {
|
2021-03-16 17:00:26 +00:00
|
|
|
match &kind {
|
2021-01-27 02:54:35 +00:00
|
|
|
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<_>>();
|
2021-03-15 08:48:19 +00:00
|
|
|
let permit = db.sending.maximum_requests.acquire().await;
|
2021-03-22 13:04:11 +00:00
|
|
|
|
2021-03-15 08:48:19 +00:00
|
|
|
let response = 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,
|
2021-03-15 08:48:19 +00:00
|
|
|
txn_id: &base64::encode_config(
|
|
|
|
Self::calculate_hash(&pdu_ids),
|
|
|
|
base64::URL_SAFE_NO_PAD,
|
|
|
|
),
|
2021-01-27 02:54:35 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await
|
2021-03-15 08:48:19 +00:00
|
|
|
.map(|_response| kind.clone())
|
|
|
|
.map_err(|e| (kind, e));
|
2021-02-26 12:24:07 +00:00
|
|
|
|
2021-03-15 08:48:19 +00:00
|
|
|
drop(permit);
|
2021-02-26 12:24:07 +00:00
|
|
|
|
2021-03-15 08:48:19 +00:00
|
|
|
response
|
2021-01-27 02:54:35 +00:00
|
|
|
}
|
2021-03-22 13:04:11 +00:00
|
|
|
OutgoingKind::Push(user, pushkey) => {
|
2021-01-27 02:54:35 +00:00
|
|
|
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)
|
2021-03-22 13:04:11 +00:00
|
|
|
.map_err(|e| (pushkey.clone(), e))?
|
2021-01-27 02:54:35 +00:00
|
|
|
.ok_or_else(|| {
|
|
|
|
(
|
2021-03-22 13:04:11 +00:00
|
|
|
pushkey.clone(),
|
2021-01-27 02:54:35 +00:00
|
|
|
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-03-16 17:00:26 +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;
|
|
|
|
}
|
|
|
|
|
2021-03-24 10:52:10 +00:00
|
|
|
let userid =
|
|
|
|
UserId::try_from(utils::string_from_bytes(user).map_err(|_| {
|
|
|
|
(
|
|
|
|
OutgoingKind::Push(user.clone(), pushkey.clone()),
|
|
|
|
Error::bad_database("Invalid push user string in db."),
|
|
|
|
)
|
|
|
|
})?)
|
|
|
|
.map_err(|_| {
|
|
|
|
(
|
|
|
|
OutgoingKind::Push(user.clone(), pushkey.clone()),
|
|
|
|
Error::bad_database("Invalid push user id in db."),
|
|
|
|
)
|
|
|
|
})?;
|
2021-03-22 13:04:11 +00:00
|
|
|
|
|
|
|
let mut senderkey = user.clone();
|
|
|
|
senderkey.push(0xff);
|
|
|
|
senderkey.extend_from_slice(pushkey);
|
|
|
|
|
|
|
|
let pusher = match db
|
|
|
|
.pusher
|
|
|
|
.get_pusher(&senderkey)
|
|
|
|
.map_err(|e| (OutgoingKind::Push(user.clone(), pushkey.clone()), e))?
|
|
|
|
{
|
|
|
|
Some(pusher) => pusher,
|
|
|
|
None => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
let rules_for_user = db
|
|
|
|
.account_data
|
|
|
|
.get::<push_rules::PushRulesEvent>(None, &userid, EventType::PushRules)
|
|
|
|
.map_err(|e| (OutgoingKind::Push(user.clone(), pushkey.clone()), e))?
|
|
|
|
.map(|ev| ev.content.global)
|
2021-04-05 20:25:10 +01:00
|
|
|
.unwrap_or_else(|| push::Ruleset::server_default(&userid));
|
2021-03-22 13:04:11 +00:00
|
|
|
|
|
|
|
let unread: UInt = if let Some(last_read) = db
|
|
|
|
.rooms
|
|
|
|
.edus
|
|
|
|
.private_read_get(&pdu.room_id, &userid)
|
|
|
|
.map_err(|e| (OutgoingKind::Push(user.clone(), pushkey.clone()), e))?
|
|
|
|
{
|
|
|
|
(db.rooms
|
|
|
|
.pdus_since(&userid, &pdu.room_id, last_read)
|
|
|
|
.map_err(|e| (OutgoingKind::Push(user.clone(), pushkey.clone()), e))?
|
|
|
|
.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)
|
|
|
|
};
|
|
|
|
|
|
|
|
let permit = db.sending.maximum_requests.acquire().await;
|
|
|
|
|
|
|
|
let _response = pusher::send_push_notice(
|
|
|
|
&userid,
|
|
|
|
unread,
|
|
|
|
&pusher,
|
|
|
|
rules_for_user,
|
|
|
|
&pdu,
|
|
|
|
db,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.map(|_response| kind.clone())
|
|
|
|
.map_err(|e| (kind.clone(), e));
|
|
|
|
|
|
|
|
drop(permit);
|
2021-01-27 02:54:35 +00:00
|
|
|
}
|
2021-03-22 13:04:11 +00:00
|
|
|
Ok(OutgoingKind::Push(user.clone(), pushkey.clone()))
|
2021-01-27 02:54:35 +00:00
|
|
|
}
|
|
|
|
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<_>>();
|
|
|
|
|
2021-03-15 08:48:19 +00:00
|
|
|
let permit = db.sending.maximum_requests.acquire().await;
|
|
|
|
|
|
|
|
let response = 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(),
|
2021-03-15 08:48:19 +00:00
|
|
|
transaction_id: &base64::encode_config(
|
|
|
|
Self::calculate_hash(&pdu_ids),
|
|
|
|
base64::URL_SAFE_NO_PAD,
|
|
|
|
),
|
2021-01-27 02:54:35 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await
|
2021-03-15 08:48:19 +00:00
|
|
|
.map(|response| {
|
2021-03-26 10:10:45 +00:00
|
|
|
for pdu in response.pdus {
|
|
|
|
if pdu.1.is_err() {
|
|
|
|
warn!("Failed to send to {}: {:?}", server, pdu);
|
|
|
|
}
|
|
|
|
}
|
2021-03-15 08:48:19 +00:00
|
|
|
kind.clone()
|
2020-12-08 09:33:44 +00:00
|
|
|
})
|
2021-03-15 08:48:19 +00:00
|
|
|
.map_err(|e| (kind, e));
|
2021-02-26 12:24:07 +00:00
|
|
|
|
2021-03-15 08:48:19 +00:00
|
|
|
drop(permit);
|
2021-02-26 12:24:07 +00:00
|
|
|
|
2021-03-15 08:48:19 +00:00
|
|
|
response
|
2021-01-27 02:54:35 +00:00
|
|
|
}
|
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-03-15 08:48:19 +00:00
|
|
|
fn parse_servercurrentpdus(key: &IVec) -> Result<(OutgoingKind, IVec)> {
|
2021-03-22 13:04:11 +00:00
|
|
|
// Appservices start with a plus
|
|
|
|
Ok::<_, Error>(if key.starts_with(b"+") {
|
|
|
|
let mut parts = key[1..].splitn(2, |&b| b == 0xff);
|
2020-12-19 15:00:11 +00:00
|
|
|
|
2021-03-22 13:04:11 +00:00
|
|
|
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")
|
|
|
|
})?;
|
2020-12-19 15:00:11 +00:00
|
|
|
|
2021-01-27 02:54:35 +00:00
|
|
|
(
|
|
|
|
OutgoingKind::Appservice(Box::<ServerName>::try_from(server).map_err(|_| {
|
|
|
|
Error::bad_database("Invalid server string in server_currenttransaction")
|
|
|
|
})?),
|
|
|
|
IVec::from(pdu),
|
|
|
|
)
|
2021-03-22 13:04:11 +00:00
|
|
|
} else if key.starts_with(b"$") {
|
|
|
|
let mut parts = key[1..].splitn(3, |&b| b == 0xff);
|
|
|
|
|
|
|
|
let user = parts.next().expect("splitn always returns one element");
|
|
|
|
let pushkey = parts
|
|
|
|
.next()
|
|
|
|
.ok_or_else(|| Error::bad_database("Invalid bytes in servercurrentpdus."))?;
|
|
|
|
let pdu = parts
|
|
|
|
.next()
|
|
|
|
.ok_or_else(|| Error::bad_database("Invalid bytes in servercurrentpdus."))?;
|
2021-01-27 02:54:35 +00:00
|
|
|
(
|
2021-03-22 13:04:11 +00:00
|
|
|
OutgoingKind::Push(user.to_vec(), pushkey.to_vec()),
|
2021-01-27 02:54:35 +00:00
|
|
|
IVec::from(pdu),
|
|
|
|
)
|
2020-12-19 15:00:11 +00:00
|
|
|
} else {
|
2021-03-22 13:04: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")
|
|
|
|
})?;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(self, globals))]
|
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
|
|
|
|
}
|
|
|
|
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(self, globals))]
|
2020-12-19 15:00:11 +00:00
|
|
|
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
|
|
|
}
|