summaryrefslogtreecommitdiff
path: root/src/push/app.rs
diff options
context:
space:
mode:
authorKit La Touche <kit@transneptune.net>2025-07-30 23:08:40 -0400
committerKit La Touche <kit@transneptune.net>2025-07-30 23:08:40 -0400
commited5e175a806f45469a6e5504ba0d3f5246997fad (patch)
tree0d4233c57596186b86d165640ca4721e7495567d /src/push/app.rs
parentb63380b251d04dd92f06aa5bbc22a72ca3e4bf8e (diff)
Test receiving push events when backgrounded
And thus also displaying notifications.
Diffstat (limited to 'src/push/app.rs')
-rw-r--r--src/push/app.rs40
1 files changed, 32 insertions, 8 deletions
diff --git a/src/push/app.rs b/src/push/app.rs
index 2d6e15c..ed8bf31 100644
--- a/src/push/app.rs
+++ b/src/push/app.rs
@@ -46,18 +46,42 @@ impl<'a> Push<'a> {
Ok(id)
}
+ pub async fn broadcast(
+ &self,
+ message: &str,
+ ) -> Result<(), EchoError> {
+ let mut tx = self.db.begin().await?;
+ let subscriptions = tx
+ .subscriptions()
+ .all()
+ .await?;
+
+ tx.commit().await?;
+
+ for subscription in subscriptions {
+ // We don't care if any of these error, for now.
+ // Eventually, we should remove rows that cause certain error conditions.
+ println!("Sending to {:#?}", subscription.info.endpoint);
+ self.send(&subscription.info, message).await.unwrap_or_else(|err| {
+ println!("Error with {:#?}: {}", subscription.info.endpoint, err);
+ })
+ }
+
+ Ok(())
+ }
+
pub async fn echo(
&self,
user: &User,
- subscription: &Id,
+ endpoint: &String,
message: &str,
) -> Result<(), EchoError> {
let mut tx = self.db.begin().await?;
let subscription = tx
.subscriptions()
- .by_id(subscription)
+ .by_endpoint(endpoint)
.await
- .not_found(|| EchoError::NotFound(subscription.clone()))?;
+ .not_found(|| EchoError::NotFound(endpoint.clone()))?;
if subscription.user != user.id {
return Err(EchoError::NotSubscriber(subscription.id, user.id.clone()));
}
@@ -89,13 +113,13 @@ impl<'a> Push<'a> {
Ok(())
}
- pub async fn unregister(&self, user: &User, subscription: &Id) -> Result<(), UnregisterError> {
+ pub async fn unregister(&self, user: &User, endpoint: &String) -> Result<(), UnregisterError> {
let mut tx = self.db.begin().await?;
let subscription = tx
.subscriptions()
- .by_id(subscription)
+ .by_endpoint(endpoint)
.await
- .not_found(|| UnregisterError::NotFound(subscription.clone()))?;
+ .not_found(|| UnregisterError::NotFound(endpoint.clone()))?;
if subscription.user != user.id {
return Err(UnregisterError::NotSubscriber(
subscription.id,
@@ -118,7 +142,7 @@ pub enum RegisterError {
#[derive(Debug, thiserror::Error)]
pub enum EchoError {
#[error("subscription {0} not found")]
- NotFound(Id),
+ NotFound(String),
#[error("user {1} is not the subscriber for subscription {0}")]
NotSubscriber(Id, user::Id),
#[error(transparent)]
@@ -130,7 +154,7 @@ pub enum EchoError {
#[derive(Debug, thiserror::Error)]
pub enum UnregisterError {
#[error("subscription {0} not found")]
- NotFound(Id),
+ NotFound(String),
#[error("user {1} is not the subscriber for subscription {0}")]
NotSubscriber(Id, user::Id),
#[error(transparent)]