chore(monorepo): delta, january, quark

This commit is contained in:
Paul Makles
2022-06-02 00:04:22 +01:00
parent 5d8432e267
commit 2ce610e1e7
232 changed files with 18094 additions and 554 deletions

View File

@@ -0,0 +1,63 @@
use std::env;
use std::ops::Deref;
use crate::r#impl::dummy::DummyDb;
use crate::r#impl::mongo::MongoDb;
use crate::AbstractDatabase;
/// Database information to use to create a client
pub enum DatabaseInfo {
/// Auto-detect the database in use
Auto,
/// Use the mock database
Dummy,
/// Connect to MongoDB
MongoDb(String),
/// Use existing MongoDB connection
MongoDbFromClient(mongodb::Client),
}
/// Database
#[derive(Debug, Clone)]
pub enum Database {
/// Mock database
Dummy(DummyDb),
/// MongoDB database
MongoDb(MongoDb),
}
impl DatabaseInfo {
/// Create a database client from the given database information
#[async_recursion]
pub async fn connect(self) -> Result<Database, String> {
Ok(match self {
DatabaseInfo::Auto => {
if let Ok(uri) = env::var("MONGODB") {
return DatabaseInfo::MongoDb(uri).connect().await;
}
DatabaseInfo::Dummy.connect().await?
}
DatabaseInfo::Dummy => Database::Dummy(DummyDb),
DatabaseInfo::MongoDb(uri) => {
let client = mongodb::Client::with_uri_str(uri)
.await
.map_err(|_| "Failed to init db connection.".to_string())?;
Database::MongoDb(MongoDb(client))
}
DatabaseInfo::MongoDbFromClient(client) => Database::MongoDb(MongoDb(client)),
})
}
}
impl Deref for Database {
type Target = dyn AbstractDatabase;
fn deref(&self) -> &Self::Target {
match self {
Database::Dummy(dummy) => dummy,
Database::MongoDb(mongo) => mongo,
}
}
}

View File

@@ -0,0 +1,173 @@
use serde::{Deserialize, Serialize};
use crate::models::channel::{FieldsChannel, PartialChannel};
use crate::models::message::{AppendMessage, PartialMessage};
use crate::models::server::{FieldsRole, FieldsServer, PartialRole, PartialServer};
use crate::models::server_member::{FieldsMember, MemberCompositeKey, PartialMember};
use crate::models::user::{FieldsUser, PartialUser, RelationshipStatus};
use crate::models::{Channel, Member, Message, Server, User, UserSettings};
use crate::Error;
/// WebSocket Client Errors
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "error")]
pub enum WebSocketError {
LabelMe,
InternalError { at: String },
InvalidSession,
OnboardingNotFinished,
AlreadyAuthenticated,
MalformedData { msg: String },
}
/// Ping Packet
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum Ping {
Binary(Vec<u8>),
Number(usize),
}
/// Untagged Error
#[derive(Serialize)]
#[serde(untagged)]
pub enum ErrorEvent {
Error(WebSocketError),
APIError(Error),
}
/// Protocol Events
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "type")]
pub enum EventV1 {
/// Multiple events
Bulk { v: Vec<EventV1> },
/// Successfully authenticated
Authenticated,
/// Basic data to cache
Ready {
users: Vec<User>,
servers: Vec<Server>,
channels: Vec<Channel>,
members: Vec<Member>,
},
/// Ping response
Pong { data: Ping },
/// New message
Message(Message),
/// Update existing message
MessageUpdate {
id: String,
channel: String,
data: PartialMessage,
},
/// Append information to existing message
MessageAppend {
id: String,
channel: String,
append: AppendMessage,
},
/// Delete message
MessageDelete { id: String, channel: String },
/// Bulk delete messages
BulkMessageDelete { channel: String, ids: Vec<String> },
/// New channel
ChannelCreate(Channel),
/// Update existing channel
ChannelUpdate {
id: String,
data: PartialChannel,
clear: Vec<FieldsChannel>,
},
/// Delete channel
ChannelDelete { id: String },
/// User joins a group
ChannelGroupJoin { id: String, user: String },
/// User leaves a group
ChannelGroupLeave { id: String, user: String },
/// User started typing in a channel
ChannelStartTyping { id: String, user: String },
/// User stopped typing in a channel
ChannelStopTyping { id: String, user: String },
/// User acknowledged message in channel
ChannelAck {
id: String,
user: String,
message_id: String,
},
/// New server
ServerCreate {
id: String,
server: Server,
channels: Vec<Channel>,
},
/// Update existing server
ServerUpdate {
id: String,
data: PartialServer,
clear: Vec<FieldsServer>,
},
/// Delete server
ServerDelete { id: String },
/// Update existing server member
ServerMemberUpdate {
id: MemberCompositeKey,
data: PartialMember,
clear: Vec<FieldsMember>,
},
/// User joins server
ServerMemberJoin { id: String, user: String },
/// User left server
ServerMemberLeave { id: String, user: String },
/// Server role created or updated
ServerRoleUpdate {
id: String,
role_id: String,
data: PartialRole,
clear: Vec<FieldsRole>,
},
/// Server role deleted
ServerRoleDelete { id: String, role_id: String },
/// Update existing user
UserUpdate {
id: String,
data: PartialUser,
clear: Vec<FieldsUser>,
},
/// Relationship with another user changed
UserRelationship {
id: String,
user: User,
// ! this field can be deprecated
status: RelationshipStatus,
},
/// Settings updated remotely
UserSettingsUpdate { id: String, update: UserSettings },
}

View File

@@ -0,0 +1,557 @@
use std::collections::HashSet;
use crate::{
get_relationship,
models::{
server_member::FieldsMember,
user::{PartialUser, Presence, RelationshipStatus},
Channel, Member, User,
},
perms,
presence::presence_filter_online,
Database, Permission, Result,
};
use super::{
client::EventV1,
state::{Cache, State},
};
/// Cache Manager
impl Cache {
/// Check whether the current user can view a channel
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 server = self.servers.get(server);
let mut perms = perms(self.users.get(&self.user_id).unwrap()).channel(channel);
if let Some(member) = member {
perms.member.set_ref(member);
}
if let Some(server) = server {
perms.server.set_ref(server);
}
perms
.has_permission(db, Permission::ViewChannel)
.await
.unwrap_or_default()
}
_ => true,
}
}
/// Filter a given vector of channels to only include the ones we can access
pub async fn filter_accessible_channels(
&self,
db: &Database,
channels: Vec<Channel>,
) -> Vec<Channel> {
let mut viewable_channels = vec![];
for channel in channels {
if self.can_view_channel(db, &channel).await {
viewable_channels.push(channel);
}
}
viewable_channels
}
/// Check whether we can subscribe to another user
pub fn can_subscribe_to_user(&self, user_id: &str) -> bool {
if let Some(user) = self.users.get(&self.user_id) {
match get_relationship(user, user_id) {
RelationshipStatus::Friend
| RelationshipStatus::Incoming
| RelationshipStatus::Outgoing
| RelationshipStatus::User => true,
_ => {
let user_id = &user_id.to_string();
for channel in self.channels.values() {
match channel {
Channel::DirectMessage { recipients, .. }
| Channel::Group { recipients, .. } => {
if recipients.contains(user_id) {
return true;
}
}
_ => {}
}
}
false
}
}
} else {
false
}
}
}
/// State Manager
impl State {
/// Generate a Ready packet for the current user
pub async fn generate_ready_payload(&mut self, db: &Database) -> Result<EventV1> {
let mut user = self.clone_user();
// Find all relationships to the user.
let mut user_ids: Vec<String> = user
.relations
.as_ref()
.map(|arr| arr.iter().map(|x| x.id.to_string()).collect())
.unwrap_or_default();
// Fetch all memberships with their corresponding servers.
let members: Vec<Member> = db.fetch_all_memberships(&user.id).await?;
let server_ids: Vec<String> = members.iter().map(|x| x.id.server.clone()).collect();
let servers = db.fetch_servers(&server_ids).await?;
// Collect channel ids from servers.
let mut channel_ids = vec![];
for server in &servers {
channel_ids.append(&mut server.channels.clone());
}
// Fetch DMs and server channels.
let mut channels = db.find_direct_messages(&user.id).await?;
channels.append(&mut db.fetch_channels(&channel_ids).await?);
// Filter server channels by permission.
let channels = self.cache.filter_accessible_channels(db, channels).await;
// Append known user IDs from DMs.
for channel in &channels {
match channel {
Channel::DirectMessage { recipients, .. } | Channel::Group { recipients, .. } => {
user_ids.append(&mut recipients.clone());
}
_ => {}
}
}
// Fetch presence data for known users.
let online_ids = presence_filter_online(&user_ids).await;
user.online = Some(true);
// Fetch user data.
let users = db
.fetch_users(
&user_ids
.into_iter()
.filter(|x| x != &user.id)
.collect::<Vec<String>>(),
)
.await?;
// Copy data into local state cache.
self.cache.users = users.iter().cloned().map(|x| (x.id.clone(), x)).collect();
self.cache
.users
.insert(self.cache.user_id.clone(), user.clone());
self.cache.servers = servers.iter().cloned().map(|x| (x.id.clone(), x)).collect();
self.cache.channels = channels
.iter()
.cloned()
.map(|x| (x.id().to_string(), x))
.collect();
self.cache.members = members
.iter()
.cloned()
.map(|x| (x.id.server.clone(), x))
.collect();
// Make all users appear from our perspective.
let mut users: Vec<User> = users
.into_iter()
.map(|mut x| {
x.online = Some(online_ids.contains(&x.id));
x.with_relationship(&user)
})
.collect();
// Make sure we see our own user correctly.
user.relationship = Some(RelationshipStatus::User);
users.push(user.foreign());
// Set subscription state internally.
self.reset_state();
self.insert_subscription(self.private_topic.clone());
for user in &users {
self.insert_subscription(user.id.clone());
}
for server in &servers {
self.insert_subscription(server.id.clone());
}
for channel in &channels {
self.insert_subscription(channel.id().to_string());
}
Ok(EventV1::Ready {
users,
servers,
channels,
members,
})
}
/// Re-determine the currently accessible server channels
pub async fn recalculate_server(&mut self, db: &Database, id: &str, event: &mut EventV1) {
if let Some(server) = self.cache.servers.get(id) {
let mut channel_ids = HashSet::new();
let mut added_channels = vec![];
let mut removed_channels = vec![];
let id = &id.to_string();
for (channel_id, channel) in &self.cache.channels {
match channel {
Channel::TextChannel { server, .. } | Channel::VoiceChannel { server, .. } => {
if server == id {
channel_ids.insert(channel_id.clone());
if self.cache.can_view_channel(db, channel).await {
added_channels.push(channel_id.clone());
} else {
removed_channels.push(channel_id.clone());
}
}
}
_ => {}
}
}
let known_ids = server.channels.iter().cloned().collect::<HashSet<String>>();
let mut bulk_events = vec![];
for id in added_channels {
self.insert_subscription(id);
}
for id in removed_channels {
self.remove_subscription(&id);
self.cache.channels.remove(&id);
bulk_events.push(EventV1::ChannelDelete { id });
}
// * NOTE: currently all channels should be cached
// * provided that a server was loaded from payload
let unknowns = known_ids
.difference(&channel_ids)
.cloned()
.collect::<Vec<String>>();
if !unknowns.is_empty() {
if let Ok(channels) = db.fetch_channels(&unknowns).await {
let viewable_channels =
self.cache.filter_accessible_channels(db, channels).await;
for channel in viewable_channels {
self.cache
.channels
.insert(channel.id().to_string(), channel.clone());
self.insert_subscription(channel.id().to_string());
bulk_events.push(EventV1::ChannelCreate(channel));
}
}
}
if !bulk_events.is_empty() {
let mut new_event = EventV1::Bulk { v: bulk_events };
std::mem::swap(&mut new_event, event);
if let EventV1::Bulk { v } = event {
v.push(new_event);
}
}
}
}
/// Push presence change to the user and all associated server topics
pub async fn broadcast_presence_change(&self, target: bool) {
if if let Some(status) = &self.cache.users.get(&self.cache.user_id).unwrap().status {
status.presence != Some(Presence::Invisible)
} else {
true
} {
let event = EventV1::UserUpdate {
id: self.cache.user_id.clone(),
data: PartialUser {
online: Some(target),
..Default::default()
},
clear: vec![],
};
for server in self.cache.servers.keys() {
event.clone().p(server.clone()).await;
}
event.p(self.cache.user_id.clone()).await;
}
}
/// Handle an incoming event for protocol version 1
pub async fn handle_incoming_event_v1(&mut self, db: &Database, event: &mut EventV1) -> bool {
/* Superseded by private topics.
if match event {
EventV1::UserRelationship { id, .. }
| EventV1::UserSettingsUpdate { id, .. }
| EventV1::ChannelAck { id, .. } => id != &self.cache.user_id,
EventV1::ServerCreate { server, .. } => server.owner != self.cache.user_id,
EventV1::ChannelCreate(channel) => match channel {
Channel::SavedMessages { user, .. } => user != &self.cache.user_id,
Channel::DirectMessage { recipients, .. } | Channel::Group { recipients, .. } => {
!recipients.contains(&self.cache.user_id)
}
_ => false,
},
_ => false,
} {
return false;
}*/
// An event may trigger recalculation of an entire server's permission.
// Keep track of whether we need to do anything.
let mut queue_server = None;
// It may also need to sub or unsub a single value.
let mut queue_add = None;
let mut queue_remove = None;
match event {
EventV1::ChannelCreate(channel) => {
let id = channel.id().to_string();
self.insert_subscription(id.clone());
self.cache.channels.insert(id, channel.clone());
}
EventV1::ChannelUpdate {
id, data, clear, ..
} => {
let could_view: bool = if let Some(channel) = self.cache.channels.get(id) {
self.cache.can_view_channel(db, channel).await
} else {
true
};
if let Some(channel) = self.cache.channels.get_mut(id) {
for field in clear {
channel.remove(field);
}
channel.apply_options(data.clone());
}
if let Some(channel) = self.cache.channels.get(id) {
let can_view = self.cache.can_view_channel(db, channel).await;
if could_view != can_view {
if can_view {
queue_add = Some(id.clone());
*event = EventV1::ChannelCreate(channel.clone());
} else {
queue_remove = Some(id.clone());
*event = EventV1::ChannelDelete { id: id.clone() };
}
}
}
}
EventV1::ChannelDelete { id } => {
self.remove_subscription(id);
self.cache.channels.remove(id);
}
EventV1::ChannelGroupJoin { user, .. } => {
self.insert_subscription(user.clone());
}
EventV1::ChannelGroupLeave { id, user, .. } => {
if user == &self.cache.user_id {
self.remove_subscription(id);
} else if !self.cache.can_subscribe_to_user(user) {
self.remove_subscription(user);
}
}
EventV1::ServerCreate {
id,
server,
channels,
} => {
self.insert_subscription(id.clone());
self.cache.servers.insert(id.to_string(), server.clone());
for channel in channels {
self.cache
.channels
.insert(channel.id().to_string(), channel.clone());
}
queue_server = Some(id.clone());
}
EventV1::ServerUpdate {
id, data, clear, ..
} => {
if let Some(server) = self.cache.servers.get_mut(id) {
for field in clear {
server.remove(field);
}
server.apply_options(data.clone());
}
if data.default_permissions.is_some() {
queue_server = Some(id.clone());
}
}
EventV1::ServerMemberJoin { .. } => {
// We will always receive ServerCreate when joining a new server.
}
EventV1::ServerMemberLeave { id, user } => {
if user == &self.cache.user_id {
self.remove_subscription(id);
if let Some(server) = self.cache.servers.remove(id) {
for channel in &server.channels {
self.remove_subscription(channel);
self.cache.channels.remove(channel);
}
}
}
}
EventV1::ServerDelete { id } => {
self.remove_subscription(id);
if let Some(server) = self.cache.servers.remove(id) {
for channel in &server.channels {
self.remove_subscription(channel);
self.cache.channels.remove(channel);
}
}
}
EventV1::ServerMemberUpdate { id, data, clear } => {
if id.user == self.cache.user_id {
if let Some(member) = self.cache.members.get_mut(&id.server) {
for field in &clear.clone() {
member.remove(field);
}
member.apply_options(data.clone());
}
if data.roles.is_some() || clear.contains(&FieldsMember::Roles) {
queue_server = Some(id.server.clone());
}
}
}
EventV1::ServerRoleUpdate {
id,
role_id,
data,
clear,
..
} => {
if let Some(server) = self.cache.servers.get_mut(id) {
if let Some(role) = server.roles.get_mut(role_id) {
for field in &clear.clone() {
role.remove(field);
}
role.apply_options(data.clone());
}
}
if data.rank.is_some() || data.permissions.is_some() {
if let Some(member) = self.cache.members.get(id) {
if let Some(roles) = &member.roles {
if roles.contains(role_id) {
queue_server = Some(id.clone());
}
}
}
}
}
EventV1::ServerRoleDelete { id, role_id } => {
if let Some(server) = self.cache.servers.get_mut(id) {
server.roles.remove(role_id);
}
if let Some(member) = self.cache.members.get(id) {
if let Some(roles) = &member.roles {
if roles.contains(role_id) {
queue_server = Some(id.clone());
}
}
}
}
EventV1::UserRelationship { id, user, .. } => {
self.cache.users.insert(id.clone(), user.clone());
if self.cache.can_subscribe_to_user(id) {
self.insert_subscription(id.clone());
} else {
self.remove_subscription(id);
}
}
_ => {}
}
// Calculate server permissions if requested.
if let Some(server_id) = queue_server {
self.recalculate_server(db, &server_id, event).await;
}
// Sub / unsub accordingly.
if let Some(id) = queue_add {
self.insert_subscription(id);
}
if let Some(id) = queue_remove {
self.remove_subscription(&id);
}
true
}
}
impl EventV1 {
/// Publish helper wrapper
pub async fn p(self, channel: String) {
#[cfg(not(debug_assertions))]
redis_kiss::p(channel, self).await;
#[cfg(debug_assertions)]
info!("Publishing event to {channel}: {self:?}");
#[cfg(debug_assertions)]
redis_kiss::publish(channel, self).await.unwrap();
}
/// Publish user event
pub async fn p_user(self, id: String, db: &Database) {
self.clone().p(id.clone()).await;
// ! FIXME: this should be captured by member list in the future
// ! and not immediately fanned out to users
if let Ok(members) = db.fetch_all_memberships(&id).await {
for member in members {
self.clone().p(member.id.server).await;
}
}
}
/// Publish private event
pub async fn private(self, id: String) {
self.p(format!("{}!", id)).await;
}
}

View File

@@ -0,0 +1,4 @@
pub mod client;
pub mod r#impl;
pub mod server;
pub mod state;

View File

@@ -0,0 +1,12 @@
use serde::Deserialize;
use super::client::Ping;
#[derive(Deserialize, Debug)]
#[serde(tag = "type")]
pub enum ClientMessage {
Authenticate { token: String },
BeginTyping { channel: String },
EndTyping { channel: String },
Ping { data: Ping, responded: Option<()> },
}

View File

@@ -0,0 +1,146 @@
use std::collections::{HashMap, HashSet};
use crate::models::{Channel, Member, Server, User};
/// Enumeration representing some change in subscriptions
pub enum SubscriptionStateChange {
/// No change
None,
/// Clear all subscriptions
Reset,
/// Append or remove subscriptions
Change {
add: Vec<String>,
remove: Vec<String>,
},
}
/// Dumb per-state cache implementation
///
/// Ideally this would use a global cache that
/// allows for mutations and could use Rc<> to
/// track usage. If Rc<> == 1, then it only
/// remains in global cache, hence should be
/// dropped.
///
/// ------------------------------------------------
/// We can strip these objects to core information!!
/// ------------------------------------------------
#[derive(Debug, Default)]
pub struct Cache {
pub user_id: String,
pub users: HashMap<String, User>,
pub channels: HashMap<String, Channel>,
pub members: HashMap<String, Member>,
pub servers: HashMap<String, Server>,
}
/// Client state
pub struct State {
pub cache: Cache,
pub private_topic: String,
subscribed: HashSet<String>,
state: SubscriptionStateChange,
}
impl State {
/// Create state from User
pub fn from(user: User) -> State {
let mut subscribed = HashSet::new();
let private_topic = format!("{}!", user.id);
subscribed.insert(private_topic.clone());
subscribed.insert(user.id.clone());
let mut cache: Cache = Cache {
user_id: user.id.clone(),
..Default::default()
};
cache.users.insert(user.id.clone(), user);
State {
cache,
subscribed,
private_topic,
state: SubscriptionStateChange::Reset,
}
}
/// Apply currently queued state
pub fn apply_state(&mut self) -> SubscriptionStateChange {
let state = std::mem::replace(&mut self.state, SubscriptionStateChange::None);
if let SubscriptionStateChange::Change { add, remove } = &state {
for id in add {
self.subscribed.insert(id.clone());
}
for id in remove {
self.subscribed.remove(id);
}
}
state
}
/// Clone the active user
pub fn clone_user(&self) -> User {
self.cache.users.get(&self.cache.user_id).unwrap().clone()
}
/// Iterate through all subscriptions
pub fn iter_subscriptions(&self) -> std::collections::hash_set::Iter<'_, std::string::String> {
self.subscribed.iter()
}
/// Reset the current state
pub fn reset_state(&mut self) {
self.state = SubscriptionStateChange::Reset;
self.subscribed.clear();
}
/// Add a new subscription
pub fn insert_subscription(&mut self, subscription: String) {
if self.subscribed.contains(&subscription) {
return;
}
match &mut self.state {
SubscriptionStateChange::None => {
self.state = SubscriptionStateChange::Change {
add: vec![subscription.clone()],
remove: vec![],
};
}
SubscriptionStateChange::Change { add, .. } => {
add.push(subscription.clone());
}
SubscriptionStateChange::Reset => {}
}
self.subscribed.insert(subscription);
}
/// Remove existing subscription
pub fn remove_subscription(&mut self, subscription: &str) {
if !self.subscribed.contains(&subscription.to_string()) {
return;
}
match &mut self.state {
SubscriptionStateChange::None => {
self.state = SubscriptionStateChange::Change {
add: vec![],
remove: vec![subscription.to_string()],
};
}
SubscriptionStateChange::Change { remove, .. } => {
remove.push(subscription.to_string());
}
SubscriptionStateChange::Reset => panic!("Should not remove during a reset!"),
}
self.subscribed.remove(subscription);
}
}

View File

@@ -0,0 +1,11 @@
use crate::{AbstractMigrations, Result};
use super::super::DummyDb;
#[async_trait]
impl AbstractMigrations for DummyDb {
async fn migrate_database(&self) -> Result<()> {
info!("Migrating the database.");
Ok(())
}
}

View File

@@ -0,0 +1,42 @@
use crate::models::attachment::File;
use crate::{AbstractAttachment, Result};
use super::super::DummyDb;
#[async_trait]
impl AbstractAttachment for DummyDb {
async fn find_and_use_attachment(
&self,
attachment_id: &str,
tag: &str,
_parent_type: &str,
parent_id: &str,
) -> Result<File> {
Ok(File {
id: attachment_id.into(),
tag: tag.into(),
filename: "file.txt".into(),
content_type: "plain/text".into(),
size: 100,
object_id: Some(parent_id.into()),
..Default::default()
})
}
async fn insert_attachment(&self, attachment: &File) -> Result<()> {
info!("Insert {attachment:?}");
Ok(())
}
async fn mark_attachment_as_reported(&self, id: &str) -> Result<()> {
info!("Marked {id} as reported");
Ok(())
}
async fn mark_attachment_as_deleted(&self, id: &str) -> Result<()> {
info!("Marked {id} as deleted");
Ok(())
}
}

View File

@@ -0,0 +1,90 @@
use std::collections::HashSet;
use crate::models::channel::{Channel, FieldsChannel, PartialChannel};
use crate::{AbstractAttachment, AbstractChannel, Error, OverrideField, Result};
use super::super::DummyDb;
#[async_trait]
impl AbstractChannel for DummyDb {
async fn fetch_channel(&self, id: &str) -> Result<Channel> {
Ok(Channel::Group {
id: id.into(),
name: "group".into(),
owner: "owner".into(),
description: None,
recipients: vec!["owner".into()],
icon: Some(
self.find_and_use_attachment("dummy", "dummy", "dummy", "dummy")
.await?,
),
last_message_id: None,
permissions: None,
nsfw: false,
})
}
async fn fetch_channels<'a>(&self, _ids: &'a [String]) -> Result<Vec<Channel>> {
Ok(vec![self.fetch_channel("sus").await.unwrap()])
}
async fn insert_channel(&self, channel: &Channel) -> Result<()> {
info!("Insert {channel:?}");
Ok(())
}
async fn update_channel(
&self,
id: &str,
channel: &PartialChannel,
remove: Vec<FieldsChannel>,
) -> Result<()> {
info!("Update {id} with {channel:?} and remove {remove:?}");
Ok(())
}
async fn delete_channel(&self, channel: &Channel) -> Result<()> {
info!("Delete {channel:?}");
Ok(())
}
async fn find_direct_messages(&self, user_id: &str) -> Result<Vec<Channel>> {
Ok(vec![self.fetch_channel(user_id).await?])
}
async fn find_saved_messages_channel(&self, user: &str) -> Result<Channel> {
self.fetch_channel(user).await
}
async fn find_direct_message_channel(&self, _user_a: &str, _user_b: &str) -> Result<Channel> {
Err(Error::NotFound)
}
async fn add_user_to_group(&self, channel: &str, user: &str) -> Result<()> {
info!("Added {user} to {channel}");
Ok(())
}
async fn remove_user_from_group(&self, channel: &str, user: &str) -> Result<()> {
info!("Removed {user} from {channel}");
Ok(())
}
async fn set_channel_role_permission(
&self,
channel: &str,
role: &str,
permissions: OverrideField,
) -> Result<()> {
info!("Updating permissions for role {role} in {channel} with {permissions:?}");
Ok(())
}
async fn check_channels_exist(&self, _channels: &HashSet<String>) -> Result<bool> {
Ok(true)
}
}

View File

@@ -0,0 +1,30 @@
use crate::models::Invite;
use crate::{AbstractChannelInvite, Result};
use super::super::DummyDb;
#[async_trait]
impl AbstractChannelInvite for DummyDb {
async fn fetch_invite(&self, code: &str) -> Result<Invite> {
Ok(Invite::Server {
code: code.into(),
server: "server".into(),
creator: "creator".into(),
channel: "channel".into(),
})
}
async fn insert_invite(&self, invite: &Invite) -> Result<()> {
info!("Insert {invite:?}");
Ok(())
}
async fn delete_invite(&self, code: &str) -> Result<()> {
info!("Delete {code}");
Ok(())
}
async fn fetch_invites_for_server(&self, server: &str) -> Result<Vec<Invite>> {
Ok(vec![self.fetch_invite(server).await.unwrap()])
}
}

View File

@@ -0,0 +1,31 @@
use crate::models::channel_unread::ChannelUnread;
use crate::{AbstractChannelUnread, Result};
use super::super::DummyDb;
#[async_trait]
impl AbstractChannelUnread for DummyDb {
async fn acknowledge_message(&self, channel: &str, user: &str, message: &str) -> Result<()> {
info!("Acknowledged {message} in {channel} for {user}");
Ok(())
}
async fn acknowledge_channels(&self, user: &str, channels: &[String]) -> Result<()> {
info!("Acknowledged {channels:?} for {user}");
Ok(())
}
async fn add_mention_to_unread<'a>(
&self,
channel: &str,
user: &str,
ids: &[String],
) -> Result<()> {
info!("Added mentions for {user} in {channel}: {ids:?}");
Ok(())
}
async fn fetch_unreads(&self, _user: &str) -> Result<Vec<ChannelUnread>> {
Ok(vec![])
}
}

View File

@@ -0,0 +1,67 @@
use crate::models::message::{AppendMessage, Message, MessageSort, PartialMessage};
use crate::{AbstractMessage, Result};
use super::super::DummyDb;
#[async_trait]
impl AbstractMessage for DummyDb {
async fn fetch_message(&self, id: &str) -> Result<Message> {
Ok(Message {
id: id.into(),
channel: "channel".into(),
author: "author".into(),
content: Some("message content".into()),
..Default::default()
})
}
async fn insert_message(&self, message: &Message) -> Result<()> {
info!("Insert {message:?}");
Ok(())
}
async fn update_message(&self, id: &str, message: &PartialMessage) -> Result<()> {
info!("Update {id} with {message:?}");
Ok(())
}
async fn append_message(&self, id: &str, append: &AppendMessage) -> Result<()> {
info!("Append {id} with {append:?}");
Ok(())
}
async fn delete_message(&self, id: &str) -> Result<()> {
info!("Delete {id}");
Ok(())
}
async fn delete_messages(&self, channel: &str, ids: Vec<String>) -> Result<()> {
info!("Delete {ids:?} in {channel}");
Ok(())
}
async fn fetch_messages(
&self,
channel: &str,
_limit: Option<i64>,
_before: Option<String>,
_after: Option<String>,
_sort: Option<MessageSort>,
_nearby: Option<String>,
) -> Result<Vec<Message>> {
Ok(vec![self.fetch_message(channel).await.unwrap()])
}
async fn search_messages(
&self,
channel: &str,
_query: &str,
_limit: Option<i64>,
_before: Option<String>,
_after: Option<String>,
_sort: MessageSort,
) -> Result<Vec<Message>> {
Ok(vec![self.fetch_message(channel).await.unwrap()])
}
}

