add: don't allow poll creator to vote

add: unfollow user when you block them
add: force other user to unfollow you by blocking them
add: leave receiver communities when you block them
This commit is contained in:
trisua 2025-06-05 16:23:57 -04:00
parent 5a330b7a18
commit 460e87e90e
11 changed files with 165 additions and 17 deletions

View file

@ -292,6 +292,32 @@ impl DataManager {
Ok(())
}
/// Delete a membership given its `id`
pub async fn delete_membership_force(&self, id: usize) -> Result<()> {
let y = self.get_membership_by_id(id).await?;
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = execute!(
&conn,
"DELETE FROM memberships WHERE id = $1",
&[&(id as i64)]
);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
}
self.2.remove(format!("atto.membership:{}", id)).await;
self.decr_community_member_count(y.community).await.unwrap();
Ok(())
}
/// Update a membership's role given its `id`
pub async fn update_membership_role(
&self,

View file

@ -237,11 +237,14 @@ impl DataManager {
}
/// Get the poll of the given post (if some).
///
/// # Returns
/// `Result<Option<(poll, voted, expired)>>`
pub async fn get_post_poll(
&self,
post: &Post,
user: &Option<User>,
) -> Result<Option<(Poll, bool)>> {
) -> Result<Option<(Poll, bool, bool)>> {
let user = if let Some(ua) = user {
ua
} else {
@ -250,12 +253,16 @@ impl DataManager {
if post.poll_id != 0 {
Ok(Some(match self.get_poll_by_id(post.poll_id).await {
Ok(p) => (
p,
self.get_pollvote_by_owner_poll(user.id, post.poll_id)
.await
.is_ok(),
),
Ok(p) => {
let expired = (unix_epoch_timestamp() as usize) - p.created > p.expires;
(
p,
self.get_pollvote_by_owner_poll(user.id, post.poll_id)
.await
.is_ok(),
expired,
)
}
Err(_) => return Err(Error::MiscError("Invalid poll ID attached".to_string())),
}))
} else {
@ -275,7 +282,7 @@ impl DataManager {
User,
Option<(User, Post)>,
Option<(Question, User)>,
Option<(Poll, bool)>,
Option<(Poll, bool, bool)>,
)>,
> {
let mut out = Vec::new();
@ -374,7 +381,7 @@ impl DataManager {
Community,
Option<(User, Post)>,
Option<(Question, User)>,
Option<(Poll, bool)>,
Option<(Poll, bool, bool)>,
)>,
> {
let mut out = Vec::new();

View file

@ -51,7 +51,7 @@ impl DataManager {
Community,
Option<(User, Post)>,
Option<(Question, User)>,
Option<(Poll, bool)>,
Option<(Poll, bool, bool)>,
)>,
> {
let stack = self.get_stack_by_id(id).await?;

View file

@ -177,6 +177,10 @@ impl DataManager {
/// # Arguments
/// * `data` - a mock [`UserBlock`] object to insert
pub async fn create_userblock(&self, data: UserBlock) -> Result<()> {
let initiator = self.get_user_by_id(data.initiator).await?;
let receiver = self.get_user_by_id(data.receiver).await?;
// ...
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
@ -197,6 +201,31 @@ impl DataManager {
return Err(Error::DatabaseError(e.to_string()));
}
// remove initiator from receiver's communities
for community in self.get_communities_by_owner(data.receiver).await? {
if let Ok(membership) = self
.get_membership_by_owner_community_no_void(data.initiator, community.id)
.await
{
self.delete_membership_force(membership.id).await?;
}
}
// unfollow/remove follower
if let Ok(f) = self
.get_userfollow_by_initiator_receiver(data.initiator, data.receiver)
.await
{
self.delete_userfollow(f.id, &initiator, false).await?;
}
if let Ok(f) = self
.get_userfollow_by_receiver_initiator(data.initiator, data.receiver)
.await
{
self.delete_userfollow(f.id, &receiver, false).await?;
}
// return
Ok(())
}