| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
use chrono::{DateTime, Utc}; |
| 23 |
use sqlx::PgPool; |
| 24 |
|
| 25 |
use crate::db::models::DbSyncGroupInvitation; |
| 26 |
use crate::db::{SyncGroupId, SyncGroupInvitationId, UserId}; |
| 27 |
use crate::error::Result; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
const SELECT_INVITATION: &str = r" |
| 32 |
SELECT i.id, i.group_id, i.inviter_user_id, i.invitee_user_id, |
| 33 |
u.email AS invitee_email, i.invitee_pubkey, |
| 34 |
i.accepted_at, i.redeemed_at, i.revoked_at, i.expires_at, i.created_at |
| 35 |
FROM sync_group_invitations i |
| 36 |
LEFT JOIN users u ON u.id = i.invitee_user_id |
| 37 |
"; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
#[tracing::instrument(skip_all)] |
| 42 |
pub async fn create_invitation( |
| 43 |
pool: &PgPool, |
| 44 |
group_id: SyncGroupId, |
| 45 |
inviter_user_id: UserId, |
| 46 |
token_hash: &str, |
| 47 |
expires_at: DateTime<Utc>, |
| 48 |
) -> Result<DbSyncGroupInvitation> { |
| 49 |
let id: SyncGroupInvitationId = sqlx::query_scalar( |
| 50 |
r" |
| 51 |
INSERT INTO sync_group_invitations |
| 52 |
(group_id, inviter_user_id, token_hash, expires_at) |
| 53 |
VALUES ($1, $2, $3, $4) |
| 54 |
RETURNING id |
| 55 |
", |
| 56 |
) |
| 57 |
.bind(group_id) |
| 58 |
.bind(inviter_user_id) |
| 59 |
.bind(token_hash) |
| 60 |
.bind(expires_at) |
| 61 |
.fetch_one(pool) |
| 62 |
.await?; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
get_invitation(pool, id) |
| 68 |
.await? |
| 69 |
.ok_or_else(|| sqlx::Error::RowNotFound.into()) |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
#[tracing::instrument(skip_all)] |
| 74 |
pub async fn get_invitation( |
| 75 |
pool: &PgPool, |
| 76 |
id: SyncGroupInvitationId, |
| 77 |
) -> Result<Option<DbSyncGroupInvitation>> { |
| 78 |
let sql = format!("{SELECT_INVITATION} WHERE i.id = $1"); |
| 79 |
let invitation = sqlx::query_as::<_, DbSyncGroupInvitation>(&sql) |
| 80 |
.bind(id) |
| 81 |
.fetch_optional(pool) |
| 82 |
.await?; |
| 83 |
Ok(invitation) |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
#[tracing::instrument(skip_all)] |
| 93 |
pub async fn get_invitation_by_token( |
| 94 |
pool: &PgPool, |
| 95 |
token_hash: &str, |
| 96 |
) -> Result<Option<DbSyncGroupInvitation>> { |
| 97 |
let sql = format!("{SELECT_INVITATION} WHERE i.token_hash = $1"); |
| 98 |
let invitation = sqlx::query_as::<_, DbSyncGroupInvitation>(&sql) |
| 99 |
.bind(token_hash) |
| 100 |
.fetch_optional(pool) |
| 101 |
.await?; |
| 102 |
Ok(invitation) |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
#[tracing::instrument(skip_all)] |
| 117 |
pub async fn accept_invitation( |
| 118 |
pool: &PgPool, |
| 119 |
token_hash: &str, |
| 120 |
invitee_user_id: UserId, |
| 121 |
invitee_pubkey: &str, |
| 122 |
) -> Result<Option<DbSyncGroupInvitation>> { |
| 123 |
let id: Option<SyncGroupInvitationId> = sqlx::query_scalar( |
| 124 |
r" |
| 125 |
UPDATE sync_group_invitations |
| 126 |
SET invitee_user_id = $2, |
| 127 |
invitee_pubkey = $3, |
| 128 |
accepted_at = NOW() |
| 129 |
WHERE token_hash = $1 |
| 130 |
AND accepted_at IS NULL |
| 131 |
AND redeemed_at IS NULL |
| 132 |
AND revoked_at IS NULL |
| 133 |
AND expires_at > NOW() |
| 134 |
RETURNING id |
| 135 |
", |
| 136 |
) |
| 137 |
.bind(token_hash) |
| 138 |
.bind(invitee_user_id) |
| 139 |
.bind(invitee_pubkey) |
| 140 |
.fetch_optional(pool) |
| 141 |
.await?; |
| 142 |
|
| 143 |
match id { |
| 144 |
Some(id) => get_invitation(pool, id).await, |
| 145 |
None => Ok(None), |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
#[tracing::instrument(skip_all)] |
| 151 |
pub async fn list_invitations( |
| 152 |
pool: &PgPool, |
| 153 |
group_id: SyncGroupId, |
| 154 |
) -> Result<Vec<DbSyncGroupInvitation>> { |
| 155 |
let sql = format!("{SELECT_INVITATION} WHERE i.group_id = $1 ORDER BY i.created_at DESC"); |
| 156 |
let invitations = sqlx::query_as::<_, DbSyncGroupInvitation>(&sql) |
| 157 |
.bind(group_id) |
| 158 |
.fetch_all(pool) |
| 159 |
.await?; |
| 160 |
Ok(invitations) |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
#[tracing::instrument(skip_all)] |
| 170 |
pub async fn redeem_invitation( |
| 171 |
pool: &PgPool, |
| 172 |
group_id: SyncGroupId, |
| 173 |
id: SyncGroupInvitationId, |
| 174 |
) -> Result<bool> { |
| 175 |
let result = sqlx::query( |
| 176 |
r" |
| 177 |
UPDATE sync_group_invitations |
| 178 |
SET redeemed_at = NOW() |
| 179 |
WHERE id = $1 AND group_id = $2 |
| 180 |
AND accepted_at IS NOT NULL |
| 181 |
AND redeemed_at IS NULL |
| 182 |
AND revoked_at IS NULL |
| 183 |
", |
| 184 |
) |
| 185 |
.bind(id) |
| 186 |
.bind(group_id) |
| 187 |
.execute(pool) |
| 188 |
.await?; |
| 189 |
Ok(result.rows_affected() > 0) |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
#[tracing::instrument(skip_all)] |
| 196 |
pub async fn revoke_invitation( |
| 197 |
pool: &PgPool, |
| 198 |
group_id: SyncGroupId, |
| 199 |
id: SyncGroupInvitationId, |
| 200 |
) -> Result<bool> { |
| 201 |
let result = sqlx::query( |
| 202 |
r" |
| 203 |
UPDATE sync_group_invitations |
| 204 |
SET revoked_at = NOW() |
| 205 |
WHERE id = $1 AND group_id = $2 |
| 206 |
AND redeemed_at IS NULL |
| 207 |
AND revoked_at IS NULL |
| 208 |
", |
| 209 |
) |
| 210 |
.bind(id) |
| 211 |
.bind(group_id) |
| 212 |
.execute(pool) |
| 213 |
.await?; |
| 214 |
Ok(result.rows_affected() > 0) |
| 215 |
} |
| 216 |
|