View File

@@ -0,0 +1,33 @@
use crate::AbstractDatabase;
pub mod admin {
pub mod migrations;
}
pub mod autumn {
pub mod attachment;
}
pub mod channels {
pub mod channel;
pub mod channel_invite;
pub mod channel_unread;
pub mod message;
}
pub mod servers {
pub mod server;
pub mod server_ban;
pub mod server_member;
}
pub mod users {
pub mod bot;
pub mod user;
pub mod user_settings;
}
#[derive(Debug, Clone)]
pub struct DummyDb;
impl AbstractDatabase for DummyDb {}

View File

@@ -0,0 +1,78 @@
use crate::models::server::{FieldsRole, FieldsServer, PartialRole, PartialServer, Role, Server};
use crate::{AbstractServer, Result, DEFAULT_PERMISSION_SERVER};
use super::super::DummyDb;
#[async_trait]
impl AbstractServer for DummyDb {
async fn fetch_server(&self, id: &str) -> Result<Server> {
Ok(Server {
id: id.into(),
owner: "owner".into(),
name: "server".into(),
description: Some("server description".into()),
channels: vec!["channel".into()],
categories: None,
system_messages: None,
roles: std::collections::HashMap::new(),
default_permissions: *DEFAULT_PERMISSION_SERVER as i64,
icon: None,
banner: None,
flags: None,
nsfw: false,
analytics: true,
discoverable: true,
})
}
async fn fetch_servers<'a>(&self, _ids: &'a [String]) -> Result<Vec<Server>> {
Ok(vec![self.fetch_server("sus").await.unwrap()])
}
async fn insert_server(&self, server: &Server) -> Result<()> {
info!("Insert {server:?}");
Ok(())
}
async fn update_server(
&self,
id: &str,
server: &PartialServer,
remove: Vec<FieldsServer>,
) -> Result<()> {
info!("Update {id} with {server:?} and remove {remove:?}");
Ok(())
}
async fn delete_server(&self, server: &Server) -> Result<()> {
info!("Delete {server:?}");
Ok(())
}
async fn insert_role(&self, server_id: &str, role_id: &str, role: &Role) -> Result<()> {
info!("Create {role:?} on {server_id} as {role_id}");
Ok(())
}
async fn update_role(
&self,
server_id: &str,
role_id: &str,
role: &PartialRole,
remove: Vec<FieldsRole>,
) -> Result<()> {
info!("Update {role_id} on {server_id} with {role:?} and remove {remove:?}");
Ok(())
}
async fn delete_role(&self, server_id: &str, role_id: &str) -> Result<()> {
info!("Delete {role_id} on {server_id}");
Ok(())
}
}

View File

@@ -0,0 +1,32 @@
use crate::models::server_member::MemberCompositeKey;
use crate::models::ServerBan;
use crate::{AbstractServerBan, Result};
use super::super::DummyDb;
#[async_trait]
impl AbstractServerBan for DummyDb {
async fn fetch_ban(&self, server: &str, user: &str) -> Result<ServerBan> {
Ok(ServerBan {
id: MemberCompositeKey {
server: server.into(),
user: user.into(),
},
reason: Some("ban reason".into()),
})
}
async fn fetch_bans(&self, server: &str) -> Result<Vec<ServerBan>> {
Ok(vec![self.fetch_ban(server, "user").await.unwrap()])
}
async fn insert_ban(&self, ban: &ServerBan) -> Result<()> {
info!("Insert {ban:?}");
Ok(())
}
async fn delete_ban(&self, id: &MemberCompositeKey) -> Result<()> {
info!("Delete {id:?}");
Ok(())
}
}

View File

@@ -0,0 +1,59 @@
use crate::models::server_member::{FieldsMember, Member, MemberCompositeKey, PartialMember};
use crate::{AbstractServerMember, Result};
use super::super::DummyDb;
#[async_trait]
impl AbstractServerMember for DummyDb {
async fn fetch_member(&self, server: &str, user: &str) -> Result<Member> {
Ok(Member {
id: MemberCompositeKey {
server: server.into(),
user: user.into(),
},
nickname: None,
avatar: None,
roles: None,
})
}
async fn insert_member(&self, member: &Member) -> Result<()> {
info!("Create {member:?}");
Ok(())
}
async fn update_member(
&self,
id: &MemberCompositeKey,
member: &PartialMember,
remove: Vec<FieldsMember>,
) -> Result<()> {
info!("Update {id:?} with {member:?} and remove {remove:?}");
Ok(())
}
async fn delete_member(&self, id: &MemberCompositeKey) -> Result<()> {
info!("Delete {id:?}");
Ok(())
}
async fn fetch_all_members<'a>(&self, server: &str) -> Result<Vec<Member>> {
Ok(vec![self.fetch_member(server, "member").await.unwrap()])
}
async fn fetch_all_memberships<'a>(&self, user: &str) -> Result<Vec<Member>> {
Ok(vec![self.fetch_member("server", user).await.unwrap()])
}
async fn fetch_members<'a>(&self, server: &str, _ids: &'a [String]) -> Result<Vec<Member>> {
Ok(vec![self.fetch_member(server, "member").await.unwrap()])
}
async fn fetch_member_count(&self, _server: &str) -> Result<usize> {
Ok(100)
}
async fn fetch_server_count(&self, _user: &str) -> Result<usize> {
Ok(5)
}
}

View File

@@ -0,0 +1,46 @@
use crate::models::bot::{Bot, FieldsBot, PartialBot};
use crate::{AbstractBot, Result};
use super::super::DummyDb;
#[async_trait]
impl AbstractBot for DummyDb {
async fn fetch_bot(&self, id: &str) -> Result<Bot> {
Ok(Bot {
id: id.into(),
owner: "user".into(),
token: "token".into(),
public: true,
analytics: true,
discoverable: true,
..Default::default()
})
}
async fn fetch_bot_by_token(&self, _token: &str) -> Result<Bot> {
self.fetch_bot("bot").await
}
async fn insert_bot(&self, bot: &Bot) -> Result<()> {
info!("Insert {bot:?}");
Ok(())
}
async fn update_bot(&self, id: &str, bot: &PartialBot, remove: Vec<FieldsBot>) -> Result<()> {
info!("Update {id} with {bot:?} and remove {remove:?}");
Ok(())
}
async fn delete_bot(&self, id: &str) -> Result<()> {
info!("Delete {id}");
Ok(())
}
async fn fetch_bots_by_user(&self, user_id: &str) -> Result<Vec<Bot>> {
Ok(vec![self.fetch_bot(user_id).await.unwrap()])
}
async fn get_number_of_bots_by_user(&self, _user_id: &str) -> Result<usize> {
Ok(1)
}
}

View File

@@ -0,0 +1,78 @@
use crate::models::user::{FieldsUser, PartialUser, RelationshipStatus, User};
use crate::{AbstractUser, Result};
use super::super::DummyDb;
#[async_trait]
impl AbstractUser for DummyDb {
async fn fetch_user(&self, id: &str) -> Result<User> {
Ok(User {
id: id.into(),
username: "username".into(),
..Default::default()
})
}
async fn fetch_user_by_username(&self, username: &str) -> Result<User> {
self.fetch_user(username).await
}
async fn fetch_user_by_token(&self, token: &str) -> Result<User> {
self.fetch_user(token).await
}
async fn insert_user(&self, user: &User) -> Result<()> {
info!("Insert {:?}", user);
Ok(())
}
async fn update_user(
&self,
id: &str,
user: &PartialUser,
remove: Vec<FieldsUser>,
) -> Result<()> {
info!("Update {id} with {user:?} and remove {remove:?}");
Ok(())
}
async fn delete_user(&self, id: &str) -> Result<()> {
info!("Delete {id}");
Ok(())
}
async fn fetch_users<'a>(&self, _id: &'a [String]) -> Result<Vec<User>> {
Ok(vec![self.fetch_user("id").await.unwrap()])
}
async fn is_username_taken(&self, _username: &str) -> Result<bool> {
Ok(false)
}
async fn fetch_mutual_user_ids(&self, _user_a: &str, _user_b: &str) -> Result<Vec<String>> {
Ok(vec!["a".into()])
}
async fn fetch_mutual_channel_ids(&self, _user_a: &str, _user_b: &str) -> Result<Vec<String>> {
Ok(vec!["b".into()])
}
async fn fetch_mutual_server_ids(&self, _user_a: &str, _user_b: &str) -> Result<Vec<String>> {
Ok(vec!["c".into()])
}
async fn set_relationship(
&self,
user_id: &str,
target_id: &str,
relationship: &RelationshipStatus,
) -> Result<()> {
info!("Set relationship from {user_id} to {target_id} as {relationship:?}");
Ok(())
}
async fn pull_relationship(&self, user_id: &str, target_id: &str) -> Result<()> {
info!("Removing relationship from {user_id} to {target_id}");
Ok(())
}
}

View File

@@ -0,0 +1,25 @@
use crate::models::UserSettings;
use crate::{AbstractUserSettings, Result};
use super::super::DummyDb;
#[async_trait]
impl AbstractUserSettings for DummyDb {
async fn fetch_user_settings(
&'_ self,
_id: &str,
_filter: &'_ [String],
) -> Result<UserSettings> {
Ok(std::collections::HashMap::new())
}
async fn set_user_settings(&self, id: &str, settings: &UserSettings) -> Result<()> {
info!("Set {id} to {settings:?}");
Ok(())
}
async fn delete_user_settings(&self, id: &str) -> Result<()> {
info!("Delete {id}");
Ok(())
}
}

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1,33 @@
use crate::{models::File, Database, Result};
impl File {
pub async fn use_attachment(db: &Database, id: &str, parent: &str) -> Result<File> {
db.find_and_use_attachment(id, "attachments", "message", parent)
.await
}
pub async fn use_background(db: &Database, id: &str, parent: &str) -> Result<File> {
db.find_and_use_attachment(id, "backgrounds", "user", parent)
.await
}
pub async fn use_avatar(db: &Database, id: &str, parent: &str) -> Result<File> {
db.find_and_use_attachment(id, "avatars", "user", parent)
.await
}
pub async fn use_icon(db: &Database, id: &str, parent: &str) -> Result<File> {
db.find_and_use_attachment(id, "icons", "object", parent)
.await
}
pub async fn use_server_icon(db: &Database, id: &str, parent: &str) -> Result<File> {
db.find_and_use_attachment(id, "icons", "object", parent)
.await
}
pub async fn use_banner(db: &Database, id: &str, parent: &str) -> Result<File> {
db.find_and_use_attachment(id, "banners", "server", parent)
.await
}
}

View File

@@ -0,0 +1,380 @@
use crate::{
events::client::EventV1,
models::{
channel::{FieldsChannel, PartialChannel},
message::SystemMessage,
Channel,
},
tasks::ack::AckEvent,
Database, Error, OverrideField, Result,
};
impl Channel {
/// Get a reference to this channel's id
pub fn id(&'_ self) -> &'_ str {
match self {
Channel::DirectMessage { id, .. }
| Channel::Group { id, .. }
| Channel::SavedMessages { id, .. }
| Channel::TextChannel { id, .. }
| Channel::VoiceChannel { id, .. } => id,
}
}
/// Represent channel as its id
pub fn as_id(self) -> String {
match self {
Channel::DirectMessage { id, .. }
| Channel::Group { id, .. }
| Channel::SavedMessages { id, .. }
| Channel::TextChannel { id, .. }
| Channel::VoiceChannel { id, .. } => id,
}
}
/// Map out whether it is a direct DM
pub fn is_direct_dm(&self) -> bool {
matches!(self, Channel::DirectMessage { .. })
}
/// Create a channel
pub async fn create(&self, db: &Database) -> Result<()> {
db.insert_channel(self).await?;
let event = EventV1::ChannelCreate(self.clone());
match self {
Self::SavedMessages { user, .. } => event.private(user.clone()).await,
Self::DirectMessage { recipients, .. } | Self::Group { recipients, .. } => {
for recipient in recipients {
event.clone().private(recipient.clone()).await;
}
}
Self::TextChannel { server, .. } | Self::VoiceChannel { server, .. } => {
event.p(server.clone()).await;
}
}
Ok(())
}
/// Update channel data
pub async fn update<'a>(
&mut self,
db: &Database,
partial: PartialChannel,
remove: Vec<FieldsChannel>,
) -> Result<()> {
for field in &remove {
self.remove(field);
}
self.apply_options(partial.clone());
let id = self.id().to_string();
db.update_channel(&id, &partial, remove.clone()).await?;
EventV1::ChannelUpdate {
id: id.clone(),
data: partial,
clear: remove,
}
.p(match self {
Self::TextChannel { server, .. } | Self::VoiceChannel { server, .. } => server.clone(),
_ => id,
})
.await;
Ok(())
}
/// Delete a channel
pub async fn delete(self, db: &Database) -> Result<()> {
let id = self.id().to_string();
EventV1::ChannelDelete { id: id.clone() }.p(id).await;
db.delete_channel(&self).await
}
/// Remove a field from Channel object
pub fn remove(&mut self, field: &FieldsChannel) {
match field {
FieldsChannel::Description => match self {
Self::Group { description, .. }
| Self::TextChannel { description, .. }
| Self::VoiceChannel { description, .. } => {
description.take();
}
_ => {}
},
FieldsChannel::Icon => match self {
Self::Group { icon, .. }
| Self::TextChannel { icon, .. }
| Self::VoiceChannel { icon, .. } => {
icon.take();
}
_ => {}
},
FieldsChannel::DefaultPermissions => match self {
Self::TextChannel {
default_permissions,
..
}
| Self::VoiceChannel {
default_permissions,
..
} => {
default_permissions.take();
}
_ => {}
},
}
}
/// Apply partial channel to channel
pub fn apply_options(&mut self, partial: PartialChannel) {
// ! FIXME: maybe flatten channel object?
match self {
Self::DirectMessage { active, .. } => {
if let Some(v) = partial.active {
*active = v;
}
}
Self::Group {
name,
owner,
description,
icon,
nsfw,
permissions,
..
} => {
if let Some(v) = partial.name {
*name = v;
}
if let Some(v) = partial.owner {
*owner = v;
}
if let Some(v) = partial.description {
description.replace(v);
}
if let Some(v) = partial.icon {
icon.replace(v);
}
if let Some(v) = partial.nsfw {
*nsfw = v;
}
if let Some(v) = partial.permissions {
permissions.replace(v);
}
}
Self::TextChannel {
name,
description,
icon,
nsfw,
default_permissions,
role_permissions,
..
}
| Self::VoiceChannel {
name,
description,
icon,
nsfw,
default_permissions,
role_permissions,
..
} => {
if let Some(v) = partial.name {
*name = v;
}
if let Some(v) = partial.description {
description.replace(v);
}
if let Some(v) = partial.icon {
icon.replace(v);
}
if let Some(v) = partial.nsfw {
*nsfw = v;
}
if let Some(v) = partial.role_permissions {
*role_permissions = v;
}
if let Some(v) = partial.default_permissions {
default_permissions.replace(v);
}
}
_ => {}
}
}
/// Acknowledge a message
pub async fn ack(&self, user: &str, message: &str) -> Result<()> {
EventV1::ChannelAck {
id: self.id().to_string(),
user: user.to_string(),
message_id: message.to_string(),
}
.private(user.to_string())
.await;
crate::tasks::ack::queue(
self.id().to_string(),
user.to_string(),
AckEvent::AckMessage {
id: message.to_string(),
},
)
.await;
Ok(())
}
/// Add user to a group
pub async fn add_user_to_group(&mut self, db: &Database, user: &str, by: &str) -> Result<()> {
if let Channel::Group { recipients, .. } = self {
recipients.push(user.to_string());
}
match &self {
Channel::Group { id, .. } => {
db.add_user_to_group(id, user).await?;
EventV1::ChannelGroupJoin {
id: id.to_string(),
user: user.to_string(),
}
.p(id.to_string())
.await;
EventV1::ChannelCreate(self.clone())
.private(user.to_string())
.await;
SystemMessage::UserAdded {
id: user.to_string(),
by: by.to_string(),
}
.into_message(id.to_string())
.create(db, self, None)
.await
.ok();
Ok(())
}
_ => Err(Error::InvalidOperation),
}
}
/// Remove user from a group
pub async fn remove_user_from_group(
&self,
db: &Database,
user: &str,
by: Option<&str>,
) -> Result<()> {
match &self {
Channel::Group {
id,
owner,
recipients,
..
} => {
if user == owner {
if let Some(new_owner) = recipients.iter().find(|x| *x != user) {
db.update_channel(
id,
&PartialChannel {
owner: Some(new_owner.into()),
..Default::default()
},
vec![],
)
.await?;
} else {
db.delete_channel(self).await?;
return Ok(());
}
}
db.remove_user_from_group(id, user).await?;
EventV1::ChannelGroupLeave {
id: id.to_string(),
user: user.to_string(),
}
.p(id.to_string())
.await;
if let Some(by) = by {
SystemMessage::UserRemove {
id: user.to_string(),
by: by.to_string(),
}
} else {
SystemMessage::UserLeft {
id: user.to_string(),
}
}
.into_message(id.to_string())
.create(db, self, None)
.await
.ok();
Ok(())
}
_ => Err(Error::InvalidOperation),
}
}
/// Set role permission on a channel
pub async fn set_role_permission(
&mut self,
db: &Database,
role: &str,
permissions: OverrideField,
) -> Result<()> {
match self {
Channel::TextChannel {
id,
server,
role_permissions,
..
}
| Channel::VoiceChannel {
id,
server,
role_permissions,
..
} => {
db.set_channel_role_permission(id, role, permissions)
.await?;
role_permissions.insert(role.to_string(), permissions);
EventV1::ChannelUpdate {
id: id.clone(),
data: PartialChannel {
role_permissions: Some(role_permissions.clone()),
..Default::default()
},
clear: vec![],
}
.p(server.clone())
.await;
Ok(())
}
_ => Err(Error::InvalidOperation),
}
}
}

View File

@@ -0,0 +1,74 @@
use nanoid::nanoid;
use crate::{
models::{Channel, Invite, User},
Database, Error, Result,
};
lazy_static! {
static ref ALPHABET: [char; 54] = [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'
];
}
impl Invite {
/// Get the invite code for this invite
pub fn code(&'_ self) -> &'_ str {
match self {
Invite::Server { code, .. } | Invite::Group { code, .. } => code,
}
}
/// Get the ID of the user who created this invite
pub fn creator(&'_ self) -> &'_ str {
match self {
Invite::Server { creator, .. } | Invite::Group { creator, .. } => creator,
}
}
/// Create a new invite from given information
pub async fn create(db: &Database, creator: &User, target: &Channel) -> Result<Invite> {
let code = nanoid!(8, &*ALPHABET);
let invite = match &target {
Channel::Group { id, .. } => Ok(Invite::Group {
code,
creator: creator.id.clone(),
channel: id.clone(),
}),
Channel::TextChannel { id, server, .. } | Channel::VoiceChannel { id, server, .. } => {
Ok(Invite::Server {
code,
creator: creator.id.clone(),
server: server.clone(),
channel: id.clone(),
})
}
_ => Err(Error::InvalidOperation),
}?;
db.insert_invite(&invite).await?;
Ok(invite)
}
/// Resolve an invite by its ID or by a public server ID
pub async fn find(db: &Database, code: &str) -> Result<Invite> {
if let Ok(invite) = db.fetch_invite(code).await {
return Ok(invite);
} else if let Ok(server) = db.fetch_server(code).await {
if server.discoverable {
if let Some(channel) = server.channels.into_iter().next() {
return Ok(Invite::Server {
code: code.to_string(),
server: server.id,
creator: server.owner,
channel,
});
}
}
}
Err(Error::NotFound)
}
}

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1,284 @@
use std::collections::HashSet;
use serde_json::json;
use ulid::Ulid;
use crate::{
events::client::EventV1,
models::{
message::{
AppendMessage, BulkMessageResponse, PartialMessage, SendableEmbed, SystemMessage,
},
Channel, Message, User,
},
presence::presence_filter_online,
tasks::ack::AckEvent,
types::{
january::{Embed, Text},
push::PushNotification,
},
Database, Result,
};
impl Message {
/// Create a message
pub async fn create_no_web_push(
&mut self,
db: &Database,
channel: &str,
is_direct_dm: bool,
) -> Result<()> {
db.insert_message(self).await?;
// Fan out events
EventV1::Message(self.clone()).p(channel.to_string()).await;
// Update last_message_id
crate::tasks::last_message_id::queue(
channel.to_string(),
self.id.to_string(),
is_direct_dm,
)
.await;
// Add mentions for affected users
if let Some(mentions) = &self.mentions {
for user in mentions {
crate::tasks::ack::queue(
channel.to_string(),
user.to_string(),
AckEvent::AddMention {
ids: vec![self.id.to_string()],
},
)
.await;
}
}
Ok(())
}
/// Create a message and Web Push events
pub async fn create(
&mut self,
db: &Database,
channel: &Channel,
sender: Option<&User>,
) -> Result<()> {
self.create_no_web_push(db, channel.id(), channel.is_direct_dm())
.await?;
// Push out Web Push notifications
crate::tasks::web_push::queue(
{
let mut target_ids = vec![];
match &channel {
Channel::DirectMessage { recipients, .. }
| Channel::Group { recipients, .. } => {
target_ids = (&recipients.iter().cloned().collect::<HashSet<String>>()
- &presence_filter_online(recipients).await)
.into_iter()
.collect::<Vec<String>>();
}
Channel::TextChannel { .. } => {
if let Some(mentions) = &self.mentions {
target_ids.append(&mut mentions.clone());
}
}
_ => {}
};
target_ids
},
json!(PushNotification::new(self.clone(), sender, channel.id())).to_string(),
)
.await;
Ok(())
}
/// Update message data
pub async fn update(&mut self, db: &Database, partial: PartialMessage) -> Result<()> {
self.apply_options(partial.clone());
db.update_message(&self.id, &partial).await?;
EventV1::MessageUpdate {
id: self.id.clone(),
channel: self.channel.clone(),
data: partial,
}
.p(self.channel.clone())
.await;
Ok(())
}
/// Append message data
pub async fn append(
db: &Database,
id: String,
channel: String,
append: AppendMessage,
) -> Result<()> {
db.append_message(&id, &append).await?;
EventV1::MessageAppend {
id,
channel: channel.to_string(),
append,
}
.p(channel)
.await;
Ok(())
}
/// Delete a message
pub async fn delete(self, db: &Database) -> Result<()> {
db.delete_message(&self.id).await?;
EventV1::MessageDelete {
id: self.id,
channel: self.channel.clone(),
}
.p(self.channel)
.await;
Ok(())
}
/// Bulk delete messages
pub async fn bulk_delete(db: &Database, channel: &str, ids: Vec<String>) -> Result<()> {
db.delete_messages(channel, ids.clone()).await?;
EventV1::BulkMessageDelete {
channel: channel.to_string(),
ids,
}
.p(channel.to_string())
.await;
Ok(())
}
}
pub trait IntoUsers {
fn get_user_ids(&self) -> Vec<String>;
}
impl IntoUsers for Message {
fn get_user_ids(&self) -> Vec<String> {
let mut ids = vec![self.author.clone()];
if let Some(msg) = &self.system {
match msg {
SystemMessage::UserAdded { id, by, .. }
| SystemMessage::UserRemove { id, by, .. } => {
ids.push(id.clone());
ids.push(by.clone());
}
SystemMessage::UserJoined { id, .. }
| SystemMessage::UserLeft { id, .. }
| SystemMessage::UserKicked { id, .. }
| SystemMessage::UserBanned { id, .. } => ids.push(id.clone()),
SystemMessage::ChannelRenamed { by, .. }
| SystemMessage::ChannelDescriptionChanged { by, .. }
| SystemMessage::ChannelIconChanged { by, .. } => ids.push(by.clone()),
_ => {}
}
}
ids
}
}
impl IntoUsers for Vec<Message> {
fn get_user_ids(&self) -> Vec<String> {
let mut ids = vec![];
for message in self {
ids.append(&mut message.get_user_ids());
}
ids
}
}
impl SystemMessage {
pub fn into_message(self, channel: String) -> Message {
Message {
id: Ulid::new().to_string(),
channel,
author: "00000000000000000000000000".to_string(),
system: Some(self),
..Default::default()
}
}
}
impl From<SystemMessage> for String {
fn from(s: SystemMessage) -> String {
match s {
SystemMessage::Text { content } => content,
SystemMessage::UserAdded { .. } => "User added to the channel.".to_string(),
SystemMessage::UserRemove { .. } => "User removed from the channel.".to_string(),
SystemMessage::UserJoined { .. } => "User joined the channel.".to_string(),
SystemMessage::UserLeft { .. } => "User left the channel.".to_string(),
SystemMessage::UserKicked { .. } => "User kicked from the channel.".to_string(),
SystemMessage::UserBanned { .. } => "User banned from the channel.".to_string(),
SystemMessage::ChannelRenamed { .. } => "Channel renamed.".to_string(),
SystemMessage::ChannelDescriptionChanged { .. } => {
"Channel description changed.".to_string()
}
SystemMessage::ChannelIconChanged { .. } => "Channel icon changed.".to_string(),
}
}
}
impl SendableEmbed {
pub async fn into_embed(self, db: &Database, message_id: String) -> Result<Embed> {
let media = if let Some(id) = self.media {
Some(
db.find_and_use_attachment(&id, "attachments", "message", &message_id)
.await?,
)
} else {
None
};
Ok(Embed::Text(Text {
icon_url: self.icon_url,
url: self.url,
title: self.title,
description: self.description,
media,
colour: self.colour,
}))
}
}
impl BulkMessageResponse {
pub async fn transform(
db: &Database,
channel: &Channel,
messages: Vec<Message>,
include_users: Option<bool>,
) -> Result<BulkMessageResponse> {
if let Some(true) = include_users {
let user_ids = messages.get_user_ids();
let users = db.fetch_users(&user_ids).await?;
Ok(match channel {
Channel::TextChannel { server, .. } | Channel::VoiceChannel { server, .. } => {
BulkMessageResponse::MessagesAndUsers {
messages,
users,
members: Some(db.fetch_members(server, &user_ids).await?),
}
}
_ => BulkMessageResponse::MessagesAndUsers {
messages,
users,
members: None,
},
})
} else {
Ok(BulkMessageResponse::JustMessages(messages))
}
}
}

View File

@@ -0,0 +1,28 @@
//! Database agnostic implementations.
pub mod admin {
pub mod migrations;
}
pub mod autumn {
pub mod attachment;
}
pub mod channels {
pub mod channel;
pub mod channel_invite;
pub mod channel_unread;
pub mod message;
}
pub mod servers {
pub mod server;
pub mod server_ban;
pub mod server_member;
}
pub mod users {
pub mod bot;
pub mod user;
pub mod user_settings;
}

View File

@@ -0,0 +1,323 @@
use ulid::Ulid;
use crate::{
events::client::EventV1,
models::{
message::SystemMessage,
server::{
FieldsRole, FieldsServer, PartialRole, PartialServer, Role, SystemMessageChannels,
},
server_member::{MemberCompositeKey, RemovalIntention},
Channel, Member, Server, ServerBan, User,
},
perms, Database, Error, OverrideField, Permission, Result,
};
impl Role {
/// Into optional struct
pub fn into_optional(self) -> PartialRole {
PartialRole {
name: Some(self.name),
permissions: Some(self.permissions),
colour: self.colour,
hoist: Some(self.hoist),
rank: Some(self.rank),
}
}
/// Create a role
pub async fn create(&self, db: &Database, server_id: &str) -> Result<String> {
let role_id = Ulid::new().to_string();
db.insert_role(server_id, &role_id, self).await?;
EventV1::ServerRoleUpdate {
id: server_id.to_string(),
role_id: role_id.to_string(),
data: self.clone().into_optional(),
clear: vec![],
}
.p(server_id.to_string())
.await;
Ok(role_id)
}
/// Update server data
pub async fn update<'a>(
&mut self,
db: &Database,
server_id: &str,
role_id: &str,
partial: PartialRole,
remove: Vec<FieldsRole>,
) -> Result<()> {
for field in &remove {
self.remove(field);
}
self.apply_options(partial.clone());
db.update_role(server_id, role_id, &partial, remove.clone())
.await?;
EventV1::ServerRoleUpdate {
id: server_id.to_string(),
role_id: role_id.to_string(),
data: partial,
clear: vec![],
}
.p(server_id.to_string())
.await;
Ok(())
}
/// Delete a role
pub async fn delete(self, db: &Database, server_id: &str, role_id: &str) -> Result<()> {
EventV1::ServerRoleDelete {
id: server_id.to_string(),
role_id: role_id.to_string(),
}
.p(server_id.to_string())
.await;
db.delete_role(server_id, role_id).await
}
/// Remove field from Role
pub fn remove(&mut self, field: &FieldsRole) {
match field {
FieldsRole::Colour => self.colour = None,
}
}
}
impl Server {
/// Create a server
pub async fn create(&self, db: &Database) -> Result<()> {
db.insert_server(self).await
}
/// Update server data
pub async fn update<'a>(
&mut self,
db: &Database,
partial: PartialServer,
remove: Vec<FieldsServer>,
) -> Result<()> {
for field in &remove {
self.remove(field);
}
self.apply_options(partial.clone());
db.update_server(&self.id, &partial, remove.clone()).await?;
EventV1::ServerUpdate {
id: self.id.clone(),
data: partial,
clear: remove,
}
.p(self.id.clone())
.await;
Ok(())
}
/// Delete a server
pub async fn delete(self, db: &Database) -> Result<()> {
EventV1::ServerDelete {
id: self.id.clone(),
}
.p(self.id.clone())
.await;
db.delete_server(&self).await
}
/// Remove a field from Server
pub fn remove(&mut self, field: &FieldsServer) {
match field {
FieldsServer::Description => self.description = None,
FieldsServer::Categories => self.categories = None,
FieldsServer::SystemMessages => self.system_messages = None,
FieldsServer::Icon => self.icon = None,
FieldsServer::Banner => self.banner = None,
}
}
/// Set role permission on a server
pub async fn set_role_permission(
&mut self,
db: &Database,
role_id: &str,
permissions: OverrideField,
) -> Result<()> {
if let Some(role) = self.roles.get_mut(role_id) {
role.update(
db,
&self.id,
role_id,
PartialRole {
permissions: Some(permissions),
..Default::default()
},
vec![],
)
.await?;
Ok(())
} else {
Err(Error::NotFound)
}
}
/// Create a new member in a server
pub async fn create_member(
&self,
db: &Database,
user: User,
channels: Option<Vec<Channel>>,
) -> Result<Vec<Channel>> {
if db.fetch_ban(&self.id, &user.id).await.is_ok() {
return Err(Error::Banned);
}
let member = Member {
id: MemberCompositeKey {
server: self.id.clone(),
user: user.id.clone(),
},
..Default::default()
};
db.insert_member(&member).await?;
let should_fetch = channels.is_none();
let mut channels = channels.unwrap_or_default();
if should_fetch {
let perm = perms(&user).server(self).member(&member);
let existing_channels = db.fetch_channels(&self.channels).await?;
for channel in existing_channels {
if perm
.clone()
.channel(&channel)
.has_permission(db, Permission::ViewChannel)
.await?
{
channels.push(channel);
}
}
}
EventV1::ServerMemberJoin {
id: self.id.clone(),
user: user.id.clone(),
}
.p(self.id.clone())
.await;
EventV1::ServerCreate {
id: self.id.clone(),
server: self.clone(),
channels: channels.clone(),
}
.private(user.id.clone())
.await;
if let Some(id) = self
.system_messages
.as_ref()
.and_then(|x| x.user_joined.as_ref())
{
SystemMessage::UserJoined {
id: user.id.clone(),
}
.into_message(id.to_string())
.create_no_web_push(db, id, false)
.await
.ok();
}
Ok(channels)
}
/// Remove a member from a server
pub async fn remove_member(
&self,
db: &Database,
member: Member,
intention: RemovalIntention,
) -> Result<()> {
db.delete_member(&member.id).await?;
EventV1::ServerMemberLeave {
id: self.id.to_string(),
user: member.id.user.clone(),
}
.p(member.id.server)
.await;
if let Some(id) = self.system_messages.as_ref().and_then(|x| match intention {
RemovalIntention::Leave => x.user_left.as_ref(),
RemovalIntention::Kick => x.user_kicked.as_ref(),
RemovalIntention::Ban => x.user_banned.as_ref(),
}) {
match intention {
RemovalIntention::Leave => SystemMessage::UserLeft { id: member.id.user },
RemovalIntention::Kick => SystemMessage::UserKicked { id: member.id.user },
RemovalIntention::Ban => SystemMessage::UserBanned { id: member.id.user },
}
.into_message(id.to_string())
.create_no_web_push(db, id, false)
.await
.ok();
}
Ok(())
}
/// Ban a member from a server
pub async fn ban_member(
self,
db: &Database,
member: Member,
reason: Option<String>,
) -> Result<ServerBan> {
let ban = ServerBan {
id: member.id.clone(),
reason,
};
self.remove_member(db, member, RemovalIntention::Ban)
.await?;
db.insert_ban(&ban).await?;
Ok(ban)
}
}
impl SystemMessageChannels {
pub fn into_channel_ids(self) -> Vec<String> {
let mut ids = vec![];
if let Some(id) = self.user_joined {
ids.push(id);
}
if let Some(id) = self.user_left {
ids.push(id);
}
if let Some(id) = self.user_kicked {
ids.push(id);
}
if let Some(id) = self.user_banned {
ids.push(id);
}
ids
}
}

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1,62 @@
use crate::{
events::client::EventV1,
models::{
server_member::{FieldsMember, PartialMember},
Member, Server,
},
Database, Result,
};
impl Member {
/// Update member data
pub async fn update<'a>(
&mut self,
db: &Database,
partial: PartialMember,
remove: Vec<FieldsMember>,
) -> Result<()> {
for field in &remove {
self.remove(field);
}
self.apply_options(partial.clone());
db.update_member(&self.id, &partial, remove.clone()).await?;
EventV1::ServerMemberUpdate {
id: self.id.clone(),
data: partial,
clear: remove,
}
.p(self.id.server.clone())
.await;
Ok(())
}
/// Get this user's current ranking
pub fn get_ranking(&self, server: &Server) -> i64 {
if let Some(roles) = &self.roles {
let mut value = i64::MAX;
for role in roles {
if let Some(role) = server.roles.get(role) {
if role.rank < value {
value = role.rank;
}
}
}
value
} else {
i64::MAX
}
}
pub fn remove(&mut self, field: &FieldsMember) {
match field {
FieldsMember::Avatar => self.avatar = None,
FieldsMember::Nickname => self.nickname = None,
FieldsMember::Roles => self.roles = None,
}
}
}

