This repository was archived by the owner on Feb 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathnotifications.rs
More file actions
104 lines (92 loc) · 3.16 KB
/
Copy pathnotifications.rs
File metadata and controls
104 lines (92 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use crate::auth::MutinyAuthClient;
use crate::{error::MutinyError, logging::MutinyLogger};
use anyhow::anyhow;
use lightning::util::logger::*;
use lightning::{log_error, log_info};
use nostr::secp256k1::XOnlyPublicKey;
use reqwest::{Method, Url};
use serde_json::{json, Value};
use std::sync::Arc;
#[derive(Clone)]
pub struct MutinyNotificationClient {
auth_client: Option<Arc<MutinyAuthClient>>,
client: Option<reqwest::Client>,
url: String,
id: Option<String>,
pub logger: Arc<MutinyLogger>,
}
impl MutinyNotificationClient {
pub fn new_authenticated(
auth_client: Arc<MutinyAuthClient>,
url: String,
logger: Arc<MutinyLogger>,
) -> Self {
log_info!(logger, "Creating authenticated notification client");
Self {
auth_client: Some(auth_client),
client: None,
url,
id: None, // we get this from the auth client
logger,
}
}
pub fn new_unauthenticated(
url: String,
identifier_key: String,
logger: Arc<MutinyLogger>,
) -> Self {
log_info!(logger, "Creating unauthenticated notification client");
Self {
auth_client: None,
client: Some(reqwest::Client::new()),
url,
id: Some(identifier_key),
logger,
}
}
async fn make_request(
&self,
method: Method,
url: Url,
body: Option<Value>,
) -> Result<reqwest::Response, MutinyError> {
match (self.auth_client.as_ref(), self.client.as_ref()) {
(Some(auth), _) => auth.request(method, url, body).await,
(None, Some(client)) => {
let mut request = client.request(method, url);
if let Some(body) = body {
request = request.json(&body);
}
request.send().await.map_err(|e| {
log_error!(self.logger, "Error making request: {e}");
MutinyError::Other(anyhow!("Error making request: {e}"))
})
}
(None, None) => unreachable!("No auth client or http client"),
}
}
pub async fn register(&self, info: Value) -> Result<(), MutinyError> {
let url = Url::parse(&format!("{}/register", self.url)).map_err(|e| {
log_error!(self.logger, "Error parsing register url: {e}");
MutinyError::InvalidArgumentsError
})?;
let body = json!({"id": self.id, "info": info});
self.make_request(Method::POST, url, Some(body)).await?;
Ok(())
}
pub async fn register_nwc(
&self,
author: XOnlyPublicKey,
tagged: XOnlyPublicKey,
relay: &str,
name: &str,
) -> Result<(), MutinyError> {
let url = Url::parse(&format!("{}/register-nwc", self.url)).map_err(|e| {
log_error!(self.logger, "Error parsing register url: {e}");
MutinyError::InvalidArgumentsError
})?;
let body = json!({"id": self.id, "author": author, "tagged": tagged, "relay": relay, "name": name});
self.make_request(Method::POST, url, Some(body)).await?;
Ok(())
}
}