Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
No results found
Show changes
Showing
with 1057 additions and 975 deletions
use crate::database::{permissions, get_collection, get_db, PermissionTuple};
use futures::StreamExt;
use log::info;
use mongodb::{bson::{doc, from_document, to_document}, options::FindOptions};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct MigrationInfo {
_id: i32,
revision: i32,
}
pub const LATEST_REVISION: i32 = 7;
pub async fn migrate_database() {
let migrations = get_collection("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(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(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 = get_collection("messages");
let attachments = get_collection("attachments");
messages
.update_many(
doc! { "attachment": { "$exists": 1 } },
doc! { "$set": { "attachment.tag": "attachments", "attachment.size": 0 } },
None,
)
.await
.expect("Failed to update messages.");
attachments
.update_many(
doc! {},
doc! { "$set": { "tag": "attachments", "size": 0 } },
None,
)
.await
.expect("Failed to update attachments.");
}
if revision <= 2 {
info!("Running migration [revision 2 / 2021-05-08]: Add servers collection.");
get_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 = get_collection("messages");
let mut cursor = messages
.find(
doc! {
"attachment": {
"$exists": 1
}
},
FindOptions::builder()
.projection(doc! {
"_id": 1,
"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 }, "$set": { "attachments": attachments } },
None,
)
.await
.unwrap();
}
get_db()
.create_collection("channel_unreads", None)
.await
.expect("Failed to create channel_unreads collection.");
get_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.");
get_db()
.create_collection("server_members", None)
.await
.expect("Failed to create server_members collection.");
get_db()
.create_collection("server_bans", None)
.await
.expect("Failed to create server_bans collection.");
get_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: PermissionTuple,
}
let server = Server {
default_permissions: (
*permissions::server::DEFAULT_PERMISSION as i32,
*permissions::channel::DEFAULT_PERMISSION_SERVER as i32
)
};
get_collection("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.");
get_db()
.run_command(
doc! {
"createIndexes": "messages",
"indexes": [
{
"key": {
"content": "text"
},
"name": "content"
}
]
},
None,
)
.await
.expect("Failed to create message index.");
}
// Reminder to update LATEST_REVISION when adding new migrations.
LATEST_REVISION
}
use mongodb::{Client, Collection, Database};
use std::env;
use crate::util::variables::MONGO_URI;
use mongodb::{Client, Collection, Database};
use once_cell::sync::OnceCell;
static DBCONN: OnceCell<Client> = OnceCell::new();
pub fn connect() {
let client =
Client::with_uri_str(&env::var("DB_URI").expect("DB_URI not in environment variables!"))
.expect("Failed to init db connection.");
pub async fn connect() {
let client = Client::with_uri_str(&MONGO_URI)
.await
.expect("Failed to init db connection.");
DBCONN.set(client).unwrap();
migrations::run_migrations().await;
}
pub fn get_connection() -> &'static Client {
......@@ -24,11 +26,11 @@ pub fn get_collection(collection: &str) -> Collection {
get_db().collection(collection)
}
pub mod channel;
pub mod guild;
pub mod message;
pub mod mutual;
pub mod entities;
pub mod guards;
pub mod migrations;
pub mod permissions;
pub mod user;
pub use entities::*;
pub use guards::*;
pub use permissions::*;
use super::{get_collection, MemberPermissions};
use bson::doc;
use mongodb::options::FindOptions;
pub fn find_mutual_guilds(user_id: &str, target_id: &str) -> Vec<String> {
let col = get_collection("members");
if let Ok(result) = col.find(
doc! {
"$and": [
{ "id": user_id },
{ "id": target_id },
]
},
FindOptions::builder().projection(doc! { "_id": 1 }).build(),
) {
let mut results = vec![];
for doc in result {
if let Ok(guild) = doc {
results.push(guild.get_str("_id").unwrap().to_string());
}
}
results
} else {
vec![]
}
}
pub fn find_mutual_friends(user_id: &str, target_id: &str) -> Vec<String> {
let col = get_collection("users");
if let Ok(result) = col.find(
doc! {
"$and": [
{ "relations": { "$elemMatch": { "id": user_id, "status": 0 } } },
{ "relations": { "$elemMatch": { "id": target_id, "status": 0 } } },
]
},
FindOptions::builder().projection(doc! { "_id": 1 }).build(),
) {
let mut results = vec![];
for doc in result {
if let Ok(user) = doc {
results.push(user.get_str("_id").unwrap().to_string());
}
}
results
} else {
vec![]
}
}
pub fn find_mutual_groups(user_id: &str, target_id: &str) -> Vec<String> {
let col = get_collection("channels");
if let Ok(result) = col.find(
doc! {
"type": 1,
"$and": [
{ "recipients": user_id },
{ "recipients": target_id },
]
},
FindOptions::builder().projection(doc! { "_id": 1 }).build(),
) {
let mut results = vec![];
for doc in result {
if let Ok(group) = doc {
results.push(group.get_str("_id").unwrap().to_string());
}
}
results
} else {
vec![]
}
}
pub fn has_mutual_connection(user_id: &str, target_id: &str, with_permission: bool) -> bool {
let mut doc = doc! { "_id": 1 };
if with_permission {
doc.insert("default_permissions", 1);
}
let opt = FindOptions::builder().projection(doc);
if let Ok(result) = get_collection("guilds").find(
doc! {
"$and": [
{ "members": { "$elemMatch": { "id": user_id } } },
{ "members": { "$elemMatch": { "id": target_id } } },
]
},
if with_permission {
opt.build()
} else {
opt.limit(1).build()
},
) {
if with_permission {
for item in result {
// ? logic should match permissions.rs#calculate
if let Ok(guild) = item {
if guild.get_str("owner").unwrap() == user_id {
return true;
}
let permissions = guild.get_i32("default_permissions").unwrap() as u32;
if MemberPermissions([permissions]).get_send_direct_messages() {
return true;
}
}
}
false
} else {
if result.count() > 0 {
true
} else {
false
}
}
} else {
false
}
}
use super::mutual::has_mutual_connection;
use crate::database::guild::Member;
use crate::database::user::UserRelationship;
use crate::guards::auth::UserRef;
use crate::guards::channel::ChannelRef;
use crate::guards::guild::{get_member, GuildRef};
use num_enum::TryFromPrimitive;
#[derive(Debug, PartialEq, Eq, TryFromPrimitive)]
#[repr(u8)]
pub enum Relationship {
Friend = 0,
Outgoing = 1,
Incoming = 2,
Blocked = 3,
BlockedOther = 4,
NONE = 5,
SELF = 6,
}
#[derive(Debug, PartialEq, Eq, TryFromPrimitive, Copy, Clone)]
#[repr(u32)]
pub enum Permission {
Access = 1,
CreateInvite = 2,
KickMembers = 4,
BanMembers = 8,
ReadMessages = 16,
SendMessages = 32,
ManageMessages = 64,
ManageChannels = 128,
ManageServer = 256,
ManageRoles = 512,
SendDirectMessages = 1024,
}
bitfield! {
pub struct MemberPermissions(MSB0 [u32]);
u32;
pub get_access, set_access: 31;
pub get_create_invite, set_create_invite: 30;
pub get_kick_members, set_kick_members: 29;
pub get_ban_members, set_ban_members: 28;
pub get_read_messages, set_read_messages: 27;
pub get_send_messages, set_send_messages: 26;
pub get_manage_messages, set_manage_messages: 25;
pub get_manage_channels, set_manage_channels: 24;
pub get_manage_server, set_manage_server: 23;
pub get_manage_roles, set_manage_roles: 22;
pub get_send_direct_messages, set_send_direct_messages: 21;
}
pub fn get_relationship_internal(
user_id: &str,
target_id: &str,
relationships: &Option<Vec<UserRelationship>>,
) -> Relationship {
if user_id == target_id {
return Relationship::SELF;
}
if let Some(arr) = &relationships {
for entry in arr {
if entry.id == target_id {
match entry.status {
0 => return Relationship::Friend,
1 => return Relationship::Outgoing,
2 => return Relationship::Incoming,
3 => return Relationship::Blocked,
4 => return Relationship::BlockedOther,
_ => return Relationship::NONE,
}
}
}
}
Relationship::NONE
}
pub fn get_relationship(a: &UserRef, b: &UserRef) -> Relationship {
if a.id == b.id {
return Relationship::SELF;
}
get_relationship_internal(&a.id, &b.id, &a.fetch_relationships())
}
pub struct PermissionCalculator {
pub user: UserRef,
pub channel: Option<ChannelRef>,
pub guild: Option<GuildRef>,
pub member: Option<Member>,
}
impl PermissionCalculator {
pub fn new(user: UserRef) -> PermissionCalculator {
PermissionCalculator {
user,
channel: None,
guild: None,
member: None,
}
}
pub fn channel(self, channel: ChannelRef) -> PermissionCalculator {
PermissionCalculator {
channel: Some(channel),
..self
}
}
pub fn guild(self, guild: GuildRef) -> PermissionCalculator {
PermissionCalculator {
guild: Some(guild),
..self
}
}
pub fn fetch_data(mut self) -> PermissionCalculator {
let guild = if let Some(value) = self.guild {
Some(value)
} else if let Some(channel) = &self.channel {
match channel.channel_type {
0..=1 => None,
2 => {
if let Some(id) = &channel.guild {
GuildRef::from(id.clone())
} else {
None
}
}
_ => None,
}
} else {
None
};
if let Some(guild) = &guild {
self.member = get_member(&guild.id, &self.user.id);
}
self.guild = guild;
self
}
pub fn calculate(&self) -> u32 {
let mut permissions: u32 = 0;
if let Some(guild) = &self.guild {
if let Some(_member) = &self.member {
// ? logic should match mutual.rs#has_mutual_connection
if guild.owner == self.user.id {
return u32::MAX;
}
permissions = guild.default_permissions as u32;
}
}
if let Some(channel) = &self.channel {
match channel.channel_type {
0 => {
if let Some(arr) = &channel.recipients {
let mut other_user = "";
for item in arr {
if item != &self.user.id {
other_user = item;
}
}
// ? In this case, it is a "self DM".
if other_user == "" {
return 1024 + 128 + 32 + 16 + 1;
}
let relationships = self.user.fetch_relationships();
let relationship =
get_relationship_internal(&self.user.id, &other_user, &relationships);
if relationship == Relationship::Friend {
permissions = 1024 + 128 + 32 + 16 + 1;
} else if relationship == Relationship::Blocked
|| relationship == Relationship::BlockedOther
{
permissions = 1;
} else if has_mutual_connection(&self.user.id, other_user, true) {
permissions = 1024 + 128 + 32 + 16 + 1;
} else {
permissions = 1;
}
}
}
1 => {
if let Some(id) = &channel.owner {
if &self.user.id == id {
return u32::MAX;
}
}
if let Some(arr) = &channel.recipients {
for item in arr {
if item == &self.user.id {
permissions = 177;
break;
}
}
}
}
2 => {
// nothing implemented yet
}
_ => {}
}
}
permissions
}
pub fn as_permission(&self) -> MemberPermissions<[u32; 1]> {
MemberPermissions([self.calculate()])
}
}
use crate::database::*;
use crate::util::result::{Error, Result};
use super::PermissionCalculator;
use num_enum::TryFromPrimitive;
use std::ops;
#[derive(Debug, PartialEq, Eq, TryFromPrimitive, Copy, Clone)]
#[repr(u32)]
pub enum ChannelPermission {
View = 0b00000000000000000000000000000001, // 1
SendMessage = 0b00000000000000000000000000000010, // 2
ManageMessages = 0b00000000000000000000000000000100, // 4
ManageChannel = 0b00000000000000000000000000001000, // 8
VoiceCall = 0b00000000000000000000000000010000, // 16
InviteOthers = 0b00000000000000000000000000100000, // 32
EmbedLinks = 0b00000000000000000000000001000000, // 64
UploadFiles = 0b00000000000000000000000010000000, // 128
}
lazy_static! {
pub static ref DEFAULT_PERMISSION_DM: u32 =
ChannelPermission::View
+ ChannelPermission::SendMessage
+ ChannelPermission::ManageChannel
+ ChannelPermission::VoiceCall
+ ChannelPermission::InviteOthers
+ ChannelPermission::EmbedLinks
+ ChannelPermission::UploadFiles;
pub static ref DEFAULT_PERMISSION_SERVER: u32 =
ChannelPermission::View
+ ChannelPermission::SendMessage
+ ChannelPermission::VoiceCall
+ ChannelPermission::InviteOthers
+ ChannelPermission::EmbedLinks
+ ChannelPermission::UploadFiles;
}
impl_op_ex!(+ |a: &ChannelPermission, b: &ChannelPermission| -> u32 { *a as u32 | *b as u32 });
impl_op_ex_commutative!(+ |a: &u32, b: &ChannelPermission| -> u32 { *a | *b as u32 });
bitfield! {
pub struct ChannelPermissions(MSB0 [u32]);
u32;
pub get_view, _: 31;
pub get_send_message, _: 30;
pub get_manage_messages, _: 29;
pub get_manage_channel, _: 28;
pub get_voice_call, _: 27;
pub get_invite_others, _: 26;
pub get_embed_links, _: 25;
pub get_upload_files, _: 24;
}
impl<'a> PermissionCalculator<'a> {
pub async fn calculate_channel(self) -> Result<u32> {
let channel = if let Some(channel) = self.channel {
channel
} else {
unreachable!()
};
match channel {
Channel::SavedMessages { user: owner, .. } => {
if &self.perspective.id == owner {
Ok(u32::MAX)
} else {
Ok(0)
}
}
Channel::DirectMessage { recipients, .. } => {
if recipients
.iter()
.find(|x| *x == &self.perspective.id)
.is_some()
{
if let Some(recipient) = recipients.iter().find(|x| *x != &self.perspective.id)
{
let perms = self.for_user(recipient).await?;
if perms.get_send_message() {
return Ok(*DEFAULT_PERMISSION_DM);
}
return Ok(ChannelPermission::View as u32);
}
}
Ok(0)
}
Channel::Group { recipients, permissions, owner, .. } => {
if &self.perspective.id == owner {
return Ok(*DEFAULT_PERMISSION_DM)
}
if recipients
.iter()
.find(|x| *x == &self.perspective.id)
.is_some()
{
if let Some(permissions) = permissions {
Ok(permissions.clone() as u32)
} else {
Ok(*DEFAULT_PERMISSION_DM)
}
} else {
Ok(0)
}
}
Channel::TextChannel { server, default_permissions, role_permissions, .. }
| Channel::VoiceChannel { server, default_permissions, role_permissions, .. } => {
let server = Ref::from_unchecked(server.clone()).fetch_server().await?;
if self.perspective.id == server.owner {
Ok(u32::MAX)
} else {
match Ref::from_unchecked(self.perspective.id.clone()).fetch_member(&server.id).await {
Ok(member) => {
let mut perm = if let Some(permission) = default_permissions {
*permission as u32
} else {
server.default_permissions.1 as u32
};
if let Some(roles) = member.roles {
for role in roles {
if let Some(permission) = role_permissions.get(&role) {
perm |= *permission as u32;
}
if let Some(server_role) = server.roles.get(&role) {
perm |= server_role.permissions.1 as u32;
}
}
}
Ok(perm)
}
Err(error) => {
match &error {
Error::NotFound => Ok(0),
_ => Err(error)
}
}
}
}
}
}
}
pub async fn for_channel(self) -> Result<ChannelPermissions<[u32; 1]>> {
Ok(ChannelPermissions([self.calculate_channel().await?]))
}
}
pub use crate::database::*;
pub mod channel;
pub mod server;
pub mod user;
pub use user::get_relationship;
pub struct PermissionCalculator<'a> {
perspective: &'a User,
user: Option<&'a User>,
relationship: Option<&'a RelationshipStatus>,
channel: Option<&'a Channel>,
server: Option<&'a Server>,
// member: Option<&'a Member>,
has_mutual_connection: bool,
}
impl<'a> PermissionCalculator<'a> {
pub fn new(perspective: &'a User) -> PermissionCalculator {
PermissionCalculator {
perspective,
user: None,
relationship: None,
channel: None,
server: None,
// member: None,
has_mutual_connection: false,
}
}
pub fn with_user(self, user: &'a User) -> PermissionCalculator {
PermissionCalculator {
user: Some(&user),
..self
}
}
pub fn with_relationship(self, relationship: &'a RelationshipStatus) -> PermissionCalculator {
PermissionCalculator {
relationship: Some(&relationship),
..self
}
}
pub fn with_channel(self, channel: &'a Channel) -> PermissionCalculator {
PermissionCalculator {
channel: Some(&channel),
..self
}
}
pub fn with_server(self, server: &'a Server) -> PermissionCalculator {
PermissionCalculator {
server: Some(&server),
..self
}
}
/* pub fn with_member(self, member: &'a Member) -> PermissionCalculator {
PermissionCalculator {
member: Some(&member),
..self
}
} */
pub fn with_mutual_connection(self) -> PermissionCalculator<'a> {
PermissionCalculator {
has_mutual_connection: true,
..self
}
}
}
use crate::util::result::{Error, Result};
use super::PermissionCalculator;
use super::Ref;
use num_enum::TryFromPrimitive;
use std::ops;
#[derive(Debug, PartialEq, Eq, TryFromPrimitive, Copy, Clone)]
#[repr(u32)]
pub enum ServerPermission {
View = 0b00000000000000000000000000000001, // 1
ManageRoles = 0b00000000000000000000000000000010, // 2
ManageChannels = 0b00000000000000000000000000000100, // 4
ManageServer = 0b00000000000000000000000000001000, // 8
KickMembers = 0b00000000000000000000000000010000, // 16
BanMembers = 0b00000000000000000000000000100000, // 32
// 6 bits of space
ChangeNickname = 0b00000000000000000001000000000000, // 4096
ManageNicknames = 0b00000000000000000010000000000000, // 8192
ChangeAvatar = 0b00000000000000000100000000000000, // 16382
RemoveAvatars = 0b00000000000000001000000000000000, // 32768
// 16 bits of space
}
lazy_static! {
pub static ref DEFAULT_PERMISSION: u32 =
ServerPermission::View
+ ServerPermission::ChangeNickname
+ ServerPermission::ChangeAvatar;
}
impl_op_ex!(+ |a: &ServerPermission, b: &ServerPermission| -> u32 { *a as u32 | *b as u32 });
impl_op_ex_commutative!(+ |a: &u32, b: &ServerPermission| -> u32 { *a | *b as u32 });
bitfield! {
pub struct ServerPermissions(MSB0 [u32]);
u32;
pub get_view, _: 31;
pub get_manage_roles, _: 30;
pub get_manage_channels, _: 29;
pub get_manage_server, _: 28;
pub get_kick_members, _: 27;
pub get_ban_members, _: 26;
pub get_change_nickname, _: 19;
pub get_manage_nicknames, _: 18;
pub get_change_avatar, _: 17;
pub get_remove_avatars, _: 16;
}
impl<'a> PermissionCalculator<'a> {
pub async fn calculate_server(self) -> Result<u32> {
let server = if let Some(server) = self.server {
server
} else {
unreachable!()
};
if self.perspective.id == server.owner {
Ok(u32::MAX)
} else {
match Ref::from_unchecked(self.perspective.id.clone()).fetch_member(&server.id).await {
Ok(member) => {
let mut perm = server.default_permissions.0 as u32;
if let Some(roles) = member.roles {
for role in roles {
if let Some(server_role) = server.roles.get(&role) {
perm |= server_role.permissions.0 as u32;
}
}
}
Ok(perm)
}
Err(error) => {
match &error {
Error::NotFound => Ok(0),
_ => Err(error)
}
}
}
}
}
pub async fn for_server(self) -> Result<ServerPermissions<[u32; 1]>> {
Ok(ServerPermissions([self.calculate_server().await?]))
}
}
use crate::database::*;
use crate::util::result::{Error, Result};
use super::PermissionCalculator;
use mongodb::bson::doc;
use num_enum::TryFromPrimitive;
use std::ops;
#[derive(Debug, PartialEq, Eq, TryFromPrimitive, Copy, Clone)]
#[repr(u32)]
pub enum UserPermission {
Access = 0b00000000000000000000000000000001, // 1
ViewProfile = 0b00000000000000000000000000000010, // 2
SendMessage = 0b00000000000000000000000000000100, // 4
Invite = 0b00000000000000000000000000001000, // 8
}
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;
}
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 });
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
}
impl<'a> PermissionCalculator<'a> {
pub async fn calculate_user(self, target: &str) -> Result<u32> {
if &self.perspective.id == target {
return Ok(u32::MAX);
}
let mut permissions: u32 = 0;
match self
.relationship
.clone()
.map(|v| v.to_owned())
.unwrap_or_else(|| get_relationship(&self.perspective, &target))
{
RelationshipStatus::Friend | RelationshipStatus::User => return Ok(u32::MAX),
RelationshipStatus::Blocked | RelationshipStatus::BlockedOther => {
return Ok(UserPermission::Access as u32)
}
RelationshipStatus::Incoming | RelationshipStatus::Outgoing => {
permissions = UserPermission::Access as u32;
// ! INFO: if we add boolean switch for permission to
// ! message people who have mutual, we need to get
// ! rid of this return statement.
// return Ok(permissions);
}
_ => {}
}
let check_server_overlap = async || {
let server_ids = User::fetch_server_ids(&self.perspective.id).await?;
Ok(
get_collection("server_members")
.find_one(
doc! {
"_id.user": &target,
"_id.server": {
"$in": server_ids
}
},
None
)
.await
.map_err(|_| Error::DatabaseError {
operation: "find_one",
with: "server_members",
})?
.is_some()
)
};
if self.has_mutual_connection
|| check_server_overlap().await?
|| get_collection("channels")
.find_one(
doc! {
"channel_type": {
"$in": ["Group", "DirectMessage"]
},
"recipients": {
"$all": [ &self.perspective.id, target ]
}
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "find_one",
with: "channels",
})?
.is_some()
{
// ! FIXME: add privacy settings
return Ok(UserPermission::Access + UserPermission::ViewProfile);
}
Ok(permissions)
}
pub async fn for_user(self, target: &str) -> Result<UserPermissions<[u32; 1]>> {
Ok(UserPermissions([self.calculate_user(&target).await?]))
}
pub async fn for_user_given(self) -> Result<UserPermissions<[u32; 1]>> {
let id = &self.user.unwrap().id;
Ok(UserPermissions([self.calculate_user(&id).await?]))
}
}
use bson::UtcDateTime;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UserEmailVerification {
pub verified: bool,
pub target: Option<String>,
pub expiry: Option<UtcDateTime>,
pub rate_limit: Option<UtcDateTime>,
pub code: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UserRelationship {
pub id: String,
pub status: u8,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct User {
#[serde(rename = "_id")]
pub id: String,
pub email: String,
pub username: String,
pub password: String,
pub access_token: Option<String>,
pub email_verification: UserEmailVerification,
pub relations: Option<Vec<UserRelationship>>,
}
use reqwest::blocking::Client;
use std::collections::HashMap;
use std::env;
fn public_uri() -> String {
env::var("PUBLIC_URI").expect("PUBLIC_URI not in environment variables!")
}
fn portal() -> String {
env::var("PORTAL_URL").expect("PORTAL_URL not in environment variables!")
}
pub fn send_email(target: String, subject: String, body: String, html: String) -> Result<(), ()> {
let mut map = HashMap::new();
map.insert("target", target.clone());
map.insert("subject", subject);
map.insert("body", body);
map.insert("html", html);
let client = Client::new();
match client
.post(&portal())
.json(&map)
.send()
{
Ok(_) => Ok(()),
Err(_) => Err(()),
}
}
pub fn send_verification_email(email: String, code: String) -> bool {
let url = format!("{}/api/account/verify/{}", public_uri(), code);
send_email(
email,
"Verify your email!".to_string(),
format!("Verify your email here: {}", url),
format!("<a href=\"{}\">Click to verify your email!</a>", url),
)
.is_ok()
}
pub fn send_welcome_email(email: String, username: String) -> bool {
send_email(
email,
"Welcome to REVOLT!".to_string(),
format!("Welcome, {}! You can now use REVOLT.", username.clone()),
format!(
"<b>Welcome, {}!</b><br/>You can now use REVOLT.<br/><a href=\"{}\">Go to REVOLT</a>",
username.clone(),
public_uri()
),
)
.is_ok()
}
use bson::{doc, from_bson, Document};
use mongodb::options::FindOneOptions;
use rocket::http::{RawStr, Status};
use rocket::request::{self, FromParam, FromRequest, Request};
use rocket::Outcome;
use serde::{Deserialize, Serialize};
use crate::database;
use database::user::{User, UserRelationship};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UserRef {
pub id: String,
pub username: String,
pub email_verified: bool,
}
impl UserRef {
pub fn from(id: String) -> Option<UserRef> {
match database::get_collection("users").find_one(
doc! { "_id": id },
FindOneOptions::builder()
.projection(doc! {
"_id": 1,
"username": 1,
"email_verification.verified": 1,
})
.build(),
) {
Ok(result) => match result {
Some(doc) => Some(UserRef {
id: doc.get_str("_id").unwrap().to_string(),
username: doc.get_str("username").unwrap().to_string(),
email_verified: doc
.get_document("email_verification")
.unwrap()
.get_bool("verified")
.unwrap(),
}),
None => None,
},
Err(_) => None,
}
}
pub fn fetch_data(&self, projection: Document) -> Option<Document> {
database::get_collection("users")
.find_one(
doc! { "_id": &self.id },
FindOneOptions::builder().projection(projection).build(),
)
.expect("Failed to fetch user from database.")
}
pub fn fetch_relationships(&self) -> Option<Vec<UserRelationship>> {
let user = database::get_collection("users")
.find_one(
doc! { "_id": &self.id },
FindOneOptions::builder()
.projection(doc! { "relations": 1 })
.build(),
)
.expect("Failed to fetch user relationships from database.")
.expect("Missing user document.");
if let Ok(arr) = user.get_array("relations") {
let mut relationships = vec![];
for item in arr {
relationships.push(from_bson(item.clone()).unwrap());
}
Some(relationships)
} else {
None
}
}
}
#[derive(Debug)]
pub enum AuthError {
BadCount,
Missing,
Invalid,
}
impl<'a, 'r> FromRequest<'a, 'r> for UserRef {
type Error = AuthError;
fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
let keys: Vec<_> = request.headers().get("x-auth-token").collect();
match keys.len() {
0 => Outcome::Failure((Status::Forbidden, AuthError::Missing)),
1 => {
let key = keys[0];
let result = database::get_collection("users")
.find_one(
doc! { "access_token": key },
FindOneOptions::builder()
.projection(doc! {
"_id": 1,
"username": 1,
"email_verification.verified": 1,
})
.build(),
)
.unwrap();
if let Some(user) = result {
Outcome::Success(UserRef {
id: user.get_str("_id").unwrap().to_string(),
username: user.get_str("username").unwrap().to_string(),
email_verified: user
.get_document("email_verification")
.unwrap()
.get_bool("verified")
.unwrap(),
})
} else {
Outcome::Failure((Status::Forbidden, AuthError::Invalid))
}
}
_ => Outcome::Failure((Status::BadRequest, AuthError::BadCount)),
}
}
}
impl<'a, 'r> FromRequest<'a, 'r> for User {
type Error = AuthError;
fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
let keys: Vec<_> = request.headers().get("x-auth-token").collect();
match keys.len() {
0 => Outcome::Failure((Status::Forbidden, AuthError::Missing)),
1 => {
let key = keys[0];
let col = database::get_collection("users");
let result = col.find_one(doc! { "access_token": key }, None).unwrap();
if let Some(user) = result {
Outcome::Success(
from_bson(bson::Bson::Document(user)).expect("Failed to unwrap user."),
)
} else {
Outcome::Failure((Status::Forbidden, AuthError::Invalid))
}
}
_ => Outcome::Failure((Status::BadRequest, AuthError::BadCount)),
}
}
}
impl<'r> FromParam<'r> for UserRef {
type Error = &'r RawStr;
fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> {
if let Some(user) = UserRef::from(param.to_string()) {
Ok(user)
} else {
Err(param)
}
}
}
use bson::{doc, from_bson, Document};
use mongodb::options::FindOneOptions;
use rocket::http::RawStr;
use rocket::request::FromParam;
use serde::{Deserialize, Serialize};
use crate::database;
use database::channel::LastMessage;
use database::message::Message;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ChannelRef {
#[serde(rename = "_id")]
pub id: String,
#[serde(rename = "type")]
pub channel_type: u8,
pub name: Option<String>,
pub last_message: Option<LastMessage>,
// information required for permission calculations
pub recipients: Option<Vec<String>>,
pub guild: Option<String>,
pub owner: Option<String>,
}
impl ChannelRef {
pub fn from(id: String) -> Option<ChannelRef> {
match database::get_collection("channels").find_one(
doc! { "_id": id },
FindOneOptions::builder()
.projection(doc! {
"_id": 1,
"type": 1,
"name": 1,
"last_message": 1,
"recipients": 1,
"guild": 1,
"owner": 1,
})
.build(),
) {
Ok(result) => match result {
Some(doc) => {
Some(from_bson(bson::Bson::Document(doc)).expect("Failed to unwrap channel."))
}
None => None,
},
Err(_) => None,
}
}
pub fn fetch_data(&self, projection: Document) -> Option<Document> {
database::get_collection("channels")
.find_one(
doc! { "_id": &self.id },
FindOneOptions::builder().projection(projection).build(),
)
.expect("Failed to fetch channel from database.")
}
}
impl<'r> FromParam<'r> for ChannelRef {
type Error = &'r RawStr;
fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> {
if let Some(channel) = ChannelRef::from(param.to_string()) {
Ok(channel)
} else {
Err(param)
}
}
}
impl<'r> FromParam<'r> for Message {
type Error = &'r RawStr;
fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> {
let col = database::get_collection("messages");
let result = col
.find_one(doc! { "_id": param.to_string() }, None)
.unwrap();
if let Some(message) = result {
Ok(from_bson(bson::Bson::Document(message)).expect("Failed to unwrap message."))
} else {
Err(param)
}
}
}
use bson::{doc, from_bson, Bson, Document};
use mongodb::options::FindOneOptions;
use rocket::http::RawStr;
use rocket::request::FromParam;
use serde::{Deserialize, Serialize};
use crate::database;
use crate::database::guild::{Ban, Invite, Member};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct GuildRef {
#[serde(rename = "_id")]
pub id: String,
pub name: String,
pub description: String,
pub owner: String,
pub bans: Vec<Ban>,
pub default_permissions: i32,
}
impl GuildRef {
pub fn from(id: String) -> Option<GuildRef> {
match database::get_collection("guilds").find_one(
doc! { "_id": id },
FindOneOptions::builder()
.projection(doc! {
"name": 1,
"description": 1,
"owner": 1,
"bans": 1,
"default_permissions": 1
})
.build(),
) {
Ok(result) => match result {
Some(doc) => {
Some(from_bson(bson::Bson::Document(doc)).expect("Failed to unwrap guild."))
}
None => None,
},
Err(_) => None,
}
}
pub fn fetch_data(&self, projection: Document) -> Option<Document> {
database::get_collection("guilds")
.find_one(
doc! { "_id": &self.id },
FindOneOptions::builder().projection(projection).build(),
)
.expect("Failed to fetch guild from database.")
}
pub fn fetch_data_given(&self, mut filter: Document, projection: Document) -> Option<Document> {
filter.insert("_id", self.id.clone());
database::get_collection("guilds")
.find_one(
filter,
FindOneOptions::builder().projection(projection).build(),
)
.expect("Failed to fetch guild from database.")
}
}
impl<'r> FromParam<'r> for GuildRef {
type Error = &'r RawStr;
fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> {
if let Some(guild) = GuildRef::from(param.to_string()) {
Ok(guild)
} else {
Err(param)
}
}
}
pub fn get_member(guild_id: &String, member: &String) -> Option<Member> {
if let Ok(result) = database::get_collection("members").find_one(
doc! {
"_id.guild": &guild_id,
"_id.user": &member,
},
None,
) {
if let Some(doc) = result {
Some(from_bson(Bson::Document(doc)).expect("Failed to unwrap member."))
} else {
None
}
} else {
None
}
}
pub fn get_invite<U: Into<Option<String>>>(
code: &String,
user: U,
) -> Option<(String, String, Invite)> {
let mut doc = doc! {
"invites": {
"$elemMatch": {
"code": &code
}
}
};
if let Some(user_id) = user.into() {
doc.insert(
"bans",
doc! {
"$not": {
"$elemMatch": {
"id": user_id
}
}
},
);
}
if let Ok(result) = database::get_collection("guilds").find_one(
doc,
FindOneOptions::builder()
.projection(doc! {
"_id": 1,
"name": 1,
"invites.$": 1,
})
.build(),
) {
if let Some(doc) = result {
let invite = doc
.get_array("invites")
.unwrap()
.iter()
.next()
.unwrap()
.as_document()
.unwrap();
Some((
doc.get_str("_id").unwrap().to_string(),
doc.get_str("name").unwrap().to_string(),
from_bson(Bson::Document(invite.clone())).unwrap(),
))
} else {
None
}
} else {
None
}
}
pub mod auth;
pub mod channel;
pub mod guild;
This diff is collapsed.
This diff is collapsed.
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UserJoin {
pub id: String,
pub user: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UserLeave {
pub id: String,
pub user: String,
pub banned: bool,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ChannelCreate {
pub id: String,
pub channel: String,
pub name: String,
pub description: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ChannelDelete {
pub id: String,
pub channel: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Delete {
pub id: String,
}
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Create {
pub id: String,
pub nonce: Option<String>,
pub channel: String,
pub author: String,
pub content: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Edit {
pub id: String,
pub channel: String,
pub author: String,
pub content: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Delete {
pub id: String,
}
This diff is collapsed.
This diff is collapsed.