feat: Message pinning
This commit is contained in:
@@ -87,3 +87,8 @@ pub mod tasks;
|
||||
pub fn if_false(t: &bool) -> bool {
|
||||
!t
|
||||
}
|
||||
|
||||
/// Utility function to check if an option doesnt contain true
|
||||
pub fn if_option_false(t: &Option<bool>) -> bool {
|
||||
t != &Some(true)
|
||||
}
|
||||
@@ -65,6 +65,9 @@ auto_derived_partial!(
|
||||
/// Name and / or avatar overrides for this message
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub masquerade: Option<Masquerade>,
|
||||
/// Whether or not the message in pinned
|
||||
#[serde(skip_serializing_if = "crate::if_option_false")]
|
||||
pub pinned: Option<bool>,
|
||||
|
||||
/// Bitfield of message flags
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
@@ -168,6 +171,8 @@ auto_derived!(
|
||||
pub author: Option<String>,
|
||||
/// Search query
|
||||
pub query: Option<String>,
|
||||
/// Search for pinned
|
||||
pub pinned: Option<bool>,
|
||||
}
|
||||
|
||||
/// Message Query
|
||||
@@ -205,6 +210,7 @@ impl Default for Message {
|
||||
interactions: Default::default(),
|
||||
masquerade: None,
|
||||
flags: None,
|
||||
pinned: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -571,6 +577,8 @@ impl Message {
|
||||
users.push(id.clone());
|
||||
}
|
||||
v0::SystemMessage::Text { .. } => {}
|
||||
v0::SystemMessage::MessagePinned { .. } => {},
|
||||
v0::SystemMessage::MessageUnpinned { .. } => {},
|
||||
}
|
||||
}
|
||||
users
|
||||
|
||||
@@ -50,6 +50,10 @@ impl AbstractMessages for MongoDb {
|
||||
false
|
||||
};
|
||||
|
||||
if let Some(pinned) = query.filter.pinned {
|
||||
filter.insert("pinned", pinned);
|
||||
};
|
||||
|
||||
// 2. Find query limit
|
||||
let limit = query.limit.unwrap_or(50);
|
||||
|
||||
@@ -156,6 +160,7 @@ impl AbstractMessages for MongoDb {
|
||||
.build(),
|
||||
)
|
||||
.await
|
||||
.inspect_err(|e| eprintln!("{e:?}"))
|
||||
.map_err(|_| create_database_error!("find", COL))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,6 +56,12 @@ impl AbstractMessages for ReferenceDb {
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(pinned) = query.filter.pinned {
|
||||
if message.pinned.unwrap_or_default() == pinned {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
})
|
||||
.cloned()
|
||||
|
||||
@@ -482,6 +482,7 @@ impl crate::Message {
|
||||
interactions: self.interactions.into(),
|
||||
masquerade: self.masquerade.map(Into::into),
|
||||
flags: self.flags.map(|flags| flags as u32).unwrap_or_default(),
|
||||
pinned: self.pinned,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -509,6 +510,7 @@ impl From<crate::PartialMessage> for PartialMessage {
|
||||
interactions: value.interactions.map(Into::into),
|
||||
masquerade: value.masquerade.map(Into::into),
|
||||
flags: value.flags.map(|flags| flags as u32),
|
||||
pinned: value.pinned,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,3 +66,8 @@ pub fn if_false(t: &bool) -> bool {
|
||||
pub fn if_zero_u32(t: &u32) -> bool {
|
||||
t == &0
|
||||
}
|
||||
|
||||
/// Utility function to check if an option doesnt contain true
|
||||
pub fn if_option_false(t: &Option<bool>) -> bool {
|
||||
t != &Some(true)
|
||||
}
|
||||
|
||||
@@ -70,6 +70,9 @@ auto_derived_partial!(
|
||||
/// Name and / or avatar overrides for this message
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub masquerade: Option<Masquerade>,
|
||||
/// Whether or not the message in pinned
|
||||
#[serde(skip_serializing_if = "crate::if_option_false")]
|
||||
pub pinned: Option<bool>,
|
||||
|
||||
/// Bitfield of message flags
|
||||
///
|
||||
@@ -127,6 +130,10 @@ auto_derived!(
|
||||
ChannelIconChanged { by: String },
|
||||
#[serde(rename = "channel_ownership_changed")]
|
||||
ChannelOwnershipChanged { from: String, to: String },
|
||||
#[serde(rename = "message_pinned")]
|
||||
MessagePinned { id: String },
|
||||
#[serde(rename = "message_unpinned")]
|
||||
MessageUnpinned { id: String },
|
||||
}
|
||||
|
||||
/// Name and / or avatar override information
|
||||
@@ -298,7 +305,9 @@ auto_derived!(
|
||||
///
|
||||
/// See [MongoDB documentation](https://docs.mongodb.com/manual/text-search/#-text-operator) for more information.
|
||||
#[cfg_attr(feature = "validator", validate(length(min = 1, max = 64)))]
|
||||
pub query: String,
|
||||
pub query: Option<String>,
|
||||
/// Whether to only search for pinned messages, cannot be sent with `query`.
|
||||
pub pinned: Option<bool>,
|
||||
|
||||
/// Maximum number of messages to fetch
|
||||
#[cfg_attr(feature = "validator", validate(range(min = 1, max = 100)))]
|
||||
@@ -418,6 +427,8 @@ impl From<SystemMessage> for String {
|
||||
SystemMessage::ChannelOwnershipChanged { .. } => {
|
||||
"Channel ownership changed.".to_string()
|
||||
}
|
||||
SystemMessage::MessagePinned { .. } => "Message pinned.".to_string(),
|
||||
SystemMessage::MessageUnpinned { .. } => "Message unpinned.".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,6 +90,8 @@ pub enum ErrorType {
|
||||
},
|
||||
AlreadyInGroup,
|
||||
NotInGroup,
|
||||
AlreadyPinned,
|
||||
NotPinned,
|
||||
|
||||
// ? Server related errors
|
||||
UnknownServer,
|
||||
|
||||
@@ -40,6 +40,8 @@ impl<'r> Responder<'r, 'static> for Error {
|
||||
ErrorType::GroupTooLarge { .. } => Status::Forbidden,
|
||||
ErrorType::AlreadyInGroup => Status::Conflict,
|
||||
ErrorType::NotInGroup => Status::NotFound,
|
||||
ErrorType::AlreadyPinned => Status::BadRequest,
|
||||
ErrorType::NotPinned => Status::BadRequest,
|
||||
|
||||
ErrorType::UnknownServer => Status::NotFound,
|
||||
ErrorType::InvalidRole => Status::NotFound,
|
||||
|
||||
Reference in New Issue
Block a user