nextcloud-talk 0.1.3

Nextcloud Talk (Spreed) API client and domain types
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//! Asynchronous client for Nextcloud Talk (Spreed) API.
//!
//! Official documentation:
//! - [Nextcloud Talk API documentation](https://nextcloud-talk.readthedocs.io/en/latest/)

use nextcloud_ocs::{OcsClient, OcsFormat, Query};
use nextcloud_sdk_core::{ApiPath, AuthProvider, Error, Transport};
use serde::Serialize;

use crate::message::ChatMessage;
use crate::room::Room;
use crate::room_type::RoomType;

/// Relative path to Nextcloud Talk room API beneath `/ocs/v2.php`.
pub const ROOM_API_PATH: &str = "apps/spreed/api/v4/room";

/// Relative path to Nextcloud Talk chat API beneath `/ocs/v2.php`.
pub const CHAT_API_PATH: &str = "apps/spreed/api/v1/chat";

/// Asynchronous client for Nextcloud Talk (Spreed) API.
///
/// Wraps an [`OcsClient`] by reference and provides high-level typed calls for:
/// - Listing conversations / rooms (`/ocs/v2.php/apps/spreed/api/v4/room`)
/// - Fetching conversation details by token (`/ocs/v2.php/apps/spreed/api/v4/room/{token}`)
/// - Creating conversations (`/ocs/v2.php/apps/spreed/api/v4/room`)
/// - Deleting conversations (`/ocs/v2.php/apps/spreed/api/v4/room/{token}`)
/// - Listing chat messages (`/ocs/v2.php/apps/spreed/api/v1/chat/{token}`)
/// - Sending chat messages (`/ocs/v2.php/apps/spreed/api/v1/chat/{token}`)
#[derive(Clone, Copy, Debug)]
pub struct TalkClient<'a, T: Transport, A: AuthProvider> {
    ocs: &'a OcsClient<T, A>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct CreateRoomRequest<'a> {
    room_type: RoomType,
    #[serde(skip_serializing_if = "Option::is_none")]
    invite: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    room_name: Option<&'a str>,
}

#[derive(Serialize)]
struct SendMessageRequest<'a> {
    message: &'a str,
}

impl<'a, T: Transport, A: AuthProvider> TalkClient<'a, T, A> {
    /// Binds a talk client to an underlying [`OcsClient`].
    pub const fn new(ocs: &'a OcsClient<T, A>) -> Self {
        Self { ocs }
    }

