2021-01-29 16:14:09 +01:00
|
|
|
use crate::{Database, Error, PduEvent, Result};
|
|
|
|
use log::{error, info, warn};
|
2021-01-27 03:54:35 +01:00
|
|
|
use ruma::{
|
2021-01-29 16:14:09 +01:00
|
|
|
api::{
|
|
|
|
client::r0::push::{Pusher, PusherKind},
|
|
|
|
push_gateway::send_event_notification::{
|
|
|
|
self,
|
|
|
|
v1::{Device, Notification, NotificationCounts, NotificationPriority},
|
2021-01-27 03:54:35 +01:00
|
|
|
},
|
2021-01-29 16:14:09 +01:00
|
|
|
OutgoingRequest,
|
2021-01-27 03:54:35 +01:00
|
|
|
},
|
2021-03-18 18:33:43 +01:00
|
|
|
events::{
|
|
|
|
room::{
|
|
|
|
member::{MemberEventContent, MembershipState},
|
|
|
|
message::{MessageEventContent, MessageType, TextMessageEventContent},
|
|
|
|
power_levels::PowerLevelsEventContent,
|
|
|
|
},
|
|
|
|
EventType,
|
2021-01-29 16:14:09 +01:00
|
|
|
},
|
|
|
|
push::{Action, PushCondition, PushFormat, Ruleset, Tweak},
|
|
|
|
uint, UInt, UserId,
|
2021-01-27 03:54:35 +01:00
|
|
|
};
|
2021-03-22 14:04:11 +01:00
|
|
|
use sled::IVec;
|
2021-01-27 03:54:35 +01:00
|
|
|
|
2021-03-18 18:33:43 +01:00
|
|
|
use std::{convert::TryFrom, fmt::Debug};
|
2021-01-29 16:14:09 +01:00
|
|
|
|
2021-01-27 03:54:35 +01:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct PushData {
|
|
|
|
/// UserId + pushkey -> Pusher
|
|
|
|
pub(super) senderkey_pusher: sled::Tree,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PushData {
|
|
|
|
pub fn new(db: &sled::Db) -> Result<Self> {
|
|
|
|
Ok(Self {
|
|
|
|
senderkey_pusher: db.open_tree("senderkey_pusher")?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_pusher(&self, sender: &UserId, pusher: Pusher) -> Result<()> {
|
|
|
|
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
|
|
|
|
.remove(key)
|
|
|
|
.map(|_| ())
|
|
|
|
.map_err(Into::into);
|
|
|
|
}
|
|
|
|
|
2021-01-27 03:54:35 +01:00
|
|
|
self.senderkey_pusher.insert(
|
2021-03-16 18:00:26 +01:00
|
|
|
key,
|
2021-01-27 03:54:35 +01:00
|
|
|
&*serde_json::to_string(&pusher).expect("Pusher is valid JSON string"),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-03-22 14:04:11 +01:00
|
|
|
pub fn get_pusher(&self, senderkey: &[u8]) -> Result<Option<Pusher>> {
|
|
|
|
self.senderkey_pusher
|
|
|
|
.get(senderkey)?
|
|
|
|
.map(|push| {
|
|
|
|
Ok(serde_json::from_slice(&*push)
|
|
|
|
.map_err(|_| Error::bad_database("Invalid Pusher in db."))?)
|
|
|
|
})
|
|
|
|
.transpose()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_pushers(&self, sender: &UserId) -> Result<Vec<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-01-27 03:54:35 +01:00
|
|
|
.values()
|
2021-02-11 13:16:14 +01:00
|
|
|
.map(|push| {
|
2021-03-16 18:00:26 +01:00
|
|
|
let push = push.map_err(|_| Error::bad_database("Invalid push bytes in db."))?;
|
2021-01-27 03:54:35 +01:00
|
|
|
Ok(serde_json::from_slice(&*push)
|
|
|
|
.map_err(|_| Error::bad_database("Invalid Pusher in db."))?)
|
|
|
|
})
|
2021-01-29 16:14:09 +01:00
|
|
|
.collect()
|
|
|
|
}
|
2021-03-22 14:04:11 +01:00
|
|
|
|
|
|
|
pub fn get_pusher_senderkeys(&self, sender: &UserId) -> impl Iterator<Item = Result<IVec>> {
|
|
|
|
let mut prefix = sender.as_bytes().to_vec();
|
|
|
|
prefix.push(0xff);
|
|
|
|
|
|
|
|
self.senderkey_pusher
|
|
|
|
.scan_prefix(prefix)
|
|
|
|
.keys()
|
|
|
|
.map(|r| Ok(r?))
|
|
|
|
}
|
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
|
|
|
|
.try_into_http_request(&destination, Some(""))
|
|
|
|
.map_err(|e| {
|
|
|
|
warn!("Failed to find destination {}: {}", destination, e);
|
|
|
|
Error::BadServerResponse("Invalid destination")
|
|
|
|
})?;
|
|
|
|
|
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();
|
2021-03-16 18:00:26 +01:00
|
|
|
let reqwest_response = globals.reqwest_client().execute(reqwest_request).await;
|
2021-01-29 16:14:09 +01:00
|
|
|
|
|
|
|
// Because reqwest::Response -> http::Response is complicated:
|
|
|
|
match reqwest_response {
|
|
|
|
Ok(mut reqwest_response) => {
|
|
|
|
let status = reqwest_response.status();
|
|
|
|
let mut http_response = http::Response::builder().status(status);
|
|
|
|
let headers = http_response.headers_mut().unwrap();
|
|
|
|
|
|
|
|
for (k, v) in reqwest_response.headers_mut().drain() {
|
|
|
|
if let Some(key) = k {
|
|
|
|
headers.insert(key, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let status = reqwest_response.status();
|
|
|
|
|
|
|
|
let body = reqwest_response
|
|
|
|
.bytes()
|
|
|
|
.await
|
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
warn!("server error {}", e);
|
|
|
|
Vec::new().into()
|
|
|
|
}) // TODO: handle timeout
|
|
|
|
.into_iter()
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
if status != 200 {
|
|
|
|
info!(
|
|
|
|
"Push gateway returned bad response {} {}\n{}\n{:?}",
|
|
|
|
destination,
|
|
|
|
status,
|
|
|
|
url,
|
|
|
|
crate::utils::string_from_bytes(&body)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let response = T::IncomingResponse::try_from(
|
|
|
|
http_response
|
|
|
|
.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn send_push_notice(
|
|
|
|
user: &UserId,
|
2021-01-29 16:14:09 +01:00
|
|
|
unread: UInt,
|
2021-03-22 14:04:11 +01:00
|
|
|
pusher: &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-02-11 13:16:14 +01:00
|
|
|
if let Some(msgtype) = pdu.content.get("msgtype").and_then(|b| b.as_str()) {
|
|
|
|
if msgtype == "m.notice" {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 03:54:35 +01:00
|
|
|
for rule in ruleset.into_iter() {
|
|
|
|
// TODO: can actions contain contradictory Actions
|
|
|
|
if rule
|
|
|
|
.actions
|
|
|
|
.iter()
|
|
|
|
.any(|act| matches!(act, ruma::push::Action::DontNotify))
|
|
|
|
|| !rule.enabled
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-03-16 18:00:26 +01:00
|
|
|
match rule.rule_id.as_str() {
|
2021-01-27 03:54:35 +01:00
|
|
|
".m.rule.master" => {}
|
2021-01-29 16:14:09 +01:00
|
|
|
".m.rule.suppress_notices" => {
|
|
|
|
if pdu.kind == EventType::RoomMessage
|
|
|
|
&& pdu
|
|
|
|
.content
|
|
|
|
.get("msgtype")
|
|
|
|
.map_or(false, |ty| ty == "m.notice")
|
|
|
|
{
|
|
|
|
let tweaks = rule
|
|
|
|
.actions
|
|
|
|
.iter()
|
|
|
|
.filter_map(|a| match a {
|
|
|
|
Action::SetTweak(tweak) => Some(tweak.clone()),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2021-03-22 14:04:11 +01:00
|
|
|
send_notice(unread, pusher, tweaks, pdu, db, rule.rule_id.as_str()).await?;
|
2021-01-29 20:19:56 +01:00
|
|
|
break;
|
2021-01-29 16:14:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
".m.rule.invite_for_me" => {
|
|
|
|
if let EventType::RoomMember = &pdu.kind {
|
|
|
|
if pdu.state_key.as_deref() == Some(user.as_str())
|
|
|
|
&& serde_json::from_value::<MemberEventContent>(pdu.content.clone())
|
|
|
|
.map_err(|_| Error::bad_database("PDU contained bad message content"))?
|
|
|
|
.membership
|
|
|
|
== MembershipState::Invite
|
|
|
|
{
|
|
|
|
let tweaks = rule
|
|
|
|
.actions
|
|
|
|
.iter()
|
|
|
|
.filter_map(|a| match a {
|
|
|
|
Action::SetTweak(tweak) => Some(tweak.clone()),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2021-03-22 14:04:11 +01:00
|
|
|
send_notice(unread, pusher, tweaks, pdu, db, rule.rule_id.as_str()).await?;
|
2021-01-29 20:19:56 +01:00
|
|
|
break;
|
2021-01-29 16:14:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-27 03:54:35 +01:00
|
|
|
".m.rule.member_event" => {
|
|
|
|
if let EventType::RoomMember = &pdu.kind {
|
|
|
|
// TODO use this?
|
|
|
|
let _member = serde_json::from_value::<MemberEventContent>(pdu.content.clone())
|
|
|
|
.map_err(|_| Error::bad_database("PDU contained bad message content"))?;
|
|
|
|
if let Some(conditions) = rule.conditions {
|
|
|
|
if conditions.iter().any(|cond| match cond {
|
|
|
|
PushCondition::EventMatch { key, pattern } => {
|
|
|
|
let mut json =
|
|
|
|
serde_json::to_value(pdu).expect("PDU is valid JSON");
|
|
|
|
for key in key.split('.') {
|
|
|
|
json = json[key].clone();
|
|
|
|
}
|
|
|
|
// TODO: this is baddddd
|
|
|
|
json.to_string().contains(pattern)
|
|
|
|
}
|
|
|
|
_ => false,
|
2021-01-29 16:14:09 +01:00
|
|
|
}) {
|
|
|
|
let tweaks = rule
|
|
|
|
.actions
|
|
|
|
.iter()
|
|
|
|
.filter_map(|a| match a {
|
|
|
|
Action::SetTweak(tweak) => Some(tweak.clone()),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2021-03-22 14:04:11 +01:00
|
|
|
send_notice(unread, pusher, tweaks, pdu, db, rule.rule_id.as_str())
|
2021-01-29 20:19:56 +01:00
|
|
|
.await?;
|
|
|
|
break;
|
2021-01-29 16:14:09 +01:00
|
|
|
}
|
2021-01-27 03:54:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
".m.rule.contains_display_name" => {
|
|
|
|
if let EventType::RoomMessage = &pdu.kind {
|
|
|
|
let msg_content =
|
|
|
|
serde_json::from_value::<MessageEventContent>(pdu.content.clone())
|
|
|
|
.map_err(|_| {
|
|
|
|
Error::bad_database("PDU contained bad message content")
|
|
|
|
})?;
|
2021-03-15 09:48:19 +01:00
|
|
|
if let MessageType::Text(TextMessageEventContent { body, .. }) =
|
|
|
|
&msg_content.msgtype
|
2021-01-27 03:54:35 +01:00
|
|
|
{
|
|
|
|
if body.contains(user.localpart()) {
|
2021-01-29 16:14:09 +01:00
|
|
|
let tweaks = rule
|
|
|
|
.actions
|
|
|
|
.iter()
|
|
|
|
.filter_map(|a| match a {
|
|
|
|
Action::SetTweak(tweak) => Some(tweak.clone()),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2021-03-22 14:04:11 +01:00
|
|
|
send_notice(unread, pusher, tweaks, pdu, db, rule.rule_id.as_str())
|
2021-01-29 20:19:56 +01:00
|
|
|
.await?;
|
|
|
|
break;
|
2021-01-29 16:14:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
".m.rule.tombstone" => {
|
|
|
|
if pdu.kind == EventType::RoomTombstone && pdu.state_key.as_deref() == Some("") {
|
|
|
|
let tweaks = rule
|
|
|
|
.actions
|
|
|
|
.iter()
|
|
|
|
.filter_map(|a| match a {
|
|
|
|
Action::SetTweak(tweak) => Some(tweak.clone()),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2021-03-22 14:04:11 +01:00
|
|
|
send_notice(unread, pusher, tweaks, pdu, db, rule.rule_id.as_str()).await?;
|
2021-01-29 20:19:56 +01:00
|
|
|
break;
|
2021-01-29 16:14:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
".m.rule.roomnotif" => {
|
|
|
|
if let EventType::RoomMessage = &pdu.kind {
|
|
|
|
let msg_content =
|
|
|
|
serde_json::from_value::<MessageEventContent>(pdu.content.clone())
|
|
|
|
.map_err(|_| {
|
|
|
|
Error::bad_database("PDU contained bad message content")
|
|
|
|
})?;
|
2021-03-15 09:48:19 +01:00
|
|
|
if let MessageType::Text(TextMessageEventContent { body, .. }) =
|
|
|
|
&msg_content.msgtype
|
2021-01-29 16:14:09 +01:00
|
|
|
{
|
|
|
|
let power_level_cmp = |pl: PowerLevelsEventContent| {
|
|
|
|
&pl.notifications.room
|
|
|
|
<= pl.users.get(&pdu.sender).unwrap_or(&ruma::int!(0))
|
|
|
|
};
|
|
|
|
let deserialize = |pl: PduEvent| {
|
|
|
|
serde_json::from_value::<PowerLevelsEventContent>(pl.content).ok()
|
|
|
|
};
|
|
|
|
if body.contains("@room")
|
|
|
|
&& db
|
|
|
|
.rooms
|
|
|
|
.room_state_get(&pdu.room_id, &EventType::RoomPowerLevels, "")?
|
|
|
|
.map(deserialize)
|
|
|
|
.flatten()
|
|
|
|
.map_or(false, power_level_cmp)
|
|
|
|
{
|
|
|
|
let tweaks = rule
|
|
|
|
.actions
|
|
|
|
.iter()
|
|
|
|
.filter_map(|a| match a {
|
|
|
|
Action::SetTweak(tweak) => Some(tweak.clone()),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2021-03-22 14:04:11 +01:00
|
|
|
send_notice(unread, pusher, tweaks, pdu, db, rule.rule_id.as_str())
|
2021-01-29 20:19:56 +01:00
|
|
|
.await?;
|
|
|
|
break;
|
2021-01-27 03:54:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
".m.rule.contains_user_name" => {
|
|
|
|
if let EventType::RoomMessage = &pdu.kind {
|
|
|
|
let msg_content =
|
|
|
|
serde_json::from_value::<MessageEventContent>(pdu.content.clone())
|
|
|
|
.map_err(|_| {
|
|
|
|
Error::bad_database("PDU contained bad message content")
|
|
|
|
})?;
|
2021-03-15 09:48:19 +01:00
|
|
|
if let MessageType::Text(TextMessageEventContent { body, .. }) =
|
|
|
|
&msg_content.msgtype
|
2021-01-27 03:54:35 +01:00
|
|
|
{
|
|
|
|
if body.contains(user.localpart()) {
|
2021-01-29 16:14:09 +01:00
|
|
|
let tweaks = rule
|
|
|
|
.actions
|
|
|
|
.iter()
|
|
|
|
.filter_map(|a| match a {
|
|
|
|
Action::SetTweak(tweak) => Some(tweak.clone()),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2021-03-22 14:04:11 +01:00
|
|
|
send_notice(unread, pusher, tweaks, pdu, db, rule.rule_id.as_str())
|
2021-01-29 20:19:56 +01:00
|
|
|
.await?;
|
|
|
|
break;
|
2021-01-27 03:54:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-29 16:14:09 +01:00
|
|
|
".m.rule.call" => {
|
|
|
|
if pdu.kind == EventType::CallInvite {
|
|
|
|
let tweaks = rule
|
|
|
|
.actions
|
|
|
|
.iter()
|
|
|
|
.filter_map(|a| match a {
|
|
|
|
Action::SetTweak(tweak) => Some(tweak.clone()),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2021-03-22 14:04:11 +01:00
|
|
|
send_notice(unread, pusher, tweaks, pdu, db, rule.rule_id.as_str()).await?;
|
2021-01-29 20:19:56 +01:00
|
|
|
break;
|
2021-01-29 16:14:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
".m.rule.encrypted_room_one_to_one" => {
|
|
|
|
if db.rooms.room_members(&pdu.room_id).count() == 2
|
|
|
|
&& pdu.kind == EventType::RoomEncrypted
|
|
|
|
{
|
|
|
|
let tweaks = rule
|
|
|
|
.actions
|
|
|
|
.iter()
|
|
|
|
.filter_map(|a| match a {
|
|
|
|
Action::SetTweak(tweak) => Some(tweak.clone()),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2021-03-22 14:04:11 +01:00
|
|
|
send_notice(unread, pusher, tweaks, pdu, db, rule.rule_id.as_str()).await?;
|
2021-01-29 20:19:56 +01:00
|
|
|
break;
|
2021-01-29 16:14:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
".m.rule.room_one_to_one" => {
|
|
|
|
if db.rooms.room_members(&pdu.room_id).count() == 2
|
|
|
|
&& pdu.kind == EventType::RoomMessage
|
|
|
|
{
|
|
|
|
let tweaks = rule
|
|
|
|
.actions
|
|
|
|
.iter()
|
|
|
|
.filter_map(|a| match a {
|
|
|
|
Action::SetTweak(tweak) => Some(tweak.clone()),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2021-03-22 14:04:11 +01:00
|
|
|
send_notice(unread, pusher, tweaks, pdu, db, rule.rule_id.as_str()).await?;
|
2021-01-29 20:19:56 +01:00
|
|
|
break;
|
2021-01-29 16:14:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
".m.rule.message" => {
|
|
|
|
if pdu.kind == EventType::RoomMessage {
|
|
|
|
let tweaks = rule
|
|
|
|
.actions
|
|
|
|
.iter()
|
|
|
|
.filter_map(|a| match a {
|
|
|
|
Action::SetTweak(tweak) => Some(tweak.clone()),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2021-03-22 14:04:11 +01:00
|
|
|
send_notice(unread, pusher, tweaks, pdu, db, rule.rule_id.as_str()).await?;
|
2021-01-29 20:19:56 +01:00
|
|
|
break;
|
2021-01-29 16:14:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
".m.rule.encrypted" => {
|
|
|
|
if pdu.kind == EventType::RoomEncrypted {
|
|
|
|
let tweaks = rule
|
|
|
|
.actions
|
|
|
|
.iter()
|
|
|
|
.filter_map(|a| match a {
|
|
|
|
Action::SetTweak(tweak) => Some(tweak.clone()),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2021-03-22 14:04:11 +01:00
|
|
|
send_notice(unread, pusher, tweaks, pdu, db, rule.rule_id.as_str()).await?;
|
2021-01-29 20:19:56 +01:00
|
|
|
break;
|
2021-01-29 16:14:09 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-27 03:54:35 +01:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2021-01-29 20:19:56 +01:00
|
|
|
|
2021-01-27 03:54:35 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-01-29 16:14:09 +01:00
|
|
|
async fn send_notice(
|
|
|
|
unread: UInt,
|
2021-03-22 14:04:11 +01:00
|
|
|
pusher: &Pusher,
|
2021-01-29 16:14:09 +01:00
|
|
|
tweaks: Vec<Tweak>,
|
|
|
|
event: &PduEvent,
|
|
|
|
db: &Database,
|
2021-01-29 20:19:56 +01:00
|
|
|
name: &str,
|
2021-01-29 16:14:09 +01:00
|
|
|
) -> Result<()> {
|
2021-03-22 14:04:11 +01:00
|
|
|
// TODO: email
|
|
|
|
if pusher.kind == Some(PusherKind::Http) {
|
|
|
|
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);
|
|
|
|
let url = if let Some(url) = pusher.data.url.as_ref() {
|
|
|
|
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;
|
|
|
|
device.data = Some(data_minus_url);
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
error!("SEND PUSH NOTICE `{}`", name);
|
|
|
|
send_request(
|
|
|
|
&db.globals,
|
|
|
|
&url,
|
|
|
|
send_event_notification::v1::Request::new(notifi),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
} else {
|
|
|
|
notifi.sender = Some(&event.sender);
|
|
|
|
notifi.event_type = Some(&event.kind);
|
|
|
|
notifi.content = serde_json::value::to_raw_value(&event.content).ok();
|
|
|
|
|
|
|
|
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();
|
|
|
|
let room_name = db
|
|
|
|
.rooms
|
|
|
|
.room_state_get(&event.room_id, &EventType::RoomName, "")?
|
|
|
|
.map(|pdu| match pdu.content.get("name") {
|
|
|
|
Some(serde_json::Value::String(s)) => Some(s.to_string()),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.flatten();
|
|
|
|
notifi.room_name = room_name.as_deref();
|
|
|
|
|
|
|
|
error!("SEND PUSH NOTICE Full `{}`", name);
|
|
|
|
send_request(
|
|
|
|
&db.globals,
|
|
|
|
&url,
|
|
|
|
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(())
|
|
|
|
}
|