backbone-mail 0.2.32

Odoo mail core port — message/notification/followers/activity/alias/sms queue (schema: messaging)
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
//! Discuss channel routes (hand-written; user-owned).
//!
//! The `/discuss` wire surface: channel/group creation, the 1:1 chat dedup
//! verb, member state verbs (join/leave/mute/notifications/pin/fold/seen/
//! fetched/separator), typing notifications, search, reactions, message
//! edits (author-or-admin), stars, and channel-level message pinning.
//! Identity comes from the guest middleware's [`WireIdentity`] extension;
//! membership gates live in the services (`NotMember` → 403). Search is the
//! one throttled verb here (30/60s — an enumeration-shaped lookup).

use axum::extract::{Path, State};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::routing::{get, post};
use axum::{Json, Router};
use serde::Deserialize;
use uuid::Uuid;

use crate::application::service::channel_write_service::CreateChannelCommand;
use crate::application::service::{
    ChannelError, ChannelQueryError, MemberError, MessageEditError, ReactionError, TypingError,
};
use crate::presentation::http::thread_routes::{
    db_err, json_err, ok_json, require_identity, ApiState,
};
use crate::presentation::middleware::{forbidden, unauthorized, IsAdmin, WireIdentity};

fn member_err(e: MemberError) -> Response {
    match e {
        MemberError::NotMember(id) => (
            StatusCode::FORBIDDEN,
            Json(serde_json::json!({"error": format!("not a member of channel {id}")})),
        )
            .into_response(),
        MemberError::Invalid(m) => {
            (StatusCode::UNPROCESSABLE_ENTITY, Json(json_err(&m))).into_response()
        }
        MemberError::Db(err) => db_err(err),
    }
}

fn channel_err(e: ChannelError) -> Response {
    match e {
        ChannelError::NotFound(id) => (
            StatusCode::NOT_FOUND,
            Json(serde_json::json!({"error": format!("no such channel {id}")})),
        )
            .into_response(),
        ChannelError::Invalid(m) => {
            (StatusCode::UNPROCESSABLE_ENTITY, Json(json_err(&m))).into_response()
        }
        ChannelError::Db(err) => db_err(err),
    }
}

fn query_err(e: ChannelQueryError) -> Response {
    match e {
        ChannelQueryError::NotAMember => forbidden("not a channel member"),
        ChannelQueryError::Db(err) => db_err(err),
    }
}

// ---------------------------------------------------------------------------
// Channel creation / chat dedup
// ---------------------------------------------------------------------------

#[derive(Deserialize)]
pub struct CreateChannelBody {
    pub name: Option<String>,
    /// chat | channel | group.
    pub channel_type: String,
    pub default_access_mode: Option<String>,
    pub email_send: Option<bool>,
}

async fn create_channel(
    State(app): State<ApiState>,
    axum::Extension(identity): axum::Extension<WireIdentity>,
    Json(body): Json<CreateChannelBody>,
) -> Response {
    let Ok(id) = require_identity(&identity) else { return unauthorized() };
    match app
        .channel_write_service
        .create(CreateChannelCommand {
            name: body.name,
            channel_type: body.channel_type,
            uuid: Some(Uuid::new_v4().to_string()),
            default_access_mode: body.default_access_mode,
            email_send: body.email_send.unwrap_or(false),
            creator: Some(id),
        })
        .await
    {
        Ok(channel_id) => ok_json(serde_json::json!({ "channel_id": channel_id })),
        Err(e) => channel_err(e),
    }
}

#[derive(Deserialize)]
pub struct ChatBody {
    /// The other partner (the caller's own partner id is the first leg).
    pub partner_id: Uuid,
}

async fn open_chat(
    State(app): State<ApiState>,
    axum::Extension(identity): axum::Extension<WireIdentity>,
    Json(body): Json<ChatBody>,
) -> Response {
    let Some(me) = identity.partner_id() else { return unauthorized() };
    // A caller may only mint a chat they are a party to — never a channel
    // between two third parties.
    match app.channel_write_service.get_or_create_chat(me, body.partner_id).await {
        Ok((channel_id, created)) => {
            ok_json(serde_json::json!({ "channel_id": channel_id, "created": created }))
        }
        Err(e) => channel_err(e),
    }
}

