mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 21:47:02 +00:00
Merge branch 'master' of github.com:revoltchat/backend into webhooks
This commit is contained in:
@@ -23,11 +23,7 @@ impl Cache {
|
||||
pub async fn can_view_channel(&self, db: &Database, channel: &Channel) -> bool {
|
||||
match &channel {
|
||||
Channel::TextChannel { server, .. } | Channel::VoiceChannel { server, .. } => {
|
||||
let member = self
|
||||
.members
|
||||
.iter()
|
||||
.map(|(_, x)| x)
|
||||
.find(|x| &x.id.server == server);
|
||||
let member = self.members.values().find(|x| &x.id.server == server);
|
||||
|
||||
let server = self.servers.get(server);
|
||||
let mut perms = perms(self.users.get(&self.user_id).unwrap()).channel(channel);
|
||||
|
||||
@@ -395,7 +395,7 @@ impl BulkMessageResponse {
|
||||
) -> Result<BulkMessageResponse> {
|
||||
if let Some(true) = include_users {
|
||||
let user_ids = messages.get_user_ids();
|
||||
let users = db.fetch_users(&user_ids).await?;
|
||||
let users = User::fetch_foreign_users(db, &user_ids).await?;
|
||||
|
||||
Ok(match channel {
|
||||
Channel::TextChannel { server, .. } | Channel::VoiceChannel { server, .. } => {
|
||||
|
||||
@@ -4,6 +4,7 @@ use crate::models::user::{
|
||||
};
|
||||
use crate::permissions::defn::UserPerms;
|
||||
use crate::permissions::r#impl::user::get_relationship;
|
||||
use crate::presence::presence_filter_online;
|
||||
use crate::{perms, Database, Error, Result};
|
||||
|
||||
use futures::try_join;
|
||||
@@ -67,6 +68,7 @@ impl User {
|
||||
}
|
||||
|
||||
/// Mutate the user object to remove redundant information
|
||||
#[must_use]
|
||||
pub fn foreign(mut self) -> User {
|
||||
self.profile = None;
|
||||
self.relations = None;
|
||||
@@ -94,6 +96,21 @@ impl User {
|
||||
self
|
||||
}
|
||||
|
||||
/// Fetch foreign users by a list of IDs
|
||||
pub async fn fetch_foreign_users(db: &Database, user_ids: &[String]) -> Result<Vec<User>> {
|
||||
let online_ids = presence_filter_online(user_ids).await;
|
||||
|
||||
Ok(db
|
||||
.fetch_users(user_ids)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|mut user| {
|
||||
user.online = Some(online_ids.contains(&user.id));
|
||||
user.foreign()
|
||||
})
|
||||
.collect::<Vec<User>>())
|
||||
}
|
||||
|
||||
/// Mutate the user object to include relationship (if it does not already exist)
|
||||
#[must_use]
|
||||
pub fn with_relationship(self, perspective: &User) -> User {
|
||||
|
||||
@@ -161,7 +161,7 @@ pub struct PartialChannel {
|
||||
}
|
||||
|
||||
/// Optional fields on channel object
|
||||
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Clone)]
|
||||
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Eq, Clone)]
|
||||
pub enum FieldsChannel {
|
||||
Description,
|
||||
Icon,
|
||||
|
||||
@@ -81,7 +81,7 @@ pub struct Masquerade {
|
||||
pub name: Option<String>,
|
||||
/// Replace the avatar shown on this message (URL to image file)
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[validate(length(min = 1, max = 128))]
|
||||
#[validate(length(min = 1, max = 256))]
|
||||
pub avatar: Option<String>,
|
||||
/// Replace the display role colour shown on this message
|
||||
///
|
||||
|
||||
@@ -134,7 +134,7 @@ pub struct Server {
|
||||
}
|
||||
|
||||
/// Optional fields on server object
|
||||
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Clone)]
|
||||
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Eq, Clone)]
|
||||
pub enum FieldsServer {
|
||||
Description,
|
||||
Categories,
|
||||
@@ -144,7 +144,7 @@ pub enum FieldsServer {
|
||||
}
|
||||
|
||||
/// Optional fields on server object
|
||||
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Clone)]
|
||||
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Eq, Clone)]
|
||||
pub enum FieldsRole {
|
||||
Colour,
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ pub struct Member {
|
||||
}
|
||||
|
||||
/// Optional fields on server member object
|
||||
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Clone)]
|
||||
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Eq, Clone)]
|
||||
pub enum FieldsMember {
|
||||
Nickname,
|
||||
Avatar,
|
||||
|
||||
@@ -56,7 +56,7 @@ pub struct Bot {
|
||||
}
|
||||
|
||||
/// Optional fields on bot object
|
||||
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq)]
|
||||
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Eq)]
|
||||
pub enum FieldsBot {
|
||||
Token,
|
||||
InteractionsURL,
|
||||
|
||||
@@ -10,7 +10,7 @@ pub fn if_false(t: &bool) -> bool {
|
||||
}
|
||||
|
||||
/// User's relationship with another user (or themselves)
|
||||
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, PartialEq)]
|
||||
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, PartialEq, Eq)]
|
||||
pub enum RelationshipStatus {
|
||||
None,
|
||||
User,
|
||||
@@ -30,7 +30,7 @@ pub struct Relationship {
|
||||
}
|
||||
|
||||
/// Presence status
|
||||
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, PartialEq)]
|
||||
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Presence {
|
||||
/// User is online
|
||||
Online,
|
||||
@@ -163,7 +163,7 @@ pub struct User {
|
||||
}
|
||||
|
||||
/// Optional fields on user object
|
||||
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Clone)]
|
||||
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Eq, Clone)]
|
||||
pub enum FieldsUser {
|
||||
Avatar,
|
||||
StatusText,
|
||||
|
||||
@@ -97,7 +97,7 @@ impl From<i64> for PermissionValue {
|
||||
|
||||
impl From<u64> for PermissionValue {
|
||||
fn from(v: u64) -> Self {
|
||||
Self(v as u64)
|
||||
Self(v)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ pub enum Permission {
|
||||
/// Manage server customisation (includes emoji)
|
||||
ManageCustomisation = 1 << 4,
|
||||
|
||||
// % 1 bits reserved
|
||||
// % 1 bit reserved
|
||||
|
||||
// * Member permissions
|
||||
/// Kick other members below their ranking
|
||||
@@ -109,10 +109,11 @@ lazy_static! {
|
||||
+ Permission::Connect
|
||||
+ Permission::Speak;
|
||||
pub static ref DEFAULT_PERMISSION_SAVED_MESSAGES: u64 = Permission::GrantAllSafe as u64;
|
||||
pub static ref DEFAULT_PERMISSION_DIRECT_MESSAGE: u64 =
|
||||
*DEFAULT_PERMISSION + Permission::ManageChannel;
|
||||
pub static ref DEFAULT_PERMISSION_DIRECT_MESSAGE: u64 = *DEFAULT_PERMISSION
|
||||
+ Permission::ManageChannel
|
||||
+ Permission::React;
|
||||
pub static ref DEFAULT_PERMISSION_SERVER: u64 =
|
||||
*DEFAULT_PERMISSION + Permission::ChangeNickname + Permission::ChangeAvatar;
|
||||
*DEFAULT_PERMISSION + Permission::React + Permission::ChangeNickname + Permission::ChangeAvatar;
|
||||
}
|
||||
|
||||
bitfield! {
|
||||
|
||||
@@ -35,7 +35,7 @@ pub async fn presence_create_session(user_id: &str, flags: u8) -> (bool, u8) {
|
||||
__set_key_presence_entry(&mut conn, user_id, entry).await;
|
||||
|
||||
// Add to region set in case of failure.
|
||||
__add_to_set_sessions(&mut conn, &*REGION_KEY, user_id, session_id).await;
|
||||
__add_to_set_sessions(&mut conn, ®ION_KEY, user_id, session_id).await;
|
||||
(was_empty, session_id)
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ async fn presence_delete_session_internal(
|
||||
|
||||
// Remove from region set.
|
||||
if !skip_region {
|
||||
__remove_from_set_sessions(&mut conn, &*REGION_KEY, user_id, session_id).await;
|
||||
__remove_from_set_sessions(&mut conn, ®ION_KEY, user_id, session_id).await;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,7 +147,7 @@ pub async fn presence_clear_region(region_id: Option<&str>) {
|
||||
// also send out any relevant events.
|
||||
for session in sessions {
|
||||
let parts = session.split(':').collect::<Vec<&str>>();
|
||||
if let (Some(user_id), Some(session_id)) = (parts.get(0), parts.get(1)) {
|
||||
if let (Some(user_id), Some(session_id)) = (parts.first(), parts.get(1)) {
|
||||
if let Ok(session_id) = session_id.parse() {
|
||||
presence_delete_session_internal(user_id, session_id, true).await;
|
||||
}
|
||||
|
||||
@@ -53,5 +53,5 @@ pub async fn __get_set_sessions(conn: &mut Conn, region_id: &str) -> Vec<String>
|
||||
|
||||
/// Delete region session set
|
||||
pub async fn __delete_set_sessions(conn: &mut Conn, region_id: &str) {
|
||||
let _: () = conn.del(region_id).await.unwrap();
|
||||
conn.del::<_, ()>(region_id).await.unwrap();
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ pub async fn queue(channel: String, id: String, content: String) {
|
||||
pub async fn worker(db: Database) {
|
||||
loop {
|
||||
let task = Q.pop().await;
|
||||
if let Ok(embeds) = Embed::generate(task.content, &*JANUARY_URL, *MAX_EMBED_COUNT).await {
|
||||
if let Ok(embeds) = Embed::generate(task.content, &JANUARY_URL, *MAX_EMBED_COUNT).await {
|
||||
if let Err(err) = Message::append(
|
||||
&db,
|
||||
task.id,
|
||||
|
||||
@@ -51,7 +51,7 @@ impl Entry {
|
||||
}
|
||||
|
||||
/// Deduct one unit from the bucket and save
|
||||
pub fn deduct(mut self, key: u64) {
|
||||
pub fn deduct(mut self) {
|
||||
let current_time = now().as_millis();
|
||||
if current_time > self.reset {
|
||||
self.used = 1;
|
||||
@@ -59,7 +59,10 @@ impl Entry {
|
||||
} else {
|
||||
self.used += 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// Save information
|
||||
pub fn save(self, key: u64) {
|
||||
MAP.insert(key, self);
|
||||
}
|
||||
|
||||
@@ -189,10 +192,12 @@ impl Ratelimiter {
|
||||
let entry = Entry::from(key);
|
||||
|
||||
let remaining = entry.get_remaining(limit);
|
||||
let reset = entry.left_until_reset();
|
||||
|
||||
if remaining > 0 {
|
||||
entry.deduct(key);
|
||||
entry.deduct();
|
||||
|
||||
let reset = entry.left_until_reset();
|
||||
entry.save(key);
|
||||
|
||||
Ok(Ratelimiter {
|
||||
key,
|
||||
limit,
|
||||
@@ -200,7 +205,7 @@ impl Ratelimiter {
|
||||
reset,
|
||||
})
|
||||
} else {
|
||||
Err(reset)
|
||||
Err(entry.left_until_reset())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user