feat: send to-device events over federation

This commit is contained in:
Timo Kösters 2021-07-20 21:17:15 +02:00
parent e20f55970e
commit fe3b5d32a7
No known key found for this signature in database
GPG key ID: 24DA7517711A2BA4
3 changed files with 69 additions and 13 deletions

View file

@ -164,9 +164,10 @@ impl Sending {
// Find events that have been added since starting the last request
let new_events = guard.sending.servernamepduids
.scan_prefix(prefix.clone())
.map(|(k, _)| {
SendingEventType::Pdu(k[prefix.len()..].to_vec())
.filter_map(|(k, _)| {
Self::parse_servercurrentevent(&k).ok()
})
.map(|(_, event)| event)
.take(30)
.collect::<Vec<_>>();
@ -290,7 +291,14 @@ impl Sending {
if let OutgoingKind::Normal(server_name) = outgoing_kind {
if let Ok((select_edus, last_count)) = Self::select_edus(db, server_name) {
events.extend_from_slice(&select_edus);
for edu in &select_edus {
let mut full_key = vec![b'*'];
full_key.extend_from_slice(&edu);
db.sending.servercurrentevents.insert(&full_key, &[])?;
}
events.extend(select_edus.into_iter().map(SendingEventType::Edu));
db.sending
.servername_educount
.insert(server_name.as_bytes(), &last_count.to_be_bytes())?;
@ -301,7 +309,7 @@ impl Sending {
Ok(Some(events))
}
pub fn select_edus(db: &Database, server: &ServerName) -> Result<(Vec<SendingEventType>, u64)> {
pub fn select_edus(db: &Database, server: &ServerName) -> Result<(Vec<Vec<u8>>, u64)> {
// u64: count of last edu
let since = db
.sending
@ -366,9 +374,7 @@ impl Sending {
}
};
events.push(SendingEventType::Edu(
serde_json::to_vec(&federation_event).expect("json can be serialized"),
));
events.push(serde_json::to_vec(&federation_event).expect("json can be serialized"));
if events.len() >= 20 {
break 'outer;
@ -402,6 +408,18 @@ impl Sending {
Ok(())
}
#[tracing::instrument(skip(self))]
pub fn send_reliable_edu(&self, server: &ServerName, serialized: &[u8]) -> Result<()> {
let mut key = server.as_bytes().to_vec();
key.push(0xff);
key.push(b'*');
key.extend_from_slice(serialized);
self.servernamepduids.insert(&key, b"")?;
self.sender.unbounded_send(key).unwrap();
Ok(())
}
#[tracing::instrument(skip(self))]
pub fn send_pdu_appservice(&self, appservice_id: &str, pdu_id: &[u8]) -> Result<()> {
let mut key = b"+".to_vec();