#[derive(Deserialize)]
pub struct ChannelUpdateBody {
    pub name: Option<String>,
    pub description: Option<String>,
    pub email_send: Option<bool>,
}

async fn update_channel(
    State(app): State<ApiState>,
    axum::Extension(identity): axum::Extension<WireIdentity>,
    Path(channel_id): Path<Uuid>,
    Json(body): Json<ChannelUpdateBody>,
) -> Response {
    let Ok(id) = require_identity(&identity) else { return unauthorized() };
    // Member gate first — only members may even propose an edit.
    if let Err(e) = app.channel_query_service.members(&id, channel_id).await {
        return query_err(e);
    }
    match app
        .channel_write_service
        .update_fields(
            channel_id,
            body.name.as_deref(),
            body.description.as_deref(),
            body.email_send,
        )
        .await
    {
        Ok(()) => ok_json(serde_json::json!({ "updated": true })),
        Err(e) => channel_err(e),
    }
}

#[derive(Deserialize)]
pub struct PinMessageBody {
    pub message_id: Uuid,
    pub pinned: bool,
}

async fn pin_message(
    State(app): State<ApiState>,
    axum::Extension(identity): axum::Extension<WireIdentity>,
    Path(channel_id): Path<Uuid>,
    Json(body): Json<PinMessageBody>,
) -> Response {
    let Ok(id) = require_identity(&identity) else { return unauthorized() };
    if let Err(e) = app.channel_query_service.members(&id, channel_id).await {
        return query_err(e);
    }
    match app
        .channel_write_service
        .set_message_pinned(channel_id, body.message_id, body.pinned)
        .await
    {
        Ok(()) => ok_json(serde_json::json!({ "pinned": body.pinned })),
        Err(e) => channel_err(e),
    }
}

// ---------------------------------------------------------------------------
// Member state verbs
// ---------------------------------------------------------------------------

#[derive(Deserialize)]
pub struct MessageIdBody {
    pub message_id: Uuid,
}

async fn mark_seen(
    State(app): State<ApiState>,
    axum::Extension(identity): axum::Extension<WireIdentity>,
    Path(channel_id): Path<Uuid>,
    Json(body): Json<MessageIdBody>,
) -> Response {
    let Ok(id) = require_identity(&identity) else { return unauthorized() };
    match app
        .channel_member_write_service
        .mark_as_read(channel_id, &id, body.message_id)
        .await
    {
        Ok(advanced) => ok_json(serde_json::json!({ "advanced": advanced })),
        Err(e) => member_err(e),
    }
}

async fn mark_fetched(
    State(app): State<ApiState>,
    axum::Extension(identity): axum::Extension<WireIdentity>,
    Path(channel_id): Path<Uuid>,
    Json(body): Json<MessageIdBody>,
) -> Response {
    let Ok(id) = require_identity(&identity) else { return unauthorized() };
    match app
        .channel_member_write_service
        .mark_fetched(channel_id, &id, body.message_id)
        .await
    {
        Ok(advanced) => ok_json(serde_json::json!({ "advanced": advanced })),
        Err(e) => member_err(e),
    }
}

async fn set_separator(
    State(app): State<ApiState>,
    axum::Extension(identity): axum::Extension<WireIdentity>,
    Path(channel_id): Path<Uuid>,
    Json(body): Json<MessageIdBody>,
) -> Response {
    let Ok(id) = require_identity(&identity) else { return unauthorized() };
    match app
        .channel_member_write_service
        .set_new_message_separator(channel_id, &id, body.message_id)
        .await
    {
        Ok(()) => ok_json(serde_json::json!({ "set": true })),
        Err(e) => member_err(e),
    }
}