View File

@@ -0,0 +1,24 @@
use nanoid::nanoid;
use crate::{
models::{bot::FieldsBot, Bot},
Database, Result,
};
impl Bot {
/// Remove a field from this object
pub fn remove(&mut self, field: &FieldsBot) {
match field {
FieldsBot::Token => self.token = nanoid!(64),
FieldsBot::InteractionsURL => {
self.interactions_url.take();
}
}
}
/// Delete this bot
pub async fn delete(&self, db: &Database) -> Result<()> {
db.fetch_user(&self.id).await?.mark_deleted(db).await?;
db.delete_bot(&self.id).await
}
}

View File

@@ -0,0 +1,430 @@
use crate::events::client::EventV1;
use crate::models::user::{
Badges, FieldsUser, PartialUser, Presence, RelationshipStatus, User, UserHint,
};
use crate::permissions::defn::UserPerms;
use crate::permissions::r#impl::user::get_relationship;
use crate::{perms, Database, Error, Result};
use futures::try_join;
use impl_ops::impl_op_ex_commutative;
use okapi::openapi3::{SecurityScheme, SecuritySchemeData};
use rocket_okapi::gen::OpenApiGenerator;
use rocket_okapi::request::{OpenApiFromRequest, RequestHeaderInput};
use std::ops;
impl_op_ex_commutative!(+ |a: &i32, b: &Badges| -> i32 { *a | *b as i32 });
impl User {
/// Update user data
pub async fn update<'a>(
&mut self,
db: &Database,
partial: PartialUser,
remove: Vec<FieldsUser>,
) -> Result<()> {
for field in &remove {
self.remove(field);
}
self.apply_options(partial.clone());
db.update_user(&self.id, &partial, remove.clone()).await?;
EventV1::UserUpdate {
id: self.id.clone(),
data: partial,
clear: remove,
}
.p_user(self.id.clone(), db)
.await;
Ok(())
}
/// Remove a field from User object
pub fn remove(&mut self, field: &FieldsUser) {
match field {
FieldsUser::Avatar => self.avatar = None,
FieldsUser::StatusText => {
if let Some(x) = self.status.as_mut() {
x.text = None;
}
}
FieldsUser::StatusPresence => {
if let Some(x) = self.status.as_mut() {
x.presence = None;
}
}
FieldsUser::ProfileContent => {
if let Some(x) = self.profile.as_mut() {
x.content = None;
}
}
FieldsUser::ProfileBackground => {
if let Some(x) = self.profile.as_mut() {
x.background = None;
}
}
}
}
/// Mutate the user object to remove redundant information
pub fn foreign(mut self) -> User {
self.profile = None;
self.relations = None;
let mut badges = self.badges.unwrap_or(0);
if let Ok(id) = ulid::Ulid::from_string(&self.id) {
// Yes, this is hard-coded
// No, I don't care + ratio
if id.datetime().timestamp_millis() < 1629638578431 {
badges = badges + Badges::EarlyAdopter;
}
}
self.badges = Some(badges);
if let Some(status) = &self.status {
if let Some(presence) = &status.presence {
if presence == &Presence::Invisible {
self.status = None;
self.online = Some(false);
}
}
}
self
}
/// Mutate the user object to include relationship (if it does not already exist)
#[must_use]
pub fn with_relationship(self, perspective: &User) -> User {
let mut user = self.foreign();
if user.relationship.is_none() {
user.relationship = Some(get_relationship(perspective, &user.id));
}
user
}
/// Mutate user object with given permission
#[must_use]
pub fn apply_permission(mut self, permission: &UserPerms) -> User {
if !permission.get_view_profile() {
self.status = None;
}
self
}
/// Helper function to apply relationship and permission
#[must_use]
pub fn with_perspective(self, perspective: &User, permission: &UserPerms) -> User {
self.with_relationship(perspective)
.apply_permission(permission)
}
/// Helper function to calculate perspective
pub async fn with_auto_perspective(self, db: &Database, perspective: &User) -> User {
let user = self.with_relationship(perspective);
let permissions = perms(perspective).user(&user).calc_user(db).await;
user.apply_permission(&permissions)
}
/// Check whether two users have a mutual connection
///
/// This will check if user and user_b share a server or a group.
pub async fn has_mutual_connection(&self, db: &Database, user_b: &str) -> Result<bool> {
Ok(!db
.fetch_mutual_server_ids(&self.id, user_b)
.await?
.is_empty()
|| !db
.fetch_mutual_channel_ids(&self.id, user_b)
.await?
.is_empty())
}
/// Check if this user can acquire another server
pub async fn can_acquire_server(&self, db: &Database) -> Result<bool> {
// ! FIXME: hardcoded max server count
Ok(db.fetch_server_count(&self.id).await? <= 100)
}
/// Update a user's username
pub async fn update_username(&mut self, db: &Database, username: String) -> Result<()> {
let username = username.trim().to_string();
if db.is_username_taken(&username).await? {
return Err(Error::UsernameTaken);
}
self.update(
db,
PartialUser {
username: Some(username),
..Default::default()
},
vec![],
)
.await
}
/// Apply a certain relationship between two users
pub async fn apply_relationship(
&self,
db: &Database,
target: &mut User,
local: RelationshipStatus,
remote: RelationshipStatus,
) -> Result<()> {
if try_join!(
db.set_relationship(&self.id, &target.id, &local),
db.set_relationship(&target.id, &self.id, &remote)
)
.is_err()
{
return Err(Error::DatabaseError {
operation: "update_one",
with: "user",
});
}
EventV1::UserRelationship {
id: target.id.clone(),
user: self.clone().with_relationship(target),
status: remote,
}
.private(target.id.clone())
.await;
EventV1::UserRelationship {
id: self.id.clone(),
user: target.clone().with_relationship(self),
status: local.clone(),
}
.private(self.id.clone())
.await;
target.relationship.replace(local);
Ok(())
}
/// Add another user as a friend
pub async fn add_friend(&self, db: &Database, target: &mut User) -> Result<()> {
match get_relationship(self, &target.id) {
RelationshipStatus::User => Err(Error::NoEffect),
RelationshipStatus::Friend => Err(Error::AlreadyFriends),
RelationshipStatus::Outgoing => Err(Error::AlreadySentRequest),
RelationshipStatus::Blocked => Err(Error::Blocked),
RelationshipStatus::BlockedOther => Err(Error::BlockedByOther),
RelationshipStatus::Incoming => {
self.apply_relationship(
db,
target,
RelationshipStatus::Friend,
RelationshipStatus::Friend,
)
.await
}
RelationshipStatus::None => {
self.apply_relationship(
db,
target,
RelationshipStatus::Outgoing,
RelationshipStatus::Incoming,
)
.await
}
}
}
/// Remove another user as a friend
pub async fn remove_friend(&self, db: &Database, target: &mut User) -> Result<()> {
match get_relationship(self, &target.id) {
RelationshipStatus::Friend
| RelationshipStatus::Outgoing
| RelationshipStatus::Incoming => {
self.apply_relationship(
db,
target,
RelationshipStatus::None,
RelationshipStatus::None,
)
.await
}
_ => Err(Error::NoEffect),
}
}
/// Block another user
pub async fn block_user(&self, db: &Database, target: &mut User) -> Result<()> {
match get_relationship(self, &target.id) {
RelationshipStatus::User | RelationshipStatus::Blocked => Err(Error::NoEffect),
RelationshipStatus::BlockedOther => {
self.apply_relationship(
db,
target,
RelationshipStatus::Blocked,
RelationshipStatus::Blocked,
)
.await
}
RelationshipStatus::None
| RelationshipStatus::Friend
| RelationshipStatus::Incoming
| RelationshipStatus::Outgoing => {
self.apply_relationship(
db,
target,
RelationshipStatus::Blocked,
RelationshipStatus::BlockedOther,
)
.await
}
}
}
/// Unblock another user
pub async fn unblock_user(&self, db: &Database, target: &mut User) -> Result<()> {
match get_relationship(self, &target.id) {
RelationshipStatus::Blocked => match get_relationship(target, &self.id) {
RelationshipStatus::Blocked => {
self.apply_relationship(
db,
target,
RelationshipStatus::BlockedOther,
RelationshipStatus::Blocked,
)
.await
}
RelationshipStatus::BlockedOther => {
self.apply_relationship(
db,
target,
RelationshipStatus::None,
RelationshipStatus::None,
)
.await
}
_ => Err(Error::InternalError),
},
_ => Err(Error::NoEffect),
}
}
/// Check whether this user has another user blocked
pub fn has_blocked(&self, user: &str) -> bool {
matches!(
get_relationship(self, user),
RelationshipStatus::Blocked | RelationshipStatus::BlockedOther
)
}
/// Mark as deleted
pub async fn mark_deleted(&mut self, db: &Database) -> Result<()> {
self.update(
db,
PartialUser {
username: Some(format!("Deleted User {}", self.id)),
flags: Some(2),
..Default::default()
},
vec![
FieldsUser::Avatar,
FieldsUser::StatusText,
FieldsUser::StatusPresence,
FieldsUser::ProfileContent,
FieldsUser::ProfileBackground,
],
)
.await
}
/// Find a user from a given token and hint
#[async_recursion]
pub async fn from_token(db: &Database, token: &str, hint: UserHint) -> Result<User> {
match hint {
UserHint::Bot => db.fetch_user(&db.fetch_bot_by_token(token).await?.id).await,
UserHint::User => db.fetch_user_by_token(token).await,
UserHint::Any => {
if let Ok(user) = User::from_token(db, token, UserHint::User).await {
Ok(user)
} else {
User::from_token(db, token, UserHint::Bot).await
}
}
}
}
}
use rauth::entities::Session;
use rocket::http::Status;
use rocket::request::{self, FromRequest, Outcome, Request};
#[rocket::async_trait]
impl<'r> FromRequest<'r> for User {
type Error = rauth::util::Error;
async fn from_request(request: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
let user: &Option<User> = request
.local_cache_async(async {
let db = request
.rocket()
.state::<Database>()
.expect("Database state not reachable!");
let header_bot_token = request
.headers()
.get("x-bot-token")
.next()
.map(|x| x.to_string());
if let Some(bot_token) = header_bot_token {
if let Ok(user) = User::from_token(db, &bot_token, UserHint::Bot).await {
return Some(user);
}
} else if let Outcome::Success(session) = request.guard::<Session>().await {
// This uses a guard so can't really easily be refactored into from_token at this stage.
if let Ok(user) = db.fetch_user(&session.user_id).await {
return Some(user);
}
}
None
})
.await;
if let Some(user) = user {
Outcome::Success(user.clone())
} else {
Outcome::Failure((Status::Unauthorized, rauth::util::Error::InvalidSession))
}
}
}
impl<'r> OpenApiFromRequest<'r> for User {
fn from_request_input(
_gen: &mut OpenApiGenerator,
_name: String,
_required: bool,
) -> rocket_okapi::Result<RequestHeaderInput> {
let mut requirements = schemars::Map::new();
requirements.insert("Api Key".to_owned(), vec![]);
Ok(RequestHeaderInput::Security(
"Api Key".to_owned(),
SecurityScheme {
data: SecuritySchemeData::ApiKey {
name: "x-session-token".to_owned(),
location: "header".to_owned(),
},
description: Some("Session Token".to_owned()),
extensions: schemars::Map::new(),
},
requirements,
))
}
}

View File

@@ -0,0 +1,22 @@
use crate::{events::client::EventV1, models::UserSettings, Database, Result};
#[async_trait]
pub trait UserSettingsImpl {
async fn set(self, db: &Database, user: &str) -> Result<()>;
}
#[async_trait]
impl UserSettingsImpl for UserSettings {
async fn set(self, db: &Database, user: &str) -> Result<()> {
db.set_user_settings(user, &self).await?;
EventV1::UserSettingsUpdate {
id: user.to_string(),
update: self,
}
.private(user.to_string())
.await;
Ok(())
}
}

View File

@@ -0,0 +1,3 @@
pub mod dummy;
pub mod generic;
pub mod mongo;

View File

@@ -0,0 +1,26 @@
use crate::{AbstractMigrations, Result};
use super::super::MongoDb;
mod init;
mod scripts;
#[async_trait]
impl AbstractMigrations for MongoDb {
async fn migrate_database(&self) -> Result<()> {
info!("Migrating the database.");
let list = self
.list_database_names(None, None)
.await
.expect("Failed to fetch database names.");
if list.iter().any(|x| x == "revolt") {
scripts::migrate_database(self).await;
} else {
init::create_database(self).await;
}
Ok(())
}
}

View File

@@ -0,0 +1,187 @@
use crate::r#impl::mongo::MongoDb;
use super::scripts::LATEST_REVISION;
use log::info;
use mongodb::bson::doc;
use mongodb::options::CreateCollectionOptions;
pub async fn create_database(db: &MongoDb) {
info!("Creating database.");
let db = db.db();
db.create_collection("accounts", None)
.await
.expect("Failed to create accounts collection.");
db.create_collection("users", None)
.await
.expect("Failed to create users collection.");
db.create_collection("channels", None)
.await
.expect("Failed to create channels collection.");
db.create_collection("messages", None)
.await
.expect("Failed to create messages collection.");
db.create_collection("servers", None)
.await
.expect("Failed to create servers collection.");
db.create_collection("server_members", None)
.await
.expect("Failed to create server_members collection.");
db.create_collection("server_bans", None)
.await
.expect("Failed to create server_bans collection.");
db.create_collection("channel_invites", None)
.await
.expect("Failed to create channel_invites collection.");
db.create_collection("channel_unreads", None)
.await
.expect("Failed to create channel_unreads collection.");
db.create_collection("migrations", None)
.await
.expect("Failed to create migrations collection.");
db.create_collection("attachments", None)
.await
.expect("Failed to create attachments collection.");
db.create_collection("user_settings", None)
.await
.expect("Failed to create user_settings collection.");
db.create_collection("bots", None)
.await
.expect("Failed to create bots collection.");
db.create_collection(
"pubsub",
CreateCollectionOptions::builder()
.capped(true)
.size(1_000_000)
.build(),
)
.await
.expect("Failed to create pubsub collection.");
db.run_command(
doc! {
"createIndexes": "users",
"indexes": [
{
"key": {
"username": 1_i32
},
"name": "username",
"unique": true,
"collation": {
"locale": "en",
"strength": 2_i32
}
}
]
},
None,
)
.await
.expect("Failed to create username index.");
db.run_command(
doc! {
"createIndexes": "messages",
"indexes": [
{
"key": {
"content": "text"
},
"name": "content"
},
{
"key": {
"channel": 1_i32
},
"name": "channel"
},
{
"key": {
"channel": 1_i32,
"_id": 1_i32
},
"name": "channel_id_compound"
}
]
},
None,
)
.await
.expect("Failed to create message index.");
db.run_command(
doc! {
"createIndexes": "channel_unreads",
"indexes": [
{
"key": {
"_id.channel": 1_i32,
"_id.user": 1_i32,
},
"name": "compound_id"
},
{
"key": {
"_id.user": 1_i32,
},
"name": "user_id"
}
]
},
None,
)
.await
.expect("Failed to create channel_unreads index.");
db.run_command(
doc! {
"createIndexes": "server_members",
"indexes": [
{
"key": {
"_id.server": 1_i32,
"_id.user": 1_i32,
},
"name": "compound_id"
},
{
"key": {
"_id.user": 1_i32,
},
"name": "user_id"
}
]
},
None,
)
.await
.expect("Failed to create server_members index.");
db.collection("migrations")
.insert_one(
doc! {
"_id": 0_i32,
"revision": LATEST_REVISION
},
None,
)
.await
.expect("Failed to save migration info.");
info!("Created database.");
}

View File

@@ -0,0 +1,610 @@
use std::time::Duration;
use bson::Bson;
use futures::StreamExt;
use log::info;
use mongodb::{
bson::{doc, from_bson, from_document, to_document, Document},
options::FindOptions,
};
use serde::{Deserialize, Serialize};
use crate::{r#impl::mongo::MongoDb, Permission, DEFAULT_PERMISSION_SERVER};
#[derive(Serialize, Deserialize)]
struct MigrationInfo {
_id: i32,
revision: i32,
}
pub const LATEST_REVISION: i32 = 15;
pub async fn migrate_database(db: &MongoDb) {
let migrations = db.col::<Document>("migrations");
let data = migrations
.find_one(None, None)
.await
.expect("Failed to fetch migration data.");
if let Some(doc) = data {
let info: MigrationInfo =
from_document(doc).expect("Failed to read migration information.");
let revision = run_migrations(db, info.revision).await;
migrations
.update_one(
doc! {
"_id": info._id
},
doc! {
"$set": {
"revision": revision
}
},
None,
)
.await
.expect("Failed to commit migration information.");
info!("Migration complete. Currently at revision {}.", revision);
} else {
panic!("Database was configured incorrectly, possibly because initalization failed.")
}
}
pub async fn run_migrations(db: &MongoDb, revision: i32) -> i32 {
info!("Starting database migration.");
if revision <= 0 {
info!("Running migration [revision 0]: Test migration system.");
}
if revision <= 1 {
info!("Running migration [revision 1 / 2021-04-24]: Migrate to Autumn v1.0.0.");
let messages = db.col::<Document>("messages");
let attachments = db.col::<Document>("attachments");
messages
.update_many(
doc! { "attachment": { "$exists": 1_i32 } },
doc! { "$set": { "attachment.tag": "attachments", "attachment.size": 0_i32 } },
None,
)
.await
.expect("Failed to update messages.");
attachments
.update_many(
doc! {},
doc! { "$set": { "tag": "attachments", "size": 0_i32 } },
None,
)
.await
.expect("Failed to update attachments.");
}
if revision <= 2 {
info!("Running migration [revision 2 / 2021-05-08]: Add servers collection.");
db.db()
.create_collection("servers", None)
.await
.expect("Failed to create servers collection.");
}
if revision <= 3 {
info!("Running migration [revision 3 / 2021-05-25]: Support multiple file uploads, add channel_unreads and user_settings.");
let messages = db.col::<Document>("messages");
let mut cursor = messages
.find(
doc! {
"attachment": {
"$exists": 1_i32
}
},
FindOptions::builder()
.projection(doc! {
"_id": 1_i32,
"attachments": [ "$attachment" ]
})
.build(),
)
.await
.expect("Failed to fetch messages.");
while let Some(result) = cursor.next().await {
let doc = result.unwrap();
let id = doc.get_str("_id").unwrap();
let attachments = doc.get_array("attachments").unwrap();
messages
.update_one(
doc! { "_id": id },
doc! { "$unset": { "attachment": 1_i32 }, "$set": { "attachments": attachments } },
None,
)
.await
.unwrap();
}
db.db()
.create_collection("channel_unreads", None)
.await
.expect("Failed to create channel_unreads collection.");
db.db()
.create_collection("user_settings", None)
.await
.expect("Failed to create user_settings collection.");
}
if revision <= 4 {
info!("Running migration [revision 4 / 2021-06-01]: Add more server collections.");
db.db()
.create_collection("server_members", None)
.await
.expect("Failed to create server_members collection.");
db.db()
.create_collection("server_bans", None)
.await
.expect("Failed to create server_bans collection.");
db.db()
.create_collection("channel_invites", None)
.await
.expect("Failed to create channel_invites collection.");
}
if revision <= 5 {
info!("Running migration [revision 5 / 2021-06-26]: Add permissions.");
#[derive(Serialize)]
struct Server {
pub default_permissions: (i32, i32),
}
let server = Server {
default_permissions: (0_i32, 0_i32),
};
db.col::<Document>("servers")
.update_many(
doc! {},
doc! {
"$set": to_document(&server).unwrap()
},
None,
)
.await
.expect("Failed to migrate servers.");
}
if revision <= 6 {
info!("Running migration [revision 6 / 2021-07-09]: Add message text index.");
db.db()
.run_command(
doc! {
"createIndexes": "messages",
"indexes": [
{
"key": {
"content": "text"
},
"name": "content"
}
]
},
None,
)
.await
.expect("Failed to create message index.");
}
if revision <= 7 {
info!("Running migration [revision 7 / 2021-08-11]: Add message text index.");
db.db()
.create_collection("bots", None)
.await
.expect("Failed to create bots collection.");
}
if revision <= 8 {
info!("Running migration [revision 8 / 2021-09-10]: Update to rAuth version 1.");
db.db()
.run_command(
doc! {
"dropIndexes": "accounts",
"index": ["email", "email_normalised"]
},
None,
)
.await
.expect("Failed to delete legacy account indexes.");
let col = db.col::<Document>("sessions");
let mut cursor = db
.col::<Document>("accounts")
.find(doc! {}, None)
.await
.unwrap();
while let Some(doc) = cursor.next().await {
if let Ok(account) = doc {
let id = account.get_str("_id").unwrap();
if let Some(sessions) = account.get("sessions") {
#[derive(Deserialize)]
struct Session {
id: String,
token: String,
friendly_name: String,
subscription: Option<Document>,
}
let sessions = from_bson::<Vec<Session>>(sessions.clone()).unwrap();
for session in sessions {
info!("Converting session {} to new format.", &session.id);
let mut doc = doc! {
"_id": session.id,
"token": session.token,
"user_id": id,
"name": session.friendly_name,
};
if let Some(sub) = session.subscription {
doc.insert("subscription", sub);
}
col.insert_one(doc, None).await.ok();
}
} else {
info!("Account doesn't have any sessions!");
}
}
}
db.col::<Document>("accounts")
.update_many(
doc! {},
doc! {
"$unset": {
"sessions": 1_i32,
},
"$set": {
"mfa": {
"recovery_codes": []
}
}
},
None,
)
.await
.unwrap();
}
if revision <= 9 {
info!("Running migration [revision 9 / 2021-09-14]: Switch from last_message to last_message_id.");
let mut cursor = db
.col::<Document>("channels")
.find(doc! {}, None)
.await
.unwrap();
while let Some(doc) = cursor.next().await {
if let Ok(channel) = doc {
let channel_id = channel.get_str("_id").unwrap();
if let Some(last_message) = channel.get("last_message") {
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Obj {
#[serde(rename = "_id")]
id: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum LastMessage {
Obj(Obj),
Id(String),
}
let lm = from_bson::<LastMessage>(last_message.clone()).unwrap();
let id = match lm {
LastMessage::Obj(Obj { id }) => id,
LastMessage::Id(id) => id,
};
info!("Converting session {} to new format.", &channel_id);
db.col::<Document>("channels")
.update_one(
doc! {
"_id": channel_id
},
doc! {
"$set": {
"last_message_id": id
},
"$unset": {
"last_message": 1_i32,
}
},
None,
)
.await
.unwrap();
} else {
info!("{} has no last_message.", &channel_id);
}
}
}
}
if revision <= 10 {
info!("Running migration [revision 10 / 2021-11-01]: Remove nonce values on channels and servers.");
db.col::<Document>("servers")
.update_many(
doc! {},
doc! {
"$unset": {
"nonce": 1_i32,
}
},
None,
)
.await
.unwrap();
db.col::<Document>("channels")
.update_many(
doc! {},
doc! {
"$unset": {
"nonce": 1_i32,
}
},
None,
)
.await
.unwrap();
}
if revision <= 11 {
info!("Running migration [revision 11 / 2021-11-14]: Add indexes to database.");
db.db()
.run_command(
doc! {
"createIndexes": "messages",
"indexes": [
{
"key": {
"channel": 1_i32
},
"name": "channel"
}
]
},
None,
)
.await
.expect("Failed to create message index.");
db.db()
.run_command(
doc! {
"createIndexes": "channel_unreads",
"indexes": [
{
"key": {
"_id.channel": 1_i32,
"_id.user": 1_i32,
},
"name": "compound_id"
},
{
"key": {
"_id.user": 1_i32,
},
"name": "user_id"
}
]
},
None,
)
.await
.expect("Failed to create channel_unreads index.");
db.db()
.run_command(
doc! {
"createIndexes": "server_members",
"indexes": [
{
"key": {
"_id.server": 1_i32,
"_id.user": 1_i32,
},
"name": "compound_id"
},
{
"key": {
"_id.user": 1_i32,
},
"name": "user_id"
}
]
},
None,
)
.await
.expect("Failed to create server_members index.");
}
if revision <= 12 {
info!("Running migration [revision 12 / 2021-11-21]: Add indexes to database.");
db.db()
.run_command(
doc! {
"createIndexes": "messages",
"indexes": [
{
"key": {
"channel": 1_i32,
"_id": 1_i32
},
"name": "channel_id_compound"
}
]
},
None,
)
.await
.expect("Failed to create message index.");
}
if revision <= 13 {
info!("Running migration [revision 13 / 22-02-2022]: Wipe legacy permission values.");
warn!("This is a destructive operation and will wipe existing permission data (excl. defaults for SendMessage).");
warn!("Taking a backup is advised.");
warn!("Continuing in 10 seconds...");
async_std::task::sleep(Duration::from_secs(10)).await;
let servers = db.col::<Document>("servers");
let mut cursor = servers.find(doc! {}, None).await.unwrap();
while let Some(Ok(mut document)) = cursor.next().await {
let id = document.get_str("_id").unwrap().to_string();
info!("Updating server {id}");
let mut update = doc! {};
// Try to pluck channel permission SendMessage (0x2)
// Structure of default_permissions used to be [server, channel]
let has_send = document
.get_array("default_permissions")
.map(|x| {
x.get(1)
.map(|x| x.as_i32().map(|x| (x as u32 & 0x2) == 0x2))
})
.ok()
.flatten()
.flatten()
.unwrap_or_default();
update.insert(
"default_permissions",
(*DEFAULT_PERMISSION_SERVER
// Remove Send Message permission if it wasn't originally granted
^ (if has_send {
0
} else {
Permission::SendMessage as u64
})) as i64,
);
if let Some(Bson::Document(mut roles)) = document.remove("roles") {
for role in roles.keys().cloned().collect::<Vec<String>>() {
if let Some(Bson::Document(role)) = roles.get_mut(role) {
role.insert(
"permissions",
doc! {
"a": 0_i64,
"d": 0_i64,
},
);
}
}
update.insert("roles", roles);
}
servers
.update_one(doc! { "_id": id }, doc! { "$set": update }, None)
.await
.unwrap();
}
let channels = db.col::<Document>("channels");
let mut cursor = channels.find(doc! {}, None).await.unwrap();
while let Some(Ok(document)) = cursor.next().await {
let id = document.get_str("_id").unwrap().to_string();
info!("Updating channel {id}");
let mut unset = doc! {
"permissions": 1_i32,
"role_permissions": 1_i32,
};
// Try to pluck channel permission SendMessage (0x2)
let has_send = document
.get_i32("default_permissions")
.map(|x| (x as u32 & 0x2) == 0x2)
.unwrap_or(true);
if has_send {
// Let parent permissions fall through.
unset.insert("default_permissions", 1_i32);
}
let mut update = doc! {
"$unset": unset
};
if !has_send {
// Block send message permission.
update.insert(
"$set",
doc! {
"default_permissions": {
"a": 0_i64,
"d": Permission::SendMessage as i64
}
},
);
}
channels
.update_one(doc! { "_id": id }, update, None)
.await
.unwrap();
}
}
if revision <= 14 {
info!("Running migration [revision 14 / 21-04-2022]: Split content into content and system fields.");
db.col::<Document>("messages")
.update_many(
doc! {
"content": {
"$type": "object"
}
},
doc! {
"$rename": {
"content": "system"
}
},
None,
)
.await
.unwrap();
}
// Need to migrate fields on attachments, change `user_id`, `object_id`, etc to `parent`.
// Reminder to update LATEST_REVISION when adding new migrations.
LATEST_REVISION
}

View File

@@ -0,0 +1,125 @@
use bson::Document;
use crate::models::attachment::File;
use crate::{AbstractAttachment, Error, Result};
use super::super::MongoDb;
static COL: &str = "attachments";
impl MongoDb {
pub async fn delete_many_attachments(&self, projection: Document) -> Result<()> {
self.col::<Document>(COL)
.update_many(
projection,
doc! {
"$set": {
"deleted": true
}
},
None,
)
.await
.map(|_| ())
.map_err(|_| Error::DatabaseError {
operation: "update_many",
with: "attachment",
})
}
}
#[async_trait]
impl AbstractAttachment for MongoDb {
async fn find_and_use_attachment(
&self,
attachment_id: &str,
tag: &str,
parent_type: &str,
parent_id: &str,
) -> Result<File> {
let key = format!("{}_id", parent_type);
match self
.find_one::<File>(
COL,
doc! {
"_id": attachment_id,
"tag": tag,
&key: {
"$exists": false
}
},
)
.await
{
Ok(file) => {
self.col::<Document>(COL)
.update_one(
doc! {
"_id": &file.id
},
doc! {
"$set": {
key: parent_id
}
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "attachment",
})?;
Ok(file)
}
Err(Error::NotFound) => Err(Error::UnknownAttachment),
Err(error) => Err(error),
}
}
async fn insert_attachment(&self, attachment: &File) -> Result<()> {
self.insert_one(COL, attachment).await.map(|_| ())
}
async fn mark_attachment_as_reported(&self, id: &str) -> Result<()> {
self.col::<Document>(COL)
.update_one(
doc! {
"_id": id
},
doc! {
"$set": {
"reported": true
}
},
None,
)
.await
.map(|_| ())
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "attachment",
})
}
async fn mark_attachment_as_deleted(&self, id: &str) -> Result<()> {
self.col::<Document>(COL)
.update_one(
doc! {
"_id": id
},
doc! {
"$set": {
"deleted": true
}
},
None,
)
.await
.map(|_| ())
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "attachment",
})
}
}

