fix: user follows, user blocks, private profile setting
This commit is contained in:
parent
de53eec0e8
commit
17564ede49
8 changed files with 220 additions and 43 deletions
|
@ -22,10 +22,13 @@ version = "1.0.0"
|
||||||
"auth:action.logout" = "Logout"
|
"auth:action.logout" = "Logout"
|
||||||
"auto:action.follow" = "Follow"
|
"auto:action.follow" = "Follow"
|
||||||
"auto:action.unfollow" = "Unfollow"
|
"auto:action.unfollow" = "Unfollow"
|
||||||
|
"auto:action.block" = "Block"
|
||||||
|
"auto:action.unblock" = "Unblock"
|
||||||
"auth:link.my_profile" = "My profile"
|
"auth:link.my_profile" = "My profile"
|
||||||
"auth:link.settings" = "Settings"
|
"auth:link.settings" = "Settings"
|
||||||
"auth:label.followers" = "Followers"
|
"auth:label.followers" = "Followers"
|
||||||
"auth:label.following" = "Following"
|
"auth:label.following" = "Following"
|
||||||
|
"auth:label.relationship" = "Relationship"
|
||||||
"auth:label.joined_communities" = "Joined communities"
|
"auth:label.joined_communities" = "Joined communities"
|
||||||
"auth:label.recent_posts" = "Recent posts"
|
"auth:label.recent_posts" = "Recent posts"
|
||||||
|
|
||||||
|
|
|
@ -85,7 +85,7 @@ main {
|
||||||
}
|
}
|
||||||
|
|
||||||
article {
|
article {
|
||||||
margin-top: 1rem;
|
margin: 1rem 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@media screen and (max-width: 900px) {
|
@media screen and (max-width: 900px) {
|
||||||
|
|
|
@ -25,9 +25,24 @@
|
||||||
|
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<select onchange="save_access(event, 'read')">
|
<select onchange="save_access(event, 'read')">
|
||||||
<option value="Everybody">Everybody</option>
|
<option
|
||||||
<option value="Unlisted">Unlisted</option>
|
value="Everybody"
|
||||||
<option value="Private">Private</option>
|
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>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -39,9 +54,24 @@
|
||||||
|
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<select onchange="save_access(event, 'write')">
|
<select onchange="save_access(event, 'write')">
|
||||||
<option value="Everybody">Everybody</option>
|
<option
|
||||||
<option value="Joined">Joined</option>
|
value="Everybody"
|
||||||
<option value="Owner">Owner only</option>
|
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>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -31,21 +31,23 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="card flex" id="social">
|
<div class="card flex flex-col gap-2" id="social">
|
||||||
<a
|
<div class="w-full flex">
|
||||||
href="/user/{{ profile.username }}/followers"
|
<a
|
||||||
class="w-full flex justify-center items-center gap-2"
|
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>
|
<h4>{{ profile.follower_count }}</h4>
|
||||||
</a>
|
<span>{{ text "auth:label.followers" }}</span>
|
||||||
<a
|
</a>
|
||||||
href="/user/{{ profile.username }}/following"
|
<a
|
||||||
class="w-full flex justify-center items-center gap-2"
|
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>
|
<h4>{{ profile.following_count }}</h4>
|
||||||
</a>
|
<span>{{ text "auth:label.following" }}</span>
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@ -73,6 +75,90 @@
|
||||||
</div>
|
</div>
|
||||||
</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-nest">
|
||||||
<div class="card small flex gap-2 items-center">
|
<div class="card small flex gap-2 items-center">
|
||||||
{{ icon "users-round" }}
|
{{ icon "users-round" }}
|
||||||
|
|
|
@ -34,6 +34,10 @@ media_theme_pref();
|
||||||
(() => {
|
(() => {
|
||||||
const self = reg_ns("atto");
|
const self = reg_ns("atto");
|
||||||
|
|
||||||
|
for (const element of document.querySelectorAll('[selected="false"]')) {
|
||||||
|
element.removeAttribute("selected");
|
||||||
|
}
|
||||||
|
|
||||||
// init
|
// init
|
||||||
use("me", () => {});
|
use("me", () => {});
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ use crate::{
|
||||||
};
|
};
|
||||||
use axum::{Extension, Json, extract::Path, response::IntoResponse};
|
use axum::{Extension, Json, extract::Path, response::IntoResponse};
|
||||||
use axum_extra::extract::CookieJar;
|
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.
|
/// Toggle following on the given user.
|
||||||
pub async fn follow_request(
|
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 {
|
if let Ok(userfollow) = data.get_userfollow_by_initiator_receiver(user.id, id).await {
|
||||||
// delete
|
// delete
|
||||||
match data.delete_userfollow(userfollow.id, user).await {
|
match data.delete_userfollow(userfollow.id, &user).await {
|
||||||
Ok(_) => Json(ApiReturn {
|
Ok(_) => Json(ApiReturn {
|
||||||
ok: true,
|
ok: true,
|
||||||
message: "User unfollowed".to_string(),
|
message: "User unfollowed".to_string(),
|
||||||
|
@ -31,11 +31,27 @@ pub async fn follow_request(
|
||||||
} else {
|
} else {
|
||||||
// create
|
// create
|
||||||
match data.create_userfollow(UserFollow::new(user.id, id)).await {
|
match data.create_userfollow(UserFollow::new(user.id, id)).await {
|
||||||
Ok(_) => Json(ApiReturn {
|
Ok(_) => {
|
||||||
ok: true,
|
if let Err(e) = data
|
||||||
message: "User followed".to_string(),
|
.create_notification(Notification::new(
|
||||||
payload: (),
|
"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()),
|
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
|
if let Ok(userfollow) = data.get_userfollow_by_initiator_receiver(user.id, id).await
|
||||||
{
|
{
|
||||||
// automatically unfollow
|
// automatically unfollow
|
||||||
match data.delete_userfollow(userfollow.id, user).await {
|
match data.delete_userfollow(userfollow.id, &user).await {
|
||||||
Ok(_) => Json(ApiReturn {
|
Ok(_) => Json(ApiReturn {
|
||||||
ok: true,
|
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: (),
|
payload: (),
|
||||||
}),
|
}),
|
||||||
Err(e) => Json(e.into()),
|
Err(e) => Json(e.into()),
|
||||||
|
|
|
@ -55,11 +55,15 @@ pub fn profile_context(
|
||||||
communities: &Vec<Community>,
|
communities: &Vec<Community>,
|
||||||
is_self: bool,
|
is_self: bool,
|
||||||
is_following: bool,
|
is_following: bool,
|
||||||
|
is_following_you: bool,
|
||||||
|
is_blocking: bool,
|
||||||
) {
|
) {
|
||||||
context.insert("profile", &profile);
|
context.insert("profile", &profile);
|
||||||
context.insert("communities", &communities);
|
context.insert("communities", &communities);
|
||||||
context.insert("is_self", &is_self);
|
context.insert("is_self", &is_self);
|
||||||
context.insert("is_following", &is_following);
|
context.insert("is_following", &is_following);
|
||||||
|
context.insert("is_following_you", &is_following_you);
|
||||||
|
context.insert("is_blocking", &is_blocking);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `/user/{username}`
|
/// `/user/{username}`
|
||||||
|
@ -94,15 +98,17 @@ pub async fn posts_request(
|
||||||
// check for private profile
|
// check for private profile
|
||||||
if other_user.settings.private_profile {
|
if other_user.settings.private_profile {
|
||||||
if let Some(ref ua) = user {
|
if let Some(ref ua) = user {
|
||||||
if data
|
if ua.id != other_user.id {
|
||||||
.0
|
if data
|
||||||
.get_userfollow_by_initiator_receiver(other_user.id, ua.id)
|
.0
|
||||||
.await
|
.get_userfollow_by_initiator_receiver(other_user.id, ua.id)
|
||||||
.is_err()
|
.await
|
||||||
{
|
.is_err()
|
||||||
return Err(Html(
|
{
|
||||||
render_error(Error::NotAllowed, &jar, &data, &user).await,
|
return Err(Html(
|
||||||
));
|
render_error(Error::NotAllowed, &jar, &data, &user).await,
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return Err(Html(
|
return Err(Html(
|
||||||
|
@ -151,6 +157,24 @@ pub async fn posts_request(
|
||||||
false
|
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);
|
context.insert("posts", &posts);
|
||||||
profile_context(
|
profile_context(
|
||||||
&mut context,
|
&mut context,
|
||||||
|
@ -158,6 +182,8 @@ pub async fn posts_request(
|
||||||
&communities,
|
&communities,
|
||||||
is_self,
|
is_self,
|
||||||
is_following,
|
is_following,
|
||||||
|
is_following_you,
|
||||||
|
is_blocking,
|
||||||
);
|
);
|
||||||
|
|
||||||
// return
|
// return
|
||||||
|
|
|
@ -181,7 +181,7 @@ impl DataManager {
|
||||||
Ok(())
|
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?;
|
let follow = self.get_userfollow_by_id(id).await?;
|
||||||
|
|
||||||
if (user.id != follow.initiator) && (user.id != follow.receiver) {
|
if (user.id != follow.initiator) && (user.id != follow.receiver) {
|
||||||
|
@ -208,11 +208,11 @@ impl DataManager {
|
||||||
self.2.remove(format!("atto.userfollow:{}", id)).await;
|
self.2.remove(format!("atto.userfollow:{}", id)).await;
|
||||||
|
|
||||||
// decr counts
|
// decr counts
|
||||||
self.incr_user_following_count(follow.initiator)
|
self.decr_user_following_count(follow.initiator)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
self.incr_user_follower_count(follow.receiver)
|
self.decr_user_follower_count(follow.receiver)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue