Compare commits

...
28 Commits
Author SHA1 Message Date
Paul Makles f513a9cb5e chore: bump version to 0.7.15 2024-08-05 16:22:18 +01:00
Paul Makles 535f01604b fix(core/database): include users from pin events 2024-08-05 16:21:00 +01:00
Paul Makles f8ec6ba5ff fix(delta): add check to roles_fetch route 2024-08-05 16:20:21 +01:00
Paul Makles b8fdc07508 refactor: remove inspect_err debug code 2024-08-05 16:20:21 +01:00
Paul Makles 5ce7530206 chore: bump nixpkgs and MSRV 2024-08-05 16:20:21 +01:00
Paul Makles b36bac3225 merge: pull request #333 from revoltchat/feat/apns
APNS fixes
2024-08-05 17:17:29 +02:00
infi beef06ebe3 chore: applemusic option for special embed
See https://github.com/revoltchat/january/pull/41

Signed-off-by: Infi <infi@infi.sh>
2024-08-05 17:16:24 +02:00
Zomatree 4fc46f765b refactor: change Channel::id to return a reference 2024-07-31 00:05:40 +01:00
Zomatree b45ae2cd4d fix: include user for pinned system messages 2024-07-30 18:09:35 +01:00
Zomatree 0a8089787b chore: remove debugging logging 2024-07-30 18:06:54 +01:00
Paul Makles 475fa6f2e6 chore: add optional fields for CI as template [skip ci]
Signed-off-by: Paul Makles <me@insrt.uk>
2024-07-28 10:43:01 +01:00
Zomatree 96bf73cb57 fix: send pin system messages 2024-07-22 00:09:27 +01:00
Zomatree 87a9fb79f2 chore: Write tests for pinning 2024-07-21 22:22:46 +01:00
Zomatree c50435d499 fix: Switch to remove for unpinning 2024-07-21 22:16:23 +01:00
Zomatree d2f4823ceb chore: Add index and migration 2024-07-21 22:12:55 +01:00
Zomatree 39230c559a fix: Bump revolt_a2 dep 2024-07-20 22:43:12 +01:00
Zomatree 32d1d5df2e feat: Send badge updates from message acks 2024-07-20 20:49:22 +01:00
Zomatree 7547fbe245 Merge branch 'IAmTomahawkx-main' 2024-07-20 19:51:41 +01:00
IAmTomahawkx 9f13cb6f47 add Badge update capabilities
Signed-off-by: IAmTomahawkx <iamtomahawkx@gmail.com>
2024-07-19 13:13:13 -07:00
Tom d94571fbf9 Merge branch 'main' into main
Signed-off-by: Tom <iamtomahawkx@gmail.com>
2024-07-19 09:20:42 -07:00
Zomatree 389ecc0e5c feat: Message pinning 2024-07-18 18:37:41 +01:00
Paul Makles 412f4a99d7 feat(core/events): add user settings / unreads to Ready payload
refactor(core/events): make all Ready payload items optional
2024-07-14 17:03:40 +01:00
Paul Makles caa0795d07 fix(core/database); never include relations on known users 2024-07-14 16:27:11 +01:00
Paul Makles 5c82343bb3 chore: bump version to 0.7.14
Signed-off-by: IAmTomahawkx <iamtomahawkx@gmail.com>
2024-07-12 15:25:33 -07:00
Paul Makles e17af1c064 feat: add reason to ServerMemberLeave event
closes #314

