From e2fd6f0095072eb230ff75f0bd3520739ea2d724 Mon Sep 17 00:00:00 2001 From: Abdulrahman Salah Date: Sun, 10 Jul 2022 15:21:21 +0000 Subject: [PATCH] feat: Support Self-note --- src/routes/users/open_dm.rs | 13 ++++++++++--- src/utils/permissions.rs | 10 +++++++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/routes/users/open_dm.rs b/src/routes/users/open_dm.rs index 41e8199..a12d5fc 100644 --- a/src/routes/users/open_dm.rs +++ b/src/routes/users/open_dm.rs @@ -1,4 +1,5 @@ use crate::extractors::*; +use crate::gateway::*; use crate::structures::*; use crate::utils::*; @@ -7,7 +8,7 @@ pub async fn open_dm( Path(id): Path, ) -> Result> { let channel = Channel::select() - .filter("type = $1 AND recipients @> $2 AND recipients @> $3") + .filter("type = $1 AND recipients @> ARRAY[$2, $3]::BIGINT[]") .bind(ChannelTypes::Direct) .bind(user.id) .bind(id) @@ -19,7 +20,13 @@ pub async fn open_dm( } let target = id.user().await?; - let channel = Channel::new_dm(user.id, target.id); + let channel = Channel::new_dm(user.id, target.id).save().await?; - Ok(channel.save().await?.into()) + publish(user.id, Payload::ChannelCreate(channel.clone())).await; + + if target.id != user.id { + publish(target.id, Payload::ChannelCreate(channel.clone())).await; + } + + Ok(channel.into()) } diff --git a/src/utils/permissions.rs b/src/utils/permissions.rs index 1de44fa..a4a1ab8 100644 --- a/src/utils/permissions.rs +++ b/src/utils/permissions.rs @@ -76,10 +76,14 @@ impl Permissions { if channel.is_dm() { p.insert(*DEFAULT_PERMISSION_DM); - let status = user.relations.0.values().next().unwrap(); + let recipients = channel.recipients.as_ref().unwrap(); + let is_notes = || recipients[0] == recipients[1]; - if status != &RelationshipStatus::Friend { - p.remove(Permissions::SEND_MESSAGES); + if !is_notes() { + let status = user.relations.0.get(&recipients[1]).unwrap(); + if status != &RelationshipStatus::Friend { + p.remove(Permissions::SEND_MESSAGES); + } } }