Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: notification metadata #505

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions migrations/20240417170201_notification_metadata.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE notification ADD COLUMN data VARCHAR(255) NULL DEFAULT NULL;
ALTER TABLE welcome_notification ADD COLUMN data VARCHAR(255) NULL DEFAULT NULL;
14 changes: 10 additions & 4 deletions src/model/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,7 @@ pub struct Notification {
pub body: String,
pub icon: Option<String>,
pub url: Option<String>,
pub data: Option<String>,
pub is_read: bool,
}

Expand Down Expand Up @@ -970,6 +971,7 @@ pub async fn get_notifications_for_subscriber(
notification.title,
notification.body,
notification.url,
notification.data,
notification.icon
FROM notification
JOIN subscriber_notification ON subscriber_notification.notification=notification.id
Expand Down Expand Up @@ -1046,6 +1048,8 @@ pub struct WelcomeNotification {
pub body: String,
#[validate(length(min = 1, max = 255))]
pub url: Option<String>,
#[validate(length(min = 1, max = 255))]
pub data: Option<String>,
}

#[instrument(skip(postgres, metrics))]
Expand All @@ -1055,7 +1059,7 @@ pub async fn get_welcome_notification(
metrics: Option<&Metrics>,
) -> Result<Option<WelcomeNotification>, sqlx::Error> {
let query = "
SELECT enabled, type, title, body, url
SELECT enabled, type, title, body, url, data
FROM welcome_notification
WHERE project=$1
";
Expand All @@ -1078,14 +1082,15 @@ pub async fn set_welcome_notification(
metrics: Option<&Metrics>,
) -> Result<(), sqlx::Error> {
let query = "
INSERT INTO welcome_notification (project, enabled, type, title, body, url)
VALUES ($1, $2, $3, $4, $5, $6)
INSERT INTO welcome_notification (project, enabled, type, title, body, url, data)
VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT (project) DO UPDATE SET
enabled=EXCLUDED.enabled,
type=EXCLUDED.type,
title=EXCLUDED.title,
body=EXCLUDED.body,
url=EXCLUDED.url
url=EXCLUDED.url,
data=EXCLUDED.data
";
let start = Instant::now();
sqlx::query(query)
Expand All @@ -1095,6 +1100,7 @@ pub async fn set_welcome_notification(
.bind(welcome_notification.title)
.bind(welcome_notification.body)
.bind(welcome_notification.url)
.bind(welcome_notification.data)
.execute(postgres)
.await?;
if let Some(metrics) = metrics {
Expand Down
1 change: 1 addition & 0 deletions src/notify_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ pub struct JwtNotification {
pub body: String,
pub icon: String,
pub url: String,
pub data: Option<String>,
pub is_read: bool,
}
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ pub async fn handle(msg: RelayIncomingMessage, state: &AppState) -> Result<(), R
title: welcome_notification.title,
body: welcome_notification.body,
url: welcome_notification.url,
data: welcome_notification.data,
icon: None,
},
&state.postgres,
Expand Down
11 changes: 8 additions & 3 deletions src/services/publisher_service/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ pub async fn upsert_notification(
pub body: String,
pub icon: Option<String>,
pub url: Option<String>,
pub data: Option<String>,
}
let query = "
INSERT INTO notification (project, notification_id, type, title, body, icon, url)
VALUES ($1, $2, $3, $4, $5, $6, $7)
INSERT INTO notification (project, notification_id, type, title, body, icon, url, data)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
ON CONFLICT (project, notification_id) DO UPDATE SET
notification_id=EXCLUDED.notification_id
RETURNING id, type, title, body, icon, url
RETURNING id, type, title, body, icon, url, data
";
let start = Instant::now();
let result = sqlx::query_as::<Postgres, Result>(query)
Expand All @@ -48,6 +49,7 @@ pub async fn upsert_notification(
.bind(notification.body)
.bind(notification.icon)
.bind(notification.url)
.bind(notification.data)
.fetch_one(postgres)
.await?;
if let Some(metrics) = metrics {
Expand All @@ -61,6 +63,7 @@ pub async fn upsert_notification(
body: result.body,
icon: result.icon,
url: result.url,
data: result.data,
},
})
}
Expand Down Expand Up @@ -98,6 +101,7 @@ pub struct NotificationToProcess {
pub notification_body: String,
pub notification_icon: Option<String>,
pub notification_url: Option<String>,
pub notification_data: Option<String>,
pub subscriber: Uuid,
#[sqlx(try_from = "String")]
pub subscriber_account: AccountId,
Expand Down Expand Up @@ -159,6 +163,7 @@ pub async fn pick_subscriber_notification_for_processing(
notification.body AS notification_body,
notification.icon AS notification_icon,
notification.url AS notification_url,
notification.data AS notification_data,
subscriber.id AS subscriber,
subscriber.account AS subscriber_account,
subscriber.sym_key AS subscriber_sym_key,
Expand Down
1 change: 1 addition & 0 deletions src/services/publisher_service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ async fn process_notification(
.to_string()
})
.unwrap_or_default(),
data: notification.notification_data.clone(),
is_read: notification.subscriber_notification_is_read,
}),
notification.subscriber_account.clone(),
Expand Down
8 changes: 8 additions & 0 deletions src/types/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub struct Notification {
pub icon: Option<String>,
#[validate(length(min = 1, max = 255))]
pub url: Option<String>,
#[validate(length(min = 1, max = 255))]
pub data: Option<String>,
}

impl Notification {
Expand All @@ -37,6 +39,7 @@ mod test {
body: "body".to_owned(),
icon: None,
url: None,
data: None,
}
.validate()
.is_ok());
Expand All @@ -47,6 +50,7 @@ mod test {
body: "body".to_owned(),
icon: Some("icon".to_owned()),
url: Some("url".to_owned()),
data: None,
}
.validate()
.is_ok());
Expand All @@ -60,6 +64,7 @@ mod test {
body: "body".to_owned(),
icon: None,
url: None,
data: None,
}
.validate()
.is_err());
Expand All @@ -70,6 +75,7 @@ mod test {
body: "".to_owned(),
icon: Some("icon".to_owned()),
url: Some("url".to_owned()),
data: None,
}
.validate()
.is_err());
Expand All @@ -80,6 +86,7 @@ mod test {
body: "body".to_owned(),
icon: Some("".to_owned()),
url: Some("url".to_owned()),
data: None,
}
.validate()
.is_err());
Expand All @@ -90,6 +97,7 @@ mod test {
body: "body".to_owned(),
icon: Some("icon".to_owned()),
url: Some("".to_owned()),
data: None,
}
.validate()
.is_err());
Expand Down
2 changes: 2 additions & 0 deletions tests/deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ async fn deployment_integration() {
body: "body".to_owned(),
icon: None,
url: None,
data: None,
};

let notify_body = NotifyBody {
Expand Down Expand Up @@ -257,6 +258,7 @@ async fn deployment_integration() {
assert_eq!(claims.msg.body, "body");
assert_eq!(claims.msg.icon, "");
assert_eq!(claims.msg.url, "");
assert_eq!(claims.msg.data, None);

unregister_identity_key(
identity_key_details.keys_server_url,
Expand Down
Loading
Loading