Skip to content

Commit

Permalink
feat: Support Self-note
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulrahman1s committed Jul 10, 2022
1 parent de357de commit e2fd6f0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
13 changes: 10 additions & 3 deletions src/routes/users/open_dm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::extractors::*;
use crate::gateway::*;
use crate::structures::*;
use crate::utils::*;

Expand All @@ -7,7 +8,7 @@ pub async fn open_dm(
Path(id): Path<i64>,
) -> Result<Json<Channel>> {
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)
Expand All @@ -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())
}
10 changes: 7 additions & 3 deletions src/utils/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down

0 comments on commit e2fd6f0

Please sign in to comment.