async fn join_channel(
    State(app): State<ApiState>,
    axum::Extension(identity): axum::Extension<WireIdentity>,
    Path(channel_id): Path<Uuid>,
) -> Response {
    let Ok(id) = require_identity(&identity) else { return unauthorized() };
    match app.channel_member_write_service.join(channel_id, &id).await {
        Ok(joined) => ok_json(serde_json::json!({ "joined": joined })),
        Err(e) => member_err(e),
    }
}

async fn leave_channel(
    State(app): State<ApiState>,
    axum::Extension(identity): axum::Extension<WireIdentity>,
    Path(channel_id): Path<Uuid>,
) -> Response {
    let Ok(id) = require_identity(&identity) else { return unauthorized() };
    match app.channel_member_write_service.leave(channel_id, &id).await {
        Ok(left) => ok_json(serde_json::json!({ "left": left })),
        Err(e) => member_err(e),
    }
}

#[derive(Deserialize)]
pub struct MuteBody {
    pub until: Option<chrono::DateTime<chrono::Utc>>,
    pub forever: Option<bool>,
}

async fn mute_channel(
    State(app): State<ApiState>,
    axum::Extension(identity): axum::Extension<WireIdentity>,
    Path(channel_id): Path<Uuid>,
    body: Option<Json<MuteBody>>,
) -> Response {
    let Ok(id) = require_identity(&identity) else { return unauthorized() };
    // No body at all = unmute (until=None, forever=false).
    let (until, forever) = match body {
        Some(Json(b)) => (b.until, b.forever.unwrap_or(false)),
        None => (None, false),
    };
    match app.channel_member_write_service.set_mute(channel_id, &id, until, forever).await {
        Ok(()) => ok_json(serde_json::json!({ "muted": until.is_some() || forever })),
        Err(e) => member_err(e),
    }
}

#[derive(Deserialize)]
pub struct NotificationsBody {
    /// all | mentions | no_notif; absent = inherit (None).
    pub custom_notifications: Option<String>,
}

async fn set_notifications(
    State(app): State<ApiState>,
    axum::Extension(identity): axum::Extension<WireIdentity>,
    Path(channel_id): Path<Uuid>,
    body: Option<Json<NotificationsBody>>,
) -> Response {
    let Ok(id) = require_identity(&identity) else { return unauthorized() };
    let value = body.and_then(|Json(b)| b.custom_notifications);
    match app
        .channel_member_write_service
        .set_custom_notifications(channel_id, &id, value.as_deref())
        .await
    {
        Ok(()) => ok_json(serde_json::json!({ "custom_notifications": value })),
        Err(e) => member_err(e),
    }
}

#[derive(Deserialize)]
pub struct PinnedBody {
    pub pinned: bool,
}

async fn set_member_pin(
    State(app): State<ApiState>,
    axum::Extension(identity): axum::Extension<WireIdentity>,
    Path(channel_id): Path<Uuid>,
    Json(body): Json<PinnedBody>,
) -> Response {
    let Ok(id) = require_identity(&identity) else { return unauthorized() };
    match app
        .channel_member_write_service
        .set_pinned(channel_id, &id, body.pinned)
        .await
    {
        Ok(()) => ok_json(serde_json::json!({ "pinned": body.pinned })),
        Err(e) => member_err(e),
    }
}

#[derive(Deserialize)]
pub struct FoldBody {
    /// open | closed; absent = undefined.
    pub fold_state: Option<String>,
}

async fn set_fold(
    State(app): State<ApiState>,
    axum::Extension(identity): axum::Extension<WireIdentity>,
    Path(channel_id): Path<Uuid>,
    body: Option<Json<FoldBody>>,
) -> Response {
    let Ok(id) = require_identity(&identity) else { return unauthorized() };
    let fold = body.and_then(|Json(b)| b.fold_state);
    match app
        .channel_member_write_service
        .set_fold_state(channel_id, &id, fold.as_deref())
        .await
    {
        Ok(()) => ok_json(serde_json::json!({ "fold_state": fold })),
        Err(e) => member_err(e),
    }
}