Signed-off-by: IAmTomahawkx <iamtomahawkx@gmail.com>
2024-07-12 15:25:33 -07:00
Paul Makles a9e309395e fix: execute query to remove users from groups
Signed-off-by: IAmTomahawkx <iamtomahawkx@gmail.com>
2024-07-12 15:25:33 -07:00
IAmTomahawkx 8468ce19cb fix apple push notifications
Signed-off-by: IAmTomahawkx <iamtomahawkx@gmail.com>
2024-07-12 15:25:33 -07:00
IAmTomahawkx 27f15f7b02 change apns to use sandbox, and (somewhat) provide a custom payload
Signed-off-by: IAmTomahawkx <iamtomahawkx@gmail.com>
2024-07-12 15:25:33 -07:00
61 changed files with 1047 additions and 186 deletions
Generated
+10 -10
View File
@@ -3593,7 +3593,7 @@ dependencies = [
[[package]]
name = "revolt-bonfire"
version = "0.7.14"
version = "0.7.15"
dependencies = [
"async-channel 2.3.1",
"async-std",
@@ -3623,7 +3623,7 @@ dependencies = [
[[package]]
name = "revolt-config"
version = "0.7.14"
version = "0.7.15"
dependencies = [
"async-std",
"cached",
@@ -3639,7 +3639,7 @@ dependencies = [
[[package]]
name = "revolt-database"
version = "0.7.14"
version = "0.7.15"
dependencies = [
"async-lock",
"async-recursion",
@@ -3686,7 +3686,7 @@ dependencies = [
[[package]]
name = "revolt-delta"
version = "0.7.14"
version = "0.7.15"
dependencies = [
"async-channel 1.6.1",
"async-std",
@@ -3733,7 +3733,7 @@ dependencies = [
[[package]]
name = "revolt-models"
version = "0.7.14"
version = "0.7.15"
dependencies = [
"indexmap 1.9.3",
"iso8601-timestamp 0.2.11",
@@ -3750,7 +3750,7 @@ dependencies = [
[[package]]
name = "revolt-permissions"
version = "0.7.14"
version = "0.7.15"
dependencies = [
"async-std",
"async-trait",
@@ -3765,7 +3765,7 @@ dependencies = [
[[package]]
name = "revolt-presence"
version = "0.7.14"
version = "0.7.15"
dependencies = [
"async-std",
"log",
@@ -3776,7 +3776,7 @@ dependencies = [
[[package]]
name = "revolt-result"
version = "0.7.14"
version = "0.7.15"
dependencies = [
"revolt_okapi",
"revolt_rocket_okapi",
@@ -3788,9 +3788,9 @@ dependencies = [
[[package]]
name = "revolt_a2"
version = "0.10.0"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "466eb5262fcbb26e6e10b8a0acf56eb7cc095008132ff4ebddfc44d8672f8066"
checksum = "edbe1f79cb41271d3cd8f932d75dddeba963c19dc93d1ee6cbe0391b495ab2f5"
dependencies = [
"base64 0.21.3",
"erased-serde",
+1 -1
View File
@@ -17,7 +17,7 @@ Note: `january`, `autumn`, and `vortex` are yet to be moved into this monorepo.
## Minimum Supported Rust Version
Rust 1.70 or higher.
Rust 1.76 or higher.
## Development Guide
+2 -2
View File
@@ -1,6 +1,6 @@
[package]
name = "revolt-bonfire"
version = "0.7.14"
version = "0.7.15"
license = "AGPL-3.0-or-later"
edition = "2021"
@@ -41,7 +41,7 @@ revolt-result = { path = "../core/result" }
revolt-models = { path = "../core/models" }
revolt-config = { path = "../core/config" }
revolt-database = { path = "../core/database" }
revolt-permissions = { version = "0.7.14", path = "../core/permissions" }
revolt-permissions = { version = "0.7.15", path = "../core/permissions" }
revolt-presence = { path = "../core/presence", features = ["redis-is-patched"] }
# redis
+12
View File
@@ -1,5 +1,6 @@
use async_tungstenite::tungstenite::{handshake, Message};
use futures::channel::oneshot::Sender;
use revolt_database::events::client::ReadyPayloadFields;
use revolt_result::{create_error, Result};
use serde::{Deserialize, Serialize};
@@ -83,6 +84,17 @@ impl ProtocolConfiguration {
pub fn get_protocol_format(&self) -> &ProtocolFormat {
&self.format
}
/// Get ready payload fields
pub fn get_ready_payload_fields(&self) -> Vec<ReadyPayloadFields> {
vec![
ReadyPayloadFields::Users,
ReadyPayloadFields::Servers,
ReadyPayloadFields::Channels,
ReadyPayloadFields::Members,
ReadyPayloadFields::Emoji,
]
}
}
/// Object holding one side of a channel for receiving the parsed information
+61 -16
View File
@@ -1,8 +1,9 @@
use std::collections::HashSet;
use revolt_database::{
events::client::EventV1, util::permissions::DatabasePermissionQuery, Channel, Database, Member,
MemberCompositeKey, Presence, RelationshipStatus,
events::client::{EventV1, ReadyPayloadFields},
util::permissions::DatabasePermissionQuery,
Channel, Database, Member, MemberCompositeKey, Presence, RelationshipStatus,
};
use revolt_models::v0;
use revolt_permissions::{calculate_channel_permissions, ChannelPermission};
@@ -90,7 +91,11 @@ impl Cache {
/// State Manager
impl State {
/// Generate a Ready packet for the current user
pub async fn generate_ready_payload(&mut self, db: &Database) -> Result<EventV1> {
pub async fn generate_ready_payload(
&mut self,
db: &Database,
fields: Vec<ReadyPayloadFields>,
) -> Result<EventV1> {
let user = self.clone_user();
self.cache.is_bot = user.bot.is_some();
@@ -150,14 +155,36 @@ impl State {
.await?;
// Fetch customisations.
let emojis = db
.fetch_emoji_by_parent_ids(
&servers
.iter()
.map(|x| x.id.to_string())
.collect::<Vec<String>>(),
let emojis = if fields.contains(&ReadyPayloadFields::Emoji) {
Some(
db.fetch_emoji_by_parent_ids(
&servers
.iter()
.map(|x| x.id.to_string())
.collect::<Vec<String>>(),
)
.await?,
)
.await?;
} else {
None
};
// Fetch user settings
let user_settings = if let Some(ReadyPayloadFields::UserSettings(keys)) = fields
.iter()
.find(|e| matches!(e, ReadyPayloadFields::UserSettings(_)))
{
Some(db.fetch_user_settings(&user.id, &keys).await?)
} else {
None
};
// Fetch channel unreads
let channel_unreads = if fields.contains(&ReadyPayloadFields::ChannelUnreads) {
Some(db.fetch_unreads(&user.id).await?)
} else {
None
};
// Copy data into local state cache.
self.cache.users = users.iter().cloned().map(|x| (x.id.clone(), x)).collect();
@@ -201,13 +228,31 @@ impl State {
for channel in &channels {
self.insert_subscription(channel.id().to_string()).await;
}
Ok(EventV1::Ready {
users,
servers: servers.into_iter().map(Into::into).collect(),
channels: channels.into_iter().map(Into::into).collect(),
members: members.into_iter().map(Into::into).collect(),
emojis: emojis.into_iter().map(Into::into).collect(),
users: if fields.contains(&ReadyPayloadFields::Users) {
Some(users)
} else {
None
},
servers: if fields.contains(&ReadyPayloadFields::Servers) {
Some(servers.into_iter().map(Into::into).collect())
} else {
None
},
channels: if fields.contains(&ReadyPayloadFields::Channels) {
Some(channels.into_iter().map(Into::into).collect())
} else {
None
},
members: if fields.contains(&ReadyPayloadFields::Members) {
Some(members.into_iter().map(Into::into).collect())
} else {
None
},
emojis: emojis.map(|vec| vec.into_iter().map(Into::into).collect()),
user_settings,
channel_unreads: channel_unreads.map(|vec| vec.into_iter().map(Into::into).collect()),
})
}
+4 -1
View File
@@ -106,7 +106,10 @@ pub async fn client(db: &'static Database, stream: TcpStream, addr: SocketAddr)
}
// Download required data to local cache and send Ready payload.
let ready_payload = match state.generate_ready_payload(db).await {
let ready_payload = match state
.generate_ready_payload(db, config.get_ready_payload_fields())
.await
{
Ok(ready_payload) => ready_payload,
Err(err) => {
sentry::capture_error(&err);
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "revolt-config"
version = "0.7.14"
version = "0.7.15"
edition = "2021"
license = "MIT"
authors = ["Paul Makles <me@insrt.uk>"]
+4
View File
@@ -21,6 +21,9 @@ host = ""
username = ""
password = ""
from_address = ""
# reply_to = ""
# port = 587
# use_tls = true
[api.vapid]
private_key = "LS0tLS1CRUdJTiBFQyBQUklWQVRFIEtFWS0tLS0tCk1IY0NBUUVFSUJSUWpyTWxLRnBiVWhsUHpUbERvcEliYk1yeVNrNXpKYzVYVzIxSjJDS3hvQW9HQ0NxR1NNNDkKQXdFSG9VUURRZ0FFWnkrQkg2TGJQZ2hEa3pEempXOG0rUXVPM3pCajRXT1phdkR6ZU00c0pqbmFwd1psTFE0WAp1ZDh2TzVodU94QWhMQlU3WWRldVovWHlBdFpWZmNyQi9BPT0KLS0tLS1FTkQgRUMgUFJJVkFURSBLRVktLS0tLQo"
@@ -30,6 +33,7 @@ public_key = "BGcvgR-i2z4IQ5Mw841vJvkLjt8wY-FjmWrw83jOLCY52qcGZS0OF7nfLzuYbjsQIS
api_key = ""
[api.apn]
sandbox = false
pkcs8 = ""
key_id = ""
team_id = ""
+1
View File
@@ -79,6 +79,7 @@ pub struct ApiFcm {
#[derive(Deserialize, Debug, Clone)]
pub struct ApiApn {
pub sandbox: bool,
pub pkcs8: String,
pub key_id: String,
pub team_id: String,
+7 -7
View File
@@ -1,6 +1,6 @@
[package]
name = "revolt-database"
version = "0.7.14"
version = "0.7.15"
edition = "2021"
license = "AGPL-3.0-or-later"
authors = ["Paul Makles <me@insrt.uk>"]
@@ -23,13 +23,13 @@ default = ["mongodb", "async-std-runtime", "tasks"]
[dependencies]
# Core
revolt-config = { version = "0.7.14", path = "../config" }
revolt-result = { version = "0.7.14", path = "../result" }
revolt-models = { version = "0.7.14", path = "../models", features = [
revolt-config = { version = "0.7.15", path = "../config" }
revolt-result = { version = "0.7.15", path = "../result" }
revolt-models = { version = "0.7.15", path = "../models", features = [
"validator",
] }
revolt-presence = { version = "0.7.14", path = "../presence" }
revolt-permissions = { version = "0.7.14", path = "../permissions", features = [
revolt-presence = { version = "0.7.15", path = "../presence" }
revolt-permissions = { version = "0.7.15", path = "../permissions", features = [
"serde",
"bson",
] }
@@ -87,7 +87,7 @@ revolt_rocket_okapi = { version = "0.9.1", optional = true }
# Notifications
fcm = "0.9.2"
web-push = "0.10.0"
revolt_a2 = { version = "0.10.0", default-features = false, features = [
revolt_a2 = { version = "0.10", default-features = false, features = [
"ring",
] }
+30 -9
View File
@@ -2,10 +2,7 @@ use authifier::AuthifierEvent;
use serde::{Deserialize, Serialize};
use revolt_models::v0::{
AppendMessage, Channel, Emoji, FieldsChannel, FieldsMember, FieldsRole, FieldsServer,
FieldsUser, FieldsWebhook, Member, MemberCompositeKey, Message, PartialChannel, PartialMember,
PartialMessage, PartialRole, PartialServer, PartialUser, PartialWebhook, RemovalIntention,
Report, Server, User, UserSettings, Webhook,
AppendMessage, Channel, ChannelUnread, Emoji, FieldsChannel, FieldsMember, FieldsMessage, FieldsRole, FieldsServer, FieldsUser, FieldsWebhook, Member, MemberCompositeKey, Message, PartialChannel, PartialMember, PartialMessage, PartialRole, PartialServer, PartialUser, PartialWebhook, RemovalIntention, Report, Server, User, UserSettings, Webhook
};
use revolt_result::Error;
@@ -39,6 +36,19 @@ pub enum ErrorEvent {
APIError(Error),
}
/// Fields provided in Ready payload
#[derive(PartialEq)]
pub enum ReadyPayloadFields {
Users,
Servers,
Channels,
Members,
Emoji,
UserSettings(Vec<String>),
ChannelUnreads,
}
/// Protocol Events
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "type")]
@@ -52,11 +62,21 @@ pub enum EventV1 {
Logout,
/// Basic data to cache
Ready {
users: Vec<User>,
servers: Vec<Server>,
channels: Vec<Channel>,
members: Vec<Member>,
emojis: Vec<Emoji>,
#[serde(skip_serializing_if = "Option::is_none")]
users: Option<Vec<User>>,
#[serde(skip_serializing_if = "Option::is_none")]
servers: Option<Vec<Server>>,
#[serde(skip_serializing_if = "Option::is_none")]
channels: Option<Vec<Channel>>,
#[serde(skip_serializing_if = "Option::is_none")]
members: Option<Vec<Member>>,
#[serde(skip_serializing_if = "Option::is_none")]
emojis: Option<Vec<Emoji>>,
#[serde(skip_serializing_if = "Option::is_none")]
user_settings: Option<UserSettings>,
#[serde(skip_serializing_if = "Option::is_none")]
channel_unreads: Option<Vec<ChannelUnread>>,
},
/// Ping response
@@ -69,6 +89,7 @@ pub enum EventV1 {
id: String,
channel: String,
data: PartialMessage,
clear: Vec<FieldsMessage>,
},
/// Append information to existing message
+5
View File
@@ -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)
}
@@ -146,7 +146,14 @@ pub async fn create_database(db: &MongoDb) {
"author": 1_i32
},
"name": "author"
}
},
{
"key": {
"channel": 1_i32,
"pinned": 1_i32
},
"name": "channel_pinned_compound"
},
]
},
None,
@@ -20,7 +20,7 @@ struct MigrationInfo {
revision: i32,
}
pub const LATEST_REVISION: i32 = 27;
pub const LATEST_REVISION: i32 = 28;
pub async fn migrate_database(db: &MongoDb) {
let migrations = db.col::<Document>("migrations");
@@ -1071,6 +1071,29 @@ pub async fn run_migrations(db: &MongoDb, revision: i32) -> i32 {
}
}
if revision <= 27 {
info!("Running migration [revision 27 / 21-07-2024]: create message pinned index.");
db.db()
.run_command(
doc! {
"createIndexes": "messages",
"indexes": [
{
"key": {
"channel": 1_i32,
"pinned": 1_i32
},
"name": "channel_pinned_compound"
}
]
},
None,
)
.await
.expect("Failed to create message index.");
}
// Need to migrate fields on attachments, change `user_id`, `object_id`, etc to `parent`.
// Reminder to update LATEST_REVISION when adding new migrations.
@@ -7,13 +7,13 @@ mod reference;
#[async_trait]
pub trait AbstractChannelUnreads: Sync + Send {
/// Acknowledge a message.
/// Acknowledge a message, and returns updated channel unread.
async fn acknowledge_message(
&self,
channel_id: &str,
user_id: &str,
message_id: &str,
) -> Result<()>;
) -> Result<Option<ChannelUnread>>;
/// Acknowledge many channels.
async fn acknowledge_channels(&self, user_id: &str, channel_ids: &[String]) -> Result<()>;
@@ -28,4 +28,7 @@ pub trait AbstractChannelUnreads: Sync + Send {
/// Fetch all channel unreads for a user.
async fn fetch_unreads(&self, user_id: &str) -> Result<Vec<ChannelUnread>>;
/// Fetch unread for a specific user in a channel.
async fn fetch_unread(&self, user_id: &str, channel_id: &str) -> Result<Option<ChannelUnread>>;
}
@@ -1,4 +1,6 @@
use bson::Document;
use mongodb::options::FindOneAndUpdateOptions;
use mongodb::options::ReturnDocument;
use mongodb::options::UpdateOptions;
use revolt_result::Result;
use ulid::Ulid;
@@ -12,31 +14,35 @@ static COL: &str = "channel_unreads";
#[async_trait]
impl AbstractChannelUnreads for MongoDb {
/// Acknowledge a message.
/// Acknowledge a message, and returns updated channel unread.
async fn acknowledge_message(
&self,
channel_id: &str,
user_id: &str,
message_id: &str,
) -> Result<()> {
self.col::<Document>(COL)
.update_one(
) -> Result<Option<ChannelUnread>> {
self.col::<ChannelUnread>(COL)
.find_one_and_update(
doc! {
"_id.channel": channel_id,
"_id.user": user_id,
},
doc! {
"$unset": {
"mentions": 1_i32
"$pull": {
"mentions": {
"$lt": message_id
}
},
"$set": {
"last_id": message_id
}
},
UpdateOptions::builder().upsert(true).build(),
FindOneAndUpdateOptions::builder()
.upsert(true)
.return_document(ReturnDocument::After)
.build(),
)
.await
.map(|_| ())
.map_err(|_| create_database_error!("update_one", COL))
}
@@ -116,4 +122,18 @@ impl AbstractChannelUnreads for MongoDb {
}
)
}
/// Fetch unread for a specific user in a channel.
async fn fetch_unread(&self, user_id: &str, channel_id: &str) -> Result<Option<ChannelUnread>> {
query!(
self,
find_one,
COL,
doc! {
"_id.user": user_id,
"_id.channel": channel_id
}
)
}
}
@@ -13,7 +13,7 @@ impl AbstractChannelUnreads for ReferenceDb {
channel_id: &str,
user_id: &str,
message_id: &str,
) -> Result<()> {
) -> Result<Option<ChannelUnread>> {
let mut unreads = self.channel_unreads.lock().await;
let key = ChannelCompositeKey {
channel: channel_id.to_string(),
@@ -27,14 +27,14 @@ impl AbstractChannelUnreads for ReferenceDb {
unreads.insert(
key.clone(),
ChannelUnread {
id: key,
id: key.clone(),
last_id: Some(message_id.to_string()),
mentions: None,
},
);
}
Ok(())
Ok(unreads.get(&key).cloned())
}
/// Acknowledge many channels.
@@ -87,4 +87,14 @@ impl AbstractChannelUnreads for ReferenceDb {
.cloned()
.collect())
}
/// Fetch unread for a specific user in a channel.
async fn fetch_unread(&self, user_id: &str, channel_id: &str) -> Result<Option<ChannelUnread>> {
let unreads = self.channel_unreads.lock().await;
Ok(unreads.get(&ChannelCompositeKey {
channel: channel_id.to_string(),
user: user_id.to_string()
}).cloned())
}
}
@@ -417,13 +417,13 @@ impl Channel {
}
/// Clone this channel's id
pub fn id(&self) -> String {
pub fn id(&self) -> &str {
match self {
Channel::DirectMessage { id, .. }
| Channel::Group { id, .. }
| Channel::SavedMessages { id, .. }
| Channel::TextChannel { id, .. }
| Channel::VoiceChannel { id, .. } => id.clone(),
| Channel::VoiceChannel { id, .. } => id,
}
}
@@ -266,7 +266,7 @@ impl AbstractChannels for MongoDb {
.await?;
// Delete the channel itself
query!(self, delete_one_by_id, COL, &channel.id()).map(|_| ())
query!(self, delete_one_by_id, COL, channel.id()).map(|_| ())
}
}
@@ -11,7 +11,7 @@ impl AbstractChannels for ReferenceDb {
/// Insert a new channel in the database
async fn insert_channel(&self, channel: &Channel) -> Result<()> {
let mut channels = self.channels.lock().await;
if let Entry::Vacant(entry) = channels.entry(channel.id()) {
if let Entry::Vacant(entry) = channels.entry(channel.id().to_string()) {
entry.insert(channel.clone());
Ok(())
} else {
@@ -148,7 +148,7 @@ impl AbstractChannels for ReferenceDb {
// Delete a channel
async fn delete_channel(&self, channel: &Channel) -> Result<()> {
let mut channels = self.channels.lock().await;
if channels.remove(&channel.id()).is_some() {
if channels.remove(channel.id()).is_some() {
Ok(())
} else {
Err(create_error!(NotFound))
@@ -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")]
@@ -99,6 +102,10 @@ auto_derived!(
ChannelIconChanged { by: String },
#[serde(rename = "channel_ownership_changed")]
ChannelOwnershipChanged { from: String, to: String },
#[serde(rename = "message_pinned")]
MessagePinned { id: String, by: String },
#[serde(rename = "message_unpinned")]
MessageUnpinned { id: String, by: String },
}
/// Name and / or avatar override information
@@ -168,6 +175,8 @@ auto_derived!(
pub author: Option<String>,
/// Search query
pub query: Option<String>,
/// Search for pinned
pub pinned: Option<bool>,
}
/// Message Query
@@ -183,6 +192,11 @@ auto_derived!(
#[serde(flatten)]
pub time_period: MessageTimePeriod,
}
/// Optional fields on message
pub enum FieldsMessage {
Pinned,
}
);
#[allow(clippy::derivable_impls)]
@@ -205,6 +219,7 @@ impl Default for Message {
interactions: Default::default(),
masquerade: None,
flags: None,
pinned: None,
}
}
}
@@ -278,7 +293,7 @@ impl Message {
let message_id = Ulid::new().to_string();
let mut message = Message {
id: message_id.clone(),
channel: channel.id(),
channel: channel.id().to_string(),
masquerade: data.masquerade.map(|masquerade| masquerade.into()),
interactions: data
.interactions
@@ -444,8 +459,8 @@ impl Message {
) -> Result<()> {
self.send_without_notifications(
db,
user,
member,
user.clone(),
member.clone(),
matches!(channel, Channel::DirectMessage { .. }),
generate_embeds,
)
@@ -463,9 +478,9 @@ impl Message {
}
},
PushNotification::from(
self.clone().into_model(None, None),
self.clone().into_model(user, member),
Some(author),
&channel.id(),
channel.id(),
)
.await,
)
@@ -513,14 +528,26 @@ impl Message {
}
/// Update message data
pub async fn update(&mut self, db: &Database, partial: PartialMessage) -> Result<()> {
pub async fn update(
&mut self,
db: &Database,
partial: PartialMessage,
remove: Vec<FieldsMessage>,
) -> Result<()> {
self.apply_options(partial.clone());
db.update_message(&self.id, &partial).await?;
for field in &remove {
self.remove_field(field);
}
db.update_message(&self.id, &partial, remove.clone())
.await?;
EventV1::MessageUpdate {
id: self.id.clone(),
channel: self.channel.clone(),
data: partial.into(),
clear: remove.into_iter().map(|field| field.into()).collect(),
}
.p(self.channel.clone())
.await;
@@ -571,6 +598,12 @@ impl Message {
users.push(id.clone());
}
v0::SystemMessage::Text { .. } => {}
v0::SystemMessage::MessagePinned { by, .. } => {
users.push(by.clone());
}
v0::SystemMessage::MessageUnpinned { by, .. } => {
users.push(by.clone());
}
}
}
users
@@ -800,6 +833,12 @@ impl Message {
// Write to database
db.clear_reaction(&self.id, emoji).await
}
pub fn remove_field(&mut self, field: &FieldsMessage) {
match field {
FieldsMessage::Pinned => self.pinned = None,
}
}
}
impl SystemMessage {
@@ -1,6 +1,6 @@
use revolt_result::Result;
use crate::{AppendMessage, Message, MessageQuery, PartialMessage};
use crate::{AppendMessage, FieldsMessage, Message, MessageQuery, PartialMessage};
mod mongodb;
mod reference;
@@ -20,7 +20,7 @@ pub trait AbstractMessages: Sync + Send {
async fn fetch_messages_by_id(&self, ids: &[String]) -> Result<Vec<Message>>;
/// Update a given message with new information
async fn update_message(&self, id: &str, message: &PartialMessage) -> Result<()>;
async fn update_message(&self, id: &str, message: &PartialMessage, remove: Vec<FieldsMessage>) -> Result<()>;
/// Append information to a given message
async fn append_message(&self, id: &str, append: &AppendMessage) -> Result<()>;
@@ -5,7 +5,8 @@ use revolt_models::v0::MessageSort;
use revolt_result::Result;
use crate::{
AppendMessage, DocumentId, Message, MessageQuery, MessageTimePeriod, MongoDb, PartialMessage,
AppendMessage, DocumentId, FieldsMessage, IntoDocumentPath, Message, MessageQuery,
MessageTimePeriod, MongoDb, PartialMessage,
};
use super::AbstractMessages;
@@ -50,6 +51,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);
@@ -177,8 +182,22 @@ impl AbstractMessages for MongoDb {
}
/// Update a given message with new information
async fn update_message(&self, id: &str, message: &PartialMessage) -> Result<()> {
query!(self, update_one_by_id, COL, id, message, vec![], None).map(|_| ())
async fn update_message(
&self,
id: &str,
message: &PartialMessage,
remove: Vec<FieldsMessage>,
) -> Result<()> {
query!(
self,
update_one_by_id,
COL,
id,
message,
remove.iter().map(|x| x as &dyn IntoDocumentPath).collect(),
None
)
.map(|_| ())
}
/// Append information to a given message
@@ -296,6 +315,14 @@ impl AbstractMessages for MongoDb {
}
}
impl IntoDocumentPath for FieldsMessage {
fn as_path(&self) -> Option<&'static str> {
Some(match self {
FieldsMessage::Pinned => "pinned",
})
}
}
impl MongoDb {
pub async fn delete_bulk_messages(&self, projection: Document) -> Result<()> {
let mut for_attachments = projection.clone();
@@ -2,7 +2,7 @@ use futures::future::try_join_all;
use indexmap::IndexSet;
use revolt_result::Result;
use crate::{AppendMessage, Message, MessageQuery, PartialMessage, ReferenceDb};
use crate::{AppendMessage, FieldsMessage, Message, MessageQuery, PartialMessage, ReferenceDb};
use super::AbstractMessages;
@@ -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()
@@ -183,10 +189,15 @@ impl AbstractMessages for ReferenceDb {
}
/// Update a given message with new information
async fn update_message(&self, id: &str, message: &PartialMessage) -> Result<()> {
async fn update_message(&self, id: &str, message: &PartialMessage, remove: Vec<FieldsMessage>) -> Result<()> {
let mut messages = self.messages.lock().await;
if let Some(message_data) = messages.get_mut(id) {
message_data.apply_options(message.to_owned());
for field in remove {
#[allow(clippy::disallowed_methods)]
message_data.remove_field(&field);
}
Ok(())
} else {
Err(create_error!(NotFound))
@@ -81,7 +81,7 @@ impl Member {
server: &Server,
user: &User,
channels: Option<Vec<Channel>>,
) -> Result<Vec<Channel>> {
) -> Result<(Member, Vec<Channel>)> {
if db.fetch_ban(&server.id, &user.id).await.is_ok() {
return Err(create_error!(Banned));
}
@@ -155,7 +155,7 @@ impl Member {
.ok();
}
Ok(channels)
Ok((member, channels))
}
/// Update member data
@@ -175,7 +175,7 @@ impl Server {
vec![]
};
server.channels = channels.iter().map(|c| c.id()).collect();
server.channels = channels.iter().map(|c| c.id().to_string()).collect();
db.insert_server(&server).await?;
Ok((server, channels))
}
+40 -9
View File
@@ -5,7 +5,9 @@ use deadqueue::limited::Queue;
use once_cell::sync::Lazy;
use std::{collections::HashMap, time::Duration};
use super::DelayedTask;
use revolt_result::Result;
use super::{apple_notifications::{self, ApnJob}, DelayedTask};
/// Enumeration of possible events
#[derive(Debug, Eq, PartialEq)]
@@ -52,8 +54,43 @@ pub async fn queue(channel: String, user: String, event: AckEvent) {
info!("Queue is using {} slots from {}.", Q.len(), Q.capacity());
}
pub async fn handle_ack_event(event: &AckEvent, db: &Database, authifier_db: &authifier::Database, user: &str, channel: &str) -> Result<()> {
match &event {
#[allow(clippy::disallowed_methods)] // event is sent by higher level function
AckEvent::AckMessage { id } => {
let unread = db.fetch_unread(user, channel).await?;
let updated = db.acknowledge_message(channel, user, id).await?;
if let (Some(before), Some(after)) = (unread, updated) {
let before_mentions = before.mentions.unwrap_or_default().len();
let after_mentions = after.mentions.unwrap_or_default().len();
let mentions_acked = before_mentions - after_mentions;
if mentions_acked > 0 {
if let Ok(sessions) = authifier_db.find_sessions(user).await {
for session in sessions {
if let Some(sub) = session.subscription {
if sub.endpoint == "apn" {
apple_notifications::queue(ApnJob::from_ack(session.id, user.to_string(), sub.auth)).await;
}
}
}
}
};
}
},
AckEvent::AddMention { ids } => {
db.add_mention_to_unread(channel, user, ids).await?;
}
};
Ok(())
}
/// Start a new worker
pub async fn worker(db: Database) {
pub async fn worker(db: Database, authifier_db: authifier::Database) {
let mut tasks = HashMap::<(String, String), DelayedTask<Task>>::new();
let mut keys = vec![];
@@ -71,13 +108,7 @@ pub async fn worker(db: Database) {
let Task { event } = task.data;
let (user, channel) = key;
if let Err(err) = match &event {
#[allow(clippy::disallowed_methods)] // event is sent by higher level function
AckEvent::AckMessage { id } => db.acknowledge_message(channel, user, id).await,
AckEvent::AddMention { ids } => {
db.add_mention_to_unread(channel, user, ids).await
}
} {
if let Err(err) = handle_ack_event(&event, &db, &authifier_db, user, channel).await {
error!("{err:?} for {event:?}. ({user}, {channel})");
} else {
info!("User {user} ack in {channel} with {event:?}");
@@ -6,22 +6,64 @@ use base64::{
};
use deadqueue::limited::Queue;
use once_cell::sync::Lazy;
use revolt_a2::{Client, ClientConfig, DefaultNotificationBuilder};
use revolt_a2::{Error, ErrorBody, ErrorReason, NotificationBuilder, Response};
use revolt_a2::{
request::{
notification::{DefaultAlert, NotificationOptions},
payload::{APSAlert, APSSound, PayloadLike, APS},
},
Client, ClientConfig, Endpoint, Error, ErrorBody, ErrorReason, Priority, PushType, Response,
};
use revolt_config::config;
use revolt_models::v0::PushNotification;
use revolt_models::v0::{Message, PushNotification};
use crate::Database;
/// Payload information, before assembly
#[derive(Debug)]
pub struct ApnPayload {
message: Message,
url: String,
authorAvatar: String,
authorDisplayName: String,
channelName: String,
}
#[derive(Serialize, Debug)]
struct Payload<'a> {
aps: APS<'a>,
#[serde(skip_serializing)]
options: NotificationOptions<'a>,
#[serde(skip_serializing)]
device_token: &'a str,
message: &'a Message,
url: &'a str,
authorAvatar: &'a str,
authorDisplayName: &'a str,
channelName: &'a str,
}
impl<'a> PayloadLike for Payload<'a> {
fn get_device_token(&self) -> &'a str {
self.device_token
}
fn get_options(&self) -> &NotificationOptions {
&self.options
}
}
/// Task information
#[derive(Debug)]
pub struct ApnTask {
pub struct AlertJob {
/// Session Id
session_id: String,
/// Device token
device_token: String,
/// User Id
user_id: String,
/// Title
title: String,
@@ -30,32 +72,116 @@ pub struct ApnTask {
/// Thread Id
thread_id: String,
/// Category (informs the client what kind of notification is being sent.)
category: String,
/// Payload used by the iOS client to modify the notification
custom_payload: ApnPayload,
}
impl ApnTask {
impl AlertJob {
fn format_title(notification: &PushNotification) -> String {
// ideally this changes depending on context
// in a server, it would look like "Sendername, #channelname in servername"
// in a group, it would look like "Sendername in groupname"
// in a dm it should just be "Sendername".
// not sure how feasible all those are given the PushNotification object as it currently stands.
format!(
"{} in {}",
notification.author, notification.message.channel
) // TODO: this absolutely needs a channel name
}
}
#[derive(Debug)]
pub struct BadgeJob {
/// Session Id
session_id: String,
/// Device token
device_token: String,
/// User Id
user_id: String,
}
#[derive(Debug)]
pub enum JobType {
Alert(AlertJob),
Badge(BadgeJob),
}
#[derive(Debug)]
pub struct ApnJob {
job_type: JobType,
}
impl ApnJob {
pub fn from_notification(
session_id: String,
user_id: String,
device_token: String,
notification: &PushNotification,
) -> ApnTask {
ApnTask {
session_id,
device_token,
title: notification.author.to_string(),
body: notification.body.to_string(),
thread_id: notification.tag.to_string(),
) -> ApnJob {
ApnJob {
job_type: JobType::Alert(AlertJob {
session_id,
device_token,
user_id,
title: AlertJob::format_title(notification),
body: notification.body.to_string(),
thread_id: notification.tag.to_string(),
category: "ALERT_MESSAGE".to_string(),
custom_payload: ApnPayload {
message: notification.message.clone(),
url: notification.url.clone(),
authorAvatar: notification.icon.clone(),
authorDisplayName: notification.author.clone(),
channelName: "#fetchchannelnamehere".to_string(), // TODO: get actual channel name
},
}),
}
}
pub fn from_ack(session_id: String, user_id: String, device_token: String) -> ApnJob {
ApnJob {
job_type: JobType::Badge(BadgeJob {
session_id,
device_token,
user_id,
}),
}
}
}
static Q: Lazy<Queue<ApnTask>> = Lazy::new(|| Queue::new(10_000));
enum AssembledPayload<'a> {
Alert(Payload<'a>),
Default(revolt_a2::request::payload::Payload<'a>),
}
static Q: Lazy<Queue<ApnJob>> = Lazy::new(|| Queue::new(10_000));
/// Queue a new task for a worker
pub async fn queue(task: ApnTask) {
pub async fn queue(task: ApnJob) {
Q.try_push(task).ok();
info!("Queue is using {} slots from {}.", Q.len(), Q.capacity());
}
async fn get_badge_count(db: &Database, user: &str) -> Option<u32> {
if let Ok(unreads) = db.fetch_unreads(user).await {
let mut mention_count = 0;
for channel in unreads {
if let Some(mentions) = channel.mentions {
mention_count += mentions.len() as u32
}
}
return Some(mention_count);
}
None
}
/// Start a new worker
pub async fn worker(db: Database) {
let config = config().await;
@@ -67,27 +193,97 @@ pub async fn worker(db: Database) {
return;
}
let endpoint = if config.api.apn.sandbox {
Endpoint::Sandbox
} else {
Endpoint::Production
};
let pkcs8 = engine::general_purpose::STANDARD
.decode(config.api.apn.pkcs8)
.expect("valid `pcks8`");
let client_config = ClientConfig::new(endpoint);
let client = Client::token(
&mut Cursor::new(pkcs8),
config.api.apn.key_id,
config.api.apn.team_id,
ClientConfig::default(),
client_config,
)
.expect("could not create APN client");
let payload_options = NotificationOptions {
apns_id: None,
apns_push_type: Some(PushType::Alert),
apns_expiration: None,
apns_priority: Some(Priority::High),
apns_topic: Some("chat.revolt.app"),
apns_collapse_id: None,
};
loop {
let task = Q.pop().await;
let payload = DefaultNotificationBuilder::new()
.set_title(&task.title)
.set_body(&task.body)
.set_thread_id(&task.thread_id)
.build(&task.device_token, Default::default());
let payload: AssembledPayload;
if let Err(err) = client.send(payload).await {
match task.job_type {
JobType::Alert(ref alert) => {
payload = AssembledPayload::Alert(Payload {
aps: APS {
alert: Some(APSAlert::Default(DefaultAlert {
title: Some(&alert.title),
subtitle: None,
body: Some(&alert.body),
title_loc_key: None,
title_loc_args: None,
action_loc_key: None,
loc_key: None,
loc_args: None,
launch_image: None,
})),
badge: get_badge_count(&db, &alert.user_id).await,
sound: Some(APSSound::Sound("default")),
thread_id: Some(&alert.thread_id),
content_available: None,
category: Some(&alert.category),
mutable_content: Some(1),
url_args: None,
},
device_token: &alert.device_token,
options: payload_options.clone(),
message: &alert.custom_payload.message,
url: &alert.custom_payload.url,
authorAvatar: &alert.custom_payload.authorAvatar,
authorDisplayName: &alert.custom_payload.authorDisplayName,
channelName: &alert.custom_payload.channelName,
});
}
JobType::Badge(ref alert) => {
payload = AssembledPayload::Default(revolt_a2::request::payload::Payload {
aps: APS {
alert: None,
badge: get_badge_count(&db, &alert.user_id).await,
sound: None,
thread_id: None,
content_available: None,
category: None,
mutable_content: None,
url_args: None,
},
device_token: &alert.device_token,
options: payload_options.clone(),
data: std::collections::BTreeMap::new(),
})
}
}
let resp = match payload {
AssembledPayload::Alert(p) => client.send(p).await,
AssembledPayload::Default(p) => client.send(p).await,
};
//println!("response from APNS: {:?}", resp);
if let Err(err) = resp {
match err {
Error::ResponseError(Response {
error:
@@ -98,7 +294,10 @@ pub async fn worker(db: Database) {
..
}) => {
if let Err(err) = db
.remove_push_subscription_by_session_id(&task.session_id)
.remove_push_subscription_by_session_id(match task.job_type {
JobType::Alert(ref a) => &a.session_id.as_str(),
JobType::Badge(ref a) => &a.session_id.as_str(),
})
.await
{
revolt_config::capture_error(&err);
+1 -1
View File
@@ -18,7 +18,7 @@ pub async fn start_workers(db: Database, authifier_db: authifier::Database) {
task::spawn(apple_notifications::worker(db.clone()));
for _ in 0..WORKER_COUNT {
task::spawn(ack::worker(db.clone()));
task::spawn(ack::worker(db.clone(), authifier_db.clone()));
task::spawn(last_message_id::worker(db.clone()));
task::spawn(process_embeds::worker(db.clone()));
task::spawn(web_push::worker(authifier_db.clone()));
+7 -8
View File
@@ -6,7 +6,6 @@ use base64::{
Engine as _,
};
use deadqueue::limited::Queue;
use fcm::FcmError;
use once_cell::sync::Lazy;
use revolt_config::config;
use revolt_models::v0::PushNotification;
@@ -82,6 +81,7 @@ pub async fn worker(db: Database) {
tag,
timestamp: _,
url: _,
message: _,
} = &task.payload;
let mut notification = fcm::NotificationBuilder::new();
@@ -105,13 +105,12 @@ pub async fn worker(db: Database) {
info!("No FCM token was specified!");
}
} else if sub.endpoint == "apn" {
apple_notifications::queue(
apple_notifications::ApnTask::from_notification(
session.id,
sub.auth,
&task.payload,
),
)
apple_notifications::queue(apple_notifications::ApnJob::from_notification(
session.id,
session.user_id,
sub.auth,
&task.payload,
))
.await;
} else {
// Use Web Push Standard
+22 -13
View File
@@ -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,
}
}
}
@@ -531,6 +533,8 @@ impl From<crate::SystemMessage> for SystemMessage {
crate::SystemMessage::UserKicked { id } => Self::UserKicked { id },
crate::SystemMessage::UserLeft { id } => Self::UserLeft { id },
crate::SystemMessage::UserRemove { id, by } => Self::UserRemove { id, by },
crate::SystemMessage::MessagePinned { id, by } => Self::MessagePinned { id, by },
crate::SystemMessage::MessageUnpinned { id, by } => Self::MessageUnpinned { id, by },
}
}
}
@@ -1024,6 +1028,8 @@ impl crate::User {
}
/// Convert user object into user model assuming mutual connection
///
/// Relations will never be included, i.e. when we process ourselves
pub fn into_known<'a, P>(self, perspective: P, is_online: bool) -> User
where
P: Into<Option<&'a crate::User>>,
@@ -1059,19 +1065,7 @@ impl crate::User {
discriminator: self.discriminator,
display_name: self.display_name,
avatar: self.avatar.map(|file| file.into()),
relations: if let Some(crate::User { id, .. }) = perspective {
if id == &self.id {
self.relations
.unwrap_or_default()
.into_iter()
.map(|relation| relation.into())
.collect()
} else {
vec![]
}
} else {
vec![]
},
relations: vec![],
badges: self.badges.unwrap_or_default() as u32,
online: can_see_profile
&& is_online
@@ -1330,3 +1324,18 @@ impl From<BotInformation> for crate::BotInformation {
}
}
}
impl From<crate::FieldsMessage> for FieldsMessage {
fn from(value: crate::FieldsMessage) -> Self {
match value {
crate::FieldsMessage::Pinned => FieldsMessage::Pinned,
}
}
}
impl From<FieldsMessage> for crate::FieldsMessage {
fn from(value: FieldsMessage) -> Self {
match value {
FieldsMessage::Pinned => crate::FieldsMessage::Pinned,
}
}
}
@@ -18,6 +18,10 @@ static TOKEN_CACHE: Lazy<Mutex<lru::LruCache<String, ()>>> =
Lazy::new(|| Mutex::new(lru::LruCache::new(NonZeroUsize::new(1000).unwrap())));
impl IdempotencyKey {
pub fn unchecked_from_string(key: String) -> Self {
Self { key }
}
// Backwards compatibility.
// Issue #109
pub async fn consume_nonce(&mut self, v: Option<String>) -> Result<()> {
+3 -3
View File
@@ -1,6 +1,6 @@
[package]
name = "revolt-models"
version = "0.7.14"
version = "0.7.15"
edition = "2021"
license = "MIT"
authors = ["Paul Makles <me@insrt.uk>"]
@@ -19,8 +19,8 @@ default = ["serde", "partials", "rocket"]
[dependencies]
# Core
revolt-config = { version = "0.7.14", path = "../config" }
revolt-permissions = { version = "0.7.14", path = "../permissions" }
revolt-config = { version = "0.7.15", path = "../config" }
revolt-permissions = { version = "0.7.15", path = "../permissions" }
# Utility
regex = "1"
+5
View File
@@ -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)
}
+16 -10
View File
@@ -84,6 +84,12 @@ auto_derived!(
content_type: BandcampType,
id: String,
},
AppleMusic {
album_id: String,
#[serde(skip_serializing_if = "Option::is_none")]
track_id: Option<String>,
},
/// Streamable Video
Streamable { id: String },
}
@@ -92,38 +98,38 @@ auto_derived!(
pub struct WebsiteMetadata {
/// Direct URL to web page
#[serde(skip_serializing_if = "Option::is_none")]
url: Option<String>,
pub url: Option<String>,
/// Original direct URL
#[serde(skip_serializing_if = "Option::is_none")]
original_url: Option<String>,
pub original_url: Option<String>,
/// Remote content
#[serde(skip_serializing_if = "Option::is_none")]
special: Option<Special>,
pub special: Option<Special>,
/// Title of website
#[serde(skip_serializing_if = "Option::is_none")]
title: Option<String>,
pub title: Option<String>,
/// Description of website
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<String>,
pub description: Option<String>,
/// Embedded image
#[serde(skip_serializing_if = "Option::is_none")]
image: Option<Image>,
pub image: Option<Image>,
/// Embedded video
#[serde(skip_serializing_if = "Option::is_none")]
video: Option<Video>,
pub video: Option<Video>,
// #[serde(skip_serializing_if = "Option::is_none")]
// opengraph_type: Option<String>,
/// Site name
#[serde(skip_serializing_if = "Option::is_none")]
site_name: Option<String>,
pub site_name: Option<String>,
/// URL to site icon
#[serde(skip_serializing_if = "Option::is_none")]
icon_url: Option<String>,
pub icon_url: Option<String>,
/// CSS Colour
#[serde(skip_serializing_if = "Option::is_none")]
colour: Option<String>,
pub colour: Option<String>,
}
/// Text Embed
+39 -5
View File
@@ -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, by: String },
#[serde(rename = "message_unpinned")]
MessageUnpinned { id: String, by: String },
}
/// Name and / or avatar override information
@@ -200,6 +207,8 @@ auto_derived!(
pub timestamp: u64,
/// URL to open when clicking notification
pub url: String,
/// The message object itself, to send to clients for processing
pub message: Message,
}
/// Representation of a text embed before it is sent.
@@ -298,7 +307,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)))]
@@ -355,6 +366,11 @@ auto_derived!(
/// Message will not send push / desktop notifications
SuppressNotifications = 1,
}
/// Optional fields on message
pub enum FieldsMessage {
Pinned
}
);
/// Message Author Abstraction
@@ -418,6 +434,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(),
}
}
}
@@ -437,15 +455,30 @@ impl PushNotification {
format!("{}/assets/logo.png", config.hosts.app)
};
let image = msg.attachments.and_then(|attachments| {
let image = msg.attachments.as_ref().and_then(|attachments| {
attachments
.first()
.map(|v| format!("{}/attachments/{}", config.hosts.autumn, v.id))
});
let body = if let Some(sys) = msg.system {
sys.into()
} else if let Some(text) = msg.content {
let body = if let Some(ref sys) = msg.system {
sys.clone().into()
} else if let Some(ref text) = msg.content {
text.clone()
} else if let Some(text) = msg.embeds.as_ref().and_then(|embeds| match embeds.first() {
Some(Embed::Image(_)) => Some("Sent an image".to_string()),
Some(Embed::Video(_)) => Some("Sent a video".to_string()),
Some(Embed::Text(e)) => e
.description
.clone()
.or(e.title.clone().or(Some("Empty Embed".to_string()))),
Some(Embed::Website(e)) => e.title.clone().or(e
.description
.clone()
.or(e.site_name.clone().or(Some("Empty Embed".to_string())))),
Some(Embed::None) => Some("Empty Message".to_string()), // ???
None => Some("Empty Message".to_string()), // ??
}) {
text
} else {
"Empty Message".to_string()
@@ -466,6 +499,7 @@ impl PushNotification {
tag: channel_id.to_string(),
timestamp,
url: format!("{}/channel/{}/{}", config.hosts.app, channel_id, msg.id),
message: msg,
}
}
}
+2 -2
View File
@@ -1,6 +1,6 @@
[package]
name = "revolt-permissions"
version = "0.7.14"
version = "0.7.15"
edition = "2021"
license = "MIT"
authors = ["Paul Makles <me@insrt.uk>"]
@@ -21,7 +21,7 @@ async-std = { version = "1.8.0", features = ["attributes"] }
[dependencies]
# Core
revolt-result = { version = "0.7.14", path = "../result" }
revolt-result = { version = "0.7.15", path = "../result" }
# Utility
auto_ops = "0.3.0"
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "revolt-presence"
version = "0.7.14"
version = "0.7.15"
edition = "2021"
license = "AGPL-3.0-or-later"
authors = ["Paul Makles <me@insrt.uk>"]
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "revolt-result"
version = "0.7.14"
version = "0.7.15"
edition = "2021"
license = "MIT"
authors = ["Paul Makles <me@insrt.uk>"]
+2
View File
@@ -90,6 +90,8 @@ pub enum ErrorType {
},
AlreadyInGroup,
NotInGroup,
AlreadyPinned,
NotPinned,
// ? Server related errors
UnknownServer,
+2
View File
@@ -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,
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "revolt-delta"
version = "0.7.14"
version = "0.7.15"
license = "AGPL-3.0-or-later"
authors = ["Paul Makles <paulmakles@gmail.com>"]
edition = "2018"
+5 -3
View File
@@ -93,7 +93,9 @@ mod test {
.client
.post(format!("/bots/{}/invite", bot.id))
.header(ContentType::JSON)
.body(json!(v0::InviteBotDestination::Group { group: group.id() }).to_string())
.body(json!(v0::InviteBotDestination::Group {
group: group.id().to_string()
}).to_string())
.header(Header::new("x-session-token", session.token.to_string()))
.dispatch()
.await;
@@ -102,8 +104,8 @@ mod test {
drop(response);
let event = harness
.wait_for_event(&group.id(), |event| match event {
EventV1::ChannelGroupJoin { id, .. } => id == &group.id(),
.wait_for_event(group.id(), |event| match event {
EventV1::ChannelGroupJoin { id, .. } => id == group.id(),
_ => false,
})
.await;
@@ -69,7 +69,7 @@ mod test {
let event = harness
.wait_for_event(&format!("{}!", user.id), |event| match event {
EventV1::ChannelAck { id, .. } => id == &group.id(),
EventV1::ChannelAck { id, .. } => id == group.id(),
_ => false,
})
.await;
@@ -82,8 +82,8 @@ mod test {
drop(response);
harness
.wait_for_event(&group.id(), |event| match event {
EventV1::ChannelDelete { id, .. } => id == &group.id(),
.wait_for_event(group.id(), |event| match event {
EventV1::ChannelDelete { id, .. } => id == group.id(),
_ => false,
})
.await;
@@ -102,8 +102,8 @@ mod test {
.await;
let event = harness
.wait_for_event(&group.id(), |event| match event {
EventV1::ChannelGroupJoin { id, .. } => id == &group.id(),
.wait_for_event(group.id(), |event| match event {
EventV1::ChannelGroupJoin { id, .. } => id == group.id(),
_ => false,
})
.await;
@@ -113,7 +113,7 @@ mod test {
_ => unreachable!(),
};
let message = harness.wait_for_message(&group.id()).await;
let message = harness.wait_for_message(group.id()).await;
assert_eq!(
message.system,
@@ -106,8 +106,8 @@ mod test {
.await;
let event = harness
.wait_for_event(&group.id(), |event| match event {
EventV1::ChannelGroupJoin { id, .. } => id == &group.id(),
.wait_for_event(group.id(), |event| match event {
EventV1::ChannelGroupJoin { id, .. } => id == group.id(),
_ => false,
})
.await;
@@ -117,7 +117,7 @@ mod test {
_ => unreachable!(),
};
let message = harness.wait_for_message(&group.id()).await;
let message = harness.wait_for_message(group.id()).await;
assert_eq!(
message.system,
@@ -27,7 +27,7 @@ pub async fn clear_reactions(
.throw_if_lacking_channel_permission(ChannelPermission::ManageMessages)?;
// Fetch relevant message
let mut message = msg.as_message_in_channel(db, &channel.id()).await?;
let mut message = msg.as_message_in_channel(db, channel.id()).await?;
// Clear reactions
message
@@ -37,6 +37,7 @@ pub async fn clear_reactions(
reactions: Some(Default::default()),
..Default::default()
},
vec![]
)
.await
.map(|_| EmptyResponse)
@@ -43,7 +43,7 @@ pub async fn edit(
permissions.throw_if_lacking_channel_permission(ChannelPermission::SendMessage)?;
let mut message = msg.as_message_in_channel(db, &channel.id()).await?;
let mut message = msg.as_message_in_channel(db, channel.id()).await?;
if message.author != user.id {
return Err(create_error!(CannotEditMessage));
}
@@ -83,7 +83,7 @@ pub async fn edit(
partial.embeds = Some(new_embeds);
message.update(db, partial).await?;
message.update(db, partial, vec![]).await?;
// Queue up a task for processing embeds if the we have sufficient permissions
if permissions.has_channel_permission(ChannelPermission::SendEmbeds) {
@@ -0,0 +1,154 @@
use revolt_database::{
util::{permissions::DatabasePermissionQuery, reference::Reference}, Database, PartialMessage, SystemMessage, User
};
use revolt_models::v0::MessageAuthor;
use revolt_permissions::{calculate_channel_permissions, ChannelPermission};
use revolt_result::{create_error, Result};
use rocket::State;
use rocket_empty::EmptyResponse;
/// # Pins a message
///
/// Pins a message by its id.
#[openapi(tag = "Messaging")]
#[post("/<target>/messages/<msg>/pin")]
pub async fn message_pin(
db: &State<Database>,
user: User,
target: Reference,
msg: Reference,
) -> Result<EmptyResponse> {
let channel = target.as_channel(db).await?;
let mut query = DatabasePermissionQuery::new(db, &user).channel(&channel);
calculate_channel_permissions(&mut query)
.await
.throw_if_lacking_channel_permission(ChannelPermission::ManageMessages)?;
let mut message = msg.as_message_in_channel(db, channel.id()).await?;
if message.pinned.unwrap_or_default() {
return Err(create_error!(AlreadyPinned))
}
message.update(db, PartialMessage {
pinned: Some(true),
..Default::default()
}, vec![]).await?;
SystemMessage::MessagePinned {
id: message.id.clone(),
by: user.id.clone()
}
.into_message(channel.id().to_string())
.send(
db,
MessageAuthor::System {
username: &user.username,
avatar: user.avatar.as_ref().map(|file| file.id.as_ref())
},
None,
None,
&channel,
false
).await?;
Ok(EmptyResponse)
}
#[cfg(test)]
mod test {
use crate::{rocket, util::test::TestHarness};
use revolt_database::{events::client::EventV1, util::{idempotency::IdempotencyKey, reference::Reference}, Member, Message, Server};
use revolt_models::v0::{self, SystemMessage};
use rocket::http::{Header, Status};
#[rocket::async_test]
async fn pin_message() {
let mut harness = TestHarness::new().await;
let (_, session, user) = harness.new_user().await;
let (server, channels) = Server::create(
&harness.db,
v0::DataCreateServer {
name: "Test Server".to_string(),
..Default::default()
},
&user,
true
).await.expect("Failed to create test server");
let (member, channels) = Member::create(&harness.db, &server, &user, Some(channels)).await.expect("Failed to create member");
let channel = &channels[0];
let message = Message::create_from_api(
&harness.db,
channel.clone(),
v0::DataMessageSend {
content:Some("Test message".to_string()),
nonce: None,
attachments: None,
replies: None,
embeds: None,
masquerade: None,
interactions: None,
flags: None
},
v0::MessageAuthor::User(&user.clone().into(&harness.db, Some(&user)).await),
Some(user.clone().into(&harness.db, Some(&user)).await),
Some(member.into()),
user.limits().await,
IdempotencyKey::unchecked_from_string("0".to_string()),
false,
false
)
.await
.expect("Failed to create message");
let response = harness
.client
.post(format!("/channels/{}/messages/{}/pin", channel.id(), &message.id))
.header(Header::new("x-session-token", session.token.to_string()))
.dispatch()
.await;
assert_eq!(response.status(), Status::NoContent);
drop(response);
harness.wait_for_event(channel.id(), |event| {
match event {
EventV1::Message(message) => {
match &message.system {
Some(SystemMessage::MessagePinned { by, .. }) => {
assert_eq!(by, &user.id);
true
},
_ => false
}
},
_ => false
}
}).await;
harness.wait_for_event(channel.id(), |event| {
match event {
EventV1::MessageUpdate { id, channel: channel_id, data, .. } => {
assert_eq!(id, &message.id);
assert_eq!(channel_id, channel.id());
assert_eq!(data.pinned, Some(true));
true
},
_ => false
}
}).await;
let updated_message = Reference::from_unchecked(message.id)
.as_message(&harness.db)
.await
.expect("Failed to find updated message");
assert_eq!(updated_message.pinned, Some(true));
}
}
@@ -26,7 +26,7 @@ pub async fn react_message(
.throw_if_lacking_channel_permission(ChannelPermission::React)?;
// Fetch relevant message
let message = msg.as_message_in_channel(db, &channel.id()).await?;
let message = msg.as_message_in_channel(db, channel.id()).await?;
// Add the reaction
message
@@ -30,6 +30,10 @@ pub async fn search(
})
})?;
if options.query.is_some() && options.pinned.is_some() {
return Err(create_error!(InvalidOperation))
}
let channel = target.as_channel(db).await?;
let mut query = DatabasePermissionQuery::new(db, &user).channel(&channel);
@@ -39,6 +43,7 @@ pub async fn search(
let v0::DataMessageSearch {
query,
pinned,
limit,
before,
after,
@@ -51,7 +56,8 @@ pub async fn search(
MessageQuery {
filter: MessageFilter {
channel: Some(channel.id().to_string()),
query: Some(query),
query,
pinned,
..Default::default()
},
time_period: MessageTimePeriod::Absolute {
@@ -0,0 +1,161 @@
use revolt_database::{
util::{permissions::DatabasePermissionQuery, reference::Reference}, Database, FieldsMessage, PartialMessage, SystemMessage, User
};
use revolt_models::v0::MessageAuthor;
use revolt_permissions::{calculate_channel_permissions, ChannelPermission};
use revolt_result::{create_error, Result};
use rocket::State;
use rocket_empty::EmptyResponse;
/// # Unpins a message
///
/// Unpins a message by its id.
#[openapi(tag = "Messaging")]
#[delete("/<target>/messages/<msg>/pin")]
pub async fn message_unpin(
db: &State<Database>,
user: User,
target: Reference,
msg: Reference,
) -> Result<EmptyResponse> {
let channel = target.as_channel(db).await?;
let mut query = DatabasePermissionQuery::new(db, &user).channel(&channel);
calculate_channel_permissions(&mut query)
.await
.throw_if_lacking_channel_permission(ChannelPermission::ManageMessages)?;
let mut message = msg.as_message_in_channel(db, channel.id()).await?;
if !message.pinned.unwrap_or_default() {
return Err(create_error!(NotPinned))
}
message.update(db, PartialMessage::default(), vec![FieldsMessage::Pinned]).await?;
SystemMessage::MessageUnpinned {
id: message.id.clone(),
by: user.id.clone()
}
.into_message(channel.id().to_string())
.send(
db,
MessageAuthor::System {
username: &user.username,
avatar: user.avatar.as_ref().map(|file| file.id.as_ref())
},
None,
None,
&channel,
false
).await?;
Ok(EmptyResponse)
}
#[cfg(test)]
mod test {
use crate::{rocket, util::test::TestHarness};
use revolt_database::{events::client::EventV1, util::{idempotency::IdempotencyKey, reference::Reference}, Member, Message, PartialMessage, Server};
use revolt_models::v0::{self, FieldsMessage, SystemMessage};
use rocket::http::{Header, Status};
#[rocket::async_test]
async fn unpin_message() {
let mut harness = TestHarness::new().await;
let (_, session, user) = harness.new_user().await;
let (server, channels) = Server::create(
&harness.db,
v0::DataCreateServer {
name: "Test Server".to_string(),
..Default::default()
},
&user,
true
).await.expect("Failed to create test server");
let channel = &channels[0];
Member::create(&harness.db, &server, &user, Some(channels.clone())).await.expect("Failed to create member");
let member = Reference::from_unchecked(user.id.clone()).as_member(&harness.db, &server.id).await.expect("Failed to get member");
let message = Message::create_from_api(
&harness.db,
channel.clone(),
v0::DataMessageSend {
content:Some("Test message".to_string()),
nonce: None,
attachments: None,
replies: None,
embeds: None,
masquerade: None,
interactions: None,
flags: None
},
v0::MessageAuthor::User(&user.clone().into(&harness.db, Some(&user)).await),
Some(user.clone().into(&harness.db, Some(&user)).await),
Some(member.into()),
user.limits().await,
IdempotencyKey::unchecked_from_string("0".to_string()),
false,
false
)
.await
.expect("Failed to create message");
harness.db.update_message(
&message.id,
&PartialMessage {
pinned: Some(true),
..Default::default()
},
vec![]
).await.expect("Failed to update message");
let response = harness
.client
.delete(format!("/channels/{}/messages/{}/pin", channel.id(), &message.id))
.header(Header::new("x-session-token", session.token.to_string()))
.dispatch()
.await;
assert_eq!(response.status(), Status::NoContent);
drop(response);
harness.wait_for_event(channel.id(), |event| {
match event {
EventV1::Message(message) => {
match &message.system {
Some(SystemMessage::MessageUnpinned { by, .. }) => {
assert_eq!(by, &user.id);
true
},
_ => false
}
},
_ => false
}
}).await;
harness.wait_for_event(channel.id(), |event| {
match event {
EventV1::MessageUpdate { id, clear, .. } => {
assert_eq!(&message.id, id);
assert_eq!(clear, &[FieldsMessage::Pinned]);
true
},
_ => false
}
}).await;
let updated_message = Reference::from_unchecked(message.id)
.as_message(&harness.db)
.await
.expect("Failed to find updated message");
assert_eq!(updated_message.pinned, None);
}
}
@@ -36,7 +36,7 @@ pub async fn unreact_message(
}
// Fetch relevant message
let message = msg.as_message_in_channel(db, &channel.id()).await?;
let message = msg.as_message_in_channel(db, channel.id()).await?;
// Check if we should wipe all of this reaction
if remove_all {
+4
View File
@@ -15,10 +15,12 @@ mod message_clear_reactions;
mod message_delete;
mod message_edit;
mod message_fetch;
mod message_pin;
mod message_query;
mod message_react;
mod message_search;
mod message_send;
mod message_unpin;
mod message_unreact;
mod permissions_set;
mod permissions_set_default;
@@ -37,10 +39,12 @@ pub fn routes() -> (Vec<Route>, OpenApi) {
message_send::message_send,
message_query::query,
message_search::search,
message_pin::message_pin,
message_fetch::fetch,
message_edit::edit,
message_bulk_delete::bulk_delete_messages,
message_delete::delete,
message_unpin::message_unpin,
group_create::create_group,
group_add_member::add_member,
group_remove_member::remove_member,
@@ -54,7 +54,7 @@ pub async fn create_webhook(
id: webhook_id,
name: data.name,
avatar,
channel_id: channel.id(),
channel_id: channel.id().to_string(),
permissions: *DEFAULT_WEBHOOK_PERMISSIONS,
token: Some(nanoid::nanoid!(64)),
};
@@ -25,7 +25,7 @@ pub async fn fetch_webhooks(
.throw_if_lacking_channel_permission(ChannelPermission::ViewChannel)?;
Ok(Json(
db.fetch_webhooks_for_channel(&channel.id())
db.fetch_webhooks_for_channel(channel.id())
.await?
.into_iter()
.map(|v| v.into())
@@ -23,7 +23,8 @@ pub async fn join(
match &invite {
Invite::Server { server, .. } => {
let server = db.fetch_server(server).await?;
let channels = Member::create(db, &server, &user, None).await?;
let (_, channels) = Member::create(db, &server, &user, None).await?;
Ok(Json(InviteJoinResponse::Server {
channels: channels.into_iter().map(|c| c.into()).collect(),
server: server.into(),
+11 -1
View File
@@ -1,5 +1,9 @@
use revolt_database::{util::reference::Reference, Database};
use revolt_database::{
util::{permissions::DatabasePermissionQuery, reference::Reference},
Database, User,
};
use revolt_models::v0;
use revolt_permissions::PermissionQuery;
use revolt_result::{create_error, Result};
use rocket::{serde::json::Json, State};
@@ -10,10 +14,16 @@ use rocket::{serde::json::Json, State};
#[get("/<target>/roles/<role_id>")]
pub async fn fetch(
db: &State<Database>,
user: User,
target: Reference,
role_id: String,
) -> Result<Json<v0::Role>> {
let mut server = target.as_server(db).await?;
let mut query = DatabasePermissionQuery::new(db, &user).server(&server);
if !query.are_we_a_member().await {
return Err(create_error!(NotFound));
}
let role = server.roles.remove(&role_id);
if let Some(role) = role {
@@ -30,7 +30,7 @@ pub async fn create_server(
user.can_acquire_server(db).await?;
let (server, channels) = Server::create(db, data, &user, true).await?;
let channels = Member::create(db, &server, &user, Some(channels)).await?;
let (_, channels) = Member::create(db, &server, &user, Some(channels)).await?;
Ok(Json(v0::CreateServerLegacyResponse {
server: server.into(),
+3 -3
View File
@@ -1,6 +1,6 @@
let
# Pinned nixpkgs, deterministic. Last updated: 11-08-2023.
pkgs = import (fetchTarball("https://github.com/NixOS/nixpkgs/archive/bb9707ef2ea4a5b749b362d5cf81ada3ded2c53f.tar.gz")) {};
# Pinned nixpkgs, deterministic. Last updated: 28-07-2024.
pkgs = import (fetchTarball("https://github.com/NixOS/nixpkgs/archive/9b34ca580417e1ebc56c4df57d8b387dad686665.tar.gz")) {};
# Rolling updates, not deterministic.
# pkgs = import (fetchTarball("channel:nixpkgs-unstable")) {};
@@ -29,7 +29,7 @@ in pkgs.mkShell {
pkgs.rustc
pkgs.clippy
pkgs.rustfmt
pkgs.pkgconfig
pkgs.pkg-config
pkgs.openssl.dev
# mdbook