mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-12-03 22:01:50 +00:00
refactor: Check user profile visibility in web UI
This commit is contained in:
11 files changed
+113
-49
No files matched your search
@@ -1,5 +1,5 @@
|
||||
use axum::{Extension, Router, extract::State, routing::on};
|
||||
use conduwuit_service::oauth::OAuthTicket;
|
||||
use conduwuit_service::{oauth::OAuthTicket, users::ProfileQuerier};
|
||||
|
||||
use crate::{
|
||||
extract::PostForm,
|
||||
@@ -33,7 +33,8 @@ async fn route_cross_signing_reset(
|
||||
PostForm(form): PostForm<()>,
|
||||
) -> Result {
|
||||
let user_id = user.expect_recent(LoginTarget::CrossSigningReset)?;
|
||||
let user_card = UserCard::for_local_user(&services, user_id.clone()).await;
|
||||
let user_card =
|
||||
UserCard::for_local_user(&services, user_id.clone(), ProfileQuerier::Owner).await;
|
||||
|
||||
if form.is_some() {
|
||||
services
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use axum::{Extension, Router, extract::State, routing::on};
|
||||
use conduwuit_api::client::full_user_deactivate;
|
||||
use conduwuit_service::users::ProfileQuerier;
|
||||
use futures::StreamExt;
|
||||
use ruma::{OwnedRoomId, OwnedUserId, UserId};
|
||||
use tower_sessions::Session;
|
||||
@@ -65,7 +66,8 @@ async fn route_deactivate(
|
||||
PostForm(form): PostForm<DeactivateForm>,
|
||||
) -> Result {
|
||||
let user_id = user.expect_recent(LoginTarget::Deactivate)?;
|
||||
let user_card = UserCard::for_local_user(&services, user_id.clone()).await;
|
||||
let user_card =
|
||||
UserCard::for_local_user(&services, user_id.clone(), ProfileQuerier::Owner).await;
|
||||
|
||||
let body = {
|
||||
if !services.config.allow_deactivation || services.oidc.enabled() {
|
||||
|
||||
@@ -4,7 +4,9 @@ use axum::{
|
||||
routing::{get, on, post},
|
||||
};
|
||||
use conduwuit_core::warn;
|
||||
use conduwuit_service::{mailer::messages, threepid::session::ValidationSessions};
|
||||
use conduwuit_service::{
|
||||
mailer::messages, threepid::session::ValidationSessions, users::ProfileQuerier,
|
||||
};
|
||||
use lettre::{Address, message::Mailbox};
|
||||
use ruma::{ClientSecret, OwnedClientSecret, OwnedSessionId};
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -88,7 +90,7 @@ async fn route_change_email(
|
||||
let Some(form) = form else {
|
||||
return response!(ChangeEmail::new(
|
||||
context.clone(),
|
||||
UserCard::for_local_user(&services, user_id.clone()).await,
|
||||
UserCard::for_local_user(&services, user_id.clone(), ProfileQuerier::Owner).await,
|
||||
services
|
||||
.threepid
|
||||
.get_email_for_localpart(user_id.localpart())
|
||||
@@ -134,7 +136,7 @@ async fn route_change_email(
|
||||
|
||||
response!(ChangeEmailValidate::new(
|
||||
context,
|
||||
UserCard::for_local_user(&services, user_id).await,
|
||||
UserCard::for_local_user(&services, user_id.clone(), ProfileQuerier::Owner).await,
|
||||
ChangeEmailValidateBody::ValidationPending {
|
||||
session_id,
|
||||
client_secret,
|
||||
@@ -158,7 +160,8 @@ async fn get_change_email_validate(
|
||||
user: User,
|
||||
) -> Result {
|
||||
let user_id = user.expect(LoginTarget::ChangeEmail)?;
|
||||
let user_card = UserCard::for_local_user(&services, user_id.clone()).await;
|
||||
let user_card =
|
||||
UserCard::for_local_user(&services, user_id.clone(), ProfileQuerier::Owner).await;
|
||||
|
||||
if !services.threepid.email_requirement().may_change() {
|
||||
return Err(WebError::Forbidden("You may not change your email address.".to_owned()));
|
||||
@@ -198,7 +201,8 @@ async fn post_delete_email(
|
||||
user: User,
|
||||
) -> Result {
|
||||
let user_id = user.expect(LoginTarget::ChangeEmail)?;
|
||||
let user_card = UserCard::for_local_user(&services, user_id.clone()).await;
|
||||
let user_card =
|
||||
UserCard::for_local_user(&services, user_id.clone(), ProfileQuerier::Owner).await;
|
||||
|
||||
if !services.threepid.email_requirement().may_remove() {
|
||||
return Err(WebError::Forbidden("You may not remove your email address.".to_owned()));
|
||||
|
||||
@@ -7,6 +7,7 @@ use axum::{
|
||||
routing::{get, on},
|
||||
};
|
||||
use conduwuit_api::client::handle_login;
|
||||
use conduwuit_service::users::ProfileQuerier;
|
||||
use openidconnect::core::CoreAuthPrompt;
|
||||
use ruma::{
|
||||
OwnedUserId,
|
||||
@@ -125,7 +126,8 @@ async fn route_login(
|
||||
return response!(Redirect::to(&next.unwrap_or_default().target_path()));
|
||||
}
|
||||
|
||||
let user_card = UserCard::for_local_user(&services, user_id.to_owned()).await;
|
||||
let user_card =
|
||||
UserCard::for_local_user(&services, user_id.clone(), ProfileQuerier::Owner).await;
|
||||
|
||||
LoginBody::Authenticated { user_card }
|
||||
},
|
||||
|
||||
@@ -5,7 +5,7 @@ use axum::{
|
||||
routing::get,
|
||||
};
|
||||
use conduwuit_core::utils::{IterStream, ReadyExt, stream::TryExpect};
|
||||
use conduwuit_service::threepid::EmailRequirement;
|
||||
use conduwuit_service::{threepid::EmailRequirement, users::ProfileQuerier};
|
||||
use futures::StreamExt;
|
||||
use ruma::{
|
||||
OwnedClientSecret, OwnedDeviceId, OwnedSessionId,
|
||||
@@ -82,7 +82,8 @@ async fn get_account(
|
||||
) -> Result {
|
||||
let user_id = user.expect(LoginTarget::Account)?;
|
||||
|
||||
let user_card = UserCard::for_local_user(&services, user_id.clone()).await;
|
||||
let user_card =
|
||||
UserCard::for_local_user(&services, user_id.clone(), ProfileQuerier::Owner).await;
|
||||
|
||||
if services.users.is_locked(&user_id).await.unwrap() {
|
||||
return response!(Account::new(context, user_card, AccountBody::Locked));
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use axum::{Extension, Router, extract::State, routing::on};
|
||||
use conduwuit_service::users::HashedPassword;
|
||||
use conduwuit_service::users::{HashedPassword, ProfileQuerier};
|
||||
use ruma::UserId;
|
||||
use validator::{Validate, ValidationError, ValidationErrors};
|
||||
|
||||
@@ -73,7 +73,8 @@ async fn route_change_password(
|
||||
}
|
||||
|
||||
let user_id = user.expect(LoginTarget::ChangePassword)?;
|
||||
let user_card = UserCard::for_local_user(&services, user_id.clone()).await;
|
||||
let user_card =
|
||||
UserCard::for_local_user(&services, user_id.clone(), ProfileQuerier::Owner).await;
|
||||
|
||||
let body = if let Some(form) = form {
|
||||
match change_password(&services, &user_id, form).await? {
|
||||
|
||||
@@ -5,7 +5,9 @@ use axum::{
|
||||
};
|
||||
use conduwuit_core::warn;
|
||||
use conduwuit_service::{
|
||||
mailer::messages, threepid::session::ValidationSessions, users::HashedPassword,
|
||||
mailer::messages,
|
||||
threepid::session::ValidationSessions,
|
||||
users::{HashedPassword, ProfileQuerier},
|
||||
};
|
||||
use lettre::{Address, message::Mailbox};
|
||||
use ruma::{ClientSecret, OwnedClientSecret, OwnedSessionId, UserId};
|
||||
@@ -207,7 +209,8 @@ async fn route_reset_password_validate(
|
||||
return Ok(response);
|
||||
}
|
||||
|
||||
let user_card = UserCard::for_local_user(&services, user_id.clone()).await;
|
||||
let user_card =
|
||||
UserCard::for_local_user(&services, user_id.clone(), ProfileQuerier::Owner).await;
|
||||
|
||||
if let Some(form) = form {
|
||||
if let Err(err) = form.validate() {
|
||||
|
||||
@@ -2,7 +2,9 @@ use std::{collections::BTreeSet, time::SystemTime};
|
||||
|
||||
use askama::{Template, filters::HtmlSafe};
|
||||
use conduwuit_core::utils;
|
||||
use conduwuit_service::{Services, oauth::client_metadata::ClientMetadata};
|
||||
use conduwuit_service::{
|
||||
Services, oauth::client_metadata::ClientMetadata, users::ProfileQuerier,
|
||||
};
|
||||
use ruma::{
|
||||
OwnedDeviceId, OwnedUserId, UserId,
|
||||
api::{OAuthClientScope, client::device::Device},
|
||||
@@ -25,18 +27,39 @@ pub(super) struct Avatar {
|
||||
impl HtmlSafe for Avatar {}
|
||||
|
||||
impl Avatar {
|
||||
pub(super) async fn for_local_user(services: &Services, user_id: &UserId) -> Self {
|
||||
let display_name = services.users.displayname(user_id).await.ok();
|
||||
pub(super) async fn for_local_user(
|
||||
services: &Services,
|
||||
user_id: &UserId,
|
||||
querying_user: ProfileQuerier<'_>,
|
||||
) -> Self {
|
||||
let avatar_type = {
|
||||
let localpart_initial = user_id
|
||||
.localpart()
|
||||
.chars()
|
||||
.next()
|
||||
.unwrap()
|
||||
.to_ascii_uppercase();
|
||||
|
||||
let avatar_type = if services.users.avatar_url(user_id).await.is_ok() {
|
||||
AvatarType::Image(profile_media_url(user_id, "avatar_url"))
|
||||
} else if let Some(initial) = display_name
|
||||
.as_ref()
|
||||
.and_then(|display_name| display_name.chars().next())
|
||||
{
|
||||
AvatarType::Initial(initial)
|
||||
} else {
|
||||
AvatarType::Initial(user_id.localpart().chars().next().unwrap())
|
||||
if services
|
||||
.users
|
||||
.user_can_see_profile(user_id, querying_user)
|
||||
.await
|
||||
{
|
||||
let display_name = services.users.displayname(user_id).await.ok();
|
||||
|
||||
if services.users.avatar_url(user_id).await.is_ok() {
|
||||
AvatarType::Image(profile_media_url(user_id, "avatar_url"))
|
||||
} else if let Some(initial) = display_name
|
||||
.as_ref()
|
||||
.and_then(|display_name| display_name.chars().next())
|
||||
{
|
||||
AvatarType::Initial(initial)
|
||||
} else {
|
||||
AvatarType::Initial(localpart_initial)
|
||||
}
|
||||
} else {
|
||||
AvatarType::Initial(localpart_initial)
|
||||
}
|
||||
};
|
||||
|
||||
Self { avatar_type }
|
||||
@@ -97,14 +120,30 @@ impl HtmlSafe for UserCard {}
|
||||
impl UserCard {
|
||||
const BANNER_KEY: &str = "chat.commet.profile_banner";
|
||||
|
||||
pub(super) async fn for_local_user(services: &Services, user_id: OwnedUserId) -> Self {
|
||||
let display_name = services.users.displayname(&user_id).await.ok();
|
||||
let avatar = Avatar::for_local_user(services, &user_id).await;
|
||||
let banner = if services
|
||||
pub(super) async fn for_local_user(
|
||||
services: &Services,
|
||||
user_id: OwnedUserId,
|
||||
querying_user: ProfileQuerier<'_>,
|
||||
) -> Self {
|
||||
let can_see_profile = services
|
||||
.users
|
||||
.get_local_profile_field(&user_id, Self::BANNER_KEY.into())
|
||||
.await
|
||||
.is_some()
|
||||
.user_can_see_profile(&user_id, querying_user)
|
||||
.await;
|
||||
|
||||
let avatar = Avatar::for_local_user(services, &user_id, querying_user).await;
|
||||
|
||||
let display_name = if can_see_profile {
|
||||
services.users.displayname(&user_id).await.ok()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let banner = if can_see_profile
|
||||
&& services
|
||||
.users
|
||||
.get_local_profile_field(&user_id, Self::BANNER_KEY.into())
|
||||
.await
|
||||
.is_some()
|
||||
{
|
||||
Some(profile_media_url(&user_id, Self::BANNER_KEY))
|
||||
} else {
|
||||
|
||||
@@ -25,13 +25,14 @@ async fn get_profile_media(
|
||||
State(services): State<crate::State>,
|
||||
Expect(Path(ProfileMediaPath { user_id, field })): Expect<Path<ProfileMediaPath>>,
|
||||
headers: HeaderMap,
|
||||
requesting_user: User<true>,
|
||||
querying_user: User<true>,
|
||||
) -> Result {
|
||||
if requesting_user
|
||||
.into_session()
|
||||
.map(|session| session.user_id)
|
||||
.as_ref()
|
||||
!= Some(&user_id)
|
||||
let querying_user = querying_user.into_session().map(|session| session.user_id);
|
||||
|
||||
if !services
|
||||
.users
|
||||
.user_can_see_profile(&user_id, querying_user.as_deref().into())
|
||||
.await
|
||||
{
|
||||
return Err(WebError::Forbidden(
|
||||
"You may not access this user's profile media.".to_owned(),
|
||||
|
||||
@@ -4,10 +4,13 @@ use axum::{
|
||||
response::Redirect,
|
||||
routing::on,
|
||||
};
|
||||
use conduwuit_service::oauth::{
|
||||
SessionInfo,
|
||||
client_metadata::ClientMetadata,
|
||||
grant::{AuthorizationCodeQuery, DeviceCodeVerifyQuery, Prompt, RequestedScopes},
|
||||
use conduwuit_service::{
|
||||
oauth::{
|
||||
SessionInfo,
|
||||
client_metadata::ClientMetadata,
|
||||
grant::{AuthorizationCodeQuery, DeviceCodeVerifyQuery, Prompt, RequestedScopes},
|
||||
},
|
||||
users::ProfileQuerier,
|
||||
};
|
||||
use ruma::{OwnedUserId, api::OAuthClientScope};
|
||||
use serde::{Deserialize, de::IgnoredAny};
|
||||
@@ -122,7 +125,7 @@ async fn route_authorization_code(
|
||||
scopes.retain(|scope| !existing_scopes.contains(scope));
|
||||
}
|
||||
|
||||
let user_avatar = Avatar::for_local_user(&services, &user_id).await;
|
||||
let user_avatar = Avatar::for_local_user(&services, &user_id, ProfileQuerier::Owner).await;
|
||||
|
||||
response!(Grant::new(
|
||||
context,
|
||||
@@ -189,7 +192,8 @@ async fn route_device_code(
|
||||
return response!(Redirect::to(&uri));
|
||||
};
|
||||
|
||||
let user_card = UserCard::for_local_user(&services, user_id.clone()).await;
|
||||
let user_card =
|
||||
UserCard::for_local_user(&services, user_id.clone(), ProfileQuerier::Owner).await;
|
||||
|
||||
match (form, query.user_code.clone()) {
|
||||
| (None, Some(user_code)) | (Some(DeviceCodeForm::Lookup { user_code }), _) => {
|
||||
@@ -220,7 +224,8 @@ async fn route_device_code(
|
||||
scopes.retain(|scope| !existing_scopes.contains(scope));
|
||||
}
|
||||
|
||||
let user_avatar = Avatar::for_local_user(&services, &user_id).await;
|
||||
let user_avatar =
|
||||
Avatar::for_local_user(&services, &user_id, ProfileQuerier::Owner).await;
|
||||
|
||||
response!(Grant::new(
|
||||
context,
|
||||
|
||||
@@ -10,6 +10,7 @@ use conduwuit_api::client_ip::ClientIp;
|
||||
use conduwuit_service::{
|
||||
oauth::grant::AuthorizationCodeResponse,
|
||||
oidc::{ClaimedLocalUser, SessionCompletionStatus},
|
||||
users::ProfileQuerier,
|
||||
};
|
||||
use futures::FutureExt;
|
||||
use ruma::OwnedServerName;
|
||||
@@ -119,8 +120,12 @@ async fn route_complete(
|
||||
.await
|
||||
{
|
||||
| Ok(ClaimedLocalUser::Existing(user_id)) => {
|
||||
let user_card =
|
||||
UserCard::for_local_user(&services, user_id.clone()).await;
|
||||
let user_card = UserCard::for_local_user(
|
||||
&services,
|
||||
user_id.clone(),
|
||||
ProfileQuerier::Owner,
|
||||
)
|
||||
.await;
|
||||
|
||||
if let Some(password) = form.password {
|
||||
if services
|
||||
|
||||
Reference in new issue
Block a user