View File

@@ -0,0 +1,323 @@
use std::collections::HashSet;
use bson::{Bson, Document};
use crate::models::channel::{Channel, FieldsChannel, PartialChannel};
use crate::r#impl::mongo::IntoDocumentPath;
use crate::{AbstractChannel, AbstractServer, Error, OverrideField, Result};
use super::super::MongoDb;
static COL: &str = "channels";
impl MongoDb {
pub async fn delete_associated_channel_objects(&self, id: Bson) -> Result<()> {
// Delete all invites to these channels.
self.col::<Document>("channel_invites")
.delete_many(
doc! {
"channel": &id
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "delete_many",
with: "channel_invites",
})?;
// Delete unread message objects on channels.
self.col::<Document>("channel_unreads")
.delete_many(
doc! {
"_id.channel": &id
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "delete_many",
with: "channel_unreads",
})
.map(|_| ())
// update many attachments with parent id
}
}
#[async_trait]
impl AbstractChannel for MongoDb {
async fn fetch_channel(&self, id: &str) -> Result<Channel> {
self.find_one_by_id(COL, id).await
}
async fn fetch_channels<'a>(&self, ids: &'a [String]) -> Result<Vec<Channel>> {
self.find(
COL,
doc! {
"_id": {
"$in": ids
}
},
)
.await
}
async fn insert_channel(&self, channel: &Channel) -> Result<()> {
self.insert_one(COL, channel).await.map(|_| ())
}
async fn update_channel(
&self,
id: &str,
channel: &PartialChannel,
remove: Vec<FieldsChannel>,
) -> Result<()> {
self.update_one_by_id(
COL,
id,
channel,
remove.iter().map(|x| x as &dyn IntoDocumentPath).collect(),
None,
)
.await
.map(|_| ())
}
async fn delete_channel(&self, channel: &Channel) -> Result<()> {
let id = channel.id().to_string();
let server_id = match channel {
Channel::TextChannel { server, .. } | Channel::VoiceChannel { server, .. } => {
Some(server)
}
_ => None,
};
// Delete invites and unreads.
self.delete_associated_channel_objects(Bson::String(id.to_string()))
.await?;
// Delete messages.
self.delete_bulk_messages(doc! {
"channel": &id
})
.await?;
// Remove from server object.
if let Some(server) = server_id {
let server = self.fetch_server(server).await?;
let mut update = doc! {
"$pull": {
"channels": &id
}
};
if let Some(sys) = &server.system_messages {
let mut unset = doc! {};
if let Some(cid) = &sys.user_joined {
if &id == cid {
unset.insert("system_messages.user_joined", 1_i32);
}
}
if let Some(cid) = &sys.user_left {
if &id == cid {
unset.insert("system_messages.user_left", 1_i32);
}
}
if let Some(cid) = &sys.user_kicked {
if &id == cid {
unset.insert("system_messages.user_kicked", 1_i32);
}
}
if let Some(cid) = &sys.user_banned {
if &id == cid {
unset.insert("system_messages.user_banned", 1_i32);
}
}
if !unset.is_empty() {
update.insert("$unset", unset);
}
}
self.col::<Document>("servers")
.update_one(
doc! {
"_id": server.id
},
update,
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "servers",
})?;
}
// Delete associated attachments
self.delete_many_attachments(doc! {
"object_id": &id
})
.await?;
// Delete the channel itself
self.delete_one_by_id(COL, &id).await.map(|_| ())
}
async fn find_direct_messages(&self, user_id: &str) -> Result<Vec<Channel>> {
self.find(
COL,
doc! {
"$or": [
{
"$or": [
{
"channel_type": "DirectMessage"
},
{
"channel_type": "Group"
}
],
"recipients": user_id
},
{
"channel_type": "SavedMessages",
"user": user_id
}
]
},
)
.await
}
async fn find_saved_messages_channel(&self, user_id: &str) -> Result<Channel> {
self.find_one(
COL,
doc! {
"channel_type": "SavedMessages",
"user": user_id
},
)
.await
}
async fn find_direct_message_channel(&self, user_a: &str, user_b: &str) -> Result<Channel> {
self.find_one(
COL,
if user_a == user_b {
doc! {
"channel_type": "SavedMessages",
"user": user_a
}
} else {
doc! {
"channel_type": "DirectMessage",
"recipients": {
"$all": [ user_a, user_b ]
}
}
},
)
.await
}
async fn add_user_to_group(&self, channel: &str, user: &str) -> Result<()> {
self.col::<Document>(COL)
.update_one(
doc! {
"_id": channel
},
doc! {
"$push": {
"recipients": user
}
},
None,
)
.await
.map(|_| ())
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "channel",
})
}
async fn remove_user_from_group(&self, channel: &str, user: &str) -> Result<()> {
self.col::<Document>(COL)
.update_one(
doc! {
"_id": channel
},
doc! {
"$pull": {
"recipients": user
}
},
None,
)
.await
.map(|_| ())
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "channel",
})
}
async fn set_channel_role_permission(
&self,
channel: &str,
role: &str,
permissions: OverrideField,
) -> Result<()> {
self.col::<Document>(COL)
.update_one(
doc! { "_id": channel },
doc! {
"$set": {
"role_permissions.".to_owned() + role: permissions
}
},
None,
)
.await
.map(|_| ())
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "channel",
})
}
async fn check_channels_exist(&self, channels: &HashSet<String>) -> Result<bool> {
let count = channels.len() as u64;
self.col::<Document>(COL)
.count_documents(
doc! {
"_id": {
"$in": channels.iter().cloned().collect::<Vec<String>>()
}
},
None,
)
.await
.map(|x| x == count)
.map_err(|_| Error::DatabaseError {
operation: "count_documents",
with: "channel",
})
}
}
impl IntoDocumentPath for FieldsChannel {
fn as_path(&self) -> Option<&'static str> {
Some(match self {
FieldsChannel::DefaultPermissions => "default_permissions",
FieldsChannel::Description => "description",
FieldsChannel::Icon => "icon",
})
}
}

View File

@@ -0,0 +1,31 @@
use crate::models::Invite;
use crate::{AbstractChannelInvite, Result};
use super::super::MongoDb;
static COL: &str = "channel_invites";
#[async_trait]
impl AbstractChannelInvite for MongoDb {
async fn fetch_invite(&self, code: &str) -> Result<Invite> {
self.find_one_by_id(COL, code).await
}
async fn insert_invite(&self, invite: &Invite) -> Result<()> {
self.insert_one(COL, invite).await.map(|_| ())
}
async fn delete_invite(&self, code: &str) -> Result<()> {
self.delete_one_by_id(COL, code).await.map(|_| ())
}
async fn fetch_invites_for_server(&self, server: &str) -> Result<Vec<Invite>> {
self.find(
COL,
doc! {
"server": server
},
)
.await
}
}

View File

@@ -0,0 +1,104 @@
use bson::Document;
use mongodb::options::UpdateOptions;
use ulid::Ulid;
use crate::models::channel_unread::ChannelUnread;
use crate::{AbstractChannelUnread, Error, Result};
use super::super::MongoDb;
static COL: &str = "channel_unreads";
#[async_trait]
impl AbstractChannelUnread for MongoDb {
async fn acknowledge_message(&self, channel: &str, user: &str, message: &str) -> Result<()> {
self.col::<Document>(COL)
.update_one(
doc! {
"_id.channel": channel,
"_id.user": user,
},
doc! {
"$unset": {
"mentions": 1_i32
},
"$set": {
"last_id": message
}
},
UpdateOptions::builder().upsert(true).build(),
)
.await
.map(|_| ())
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "channel_unread",
})
}
async fn acknowledge_channels(&self, user: &str, channels: &[String]) -> Result<()> {
self.col::<Document>(COL)
.update_one(
doc! {
"_id.channel": {
"$in": channels
},
"_id.user": user,
},
doc! {
"$unset": {
"mentions": 1_i32
},
"$set": {
"last_id": Ulid::new().to_string()
}
},
UpdateOptions::builder().upsert(true).build(),
)
.await
.map(|_| ())
.map_err(|_| Error::DatabaseError {
operation: "update",
with: "channel_unread",
})
}
async fn add_mention_to_unread<'a>(
&self,
channel: &str,
user: &str,
ids: &[String],
) -> Result<()> {
self.col::<Document>(COL)
.update_one(
doc! {
"_id.channel": channel,
"_id.user": user,
},
doc! {
"$push": {
"mentions": {
"$each": ids
}
}
},
UpdateOptions::builder().upsert(true).build(),
)
.await
.map(|_| ())
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "channel_unread",
})
}
async fn fetch_unreads(&self, user: &str) -> Result<Vec<ChannelUnread>> {
self.find(
COL,
doc! {
"_id.user": user
},
)
.await
}
}

View File

@@ -0,0 +1,285 @@
use bson::{to_bson, Document};
use futures::try_join;
use mongodb::options::FindOptions;
use crate::models::message::{AppendMessage, Message, MessageSort, PartialMessage};
use crate::r#impl::mongo::DocumentId;
use crate::{AbstractMessage, Error, Result};
use super::super::MongoDb;
static COL: &str = "messages";
impl MongoDb {
pub async fn delete_bulk_messages(&self, projection: Document) -> Result<()> {
let mut for_attachments = projection.clone();
for_attachments.insert(
"attachment",
doc! {
"$exists": 1_i32
},
);
// Check if there are any attachments we need to delete.
let message_ids_with_attachments = self
.find_with_options::<_, DocumentId>(
COL,
for_attachments,
FindOptions::builder()
.projection(doc! { "_id": 1_i32 })
.build(),
)
.await?
.into_iter()
.map(|x| x.id)
.collect::<Vec<String>>();
// If we found any, mark them as deleted.
if !message_ids_with_attachments.is_empty() {
self.col::<Document>("attachments")
.update_many(
doc! {
"message_id": {
"$in": message_ids_with_attachments
}
},
doc! {
"$set": {
"deleted": true
}
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_many",
with: "attachments",
})?;
}
// And then delete said messages.
self.col::<Document>(COL)
.delete_many(projection, None)
.await
.map(|_| ())
.map_err(|_| Error::DatabaseError {
operation: "delete_many",
with: "messages",
})
}
}
#[async_trait]
impl AbstractMessage for MongoDb {
async fn fetch_message(&self, id: &str) -> Result<Message> {
self.find_one_by_id(COL, id).await
}
async fn insert_message(&self, message: &Message) -> Result<()> {
self.insert_one(COL, message).await.map(|_| ())
}
async fn update_message(&self, id: &str, message: &PartialMessage) -> Result<()> {
self.update_one_by_id(COL, id, message, vec![], None)
.await
.map(|_| ())
}
async fn append_message(&self, id: &str, append: &AppendMessage) -> Result<()> {
let mut query = doc! {};
if let Some(embeds) = &append.embeds {
if !embeds.is_empty() {
query.insert(
"$push",
doc! {
"embeds": {
"$each": to_bson(embeds)
.map_err(|_| Error::DatabaseError {
operation: "to_bson",
with: "embeds"
})?
}
},
);
}
}
if query.is_empty() {
return Ok(());
}
self.col::<Document>(COL)
.update_one(
doc! {
"_id": id
},
query,
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "message",
})
.map(|_| ())
}
async fn delete_message(&self, id: &str) -> Result<()> {
self.delete_bulk_messages(doc! {
"_id": id
})
.await
}
async fn delete_messages(&self, channel: &str, ids: Vec<String>) -> Result<()> {
self.delete_bulk_messages(doc! {
"channel": channel,
"_id": {
"$in": ids
}
})
.await
}
async fn fetch_messages(
&self,
channel: &str,
limit: Option<i64>,
before: Option<String>,
after: Option<String>,
sort: Option<MessageSort>,
nearby: Option<String>,
) -> Result<Vec<Message>> {
let limit = limit.unwrap_or(50);
Ok(if let Some(nearby) = nearby {
let (a, b) = try_join!(
self.find_with_options::<_, Message>(
COL,
doc! {
"channel": channel,
"_id": {
"$gte": &nearby
}
},
FindOptions::builder()
.limit(limit / 2 + 1)
.sort(doc! {
"_id": 1_i32
})
.build(),
),
self.find_with_options::<_, Message>(
COL,
doc! {
"channel": channel,
"_id": {
"$lt": &nearby
}
},
FindOptions::builder()
.limit(limit / 2)
.sort(doc! {
"_id": -1_i32
})
.build(),
)
)?;
[a, b].concat()
} else {
let mut query = doc! { "channel": channel };
if let Some(before) = before {
query.insert("_id", doc! { "$lt": before });
}
if let Some(after) = after {
query.insert("_id", doc! { "$gt": after });
}
let sort: i32 = if let MessageSort::Latest = sort.unwrap_or(MessageSort::Latest) {
-1
} else {
1
};
self.find_with_options::<_, Message>(
COL,
query,
FindOptions::builder()
.limit(limit)
.sort(doc! {
"_id": sort
})
.build(),
)
.await?
})
}
async fn search_messages(
&self,
channel: &str,
query: &str,
limit: Option<i64>,
before: Option<String>,
after: Option<String>,
sort: MessageSort,
) -> Result<Vec<Message>> {
let limit = limit.unwrap_or(50);
let mut filter = doc! {
"channel": channel,
"$text": {
"$search": query
}
};
if let Some(doc) = match (before, after) {
(Some(before), Some(after)) => Some(doc! {
"lt": before,
"gt": after
}),
(Some(before), _) => Some(doc! {
"lt": before
}),
(_, Some(after)) => Some(doc! {
"gt": after
}),
_ => None,
} {
filter.insert("_id", doc);
}
self.find_with_options(
COL,
filter,
FindOptions::builder()
.projection(if let MessageSort::Relevance = &sort {
doc! {
"score": {
"$meta": "textScore"
}
}
} else {
doc! {}
})
.limit(limit)
.sort(match &sort {
MessageSort::Relevance => doc! {
"score": {
"$meta": "textScore"
}
},
MessageSort::Latest => doc! {
"_id": -1_i32
},
MessageSort::Oldest => doc! {
"_id": 1_i32
},
})
.build(),
)
.await
}
}

View File

@@ -0,0 +1,250 @@
use std::ops::Deref;
use bson::{to_document, Document};
use futures::StreamExt;
use mongodb::{
options::{FindOneOptions, FindOptions},
results::{DeleteResult, InsertOneResult, UpdateResult},
};
use rocket::serde::DeserializeOwned;
use serde::{Deserialize, Serialize};
use crate::{util::manipulation::prefix_keys, AbstractDatabase, Error, Result};
pub mod admin {
pub mod migrations;
}
pub mod autumn {
pub mod attachment;
}
pub mod channels {
pub mod channel;
pub mod channel_invite;
pub mod channel_unread;
pub mod message;
}
pub mod servers {
pub mod server;
pub mod server_ban;
pub mod server_member;
}
pub mod users {
pub mod bot;
pub mod user;
pub mod user_settings;
}
#[derive(Debug, Clone)]
pub struct MongoDb(pub mongodb::Client);
impl AbstractDatabase for MongoDb {}
impl Deref for MongoDb {
type Target = mongodb::Client;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl MongoDb {
pub fn db(&self) -> mongodb::Database {
self.database("revolt")
}
pub fn col<T>(&self, collection: &str) -> mongodb::Collection<T> {
self.db().collection(collection)
}
async fn insert_one<T: Serialize>(
&self,
collection: &'static str,
document: T,
) -> Result<InsertOneResult> {
self.col::<T>(collection)
.insert_one(document, None)
.await
.map_err(|_| Error::DatabaseError {
operation: "insert_one",
with: collection,
})
}
async fn find_with_options<O, T: DeserializeOwned + Unpin + Send + Sync>(
&self,
collection: &'static str,
projection: Document,
options: O,
) -> Result<Vec<T>>
where
O: Into<Option<FindOptions>>,
{
Ok(self
.col::<T>(collection)
.find(projection, options)
.await
.map_err(|_| Error::DatabaseError {
operation: "find",
with: collection,
})?
.filter_map(|s| async { s.ok() })
.collect::<Vec<T>>()
.await)
}
async fn find<T: DeserializeOwned + Unpin + Send + Sync>(
&self,
collection: &'static str,
projection: Document,
) -> Result<Vec<T>> {
self.find_with_options(collection, projection, None).await
}
async fn find_one_with_options<O, T: DeserializeOwned + Unpin + Send + Sync>(
&self,
collection: &'static str,
projection: Document,
options: O,
) -> Result<T>
where
O: Into<Option<FindOneOptions>>,
{
self.col::<T>(collection)
.find_one(projection, options)
.await
.map_err(|_| Error::DatabaseError {
operation: "find_one",
with: collection,
})?
.ok_or(Error::NotFound)
}
async fn find_one<T: DeserializeOwned + Unpin + Send + Sync>(
&self,
collection: &'static str,
projection: Document,
) -> Result<T> {
self.find_one_with_options(collection, projection, None)
.await
}
async fn find_one_by_id<T: DeserializeOwned + Unpin + Send + Sync>(
&self,
collection: &'static str,
id: &str,
) -> Result<T> {
self.find_one(
collection,
doc! {
"_id": id
},
)
.await
}
async fn update_one<P, T: Serialize>(
&self,
collection: &'static str,
projection: Document,
partial: T,
remove: Vec<&dyn IntoDocumentPath>,
prefix: P,
) -> Result<UpdateResult>
where
P: Into<Option<String>>,
{
let prefix = prefix.into();
let mut unset = doc! {};
for field in remove {
if let Some(path) = field.as_path() {
if let Some(prefix) = &prefix {
unset.insert(prefix.to_owned() + path, 1_i32);
} else {
unset.insert(path, 1_i32);
}
}
}
let query = doc! {
"$unset": unset,
"$set": if let Some(prefix) = &prefix {
to_document(&prefix_keys(&partial, prefix))
} else {
to_document(&partial)
}.map_err(|_| Error::DatabaseError {
operation: "to_document",
with: collection
})?
};
self.col::<Document>(collection)
.update_one(projection, query, None)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: collection,
})
}
async fn update_one_by_id<P, T: Serialize>(
&self,
collection: &'static str,
id: &str,
partial: T,
remove: Vec<&dyn IntoDocumentPath>,
prefix: P,
) -> Result<UpdateResult>
where
P: Into<Option<String>>,
{
self.update_one(
collection,
doc! {
"_id": id
},
partial,
remove,
prefix,
)
.await
}
async fn delete_one(
&self,
collection: &'static str,
projection: Document,
) -> Result<DeleteResult> {
self.col::<Document>(collection)
.delete_one(projection, None)
.await
.map_err(|_| Error::DatabaseError {
operation: "delete_one",
with: collection,
})
}
async fn delete_one_by_id(&self, collection: &'static str, id: &str) -> Result<DeleteResult> {
self.delete_one(
collection,
doc! {
"_id": id
},
)
.await
}
}
#[derive(Deserialize)]
pub struct DocumentId {
#[serde(rename = "_id")]
pub id: String,
}
pub trait IntoDocumentPath: Send + Sync {
fn as_path(&self) -> Option<&'static str>;
}

View File

@@ -0,0 +1,228 @@
use bson::{to_document, Bson, Document};
use crate::models::server::{FieldsRole, FieldsServer, PartialRole, PartialServer, Role, Server};
use crate::r#impl::mongo::IntoDocumentPath;
use crate::{AbstractServer, Database, Error, Result};
use super::super::MongoDb;
static COL: &str = "servers";
impl MongoDb {
pub async fn delete_associated_server_objects(&self, server: &Server) -> Result<()> {
// Check if there are any attachments we need to delete.
self.delete_bulk_messages(doc! {
"channel": {
"$in": &server.channels
}
})
.await?;
// Delete all channels.
self.col::<Document>("channels")
.delete_many(
doc! {
"server": &server.id
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "delete_many",
with: "channels",
})?;
// Delete any associated objects, e.g. unreads and invites.
self.delete_associated_channel_objects(Bson::Document(doc! { "$in": &server.channels }))
.await?;
// Delete members and bans.
for with in &["server_members", "server_bans"] {
self.col::<Document>(with)
.delete_many(
doc! {
"_id.server": &server.id
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "delete_many",
with,
})?;
}
// Update many attachments with parent id.
self.delete_many_attachments(doc! {
"object_id": &server.id
})
.await?;
Ok(())
}
}
#[async_trait]
impl AbstractServer for MongoDb {
async fn fetch_server(&self, id: &str) -> Result<Server> {
self.find_one_by_id(COL, id).await
}
async fn fetch_servers<'a>(&self, ids: &'a [String]) -> Result<Vec<Server>> {
self.find(
COL,
doc! {
"_id": {
"$in": ids
}
},
)
.await
}
async fn insert_server(&self, server: &Server) -> Result<()> {
self.insert_one(COL, server).await.map(|_| ())
}
async fn update_server(
&self,
id: &str,
server: &PartialServer,
remove: Vec<FieldsServer>,
) -> Result<()> {
self.update_one_by_id(
COL,
id,
server,
remove.iter().map(|x| x as &dyn IntoDocumentPath).collect(),
None,
)
.await
.map(|_| ())
}
async fn delete_server(&self, server: &Server) -> Result<()> {
self.delete_associated_server_objects(server).await?;
self.delete_one_by_id(COL, &server.id).await.map(|_| ())
}
async fn insert_role(&self, server_id: &str, role_id: &str, role: &Role) -> Result<()> {
self.col::<Database>(COL)
.update_one(
doc! {
"_id": server_id
},
doc! {
"$set": {
"roles.".to_owned() + role_id: to_document(role)
.map_err(|_| Error::DatabaseError {
operation: "to_document",
with: "role"
})?
}
},
None,
)
.await
.map(|_| ())
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "server",
})
}
async fn update_role(
&self,
server_id: &str,
role_id: &str,
role: &PartialRole,
remove: Vec<FieldsRole>,
) -> Result<()> {
self.update_one_by_id(
COL,
server_id,
role,
remove.iter().map(|x| x as &dyn IntoDocumentPath).collect(),
"roles.".to_owned() + role_id + ".",
)
.await
.map(|_| ())
}
async fn delete_role(&self, server_id: &str, role_id: &str) -> Result<()> {
self.col::<Document>("server_members")
.update_many(
doc! {
"_id.server": server_id
},
doc! {
"$pull": {
"roles": &role_id
}
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_many",
with: "server_members",
})?;
self.col::<Document>("channels")
.update_one(
doc! {
"server": server_id
},
doc! {
"$unset": {
"role_permissions.".to_owned() + role_id: 1_i32
}
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "channels",
})?;
self.col::<Document>("servers")
.update_one(
doc! {
"_id": server_id
},
doc! {
"$unset": {
"roles.".to_owned() + role_id: 1_i32
}
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "servers",
})
.map(|_| ())
}
}
impl IntoDocumentPath for FieldsServer {
fn as_path(&self) -> Option<&'static str> {
Some(match self {
FieldsServer::Banner => "banner",
FieldsServer::Categories => "categories",
FieldsServer::Description => "description",
FieldsServer::Icon => "icon",
FieldsServer::SystemMessages => "system_messages",
})
}
}
impl IntoDocumentPath for FieldsRole {
fn as_path(&self) -> Option<&'static str> {
Some(match self {
FieldsRole::Colour => "colour",
})
}
}

View File

@@ -0,0 +1,47 @@
use crate::models::server_member::MemberCompositeKey;
use crate::models::ServerBan;
use crate::{AbstractServerBan, Result};
use super::super::MongoDb;
static COL: &str = "server_bans";
#[async_trait]
impl AbstractServerBan for MongoDb {
async fn fetch_ban(&self, server: &str, user: &str) -> Result<ServerBan> {
self.find_one(
COL,
doc! {
"_id.server": server,
"_id.user": user
},
)
.await
}
async fn fetch_bans(&self, server: &str) -> Result<Vec<ServerBan>> {
self.find(
COL,
doc! {
"_id.server": server
},
)
.await
}
async fn insert_ban(&self, ban: &ServerBan) -> Result<()> {
self.insert_one(COL, ban).await.map(|_| ())
}
async fn delete_ban(&self, id: &MemberCompositeKey) -> Result<()> {
self.delete_one(
COL,
doc! {
"_id.server": &id.server,
"_id.user": &id.user
},
)
.await
.map(|_| ())
}
}

View File

@@ -0,0 +1,134 @@
use bson::Document;
use crate::models::server_member::{FieldsMember, Member, MemberCompositeKey, PartialMember};
use crate::r#impl::mongo::IntoDocumentPath;
use crate::{AbstractServerMember, Error, Result};
use super::super::MongoDb;
static COL: &str = "server_members";
#[async_trait]
impl AbstractServerMember for MongoDb {
async fn fetch_member(&self, server: &str, user: &str) -> Result<Member> {
self.find_one(
COL,
doc! {
"_id.server": server,
"_id.user": user
},
)
.await
}
async fn insert_member(&self, member: &Member) -> Result<()> {
self.insert_one(COL, member).await.map(|_| ())
}
async fn update_member(
&self,
id: &MemberCompositeKey,
member: &PartialMember,
remove: Vec<FieldsMember>,
) -> Result<()> {
self.update_one(
COL,
doc! {
"_id.server": &id.server,
"_id.user": &id.user
},
member,
remove.iter().map(|x| x as &dyn IntoDocumentPath).collect(),
None,
)
.await
.map(|_| ())
}
async fn delete_member(&self, id: &MemberCompositeKey) -> Result<()> {
self.delete_one(
COL,
doc! {
"_id.server": &id.server,
"_id.user": &id.user
},
)
.await
.map(|_| ())
}
async fn fetch_all_members<'a>(&self, server: &str) -> Result<Vec<Member>> {
self.find(
COL,
doc! {
"_id.server": server
},
)
.await
}
async fn fetch_all_memberships<'a>(&self, user: &str) -> Result<Vec<Member>> {
self.find(
COL,
doc! {
"_id.user": user
},
)
.await
}
async fn fetch_members<'a>(&self, server: &str, ids: &'a [String]) -> Result<Vec<Member>> {
self.find(
COL,
doc! {
"_id.server": server,
"_id.user": {
"$in": ids
}
},
)
.await
}
async fn fetch_member_count(&self, server: &str) -> Result<usize> {
self.col::<Document>(COL)
.count_documents(
doc! {
"_id.server": server
},
None,
)
.await
.map(|c| c as usize)
.map_err(|_| Error::DatabaseError {
operation: "count_documents",
with: "server_members",
})
}
async fn fetch_server_count(&self, user: &str) -> Result<usize> {
self.col::<Document>(COL)
.count_documents(
doc! {
"_id.user": user
},
None,
)
.await
.map(|c| c as usize)
.map_err(|_| Error::DatabaseError {
operation: "count_documents",
with: "server_members",
})
}
}
impl IntoDocumentPath for FieldsMember {
fn as_path(&self) -> Option<&'static str> {
Some(match self {
FieldsMember::Avatar => "avatar",
FieldsMember::Nickname => "nickname",
FieldsMember::Roles => "roles",
})
}
}

View File

@@ -0,0 +1,68 @@
use crate::models::bot::{Bot, FieldsBot, PartialBot};
use crate::r#impl::mongo::IntoDocumentPath;
use crate::{AbstractBot, Result};
use super::super::MongoDb;
static COL: &str = "bots";
#[async_trait]
impl AbstractBot for MongoDb {
async fn fetch_bot(&self, id: &str) -> Result<Bot> {
self.find_one_by_id(COL, id).await
}
async fn fetch_bot_by_token(&self, token: &str) -> Result<Bot> {
self.find_one(
COL,
doc! {
"token": token
},
)
.await
}
async fn insert_bot(&self, bot: &Bot) -> Result<()> {
self.insert_one(COL, &bot).await.map(|_| ())
}
async fn update_bot(&self, id: &str, bot: &PartialBot, remove: Vec<FieldsBot>) -> Result<()> {
self.update_one_by_id(
COL,
id,
bot,
remove.iter().map(|x| x as &dyn IntoDocumentPath).collect(),
None,
)
.await
.map(|_| ())
}
async fn delete_bot(&self, id: &str) -> Result<()> {
self.delete_one_by_id(COL, id).await.map(|_| ())
}
async fn fetch_bots_by_user(&self, user_id: &str) -> Result<Vec<Bot>> {
self.find(
COL,
doc! {
"owner": user_id
},
)
.await
}
async fn get_number_of_bots_by_user(&self, user_id: &str) -> Result<usize> {
// ! FIXME: move this to generic?
self.fetch_bots_by_user(user_id).await.map(|x| x.len())
}
}
impl IntoDocumentPath for FieldsBot {
fn as_path(&self) -> Option<&'static str> {
match self {
FieldsBot::InteractionsURL => Some("interactions_url"),
FieldsBot::Token => None,
}
}
}

View File

@@ -0,0 +1,319 @@
use bson::Document;
use futures::StreamExt;
use mongodb::options::{Collation, CollationStrength, FindOneOptions, FindOptions};
use crate::models::user::{FieldsUser, PartialUser, RelationshipStatus, User};
use crate::r#impl::mongo::IntoDocumentPath;
use crate::{AbstractUser, Error, Result};
use super::super::MongoDb;
lazy_static! {
static ref FIND_USERNAME_OPTIONS: FindOneOptions = FindOneOptions::builder()
.collation(
Collation::builder()
.locale("en")
.strength(CollationStrength::Secondary)
.build()
)
.build();
}
static COL: &str = "users";
#[async_trait]
impl AbstractUser for MongoDb {
async fn fetch_user(&self, id: &str) -> Result<User> {
self.find_one_by_id(COL, id).await
}
async fn fetch_user_by_username(&self, username: &str) -> Result<User> {
self.find_one_with_options(
COL,
doc! {
"username": username
},
FIND_USERNAME_OPTIONS.clone(),
)
.await
}
async fn fetch_user_by_token(&self, token: &str) -> Result<User> {
let session = self
.col::<Document>("sessions")
.find_one(
doc! {
"token": token
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "find_one",
with: "sessions",
})?
.ok_or(Error::InvalidSession)?;
self.fetch_user(session.get_str("user_id").unwrap()).await
}
async fn insert_user(&self, user: &User) -> Result<()> {
self.insert_one(COL, user).await.map(|_| ())
}
async fn update_user(
&self,
id: &str,
user: &PartialUser,
remove: Vec<FieldsUser>,
) -> Result<()> {
self.update_one_by_id(
COL,
id,
user,
remove.iter().map(|x| x as &dyn IntoDocumentPath).collect(),
None,
)
.await
.map(|_| ())
}
async fn delete_user(&self, id: &str) -> Result<()> {
self.delete_one_by_id(COL, id).await.map(|_| ())
}
async fn fetch_users<'a>(&self, ids: &'a [String]) -> Result<Vec<User>> {
let mut cursor = self
.col::<User>(COL)
.find(
doc! {
"_id": {
"$in": ids
}
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "find",
with: "users",
})?;
let mut users = vec![];
while let Some(Ok(user)) = cursor.next().await {
users.push(user);
}
Ok(users)
}
async fn is_username_taken(&self, username: &str) -> Result<bool> {
// ! FIXME: move this up to generic
match self.fetch_user_by_username(username).await {
Ok(_) => Ok(true),
Err(Error::NotFound) => Ok(false),
Err(error) => Err(error),
}
}
async fn fetch_mutual_user_ids(&self, user_a: &str, user_b: &str) -> Result<Vec<String>> {
Ok(self
.col::<Document>(COL)
.find(
doc! {
"$and": [
{ "relations": { "$elemMatch": { "_id": &user_a, "status": "Friend" } } },
{ "relations": { "$elemMatch": { "_id": &user_b, "status": "Friend" } } }
]
},
FindOptions::builder().projection(doc! { "_id": 1 }).build(),
)
.await
.map_err(|_| Error::DatabaseError {
operation: "find",
with: "users",
})?
.filter_map(|s| async { s.ok() })
.collect::<Vec<Document>>()
.await
.into_iter()
.filter_map(|x| x.get_str("_id").ok().map(|x| x.to_string()))
.collect::<Vec<String>>())
}
async fn fetch_mutual_channel_ids(&self, user_a: &str, user_b: &str) -> Result<Vec<String>> {
Ok(self
.col::<Document>("channels")
.find(
doc! {
"channel_type": {
"$in": ["Group", "DirectMessage"]
},
"recipients": {
"$all": [ user_a, user_b ]
}
},
FindOptions::builder().projection(doc! { "_id": 1 }).build(),
)
.await
.map_err(|_| Error::DatabaseError {
operation: "find",
with: "channels",
})?
.filter_map(|s| async { s.ok() })
.collect::<Vec<Document>>()
.await
.into_iter()
.filter_map(|x| x.get_str("_id").ok().map(|x| x.to_string()))
.collect::<Vec<String>>())
}
async fn fetch_mutual_server_ids(&self, user_a: &str, user_b: &str) -> Result<Vec<String>> {
Ok(self
.col::<Document>("server_members")
.aggregate(
vec![
doc! {
"$match": {
"_id.user": user_a
}
},
doc! {
"$lookup": {
"from": "server_members",
"as": "members",
"let": {
"server": "$_id.server"
},
"pipeline": [
{
"$match": {
"$expr": {
"$and": [
{ "$eq": [ "$_id.user", user_b ] },
{ "$eq": [ "$_id.server", "$$server" ] }
]
}
}
}
]
}
},
doc! {
"$match": {
"members": {
"$size": 1_i32
}
}
},
doc! {
"$project": {
"_id": "$_id.server"
}
},
],
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "aggregate",
with: "server_members",
})?
.filter_map(|s| async { s.ok() })
.collect::<Vec<Document>>()
.await
.into_iter()
.filter_map(|x| x.get_str("_id").ok().map(|i| i.to_string()))
.collect::<Vec<String>>())
}
async fn set_relationship(
&self,
user_id: &str,
target_id: &str,
relationship: &RelationshipStatus,
) -> Result<()> {
if let RelationshipStatus::None = relationship {
return self.pull_relationship(user_id, target_id).await;
}
self.col::<Document>(COL)
.update_one(
doc! {
"_id": user_id
},
vec![doc! {
"$set": {
"relations": {
"$concatArrays": [
{
"$ifNull": [
{
"$filter": {
"input": "$relations",
"cond": {
"$ne": [
"$$this._id",
target_id
]
}
}
},
[]
]
},
[
{
"_id": target_id,
"status": format!("{:?}", relationship)
}
]
]
}
}
}],
None,
)
.await
.map(|_| ())
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "user",
})
}
async fn pull_relationship(&self, user_id: &str, target_id: &str) -> Result<()> {
self.col::<Document>(COL)
.update_one(
doc! {
"_id": user_id
},
doc! {
"$pull": {
"relations": {
"_id": target_id
}
}
},
None,
)
.await
.map(|_| ())
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "user",
})
}
}
impl IntoDocumentPath for FieldsUser {
fn as_path(&self) -> Option<&'static str> {
Some(match self {
FieldsUser::Avatar => "avatar",
FieldsUser::ProfileBackground => "profile.background",
FieldsUser::ProfileContent => "profile.content",
FieldsUser::StatusPresence => "status.presence",
FieldsUser::StatusText => "status.text",
})
}
}

View File

@@ -0,0 +1,62 @@
use bson::{to_bson, Document};
use mongodb::options::{FindOneOptions, UpdateOptions};
use crate::models::UserSettings;
use crate::{AbstractUserSettings, Error, Result};
use super::super::MongoDb;
static COL: &str = "user_settings";
#[async_trait]
impl AbstractUserSettings for MongoDb {
async fn fetch_user_settings(&'_ self, id: &str, filter: &'_ [String]) -> Result<UserSettings> {
let mut projection = doc! {
"_id": 0,
};
for key in filter {
projection.insert(key, 1);
}
self.find_one_with_options(
COL,
doc! {
"_id": id
},
FindOneOptions::builder().projection(projection).build(),
)
.await
}
async fn set_user_settings(&self, id: &str, settings: &UserSettings) -> Result<()> {
let mut set = doc! {};
for (key, data) in settings {
set.insert(
key,
vec![to_bson(&data.0).unwrap(), to_bson(&data.1).unwrap()],
);
}
self.col::<Document>(COL)
.update_one(
doc! {
"_id": id
},
doc! {
"$set": set
},
UpdateOptions::builder().upsert(true).build(),
)
.await
.map(|_| ())
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "user_settings",
})
}
async fn delete_user_settings(&self, id: &str) -> Result<()> {
self.delete_one_by_id(COL, id).await.map(|_| ())
}
}

72
crates/quark/src/lib.rs Normal file
View File

@@ -0,0 +1,72 @@
#[macro_use]
extern crate async_trait;
#[macro_use]
extern crate schemars;
#[macro_use]
extern crate async_recursion;
#[macro_use]
extern crate log;
#[macro_use]
extern crate impl_ops;
#[macro_use]
extern crate optional_struct;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate bitfield;
#[macro_use]
pub extern crate bson;
pub use iso8601_timestamp::Timestamp;
pub use redis_kiss;
pub mod events;
pub mod r#impl;
pub mod models;
pub mod presence;
pub mod tasks;
pub mod types;
mod database;
mod permissions;
mod traits;
mod util;
pub use database::*;
pub use traits::*;
pub use permissions::defn::*;
pub use permissions::{get_relationship, perms};
pub use util::r#ref::Ref;
pub use util::result::{EmptyResponse, Error, Result};
pub use util::variables;
#[cfg(feature = "rocket_impl")]
use rocket::State;
#[cfg(feature = "rocket_impl")]
pub type Db = State<Database>;
/// Configure logging and common Rust variables
pub fn setup_logging() -> sentry::ClientInitGuard {
dotenv::dotenv().ok();
if std::env::var("RUST_LOG").is_err() {
std::env::set_var("RUST_LOG", "info");
}
if std::env::var("ROCKET_ADDRESS").is_err() {
std::env::set_var("ROCKET_ADDRESS", "0.0.0.0");
}
pretty_env_logger::init();
sentry::init((
"https://62fd0e02c5354905b4e286757f4beb16@sentry.insert.moe/4",
sentry::ClientOptions {
release: sentry::release_name!(),
..Default::default()
},
))
}

View File

@@ -0,0 +1,11 @@
use serde::{Deserialize, Serialize};
/// Document representing migration information
#[derive(Serialize, Deserialize)]
pub struct MigrationInfo {
/// Unique Id
#[serde(rename = "_id")]
id: i32,
/// Current database revision
revision: i32,
}

View File

@@ -0,0 +1,8 @@
use serde::{Deserialize, Serialize};
/// Simple database model for testing
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct SimpleModel {
pub number: i32,
pub value: String,
}

View File

@@ -0,0 +1,61 @@
use serde::{Deserialize, Serialize};
/// Metadata associated with file
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
#[serde(tag = "type")]
pub enum Metadata {
/// File is just a generic uncategorised file
File,
/// File contains textual data and should be displayed as such
Text,
/// File is an image with specific dimensions
Image { width: isize, height: isize },
/// File is a video with specific dimensions
Video { width: isize, height: isize },
/// File is audio
Audio,
}
impl Default for Metadata {
fn default() -> Metadata {
Metadata::File
}
}
/// Representation of a File on Revolt
/// Generated by Autumn
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, Default)]
pub struct File {
/// Unique Id
#[serde(rename = "_id")]
pub id: String,
/// Tag / bucket this file was uploaded to
pub tag: String,
/// Original filename
pub filename: String,
/// Parsed metadata of this file
pub metadata: Metadata,
/// Raw content type of this file
pub content_type: String,
/// Size of this file (in bytes)
pub size: isize,
/// Whether this file was deleted
#[serde(skip_serializing_if = "Option::is_none")]
pub deleted: Option<bool>,
/// Whether this file was reported
#[serde(skip_serializing_if = "Option::is_none")]
pub reported: Option<bool>,
// ! THE FOLLOWING SHOULD BE DEPRECATED
#[serde(skip_serializing_if = "Option::is_none")]
pub message_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub user_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub server_id: Option<String>,
/// Id of the object this file is associated with
#[serde(skip_serializing_if = "Option::is_none")]
pub object_id: Option<String>,
}

View File

@@ -0,0 +1,169 @@
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use crate::{models::attachment::File, OverrideField};
/// Utility function to check if a boolean value is false
pub fn if_false(t: &bool) -> bool {
!t
}
/// Representation of a channel on Revolt
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
#[serde(tag = "channel_type")]
pub enum Channel {
/// Personal "Saved Notes" channel which allows users to save messages
SavedMessages {
/// Unique Id
#[serde(rename = "_id")]
id: String,
/// Id of the user this channel belongs to
user: String,
},
/// Direct message channel between two users
DirectMessage {
/// Unique Id
#[serde(rename = "_id")]
id: String,
/// Whether this direct message channel is currently open on both sides
active: bool,
/// 2-tuple of user ids participating in direct message
recipients: Vec<String>,
/// Id of the last message sent in this channel
#[serde(skip_serializing_if = "Option::is_none")]
last_message_id: Option<String>,
},
/// Group channel between 1 or more participants
Group {
/// Unique Id
#[serde(rename = "_id")]
id: String,
/// Display name of the channel
name: String,
/// User id of the owner of the group
owner: String,
/// Channel description
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<String>,
/// Array of user ids participating in channel
recipients: Vec<String>,
/// Custom icon attachment
#[serde(skip_serializing_if = "Option::is_none")]
icon: Option<File>,
/// Id of the last message sent in this channel
#[serde(skip_serializing_if = "Option::is_none")]
last_message_id: Option<String>,
/// Permissions assigned to members of this group
/// (does not apply to the owner of the group)
#[serde(skip_serializing_if = "Option::is_none")]
permissions: Option<i64>,
/// Whether this group is marked as not safe for work
#[serde(skip_serializing_if = "if_false", default)]
nsfw: bool,
},
/// Text channel belonging to a server
TextChannel {
/// Unique Id
#[serde(rename = "_id")]
id: String,
/// Id of the server this channel belongs to
server: String,
/// Display name of the channel
name: String,
/// Channel description
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<String>,
/// Custom icon attachment
#[serde(skip_serializing_if = "Option::is_none")]
icon: Option<File>,
/// Id of the last message sent in this channel
#[serde(skip_serializing_if = "Option::is_none")]
last_message_id: Option<String>,
/// Default permissions assigned to users in this channel
#[serde(skip_serializing_if = "Option::is_none")]
default_permissions: Option<OverrideField>,
/// Permissions assigned based on role to this channel
#[serde(
default = "HashMap::<String, OverrideField>::new",
skip_serializing_if = "HashMap::<String, OverrideField>::is_empty"
)]
role_permissions: HashMap<String, OverrideField>,
/// Whether this channel is marked as not safe for work
#[serde(skip_serializing_if = "if_false", default)]
nsfw: bool,
},
/// Voice channel belonging to a server
VoiceChannel {
/// Unique Id
#[serde(rename = "_id")]
id: String,
/// Id of the server this channel belongs to
server: String,
/// Display name of the channel
name: String,
#[serde(skip_serializing_if = "Option::is_none")]
/// Channel description
description: Option<String>,
/// Custom icon attachment
#[serde(skip_serializing_if = "Option::is_none")]
icon: Option<File>,
/// Default permissions assigned to users in this channel
#[serde(skip_serializing_if = "Option::is_none")]
default_permissions: Option<OverrideField>,
/// Permissions assigned based on role to this channel
#[serde(
default = "HashMap::<String, OverrideField>::new",
skip_serializing_if = "HashMap::<String, OverrideField>::is_empty"
)]
role_permissions: HashMap<String, OverrideField>,
/// Whether this channel is marked as not safe for work
#[serde(skip_serializing_if = "if_false", default)]
nsfw: bool,
},
}
/// Partial values of [Channel]
#[derive(Serialize, Deserialize, JsonSchema, Debug, Default, Clone)]
pub struct PartialChannel {
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub owner: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub icon: Option<File>,
#[serde(skip_serializing_if = "Option::is_none")]
pub nsfw: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub active: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub permissions: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub role_permissions: Option<HashMap<String, OverrideField>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub default_permissions: Option<OverrideField>,
#[serde(skip_serializing_if = "Option::is_none")]
pub last_message_id: Option<String>,
}
/// Optional fields on channel object
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Clone)]
pub enum FieldsChannel {
Description,
Icon,
DefaultPermissions,
}

View File

@@ -0,0 +1,32 @@
use serde::{Deserialize, Serialize};
/// Representation of an invite to a channel on Revolt
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
#[serde(tag = "type")]
pub enum Invite {
/// Invite to a specific server channel
Server {
/// Invite code
#[serde(rename = "_id")]
code: String,
/// Id of the server this invite points to
server: String,
/// Id of user who created this invite
creator: String,
/// Id of the server channel this invite points to
channel: String,
},
/// Invite to a group channel
Group {
/// Invite code
#[serde(rename = "_id")]
code: String,
/// Id of user who created this invite
creator: String,
/// Id of the group channel this invite points to
channel: String,
}, /* User {
code: String,
user: String
} */
}

View File

@@ -0,0 +1,25 @@
use serde::{Deserialize, Serialize};
/// Composite primary key consisting of channel and user id
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
pub struct ChannelCompositeKey {
/// Channel Id
pub channel: String,
/// User Id
pub user: String,
}
/// Representation of the state of a channel from the perspective of a user
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
pub struct ChannelUnread {
/// Composite key pointing to a user's view of a channel
#[serde(rename = "_id")]
pub id: ChannelCompositeKey,
/// Id of the last message read in this channel by a user
#[serde(skip_serializing_if = "Option::is_none")]
pub last_id: Option<String>,
/// Array of message ids that mention the user
#[serde(skip_serializing_if = "Option::is_none")]
pub mentions: Option<Vec<String>>,
}

View File

@@ -0,0 +1,170 @@
use serde::{Deserialize, Serialize};
use validator::Validate;
use iso8601_timestamp::Timestamp;
#[cfg(feature = "rocket_impl")]
use rocket::FromFormField;
use crate::{
models::{attachment::File, Member, User},
types::january::Embed,
};
/// # Reply
///
/// Representation of a message reply before it is sent.
#[derive(Serialize, Deserialize, JsonSchema, Clone, Debug)]
pub struct Reply {
/// Message Id
pub id: String,
/// Whether this reply should mention the message's author
pub mention: bool,
}
/// Representation of a text embed before it is sent.
#[derive(Validate, Serialize, Deserialize, JsonSchema, Clone, Debug)]
pub struct SendableEmbed {
#[validate(length(min = 1, max = 128))]
pub icon_url: Option<String>,
pub url: Option<String>,
#[validate(length(min = 1, max = 100))]
pub title: Option<String>,
#[validate(length(min = 1, max = 2000))]
pub description: Option<String>,
pub media: Option<String>,
#[validate(length(min = 1, max = 64))]
pub colour: Option<String>,
}
/// Representation of a system event message
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
#[serde(tag = "type")]
pub enum SystemMessage {
#[serde(rename = "text")]
Text { content: String },
#[serde(rename = "user_added")]
UserAdded { id: String, by: String },
#[serde(rename = "user_remove")]
UserRemove { id: String, by: String },
#[serde(rename = "user_joined")]
UserJoined { id: String },
#[serde(rename = "user_left")]
UserLeft { id: String },
#[serde(rename = "user_kicked")]
UserKicked { id: String },
#[serde(rename = "user_banned")]
UserBanned { id: String },
#[serde(rename = "channel_renamed")]
ChannelRenamed { name: String, by: String },
#[serde(rename = "channel_description_changed")]
ChannelDescriptionChanged { by: String },
#[serde(rename = "channel_icon_changed")]
ChannelIconChanged { by: String },
}
/// Name and / or avatar override information
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, Validate)]
pub struct Masquerade {
/// Replace the display name shown on this message
#[serde(skip_serializing_if = "Option::is_none")]
#[validate(length(min = 1, max = 32))]
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))]
pub avatar: Option<String>,
}
/// Representation of a Message on Revolt
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, OptionalStruct, Default)]
#[optional_derive(Serialize, Deserialize, JsonSchema, Debug, Default, Clone)]
#[optional_name = "PartialMessage"]
#[opt_skip_serializing_none]
#[opt_some_priority]
pub struct Message {
/// Unique Id
#[serde(rename = "_id")]
pub id: String,
/// Unique value generated by client sending this message
#[serde(skip_serializing_if = "Option::is_none")]
pub nonce: Option<String>,
/// Id of the channel this message was sent in
pub channel: String,
/// Id of the user that sent this message
pub author: String,
/// Message content
#[serde(skip_serializing_if = "Option::is_none")]
pub content: Option<String>,
/// System message
#[serde(skip_serializing_if = "Option::is_none")]
pub system: Option<SystemMessage>,
/// Array of attachments
#[serde(skip_serializing_if = "Option::is_none")]
pub attachments: Option<Vec<File>>,
/// Time at which this message was last edited
#[serde(skip_serializing_if = "Option::is_none")]
pub edited: Option<Timestamp>,
/// Attached embeds to this message
#[serde(skip_serializing_if = "Option::is_none")]
pub embeds: Option<Vec<Embed>>,
/// Array of user ids mentioned in this message
#[serde(skip_serializing_if = "Option::is_none")]
pub mentions: Option<Vec<String>>,
/// Array of message ids this message is replying to
#[serde(skip_serializing_if = "Option::is_none")]
pub replies: Option<Vec<String>>,
/// Name and / or avatar overrides for this message
#[serde(skip_serializing_if = "Option::is_none")]
pub masquerade: Option<Masquerade>,
}
/// # Message Sort
///
/// Sort used for retrieving messages
#[derive(Serialize, Deserialize, JsonSchema)]
#[cfg_attr(feature = "rocket_impl", derive(FromFormField))]
pub enum MessageSort {
/// Sort by the most relevant messages
Relevance,
/// Sort by the newest messages first
Latest,
/// Sort by the oldest messages first
Oldest,
}
impl Default for MessageSort {
fn default() -> MessageSort {
MessageSort::Relevance
}
}
/// # Bulk Message Response
///
/// Response used when multiple messages are fetched
#[derive(Serialize, JsonSchema)]
#[serde(untagged)]
pub enum BulkMessageResponse {
JustMessages(
/// List of messages
Vec<Message>,
),
MessagesAndUsers {
/// List of messages
messages: Vec<Message>,
/// List of users
users: Vec<User>,
/// List of members
#[serde(skip_serializing_if = "Option::is_none")]
members: Option<Vec<Member>>,
},
}
/// # Appended Information
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct AppendMessage {
/// Additional embeds to include in this message
#[serde(skip_serializing_if = "Option::is_none")]
pub embeds: Option<Vec<Embed>>,
}

View File

@@ -0,0 +1,47 @@
mod admin {
pub mod migrations;
pub mod simple;
}
mod autumn {
pub mod attachment;
}
mod channels {
pub mod channel;
pub mod channel_invite;
pub mod channel_unread;
pub mod message;
}
mod servers {
pub mod server;
pub mod server_ban;
pub mod server_member;
}
mod users {
pub mod bot;
pub mod user;
pub mod user_settings;
}
pub use admin::*;
pub use autumn::*;
pub use channels::*;
pub use servers::*;
pub use users::*;
pub use attachment::File;
pub use bot::Bot;
pub use channel::Channel;
pub use channel_invite::Invite;
pub use channel_unread::ChannelUnread;
pub use message::Message;
pub use migrations::MigrationInfo;
pub use server::Server;
pub use server_ban::ServerBan;
pub use server_member::Member;
pub use simple::SimpleModel;
pub use user::User;
pub use user_settings::UserSettings;

View File

