fix: user follows, user blocks, private profile setting

This commit is contained in:
trisua 2025-03-31 20:02:09 -04:00
parent de53eec0e8
commit 17564ede49
8 changed files with 220 additions and 43 deletions

View file

@ -22,10 +22,13 @@ version = "1.0.0"
"auth:action.logout" = "Logout"
"auto:action.follow" = "Follow"
"auto:action.unfollow" = "Unfollow"
"auto:action.block" = "Block"
"auto:action.unblock" = "Unblock"
"auth:link.my_profile" = "My profile"
"auth:link.settings" = "Settings"
"auth:label.followers" = "Followers"
"auth:label.following" = "Following"
"auth:label.relationship" = "Relationship"
"auth:label.joined_communities" = "Joined communities"
"auth:label.recent_posts" = "Recent posts"

View file

@ -85,7 +85,7 @@ main {
}
article {
margin-top: 1rem;
margin: 1rem 0;
}
@media screen and (max-width: 900px) {

View file

@ -25,9 +25,24 @@
<div class="card">
<select onchange="save_access(event, 'read')">
<option value="Everybody">Everybody</option>
<option value="Unlisted">Unlisted</option>
<option value="Private">Private</option>
<option
value="Everybody"
selected="{% if community.read_access == 'Everybody' %}true{% else %}false{% endif %}"
>
Everybody
</option>
<option
value="Unlisted"
selected="{% if community.read_access == 'Unlisted' %}true{% else %}false{% endif %}"
>
Unlisted
</option>
<option
value="Private"
selected="{% if community.read_access == 'Private' %}true{% else %}false{% endif %}"
>
Private
</option>
</select>
</div>
</div>
@ -39,9 +54,24 @@
<div class="card">
<select onchange="save_access(event, 'write')">
<option value="Everybody">Everybody</option>
<option value="Joined">Joined</option>
<option value="Owner">Owner only</option>
<option
value="Everybody"
selected="{% if community.write_access == 'Everybody' %}true{% else %}false{% endif %}"
>
Everybody
</option>
<option
value="Joined"
selected="{% if community.write_access == 'Joined' %}true{% else %}false{% endif %}"
>
Joined
</option>
<option
value="Owner"
selected="{% if community.write_access == 'Owner' %}true{% else %}false{% endif %}"
>
Owner only
</option>
</select>
</div>
</div>

View file

@ -31,21 +31,23 @@
</div>
</div>
<div class="card flex" id="social">
<a
href="/user/{{ profile.username }}/followers"
class="w-full flex justify-center items-center gap-2"
>
<h4>{{ profile.follower_count }}</h4>
<span>{{ text "auth:label.followers" }}</span>
</a>
<a
href="/user/{{ profile.username }}/following"
class="w-full flex justify-center items-center gap-2"
>
<h4>{{ profile.following_count }}</h4>
<span>{{ text "auth:label.following" }}</span>
</a>
<div class="card flex flex-col gap-2" id="social">
<div class="w-full flex">
<a
href="/user/{{ profile.username }}/followers"
class="w-full flex justify-center items-center gap-2"
>
<h4>{{ profile.follower_count }}</h4>
<span>{{ text "auth:label.followers" }}</span>
</a>
<a
href="/user/{{ profile.username }}/following"
class="w-full flex justify-center items-center gap-2"
>
<h4>{{ profile.following_count }}</h4>
<span>{{ text "auth:label.following" }}</span>
</a>
</div>
</div>
</div>
@ -73,6 +75,90 @@
</div>
</div>
{% if not is_self %}
<div class="card-nest">
<div class="card small">
<b>{{ text "auth:label.relationship" }}</b>
</div>
<div class="card flex gap-2 flex-wrap">
{% if not is_blocking %} {% if not is_following %}
<button onclick="toggle_follow_user()">
{{ icon "user-plus" }}
<span>{{ text "auto:action.follow" }}</span>
</button>
{% else %}
<button
onclick="toggle_follow_user()"
class="quaternary red"
>
{{ icon "user-minus" }}
<span>{{ text "auto:action.unfollow" }}</span>
</button>
{% endif %}
<button
onclick="toggle_block_user()"
class="quaternary red"
>
{{ icon "shield" }}
<span>{{ text "auto:action.block" }}</span>
</button>
{% else %}
<button
onclick="toggle_block_user()"
class="quaternary red"
>
{{ icon "shield-off" }}
<span>{{ text "auto:action.unblock" }}</span>
</button>
{% endif %}
<script>
globalThis.toggle_follow_user = () => {
fetch(
"/api/v1/auth/profile/{{ profile.id }}/follow",
{
method: "POST",
},
)
.then((res) => res.json())
.then((res) => {
trigger("atto::toast", [
res.ok ? "success" : "error",
res.message,
]);
});
};
globalThis.toggle_block_user = async () => {
if (
!(await trigger("atto::confirm", [
"Are you sure you would like to do this?",
]))
) {
return;
}
fetch(
"/api/v1/auth/profile/{{ profile.id }}/block",
{
method: "POST",
},
)
.then((res) => res.json())
.then((res) => {
trigger("atto::toast", [
res.ok ? "success" : "error",
res.message,
]);
});
};
</script>
</div>
</div>
{% endif %}
<div class="card-nest">
<div class="card small flex gap-2 items-center">
{{ icon "users-round" }}

View file

@ -34,6 +34,10 @@ media_theme_pref();
(() => {
const self = reg_ns("atto");
for (const element of document.querySelectorAll('[selected="false"]')) {
element.removeAttribute("selected");
}
// init
use("me", () => {});

View file

@ -4,7 +4,7 @@ use crate::{
};
use axum::{Extension, Json, extract::Path, response::IntoResponse};
use axum_extra::extract::CookieJar;
use tetratto_core::model::auth::{UserBlock, UserFollow};
use tetratto_core::model::auth::{Notification, UserBlock, UserFollow};
/// Toggle following on the given user.
pub async fn follow_request(
@ -20,7 +20,7 @@ pub async fn follow_request(
if let Ok(userfollow) = data.get_userfollow_by_initiator_receiver(user.id, id).await {
// delete
match data.delete_userfollow(userfollow.id, user).await {
match data.delete_userfollow(userfollow.id, &user).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "User unfollowed".to_string(),
@ -31,11 +31,27 @@ pub async fn follow_request(
} else {
// create
match data.create_userfollow(UserFollow::new(user.id, id)).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "User followed".to_string(),
payload: (),
}),
Ok(_) => {
if let Err(e) = data
.create_notification(Notification::new(
"Somebody has followed you!".to_string(),
format!(
"You have been followed by [@{}](/api/v1/auth/profile/find/{}).",
user.username, user.id
),
id,
))
.await
{
return Json(e.into());
};
Json(ApiReturn {
ok: true,
message: "User followed".to_string(),
payload: (),
})
}
Err(e) => Json(e.into()),
}
}
@ -70,10 +86,22 @@ pub async fn block_request(
if let Ok(userfollow) = data.get_userfollow_by_initiator_receiver(user.id, id).await
{
// automatically unfollow
match data.delete_userfollow(userfollow.id, user).await {
match data.delete_userfollow(userfollow.id, &user).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "User unfollowed".to_string(),
message: "User blocked".to_string(),
payload: (),
}),
Err(e) => Json(e.into()),
}
} else if let Ok(userfollow) =
data.get_userfollow_by_receiver_initiator(user.id, id).await
{
// automatically unfollow
match data.delete_userfollow(userfollow.id, &user).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "User blocked".to_string(),
payload: (),
}),
Err(e) => Json(e.into()),

View file

@ -55,11 +55,15 @@ pub fn profile_context(
communities: &Vec<Community>,
is_self: bool,
is_following: bool,
is_following_you: bool,
is_blocking: bool,
) {
context.insert("profile", &profile);
context.insert("communities", &communities);
context.insert("is_self", &is_self);
context.insert("is_following", &is_following);
context.insert("is_following_you", &is_following_you);
context.insert("is_blocking", &is_blocking);
}
/// `/user/{username}`
@ -94,15 +98,17 @@ pub async fn posts_request(
// check for private profile
if other_user.settings.private_profile {
if let Some(ref ua) = user {
if data
.0
.get_userfollow_by_initiator_receiver(other_user.id, ua.id)
.await
.is_err()
{
return Err(Html(
render_error(Error::NotAllowed, &jar, &data, &user).await,
));
if ua.id != other_user.id {
if data
.0
.get_userfollow_by_initiator_receiver(other_user.id, ua.id)
.await
.is_err()
{
return Err(Html(
render_error(Error::NotAllowed, &jar, &data, &user).await,
));
}
}
} else {
return Err(Html(
@ -151,6 +157,24 @@ pub async fn posts_request(
false
};
let is_following_you = if let Some(ref ua) = user {
data.0
.get_userfollow_by_receiver_initiator(ua.id, other_user.id)
.await
.is_ok()
} else {
false
};
let is_blocking = if let Some(ref ua) = user {
data.0
.get_userblock_by_initiator_receiver(ua.id, other_user.id)
.await
.is_ok()
} else {
false
};
context.insert("posts", &posts);
profile_context(
&mut context,
@ -158,6 +182,8 @@ pub async fn posts_request(
&communities,
is_self,
is_following,
is_following_you,
is_blocking,
);
// return

View file

@ -181,7 +181,7 @@ impl DataManager {
Ok(())
}
pub async fn delete_userfollow(&self, id: usize, user: User) -> Result<()> {
pub async fn delete_userfollow(&self, id: usize, user: &User) -> Result<()> {
let follow = self.get_userfollow_by_id(id).await?;
if (user.id != follow.initiator) && (user.id != follow.receiver) {
@ -208,11 +208,11 @@ impl DataManager {
self.2.remove(format!("atto.userfollow:{}", id)).await;
// decr counts
self.incr_user_following_count(follow.initiator)
self.decr_user_following_count(follow.initiator)
.await
.unwrap();
self.incr_user_follower_count(follow.receiver)
self.decr_user_follower_count(follow.receiver)
.await
.unwrap();