Skip to content

Commit

Permalink
WIP party changes
Browse files Browse the repository at this point in the history
  • Loading branch information
novacrazy committed Jan 9, 2024
1 parent 7bd4194 commit bd18c2b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 35 deletions.
14 changes: 4 additions & 10 deletions src/models/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,9 @@ pub mod events {
#[cfg_attr(feature = "rkyv", archive(check_bytes))]
pub struct TypingStart {
pub room_id: Snowflake,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub party_id: Option<Snowflake>,
pub party_id: Snowflake,
pub user_id: Snowflake,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub member: Option<PartyMember>,
pub member: PartyMember,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub parent: Option<Snowflake>,
// maybe timestamp?
Expand Down Expand Up @@ -212,10 +210,7 @@ pub mod events {
pub struct MessageDeleteEvent {
pub id: Snowflake,
pub room_id: Snowflake,

#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "rkyv", with(NicheSnowflake))]
pub party_id: Option<Snowflake>,
pub party_id: Snowflake,
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -267,8 +262,7 @@ pub mod events {
pub struct UserReactionEvent {
pub user_id: Snowflake,
pub room_id: Snowflake,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub party_id: Option<Snowflake>,
pub party_id: Snowflake,
pub msg_id: Snowflake,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "rkyv", with(rkyv::with::Niche))]
Expand Down
9 changes: 2 additions & 7 deletions src/models/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,12 @@ common::impl_rkyv_for_pod!(MessageKind);
pub struct Message {
pub id: Snowflake,
pub room_id: Snowflake,

#[serde(default, skip_serializing_if = "Option::is_none")]
pub party_id: Option<Snowflake>,
pub party_id: Snowflake,

#[serde(default, skip_serializing_if = "is_default")]
pub kind: MessageKind,

pub author: User,

#[serde(default, skip_serializing_if = "Option::is_none")]
pub member: Option<PartialPartyMember>,
pub author: PartyMember,

#[serde(default, skip_serializing_if = "Option::is_none")]
pub parent: Option<Snowflake>,
Expand Down
34 changes: 17 additions & 17 deletions src/models/party/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ bitflags::bitflags! {
/// the minimum age required to join.
const ADULT = 1 << 5;

/// Another way to refer to a direct-message is a "closed" party.
const CLOSED = 1 << 6;

/// Top 6 bits are a language code
const LANGUAGE = 0b11_11_11 << (32 - 6);

Expand Down Expand Up @@ -108,6 +111,12 @@ bitflags::bitflags! {
}
}

impl Default for PartyMemberFlags {
fn default() -> Self {
PartyMemberFlags::empty()
}
}

common::impl_serde_for_bitflags!(PartyMemberFlags);
common::impl_schema_for_bitflags!(PartyMemberFlags);
common::impl_sql_for_bitflags!(PartyMemberFlags);
Expand All @@ -116,35 +125,26 @@ common::impl_sql_for_bitflags!(PartyMemberFlags);
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
#[cfg_attr(feature = "rkyv", archive(check_bytes))]
pub struct PartialPartyMember {
pub struct PartyMember {
pub user: User,

/// Will be `None` if no longer in party
pub joined_at: Option<Timestamp>,

#[serde(default, skip_serializing_if = "Option::is_none")]
pub flags: Option<PartyMemberFlags>,
#[serde(default, skip_serializing_if = "PartyMemberFlags::is_empty")]
pub flags: PartyMemberFlags,

/// List of Role id snowflakes
/// List of Role id snowflakes, may be excluded from some queries
#[serde(default, skip_serializing_if = "ThinVec::is_empty")]
#[cfg_attr(feature = "rkyv", with(rkyv::with::CopyOptimize))]
pub roles: ThinVec<Snowflake>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
#[cfg_attr(feature = "rkyv", archive(check_bytes))]
pub struct PartyMember {
pub user: User,

#[serde(flatten)]
pub partial: PartialPartyMember,
}

impl Deref for PartyMember {
type Target = PartialPartyMember;
type Target = User;

fn deref(&self) -> &Self::Target {
&self.partial
&self.user
}
}

Expand Down
14 changes: 13 additions & 1 deletion src/models/permission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ bitflags::bitflags! {
const CHANGE_NICKNAME = 1u128 << 13;
const MANAGE_PERMS = 1u128 << 14;

const DEFAULT_ONLY = 1u128 << 20;

const VIEW_ROOM = 1u128 << 30;
const READ_MESSAGE_HISTORY = 1u128 << 31 | Self::VIEW_ROOM.bits;
const SEND_MESSAGES = 1u128 << 32 | Self::VIEW_ROOM.bits;
Expand Down Expand Up @@ -234,11 +236,21 @@ impl Permissions {
self.contains(Permissions::ADMINISTRATOR)
}

pub fn compute_overwrites(mut self, overwrites: &[Overwrite], roles: &[Snowflake], user_id: Snowflake) -> Permissions {
pub const fn normalize(self) -> Self {
if self.contains(Permissions::DEFAULT_ONLY) {
return Permissions::DEFAULT;
}

if self.contains(Permissions::ADMINISTRATOR) {
return Permissions::all();
}

self
}

pub fn compute_overwrites(mut self, overwrites: &[Overwrite], roles: &[Snowflake], user_id: Snowflake) -> Permissions {
self = self.normalize();

let mut allow = Permissions::empty();
let mut deny = Permissions::empty();

Expand Down

0 comments on commit bd18c2b

Please sign in to comment.