@@ -0,0 +1,150 @@
use std::collections::HashMap;
use num_enum::TryFromPrimitive;
use serde::{Deserialize, Serialize};
use validator::Validate;
use crate::{models::attachment::File, OverrideField};
/// Utility function to check if a boolean value is false
pub fn if_false(t: &bool) -> bool {
!t
}
/// Representation of a server role
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, OptionalStruct, Default)]
#[optional_derive(Serialize, Deserialize, JsonSchema, Debug, Clone, Default)]
#[optional_name = "PartialRole"]
#[opt_skip_serializing_none]
#[opt_some_priority]
pub struct Role {
/// Role name
pub name: String,
/// Permissions available to this role
pub permissions: OverrideField,
/// Colour used for this role
///
/// This can be any valid CSS colour
#[serde(skip_serializing_if = "Option::is_none")]
pub colour: Option<String>,
/// Whether this role should be shown separately on the member sidebar
#[serde(skip_serializing_if = "if_false", default)]
pub hoist: bool,
/// Ranking of this role
#[serde(default)]
pub rank: i64,
}
/// Channel category
#[derive(Validate, Serialize, Deserialize, JsonSchema, Debug, Clone)]
pub struct Category {
/// Unique ID for this category
#[validate(length(min = 1, max = 32))]
pub id: String,
/// Title for this category
#[validate(length(min = 1, max = 32))]
pub title: String,
/// Channels in this category
pub channels: Vec<String>,
}
/// System message channel assignments
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
pub struct SystemMessageChannels {
/// ID of channel to send user join messages in
#[serde(skip_serializing_if = "Option::is_none")]
pub user_joined: Option<String>,
/// ID of channel to send user left messages in
#[serde(skip_serializing_if = "Option::is_none")]
pub user_left: Option<String>,
/// ID of channel to send user kicked messages in
#[serde(skip_serializing_if = "Option::is_none")]
pub user_kicked: Option<String>,
/// ID of channel to send user banned messages in
#[serde(skip_serializing_if = "Option::is_none")]
pub user_banned: Option<String>,
}
/// Server flag enum
#[derive(Debug, PartialEq, Eq, TryFromPrimitive, Copy, Clone)]
#[repr(i32)]
pub enum ServerFlags {
Verified = 1,
Official = 2,
}
/// Representation of a server on Revolt
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, OptionalStruct, Default)]
#[optional_derive(Serialize, Deserialize, JsonSchema, Debug, Default, Clone)]
#[optional_name = "PartialServer"]
#[opt_skip_serializing_none]
#[opt_some_priority]
pub struct Server {
/// Unique Id
#[serde(rename = "_id")]
pub id: String,
/// User id of the owner
pub owner: String,
/// Name of the server
pub name: String,
/// Description for the server
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
/// Channels within this server
// ! FIXME: this may be redundant
pub channels: Vec<String>,
/// Categories for this server
#[serde(skip_serializing_if = "Option::is_none")]
pub categories: Option<Vec<Category>>,
/// Configuration for sending system event messages
#[serde(skip_serializing_if = "Option::is_none")]
pub system_messages: Option<SystemMessageChannels>,
/// Roles for this server
#[serde(
default = "HashMap::<String, Role>::new",
skip_serializing_if = "HashMap::<String, Role>::is_empty"
)]
pub roles: HashMap<String, Role>,
/// Default set of server and channel permissions
pub default_permissions: i64,
/// Icon attachment
#[serde(skip_serializing_if = "Option::is_none")]
pub icon: Option<File>,
/// Banner attachment
#[serde(skip_serializing_if = "Option::is_none")]
pub banner: Option<File>,
/// Enum of server flags
#[serde(skip_serializing_if = "Option::is_none")]
pub flags: Option<i32>,
/// Whether this server is flagged as not safe for work
#[serde(skip_serializing_if = "if_false", default)]
pub nsfw: bool,
/// Whether to enable analytics
#[serde(skip_serializing_if = "if_false", default)]
pub analytics: bool,
/// Whether this server should be publicly discoverable
#[serde(skip_serializing_if = "if_false", default)]
pub discoverable: bool,
}
/// Optional fields on server object
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Clone)]
pub enum FieldsServer {
Description,
Categories,
SystemMessages,
Icon,
Banner,
}
/// Optional fields on server object
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Clone)]
pub enum FieldsRole {
Colour,
}

View File

@@ -0,0 +1,13 @@
use serde::{Deserialize, Serialize};
use super::server_member::MemberCompositeKey;
/// Representation of a server ban on Revolt
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
pub struct ServerBan {
/// Unique member id
#[serde(rename = "_id")]
pub id: MemberCompositeKey,
/// Reason for ban creation
pub reason: Option<String>,
}

View File

@@ -0,0 +1,50 @@
use serde::{Deserialize, Serialize};
use crate::models::attachment::File;
/// Composite primary key consisting of server and user id
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, Default)]
pub struct MemberCompositeKey {
/// Server Id
pub server: String,
/// User Id
pub user: String,
}
/// Representation of a member of a server on Revolt
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, OptionalStruct, Default)]
#[optional_derive(Serialize, Deserialize, JsonSchema, Debug, Default, Clone)]
#[optional_name = "PartialMember"]
#[opt_skip_serializing_none]
#[opt_some_priority]
pub struct Member {
/// Unique member id
#[serde(rename = "_id")]
pub id: MemberCompositeKey,
/// Member's nickname
#[serde(skip_serializing_if = "Option::is_none")]
pub nickname: Option<String>,
/// Avatar attachment
#[serde(skip_serializing_if = "Option::is_none")]
pub avatar: Option<File>,
/// Member's roles
#[serde(skip_serializing_if = "Option::is_none")]
pub roles: Option<Vec<String>>,
}
/// Optional fields on server member object
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Clone)]
pub enum FieldsMember {
Nickname,
Avatar,
Roles,
}
/// Member removal intention
pub enum RemovalIntention {
Leave,
Kick,
Ban,
}

View File

@@ -0,0 +1,63 @@
use num_enum::TryFromPrimitive;
use serde::{Deserialize, Serialize};
/// Utility function to check if a boolean value is false
pub fn if_false(t: &bool) -> bool {
!t
}
/// Bot flag enum
#[derive(Debug, PartialEq, Eq, TryFromPrimitive, Copy, Clone)]
#[repr(i32)]
pub enum BotFlags {
Verified = 1,
Official = 2,
}
/// Representation of a bot on Revolt
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, OptionalStruct, Default)]
#[optional_derive(Serialize, Deserialize, JsonSchema, Debug, Default, Clone)]
#[optional_name = "PartialBot"]
#[opt_skip_serializing_none]
#[opt_some_priority]
pub struct Bot {
/// Bot Id
///
/// This equals the associated bot user's id.
#[serde(rename = "_id")]
pub id: String,
/// User Id of the bot owner
pub owner: String,
/// Token used to authenticate requests for this bot
pub token: String,
/// Whether the bot is public
/// (may be invited by anyone)
pub public: bool,
/// Whether to enable analytics
#[serde(skip_serializing_if = "if_false", default)]
pub analytics: bool,
/// Whether this bot should be publicly discoverable
#[serde(skip_serializing_if = "if_false", default)]
pub discoverable: bool,
/// Reserved; URL for handling interactions
#[serde(skip_serializing_if = "Option::is_none")]
pub interactions_url: Option<String>,
/// URL for terms of service
#[serde(skip_serializing_if = "Option::is_none")]
pub terms_of_service_url: Option<String>,
/// URL for privacy policy
#[serde(skip_serializing_if = "Option::is_none")]
pub privacy_policy_url: Option<String>,
/// Enum of bot flags
#[serde(skip_serializing_if = "Option::is_none")]
pub flags: Option<i32>,
}
/// Optional fields on bot object
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq)]
pub enum FieldsBot {
Token,
InteractionsURL,
}

View File

@@ -0,0 +1,177 @@
use num_enum::TryFromPrimitive;
use serde::{Deserialize, Serialize};
use validator::Validate;
use crate::models::attachment::File;
/// Utility function to check if a boolean value is false
pub fn if_false(t: &bool) -> bool {
!t
}
/// User's relationship with another user (or themselves)
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, PartialEq)]
pub enum RelationshipStatus {
None,
User,
Friend,
Outgoing,
Incoming,
Blocked,
BlockedOther,
}
/// Relationship entry indicating current status with other user
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
pub struct Relationship {
#[serde(rename = "_id")]
pub id: String,
pub status: RelationshipStatus,
}
/// Presence status
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, PartialEq)]
pub enum Presence {
Online,
Idle,
Busy,
Invisible,
}
/// User's active status
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, Validate, Default)]
pub struct UserStatus {
/// Custom status text
#[validate(length(min = 1, max = 128))]
#[serde(skip_serializing_if = "Option::is_none")]
pub text: Option<String>,
/// Current presence option
#[serde(skip_serializing_if = "Option::is_none")]
pub presence: Option<Presence>,
}
/// User's profile
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, Default)]
pub struct UserProfile {
/// Text content on user's profile
#[serde(skip_serializing_if = "Option::is_none")]
pub content: Option<String>,
/// Background visible on user's profile
#[serde(skip_serializing_if = "Option::is_none")]
pub background: Option<File>,
}
/// User badge bitfield
#[derive(Debug, PartialEq, Eq, TryFromPrimitive, Copy, Clone)]
#[repr(i32)]
pub enum Badges {
/// Revolt Developer
Developer = 1,
/// Helped translate Revolt
Translator = 2,
/// Monetarily supported Revolt
Supporter = 4,
/// Responsibly disclosed a security issue
ResponsibleDisclosure = 8,
/// Revolt Founder
Founder = 16,
/// Platform moderator
PlatformModeration = 32,
/// Active monetary supporter
ActiveSupporter = 64,
/// 🦊🦝
Paw = 128,
/// Joined as one of the first 1000 users in 2021
EarlyAdopter = 256,
/// Amogus
ReservedRelevantJokeBadge1 = 512,
/// Low resolution troll face
ReservedRelevantJokeBadge2 = 1024,
}
/// User flag enum
#[derive(Debug, PartialEq, Eq, TryFromPrimitive, Copy, Clone)]
#[repr(i32)]
pub enum Flags {
/// User has been suspended from the platform
Suspended = 1,
/// User has deleted their account
Deleted = 2,
/// User was banned off the platform
Banned = 4,
}
/// Bot information for if the user is a bot
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
pub struct BotInformation {
/// Id of the owner of this bot
pub owner: String,
}
/// Representiation of a User on Revolt.
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, OptionalStruct, Default)]
#[optional_derive(Serialize, Deserialize, Debug, Default, Clone)]
#[optional_name = "PartialUser"]
#[opt_skip_serializing_none]
#[opt_some_priority]
pub struct User {
/// Unique Id
#[serde(rename = "_id")]
pub id: String,
/// Username
pub username: String,
#[serde(skip_serializing_if = "Option::is_none")]
/// Avatar attachment
pub avatar: Option<File>,
/// Relationships with other users
#[serde(skip_serializing_if = "Option::is_none")]
pub relations: Option<Vec<Relationship>>,
/// Bitfield of user badges
#[serde(skip_serializing_if = "Option::is_none")]
pub badges: Option<i32>,
/// User's current status
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<UserStatus>,
/// User's profile page
#[serde(skip_serializing_if = "Option::is_none")]
pub profile: Option<UserProfile>,
/// Enum of user flags
#[serde(skip_serializing_if = "Option::is_none")]
pub flags: Option<i32>,
/// Whether this user is privileged
#[serde(skip_serializing_if = "if_false", default)]
pub privileged: bool,
/// Bot information
#[serde(skip_serializing_if = "Option::is_none")]
pub bot: Option<BotInformation>,
// ? Entries below should never be pushed to the database
/// Current session user's relationship with this user
#[serde(skip_serializing_if = "Option::is_none")]
pub relationship: Option<RelationshipStatus>,
/// Whether this user is currently online
#[serde(skip_serializing_if = "Option::is_none")]
pub online: Option<bool>,
}
/// Optional fields on user object
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Clone)]
pub enum FieldsUser {
Avatar,
StatusText,
StatusPresence,
ProfileContent,
ProfileBackground,
}
/// Enumeration providing a hint to the type of user we are handling
pub enum UserHint {
/// Could be either a user or a bot
Any,
/// Only match bots
Bot,
/// Only match users
User,
}

View File

@@ -0,0 +1,6 @@
use std::collections::HashMap;
/// HashMap of user settings
/// Each key is mapped to a tuple consisting of the
/// revision timestamp and serialised data (in JSON format)
pub type UserSettings = HashMap<String, (i64, String)>;

View File

@@ -0,0 +1,93 @@
mod permission;
mod user;
use bson::Bson;
pub use permission::*;
pub use user::*;
use serde::{Deserialize, Serialize};
/// Holds a permission value to manipulate.
#[derive(Debug)]
pub struct PermissionValue(u64);
/// Representation of a single permission override
#[derive(Deserialize, JsonSchema, Debug, Clone, Copy)]
pub struct Override {
/// Allow bit flags
allow: u64,
/// Disallow bit flags
deny: u64,
}
/// Representation of a single permission override
/// as it appears on models and in the database
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, Copy, Default)]
pub struct OverrideField {
/// Allow bit flags
a: i64,
/// Disallow bit flags
d: i64,
}
impl Override {
/// Into allows
pub fn allows(&self) -> u64 {
self.allow
}
/// Into denies
pub fn denies(&self) -> u64 {
self.deny
}
}
impl PermissionValue {
/// Apply a given override to this value
pub fn apply(&mut self, v: Override) {
self.0 |= v.allow;
self.0 &= !v.deny;
}
}
impl From<Override> for OverrideField {
fn from(v: Override) -> Self {
Self {
a: v.allow as i64,
d: v.deny as i64,
}
}
}
impl From<OverrideField> for Override {
fn from(v: OverrideField) -> Self {
Self {
allow: v.a as u64,
deny: v.d as u64,
}
}
}
impl From<OverrideField> for Bson {
fn from(v: OverrideField) -> Self {
Self::Document(bson::to_document(&v).unwrap())
}
}
impl From<i64> for PermissionValue {
fn from(v: i64) -> Self {
Self(v as u64)
}
}
impl From<u64> for PermissionValue {
fn from(v: u64) -> Self {
Self(v as u64)
}
}
impl From<PermissionValue> for u64 {
fn from(v: PermissionValue) -> Self {
v.0
}
}

View File

@@ -0,0 +1,156 @@
use num_enum::TryFromPrimitive;
use serde::{Deserialize, Serialize};
use std::ops;
/// Permission value on Revolt
///
/// This should be restricted to the lower 52 bits to prevent any
/// potential issues with Javascript. Also leave empty spaces for
/// future permission flags to be added.
#[derive(
Serialize, Deserialize, Debug, PartialEq, Eq, TryFromPrimitive, Copy, Clone, JsonSchema,
)]
#[repr(u64)]
pub enum Permission {
// * Generic permissions
/// Manage the channel or channels on the server
ManageChannel = 1 << 0,
/// Manage the server
ManageServer = 1 << 1,
/// Manage permissions on servers or channels
ManagePermissions = 1 << 2,
/// Manage roles on server
ManageRole = 1 << 3,
// % 2 bits reserved
// * Member permissions
/// Kick other members below their ranking
KickMembers = 1 << 6,
/// Ban other members below their ranking
BanMembers = 1 << 7,
/// Timeout other members below their ranking
TimeoutMembers = 1 << 8,
/// Assign roles to members below their ranking
AssignRoles = 1 << 9,
/// Change own nickname
ChangeNickname = 1 << 10,
/// Change or remove other's nicknames below their ranking
ManageNicknames = 1 << 11,
/// Change own avatar
ChangeAvatar = 1 << 12,
/// Remove other's avatars below their ranking
RemoveAvatars = 1 << 13,
// % 7 bits reserved
// * Channel permissions
/// View a channel
ViewChannel = 1 << 20,
/// Read a channel's past message history
ReadMessageHistory = 1 << 21,
/// Send a message in a channel
SendMessage = 1 << 22,
/// Delete messages in a channel
ManageMessages = 1 << 23,
/// Manage webhook entries on a channel
ManageWebhooks = 1 << 24,
/// Create invites to this channel
InviteOthers = 1 << 25,
/// Send embedded content in this channel
SendEmbeds = 1 << 26,
/// Send attachments and media in this channel
UploadFiles = 1 << 27,
/// Masquerade messages using custom nickname and avatar
Masquerade = 1 << 28,
// % 1 bits reserved
// * Voice permissions
/// Connect to a voice channel
Connect = 1 << 30,
/// Speak in a voice call
Speak = 1 << 31,
/// Share video in a voice call
Video = 1 << 32,
/// Mute other members with lower ranking in a voice call
MuteMembers = 1 << 33,
/// Deafen other members with lower ranking in a voice call
DeafenMembers = 1 << 34,
/// Move members between voice channels
MoveMembers = 1 << 35,
// * Misc. permissions
// % Bits 36 to 52: free area
// % Bits 53 to 64: do not use
// * Grant all permissions
/// Safely grant all permissions
GrantAllSafe = 0x000F_FFFF_FFFF_FFFF,
/// Grant all permissions
GrantAll = u64::MAX,
}
impl_op_ex!(+ |a: &Permission, b: &Permission| -> u64 { *a as u64 | *b as u64 });
impl_op_ex_commutative!(+ |a: &u64, b: &Permission| -> u64 { *a | *b as u64 });
lazy_static! {
pub static ref DEFAULT_PERMISSION_VIEW_ONLY: u64 =
Permission::ViewChannel + Permission::ReadMessageHistory;
pub static ref DEFAULT_PERMISSION: u64 = *DEFAULT_PERMISSION_VIEW_ONLY
+ Permission::SendMessage
+ Permission::InviteOthers
+ Permission::SendEmbeds
+ Permission::UploadFiles
+ 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_SERVER: u64 =
*DEFAULT_PERMISSION + Permission::ChangeNickname + Permission::ChangeAvatar;
}
bitfield! {
#[derive(Default)]
pub struct Permissions(MSB0 [u64]);
u64;
// * Server permissions
pub can_manage_channel, _: 63;
pub can_manage_server, _: 62;
pub can_manage_permissions, _: 61;
pub can_manage_roles, _: 60;
// * Member permissions
pub can_kick_members, _: 57;
pub can_ban_members, _: 56;
pub can_timeout_members, _: 55;
pub can_assign_roles, _: 54;
pub can_change_nickname, _: 53;
pub can_manage_nicknames, _: 52;
pub can_change_avatar, _: 51;
pub can_remove_avatars, _: 50;
// * Channel permissions
pub can_view_channel, _: 42;
pub can_read_message_history, _: 41;
pub can_send_message, _: 40;
pub can_manage_messages, _: 39;
pub can_manage_webhooks, _: 38;
pub can_invite_others, _: 37;
pub can_send_embeds, _: 36;
pub can_upload_files, _: 35;
pub can_masquerade, _: 34;
// * Voice permissions
pub can_connect, _: 32;
pub can_speak, _: 31;
pub can_share_video, _: 30;
pub can_mute_members, _: 29;
pub can_deafen_members, _: 28;
pub can_move_members, _: 27;
}
pub type Perms = Permissions<[u64; 1]>;

View File

@@ -0,0 +1,29 @@
use num_enum::TryFromPrimitive;
use serde::{Deserialize, Serialize};
use std::ops;
/// User permission definitions
#[derive(
Serialize, Deserialize, Debug, PartialEq, Eq, TryFromPrimitive, Copy, Clone, JsonSchema,
)]
#[repr(u32)]
pub enum UserPermission {
Access = 1 << 0,
ViewProfile = 1 << 1,
SendMessage = 1 << 2,
Invite = 1 << 3,
}
impl_op_ex!(+ |a: &UserPermission, b: &UserPermission| -> u32 { *a as u32 | *b as u32 });
impl_op_ex_commutative!(+ |a: &u32, b: &UserPermission| -> u32 { *a | *b as u32 });
bitfield! {
pub struct UserPermissions(MSB0 [u32]);
u32;
pub get_access, _: 31;
pub get_view_profile, _: 30;
pub get_send_message, _: 29;
pub get_invite, _: 28;
}
pub type UserPerms = UserPermissions<[u32; 1]>;

View File

@@ -0,0 +1,2 @@
pub mod permission;
pub mod user;

View File

@@ -0,0 +1,219 @@
use std::collections::HashSet;
use crate::{
models::Channel, permissions::PermissionCalculator, Override, Permission, PermissionValue,
Permissions, Perms, Result, DEFAULT_PERMISSION_DIRECT_MESSAGE,
DEFAULT_PERMISSION_SAVED_MESSAGES, DEFAULT_PERMISSION_VIEW_ONLY,
};
use super::super::Permission::GrantAllSafe;
impl PermissionCalculator<'_> {
/// Calculate the permissions from our perspective to the given server or channel
///
/// Refer to https://developers.revolt.chat/stack/delta/permissions#flow-chart for more information
pub async fn calc(&mut self, db: &crate::Database) -> Result<Perms> {
if self.perspective.privileged {
return Ok(Permissions([GrantAllSafe as u64]));
}
let value = if self.channel.has() {
calculate_channel_permission(self, db).await?
} else if self.server.has() {
calculate_server_permission(self, db).await?
} else {
panic!("Expected `PermissionCalculator.(user|server) to exist.");
}
.into();
self.cached_permission = Some(value);
Ok(Permissions([value]))
}
}
/// Internal helper function for calculating server permission
async fn calculate_server_permission(
data: &mut PermissionCalculator<'_>,
db: &crate::Database,
) -> Result<PermissionValue> {
let server = data.server.get().unwrap();
// 1. Check if owner.
if data.perspective.id == server.owner {
return Ok((Permission::GrantAllSafe as u64).into());
}
// 2. Fetch member.
if !data.member.has() {
data.member
.set(db.fetch_member(&server.id, &data.perspective.id).await?);
}
let member = data.member.get().expect("Member should be present by now.");
// 3. Apply allows from default_permissions.
let mut permissions: PermissionValue = server.default_permissions.into();
// 4. Resolve each role in order.
let member_roles: HashSet<&String> = if let Some(roles) = member.roles.as_ref() {
roles.iter().collect()
} else {
HashSet::new()
};
if !member_roles.is_empty() {
let mut roles = server
.roles
.iter()
.filter(|(id, _)| member_roles.contains(id))
.map(|(_, role)| {
let v: Override = role.permissions.into();
(role.rank, v)
})
.collect::<Vec<(i64, Override)>>();
roles.sort_by(|a, b| b.0.cmp(&a.0));
// 5. Apply allows and denies from roles.
for (_, v) in roles {
permissions.apply(v);
}
}
Ok(permissions)
}
/// Internal helper function for calculating channel permission
async fn calculate_channel_permission(
data: &mut PermissionCalculator<'_>,
db: &crate::Database,
) -> Result<PermissionValue> {
// Pre-calculate server permissions if applicable.
// We do this to satisfy the borrow checker.
let server_id = match data.channel.get().unwrap() {
Channel::TextChannel { server, .. } | Channel::VoiceChannel { server, .. } => Some(server),
_ => None,
};
let mut permissions = if let Some(server) = server_id {
if !data.server.has() {
data.server.set(db.fetch_server(server).await?);
}
calculate_server_permission(data, db).await?
} else {
0_u64.into()
};
// Borrow the channel now and continue as normal.
let channel = data.channel.get().unwrap();
// 1. Check channel type.
let value: PermissionValue = match channel {
Channel::SavedMessages { .. } => (*DEFAULT_PERMISSION_SAVED_MESSAGES).into(),
Channel::DirectMessage { recipients, .. } => {
// 2. Fetch user.
let other_user = recipients
.iter()
.find(|x| x != &&data.perspective.id)
.unwrap();
let user = db.fetch_user(other_user).await?;
data.user.set(user);
// 3. Calculate user permissions.
let perms = data.calc_user(db).await;
// 4. Check if the user can send messages.
if perms.get_send_message() {
(*DEFAULT_PERMISSION_DIRECT_MESSAGE).into()
} else {
(*DEFAULT_PERMISSION_VIEW_ONLY).into()
}
}
Channel::Group {
owner,
permissions,
recipients,
..
} => {
// 2. Check if user is owner.
if &data.perspective.id == owner {
(Permission::GrantAllSafe as u64).into()
} else {
// 3. Check that we are actually in the group.
if recipients.contains(&data.perspective.id) {
// 4. Pull out group permissions.
permissions
.map(|x| x as u64)
.unwrap_or(*DEFAULT_PERMISSION_DIRECT_MESSAGE)
.into()
} else {
0_u64.into()
}
}
}
Channel::TextChannel {
default_permissions,
role_permissions,
..
}
| Channel::VoiceChannel {
default_permissions,
role_permissions,
..
} => {
// 2. If server owner, just grant all permissions.
//
// Member may be present and we need to check or
// we can just grant all if member is not present.
//
// In the case member isn't present, the previous
// step did not fetch member as we are the server owner.
if let Some(member) = data.member.get() {
let server = data.server.get().unwrap();
if server.owner == member.id.user {
return Ok((Permission::GrantAllSafe as u64).into());
}
// 3. Apply default allows and denies for channel.
if let Some(default) = default_permissions {
permissions.apply((*default).into());
}
// 4. Resolve each role in order.
let member_roles: HashSet<&String> = if let Some(roles) = member.roles.as_ref() {
roles.iter().collect()
} else {
HashSet::new()
};
if !member_roles.is_empty() {
let mut roles = role_permissions
.iter()
.filter(|(id, _)| member_roles.contains(id))
.filter_map(|(id, permission)| {
server.roles.get(id).map(|role| {
let v: Override = (*permission).into();
(role.rank, v)
})
})
.collect::<Vec<(i64, Override)>>();
roles.sort_by(|a, b| b.0.cmp(&a.0));
// 5. Apply allows and denies from roles.
for (_, v) in roles {
permissions.apply(v);
}
}
permissions
} else {
(Permission::GrantAllSafe as u64).into()
}
}
};
Ok(value)
}

View File

@@ -0,0 +1,100 @@
use crate::{
models::{user::RelationshipStatus, User},
permissions::PermissionCalculator,
UserPermission, UserPermissions, UserPerms,
};
impl PermissionCalculator<'_> {
/// Calculate the permissions from our perspective to the given user
///
/// How the permission is calculated:
/// 1. Are we the target?
/// - If so: return maximum permissions
/// 2. Do we have a relationship with the target?
/// - If we are friends: return maximum permissions
/// - If either user blocked each other: return only `Access`
/// - If incoming / outgoing request: add `Access` to the list
/// 3. Determine whether there is a mutual connection:
/// 1. Check if the "mutual connection" flag is set.
/// 2. Check if we share any servers with the target.
/// 3. Check if we share any DMs or groups with the target.
/// 4. Do we have a mutual connection with the target?
/// - If so: return `Access` + `ViewProfile`
/// 5. Return no permissions
pub async fn calc_user(&mut self, db: &crate::Database) -> UserPerms {
if self.user.has() {
let v = calculate_permission(self, db).await;
self.cached_user_permission = Some(v);
UserPermissions([v])
} else {
panic!("Expected `PermissionCalculator.user` to exist.")
}
}
}
/// Find the relationship between two users
pub fn get_relationship(a: &User, b: &str) -> RelationshipStatus {
if a.id == b {
return RelationshipStatus::User;
}
if let Some(relations) = &a.relations {
if let Some(relationship) = relations.iter().find(|x| x.id == b) {
return relationship.status.clone();
}
}
RelationshipStatus::None
}
/// Internal helper function for calculating permission
async fn calculate_permission(data: &mut PermissionCalculator<'_>, db: &crate::Database) -> u32 {
let user = data.user.get().unwrap();
if data.perspective.id == user.id {
return u32::MAX;
}
let relationship = data.flag_known_relationship.cloned().unwrap_or_else(|| {
user.relationship
.as_ref()
.cloned()
.unwrap_or_else(|| get_relationship(data.perspective, &user.id))
});
let mut permissions: u32 = 0;
match relationship {
RelationshipStatus::Friend => return u32::MAX,
RelationshipStatus::Blocked | RelationshipStatus::BlockedOther => {
return UserPermission::Access as u32
}
RelationshipStatus::Incoming | RelationshipStatus::Outgoing => {
permissions = UserPermission::Access as u32;
}
_ => {}
}
// ! FIXME: add boolean switch for permission for users to globally message a user
// maybe an enum?
// PrivacyLevel { Private, Friends, Mutual, Public, Global }
// ! FIXME: add boolean switch for permission for users to mutually DM a user
if data.flag_has_mutual_connection
|| data
.perspective
.has_mutual_connection(db, &user.id)
.await
.unwrap_or(false)
{
permissions = UserPermission::Access + UserPermission::ViewProfile;
if user.bot.is_some() || data.perspective.bot.is_some() {
permissions += UserPermission::SendMessage as u32;
}
return permissions;
}
permissions
}

View File