#[derive(Deserialize)]
pub struct TypingBody {
    pub typing: bool,
}

async fn notify_typing(
    State(app): State<ApiState>,
    axum::Extension(identity): axum::Extension<WireIdentity>,
    Path(channel_id): Path<Uuid>,
    Json(body): Json<TypingBody>,
) -> Response {
    let Ok(id) = require_identity(&identity) else { return unauthorized() };
    match app.typing_service.notify_typing(channel_id, &id, body.typing).await {
        Ok(()) => ok_json(serde_json::json!({ "ok": true })),
        Err(TypingError::Db(err)) => db_err(err),
    }
}

// ---------------------------------------------------------------------------
// Search + reads
// ---------------------------------------------------------------------------

#[derive(Deserialize)]
pub struct SearchBody {
    pub term: Option<String>,
}

async fn search_channels(
    State(app): State<ApiState>,
    axum::Extension(identity): axum::Extension<WireIdentity>,
    Json(body): Json<SearchBody>,
) -> Response {
    let Ok(id) = require_identity(&identity) else { return unauthorized() };
    match app.channel_query_service.search(&id, body.term.as_deref()).await {
        Ok(hits) => ok_json(serde_json::json!({ "channels": hits })),
        Err(e) => query_err(e),
    }
}

async fn channel_members(
    State(app): State<ApiState>,
    axum::Extension(identity): axum::Extension<WireIdentity>,
    Path(channel_id): Path<Uuid>,
) -> Response {
    let Ok(id) = require_identity(&identity) else { return unauthorized() };
    match app.channel_query_service.members(&id, channel_id).await {
        Ok(rows) => ok_json(serde_json::json!({ "members": rows
            .into_iter()
            .map(|(member_id, partner_id, guest_id)| serde_json::json!({
                "member_id": member_id,
                "partner_id": partner_id,
                "guest_id": guest_id,
            }))
            .collect::<Vec<_>>() })),
        Err(e) => query_err(e),
    }
}

async fn pinned_messages(
    State(app): State<ApiState>,
    axum::Extension(identity): axum::Extension<WireIdentity>,
    Path(channel_id): Path<Uuid>,
) -> Response {
    let Ok(id) = require_identity(&identity) else { return unauthorized() };
    match app.channel_query_service.pinned_messages(&id, channel_id).await {
        Ok(ids) => ok_json(serde_json::json!({ "message_ids": ids })),
        Err(e) => query_err(e),
    }
}

// ---------------------------------------------------------------------------
// Message verbs: reactions, edits, stars
// ---------------------------------------------------------------------------

#[derive(Deserialize)]
pub struct ReactionBody {
    pub content: String,
}

async fn toggle_reaction(
    State(app): State<ApiState>,
    axum::Extension(identity): axum::Extension<WireIdentity>,
    Path(message_id): Path<Uuid>,
    Json(body): Json<ReactionBody>,
) -> Response {
    let Ok(id) = require_identity(&identity) else { return unauthorized() };
    match app.reaction_write_service.toggle(message_id, &id, &body.content).await {
        Ok(reacted) => ok_json(serde_json::json!({ "reacted": reacted })),
        Err(e) => match e {
            ReactionError::NotFound(mid) => (
                StatusCode::NOT_FOUND,
                Json(serde_json::json!({"error": format!("no such message {mid}")})),
            )
                .into_response(),
            ReactionError::Invalid(m) => {
                (StatusCode::UNPROCESSABLE_ENTITY, Json(json_err(&m))).into_response()
            }
            ReactionError::NeedsIdentity => unauthorized(),
            ReactionError::Db(err) => db_err(err),
        },
    }
}

#[derive(Deserialize)]
pub struct EditBody {
    pub subject: Option<String>,
    pub body: Option<String>,
}

