1use serde::{Deserialize, Serialize};
2use serde_json::json;
3
4use crate::authentification::{CredentialsOrApiKey, Profile};
5use crate::error::Result;
6use crate::http::{client, encode, ok};
7use crate::parrot::chat::LastMessage;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
12#[serde(rename_all = "lowercase")]
13pub enum OwnerKind {
14 Group,
15 Friend,
16 Partnership,
17}
18
19#[derive(Debug, Clone, Serialize, Deserialize)]
21pub struct OwnerGroup {
22 pub id: String,
23 pub name: String,
24}
25
26#[derive(Debug, Clone, Serialize, Deserialize)]
28pub struct Owner {
29 pub kind: OwnerKind,
30 pub id: String,
32 pub name: String,
34 #[serde(default)]
37 pub trust: Option<f64>,
38 #[serde(default, skip_serializing_if = "Option::is_none")]
40 pub group: Option<OwnerGroup>,
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize)]
45pub struct Chat {
46 pub id: String,
47 pub name: String,
48 #[serde(default)]
50 pub human: bool,
51 pub owner: Owner,
52 #[serde(default)]
55 pub unread: u32,
56 pub updated: i64,
58 #[serde(default)]
59 pub last: Option<LastMessage>,
60}
61
62#[derive(Debug, Clone, Serialize, Deserialize)]
64pub struct Message {
65 pub position: u32,
66 pub role: String,
67 pub text: String,
68 #[serde(default, skip_serializing_if = "Option::is_none")]
69 pub author: Option<Profile>,
70 #[serde(default, skip_serializing_if = "Option::is_none")]
72 pub via: Option<String>,
73 pub created: i64,
75}
76
77#[derive(Debug, Clone, Serialize, Deserialize)]
79pub struct Unread {
80 pub total: u32,
81 #[serde(default)]
82 pub chats: Vec<Chat>,
83}
84
85#[derive(Debug, Clone, Serialize, Deserialize)]
87pub struct CaughtUpChat {
88 pub id: String,
89 pub name: String,
90 #[serde(default)]
91 pub human: bool,
92 pub owner: Owner,
93 pub updated: i64,
94 #[serde(default)]
95 pub messages: Vec<Message>,
96}
97
98#[derive(Debug, Clone, Serialize, Deserialize)]
99pub struct CaughtUp {
100 #[serde(default)]
101 pub chats: Vec<CaughtUpChat>,
102 pub more: bool,
104 pub unread: u32,
106}
107
108#[derive(Debug, Clone, Serialize, Deserialize)]
110pub struct ChatReference {
111 pub id: String,
112 pub name: String,
113 pub owner: Owner,
114}
115
116#[derive(Debug, Clone, Serialize, Deserialize)]
117pub struct Found {
118 pub chat: ChatReference,
119 pub message: Message,
120}
121
122#[derive(Debug, Clone, Copy, Default)]
124pub struct UnreadOptions {
125 pub min_trust: Option<f64>,
127}
128
129#[derive(Debug, Clone, Copy, Default)]
131pub struct CatchUpOptions {
132 pub min_trust: Option<f64>,
134 pub max: Option<u32>,
136}
137
138#[derive(Debug, Clone, Copy, Default)]
140pub struct SearchOptions {
141 pub cursor: Option<u32>,
143 pub max: Option<u32>,
145}
146
147pub async fn list(authorization: &CredentialsOrApiKey) -> Result<Vec<Chat>> {
151 let response = client()
152 .get(format!("{}/chats/shared", crate::get_url()))
153 .header(
154 "Authorization",
155 match authorization {
156 CredentialsOrApiKey::Credentials(credentials) => {
157 format!("Token {}", credentials.token)
158 }
159 CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
160 },
161 )
162 .send()
163 .await?;
164 Ok(ok(response).await?.json().await?)
165}
166
167pub async fn unread(authorization: &CredentialsOrApiKey, options: UnreadOptions) -> Result<Unread> {
170 let query = options
171 .min_trust
172 .map(|min_trust| format!("?minTrust={}", encode(&min_trust.to_string())))
173 .unwrap_or_default();
174 let response = client()
175 .get(format!("{}/chats/unread{}", crate::get_url(), query))
176 .header(
177 "Authorization",
178 match authorization {
179 CredentialsOrApiKey::Credentials(credentials) => {
180 format!("Token {}", credentials.token)
181 }
182 CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
183 },
184 )
185 .send()
186 .await?;
187 Ok(ok(response).await?.json().await?)
188}
189
190pub async fn catch_up(
195 authorization: &CredentialsOrApiKey,
196 options: CatchUpOptions,
197) -> Result<CaughtUp> {
198 let response = client()
199 .post(format!("{}/chats/catch-up", crate::get_url()))
200 .header(
201 "Authorization",
202 match authorization {
203 CredentialsOrApiKey::Credentials(credentials) => {
204 format!("Token {}", credentials.token)
205 }
206 CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
207 },
208 )
209 .json(&json!({ "minTrust": options.min_trust, "max": options.max }))
210 .send()
211 .await?;
212 Ok(ok(response).await?.json().await?)
213}
214
215pub async fn search(
219 authorization: &CredentialsOrApiKey,
220 query: &str,
221 options: SearchOptions,
222) -> Result<Vec<Found>> {
223 let response = client()
224 .post(format!("{}/chats/search", crate::get_url()))
225 .header(
226 "Authorization",
227 match authorization {
228 CredentialsOrApiKey::Credentials(credentials) => {
229 format!("Token {}", credentials.token)
230 }
231 CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
232 },
233 )
234 .json(&json!({ "query": query, "cursor": options.cursor, "max": options.max }))
235 .send()
236 .await?;
237 Ok(ok(response).await?.json().await?)
238}
239
240pub async fn post(authorization: &CredentialsOrApiKey, chat: &str, text: &str) -> Result<()> {
245 let opened = client()
246 .post(format!("{}/chat/live/open", crate::parrot::get_url()))
247 .header(
248 "Authorization",
249 match authorization {
250 CredentialsOrApiKey::Credentials(credentials) => {
251 format!("Token {}", credentials.token)
252 }
253 CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
254 },
255 )
256 .json(&json!({ "chatId": chat }))
257 .send()
258 .await?;
259 ok(opened).await?;
260 let response = client()
261 .post(format!("{}/chat/live/append", crate::parrot::get_url()))
262 .header(
263 "Authorization",
264 match authorization {
265 CredentialsOrApiKey::Credentials(credentials) => {
266 format!("Token {}", credentials.token)
267 }
268 CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
269 },
270 )
271 .header("Server", format!("parrot:chat:{chat}"))
272 .json(&json!({ "role": "USER", "parts": [{ "type": "TEXT", "text": text }] }))
273 .send()
274 .await?;
275 ok(response).await?;
276 Ok(())
277}