@@ -0,0 +1,171 @@
use crate::{
models::{user::RelationshipStatus, Channel, Member, Server, User},
util::value::Value,
Database, Error, Override, Permission, Result,
};
pub mod defn;
pub mod r#impl;
pub use r#impl::user::get_relationship;
/// Permissions calculator
#[derive(Clone)]
pub struct PermissionCalculator<'a> {
perspective: &'a User,
pub user: Value<'a, User>,
pub channel: Value<'a, Channel>,
pub server: Value<'a, Server>,
pub member: Value<'a, Member>,
flag_known_relationship: Option<&'a RelationshipStatus>,
flag_has_mutual_connection: bool,
cached_user_permission: Option<u32>,
cached_permission: Option<u64>,
}
impl<'a> PermissionCalculator<'a> {
/// Create a new permission calculator
pub fn new(perspective: &'a User) -> PermissionCalculator {
PermissionCalculator {
perspective,
user: Value::None,
channel: Value::None,
server: Value::None,
member: Value::None,
flag_known_relationship: None,
flag_has_mutual_connection: false,
cached_user_permission: None,
cached_permission: None,
}
}
/// Use user by ref
pub fn user(self, user: &'a User) -> PermissionCalculator {
PermissionCalculator {
user: Value::Ref(user),
..self
}
}
/// Use channel by ref
pub fn channel(self, channel: &'a Channel) -> PermissionCalculator {
PermissionCalculator {
channel: Value::Ref(channel),
..self
}
}
/// Use server by ref
pub fn server(self, server: &'a Server) -> PermissionCalculator {
PermissionCalculator {
server: Value::Ref(server),
..self
}
}
/// Use member by ref
pub fn member(self, member: &'a Member) -> PermissionCalculator {
PermissionCalculator {
member: Value::Ref(member),
..self
}
}
/// Use existing relationship by ref
pub fn with_relationship(self, relationship: &'a RelationshipStatus) -> PermissionCalculator {
PermissionCalculator {
flag_known_relationship: Some(relationship),
..self
}
}
/// Check whether the calculated permission contains a given value
pub async fn has_permission_value(&mut self, db: &Database, value: u64) -> Result<bool> {
let perms = if let Some(perms) = self.cached_permission {
perms
} else {
self.calc(db).await?.0[0]
};
Ok((value) & perms == (value))
}
/// Check whether we have a given permission
pub async fn has_permission(&mut self, db: &Database, permission: Permission) -> Result<bool> {
self.has_permission_value(db, permission as u64).await
}
/// Check whether we have a given permission, otherwise throw an error
pub async fn throw_permission_value(&mut self, db: &Database, value: u64) -> Result<()> {
if self.has_permission_value(db, value).await? {
Ok(())
} else {
Err(Error::CannotGiveMissingPermissions)
}
}
/// Check whether we have a given permission, otherwise throw an error
pub async fn throw_permission(&mut self, db: &Database, permission: Permission) -> Result<()> {
if self.has_permission(db, permission).await? {
Ok(())
} else {
Error::from_permission(permission)
}
}
/// Throw an error if we cannot grant permissions on either allows or denies
/// going from the previous given value to the next given value.
///
/// We need to check any:
/// - allows added (permissions now granted)
/// - denies removed (permissions now neutral or granted)
pub async fn throw_permission_override<C>(
&mut self,
db: &Database,
current_value: C,
next_value: Override,
) -> Result<()>
where
C: Into<Option<Override>>,
{
let current_value = current_value.into();
if let Some(current_value) = current_value {
self.throw_permission_value(db, !current_value.allows() & next_value.allows())
.await?;
self.throw_permission_value(db, current_value.denies() & !next_value.denies())
.await
} else {
self.throw_permission_value(db, next_value.allows()).await
}
}
/// Check whether we has the ViewChannel and another given permission, otherwise throw an error
pub async fn throw_permission_and_view_channel(
&mut self,
db: &Database,
permission: Permission,
) -> Result<()> {
self.throw_permission(db, Permission::ViewChannel).await?;
self.throw_permission(db, permission).await
}
/// Get the known member's current ranking
pub fn get_member_rank(&self) -> Option<i64> {
self.member
.get()
.map(|member| member.get_ranking(self.server.get().unwrap()))
}
}
/// Short-hand for creating a permission calculator
pub fn perms(perspective: &'_ User) -> PermissionCalculator<'_> {
PermissionCalculator::new(perspective)
}

View File

@@ -0,0 +1,64 @@
use std::env;
use serde::{Deserialize, Serialize};
lazy_static! {
pub static ref REGION_ID: u16 = env::var("REGION_ID")
.unwrap_or_else(|_| "0".to_string())
.parse()
.unwrap();
pub static ref REGION_KEY: String = format!("region{}", &*REGION_ID);
}
/// Compact presence information for a user
#[derive(Serialize, Deserialize, Debug)]
pub struct PresenceEntry {
/// Region this session exists in
///
/// We can have up to 65535 regions
pub region_id: u16,
/// Unique session ID
pub session_id: u8,
/// Known flags about session
pub flags: u8,
}
impl PresenceEntry {
/// Create a new presence entry from a given session ID and known flags
pub fn from(session_id: u8, flags: u8) -> Self {
Self {
region_id: *REGION_ID,
session_id,
flags,
}
}
}
pub trait PresenceOp {
/// Find next available session ID
fn find_next_id(&self) -> u8;
}
impl PresenceOp for Vec<PresenceEntry> {
fn find_next_id(&self) -> u8 {
// O(n^2) scan algorithm
// should be relatively fast at low numbers anyways
for i in 0..255 {
let mut found = false;
for entry in self {
if entry.session_id == i {
found = true;
break;
}
}
if !found {
return i;
}
}
255
}
}

View File

@@ -0,0 +1,162 @@
use std::collections::HashSet;
use redis_kiss::{get_connection, AsyncCommands};
mod entry;
mod operations;
use entry::{PresenceEntry, PresenceOp};
use operations::{
__add_to_set_sessions, __delete_key_presence_entry, __get_key_presence_entry,
__get_set_sessions, __remove_from_set_sessions, __set_key_presence_entry,
};
use crate::presence::operations::__delete_set_sessions;
use self::entry::REGION_KEY;
/// Create a new presence session, returns the ID of this session
pub async fn presence_create_session(user_id: &str, flags: u8) -> (bool, u8) {
info!("Creating a presence session for {user_id} with flags {flags}");
// Try to find the presence entry for this user.
let mut conn = get_connection().await.unwrap();
let mut entry: Vec<PresenceEntry> = __get_key_presence_entry(&mut conn, user_id)
.await
.unwrap_or_default();
// Return whether this was the first session.
let was_empty = entry.is_empty();
info!("User ID {} just came online.", &user_id);
// Generate session ID and push new entry.
let session_id = entry.find_next_id();
entry.push(PresenceEntry::from(session_id, flags));
__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;
(was_empty, session_id)
}
/// Delete existing presence session
pub async fn presence_delete_session(user_id: &str, session_id: u8) -> bool {
presence_delete_session_internal(user_id, session_id, false).await
}
/// Delete existing presence session (but also choose whether to skip region)
async fn presence_delete_session_internal(
user_id: &str,
session_id: u8,
skip_region: bool,
) -> bool {
info!("Deleting presence session for {user_id} with id {session_id}");
// Return whether this was the last session.
let mut is_empty = false;
// Only continue if we can actually find one.
let mut conn = get_connection().await.unwrap();
let entry: Option<Vec<PresenceEntry>> = __get_key_presence_entry(&mut conn, user_id).await;
if let Some(entry) = entry {
let entries = entry
.into_iter()
.filter(|x| x.session_id != session_id)
.collect::<Vec<PresenceEntry>>();
// If entry is empty, then just delete it.
if entries.is_empty() {
__delete_key_presence_entry(&mut conn, user_id).await;
is_empty = true;
} else {
__set_key_presence_entry(&mut conn, user_id, entries).await;
}
// Remove from region set.
if !skip_region {
__remove_from_set_sessions(&mut conn, &*REGION_KEY, user_id, session_id).await;
}
}
if is_empty {
info!("User ID {} just went offline.", &user_id);
}
is_empty
}
/// Check whether a given user ID is online
pub async fn presence_is_online(user_id: &str) -> bool {
if let Ok(mut conn) = get_connection().await {
conn.exists(user_id).await.unwrap_or(false)
} else {
false
}
}
/// Check whether a set of users is online, returns a set of the online user IDs
pub async fn presence_filter_online(user_ids: &'_ [String]) -> HashSet<String> {
// Ignore empty list immediately, to save time.
let mut set = HashSet::new();
if user_ids.is_empty() {
return set;
}
// We need to handle a special case where only one is present
// as for some reason or another, Redis does not like us sending
// a list of just one ID to the server.
if user_ids.len() == 1 {
if presence_is_online(&user_ids[0]).await {
set.insert(user_ids[0].to_string());
}
return set;
}
// Otherwise, go ahead as normal.
if let Ok(mut conn) = get_connection().await {
let data: Vec<Option<Vec<u8>>> = conn.get(user_ids).await.unwrap_or_default();
if data.is_empty() {
return set;
}
// We filter known values to figure out who is online.
for i in 0..user_ids.len() {
if data[i].is_some() {
set.insert(user_ids[i].to_string());
}
}
}
set
}
/// Reset any stale presence data
pub async fn presence_clear_region(region_id: Option<&str>) {
let region_id = region_id.unwrap_or(&*REGION_KEY);
let mut conn = get_connection().await.expect("Redis connection");
let sessions = __get_set_sessions(&mut conn, region_id).await;
if !sessions.is_empty() {
info!(
"Cleaning up {} sessions, this may take a while...",
sessions.len()
);
// Iterate and delete each session, this will
// 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 Ok(session_id) = session_id.parse() {
presence_delete_session_internal(user_id, session_id, true).await;
}
}
}
// Then clear the set in Redis.
__delete_set_sessions(&mut conn, region_id).await;
info!("Clean up complete.");
}
}

View File

@@ -0,0 +1,57 @@
use redis_kiss::{AsyncCommands, Conn};
use super::entry::PresenceEntry;
/// Set presence entry by given ID
pub async fn __set_key_presence_entry(conn: &mut Conn, id: &str, data: Vec<PresenceEntry>) {
let _: Option<()> = conn.set(id, bincode::serialize(&data).unwrap()).await.ok();
}
/// Delete presence entry by given ID
pub async fn __delete_key_presence_entry(conn: &mut Conn, id: &str) {
let _: Option<()> = conn.del(id).await.ok();
}
/// Get presence entry by given ID
pub async fn __get_key_presence_entry(conn: &mut Conn, id: &str) -> Option<Vec<PresenceEntry>> {
conn.get::<_, Option<Vec<u8>>>(id)
.await
.unwrap()
.map(|entry| bincode::deserialize(&entry[..]).unwrap())
}
/// Add to region session set
pub async fn __add_to_set_sessions(
conn: &mut Conn,
region_id: &str,
user_id: &str,
session_id: u8,
) {
let _: Option<()> = conn
.sadd(region_id, format!("{user_id}:{session_id}"))
.await
.ok();
}
/// Remove from region session set
pub async fn __remove_from_set_sessions(
conn: &mut Conn,
region_id: &str,
user_id: &str,
session_id: u8,
) {
let _: Option<()> = conn
.srem(region_id, format!("{user_id}:{session_id}"))
.await
.ok();
}
/// Get region session set as list
pub async fn __get_set_sessions(conn: &mut Conn, region_id: &str) -> Vec<String> {
conn.smembers::<_, Vec<String>>(region_id).await.unwrap()
}
/// Delete region session set
pub async fn __delete_set_sessions(conn: &mut Conn, region_id: &str) {
let _: () = conn.del(region_id).await.unwrap();
}

View File

@@ -0,0 +1,123 @@
// Queue Type: Debounced
use crate::Database;
use deadqueue::limited::Queue;
use mongodb::bson::doc;
use std::{collections::HashMap, time::Duration};
use super::DelayedTask;
/// Enumeration of possible events
#[derive(Debug, Eq, PartialEq)]
pub enum AckEvent {
/// Add mentions for a user in a channel
AddMention {
/// Message IDs
ids: Vec<String>,
},
/// Acknowledge message in a channel for a user
AckMessage {
/// Message ID
id: String,
},
}
/// Task information
struct Data {
/// Channel to ack
channel: String,
/// User to ack for
user: String,
/// Event
event: AckEvent,
}
#[derive(Debug)]
struct Task {
event: AckEvent,
}
lazy_static! {
static ref Q: Queue<Data> = Queue::new(10_000);
}
/// Queue a new task for a worker
pub async fn queue(channel: String, user: String, event: AckEvent) {
Q.try_push(Data {
channel,
user,
event,
})
.ok();
info!("Queue is using {} slots from {}.", Q.len(), Q.capacity());
}
/// Start a new worker
pub async fn worker(db: Database) {
let mut tasks = HashMap::<(String, String), DelayedTask<Task>>::new();
let mut keys = vec![];
loop {
// Find due tasks.
for (key, task) in &tasks {
if task.should_run() {
keys.push(key.clone());
}
}
// Commit any due tasks to the database.
for key in &keys {
if let Some(task) = tasks.remove(key) {
let Task { event } = task.data;
let (user, channel) = key;
if let Err(err) = match &event {
AckEvent::AckMessage { id } => db.acknowledge_message(channel, user, id).await,
AckEvent::AddMention { ids } => {
db.add_mention_to_unread(channel, user, ids).await
}
} {
error!("{err:?} for {event:?}. ({user}, {channel})");
} else {
info!("User {user} ack in {channel} with {event:?}");
}
}
}
// Clear keys
keys.clear();
// Queue incoming tasks.
while let Some(Data {
channel,
user,
mut event,
}) = Q.try_pop()
{
let key = (user, channel);
if let Some(mut task) = tasks.get_mut(&key) {
task.delay();
match &mut event {
AckEvent::AddMention { ids } => {
if let AckEvent::AddMention { ids: existing } = &mut task.data.event {
existing.append(ids);
} else {
task.data.event = event;
}
}
AckEvent::AckMessage { .. } => {
task.data.event = event;
}
}
} else {
tasks.insert(key, DelayedTask::new(Task { event }));
}
}
// Sleep for an arbitrary amount of time.
async_std::task::sleep(Duration::from_secs(1)).await;
}
}

View File

@@ -0,0 +1,90 @@
// Queue Type: Debounced
use crate::{models::channel::PartialChannel, Database};
use deadqueue::limited::Queue;
use log::info;
use mongodb::bson::doc;
use std::{collections::HashMap, time::Duration};
use super::DelayedTask;
/// Task information
struct Data {
/// Channel to update
channel: String,
/// Latest message ID
id: String,
/// Whether the channel is a DM
is_dm: bool,
}
/// Task information
#[derive(Debug)]
struct Task {
/// Latest message ID
id: String,
/// Whether the channel is a DM
is_dm: bool,
}
lazy_static! {
static ref Q: Queue<Data> = Queue::new(10_000);
}
/// Queue a new task for a worker
pub async fn queue(channel: String, id: String, is_dm: bool) {
Q.try_push(Data { channel, id, is_dm }).ok();
info!("Queue is using {} slots from {}.", Q.len(), Q.capacity());
}
/// Start a new worker
pub async fn worker(db: Database) {
let mut tasks = HashMap::<String, DelayedTask<Task>>::new();
let mut keys = vec![];
loop {
// Find due tasks.
for (key, task) in &tasks {
if task.should_run() {
keys.push(key.clone());
}
}
// Commit any due tasks to the database.
for key in &keys {
if let Some(task) = tasks.remove(key) {
let Task { id, is_dm, .. } = task.data;
let mut channel = PartialChannel {
last_message_id: Some(id.to_string()),
..Default::default()
};
if is_dm {
channel.active = Some(true);
}
match db.update_channel(key, &channel, vec![]).await {
Ok(_) => info!("Updated last_message_id for {key} to {id}."),
Err(err) => error!("Failed to update last_message_id with {err:?}!"),
}
}
}
// Clear keys
keys.clear();
// Queue incoming tasks.
while let Some(Data { channel, id, is_dm }) = Q.try_pop() {
if let Some(mut task) = tasks.get_mut(&channel) {
task.data.id = id;
task.delay();
} else {
tasks.insert(channel, DelayedTask::new(Task { id, is_dm }));
}
}
// Sleep for an arbitrary amount of time.
async_std::task::sleep(Duration::from_secs(1)).await;
}
}

View File

@@ -0,0 +1,58 @@
//! Semi-important background task management
use crate::Database;
use async_std::task;
use std::time::Instant;
const WORKER_COUNT: usize = 5;
pub mod ack;
pub mod last_message_id;
pub mod process_embeds;
pub mod web_push;
/// Spawn background workers
pub async fn start_workers(db: Database) {
for _ in 0..WORKER_COUNT {
task::spawn(ack::worker(db.clone()));
task::spawn(last_message_id::worker(db.clone()));
task::spawn(process_embeds::worker(db.clone()));
task::spawn(web_push::worker(db.clone()));
}
}
/// Task with additional information on when it should run
pub struct DelayedTask<T> {
pub data: T,
last_updated: Instant,
first_seen: Instant,
}
/// Commit to database every 30 seconds if the task is particularly active.
static EXPIRE_CONSTANT: u64 = 30;
/// Otherwise, commit to database after 5 seconds.
static SAVE_CONSTANT: u64 = 5;
impl<T> DelayedTask<T> {
/// Create a new delayed task
pub fn new(data: T) -> Self {
DelayedTask {
data,
last_updated: Instant::now(),
first_seen: Instant::now(),
}
}
/// Push a task further back in time
pub fn delay(&mut self) {
self.last_updated = Instant::now()
}
/// Check if a task should run yet
pub fn should_run(&self) -> bool {
self.first_seen.elapsed().as_secs() > EXPIRE_CONSTANT
|| self.last_updated.elapsed().as_secs() > SAVE_CONSTANT
}
}

View File

@@ -0,0 +1,57 @@
use crate::util::variables::delta::{JANUARY_URL, MAX_EMBED_COUNT};
use crate::{
models::{message::AppendMessage, Message},
types::january::Embed,
Database,
};
use deadqueue::limited::Queue;
use log::error;
/// Task information
#[derive(Debug)]
struct EmbedTask {
/// Channel we're processing the event in
channel: String,
/// ID of the message we're processing
id: String,
/// Content of the message
content: String,
}
lazy_static! {
static ref Q: Queue<EmbedTask> = Queue::new(10_000);
}
/// Queue a new task for a worker
pub async fn queue(channel: String, id: String, content: String) {
Q.try_push(EmbedTask {
channel,
id,
content,
})
.ok();
info!("Queue is using {} slots from {}.", Q.len(), Q.capacity());
}
/// Start a new worker
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 Err(err) = Message::append(
&db,
task.id,
task.channel,
AppendMessage {
embeds: Some(embeds),
},
)
.await
{
error!("Encountered an error appending to message: {:?}", err);
}
}
}
}

View File

@@ -0,0 +1,135 @@
use crate::util::variables::delta::VAPID_PRIVATE_KEY;
use crate::{bson::doc, r#impl::mongo::MongoDb, Database};
use deadqueue::limited::Queue;
use futures::StreamExt;
use rauth::entities::Session;
use web_push::{
ContentEncoding, SubscriptionInfo, SubscriptionKeys, VapidSignatureBuilder, WebPushClient,
WebPushMessageBuilder,
};
/// Task information
#[derive(Debug)]
struct PushTask {
/// User IDs of the targets that are to receive this notification
recipients: Vec<String>,
/// Raw JSON payload to send to clients
payload: String,
}
lazy_static! {
static ref Q: Queue<PushTask> = Queue::new(10_000);
}
/// Queue a new task for a worker
pub async fn queue(recipients: Vec<String>, payload: String) {
if recipients.is_empty() {
return;
}
Q.try_push(PushTask {
recipients,
payload,
})
.ok();
info!("Queue is using {} slots from {}.", Q.len(), Q.capacity());
}
/// Start a new worker
pub async fn worker(db: Database) {
let client = WebPushClient::new();
let key = base64::decode_config(VAPID_PRIVATE_KEY.clone(), base64::URL_SAFE)
.expect("valid `VAPID_PRIVATE_KEY`");
if let Database::MongoDb(MongoDb(db)) = db {
loop {
let task = Q.pop().await;
// ! FIXME: this is hard-coded until rauth is merged into quark
if let Ok(mut cursor) = db
.database("revolt")
.collection::<Session>("sessions")
.find(
doc! {
"user_id": {
"$in": task.recipients
},
"subscription": {
"$exists": true
}
},
None,
)
.await
{
while let Some(Ok(session)) = cursor.next().await {
if let Some(sub) = session.subscription {
let subscription = SubscriptionInfo {
endpoint: sub.endpoint,
keys: SubscriptionKeys {
auth: sub.auth,
p256dh: sub.p256dh,
},
};
match WebPushMessageBuilder::new(&subscription) {
Ok(mut builder) => {
match VapidSignatureBuilder::from_pem(
std::io::Cursor::new(&key),
&subscription,
) {
Ok(sig_builder) => match sig_builder.build() {
Ok(signature) => {
builder.set_vapid_signature(signature);
builder.set_payload(
ContentEncoding::AesGcm,
task.payload.as_bytes(),
);
match builder.build() {
Ok(msg) => match client.send(msg).await {
Ok(_) => {
info!(
"Sent Web Push notification to {:?}.",
session.id
)
}
Err(err) => {
error!(
"Hit error sending Web Push! {:?}",
err
)
}
},
Err(err) => {
error!(
"Failed to build message for {}! {:?}",
session.user_id, err
)
}
}
}
Err(err) => error!(
"Failed to build signature for {}! {:?}",
session.user_id, err
),
},
Err(err) => error!(
"Failed to create signature builder for {}! {:?}",
session.user_id, err
),
}
}
Err(err) => error!(
"Invalid subscription information for {}! {:?}",
session.user_id, err
),
}
}
}
}
}
}
}

View File

@@ -0,0 +1,6 @@
use crate::Result;
#[async_trait]
pub trait AbstractMigrations: Sync + Send {
async fn migrate_database(&self) -> Result<()>;
}

View File

@@ -0,0 +1,16 @@
use crate::models::attachment::File;
use crate::Result;
#[async_trait]
pub trait AbstractAttachment: Sync + Send {
async fn find_and_use_attachment(
&self,
id: &str,
tag: &str,
parent_type: &str,
parent_id: &str,
) -> Result<File>;
async fn insert_attachment(&self, attachment: &File) -> Result<()>;
async fn mark_attachment_as_reported(&self, id: &str) -> Result<()>;
async fn mark_attachment_as_deleted(&self, id: &str) -> Result<()>;
}

View File

@@ -0,0 +1,61 @@
use std::collections::HashSet;
use crate::models::channel::{Channel, FieldsChannel, PartialChannel};
use crate::{OverrideField, Result};
#[async_trait]
pub trait AbstractChannel: Sync + Send {
/// Fetch a channel by its id
async fn fetch_channel(&self, id: &str) -> Result<Channel>;
/// Fetch channels by their ids
async fn fetch_channels<'a>(&self, ids: &'a [String]) -> Result<Vec<Channel>>;
/// Insert a new channel into the database
async fn insert_channel(&self, channel: &Channel) -> Result<()>;
/// Update an existing channel using some data
/// ! TODO: we need separate Channel::update which also sends out the relevant events
/// ! also applies to other methods I guess, try to restrict event bound methods to
/// ! the models themselves instead of the abstract database
async fn update_channel(
&self,
id: &str,
channel: &PartialChannel,
remove: Vec<FieldsChannel>,
) -> Result<()>;
/// Delete a channel by its id
///
/// This will also delete all associated messages and files.
async fn delete_channel(&self, channel: &Channel) -> Result<()>;
/// Find all direct messages that a user is involved in
///
/// Returns group DMs, any DMs marked as "active" and saved messages.
async fn find_direct_messages(&self, user_id: &str) -> Result<Vec<Channel>>;
/// Find a direct message channel between two users
async fn find_direct_message_channel(&self, user_a: &str, user_b: &str) -> Result<Channel>;
/// Find a saved message channel owned by a user
async fn find_saved_messages_channel(&self, user_id: &str) -> Result<Channel>;
/// Add user to a group
async fn add_user_to_group(&self, channel: &str, user: &str) -> Result<()>;
/// Remove a user from a group
async fn remove_user_from_group(&self, channel: &str, user: &str) -> Result<()>;
/// Set role permission for a channel
/// ! FIXME: may want to refactor to just use normal updates
async fn set_channel_role_permission(
&self,
channel: &str,
role: &str,
permissions: OverrideField,
) -> Result<()>;
/// Validate existence of channels
async fn check_channels_exist(&self, channels: &HashSet<String>) -> Result<bool>;
}

View File

@@ -0,0 +1,17 @@
use crate::models::Invite;
use crate::Result;
#[async_trait]
pub trait AbstractChannelInvite: Sync + Send {
/// Fetch an invite by its id
async fn fetch_invite(&self, code: &str) -> Result<Invite>;
/// Insert a new invite into the database
async fn insert_invite(&self, invite: &Invite) -> Result<()>;
/// Delete an invite by its id
async fn delete_invite(&self, code: &str) -> Result<()>;
/// Fetch all invites for a server
async fn fetch_invites_for_server(&self, server: &str) -> Result<Vec<Invite>>;
}

View File

@@ -0,0 +1,22 @@
use crate::models::channel_unread::ChannelUnread;
use crate::Result;
#[async_trait]
pub trait AbstractChannelUnread: Sync + Send {
/// Acknowledge a message.
async fn acknowledge_message(&self, channel: &str, user: &str, message: &str) -> Result<()>;
/// Acknowledge many channels.
async fn acknowledge_channels(&self, user: &str, channels: &[String]) -> Result<()>;
/// Add a mention.
async fn add_mention_to_unread<'a>(
&self,
channel: &str,
user: &str,
ids: &[String],
) -> Result<()>;
/// Fetch all channel unreads for a user.
async fn fetch_unreads(&self, user: &str) -> Result<Vec<ChannelUnread>>;
}

View File

@@ -0,0 +1,45 @@
use crate::models::message::{AppendMessage, Message, MessageSort, PartialMessage};
use crate::Result;
#[async_trait]
pub trait AbstractMessage: Sync + Send {
/// Fetch a message by its id
async fn fetch_message(&self, id: &str) -> Result<Message>;
/// Insert a new message into the database
async fn insert_message(&self, message: &Message) -> Result<()>;
/// Update a given message with new information
async fn update_message(&self, id: &str, message: &PartialMessage) -> Result<()>;
/// Append information to a given message
async fn append_message(&self, id: &str, append: &AppendMessage) -> Result<()>;
/// Delete a message from the database by its id
async fn delete_message(&self, id: &str) -> Result<()>;
/// Delete messages from a channel by their ids and corresponding channel id
async fn delete_messages(&self, channel: &str, ids: Vec<String>) -> Result<()>;
/// Fetch multiple messages
async fn fetch_messages(
&self,
channel: &str,
limit: Option<i64>,
before: Option<String>,
after: Option<String>,
sort: Option<MessageSort>,
nearby: Option<String>,
) -> Result<Vec<Message>>;
/// Search for messages
async fn search_messages(
&self,
channel: &str,
query: &str,
limit: Option<i64>,
before: Option<String>,
after: Option<String>,
sort: MessageSort,
) -> Result<Vec<Message>>;
}

View File

@@ -0,0 +1,64 @@
mod admin {
pub mod migrations;
}
mod autumn {
pub mod attachment;
}
mod channels {
pub mod channel;
pub mod channel_invite;
pub mod channel_unread;
pub mod message;
}
mod servers {
pub mod server;
pub mod server_ban;
pub mod server_member;
}
mod users {
pub mod bot;
pub mod user;
pub mod user_settings;
}
pub use admin::migrations::AbstractMigrations;
pub use autumn::attachment::AbstractAttachment;
pub use channels::channel::AbstractChannel;
pub use channels::channel_invite::AbstractChannelInvite;
pub use channels::channel_unread::AbstractChannelUnread;
pub use channels::message::AbstractMessage;
pub use servers::server::AbstractServer;
pub use servers::server_ban::AbstractServerBan;
pub use servers::server_member::AbstractServerMember;
pub use users::bot::AbstractBot;
pub use users::user::AbstractUser;
pub use users::user_settings::AbstractUserSettings;
// pub trait AbstractEventEmitter {}
// + AbstractEventEmitter
pub trait AbstractDatabase:
Sync
+ Send
+ AbstractMigrations
+ AbstractAttachment
+ AbstractChannel
+ AbstractChannelInvite
+ AbstractChannelUnread
+ AbstractMessage
+ AbstractServer
+ AbstractServerBan
+ AbstractServerMember
+ AbstractBot
+ AbstractUser
+ AbstractUserSettings
{
}

View File

@@ -0,0 +1,42 @@
use crate::models::server::{FieldsRole, FieldsServer, PartialRole, PartialServer, Role, Server};
use crate::Result;
#[async_trait]
pub trait AbstractServer: Sync + Send {
/// Fetch a server by its id
async fn fetch_server(&self, id: &str) -> Result<Server>;
/// Fetch a servers by their ids
async fn fetch_servers<'a>(&self, ids: &'a [String]) -> Result<Vec<Server>>;
/// Insert a new server into database
async fn insert_server(&self, server: &Server) -> Result<()>;
/// Update a server with new information
async fn update_server(
&self,
id: &str,
server: &PartialServer,
remove: Vec<FieldsServer>,
) -> Result<()>;
/// Delete a server by its id
async fn delete_server(&self, server: &Server) -> Result<()>;
/// Insert a new role into server object
async fn insert_role(&self, server_id: &str, role_id: &str, role: &Role) -> Result<()>;
/// Update an existing role on a server
async fn update_role(
&self,
server_id: &str,
role_id: &str,
role: &PartialRole,
remove: Vec<FieldsRole>,
) -> Result<()>;
/// Delete a role from a server
///
/// Also updates channels and members.
async fn delete_role(&self, server_id: &str, role_id: &str) -> Result<()>;
}

View File

