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.

View File

@@ -1,10 +1,11 @@
use super::File;
use revolt_permissions::OverrideField;
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
auto_derived!(
/// Channel
#[serde(tag = "channel_type")]
pub enum Channel {
/// Personal "Saved Notes" channel which allows users to save messages
SavedMessages {
@@ -205,4 +206,28 @@ auto_derived!(
#[cfg_attr(feature = "serde", serde(default))]
pub remove: Option<Vec<FieldsChannel>>,
}
/// Create new group
#[derive(Default)]
#[cfg_attr(feature = "validator", derive(validator::Validate))]
pub struct DataCreateGroup {
/// Group name
#[validate(length(min = 1, max = 32))]
pub name: String,
/// Group description
#[validate(length(min = 0, max = 1024))]
pub description: Option<String>,
/// Group icon
#[validate(length(min = 1, max = 128))]
pub icon: Option<String>,
/// Array of user IDs to add to the group
///
/// Must be friends with these users.
#[validate(length(min = 0, max = 49))]
#[serde(default)]
pub users: HashSet<String>,
/// Whether this group is age-restricted
#[serde(skip_serializing_if = "Option::is_none")]
pub nsfw: Option<bool>,
}
);