forked from coracle/caravel
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 62dde4b2ac |
@@ -19,7 +19,7 @@ Members:
|
|||||||
|
|
||||||
## `async fn handle_activity(&self, activity: &Activity)`
|
## `async fn handle_activity(&self, activity: &Activity)`
|
||||||
|
|
||||||
- For `create_relay`, `update_relay`, or `deactivate_relay` activity, calls `sync_and_report`.
|
- For `create_relay`, `update_relay`, `activate_relay`, or `deactivate_relay` activity, calls `sync_and_report`.
|
||||||
- All other activity types are ignored (e.g. `fail_relay_sync`, `complete_relay_sync`).
|
- All other activity types are ignored (e.g. `fail_relay_sync`, `complete_relay_sync`).
|
||||||
|
|
||||||
## `async fn sync_and_report(&self, relay: &Relay, is_new: bool)`
|
## `async fn sync_and_report(&self, relay: &Relay, is_new: bool)`
|
||||||
|
|||||||
@@ -901,10 +901,7 @@ mod tests {
|
|||||||
&unknown_status_paid
|
&unknown_status_paid
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use sqlx::SqlitePool;
|
use sqlx::SqlitePool;
|
||||||
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
|
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
|
||||||
|
|||||||
@@ -209,8 +209,7 @@ impl Command {
|
|||||||
.execute(&mut *tx)
|
.execute(&mut *tx)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let activity =
|
let activity = Self::insert_activity(&mut tx, activity_type, "relay", relay_id).await?;
|
||||||
Self::insert_activity(&mut tx, "deactivate_relay", "relay", &relay_id).await?;
|
|
||||||
|
|
||||||
tx.commit().await?;
|
tx.commit().await?;
|
||||||
self.emit(activity);
|
self.emit(activity);
|
||||||
|
|||||||
+23
-7
@@ -56,10 +56,7 @@ impl Infra {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_activity(&self, activity: &Activity) -> Result<()> {
|
async fn handle_activity(&self, activity: &Activity) -> Result<()> {
|
||||||
let needs_sync = matches!(
|
let needs_sync = should_sync_relay_activity(activity.activity_type.as_str());
|
||||||
activity.activity_type.as_str(),
|
|
||||||
"create_relay" | "update_relay" | "deactivate_relay"
|
|
||||||
);
|
|
||||||
|
|
||||||
if needs_sync {
|
if needs_sync {
|
||||||
let Some(relay) = self.query.get_relay(&activity.resource_id).await? else {
|
let Some(relay) = self.query.get_relay(&activity.resource_id).await? else {
|
||||||
@@ -93,7 +90,9 @@ impl Infra {
|
|||||||
async fn nip98_auth(&self, url: &str, method: HttpMethod) -> Result<String> {
|
async fn nip98_auth(&self, url: &str, method: HttpMethod) -> Result<String> {
|
||||||
let keys = Keys::parse(&self.api_secret)?;
|
let keys = Keys::parse(&self.api_secret)?;
|
||||||
let server_url = Url::parse(url)?;
|
let server_url = Url::parse(url)?;
|
||||||
let auth = HttpData::new(server_url, method).to_authorization(&keys).await?;
|
let auth = HttpData::new(server_url, method)
|
||||||
|
.to_authorization(&keys)
|
||||||
|
.await?;
|
||||||
Ok(auth)
|
Ok(auth)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,11 +149,21 @@ impl Infra {
|
|||||||
let response = if is_new {
|
let response = if is_new {
|
||||||
let url = format!("{}/relay/{}", base, relay.id);
|
let url = format!("{}/relay/{}", base, relay.id);
|
||||||
let auth = self.nip98_auth(&url, HttpMethod::POST).await?;
|
let auth = self.nip98_auth(&url, HttpMethod::POST).await?;
|
||||||
client.post(&url).header("Authorization", auth).json(&body).send().await?
|
client
|
||||||
|
.post(&url)
|
||||||
|
.header("Authorization", auth)
|
||||||
|
.json(&body)
|
||||||
|
.send()
|
||||||
|
.await?
|
||||||
} else {
|
} else {
|
||||||
let url = format!("{}/relay/{}", base, relay.id);
|
let url = format!("{}/relay/{}", base, relay.id);
|
||||||
let auth = self.nip98_auth(&url, HttpMethod::PUT).await?;
|
let auth = self.nip98_auth(&url, HttpMethod::PUT).await?;
|
||||||
client.put(&url).header("Authorization", auth).json(&body).send().await?
|
client
|
||||||
|
.put(&url)
|
||||||
|
.header("Authorization", auth)
|
||||||
|
.json(&body)
|
||||||
|
.send()
|
||||||
|
.await?
|
||||||
};
|
};
|
||||||
|
|
||||||
if !response.status().is_success() {
|
if !response.status().is_success() {
|
||||||
@@ -165,3 +174,10 @@ impl Infra {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn should_sync_relay_activity(activity_type: &str) -> bool {
|
||||||
|
matches!(
|
||||||
|
activity_type,
|
||||||
|
"create_relay" | "update_relay" | "activate_relay" | "deactivate_relay"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user