    /// Borrows the underlying [`OcsClient`].
    pub const fn ocs(&self) -> &'a OcsClient<T, A> {
        self.ocs
    }

    /// Lists all conversations the authenticated user belongs to.
    ///
    /// Sends `GET /ocs/v2.php/apps/spreed/api/v4/room`.
    pub async fn list_rooms(&self) -> Result<Vec<Room>, Error> {
        tracing::debug!("Listing Talk rooms");
        let path = ApiPath::ocs(ROOM_API_PATH)?;
        self.ocs
            .get(path, Query::new().format(OcsFormat::Json))
            .await
    }

    /// Retrieves conversation details for a specific room token.
    ///
    /// Sends `GET /ocs/v2.php/apps/spreed/api/v4/room/{token}`.
    pub async fn get_room(&self, token: &str) -> Result<Room, Error> {
        tracing::debug!(token, "Fetching Talk room details");
        let path = ApiPath::ocs(format!("{ROOM_API_PATH}/{token}"))?;
        self.ocs
            .get(path, Query::new().format(OcsFormat::Json))
            .await
    }

    /// Creates a new conversation room.
    ///
    /// Sends `POST /ocs/v2.php/apps/spreed/api/v4/room`.
    pub async fn create_room(
        &self,
        room_type: RoomType,
        invite: Option<&str>,
        name: Option<&str>,
    ) -> Result<Room, Error> {
        tracing::info!(?room_type, ?name, "Creating Talk room");
        let path = ApiPath::ocs(ROOM_API_PATH)?;
        let request = CreateRoomRequest {
            room_type,
            invite,
            room_name: name,
        };
        self.ocs
            .post_with_query(path, Query::new().format(OcsFormat::Json), &request)
            .await
    }

    /// Deletes a conversation room by its token.
    ///
    /// Sends `DELETE /ocs/v2.php/apps/spreed/api/v4/room/{token}`.
    pub async fn delete_room(&self, token: &str) -> Result<(), Error> {
        tracing::info!(token, "Deleting Talk room");
        let path = ApiPath::ocs(format!("{ROOM_API_PATH}/{token}"))?;
        self.ocs
            .delete_with_query::<()>(path, Query::new().format(OcsFormat::Json))
            .await
    }

    /// Lists chat messages in a conversation.
    ///
    /// Sends `GET /ocs/v2.php/apps/spreed/api/v1/chat/{token}` with `lookIntoFuture=0`
    /// and optional `limit`.
    pub async fn list_messages(
        &self,
        token: &str,
        limit: Option<u32>,
    ) -> Result<Vec<ChatMessage>, Error> {
        let path = ApiPath::ocs(format!("{CHAT_API_PATH}/{token}"))?;
        let mut query = Query::new()
            .format(OcsFormat::Json)
            .param("lookIntoFuture", "0");
        if let Some(limit) = limit {
            query = query.param("limit", limit.to_string());
        }
        self.ocs.get(path, query).await
    }

    /// Sends a new chat message to a conversation.
    ///
    /// Sends `POST /ocs/v2.php/apps/spreed/api/v1/chat/{token}` with `{"message": "..."}`.
    pub async fn send_message(&self, token: &str, message: &str) -> Result<ChatMessage, Error> {
        let path = ApiPath::ocs(format!("{CHAT_API_PATH}/{token}"))?;
        let request = SendMessageRequest { message };
        self.ocs
            .post_with_query(path, Query::new().format(OcsFormat::Json), &request)
            .await
    }
}

#[cfg(test)]
mod tests {
    use std::future::Future;
    use std::sync::{Arc, Mutex};
    use std::task::{Context, Poll, Waker};

    use nextcloud_ocs::OcsClient;
    use nextcloud_sdk_core::{
        AuthProvider, Error, Headers, HttpMethod, Request, Response, StatusCode, Transport,
    };

    use super::TalkClient;
    use crate::room_type::RoomType;

    fn block_on<F: Future>(future: F) -> F::Output {
        let mut future = std::pin::pin!(future);
        let mut context = Context::from_waker(Waker::noop());
        loop {
            match future.as_mut().poll(&mut context) {
                Poll::Ready(value) => return value,
                Poll::Pending => std::thread::yield_now(),
            }
        }
    }

    #[derive(Clone, Debug)]
    struct FakeTransport {
        response: Response,
        seen: Arc<Mutex<Option<Request>>>,
    }

    impl FakeTransport {
        fn new(status: u16, body: &str) -> Self {
            Self {
                response: Response::new(StatusCode::new(status), Headers::new(), body.as_bytes()),
                seen: Arc::new(Mutex::new(None)),
            }
        }

        fn request(&self) -> Option<Request> {
            self.seen.lock().ok().and_then(|s| s.clone())
        }
    }

    impl Transport for FakeTransport {
        async fn execute(&self, request: Request) -> Result<Response, Error> {
            if let Ok(mut seen) = self.seen.lock() {
                *seen = Some(request);
            }
            Ok(self.response.clone())
        }
    }

    #[derive(Clone, Debug)]
    struct FakeAuth;

    impl AuthProvider for FakeAuth {
        fn authenticate(&self, headers: &mut Headers) {
            headers.set("Authorization", "Bearer test-talk-token");
        }
    }

