2021-01-29 16:14:09 +01:00
|
|
|
use crate::{Database, Error, PduEvent, Result};
|
2021-04-23 18:45:06 +02:00
|
|
|
use bytes::BytesMut;
|
2021-01-27 03:54:35 +01:00
|
|
|
use ruma::{
|
2021-01-29 16:14:09 +01:00
|
|
|
api::{
|
2022-02-18 15:33:14 +01:00
|
|
|
client::push::{get_pushers, set_pusher, PusherKind},
|
2021-01-29 16:14:09 +01:00
|
|
|
push_gateway::send_event_notification::{
|
|
|
|
self,
|
|
|
|
v1::{Device, Notification, NotificationCounts, NotificationPriority},
|
2021-01-27 03:54:35 +01:00
|
|
|
},
|
2022-02-12 21:04:38 +01:00
|
|
|
IncomingResponse, MatrixVersion, OutgoingRequest, SendAccessToken,
|
2021-01-27 03:54:35 +01:00
|
|
|
},
|
2021-10-13 10:16:45 +02:00
|
|
|
events::{
|
|
|
|
room::{name::RoomNameEventContent, power_levels::RoomPowerLevelsEventContent},
|
|
|
|
AnySyncRoomEvent, EventType,
|
|
|
|
},
|
2021-04-05 21:25:10 +02:00
|
|
|
push::{Action, PushConditionRoomCtx, PushFormat, Ruleset, Tweak},
|
2021-07-30 12:11:06 +02:00
|
|
|
serde::Raw,
|
|
|
|
uint, RoomId, UInt, UserId,
|
2021-01-27 03:54:35 +01:00
|
|
|
};
|
2021-07-29 08:36:01 +02:00
|
|
|
use tracing::{error, info, warn};
|
2021-01-27 03:54:35 +01:00
|
|
|
|
2022-02-03 13:24:04 +01:00
|
|
|
use std::{fmt::Debug, mem, sync::Arc};
|
2021-06-08 18:10:00 +02:00
|
|
|
|
|
|
|
use super::abstraction::Tree;
|
2021-01-29 16:14:09 +01:00
|
|
|
|
2021-01-27 03:54:35 +01:00
|
|
|
pub struct PushData {
|
|
|
|
/// UserId + pushkey -> Pusher
|
2021-06-08 18:10:00 +02:00
|
|
|
pub(super) senderkey_pusher: Arc<dyn Tree>,
|
2021-01-27 03:54:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PushData {
|
2021-07-29 08:36:01 +02:00
|
|
|
#[tracing::instrument(skip(self, sender, pusher))]
|
2022-02-18 15:33:14 +01:00
|
|
|
pub fn set_pusher(&self, sender: &UserId, pusher: set_pusher::v3::Pusher) -> Result<()> {
|
2021-01-27 03:54:35 +01:00
|
|
|
let mut key = sender.as_bytes().to_vec();
|
2021-02-11 13:16:14 +01:00
|
|
|
key.push(0xff);
|
2021-01-27 03:54:35 +01:00
|
|
|
key.extend_from_slice(pusher.pushkey.as_bytes());
|
|
|
|
|
2021-01-29 16:14:09 +01:00
|
|
|
// There are 2 kinds of pushers but the spec says: null deletes the pusher.
|
|
|
|
if pusher.kind.is_none() {
|
|
|
|
return self
|
|
|
|
.senderkey_pusher
|
2021-06-08 18:10:00 +02:00
|
|
|
.remove(&key)
|
2021-01-29 16:14:09 +01:00
|
|
|
.map(|_| ())
|
|
|
|
.map_err(Into::into);
|
|
|
|
}
|
|
|
|
|
2021-01-27 03:54:35 +01:00
|
|
|
self.senderkey_pusher.insert(
|
2021-06-08 18:10:00 +02:00
|
|
|
&key,
|
|
|
|
&serde_json::to_vec(&pusher).expect("Pusher is valid JSON value"),
|
2021-01-27 03:54:35 +01:00
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-29 08:36:01 +02:00
|
|
|
#[tracing::instrument(skip(self, senderkey))]
|
2022-02-18 15:33:14 +01:00
|
|
|
pub fn get_pusher(&self, senderkey: &[u8]) -> Result<Option<get_pushers::v3::Pusher>> {
|
2021-03-22 14:04:11 +01:00
|
|
|
self.senderkey_pusher
|
|
|
|
.get(senderkey)?
|
|
|
|
.map(|push| {
|
2021-06-17 20:34:14 +02:00
|
|
|
serde_json::from_slice(&*push)
|
|
|
|
.map_err(|_| Error::bad_database("Invalid Pusher in db."))
|
2021-03-22 14:04:11 +01:00
|
|
|
})
|
|
|
|
.transpose()
|
|
|
|
}
|
|
|
|
|
2021-07-29 08:36:01 +02:00
|
|
|
#[tracing::instrument(skip(self, sender))]
|
2022-02-18 15:33:14 +01:00
|
|
|
pub fn get_pushers(&self, sender: &UserId) -> Result<Vec<get_pushers::v3::Pusher>> {
|
2021-02-11 13:16:14 +01:00
|
|
|
let mut prefix = sender.as_bytes().to_vec();
|
|
|
|
prefix.push(0xff);
|
|
|
|
|
2021-01-27 03:54:35 +01:00
|
|
|
self.senderkey_pusher
|
2021-03-16 18:00:26 +01:00
|
|
|
.scan_prefix(prefix)
|
2021-06-08 18:10:00 +02:00
|
|
|
.map(|(_, push)| {
|
2021-06-17 20:34:14 +02:00
|
|
|
serde_json::from_slice(&*push)
|
|
|
|
.map_err(|_| Error::bad_database("Invalid Pusher in db."))
|
2021-01-27 03:54:35 +01:00
|
|
|
})
|
2021-01-29 16:14:09 +01:00
|
|
|
.collect()
|
|
|
|
}
|
2021-03-22 14:04:11 +01:00
|
|
|
|
2021-07-29 08:36:01 +02:00
|
|
|
#[tracing::instrument(skip(self, sender))]
|
2021-06-08 18:10:00 +02:00
|
|
|
pub fn get_pusher_senderkeys<'a>(
|
|
|
|
&'a self,
|
|
|
|
sender: &UserId,
|
2021-07-14 09:07:08 +02:00
|
|
|
) -> impl Iterator<Item = Vec<u8>> + 'a {
|
2021-03-22 14:04:11 +01:00
|
|
|
let mut prefix = sender.as_bytes().to_vec();
|
|
|
|
prefix.push(0xff);
|
|
|
|
|
2021-06-08 18:10:00 +02:00
|
|
|
self.senderkey_pusher.scan_prefix(prefix).map(|(k, _)| k)
|
2021-03-22 14:04:11 +01:00
|
|
|
}
|
2021-01-29 16:14:09 +01:00
|
|
|
}
|
|
|
|
|
2021-07-29 08:36:01 +02:00
|
|
|
#[tracing::instrument(skip(globals, destination, request))]
|
2021-01-29 16:14:09 +01:00
|
|
|
pub async fn send_request<T: OutgoingRequest>(
|
|
|
|
globals: &crate::database::globals::Globals,
|
|
|
|
destination: &str,
|
|
|
|
request: T,
|
|
|
|
) -> Result<T::IncomingResponse>
|
|
|
|
where
|
|
|
|
T: Debug,
|
|
|
|
{
|
|
|
|
let destination = destination.replace("/_matrix/push/v1/notify", "");
|
|
|
|
|
|
|
|
let http_request = request
|
2022-02-12 21:04:38 +01:00
|
|
|
.try_into_http_request::<BytesMut>(
|
|
|
|
&destination,
|
|
|
|
SendAccessToken::IfRequired(""),
|
|
|
|
&[MatrixVersion::V1_0],
|
|
|
|
)
|
2021-01-29 16:14:09 +01:00
|
|
|
.map_err(|e| {
|
|
|
|
warn!("Failed to find destination {}: {}", destination, e);
|
|
|
|
Error::BadServerResponse("Invalid destination")
|
2021-04-23 18:45:06 +02:00
|
|
|
})?
|
|
|
|
.map(|body| body.freeze());
|
2021-01-29 16:14:09 +01:00
|
|
|
|
2021-02-11 13:16:14 +01:00
|
|
|
let reqwest_request = reqwest::Request::try_from(http_request)
|
2021-01-29 16:14:09 +01:00
|
|
|
.expect("all http requests are valid reqwest requests");
|
|
|
|
|
|
|
|
// TODO: we could keep this very short and let expo backoff do it's thing...
|
2021-02-11 13:16:14 +01:00
|
|
|
//*reqwest_request.timeout_mut() = Some(Duration::from_secs(5));
|
2021-01-29 16:14:09 +01:00
|
|
|
|
|
|
|
let url = reqwest_request.url().clone();
|
2022-01-28 19:42:47 +01:00
|
|
|
let response = globals.default_client().execute(reqwest_request).await;
|
2021-04-29 20:58:05 +02:00
|
|
|
|
|
|
|
match response {
|
|
|
|
Ok(mut response) => {
|
|
|
|
// reqwest::Response -> http::Response conversion
|
|
|
|
let status = response.status();
|
|
|
|
let mut http_response_builder = http::Response::builder()
|
|
|
|
.status(status)
|
|
|
|
.version(response.version());
|
|
|
|
mem::swap(
|
|
|
|
response.headers_mut(),
|
|
|
|
http_response_builder
|
|
|
|
.headers_mut()
|
|
|
|
.expect("http::response::Builder is usable"),
|
|
|
|
);
|
2021-01-29 16:14:09 +01:00
|
|
|
|
2021-04-29 20:58:05 +02:00
|
|
|
let body = response.bytes().await.unwrap_or_else(|e| {
|
2021-04-13 15:00:45 +02:00
|
|
|
warn!("server error {}", e);
|
|
|
|
Vec::new().into()
|
|
|
|
}); // TODO: handle timeout
|
2021-01-29 16:14:09 +01:00
|
|
|
|
|
|
|
if status != 200 {
|
|
|
|
info!(
|
|
|
|
"Push gateway returned bad response {} {}\n{}\n{:?}",
|
|
|
|
destination,
|
|
|
|
status,
|
|
|
|
url,
|
|
|
|
crate::utils::string_from_bytes(&body)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-13 15:00:45 +02:00
|
|
|
let response = T::IncomingResponse::try_from_http_response(
|
2021-04-29 20:58:05 +02:00
|
|
|
http_response_builder
|
2021-01-29 16:14:09 +01:00
|
|
|
.body(body)
|
|
|
|
.expect("reqwest body is valid http body"),
|
|
|
|
);
|
|
|
|
response.map_err(|_| {
|
|
|
|
info!(
|
|
|
|
"Push gateway returned invalid response bytes {}\n{}",
|
|
|
|
destination, url
|
|
|
|
);
|
|
|
|
Error::BadServerResponse("Push gateway returned bad response.")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
Err(e) => Err(e.into()),
|
2021-01-27 03:54:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 08:36:01 +02:00
|
|
|
#[tracing::instrument(skip(user, unread, pusher, ruleset, pdu, db))]
|
2021-01-27 03:54:35 +01:00
|
|
|
pub async fn send_push_notice(
|
|
|
|
user: &UserId,
|
2021-01-29 16:14:09 +01:00
|
|
|
unread: UInt,
|
2022-02-18 15:33:14 +01:00
|
|
|
pusher: &get_pushers::v3::Pusher,
|
2021-01-27 03:54:35 +01:00
|
|
|
ruleset: Ruleset,
|
|
|
|
pdu: &PduEvent,
|
2021-01-29 16:14:09 +01:00
|
|
|
db: &Database,
|
2021-01-27 03:54:35 +01:00
|
|
|
) -> Result<()> {
|
2021-04-05 21:25:10 +02:00
|
|
|
let mut notify = None;
|
|
|
|
let mut tweaks = Vec::new();
|
|
|
|
|
2021-10-13 10:16:45 +02:00
|
|
|
let power_levels: RoomPowerLevelsEventContent = db
|
2021-07-30 12:11:06 +02:00
|
|
|
.rooms
|
|
|
|
.room_state_get(&pdu.room_id, &EventType::RoomPowerLevels, "")?
|
|
|
|
.map(|ev| {
|
2021-10-13 10:16:45 +02:00
|
|
|
serde_json::from_str(ev.content.get())
|
2021-07-30 12:11:06 +02:00
|
|
|
.map_err(|_| Error::bad_database("invalid m.room.power_levels event"))
|
|
|
|
})
|
|
|
|
.transpose()?
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
|
|
|
for action in get_actions(
|
|
|
|
user,
|
|
|
|
&ruleset,
|
|
|
|
&power_levels,
|
|
|
|
&pdu.to_sync_room_event(),
|
|
|
|
&pdu.room_id,
|
|
|
|
db,
|
|
|
|
)? {
|
2021-04-05 21:25:10 +02:00
|
|
|
let n = match action {
|
|
|
|
Action::DontNotify => false,
|
|
|
|
// TODO: Implement proper support for coalesce
|
|
|
|
Action::Notify | Action::Coalesce => true,
|
|
|
|
Action::SetTweak(tweak) => {
|
|
|
|
tweaks.push(tweak.clone());
|
|
|
|
continue;
|
2021-01-29 16:14:09 +01:00
|
|
|
}
|
2021-04-05 21:25:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if notify.is_some() {
|
|
|
|
return Err(Error::bad_database(
|
|
|
|
r#"Malformed pushrule contains more than one of these actions: ["dont_notify", "notify", "coalesce"]"#,
|
|
|
|
));
|
2021-01-27 03:54:35 +01:00
|
|
|
}
|
2021-04-05 21:25:10 +02:00
|
|
|
|
|
|
|
notify = Some(n);
|
|
|
|
}
|
|
|
|
|
2021-04-11 21:01:27 +02:00
|
|
|
if notify == Some(true) {
|
2021-04-05 21:25:10 +02:00
|
|
|
send_notice(unread, pusher, tweaks, pdu, db).await?;
|
2021-01-27 03:54:35 +01:00
|
|
|
}
|
2021-04-11 10:50:30 +02:00
|
|
|
// Else the event triggered no actions
|
2021-04-11 21:01:27 +02:00
|
|
|
|
2021-01-27 03:54:35 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-29 08:36:01 +02:00
|
|
|
#[tracing::instrument(skip(user, ruleset, pdu, db))]
|
2021-04-12 12:40:16 +02:00
|
|
|
pub fn get_actions<'a>(
|
|
|
|
user: &UserId,
|
|
|
|
ruleset: &'a Ruleset,
|
2021-10-13 10:16:45 +02:00
|
|
|
power_levels: &RoomPowerLevelsEventContent,
|
2021-07-30 12:11:06 +02:00
|
|
|
pdu: &Raw<AnySyncRoomEvent>,
|
|
|
|
room_id: &RoomId,
|
2021-04-12 12:40:16 +02:00
|
|
|
db: &Database,
|
2021-04-22 11:26:20 +02:00
|
|
|
) -> Result<&'a [Action]> {
|
2021-04-12 12:40:16 +02:00
|
|
|
let ctx = PushConditionRoomCtx {
|
2021-11-26 20:36:40 +01:00
|
|
|
room_id: room_id.to_owned(),
|
2021-04-14 12:55:14 +02:00
|
|
|
member_count: 10_u32.into(), // TODO: get member count efficiently
|
2021-04-12 12:40:16 +02:00
|
|
|
user_display_name: db
|
|
|
|
.users
|
2021-09-13 19:45:56 +02:00
|
|
|
.displayname(user)?
|
2021-04-14 10:43:31 +02:00
|
|
|
.unwrap_or_else(|| user.localpart().to_owned()),
|
2021-07-30 12:11:06 +02:00
|
|
|
users_power_levels: power_levels.users.clone(),
|
2021-04-12 12:40:16 +02:00
|
|
|
default_power_level: power_levels.users_default,
|
2021-07-30 12:11:06 +02:00
|
|
|
notification_power_levels: power_levels.notifications.clone(),
|
2021-04-12 12:40:16 +02:00
|
|
|
};
|
|
|
|
|
2021-07-30 12:11:06 +02:00
|
|
|
Ok(ruleset.get_actions(pdu, &ctx))
|
2021-04-12 12:40:16 +02:00
|
|
|
}
|
|
|
|
|
2021-07-29 08:36:01 +02:00
|
|
|
#[tracing::instrument(skip(unread, pusher, tweaks, event, db))]
|
2021-01-29 16:14:09 +01:00
|
|
|
async fn send_notice(
|
|
|
|
unread: UInt,
|
2022-02-18 15:33:14 +01:00
|
|
|
pusher: &get_pushers::v3::Pusher,
|
2021-01-29 16:14:09 +01:00
|
|
|
tweaks: Vec<Tweak>,
|
|
|
|
event: &PduEvent,
|
|
|
|
db: &Database,
|
|
|
|
) -> Result<()> {
|
2021-03-22 14:04:11 +01:00
|
|
|
// TODO: email
|
2021-04-22 11:26:20 +02:00
|
|
|
if pusher.kind == PusherKind::Email {
|
2021-03-22 14:04:11 +01:00
|
|
|
return Ok(());
|
|
|
|
}
|
2021-01-29 16:14:09 +01:00
|
|
|
|
|
|
|
// TODO:
|
|
|
|
// Two problems with this
|
|
|
|
// 1. if "event_id_only" is the only format kind it seems we should never add more info
|
|
|
|
// 2. can pusher/devices have conflicting formats
|
2021-03-22 14:04:11 +01:00
|
|
|
let event_id_only = pusher.data.format == Some(PushFormat::EventIdOnly);
|
2021-04-22 11:26:20 +02:00
|
|
|
let url = if let Some(url) = &pusher.data.url {
|
2021-03-22 14:04:11 +01:00
|
|
|
url
|
|
|
|
} else {
|
|
|
|
error!("Http Pusher must have URL specified.");
|
|
|
|
return Ok(());
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut device = Device::new(pusher.app_id.clone(), pusher.pushkey.clone());
|
|
|
|
let mut data_minus_url = pusher.data.clone();
|
|
|
|
// The url must be stripped off according to spec
|
|
|
|
data_minus_url.url = None;
|
2021-11-26 20:36:40 +01:00
|
|
|
device.data = data_minus_url;
|
2021-03-22 14:04:11 +01:00
|
|
|
|
|
|
|
// Tweaks are only added if the format is NOT event_id_only
|
|
|
|
if !event_id_only {
|
|
|
|
device.tweaks = tweaks.clone();
|
|
|
|
}
|
2021-01-29 20:19:56 +01:00
|
|
|
|
2021-03-22 14:04:11 +01:00
|
|
|
let d = &[device];
|
|
|
|
let mut notifi = Notification::new(d);
|
2021-01-29 16:14:09 +01:00
|
|
|
|
2021-03-22 14:04:11 +01:00
|
|
|
notifi.prio = NotificationPriority::Low;
|
|
|
|
notifi.event_id = Some(&event.event_id);
|
|
|
|
notifi.room_id = Some(&event.room_id);
|
|
|
|
// TODO: missed calls
|
|
|
|
notifi.counts = NotificationCounts::new(unread, uint!(0));
|
2021-01-29 16:14:09 +01:00
|
|
|
|
2021-03-22 14:04:11 +01:00
|
|
|
if event.kind == EventType::RoomEncrypted
|
|
|
|
|| tweaks
|
|
|
|
.iter()
|
|
|
|
.any(|t| matches!(t, Tweak::Highlight(true) | Tweak::Sound(_)))
|
|
|
|
{
|
|
|
|
notifi.prio = NotificationPriority::High
|
|
|
|
}
|
2021-01-29 16:14:09 +01:00
|
|
|
|
2021-03-22 14:04:11 +01:00
|
|
|
if event_id_only {
|
|
|
|
send_request(
|
|
|
|
&db.globals,
|
2021-09-13 19:45:56 +02:00
|
|
|
url,
|
2021-03-22 14:04:11 +01:00
|
|
|
send_event_notification::v1::Request::new(notifi),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
} else {
|
|
|
|
notifi.sender = Some(&event.sender);
|
|
|
|
notifi.event_type = Some(&event.kind);
|
2021-05-20 23:46:52 +02:00
|
|
|
let content = serde_json::value::to_raw_value(&event.content).ok();
|
|
|
|
notifi.content = content.as_deref();
|
2021-03-22 14:04:11 +01:00
|
|
|
|
|
|
|
if event.kind == EventType::RoomMember {
|
|
|
|
notifi.user_is_target = event.state_key.as_deref() == Some(event.sender.as_str());
|
2021-01-29 16:14:09 +01:00
|
|
|
}
|
|
|
|
|
2021-03-22 14:04:11 +01:00
|
|
|
let user_name = db.users.displayname(&event.sender)?;
|
|
|
|
notifi.sender_display_name = user_name.as_deref();
|
2021-10-13 10:16:45 +02:00
|
|
|
|
|
|
|
let room_name = if let Some(room_name_pdu) =
|
|
|
|
db.rooms
|
|
|
|
.room_state_get(&event.room_id, &EventType::RoomName, "")?
|
|
|
|
{
|
|
|
|
serde_json::from_str::<RoomNameEventContent>(room_name_pdu.content.get())
|
|
|
|
.map_err(|_| Error::bad_database("Invalid room name event in database."))?
|
|
|
|
.name
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2021-03-22 14:04:11 +01:00
|
|
|
notifi.room_name = room_name.as_deref();
|
|
|
|
|
|
|
|
send_request(
|
|
|
|
&db.globals,
|
2021-09-13 19:45:56 +02:00
|
|
|
url,
|
2021-03-22 14:04:11 +01:00
|
|
|
send_event_notification::v1::Request::new(notifi),
|
|
|
|
)
|
|
|
|
.await?;
|
2021-01-27 03:54:35 +01:00
|
|
|
}
|
2021-01-29 16:14:09 +01:00
|
|
|
|
|
|
|
// TODO: email
|
|
|
|
|
2021-01-27 03:54:35 +01:00
|
|
|
Ok(())
|
|
|
|
}
|