mostro_client/cli/
rate_user.rs

1use anyhow::Result;
2use mostro_core::prelude::*;
3use nostr_sdk::prelude::*;
4use uuid::Uuid;
5
6const RATING_BOUNDARIES: [u8; 5] = [1, 2, 3, 4, 5];
7
8use crate::{cli::Context, db::Order, util::send_dm};
9
10// Get the user rate
11fn get_user_rate(rating: &u8) -> Result<Payload> {
12    if let Some(rating) = RATING_BOUNDARIES.iter().find(|r| r == &rating) {
13        Ok(Payload::RatingUser(*rating))
14    } else {
15        Err(anyhow::anyhow!("Rating must be in the range 1 - 5"))
16    }
17}
18
19pub async fn execute_rate_user(order_id: &Uuid, rating: &u8, ctx: &Context) -> Result<()> {
20    // Check boundaries
21    let rating_content = get_user_rate(rating)?;
22
23    // Get the trade keys
24    let trade_keys =
25        if let Ok(order_to_vote) = Order::get_by_id(&ctx.pool, &order_id.to_string()).await {
26            match order_to_vote.trade_keys.as_ref() {
27                Some(trade_keys) => Keys::parse(trade_keys)?,
28                None => {
29                    return Err(anyhow::anyhow!("No trade_keys found for this order"));
30                }
31            }
32        } else {
33            return Err(anyhow::anyhow!("order {} not found", order_id));
34        };
35
36    // Create rating message of counterpart
37    let rate_message = Message::new_order(
38        Some(*order_id),
39        None,
40        None,
41        Action::RateUser,
42        Some(rating_content),
43    )
44    .as_json()
45    .map_err(|_| anyhow::anyhow!("Failed to serialize message"))?;
46
47    send_dm(
48        &ctx.client,
49        Some(&ctx.identity_keys),
50        &trade_keys,
51        &ctx.mostro_pubkey,
52        rate_message,
53        None,
54        false,
55    )
56    .await?;
57
58    Ok(())
59}