Skip to content

Commit f0ff8ef

Browse files
committed
Move notification changes from old branch
1 parent c4c2fc5 commit f0ff8ef

File tree

11 files changed

+260
-26
lines changed

11 files changed

+260
-26
lines changed

Cargo.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindings/matrix-sdk-ffi/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ tracing = { workspace = true }
3939
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
4040
uniffi = { workspace = true }
4141
uniffi_macros = { workspace = true }
42+
rand = "0.8.5"

bindings/matrix-sdk-ffi/src/api.udl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,17 @@ interface SlidingSyncBuilder {
151151
SlidingSync build();
152152
};
153153

154+
[Enum]
155+
interface PusherKind {
156+
Http();
157+
Email();
158+
};
159+
160+
[Enum]
161+
interface PushFormat {
162+
EventIdOnly();
163+
};
164+
154165
interface Client {
155166
void set_delegate(ClientDelegate? delegate);
156167

@@ -195,6 +206,18 @@ interface Client {
195206

196207
[Throws=ClientError]
197208
void logout();
209+
210+
[Throws=ClientError]
211+
void set_pusher(string pushkey,
212+
PusherKind? kind,
213+
string app_id,
214+
string app_display_name,
215+
string device_display_name,
216+
string? profile_tag,
217+
string lang,
218+
string? url,
219+
PushFormat? format,
220+
string? default_payload);
198221
};
199222

200223
interface Room {
@@ -361,6 +384,21 @@ interface AuthenticationService {
361384
Client restore_with_access_token(string token, string device_id);
362385
};
363386

387+
dictionary NotificationItem {
388+
TimelineItem item;
389+
string title;
390+
string? subtitle;
391+
boolean is_noisy;
392+
string? avatar_url;
393+
};
394+
395+
interface NotificationService {
396+
constructor(string base_path, string user_id);
397+
398+
[Throws=ClientError]
399+
NotificationItem? get_notification_item(string room_id, string event_id);
400+
};
401+
364402
interface SessionVerificationEmoji {};
365403

366404
callback interface SessionVerificationControllerDelegate {

bindings/matrix-sdk-ffi/src/client.rs

Lines changed: 89 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,62 @@ use matrix_sdk::{
55
config::SyncSettings,
66
media::{MediaFormat, MediaRequest, MediaThumbnailSize},
77
ruma::{
8-
api::client::{
9-
account::whoami,
10-
error::ErrorKind,
11-
filter::{FilterDefinition, LazyLoadOptions, RoomEventFilter, RoomFilter},
12-
media::get_content_thumbnail::v3::Method,
13-
session::get_login_types,
14-
sync::sync_events::v3::Filter,
8+
api::{
9+
self,
10+
client::{
11+
account::whoami,
12+
error::ErrorKind,
13+
filter::{FilterDefinition, LazyLoadOptions, RoomEventFilter, RoomFilter},
14+
media::get_content_thumbnail::v3::Method,
15+
push::set_pusher::v3::PusherInit,
16+
session::get_login_types,
17+
sync::sync_events::v3::Filter,
18+
},
1519
},
1620
events::{room::MediaSource, AnyToDeviceEvent},
21+
push::PusherData,
1722
serde::Raw,
1823
TransactionId, UInt,
1924
},
2025
Client as MatrixClient, Error, LoopCtrl, RumaApiError, Session,
2126
};
27+
use serde_json::Value;
2228

2329
use super::{
2430
room::Room, session_verification::SessionVerificationController, ClientState, RestoreToken,
2531
RUNTIME,
2632
};
2733

34+
#[derive(Clone)]
35+
pub enum PusherKind {
36+
Http,
37+
Email,
38+
}
39+
40+
impl PusherKind {
41+
pub fn convert(&self) -> api::client::push::PusherKind {
42+
use api::client::push::PusherKind as PK;
43+
match self {
44+
Http => PK::Http,
45+
Email => PK::Email,
46+
}
47+
}
48+
}
49+
50+
#[derive(Clone)]
51+
pub enum PushFormat {
52+
EventIdOnly,
53+
}
54+
55+
impl PushFormat {
56+
pub fn convert(&self) -> matrix_sdk::ruma::push::PushFormat {
57+
use matrix_sdk::ruma::push::PushFormat as PF;
58+
match self {
59+
EventIdOnly => PF::EventIdOnly,
60+
}
61+
}
62+
}
63+
2864
impl std::ops::Deref for Client {
2965
type Target = MatrixClient;
3066
fn deref(&self) -> &MatrixClient {
@@ -295,6 +331,52 @@ impl Client {
295331
tracing::warn!("Ignoring sync error: {:?}", sync_error);
296332
LoopCtrl::Continue
297333
}
334+
335+
/// Registers a pusher with given parameters
336+
pub fn set_pusher(
337+
&self,
338+
pushkey: String,
339+
kind: Option<PusherKind>,
340+
app_id: String,
341+
app_display_name: String,
342+
device_display_name: String,
343+
profile_tag: Option<String>,
344+
lang: String,
345+
url: Option<String>,
346+
format: Option<PushFormat>,
347+
default_payload: Option<String>,
348+
) -> anyhow::Result<()> {
349+
RUNTIME.block_on(async move {
350+
let mut pusher_data = PusherData::new();
351+
pusher_data.url = url;
352+
pusher_data.format = match format {
353+
Some(fmt) => Some(fmt.convert()),
354+
_ => None,
355+
};
356+
pusher_data.default_payload = match default_payload {
357+
Some(json) => serde_json::from_str(&json)?,
358+
_ => Value::Null,
359+
};
360+
361+
let pusher_init = PusherInit {
362+
pushkey: pushkey,
363+
kind: match kind {
364+
Some(knd) => Some(knd.convert()),
365+
None => None,
366+
},
367+
app_id: app_id,
368+
app_display_name: app_display_name,
369+
device_display_name: device_display_name,
370+
profile_tag: profile_tag,
371+
lang: lang,
372+
data: pusher_data,
373+
};
374+
match self.client.set_pusher(pusher_init.into()).await {
375+
Ok(_) => Ok(()),
376+
Err(error) => Err(anyhow!(error.to_string())),
377+
}
378+
})
379+
}
298380
}
299381

300382
#[uniffi::export]

bindings/matrix-sdk-ffi/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub mod authentication_service;
2424
pub mod client;
2525
pub mod client_builder;
2626
mod helpers;
27+
pub mod notification_service;
2728
pub mod room;
2829
pub mod session_verification;
2930
pub mod sliding_sync;
@@ -50,8 +51,8 @@ pub use matrix_sdk::{
5051
};
5152

5253
pub use self::{
53-
authentication_service::*, client::*, room::*, session_verification::*, sliding_sync::*,
54-
timeline::*,
54+
authentication_service::*, client::*, notification_service::*, room::*,
55+
session_verification::*, sliding_sync::*, timeline::*,
5556
};
5657

5758
#[derive(Default, Debug)]
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
use std::sync::Arc;
2+
3+
use matrix_sdk::{
4+
room::timeline::{
5+
BundledReactions, EventTimelineItem, Message, TimelineItemContent, TimelineKey,
6+
},
7+
ruma::{
8+
api::client::message,
9+
events::room::{
10+
message::{ImageMessageEventContent, MessageType, TextMessageEventContent},
11+
ImageInfo,
12+
},
13+
EventId, MilliSecondsSinceUnixEpoch, MxcUri, ServerName, UInt, UserId,
14+
},
15+
};
16+
use rand::{random, Rng};
17+
18+
use crate::{ClientError, TimelineItem};
19+
20+
pub struct NotificationService {
21+
base_path: String,
22+
user_id: String,
23+
}
24+
25+
pub struct NotificationItem {
26+
pub item: Arc<TimelineItem>,
27+
pub title: String,
28+
pub subtitle: Option<String>,
29+
pub is_noisy: bool,
30+
pub avatar_url: Option<String>,
31+
}
32+
33+
impl NotificationService {
34+
/// Creates a new notification service.
35+
pub fn new(base_path: String, user_id: String) -> Self {
36+
NotificationService { base_path, user_id }
37+
}
38+
39+
pub fn get_notification_item(
40+
&self,
41+
_room_id: String,
42+
event_id: String,
43+
) -> anyhow::Result<Option<NotificationItem>> {
44+
let text_message_type = MessageType::Text(TextMessageEventContent::plain("Notified text"));
45+
let mut image_info = ImageInfo::new();
46+
image_info.height = UInt::new(640);
47+
image_info.width = UInt::new(638);
48+
image_info.mimetype = Some("image/jpeg".to_owned());
49+
image_info.size = UInt::new(118141);
50+
image_info.blurhash = Some("TFF~Ba5at616yD^%~T-oXU9bt6kr".to_owned());
51+
let image_message_type = MessageType::Image(ImageMessageEventContent::plain(
52+
"body".to_owned(),
53+
(*Box::<MxcUri>::from("mxc://matrix.org/vNOdmUTIPIwWvMneNzyCzNhb")).to_owned(),
54+
Some(Box::new(image_info)),
55+
));
56+
57+
let random = rand::thread_rng().gen_range(0..2);
58+
59+
let sdk_event = EventTimelineItem {
60+
key: TimelineKey::EventId(EventId::parse(event_id.to_owned()).unwrap()),
61+
event_id: Some(EventId::parse(event_id.to_owned()).unwrap()),
62+
sender: UserId::parse("@username:example.com").unwrap(),
63+
content: TimelineItemContent::Message(Message {
64+
msgtype: match random {
65+
1 => image_message_type,
66+
_ => text_message_type,
67+
},
68+
in_reply_to: None,
69+
edited: false,
70+
}),
71+
reactions: BundledReactions::default(),
72+
origin_server_ts: Some(MilliSecondsSinceUnixEpoch::now()),
73+
is_own: false,
74+
encryption_info: None,
75+
raw: None,
76+
};
77+
78+
let item = NotificationItem {
79+
item: Arc::new(TimelineItem(matrix_sdk::room::timeline::TimelineItem::Event(
80+
sdk_event,
81+
))),
82+
title: "ismailgulek".to_owned(),
83+
subtitle: Some("Element iOS - Internal".to_owned()),
84+
is_noisy: true,
85+
avatar_url: Some("mxc://matrix.org/XzNaquIfpvjHtftUJBCRNDgX".to_owned()),
86+
};
87+
Ok(Some(item))
88+
}
89+
}

bindings/matrix-sdk-ffi/src/timeline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ pub enum TimelineChange {
128128

129129
#[repr(transparent)]
130130
#[derive(Clone)]
131-
pub struct TimelineItem(matrix_sdk::room::timeline::TimelineItem);
131+
pub struct TimelineItem(pub matrix_sdk::room::timeline::TimelineItem);
132132

133133
impl TimelineItem {
134134
fn from_arc(arc: Arc<matrix_sdk::room::timeline::TimelineItem>) -> Arc<Self> {

crates/matrix-sdk/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ matrix-sdk-sled = { version = "0.2.0", path = "../matrix-sdk-sled", default-feat
8181
mime = "0.3.16"
8282
rand = { version = "0.8.5", optional = true }
8383
reqwest = { version = "0.11.10", default_features = false }
84-
ruma = { workspace = true, features = ["compat", "rand", "unstable-msc2448", "unstable-msc2965"] }
84+
ruma = { workspace = true, features = ["compat", "rand", "unstable-msc2448", "unstable-msc2965", "unstable-pre-spec"] }
8585
serde = "1.0.136"
8686
serde_json = "1.0.79"
8787
thiserror = "1.0.30"

crates/matrix-sdk/src/client/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ use ruma::{
5050
error::ErrorKind,
5151
filter::{create_filter::v3::Request as FilterUploadRequest, FilterDefinition},
5252
membership::{join_room_by_id, join_room_by_id_or_alias},
53-
push::get_notifications::v3::Notification,
53+
push::{
54+
get_notifications::v3::Notification,
55+
set_pusher::{self, v3::Pusher},
56+
},
5457
room::create_room,
5558
session::{
5659
get_login_types, login, logout, refresh_token, sso_login, sso_login_with_provider,
@@ -2365,6 +2368,12 @@ impl Client {
23652368
let request = logout::v3::Request::new();
23662369
self.send(request, None).await
23672370
}
2371+
2372+
/// Sets a given pusher
2373+
pub async fn set_pusher(&self, pusher: Pusher) -> HttpResult<set_pusher::v3::Response> {
2374+
let request = set_pusher::v3::Request::new(pusher);
2375+
self.send(request, None).await
2376+
}
23682377
}
23692378

23702379
// The http mocking library is not supported for wasm32

0 commit comments

Comments
 (0)