mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 13:36:59 +00:00
feat(core): provide user profile where appropriate
This commit is contained in:
@@ -60,7 +60,7 @@ impl AbstractMessages for ReferenceDb {
|
||||
.cloned()
|
||||
.collect();
|
||||
|
||||
// TODO: sorting, etc
|
||||
// TODO: sorting, etc (will be required for tests)
|
||||
|
||||
Ok(matched_messages)
|
||||
|
||||
|
||||
@@ -391,14 +391,14 @@ impl User {
|
||||
|
||||
EventV1::UserRelationship {
|
||||
id: target.id.clone(),
|
||||
user: self.clone().into(Some(&*target)).await,
|
||||
user: self.clone().into(db, Some(&*target)).await,
|
||||
}
|
||||
.private(target.id.clone())
|
||||
.await;
|
||||
|
||||
EventV1::UserRelationship {
|
||||
id: self.id.clone(),
|
||||
user: target.clone().into(Some(&*self)).await,
|
||||
user: target.clone().into(db, Some(&*self)).await,
|
||||
}
|
||||
.private(self.id.clone())
|
||||
.await;
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
use revolt_models::v0::*;
|
||||
use revolt_permissions::{calculate_user_permissions, UserPermission};
|
||||
|
||||
use crate::{util::permissions::DatabasePermissionQuery, Database};
|
||||
|
||||
impl crate::Bot {
|
||||
pub fn into_public_bot(self, user: crate::User) -> PublicBot {
|
||||
@@ -635,43 +638,69 @@ impl From<crate::FieldsRole> for FieldsRole {
|
||||
}
|
||||
|
||||
impl crate::User {
|
||||
pub async fn into<'a, P>(self, perspective: P) -> User
|
||||
pub async fn into<'a, P>(self, db: &Database, perspective: P) -> User
|
||||
where
|
||||
P: Into<Option<&'a crate::User>>,
|
||||
{
|
||||
let relationship = if let Some(perspective) = perspective.into() {
|
||||
let perspective = perspective.into();
|
||||
let (relationship, can_see_profile) = if self.bot.is_some() {
|
||||
(RelationshipStatus::None, true)
|
||||
} else if let Some(perspective) = perspective {
|
||||
let mut query = DatabasePermissionQuery::new(db, perspective).user(&self);
|
||||
|
||||
if perspective.id == self.id {
|
||||
RelationshipStatus::User
|
||||
(RelationshipStatus::User, true)
|
||||
} else {
|
||||
perspective
|
||||
.relations
|
||||
.as_ref()
|
||||
.map(|relations| {
|
||||
relations
|
||||
.iter()
|
||||
.find(|relationship| relationship.id == self.id)
|
||||
.map(|relationship| relationship.status.clone().into())
|
||||
.unwrap_or_default()
|
||||
})
|
||||
.unwrap_or_default()
|
||||
(
|
||||
perspective
|
||||
.relations
|
||||
.as_ref()
|
||||
.map(|relations| {
|
||||
relations
|
||||
.iter()
|
||||
.find(|relationship| relationship.id == self.id)
|
||||
.map(|relationship| relationship.status.clone().into())
|
||||
.unwrap_or_default()
|
||||
})
|
||||
.unwrap_or_default(),
|
||||
calculate_user_permissions(&mut query)
|
||||
.await
|
||||
.has_user_permission(UserPermission::ViewProfile),
|
||||
)
|
||||
}
|
||||
} else {
|
||||
RelationshipStatus::None
|
||||
(RelationshipStatus::None, false)
|
||||
};
|
||||
|
||||
// do permission stuff here
|
||||
// TODO: implement permissions =)
|
||||
let can_see_profile = false;
|
||||
|
||||
User {
|
||||
username: self.username,
|
||||
discriminator: self.discriminator,
|
||||
display_name: self.display_name,
|
||||
avatar: self.avatar.map(|file| file.into()),
|
||||
relations: vec![],
|
||||
relations: if let Some(crate::User { id, .. }) = perspective {
|
||||
if id == &self.id {
|
||||
self.relations
|
||||
.unwrap_or_default()
|
||||
.into_iter()
|
||||
.map(|relation| relation.into())
|
||||
.collect()
|
||||
} else {
|
||||
vec![]
|
||||
}
|
||||
} else {
|
||||
vec![]
|
||||
},
|
||||
badges: self.badges.unwrap_or_default() as u32,
|
||||
status: None,
|
||||
profile: None,
|
||||
status: if can_see_profile {
|
||||
self.status.map(|status| status.into())
|
||||
} else {
|
||||
None
|
||||
},
|
||||
profile: if can_see_profile {
|
||||
self.profile.map(|profile| profile.into())
|
||||
} else {
|
||||
None
|
||||
},
|
||||
flags: self.flags.unwrap_or_default() as u32,
|
||||
privileged: self.privileged,
|
||||
bot: self.bot.map(|bot| bot.into()),
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
use revolt_permissions::{
|
||||
calculate_user_permissions, ChannelType, Override, PermissionQuery, RelationshipStatus,
|
||||
calculate_user_permissions, ChannelType, Override, PermissionQuery, PermissionValue,
|
||||
RelationshipStatus,
|
||||
};
|
||||
|
||||
use crate::{Channel, Database, Member, Server, User};
|
||||
@@ -19,7 +20,8 @@ pub struct DatabasePermissionQuery<'a> {
|
||||
member: Option<Cow<'a, Member>>,
|
||||
|
||||
// flag_known_relationship: Option<&'a RelationshipStatus>,
|
||||
cached_user_permission: Option<u32>,
|
||||
cached_user_permission: Option<PermissionValue>,
|
||||
cached_mutual_connection: Option<bool>,
|
||||
cached_permission: Option<u64>,
|
||||
}
|
||||
|
||||
@@ -49,6 +51,10 @@ impl PermissionQuery for DatabasePermissionQuery<'_> {
|
||||
/// Get the relationship with have with the currently selected user
|
||||
async fn user_relationship(&mut self) -> RelationshipStatus {
|
||||
if let Some(other_user) = &self.user {
|
||||
if self.perspective.id == other_user.id {
|
||||
return RelationshipStatus::User;
|
||||
}
|
||||
|
||||
if let Some(relations) = &self.perspective.relations {
|
||||
for entry in relations {
|
||||
if entry.id == other_user.id {
|
||||
@@ -82,14 +88,17 @@ impl PermissionQuery for DatabasePermissionQuery<'_> {
|
||||
|
||||
/// Do we have a mutual connection with the currently selected user?
|
||||
async fn have_mutual_connection(&mut self) -> bool {
|
||||
if let Some(user) = &self.user {
|
||||
// TODO: cache result?
|
||||
matches!(
|
||||
self.perspective
|
||||
.has_mutual_connection(self.database, &user.id)
|
||||
.await,
|
||||
Ok(true)
|
||||
)
|
||||
if let Some(value) = self.cached_mutual_connection {
|
||||
value
|
||||
} else if let Some(user) = &self.user {
|
||||
let value = self
|
||||
.perspective
|
||||
.has_mutual_connection(self.database, &user.id)
|
||||
.await
|
||||
.unwrap_or_default();
|
||||
|
||||
self.cached_mutual_connection = Some(value);
|
||||
matches!(value, true)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
@@ -352,6 +361,7 @@ impl<'a> DatabasePermissionQuery<'a> {
|
||||
server: None,
|
||||
member: None,
|
||||
|
||||
cached_mutual_connection: None,
|
||||
cached_user_permission: None,
|
||||
cached_permission: None,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user