async fn edit_message(
    State(app): State<ApiState>,
    axum::Extension(identity): axum::Extension<WireIdentity>,
    axum::Extension(is_admin): axum::Extension<IsAdmin>,
    Path(message_id): Path<Uuid>,
    Json(body): Json<EditBody>,
) -> Response {
    let Ok(id) = require_identity(&identity) else { return unauthorized() };
    match app
        .message_edit_service
        .update_content(
            message_id,
            &id,
            is_admin.0,
            body.subject.as_deref(),
            body.body.as_deref(),
        )
        .await
    {
        Ok(changed) => ok_json(serde_json::json!({ "changed": changed })),
        Err(e) => match e {
            MessageEditError::NotFound(mid) => (
                StatusCode::NOT_FOUND,
                Json(serde_json::json!({"error": format!("no such message {mid}")})),
            )
                .into_response(),
            MessageEditError::Forbidden(_) => {
                forbidden("only the author (or an admin route) may edit")
            }
            MessageEditError::NeedsPartner => unauthorized(),
            MessageEditError::Invalid(m) => {
                (StatusCode::UNPROCESSABLE_ENTITY, Json(json_err(&m))).into_response()
            }
            MessageEditError::Db(err) => db_err(err),
        },
    }
}

async fn toggle_star(
    State(app): State<ApiState>,
    axum::Extension(identity): axum::Extension<WireIdentity>,
    Path(message_id): Path<Uuid>,
) -> Response {
    let Ok(id) = require_identity(&identity) else { return unauthorized() };
    match app.message_edit_service.toggle_star(message_id, &id).await {
        Ok(starred) => ok_json(serde_json::json!({ "starred": starred })),
        Err(e) => match e {
            MessageEditError::NotFound(mid) => (
                StatusCode::NOT_FOUND,
                Json(serde_json::json!({"error": format!("no such message {mid}")})),
            )
                .into_response(),
            MessageEditError::NeedsPartner => unauthorized(),
            MessageEditError::Invalid(m) => {
                (StatusCode::UNPROCESSABLE_ENTITY, Json(json_err(&m))).into_response()
            }
            MessageEditError::Forbidden(_) => forbidden("only the author may edit"),
            MessageEditError::Db(err) => db_err(err),
        },
    }
}

// ---------------------------------------------------------------------------
// Composer
// ---------------------------------------------------------------------------

/// The discuss route group (guarded surface — mount behind app user auth +
/// the guest middleware). Search is throttled 30/60s.
pub fn composer() -> Router<ApiState> {
    use axum::middleware as axum_mw;

    let throttled = Router::<ApiState>::new()
        .route("/discuss/search", post(search_channels))
        .route_layer(axum_mw::from_fn_with_state(
            backbone_rate_limit::middleware(30, 60),
            backbone_rate_limit::rate_limit_middleware,
        ));

    Router::new()
        .route("/discuss/channel", post(create_channel))
        .route("/discuss/chat", post(open_chat))
        .route("/discuss/channel/:id", post(update_channel))
        .route("/discuss/channel/:id/join", post(join_channel))
        .route("/discuss/channel/:id/leave", post(leave_channel))
        .route("/discuss/channel/:id/mute", post(mute_channel))
        .route("/discuss/channel/:id/notifications", post(set_notifications))
        .route("/discuss/channel/:id/pin", post(set_member_pin))
        .route("/discuss/channel/:id/fold", post(set_fold))
        .route("/discuss/channel/:id/seen", post(mark_seen))
        .route("/discuss/channel/:id/fetched", post(mark_fetched))
        .route("/discuss/channel/:id/separator", post(set_separator))
        .route("/discuss/channel/:id/typing", post(notify_typing))
        .route("/discuss/channel/:id/pin_message", post(pin_message))
        .route("/discuss/channel/:id/members", get(channel_members))
        .route("/discuss/channel/:id/pinned", get(pinned_messages))
        .route("/discuss/message/:id/reaction", post(toggle_reaction))
        .route("/discuss/message/:id/edit", post(edit_message))
        .route("/discuss/message/:id/star", post(toggle_star))
        .merge(throttled)
}