Skip to content

Commit

Permalink
No need to require pubkey, we have it in rumor, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dcadenas committed Apr 4, 2024
1 parent 97d1f0b commit de05df3
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 48 deletions.
1 change: 1 addition & 0 deletions src/actors/gift_unwrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ impl Actor for GiftUnwrapper {
mod tests {
use super::*;
use crate::actors::TestActor;
use crate::domain_objects::as_gift_wrap::AsGiftWrap;
use ractor::{cast, Actor};
use serde_json::json;
use std::sync::Arc;
Expand Down
2 changes: 1 addition & 1 deletion src/bin/giftwrapper.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use nostr_sdk::prelude::*;
use reportinator_server::domain_objects::ReportRequest;
use reportinator_server::{AsGiftWrap, ReportRequest};
use std::env;
use std::io::{self, BufRead};
use std::str::FromStr;
Expand Down
2 changes: 2 additions & 0 deletions src/domain_objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ pub use gift_wrap::GiftWrappedReportRequest;
pub mod report_request;
pub use report_request::ReportRequest;

pub mod as_gift_wrap;

pub mod moderation_category;
pub use moderation_category::ModerationCategory;

Expand Down
54 changes: 54 additions & 0 deletions src/domain_objects/as_gift_wrap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use super::ReportRequest;
use crate::domain_objects::GiftWrappedReportRequest;
use anyhow::Result;
use nostr_sdk::prelude::*;

#[async_trait]
pub trait AsGiftWrap {
async fn as_gift_wrap(
&self,
reporter_keys: &Keys,
receiver_pubkey: &PublicKey,
) -> Result<GiftWrappedReportRequest>;
}

#[async_trait]
impl AsGiftWrap for ReportRequest {
// NOTE: This roughly creates a message as described by nip 17 but it's still
// not ready, just for testing purposes. There are more details to consider to
// properly implement the nip like created_at treatment. The nip itself is not
// finished at this time so hopefully in the future this can be done through the
// nostr crate.
async fn as_gift_wrap(
&self,
reporter_keys: &Keys,
receiver_pubkey: &PublicKey,
) -> Result<GiftWrappedReportRequest> {
if self.reporter_pubkey() != &reporter_keys.public_key() {
return Err(anyhow::anyhow!(
"Reporter public key doesn't match the provided keys"
));
}

let report_request_json =
serde_json::to_string(self).expect("Failed to serialize ReportRequest to JSON");
// Compose rumor
let kind_14_rumor =
EventBuilder::sealed_direct(receiver_pubkey.clone(), report_request_json)
.to_unsigned_event(reporter_keys.public_key());

// Compose seal
let content: String = NostrSigner::Keys(reporter_keys.clone())
.nip44_encrypt(receiver_pubkey.clone(), kind_14_rumor.as_json())
.await?;
let kind_13_seal = EventBuilder::new(Kind::Seal, content, []).to_event(&reporter_keys)?;

// Compose gift wrap
let expiration = None; // TODO
let kind_1059_gift_wrap: Event =
EventBuilder::gift_wrap_from_seal(&receiver_pubkey, &kind_13_seal, expiration)?;

let gift_wrap = GiftWrappedReportRequest::try_from(kind_1059_gift_wrap)?;
Ok(gift_wrap)
}
}
12 changes: 10 additions & 2 deletions src/domain_objects/gift_wrap.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::report_request::ReportRequestRumorContent;
use crate::domain_objects::ReportRequest;
use anyhow::{bail, Context, Result};
use nostr_sdk::prelude::*;
Expand All @@ -20,8 +21,15 @@ impl GiftWrappedReportRequest {
pub fn extract_report_request(&self, keys: &Keys) -> Result<ReportRequest> {
let unwrapped_gift = extract_rumor(keys, &self.0).context("Couldn't extract rumor")?;

let report_request = serde_json::from_str::<ReportRequest>(&unwrapped_gift.rumor.content)
.context("Failed to parse report request")?;
let report_request_rumor_content =
serde_json::from_str::<ReportRequestRumorContent>(&unwrapped_gift.rumor.content)
.context(format!(
"Failed to parse report request rumor content: {}",
unwrapped_gift.rumor.content
))?;

let report_request =
report_request_rumor_content.to_report_request(unwrapped_gift.rumor.pubkey);

if !report_request.valid() {
bail!("{} is not a valid gift wrapped report request", self.0.id());
Expand Down
53 changes: 12 additions & 41 deletions src/domain_objects/report_request.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
use super::{ModeratedReport, ModerationCategory};
use crate::domain_objects::GiftWrappedReportRequest;
use anyhow::Result;
use nostr_sdk::prelude::*;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use std::fmt::{self, Display, Formatter};

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ReportRequestRumorContent {
reported_event: Event,
reporter_text: Option<String>,
}
impl ReportRequestRumorContent {
pub fn to_report_request(self, pubkey: PublicKey) -> ReportRequest {
ReportRequest::new(self.reported_event, pubkey, self.reporter_text)
}
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ReportRequest {
Expand Down Expand Up @@ -41,10 +52,6 @@ impl ReportRequest {
self.reporter_text.as_ref()
}

pub fn to_json(&self) -> String {
serde_json::to_string(self).expect("Failed to serialize ReportRequest to JSON")
}

pub fn valid(&self) -> bool {
self.reported_event.verify().is_ok()
}
Expand All @@ -64,42 +71,6 @@ impl ReportRequest {
)?;
Ok(Some(moderated_report))
}

// NOTE: This roughly creates a message as described by nip 17 but it's still
// not ready, just for testing purposes. There are more details to consider to
// properly implement the nip like created_at treatment. The nip itself is not
// finished at this time so hopefully in the future this can be done through the
// nostr crate.
#[allow(unused)]
pub async fn as_gift_wrap(
&self,
reporter_keys: &Keys,
receiver_pubkey: &PublicKey,
) -> Result<GiftWrappedReportRequest> {
if self.reporter_pubkey() != &reporter_keys.public_key() {
return Err(anyhow::anyhow!(
"Reporter public key doesn't match the provided keys"
));
}

// Compose rumor
let kind_14_rumor = EventBuilder::sealed_direct(receiver_pubkey.clone(), self.to_json())
.to_unsigned_event(reporter_keys.public_key());

// Compose seal
let content: String = NostrSigner::Keys(reporter_keys.clone())
.nip44_encrypt(receiver_pubkey.clone(), kind_14_rumor.as_json())
.await?;
let kind_13_seal = EventBuilder::new(Kind::Seal, content, []).to_event(&reporter_keys)?;

// Compose gift wrap
let expiration = None; // TODO
let kind_1059_gift_wrap: Event =
EventBuilder::gift_wrap_from_seal(&receiver_pubkey, &kind_13_seal, expiration)?;

let gift_wrap = GiftWrappedReportRequest::try_from(kind_1059_gift_wrap)?;
Ok(gift_wrap)
}
}

impl Display for ReportRequest {
Expand Down
7 changes: 3 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod actors;
pub mod adapters;
pub mod domain_objects;
pub mod service_manager;
mod domain_objects;
pub use crate::domain_objects::as_gift_wrap::AsGiftWrap;
pub use crate::domain_objects::report_request::ReportRequest;

0 comments on commit de05df3

Please sign in to comment.