You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

336 lines
12 KiB

  1. // Copyright 2022 The Matrix.org Foundation C.I.C.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //! Contains the definitions of the "base" push rules.
  15. use std::borrow::Cow;
  16. use std::collections::HashMap;
  17. use lazy_static::lazy_static;
  18. use serde_json::Value;
  19. use super::KnownCondition;
  20. use crate::push::Action;
  21. use crate::push::Condition;
  22. use crate::push::EventMatchCondition;
  23. use crate::push::PushRule;
  24. use crate::push::SetTweak;
  25. use crate::push::TweakValue;
  26. const HIGHLIGHT_ACTION: Action = Action::SetTweak(SetTweak {
  27. set_tweak: Cow::Borrowed("highlight"),
  28. value: None,
  29. other_keys: Value::Null,
  30. });
  31. const HIGHLIGHT_FALSE_ACTION: Action = Action::SetTweak(SetTweak {
  32. set_tweak: Cow::Borrowed("highlight"),
  33. value: Some(TweakValue::Other(Value::Bool(false))),
  34. other_keys: Value::Null,
  35. });
  36. const SOUND_ACTION: Action = Action::SetTweak(SetTweak {
  37. set_tweak: Cow::Borrowed("sound"),
  38. value: Some(TweakValue::String(Cow::Borrowed("default"))),
  39. other_keys: Value::Null,
  40. });
  41. const RING_ACTION: Action = Action::SetTweak(SetTweak {
  42. set_tweak: Cow::Borrowed("sound"),
  43. value: Some(TweakValue::String(Cow::Borrowed("ring"))),
  44. other_keys: Value::Null,
  45. });
  46. pub const BASE_PREPEND_OVERRIDE_RULES: &[PushRule] = &[PushRule {
  47. rule_id: Cow::Borrowed("global/override/.m.rule.master"),
  48. priority_class: 5,
  49. conditions: Cow::Borrowed(&[]),
  50. actions: Cow::Borrowed(&[Action::DontNotify]),
  51. default: true,
  52. default_enabled: false,
  53. }];
  54. pub const BASE_APPEND_OVERRIDE_RULES: &[PushRule] = &[
  55. PushRule {
  56. rule_id: Cow::Borrowed("global/override/.m.rule.suppress_notices"),
  57. priority_class: 5,
  58. conditions: Cow::Borrowed(&[Condition::Known(KnownCondition::EventMatch(
  59. EventMatchCondition {
  60. key: Cow::Borrowed("content.msgtype"),
  61. pattern: Some(Cow::Borrowed("m.notice")),
  62. pattern_type: None,
  63. },
  64. ))]),
  65. actions: Cow::Borrowed(&[Action::DontNotify]),
  66. default: true,
  67. default_enabled: true,
  68. },
  69. PushRule {
  70. rule_id: Cow::Borrowed("global/override/.m.rule.invite_for_me"),
  71. priority_class: 5,
  72. conditions: Cow::Borrowed(&[
  73. Condition::Known(KnownCondition::EventMatch(EventMatchCondition {
  74. key: Cow::Borrowed("type"),
  75. pattern: Some(Cow::Borrowed("m.room.member")),
  76. pattern_type: None,
  77. })),
  78. Condition::Known(KnownCondition::EventMatch(EventMatchCondition {
  79. key: Cow::Borrowed("content.membership"),
  80. pattern: Some(Cow::Borrowed("invite")),
  81. pattern_type: None,
  82. })),
  83. Condition::Known(KnownCondition::EventMatch(EventMatchCondition {
  84. key: Cow::Borrowed("state_key"),
  85. pattern: None,
  86. pattern_type: Some(Cow::Borrowed("user_id")),
  87. })),
  88. ]),
  89. actions: Cow::Borrowed(&[Action::Notify, HIGHLIGHT_FALSE_ACTION, SOUND_ACTION]),
  90. default: true,
  91. default_enabled: true,
  92. },
  93. PushRule {
  94. rule_id: Cow::Borrowed("global/override/.m.rule.member_event"),
  95. priority_class: 5,
  96. conditions: Cow::Borrowed(&[Condition::Known(KnownCondition::EventMatch(
  97. EventMatchCondition {
  98. key: Cow::Borrowed("type"),
  99. pattern: Some(Cow::Borrowed("m.room.member")),
  100. pattern_type: None,
  101. },
  102. ))]),
  103. actions: Cow::Borrowed(&[Action::DontNotify]),
  104. default: true,
  105. default_enabled: true,
  106. },
  107. PushRule {
  108. rule_id: Cow::Borrowed("global/override/.m.rule.contains_display_name"),
  109. priority_class: 5,
  110. conditions: Cow::Borrowed(&[Condition::Known(KnownCondition::ContainsDisplayName)]),
  111. actions: Cow::Borrowed(&[Action::Notify, HIGHLIGHT_ACTION, SOUND_ACTION]),
  112. default: true,
  113. default_enabled: true,
  114. },
  115. PushRule {
  116. rule_id: Cow::Borrowed("global/override/.m.rule.roomnotif"),
  117. priority_class: 5,
  118. conditions: Cow::Borrowed(&[
  119. Condition::Known(KnownCondition::SenderNotificationPermission {
  120. key: Cow::Borrowed("room"),
  121. }),
  122. Condition::Known(KnownCondition::EventMatch(EventMatchCondition {
  123. key: Cow::Borrowed("content.body"),
  124. pattern: Some(Cow::Borrowed("@room")),
  125. pattern_type: None,
  126. })),
  127. ]),
  128. actions: Cow::Borrowed(&[Action::Notify, HIGHLIGHT_ACTION]),
  129. default: true,
  130. default_enabled: true,
  131. },
  132. PushRule {
  133. rule_id: Cow::Borrowed("global/override/.m.rule.tombstone"),
  134. priority_class: 5,
  135. conditions: Cow::Borrowed(&[
  136. Condition::Known(KnownCondition::EventMatch(EventMatchCondition {
  137. key: Cow::Borrowed("type"),
  138. pattern: Some(Cow::Borrowed("m.room.tombstone")),
  139. pattern_type: None,
  140. })),
  141. Condition::Known(KnownCondition::EventMatch(EventMatchCondition {
  142. key: Cow::Borrowed("state_key"),
  143. pattern: Some(Cow::Borrowed("")),
  144. pattern_type: None,
  145. })),
  146. ]),
  147. actions: Cow::Borrowed(&[Action::Notify, HIGHLIGHT_ACTION]),
  148. default: true,
  149. default_enabled: true,
  150. },
  151. PushRule {
  152. rule_id: Cow::Borrowed("global/override/.m.rule.reaction"),
  153. priority_class: 5,
  154. conditions: Cow::Borrowed(&[Condition::Known(KnownCondition::EventMatch(
  155. EventMatchCondition {
  156. key: Cow::Borrowed("type"),
  157. pattern: Some(Cow::Borrowed("m.reaction")),
  158. pattern_type: None,
  159. },
  160. ))]),
  161. actions: Cow::Borrowed(&[Action::DontNotify]),
  162. default: true,
  163. default_enabled: true,
  164. },
  165. PushRule {
  166. rule_id: Cow::Borrowed("global/override/.org.matrix.msc3786.rule.room.server_acl"),
  167. priority_class: 5,
  168. conditions: Cow::Borrowed(&[
  169. Condition::Known(KnownCondition::EventMatch(EventMatchCondition {
  170. key: Cow::Borrowed("type"),
  171. pattern: Some(Cow::Borrowed("m.room.server_acl")),
  172. pattern_type: None,
  173. })),
  174. Condition::Known(KnownCondition::EventMatch(EventMatchCondition {
  175. key: Cow::Borrowed("state_key"),
  176. pattern: Some(Cow::Borrowed("")),
  177. pattern_type: None,
  178. })),
  179. ]),
  180. actions: Cow::Borrowed(&[]),
  181. default: true,
  182. default_enabled: true,
  183. },
  184. ];
  185. pub const BASE_APPEND_CONTENT_RULES: &[PushRule] = &[PushRule {
  186. rule_id: Cow::Borrowed("global/content/.m.rule.contains_user_name"),
  187. priority_class: 4,
  188. conditions: Cow::Borrowed(&[Condition::Known(KnownCondition::EventMatch(
  189. EventMatchCondition {
  190. key: Cow::Borrowed("content.body"),
  191. pattern: None,
  192. pattern_type: Some(Cow::Borrowed("user_localpart")),
  193. },
  194. ))]),
  195. actions: Cow::Borrowed(&[Action::Notify, HIGHLIGHT_ACTION, SOUND_ACTION]),
  196. default: true,
  197. default_enabled: true,
  198. }];
  199. pub const BASE_APPEND_UNDERRIDE_RULES: &[PushRule] = &[
  200. PushRule {
  201. rule_id: Cow::Borrowed("global/underride/.m.rule.call"),
  202. priority_class: 1,
  203. conditions: Cow::Borrowed(&[Condition::Known(KnownCondition::EventMatch(
  204. EventMatchCondition {
  205. key: Cow::Borrowed("type"),
  206. pattern: Some(Cow::Borrowed("m.call.invite")),
  207. pattern_type: None,
  208. },
  209. ))]),
  210. actions: Cow::Borrowed(&[Action::Notify, RING_ACTION, HIGHLIGHT_FALSE_ACTION]),
  211. default: true,
  212. default_enabled: true,
  213. },
  214. PushRule {
  215. rule_id: Cow::Borrowed("global/underride/.m.rule.room_one_to_one"),
  216. priority_class: 1,
  217. conditions: Cow::Borrowed(&[
  218. Condition::Known(KnownCondition::EventMatch(EventMatchCondition {
  219. key: Cow::Borrowed("type"),
  220. pattern: Some(Cow::Borrowed("m.room.message")),
  221. pattern_type: None,
  222. })),
  223. Condition::Known(KnownCondition::RoomMemberCount {
  224. is: Some(Cow::Borrowed("2")),
  225. }),
  226. ]),
  227. actions: Cow::Borrowed(&[Action::Notify, SOUND_ACTION, HIGHLIGHT_FALSE_ACTION]),
  228. default: true,
  229. default_enabled: true,
  230. },
  231. PushRule {
  232. rule_id: Cow::Borrowed("global/underride/.m.rule.encrypted_room_one_to_one"),
  233. priority_class: 1,
  234. conditions: Cow::Borrowed(&[
  235. Condition::Known(KnownCondition::EventMatch(EventMatchCondition {
  236. key: Cow::Borrowed("type"),
  237. pattern: Some(Cow::Borrowed("m.room.encrypted")),
  238. pattern_type: None,
  239. })),
  240. Condition::Known(KnownCondition::RoomMemberCount {
  241. is: Some(Cow::Borrowed("2")),
  242. }),
  243. ]),
  244. actions: Cow::Borrowed(&[Action::Notify, SOUND_ACTION, HIGHLIGHT_FALSE_ACTION]),
  245. default: true,
  246. default_enabled: true,
  247. },
  248. PushRule {
  249. rule_id: Cow::Borrowed("global/underride/.org.matrix.msc3772.thread_reply"),
  250. priority_class: 1,
  251. conditions: Cow::Borrowed(&[Condition::Known(KnownCondition::RelationMatch {
  252. rel_type: Cow::Borrowed("m.thread"),
  253. sender: None,
  254. sender_type: Some(Cow::Borrowed("user_id")),
  255. })]),
  256. actions: Cow::Borrowed(&[Action::Notify, HIGHLIGHT_FALSE_ACTION]),
  257. default: true,
  258. default_enabled: true,
  259. },
  260. PushRule {
  261. rule_id: Cow::Borrowed("global/underride/.m.rule.message"),
  262. priority_class: 1,
  263. conditions: Cow::Borrowed(&[Condition::Known(KnownCondition::EventMatch(
  264. EventMatchCondition {
  265. key: Cow::Borrowed("type"),
  266. pattern: Some(Cow::Borrowed("m.room.message")),
  267. pattern_type: None,
  268. },
  269. ))]),
  270. actions: Cow::Borrowed(&[Action::Notify, HIGHLIGHT_FALSE_ACTION]),
  271. default: true,
  272. default_enabled: true,
  273. },
  274. PushRule {
  275. rule_id: Cow::Borrowed("global/underride/.m.rule.encrypted"),
  276. priority_class: 1,
  277. conditions: Cow::Borrowed(&[Condition::Known(KnownCondition::EventMatch(
  278. EventMatchCondition {
  279. key: Cow::Borrowed("type"),
  280. pattern: Some(Cow::Borrowed("m.room.encrypted")),
  281. pattern_type: None,
  282. },
  283. ))]),
  284. actions: Cow::Borrowed(&[Action::Notify, HIGHLIGHT_FALSE_ACTION]),
  285. default: true,
  286. default_enabled: true,
  287. },
  288. PushRule {
  289. rule_id: Cow::Borrowed("global/underride/.im.vector.jitsi"),
  290. priority_class: 1,
  291. conditions: Cow::Borrowed(&[
  292. Condition::Known(KnownCondition::EventMatch(EventMatchCondition {
  293. key: Cow::Borrowed("type"),
  294. pattern: Some(Cow::Borrowed("im.vector.modular.widgets")),
  295. pattern_type: None,
  296. })),
  297. Condition::Known(KnownCondition::EventMatch(EventMatchCondition {
  298. key: Cow::Borrowed("content.type"),
  299. pattern: Some(Cow::Borrowed("jitsi")),
  300. pattern_type: None,
  301. })),
  302. Condition::Known(KnownCondition::EventMatch(EventMatchCondition {
  303. key: Cow::Borrowed("state_key"),
  304. pattern: Some(Cow::Borrowed("*")),
  305. pattern_type: None,
  306. })),
  307. ]),
  308. actions: Cow::Borrowed(&[Action::Notify, HIGHLIGHT_FALSE_ACTION]),
  309. default: true,
  310. default_enabled: true,
  311. },
  312. ];
  313. lazy_static! {
  314. pub static ref BASE_RULES_BY_ID: HashMap<&'static str, &'static PushRule> =
  315. BASE_PREPEND_OVERRIDE_RULES
  316. .iter()
  317. .chain(BASE_APPEND_OVERRIDE_RULES.iter())
  318. .chain(BASE_APPEND_CONTENT_RULES.iter())
  319. .chain(BASE_APPEND_UNDERRIDE_RULES.iter())
  320. .map(|rule| { (&*rule.rule_id, rule) })
  321. .collect();
  322. }