feat(core): implement create group

This commit is contained in:
Paul Makles
2023-09-03 18:12:08 +01:00
parent eb1f45d208
commit 4270f0c5d4
8 changed files with 161 additions and 80 deletions

View File

@@ -1,6 +1,6 @@
use std::collections::HashMap;
use revolt_models::v0::MessageAuthor;
use revolt_models::v0::{self, MessageAuthor};
use revolt_permissions::OverrideField;
use revolt_result::Result;
use serde::{Deserialize, Serialize};
@@ -168,7 +168,7 @@ auto_derived!(
#[allow(clippy::disallowed_methods)]
impl Channel {
/// Create a channel
/* /// Create a channel
pub async fn create(&self, db: &Database) -> Result<()> {
db.insert_channel(self).await?;
@@ -186,6 +186,39 @@ impl Channel {
}
Ok(())
}*/
/// Create a group
pub async fn create_group(
db: &Database,
data: v0::DataCreateGroup,
owner_id: String,
) -> Result<Channel> {
let recipients = data.users.into_iter().collect::<Vec<String>>();
let channel = Channel::Group {
id: ulid::Ulid::new().to_string(),
name: data.name,
owner: owner_id,
description: data.description,
recipients: recipients.clone(),
icon: None,
last_message_id: None,
permissions: None,
nsfw: data.nsfw.unwrap_or(false),
};
db.insert_channel(&channel).await?;
let event = EventV1::ChannelCreate(channel.clone().into());
for recipient in recipients {
event.clone().private(recipient).await;
}
Ok(channel)
}
/// Add user to a group

View File

@@ -182,6 +182,21 @@ impl User {
Ok(user)
}
/// Get the relationship with another user
pub fn relationship_with(&self, user_b: &str) -> RelationshipStatus {
if self.id == user_b {
return RelationshipStatus::User;
}
if let Some(relations) = &self.relations {
if let Some(relationship) = relations.iter().find(|x| x.id == user_b) {
return relationship.status.clone();
}
}
RelationshipStatus::None
}
/// Check whether two users have a mutual connection
///
/// This will check if user and user_b share a server or a group.