bridge_common/repo/
chats.rs

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
// Copyright 2024 StarfleetAI
// SPDX-License-Identifier: Apache-2.0

use anyhow::Context;
use chrono::Utc;
use sqlx::{query, query_as, Executor, Postgres};

use crate::types::{
    chats::{Chat, Kind},
    Result,
};

/// List all chats.
///
/// # Errors
///
/// Returns error if there was a problem while accessing database.
pub async fn list<'a, E>(executor: E, company_id: i32, is_pinned: Option<bool>) -> Result<Vec<Chat>>
where
    E: Executor<'a, Database = Postgres>,
{
    if let Some(is_pinned) = is_pinned {
        return Ok(query_as!(
            Chat,
            r#"
            SELECT *
            FROM chats
            WHERE
                company_id = $1 AND
                is_pinned = $2 AND
                kind = $3
            ORDER BY updated_at DESC
            "#,
            company_id,
            is_pinned,
            Kind::Direct.to_string()
        )
        .fetch_all(executor)
        .await?);
    }

    Ok(query_as!(
        Chat,
        "SELECT * FROM chats WHERE company_id = $1 AND kind = $2 ORDER BY id DESC",
        company_id,
        Kind::Direct.to_string()
    )
    .fetch_all(executor)
    .await?)
}

/// Get chat by id.
///
/// # Errors
///
/// Returns error if there was a problem while accessing database.
pub async fn get<'a, E>(executor: E, company_id: i32, id: i32) -> Result<Chat>
where
    E: Executor<'a, Database = Postgres>,
{
    Ok(query_as!(
        Chat,
        "SELECT * FROM chats WHERE company_id = $1 AND id = $2 LIMIT 1",
        company_id,
        id
    )
    .fetch_one(executor)
    .await?)
}

/// Delete chat by id.
///
/// # Errors
///
/// Returns error if there was a problem while accessing database.
pub async fn delete<'a, E>(executor: E, company_id: i32, id: i32) -> Result<()>
where
    E: Executor<'a, Database = Postgres>,
{
    query!(
        "DELETE FROM chats WHERE company_id = $1 AND id = $2",
        company_id,
        id
    )
    .execute(executor)
    .await
    .with_context(|| "Failed to delete chat")?;

    Ok(())
}

/// Create chat.
///
/// # Errors
///
/// Returns error if there was a problem while creating chat.
pub async fn create<'a, E>(executor: E, company_id: i32, kind: Kind) -> Result<Chat>
where
    E: Executor<'a, Database = Postgres>,
{
    let now = Utc::now();

    Ok(query_as!(
        Chat,
        "INSERT INTO chats (company_id, kind, created_at, updated_at) VALUES ($1, $2, $3, $3) RETURNING *",
        company_id,
        kind.to_string(),
        now
    )
        .fetch_one(executor)
        .await?)
}

/// Update chat title by id.
///
/// # Errors
///
/// Returns error if there was a problem while accessing database or if the chat with the given ID does not exist.
pub async fn update_title<'a, E>(executor: E, company_id: i32, id: i32, title: &str) -> Result<()>
where
    E: Executor<'a, Database = Postgres>,
{
    let now = Utc::now();
    query!(
        "UPDATE chats SET title = $1, updated_at = $2 WHERE company_id = $3 AND id = $4",
        title,
        now,
        company_id,
        id
    )
    .execute(executor)
    .await
    .with_context(|| format!("Failed to update title for chat with id: {id}"))?;

    Ok(())
}

/// Toggle chat is pinned status by id.
///
/// # Errors
///
/// Returns error if the chat with the given ID does not exist.
pub async fn toggle_is_pinned<'a, E>(executor: E, company_id: i32, id: i32) -> Result<()>
where
    E: Executor<'a, Database = Postgres>,
{
    query!(
        "UPDATE chats SET is_pinned = NOT is_pinned WHERE company_id = $1 AND id = $2",
        company_id,
        id
    )
    .execute(executor)
    .await
    .with_context(|| format!("Failed to toggle pin status for chat with id: {id}"))?;

    Ok(())
}

/// Change chat model by id
///
/// # Errors
///
/// Return error if the chat with the given ID does not exist.
pub async fn update_model_id<'a, E>(
    executor: E,
    company_id: i32,
    id: i32,
    model_id: Option<i32>,
) -> Result<()>
where
    E: Executor<'a, Database = Postgres>,
{
    let now = Utc::now();
    query!(
        "UPDATE chats SET model_id = $3, updated_at = $4 WHERE company_id = $1 AND id = $2",
        company_id,
        id,
        model_id,
        now
    )
    .execute(executor)
    .await
    .with_context(|| format!("Failed to change model for chat with id: {id}"))?;

    Ok(())
}