@@ -0,0 +1,18 @@
use crate::models::server_member::MemberCompositeKey;
use crate::models::ServerBan;
use crate::Result;
#[async_trait]
pub trait AbstractServerBan: Sync + Send {
/// Fetch a server ban by server and user id
async fn fetch_ban(&self, server: &str, user: &str) -> Result<ServerBan>;
/// Fetch all bans in a server
async fn fetch_bans(&self, server: &str) -> Result<Vec<ServerBan>>;
/// Insert new ban into database
async fn insert_ban(&self, ban: &ServerBan) -> Result<()>;
/// Delete a ban from the database
async fn delete_ban(&self, id: &MemberCompositeKey) -> Result<()>;
}

View File

@@ -0,0 +1,37 @@
use crate::models::server_member::{FieldsMember, Member, MemberCompositeKey, PartialMember};
use crate::Result;
#[async_trait]
pub trait AbstractServerMember: Sync + Send {
/// Fetch a server member by their id
async fn fetch_member(&self, server: &str, user: &str) -> Result<Member>;
/// Insert a new server member into the database
async fn insert_member(&self, member: &Member) -> Result<()>;
/// Update information for a server member
async fn update_member(
&self,
id: &MemberCompositeKey,
member: &PartialMember,
remove: Vec<FieldsMember>,
) -> Result<()>;
/// Delete a server member by their id
async fn delete_member(&self, id: &MemberCompositeKey) -> Result<()>;
/// Fetch all members in a server
async fn fetch_all_members<'a>(&self, server: &str) -> Result<Vec<Member>>;
/// Fetch all memberships for a user
async fn fetch_all_memberships<'a>(&self, user: &str) -> Result<Vec<Member>>;
/// Fetch multiple members by their ids
async fn fetch_members<'a>(&self, server: &str, ids: &'a [String]) -> Result<Vec<Member>>;
/// Fetch member count of a server
async fn fetch_member_count(&self, server: &str) -> Result<usize>;
/// Fetch server count of a user
async fn fetch_server_count(&self, user: &str) -> Result<usize>;
}

View File

@@ -0,0 +1,26 @@
use crate::models::bot::{Bot, FieldsBot, PartialBot};
use crate::Result;
#[async_trait]
pub trait AbstractBot: Sync + Send {
/// Fetch a bot by its id
async fn fetch_bot(&self, id: &str) -> Result<Bot>;
/// Fetch a bot by its token
async fn fetch_bot_by_token(&self, token: &str) -> Result<Bot>;
/// Insert new bot into the database
async fn insert_bot(&self, bot: &Bot) -> Result<()>;
/// Update bot with new information
async fn update_bot(&self, id: &str, bot: &PartialBot, remove: Vec<FieldsBot>) -> Result<()>;
/// Delete a bot from the database
async fn delete_bot(&self, id: &str) -> Result<()>;
/// Fetch bots owned by a user
async fn fetch_bots_by_user(&self, user_id: &str) -> Result<Vec<Bot>>;
/// Get the number of bots owned by a user
async fn get_number_of_bots_by_user(&self, user_id: &str) -> Result<usize>;
}

View File

@@ -0,0 +1,56 @@
use crate::models::user::{FieldsUser, PartialUser, RelationshipStatus, User};
use crate::Result;
#[async_trait]
pub trait AbstractUser: Sync + Send {
/// Fetch a user from the database
async fn fetch_user(&self, id: &str) -> Result<User>;
/// Fetch a user from the database by their username
async fn fetch_user_by_username(&self, username: &str) -> Result<User>;
/// Fetch a user from the database by their session token
async fn fetch_user_by_token(&self, token: &str) -> Result<User>;
/// Insert a new user into the database
async fn insert_user(&self, user: &User) -> Result<()>;
/// Update a user by their id given some data
async fn update_user(
&self,
id: &str,
user: &PartialUser,
remove: Vec<FieldsUser>,
) -> Result<()>;
/// Delete a user by their id
async fn delete_user(&self, id: &str) -> Result<()>;
/// Fetch multiple users by their ids
async fn fetch_users<'a>(&self, ids: &'a [String]) -> Result<Vec<User>>;
/// Check whether a username is already in use by another user
async fn is_username_taken(&self, username: &str) -> Result<bool>;
/// Fetch ids of users that both users are friends with
async fn fetch_mutual_user_ids(&self, user_a: &str, user_b: &str) -> Result<Vec<String>>;
/// Fetch ids of channels that both users are in
async fn fetch_mutual_channel_ids(&self, user_a: &str, user_b: &str) -> Result<Vec<String>>;
/// Fetch ids of servers that both users share
async fn fetch_mutual_server_ids(&self, user_a: &str, user_b: &str) -> Result<Vec<String>>;
/// Set relationship with another user
///
/// This should use pull_relationship if relationship is None.
async fn set_relationship(
&self,
user_id: &str,
target_id: &str,
relationship: &RelationshipStatus,
) -> Result<()>;
/// Remove relationship with another user
async fn pull_relationship(&self, user_id: &str, target_id: &str) -> Result<()>;
}

View File

@@ -0,0 +1,14 @@
use crate::models::UserSettings;
use crate::Result;
#[async_trait]
pub trait AbstractUserSettings: Sync + Send {
/// Fetch a subset of user settings
async fn fetch_user_settings(&'_ self, id: &str, filter: &'_ [String]) -> Result<UserSettings>;
/// Update a subset of user settings
async fn set_user_settings(&self, id: &str, settings: &UserSettings) -> Result<()>;
/// Delete all user settings
async fn delete_user_settings(&self, id: &str) -> Result<()>;
}

View File

@@ -0,0 +1,243 @@
use linkify::{LinkFinder, LinkKind};
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use crate::{models::attachment::File, Error, Result};
/// Image positioning and size
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
pub enum ImageSize {
/// Show large preview at the bottom of the embed
Large,
/// Show small preview to the side of the embed
Preview,
}
/// Image
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
pub struct Image {
/// URL to the original image
pub url: String,
/// Width of the image
pub width: isize,
/// Height of the image
pub height: isize,
/// Positioning and size
pub size: ImageSize,
}
/// Video
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
pub struct Video {
/// URL to the original video
pub url: String,
/// Width of the video
pub width: isize,
/// Height of the video
pub height: isize,
}
/// Type of remote Twitch content
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
pub enum TwitchType {
Channel,
Video,
Clip,
}
/// Type of remote Lightspeed.tv content
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
pub enum LightspeedType {
Channel,
}
/// Type of remote Bandcamp content
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
pub enum BandcampType {
Album,
Track,
}
/// Information about special remote content
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
#[serde(tag = "type")]
pub enum Special {
/// No remote content
None,
/// Content hint that this contains a GIF
///
/// Use metadata to find video or image to play
GIF,
/// YouTube video
YouTube {
id: String,
#[serde(skip_serializing_if = "Option::is_none")]
timestamp: Option<String>,
},
/// Lightspeed.tv stream
Lightspeed {
content_type: LightspeedType,
id: String,
},
/// Twitch stream or clip
Twitch {
content_type: TwitchType,
id: String,
},
/// Spotify track
Spotify { content_type: String, id: String },
/// Soundcloud track
Soundcloud,
/// Bandcamp track
Bandcamp {
content_type: BandcampType,
id: String,
},
}
/// Website metadata
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
pub struct Metadata {
/// Direct URL to web page
#[serde(skip_serializing_if = "Option::is_none")]
url: Option<String>,
/// Original direct URL
#[serde(skip_serializing_if = "Option::is_none")]
original_url: Option<String>,
/// Remote content
#[serde(skip_serializing_if = "Option::is_none")]
special: Option<Special>,
/// Title of website
#[serde(skip_serializing_if = "Option::is_none")]
title: Option<String>,
/// Description of website
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<String>,
/// Embedded image
#[serde(skip_serializing_if = "Option::is_none")]
image: Option<Image>,
/// Embedded video
#[serde(skip_serializing_if = "Option::is_none")]
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>,
/// URL to site icon
#[serde(skip_serializing_if = "Option::is_none")]
icon_url: Option<String>,
/// CSS Colour
#[serde(skip_serializing_if = "Option::is_none")]
colour: Option<String>,
}
/// Text Embed
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
pub struct Text {
/// URL to icon
#[serde(skip_serializing_if = "Option::is_none")]
pub icon_url: Option<String>,
/// URL for title
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
/// Title of text embed
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
/// Description of text embed
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
/// ID of uploaded autumn file
#[serde(skip_serializing_if = "Option::is_none")]
pub media: Option<File>,
/// CSS Colour
#[serde(skip_serializing_if = "Option::is_none")]
pub colour: Option<String>,
}
/// Embed
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
#[serde(tag = "type")]
pub enum Embed {
Website(Metadata),
Image(Image),
Video(Video),
Text(Text),
None,
}
impl Embed {
/// Generate embeds from given content
pub async fn generate(content: String, host: &str, max_embeds: usize) -> Result<Vec<Embed>> {
lazy_static! {
static ref RE_CODE: Regex = Regex::new("```(?:.|\n)+?```|`(?:.|\n)+?`").unwrap();
static ref RE_IGNORED: Regex = Regex::new("(<http.+>)").unwrap();
}
// Ignore code blocks.
let content = RE_CODE.replace_all(&content, "");
// Ignore all content between angle brackets starting with http.
let content = RE_IGNORED.replace_all(&content, "");
let content = content
// Ignore quoted lines.
.split('\n')
.map(|v| {
if let Some(c) = v.chars().next() {
if c == '>' {
return "";
}
}
v
})
.collect::<Vec<&str>>()
.join("\n");
let mut finder = LinkFinder::new();
finder.kinds(&[LinkKind::Url]);
let links: HashSet<String> = finder
.links(&content)
.take(max_embeds)
.map(|x| x.as_str().to_string())
.collect();
if links.is_empty() {
return Err(Error::LabelMe);
}
// ! FIXME: batch request to january?
let mut embeds: Vec<Embed> = Vec::new();
let client = reqwest::Client::new();
for link in links {
let result = client
.get(&format!("{}/embed", host))
.query(&[("url", link)])
.send()
.await;
if result.is_err() {
continue;
}
let response = result.unwrap();
if response.status().is_success() {
let res: Embed = response.json().await.map_err(|_| Error::InvalidOperation)?;
embeds.push(res);
}
}
// Prevent database update when no embeds are found.
if !embeds.is_empty() {
Ok(embeds)
} else {
Err(Error::LabelMe)
}
}
}

View File

@@ -0,0 +1,2 @@
pub mod january;
pub mod push;

View File

@@ -0,0 +1,72 @@
use std::time::SystemTime;
use serde::{Deserialize, Serialize};
use crate::models::{Message, User};
use crate::variables::delta::{APP_URL, AUTUMN_URL, PUBLIC_URL};
/// Push Notification
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PushNotification {
/// Known author name
pub author: String,
/// URL to author avatar
pub icon: String,
/// URL to first matching attachment
#[serde(skip_serializing_if = "Option::is_none")]
pub image: Option<String>,
/// Message content or system message information
pub body: String,
/// Unique tag, usually the channel ID
pub tag: String,
/// Timestamp at which this notification was created
pub timestamp: u64,
/// URL to open when clicking notification
pub url: String,
}
impl PushNotification {
/// Create a new notification from a given message, author and channel ID
pub fn new(msg: Message, author: Option<&User>, channel_id: &str) -> Self {
let icon = if let Some(author) = author {
if let Some(avatar) = &author.avatar {
format!("{}/avatars/{}", &*AUTUMN_URL, avatar.id)
} else {
format!("{}/users/{}/default_avatar", &*PUBLIC_URL, msg.author)
}
} else {
format!("{}/assets/logo.png", &*APP_URL)
};
let image = msg.attachments.and_then(|attachments| {
attachments
.first()
.map(|v| format!("{}/attachments/{}", &*AUTUMN_URL, v.id))
});
let body = if let Some(sys) = msg.system {
sys.into()
} else if let Some(text) = msg.content {
text
} else {
"Empty Message".to_string()
};
let timestamp = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.expect("Time went backwards")
.as_secs();
Self {
author: author
.map(|x| x.username.to_string())
.unwrap_or_else(|| "Revolt".to_string()),
icon,
image,
body,
tag: channel_id.to_string(),
timestamp,
url: format!("{}/channel/{}/{}", &*APP_URL, channel_id, msg.id),
}
}
}

View File

@@ -0,0 +1,13 @@
use std::collections::HashMap;
use serde::Serialize;
/// Prefix keys on an arbitrary object
pub fn prefix_keys<T: Serialize>(t: &T, prefix: &str) -> HashMap<String, serde_json::Value> {
let v: String = serde_json::to_string(t).unwrap();
let v: HashMap<String, serde_json::Value> = serde_json::from_str(&v).unwrap();
v.into_iter()
.filter(|(_k, v)| !v.is_null())
.map(|(k, v)| (prefix.to_owned() + &k, v))
.collect()
}

View File

@@ -0,0 +1,5 @@
pub mod manipulation;
pub mod r#ref;
pub mod result;
pub mod value;
pub mod variables;

View File

@@ -0,0 +1,87 @@
use futures::future::join;
use rocket::request::FromParam;
use schemars::schema::{InstanceType, Schema, SchemaObject, SingleOrVec};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::models::{Bot, Channel, Invite, Member, Message, Server, ServerBan, User};
use crate::presence::presence_is_online;
use crate::{Database, Result};
/// Reference to some object in the database
#[derive(Serialize, Deserialize)]
pub struct Ref {
/// Id of object
pub id: String,
}
impl Ref {
/// Create a Ref from an unchecked string
pub fn from_unchecked(id: String) -> Ref {
Ref { id }
}
/// Fetch user from Ref
pub async fn as_user(&self, db: &Database) -> Result<User> {
let (user, online) = join(db.fetch_user(&self.id), presence_is_online(&self.id)).await;
let mut user = user?;
user.online = Some(online);
Ok(user)
}
/// Fetch channel from Ref
pub async fn as_channel(&self, db: &Database) -> Result<Channel> {
db.fetch_channel(&self.id).await
}
/// Fetch server from Ref
pub async fn as_server(&self, db: &Database) -> Result<Server> {
db.fetch_server(&self.id).await
}
/// Fetch message from Ref
pub async fn as_message(&self, db: &Database) -> Result<Message> {
db.fetch_message(&self.id).await
}
/// Fetch bot from Ref
pub async fn as_bot(&self, db: &Database) -> Result<Bot> {
db.fetch_bot(&self.id).await
}
/// Fetch invite from Ref
pub async fn as_invite(&self, db: &Database) -> Result<Invite> {
Invite::find(db, &self.id).await
}
/// Fetch member from Ref
pub async fn as_member(&self, db: &Database, server: &str) -> Result<Member> {
db.fetch_member(server, &self.id).await
}
/// Fetch ban from Ref
pub async fn as_ban(&self, db: &Database, server: &str) -> Result<ServerBan> {
db.fetch_ban(server, &self.id).await
}
}
impl<'r> FromParam<'r> for Ref {
type Error = &'r str;
fn from_param(param: &'r str) -> Result<Self, Self::Error> {
Ok(Ref::from_unchecked(param.into()))
}
}
impl JsonSchema for Ref {
fn schema_name() -> String {
"Id".to_string()
}
fn json_schema(_gen: &mut schemars::gen::SchemaGenerator) -> Schema {
Schema::Object(SchemaObject {
instance_type: Some(SingleOrVec::Single(Box::new(InstanceType::String))),
..Default::default()
})
}
}

View File

@@ -0,0 +1,264 @@
use okapi::openapi3::{self, RefOr, SchemaObject};
use rocket::{
http::{ContentType, Status},
response::{self, Responder},
serde::json::serde_json::json,
Request, Response,
};
use schemars::schema::Schema;
use serde::{Deserialize, Serialize};
use std::io::Cursor;
use validator::ValidationErrors;
use crate::{Permission, UserPermission};
/// Possible API Errors
#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
#[serde(tag = "type")]
pub enum Error {
/// This error was not labeled :(
LabelMe,
// ? Onboarding related errors.
AlreadyOnboarded,
// ? User related errors.
UsernameTaken,
UnknownUser,
AlreadyFriends,
AlreadySentRequest,
Blocked,
BlockedByOther,
NotFriends,
// ? Channel related errors.
UnknownChannel,
UnknownAttachment,
UnknownMessage,
CannotEditMessage,
CannotJoinCall,
TooManyAttachments,
TooManyReplies,
EmptyMessage,
CannotRemoveYourself,
GroupTooLarge {
max: usize,
},
AlreadyInGroup,
NotInGroup,
// ? Server related errors.
UnknownServer,
InvalidRole,
Banned,
TooManyServers {
max: usize,
},
// ? Bot related errors.
ReachedMaximumBots,
IsBot,
BotIsPrivate,
// ? Permission errors.
MissingPermission {
permission: Permission,
},
MissingUserPermission {
permission: UserPermission,
},
NotElevated,
CannotGiveMissingPermissions,
// ? General errors.
DatabaseError {
operation: &'static str,
with: &'static str,
},
InternalError,
InvalidOperation,
InvalidCredentials,
InvalidSession,
DuplicateNonce,
VosoUnavailable,
NotFound,
NoEffect,
FailedValidation {
#[serde(skip_serializing, skip_deserializing)]
error: ValidationErrors,
},
}
impl Error {
/// Create a missing permission error from a given permission
pub fn from_permission<T>(permission: Permission) -> Result<T> {
Err(if let Permission::ViewChannel = permission {
Error::NotFound
} else {
Error::MissingPermission { permission }
})
}
/// Create a missing user permission error from a given user permission
pub fn from_user_permission<T>(permission: UserPermission) -> Result<T> {
Err(if let UserPermission::Access = permission {
Error::NotFound
} else {
Error::MissingUserPermission { permission }
})
}
/// Create a failed validation error from given validation errors
pub fn from_invalid<T>(validation_error: ValidationErrors) -> Result<T> {
Err(Error::FailedValidation {
error: validation_error,
})
}
}
/// Empty 204 HTTP Response
pub struct EmptyResponse;
/// Result type with custom Error
pub type Result<T, E = Error> = std::result::Result<T, E>;
// ! FIXME: #[cfg]
impl<'r> Responder<'r, 'static> for EmptyResponse {
fn respond_to(self, _: &'r Request<'_>) -> response::Result<'static> {
Response::build()
.status(rocket::http::Status { code: 204 })
.ok()
}
}
/// HTTP response builder for Error enum
impl<'r> Responder<'r, 'static> for Error {
fn respond_to(self, _: &'r Request<'_>) -> response::Result<'static> {
let status = match self {
Error::LabelMe => Status::InternalServerError,
Error::AlreadyOnboarded => Status::Forbidden,
Error::UnknownUser => Status::NotFound,
Error::UsernameTaken => Status::Conflict,
Error::AlreadyFriends => Status::Conflict,
Error::AlreadySentRequest => Status::Conflict,
Error::Blocked => Status::Conflict,
Error::BlockedByOther => Status::Forbidden,
Error::NotFriends => Status::Forbidden,
Error::UnknownChannel => Status::NotFound,
Error::UnknownMessage => Status::NotFound,
Error::UnknownAttachment => Status::BadRequest,
Error::CannotEditMessage => Status::Forbidden,
Error::CannotJoinCall => Status::BadRequest,
Error::TooManyAttachments => Status::BadRequest,
Error::TooManyReplies => Status::BadRequest,
Error::EmptyMessage => Status::UnprocessableEntity,
Error::CannotRemoveYourself => Status::BadRequest,
Error::GroupTooLarge { .. } => Status::Forbidden,
Error::AlreadyInGroup => Status::Conflict,
Error::NotInGroup => Status::NotFound,
Error::UnknownServer => Status::NotFound,
Error::InvalidRole => Status::NotFound,
Error::Banned => Status::Forbidden,
Error::TooManyServers { .. } => Status::Forbidden,
Error::ReachedMaximumBots => Status::BadRequest,
Error::IsBot => Status::BadRequest,
Error::BotIsPrivate => Status::Forbidden,
Error::MissingPermission { .. } => Status::Forbidden,
Error::MissingUserPermission { .. } => Status::Forbidden,
Error::NotElevated => Status::Forbidden,
Error::CannotGiveMissingPermissions => Status::Forbidden,
Error::DatabaseError { .. } => Status::InternalServerError,
Error::InternalError => Status::InternalServerError,
Error::InvalidOperation => Status::BadRequest,
Error::InvalidCredentials => Status::Unauthorized,
Error::InvalidSession => Status::Unauthorized,
Error::DuplicateNonce => Status::Conflict,
Error::VosoUnavailable => Status::BadRequest,
Error::NotFound => Status::NotFound,
Error::NoEffect => Status::Ok,
Error::FailedValidation { .. } => Status::BadRequest,
};
// Serialize the error data structure into JSON.
let string = json!(self).to_string();
// Build and send the request.
Response::build()
.sized_body(string.len(), Cursor::new(string))
.header(ContentType::new("application", "json"))
.status(status)
.ok()
}
}
impl rocket_okapi::response::OpenApiResponderInner for Error {
fn responses(
gen: &mut rocket_okapi::gen::OpenApiGenerator,
) -> std::result::Result<openapi3::Responses, rocket_okapi::OpenApiError> {
let mut content = okapi::Map::new();
let settings = schemars::gen::SchemaSettings::default().with(|s| {
s.option_nullable = true;
s.option_add_null_type = false;
s.definitions_path = "#/components/schemas/".to_string();
});
let mut schema_generator = settings.into_generator();
let schema = schema_generator.root_schema_for::<Error>();
let definitions = gen.schema_generator().definitions_mut();
for (key, value) in schema.definitions {
definitions.insert(key, value);
}
definitions.insert("Error".to_string(), Schema::Object(schema.schema));
content.insert(
"application/json".to_string(),
openapi3::MediaType {
schema: Some(SchemaObject {
reference: Some("#/components/schemas/Error".to_string()),
..Default::default()
}),
..Default::default()
},
);
Ok(openapi3::Responses {
default: Some(openapi3::RefOr::Object(openapi3::Response {
content,
description: "An error occurred.".to_string(),
..Default::default()
})),
..Default::default()
})
}
}
impl rocket_okapi::response::OpenApiResponderInner for EmptyResponse {
fn responses(
_gen: &mut rocket_okapi::gen::OpenApiGenerator,
) -> std::result::Result<openapi3::Responses, rocket_okapi::OpenApiError> {
let mut responses = okapi::Map::new();
responses.insert(
"204".to_string(),
RefOr::Object(openapi3::Response {
description: "Success".to_string(),
..Default::default()
}),
);
Ok(openapi3::Responses {
responses,
..Default::default()
})
}
}

View File

@@ -0,0 +1,38 @@
/// Owned, Ref or None Value
#[derive(Clone)]
pub enum Value<'a, T> {
Owned(T),
Ref(&'a T),
None,
}
impl<'a, T> Value<'a, T> {
/// Check whether this Value exists
pub fn has(&self) -> bool {
!matches!(self, Self::None)
}
/// Get this Value as an Option Ref
pub fn get(&self) -> Option<&T> {
match self {
Self::Owned(t) => Some(t),
Self::Ref(t) => Some(t),
Self::None => None,
}
}
/// Set owned value
pub fn set(&mut self, t: T) {
*self = Value::Owned(t);
}
/// Set referential value
pub fn set_ref(&mut self, t: &'a T) {
*self = Value::Ref(t);
}
/// Clear current value
pub fn clear(&mut self) {
*self = Value::None;
}
}

View File

@@ -0,0 +1,98 @@
use std::env;
#[cfg(debug_assertions)]
use log::warn;
lazy_static! {
// Application Settings
pub static ref PUBLIC_URL: String =
env::var("REVOLT_PUBLIC_URL").expect("Missing REVOLT_PUBLIC_URL environment variable.");
pub static ref APP_URL: String =
env::var("REVOLT_APP_URL").expect("Missing REVOLT_APP_URL environment variable.");
pub static ref EXTERNAL_WS_URL: String =
env::var("REVOLT_EXTERNAL_WS_URL").expect("Missing REVOLT_EXTERNAL_WS_URL environment variable.");
pub static ref AUTUMN_URL: String =
env::var("AUTUMN_PUBLIC_URL").unwrap_or_else(|_| "https://example.com".to_string());
pub static ref JANUARY_URL: String =
env::var("JANUARY_PUBLIC_URL").unwrap_or_else(|_| "https://example.com".to_string());
pub static ref VOSO_URL: String =
env::var("VOSO_PUBLIC_URL").unwrap_or_else(|_| "https://example.com".to_string());
pub static ref VOSO_WS_HOST: String =
env::var("VOSO_WS_HOST").unwrap_or_else(|_| "wss://example.com".to_string());
pub static ref VOSO_MANAGE_TOKEN: String =
env::var("VOSO_MANAGE_TOKEN").unwrap_or_else(|_| "0".to_string());
pub static ref HCAPTCHA_KEY: String =
env::var("REVOLT_HCAPTCHA_KEY").unwrap_or_else(|_| "0x0000000000000000000000000000000000000000".to_string());
pub static ref HCAPTCHA_SITEKEY: String =
env::var("REVOLT_HCAPTCHA_SITEKEY").unwrap_or_else(|_| "10000000-ffff-ffff-ffff-000000000001".to_string());
pub static ref VAPID_PRIVATE_KEY: String =
env::var("REVOLT_VAPID_PRIVATE_KEY").expect("Missing REVOLT_VAPID_PRIVATE_KEY environment variable.");
pub static ref VAPID_PUBLIC_KEY: String =
env::var("REVOLT_VAPID_PUBLIC_KEY").expect("Missing REVOLT_VAPID_PUBLIC_KEY environment variable.");
// Application Flags
pub static ref INVITE_ONLY: bool = env::var("REVOLT_INVITE_ONLY").map_or(false, |v| v == "1");
pub static ref USE_EMAIL: bool = env::var("REVOLT_USE_EMAIL_VERIFICATION").map_or(
env::var("REVOLT_SMTP_HOST").is_ok()
&& env::var("REVOLT_SMTP_USERNAME").is_ok()
&& env::var("REVOLT_SMTP_PASSWORD").is_ok()
&& env::var("REVOLT_SMTP_FROM").is_ok(),
|v| v == *"1"
);
pub static ref USE_HCAPTCHA: bool = env::var("REVOLT_HCAPTCHA_KEY").is_ok();
pub static ref USE_AUTUMN: bool = env::var("AUTUMN_PUBLIC_URL").is_ok();
pub static ref USE_JANUARY: bool = env::var("JANUARY_PUBLIC_URL").is_ok();
pub static ref USE_VOSO: bool = env::var("VOSO_PUBLIC_URL").is_ok() && env::var("VOSO_MANAGE_TOKEN").is_ok();
// SMTP Settings
pub static ref SMTP_HOST: String =
env::var("REVOLT_SMTP_HOST").unwrap_or_else(|_| "".to_string());
pub static ref SMTP_USERNAME: String =
env::var("REVOLT_SMTP_USERNAME").unwrap_or_else(|_| "".to_string());
pub static ref SMTP_PASSWORD: String =
env::var("REVOLT_SMTP_PASSWORD").unwrap_or_else(|_| "".to_string());
pub static ref SMTP_FROM: String = env::var("REVOLT_SMTP_FROM").unwrap_or_else(|_| "".to_string());
// Application Logic Settings
pub static ref MAX_GROUP_SIZE: usize =
env::var("REVOLT_MAX_GROUP_SIZE").unwrap_or_else(|_| "50".to_string()).parse().unwrap();
pub static ref MAX_BOT_COUNT: usize =
env::var("REVOLT_MAX_BOT_COUNT").unwrap_or_else(|_| "5".to_string()).parse().unwrap();
pub static ref MAX_EMBED_COUNT: usize =
env::var("REVOLT_MAX_EMBED_COUNT").unwrap_or_else(|_| "5".to_string()).parse().unwrap();
pub static ref MAX_SERVER_COUNT: usize =
env::var("REVOLT_MAX_SERVER_COUNT").unwrap_or_else(|_| "100".to_string()).parse().unwrap();
pub static ref EARLY_ADOPTER_BADGE: i64 =
env::var("REVOLT_EARLY_ADOPTER_BADGE").unwrap_or_else(|_| "0".to_string()).parse().unwrap();
}
pub fn preflight_checks() {
format!("url = {}", *APP_URL);
format!("public = {}", *PUBLIC_URL);
format!("external = {}", *EXTERNAL_WS_URL);
format!("privkey = {}", *VAPID_PRIVATE_KEY);
format!("pubkey = {}", *VAPID_PUBLIC_KEY);
if !(*USE_EMAIL) {
#[cfg(not(debug_assertions))]
if !env::var("REVOLT_UNSAFE_NO_EMAIL").map_or(false, |v| v == *"1") {
panic!("Running in production without email is not recommended, set REVOLT_UNSAFE_NO_EMAIL=1 to override.");
}
#[cfg(debug_assertions)]
warn!("No SMTP settings specified! Remember to configure email.");
}
if !(*USE_HCAPTCHA) {
#[cfg(not(debug_assertions))]
if !env::var("REVOLT_UNSAFE_NO_CAPTCHA").map_or(false, |v| v == *"1") {
panic!("Running in production without CAPTCHA is not recommended, set REVOLT_UNSAFE_NO_CAPTCHA=1 to override.");
}
#[cfg(debug_assertions)]
warn!("No Captcha key specified! Remember to add hCaptcha key.");
}
}

Some files were not shown because too many files have changed in this diff Show More