    const LIST_ROOMS_RESPONSE: &str = r#"{
        "ocs": {
            "meta": { "status": "ok", "statuscode": 200, "message": "OK" },
            "data": [
                {
                    "token": "tok1",
                    "name": "General",
                    "displayName": "General",
                    "type": 2,
                    "description": "General discussions",
                    "hasPassword": false,
                    "participantCount": 8
                },
                {
                    "token": "tok2",
                    "name": "Direct Alice",
                    "type": 1,
                    "hasPassword": false
                }
            ]
        }
    }"#;

    #[test]
    fn test_list_rooms() {
        let transport = FakeTransport::new(200, LIST_ROOMS_RESPONSE);
        let ocs = OcsClient::new(transport.clone(), FakeAuth);
        let client = TalkClient::new(&ocs);

        let rooms = block_on(client.list_rooms()).expect("list rooms succeeds");
        assert_eq!(rooms.len(), 2);
        assert_eq!(rooms[0].token, "tok1");
        assert_eq!(rooms[0].name, "General");
        assert_eq!(rooms[0].room_type, RoomType::Group);
        assert_eq!(rooms[0].description.as_deref(), Some("General discussions"));
        assert!(!rooms[0].has_password);
        assert_eq!(rooms[0].participant_count, Some(8));

        assert_eq!(rooms[1].token, "tok2");
        assert_eq!(rooms[1].room_type, RoomType::OneToOne);

        let req = transport.request().expect("request was sent");
        assert_eq!(req.method(), HttpMethod::Get);
        assert_eq!(
            req.path().as_str(),
            "/ocs/v2.php/apps/spreed/api/v4/room?format=json"
        );
    }

    const GET_ROOM_RESPONSE: &str = r#"{
        "ocs": {
            "meta": { "status": "ok", "statuscode": 200, "message": "OK" },
            "data": {
                "token": "abcde",
                "roomName": "Dev Team",
                "roomType": 2,
                "description": "Engineers only",
                "hasPassword": true,
                "participantCount": 4
            }
        }
    }"#;

    #[test]
    fn test_get_room() {
        let transport = FakeTransport::new(200, GET_ROOM_RESPONSE);
        let ocs = OcsClient::new(transport.clone(), FakeAuth);
        let client = TalkClient::new(&ocs);

        let room = block_on(client.get_room("abcde")).expect("get room succeeds");
        assert_eq!(room.token, "abcde");
        assert_eq!(room.name, "Dev Team");
        assert_eq!(room.room_type, RoomType::Group);
        assert_eq!(room.description.as_deref(), Some("Engineers only"));
        assert!(room.has_password);
        assert_eq!(room.participant_count, Some(4));

        let req = transport.request().expect("request was sent");
        assert_eq!(req.method(), HttpMethod::Get);
        assert_eq!(
            req.path().as_str(),
            "/ocs/v2.php/apps/spreed/api/v4/room/abcde?format=json"
        );
    }

    const CREATE_ROOM_RESPONSE: &str = r#"{
        "ocs": {
            "meta": { "status": "ok", "statuscode": 200, "message": "OK" },
            "data": {
                "token": "newtok123",
                "name": "New Project",
                "type": 2,
                "description": "",
                "hasPassword": false
            }
        }
    }"#;

    #[test]
    fn test_create_room() {
        let transport = FakeTransport::new(200, CREATE_ROOM_RESPONSE);
        let ocs = OcsClient::new(transport.clone(), FakeAuth);
        let client = TalkClient::new(&ocs);

        let room = block_on(client.create_room(RoomType::Group, None, Some("New Project")))
            .expect("create room succeeds");
        assert_eq!(room.token, "newtok123");
        assert_eq!(room.name, "New Project");
        assert_eq!(room.room_type, RoomType::Group);

        let req = transport.request().expect("request was sent");
        assert_eq!(req.method(), HttpMethod::Post);
        assert_eq!(
            req.path().as_str(),
            "/ocs/v2.php/apps/spreed/api/v4/room?format=json"
        );

        let body_str = String::from_utf8(req.body().expect("request body").to_vec()).unwrap();
        assert!(body_str.contains(r#""roomType":2"#));
        assert!(body_str.contains(r#""roomName":"New Project""#));
        assert!(!body_str.contains("invite"));
    }

    const EMPTY_OCS_RESPONSE: &str = r#"{
        "ocs": {
            "meta": { "status": "ok", "statuscode": 200, "message": "OK" },
            "data": []
        }
    }"#;

    #[test]
    fn test_delete_room() {
        let transport = FakeTransport::new(200, EMPTY_OCS_RESPONSE);
        let ocs = OcsClient::new(transport.clone(), FakeAuth);
        let client = TalkClient::new(&ocs);

        block_on(client.delete_room("deadbeef")).expect("delete room succeeds");

        let req = transport.request().expect("request was sent");
        assert_eq!(req.method(), HttpMethod::Delete);
        assert_eq!(
            req.path().as_str(),
            "/ocs/v2.php/apps/spreed/api/v4/room/deadbeef?format=json"
        );
    }

    const LIST_MESSAGES_RESPONSE: &str = r#"{
        "ocs": {
            "meta": { "status": "ok", "statuscode": 200, "message": "OK" },
            "data": [
                {
                    "id": 1,
                    "token": "tok1",
                    "actorType": "users",
                    "actorId": "bob",
                    "actorDisplayName": "Bob",
                    "timestamp": 1718000000,
                    "message": "Hi all",
                    "systemMessage": ""
                },
                {
                    "id": 2,
                    "token": "tok1",
                    "actorType": "users",
                    "actorId": "system",
                    "actorDisplayName": "System",
                    "timestamp": 1718000100,
                    "message": "User left",
                    "systemMessage": "user_left"
                }
            ]
        }
    }"#;

    #[test]
    fn test_list_messages() {
        let transport = FakeTransport::new(200, LIST_MESSAGES_RESPONSE);
        let ocs = OcsClient::new(transport.clone(), FakeAuth);
        let client = TalkClient::new(&ocs);

        let messages =
            block_on(client.list_messages("tok1", Some(50))).expect("list messages succeeds");
        assert_eq!(messages.len(), 2);
        assert_eq!(messages[0].id, 1);
        assert_eq!(messages[0].actor_id, "bob");
        assert_eq!(messages[0].message, "Hi all");
        assert!(!messages[0].is_system_message());

        assert_eq!(messages[1].id, 2);
        assert_eq!(messages[1].system_message, "user_left");
        assert!(messages[1].is_system_message());

        let req = transport.request().expect("request was sent");
        assert_eq!(req.method(), HttpMethod::Get);
        assert_eq!(
            req.path().as_str(),
            "/ocs/v2.php/apps/spreed/api/v1/chat/tok1?format=json&lookIntoFuture=0&limit=50"
        );
    }

    const SEND_MESSAGE_RESPONSE: &str = r#"{
        "ocs": {
            "meta": { "status": "ok", "statuscode": 200, "message": "OK" },
            "data": {
                "id": 42,
                "token": "tok1",
                "actorType": "users",
                "actorId": "alice",
                "actorDisplayName": "Alice Cooper",
                "timestamp": 1718000200,
                "message": "Hello world!",
                "systemMessage": ""
            }
        }
    }"#;

    #[test]
    fn test_send_message() {
        let transport = FakeTransport::new(200, SEND_MESSAGE_RESPONSE);
        let ocs = OcsClient::new(transport.clone(), FakeAuth);
        let client = TalkClient::new(&ocs);

        let msg =
            block_on(client.send_message("tok1", "Hello world!")).expect("send message succeeds");
        assert_eq!(msg.id, 42);
        assert_eq!(msg.token, "tok1");
        assert_eq!(msg.actor_id, "alice");
        assert_eq!(msg.actor_display_name, "Alice Cooper");
        assert_eq!(msg.message, "Hello world!");

        let req = transport.request().expect("request was sent");
        assert_eq!(req.method(), HttpMethod::Post);
        assert_eq!(
            req.path().as_str(),
            "/ocs/v2.php/apps/spreed/api/v1/chat/tok1?format=json"
        );

        let body_str = String::from_utf8(req.body().expect("request body").to_vec()).unwrap();
        assert_eq!(body_str, r#"{"message":"Hello world!"}"#);
    }
}