Added groups and roles field to messages

This commit is contained in:
xeovalyte 2025-02-24 16:19:35 +01:00
parent 0924e3c3fa
commit ea0a5a3ee2
Signed by: xeovalyte
SSH Key Fingerprint: SHA256:GWI1hq+MNKR2UOcvk7n9tekASXT8vyazK7vDF9Xyciw
5 changed files with 87 additions and 3 deletions

View File

@ -0,0 +1,3 @@
ALTER TABLE messages
ADD COLUMN member_groups bigint,
ADD COLUMN member_roles bigint;

View File

@ -1,7 +1,10 @@
use chrono::{DateTime, Utc};
use sqlx::{PgPool, Postgres, QueryBuilder};
use crate::model::message::{Channel, MessageStatus};
use crate::model::{
member::{Groups, Roles},
message::{Channel, MessageStatus},
};
#[derive(Debug)]
pub struct Message {
@ -12,6 +15,8 @@ pub struct Message {
pub title: String,
pub content: String,
pub channel: Channel,
pub member_groups: Groups,
pub member_roles: Roles,
pub thumbnail_url: Option<String>,
}
@ -28,6 +33,7 @@ impl Message {
status,
title, content,
channel,
member_groups, member_roles,
thumbnail_url
) VALUES (
$1,
@ -35,7 +41,8 @@ impl Message {
$4,
$5, $6,
$7,
$8
$8, $9,
$10
)
",
self.message_id,
@ -45,6 +52,8 @@ impl Message {
self.title,
self.content,
self.channel.bits() as i64,
self.member_groups.bits() as i64,
self.member_roles.bits() as i64,
self.thumbnail_url,
)
.execute(&mut **transaction)

View File

@ -84,12 +84,50 @@ impl From<i64> for Groups {
}
}
impl From<Option<Groups>> for Groups {
fn from(value: Option<Groups>) -> Self {
match value {
Some(groups) => groups,
None => Self::empty(),
}
}
}
impl Groups {
pub fn to_option(self) -> Option<Self> {
if self.is_empty() {
None
} else {
Some(self)
}
}
}
impl From<i64> for Roles {
fn from(value: i64) -> Self {
Self::from_bits(value as u64).unwrap_or(Roles::empty())
}
}
impl From<Option<Roles>> for Roles {
fn from(value: Option<Roles>) -> Self {
match value {
Some(roles) => roles,
None => Self::empty(),
}
}
}
impl Roles {
pub fn to_option(self) -> Option<Self> {
if self.is_empty() {
None
} else {
Some(self)
}
}
}
use crate::database::model::Member as DbMember;
impl From<DbMember> for Member {
fn from(value: DbMember) -> Self {

View File

@ -10,6 +10,8 @@ pub struct Message {
pub title: String,
pub content: String,
pub channel: Channel,
pub member_groups: Option<Groups>,
pub member_roles: Option<Roles>,
pub thumbnail_url: Option<String>,
}
@ -41,6 +43,8 @@ pub struct MessageCreate {
pub title: String,
pub content: String,
pub channel: Channel,
pub member_groups: Option<Groups>,
pub member_roles: Option<Roles>,
pub thumbnail_url: Option<String>,
}
@ -58,12 +62,16 @@ impl Message {
content: message_create.content,
channel: message_create.channel,
thumbnail_url: message_create.thumbnail_url,
member_groups: message_create.member_groups,
member_roles: message_create.member_roles,
status: MessageStatus::Pending,
}
}
}
use crate::database::model::Message as DbMessage;
use super::member::{Groups, Roles};
impl From<DbMessage> for Message {
fn from(value: DbMessage) -> Self {
Self {
@ -74,6 +82,8 @@ impl From<DbMessage> for Message {
title: value.title,
content: value.content,
channel: value.channel,
member_groups: value.member_groups.into(),
member_roles: value.member_roles.into(),
thumbnail_url: value.thumbnail_url,
}
}
@ -89,6 +99,8 @@ impl From<Message> for DbMessage {
title: value.title,
content: value.content,
channel: value.channel,
member_groups: value.member_groups.into(),
member_roles: value.member_roles.into(),
thumbnail_url: value.thumbnail_url,
}
}

View File

@ -10,7 +10,7 @@ use crate::{
auth::get_user_from_header,
database::model::Message as DbMessage,
model::{
member::Roles,
member::{Groups, Roles},
message::{Channel, MessageCreate},
Message,
},
@ -26,6 +26,8 @@ pub struct MessageCreateRequest {
title: String,
content: String,
channel: String,
member_groups: Option<String>,
member_roles: Option<String>,
}
pub async fn message_create(
@ -44,10 +46,30 @@ pub async fn message_create(
}
})?;
let member_groups: Option<Groups> = request
.member_groups
.map(|groups| {
bitflags::parser::from_str_strict(&groups).map_err(|_| crate::Error::BadRequest {
expected: String::from("Error while parsing member groups"),
})
})
.transpose()?;
let member_roles: Option<Roles> = request
.member_roles
.map(|roles| {
bitflags::parser::from_str_strict(&roles).map_err(|_| crate::Error::BadRequest {
expected: String::from("Error while parsing member roles"),
})
})
.transpose()?;
let db_message: DbMessage = Message::new(MessageCreate {
title: request.title,
content: request.content,
channel,
member_groups,
member_roles,
scheduled_at: None,
thumbnail_url: None,
})