mostro_core/rating.rs
1//! Encoding of user reputation as Nostr event tags.
2//!
3//! Mostro publishes user reputation as addressable Nostr events of kind
4//! [`NOSTR_RATING_EVENT_KIND`](crate::prelude::NOSTR_RATING_EVENT_KIND). The
5//! [`Rating`] struct in this module mirrors the tag set used on those events
6//! and provides helpers to serialize to / deserialize from both JSON and
7//! `nostr_sdk::Tags`.
8
9use nostr_sdk::prelude::*;
10use serde::{Deserialize, Serialize};
11
12use crate::error::ServiceError;
13
14/// User reputation snapshot, suitable for publishing as Nostr tags.
15///
16/// The fields are the same aggregates stored on [`crate::user::User`], but
17/// typed for transport (unsigned integers for counts, `u8` for rating values).
18#[derive(Debug, Deserialize, Serialize, Clone)]
19pub struct Rating {
20 /// Total number of ratings received.
21 pub total_reviews: u64,
22 /// Weighted rating average across all reviews.
23 pub total_rating: f64,
24 /// Most recent rating, in the `MIN_RATING..=MAX_RATING` range.
25 pub last_rating: u8,
26 /// Highest rating ever received.
27 pub max_rate: u8,
28 /// Lowest rating ever received.
29 pub min_rate: u8,
30}
31
32impl Rating {
33 /// Construct a new [`Rating`] from its individual components.
34 pub fn new(
35 total_reviews: u64,
36 total_rating: f64,
37 last_rating: u8,
38 min_rate: u8,
39 max_rate: u8,
40 ) -> Self {
41 Self {
42 total_reviews,
43 total_rating,
44 last_rating,
45 min_rate,
46 max_rate,
47 }
48 }
49
50 /// Parse a [`Rating`] from its JSON representation.
51 ///
52 /// Returns [`ServiceError::MessageSerializationError`] if `json` is not a
53 /// valid serialization of this type.
54 pub fn from_json(json: &str) -> Result<Self, ServiceError> {
55 serde_json::from_str(json).map_err(|_| ServiceError::MessageSerializationError)
56 }
57
58 /// Serialize the rating to a JSON string.
59 pub fn as_json(&self) -> Result<String, ServiceError> {
60 serde_json::to_string(&self).map_err(|_| ServiceError::MessageSerializationError)
61 }
62
63 /// Encode the rating as a set of Nostr tags, ready to attach to an event.
64 ///
65 /// The returned [`Tags`] value contains one entry per numeric field plus
66 /// a `z` marker tag identifying the payload as a rating.
67 pub fn to_tags(&self) -> Result<Tags> {
68 let tags = vec![
69 Tag::custom(
70 TagKind::Custom(std::borrow::Cow::Borrowed("total_reviews")),
71 vec![self.total_reviews.to_string()],
72 ),
73 Tag::custom(
74 TagKind::Custom(std::borrow::Cow::Borrowed("total_rating")),
75 vec![self.total_rating.to_string()],
76 ),
77 Tag::custom(
78 TagKind::Custom(std::borrow::Cow::Borrowed("last_rating")),
79 vec![self.last_rating.to_string()],
80 ),
81 Tag::custom(
82 TagKind::Custom(std::borrow::Cow::Borrowed("max_rate")),
83 vec![self.max_rate.to_string()],
84 ),
85 Tag::custom(
86 TagKind::Custom(std::borrow::Cow::Borrowed("min_rate")),
87 vec![self.min_rate.to_string()],
88 ),
89 Tag::custom(
90 TagKind::Custom(std::borrow::Cow::Borrowed("z")),
91 vec!["rating".to_string()],
92 ),
93 ];
94
95 let tags = Tags::from_list(tags);
96
97 Ok(tags)
98 }
99
100 /// Rebuild a [`Rating`] from a set of Nostr tags previously produced by
101 /// [`Rating::to_tags`].
102 ///
103 /// Unknown tag keys are ignored so that the function keeps working if the
104 /// server adds new metadata fields. Returns a [`ServiceError`] if a
105 /// required key carries a non-parseable value.
106 pub fn from_tags(tags: Tags) -> Result<Self, ServiceError> {
107 let mut total_reviews = 0;
108 let mut total_rating = 0.0;
109 let mut last_rating = 0;
110 let mut max_rate = 0;
111 let mut min_rate = 0;
112
113 for tag in tags.into_iter() {
114 let t = tag.to_vec();
115 let key = t
116 .first()
117 .ok_or_else(|| ServiceError::NostrError("Missing tag key".to_string()))?;
118 let value = t
119 .get(1)
120 .ok_or_else(|| ServiceError::NostrError("Missing tag value".to_string()))?;
121 match key.as_str() {
122 "total_reviews" => {
123 total_reviews = value
124 .parse::<u64>()
125 .map_err(|_| ServiceError::ParsingNumberError)?
126 }
127 "total_rating" => {
128 total_rating = value
129 .parse::<f64>()
130 .map_err(|_| ServiceError::ParsingNumberError)?
131 }
132 "last_rating" => {
133 last_rating = value
134 .parse::<u8>()
135 .map_err(|_| ServiceError::ParsingNumberError)?
136 }
137 "max_rate" => {
138 max_rate = value
139 .parse::<u8>()
140 .map_err(|_| ServiceError::ParsingNumberError)?
141 }
142 "min_rate" => {
143 min_rate = value
144 .parse::<u8>()
145 .map_err(|_| ServiceError::ParsingNumberError)?
146 }
147 _ => {}
148 }
149 }
150
151 Ok(Self {
152 total_reviews,
153 total_rating,
154 last_rating,
155 max_rate,
156 min_rate,
157 })
158 }
159}