ore_types/
response.rs

1use chrono::NaiveDateTime;
2use serde::{Deserialize, Serialize};
3use solana_sdk::pubkey::Pubkey;
4
5#[cfg(feature = "redis")]
6use redis_derive::{FromRedisValue, ToRedisArgs};
7
8/// Response for a successful login, containing the JWT.
9#[derive(Serialize, Deserialize, Debug, Clone)]
10pub struct AuthResponse {
11    pub token: String,
12}
13
14/// Response for a successful supply endpoint.
15#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
16pub struct SupplyResponse {
17    pub result: String,
18}
19
20/// Response after successfully sending a chat message.
21#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
22pub struct ChatSendMessageResponse {
23    pub status: String,
24    pub message: String,
25}
26
27#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
28pub struct User {
29    pub authority: String,
30    pub username: String,
31    pub profile_photo_url: Option<String>,
32    pub discord_user: Option<DiscordUser>,
33    pub updated_at: NaiveDateTime,
34    pub risk_score: i64,
35    pub is_banned: bool,
36    pub role: Option<String>,
37}
38
39// Notifications
40#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
41#[cfg_attr(feature = "redis", derive(FromRedisValue, ToRedisArgs))]
42pub struct ChatNotification {
43    pub authority: String,
44    pub username: String,
45    pub text: String,
46    pub id: u64,
47    pub ts: i64,
48    pub profile_photo_url: Option<String>,
49    pub role: Option<String>,
50    pub discord_user_id: Option<String>,
51    // Reply fields (optional - null if not a reply)
52    #[serde(default, skip_serializing_if = "Option::is_none")]
53    pub reply_to_id: Option<u64>,
54    #[serde(default, skip_serializing_if = "Option::is_none")]
55    pub reply_to_text: Option<String>,
56    #[serde(default, skip_serializing_if = "Option::is_none")]
57    pub reply_to_username: Option<String>,
58    // Reaction counts (optional - for backwards compatibility)
59    #[serde(default, skip_serializing_if = "Option::is_none")]
60    pub reactions: Option<ChatReactions>,
61}
62
63/// Reaction counts for a chat message.
64/// Uses named fields instead of emoji keys for type safety and Redis compatibility.
65#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
66#[cfg_attr(feature = "redis", derive(FromRedisValue, ToRedisArgs))]
67pub struct ChatReactions {
68    #[serde(default, skip_serializing_if = "is_zero")]
69    pub thumbs_up: u32, // 👍
70    #[serde(default, skip_serializing_if = "is_zero")]
71    pub heart: u32, // ❤️
72    #[serde(default, skip_serializing_if = "is_zero")]
73    pub laughing: u32, // 😂
74    #[serde(default, skip_serializing_if = "is_zero")]
75    pub surprised: u32, // 😮
76    #[serde(default, skip_serializing_if = "is_zero")]
77    pub sad: u32, // 😢
78    #[serde(default, skip_serializing_if = "is_zero")]
79    pub fire: u32, // 🔥
80}
81
82fn is_zero(n: &u32) -> bool {
83    *n == 0
84}
85
86impl ChatReactions {
87    /// Check if all reaction counts are zero.
88    pub fn is_empty(&self) -> bool {
89        self.thumbs_up == 0
90            && self.heart == 0
91            && self.laughing == 0
92            && self.surprised == 0
93            && self.sad == 0
94            && self.fire == 0
95    }
96
97    /// Get the count for a specific emoji.
98    pub fn get(&self, emoji: &str) -> u32 {
99        match emoji {
100            "👍" => self.thumbs_up,
101            "❤️" | "❤" => self.heart,
102            "😂" => self.laughing,
103            "😮" => self.surprised,
104            "😢" => self.sad,
105            "🔥" => self.fire,
106            _ => 0,
107        }
108    }
109
110    /// Set the count for a specific emoji.
111    pub fn set(&mut self, emoji: &str, count: u32) {
112        match emoji {
113            "👍" => self.thumbs_up = count,
114            "❤️" | "❤" => self.heart = count,
115            "😂" => self.laughing = count,
116            "😮" => self.surprised = count,
117            "😢" => self.sad = count,
118            "🔥" => self.fire = count,
119            _ => {}
120        }
121    }
122}
123
124/// Allowed reaction emoji.
125pub const ALLOWED_REACTION_EMOJI: [&str; 6] = ["👍", "❤️", "😂", "😮", "😢", "🔥"];
126
127/// Check if an emoji is in the allowed reaction set.
128pub fn is_valid_reaction_emoji(emoji: &str) -> bool {
129    // Handle heart emoji variant (with and without variation selector)
130    if emoji == "❤" {
131        return true;
132    }
133    ALLOWED_REACTION_EMOJI.contains(&emoji)
134}
135
136#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
137pub struct ResetNotification {
138    pub block_id: u64,
139}
140
141/// Notification for a deploy event, containing all relevant data for clients.
142#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
143#[cfg_attr(feature = "redis", derive(FromRedisValue, ToRedisArgs))]
144pub struct DeployNotification {
145    /// The authority (miner) who deployed.
146    pub authority: String,
147    /// The amount of SOL deployed (in lamports).
148    pub amount: u64,
149    /// The mask (bitmask of selected squares).
150    pub mask: u64,
151    /// The round ID this deployment is for.
152    pub round_id: u64,
153    /// The signer of the transaction.
154    pub signer: String,
155    /// The strategy used for deployment.
156    pub strategy: u64,
157    /// Total squares selected.
158    pub total_squares: u64,
159    /// Timestamp of the deployment.
160    pub ts: i64,
161    /// Transaction signature.
162    pub sig: String,
163}
164
165/// Notification for a reaction being added or removed from a chat message.
166#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
167pub struct ReactionNotification {
168    pub message_id: u64,
169    pub emoji: String,
170    pub count: u32,
171    pub action: String, // "added" or "removed"
172}
173
174/// A user currently typing in chat.
175#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
176pub struct TypingUser {
177    pub authority: String,
178    pub username: String,
179}
180
181/// Notification for typing indicator updates.
182#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
183pub struct TypingNotification {
184    pub users: Vec<TypingUser>,
185}
186
187#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
188pub enum Notification {
189    Chat(ChatNotification),
190    Reset(ResetNotification),
191    Deploy(DeployNotification),
192    Reaction(ReactionNotification),
193    Typing(TypingNotification),
194}
195
196impl Notification {
197    pub fn id(&self) -> String {
198        match self {
199            Notification::Chat(chat) => chat.id.to_string(),
200            Notification::Reset(reset) => reset.block_id.to_string(),
201            Notification::Deploy(deploy) => deploy.sig.clone(),
202            Notification::Reaction(reaction) => {
203                format!("{}:{}", reaction.message_id, reaction.emoji)
204            }
205            Notification::Typing(_) => "typing".to_string(),
206        }
207    }
208}
209
210/// Response after adding/removing a reaction.
211#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
212pub struct ChatReactResponse {
213    pub status: String,
214    pub action: String,     // "added" or "removed"
215    pub message_id: u64,
216    pub emoji: String,
217    pub count: u32,         // new count for this emoji on this message
218}
219
220/// Response for typing indicator endpoint.
221#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
222pub struct ChatTypingResponse {
223    pub status: String,
224}
225
226#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
227pub struct DailyRevenue {
228    pub day: String,
229    pub revenue: i64,
230}
231
232// Username validation
233
234#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
235pub struct UsernameValidationResponse {
236    pub valid: bool,
237    pub error: Option<String>,
238}
239
240impl UsernameValidationResponse {
241    pub fn valid() -> Self {
242        Self {
243            valid: true,
244            error: None,
245        }
246    }
247
248    pub fn invalid(error: String) -> Self {
249        Self {
250            valid: false,
251            error: Some(error),
252        }
253    }
254}
255
256/// Response for a successful Discord authentication.
257#[derive(Serialize, Deserialize, Debug, Clone)]
258pub struct DiscordAuthResponse {
259    pub access_token: String,
260}
261
262/// Google user information returned after authentication.
263#[derive(Serialize, Deserialize, Debug, Clone)]
264pub struct GoogleUser {
265    pub email: String,
266    pub name: String,
267    pub picture: Option<String>,
268}
269
270/// Response for a successful Google authentication.
271#[derive(Serialize, Deserialize, Debug, Clone)]
272pub struct GoogleAuthResponse {
273    pub jwt: String,
274    pub user: GoogleUser,
275}
276
277#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
278pub struct DiscordUser {
279    pub id: String,
280    pub username: String,
281    pub discriminator: String,
282    #[serde(default, skip_serializing_if = "Option::is_none")]
283    pub global_name: Option<String>,
284    #[serde(default, skip_serializing_if = "Option::is_none")]
285    pub avatar: Option<String>,
286    #[serde(default, skip_serializing_if = "Option::is_none")]
287    pub verified: Option<bool>,
288    #[serde(default, skip_serializing_if = "Option::is_none")]
289    pub email: Option<String>,
290}
291
292#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
293pub struct OreBalance {
294    pub wallet: u64,
295    pub staked: u64,
296    pub unrefined: u64,
297    pub refined: u64,
298    pub lifetime_deployed_sol: u64,
299}
300
301#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
302pub struct LeaderboardEntry {
303    pub authority: String,
304    pub amount: u64,
305    pub username: Option<String>,
306    pub profile_picture_url: Option<String>,
307}
308
309/// Response type for reset events with enriched top miner user info.
310/// Maintains field order matching ore_api::event::ResetEvent for backwards compatibility.
311#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
312pub struct ResetEventResponse {
313    pub disc: u8,
314    pub round_id: u64,
315    pub start_slot: u64,
316    pub end_slot: u64,
317    pub winning_square: u64,
318    pub top_miner: Pubkey,
319    pub num_winners: u64,
320    pub motherlode: u64,
321    pub total_deployed: u64,
322    pub total_vaulted: u64,
323    pub total_winnings: u64,
324    pub total_minted: u64,
325    pub ts: i64,
326    pub rng: u64,
327    pub deployed_winning_square: u64,
328    pub top_miner_username: Option<String>,
329    pub top_miner_profile_photo: Option<String>,
330}
331
332/// Response type for a user's deploy history event.
333#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
334pub struct DeployHistoryEvent {
335    pub sig: String,
336    pub authority: String,
337    pub signer: String,
338    pub amount: u64,
339    pub mask: i64,
340    pub round_id: i64,
341    pub total_squares: i64,
342    pub ts: i64,
343    pub winning_square: i64,
344    pub top_miner: String,
345    pub rewards_sol: u64,
346    pub rewards_ore: u64,
347    pub total_winnings_sol: u64,
348    pub deployed_winning_square: u64,
349    pub motherlode: u64,
350}
351
352/// Response type for a winner in a round, with user data and betting pattern.
353#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
354pub struct RoundWinner {
355    /// The miner's public key.
356    pub authority: String,
357    /// User's display name (if set).
358    pub username: Option<String>,
359    /// User's profile photo URL (if set).
360    pub profile_photo_url: Option<String>,
361    /// Amount deployed on the winning square (in lamports).
362    pub deployed_on_winning: u64,
363    /// Combined mask of all squares the user deployed to (bitmask).
364    pub combined_mask: u64,
365}
366
367/// Response type for listing winners in a round.
368#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
369pub struct RoundWinnersResponse {
370    /// The round ID.
371    pub round_id: u64,
372    /// The winning square (0-24).
373    pub winning_square: u64,
374    /// Total amount deployed on the winning square by all miners.
375    pub total_on_winning: u64,
376    /// Total winnings for the round (in lamports).
377    pub total_winnings: u64,
378    /// List of winners sorted by deployed_on_winning descending.
379    pub winners: Vec<RoundWinner>,
380}