fix: user community membership checks for timelines

This commit is contained in:
trisua 2025-06-29 12:26:22 -04:00
parent 0272985b81
commit 50f4592de2
4 changed files with 67 additions and 17 deletions

View file

@ -1,11 +1,9 @@
(text "{%- import \"components.html\" as components -%} {%- import \"macros.html\" as macros -%}") (text "{%- import \"components.html\" as components -%} {%- import \"macros.html\" as macros -%}")
(text "{% for post in list %} (text "{% for post in list %}
{% if post[2].read_access == \"Everybody\" -%} {% if post[0].context.repost and post[0].context.repost.reposting -%}
{% if post[0].context.repost and post[0].context.repost.reposting -%} {{ components::repost(repost=post[3], post=post[0], owner=post[1], secondary=true, community=post[2], show_community=true) }}
{{ components::repost(repost=post[3], post=post[0], owner=post[1], secondary=true, community=post[2], show_community=true) }} {% else %}
{% else %} {{ components::post(post=post[0], owner=post[1], question=post[4], secondary=true, community=post[2], poll=post[5]) }}
{{ components::post(post=post[0], owner=post[1], question=post[4], secondary=true, community=post[2], poll=post[5]) }}
{%- endif %}
{%- endif %} {%- endif %}
{% endfor %}") {% endfor %}")
(datalist (datalist

View file

@ -425,6 +425,7 @@
self.define("notifications_stream", ({ _, streams }) => { self.define("notifications_stream", ({ _, streams }) => {
const element = document.getElementById("notifications_span"); const element = document.getElementById("notifications_span");
let current = Number.parseInt(element.innerText || "0");
streams.subscribe("notifs"); streams.subscribe("notifs");
streams.event("notifs", "message", (data) => { streams.event("notifs", "message", (data) => {
@ -435,13 +436,12 @@
const inner_data = JSON.parse(data.data); const inner_data = JSON.parse(data.data);
if (data.method.Packet.Crud === "Create") { if (data.method.Packet.Crud === "Create") {
const current = Number.parseInt(element.innerText || "0");
if (current <= 0) { if (current <= 0) {
element.classList.remove("hidden"); element.classList.remove("hidden");
} }
element.innerText = current + 1; current += 1;
element.innerText = current;
// check if we're already connected // check if we're already connected
const connected = const connected =
@ -477,16 +477,19 @@
console.info("notification created"); console.info("notification created");
} }
} else if (data.method.Packet.Crud === "Delete") { } else if (data.method.Packet.Crud === "Delete") {
const current = Number.parseInt(element.innerText || "0");
if (current - 1 <= 0) { if (current - 1 <= 0) {
element.classList.add("hidden"); element.classList.add("hidden");
} }
element.innerText = current - 1; current -= 1;
element.innerText = current;
} else { } else {
console.warn("correct packet type but with wrong data"); console.warn("correct packet type but with wrong data");
} }
if (element.innerText !== current) {
element.innerText = current;
}
}); });
}); });

View file

@ -1,8 +1,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use oiseau::cache::Cache;
use crate::config::StringBan; use crate::config::StringBan;
use crate::model::auth::Notification; use crate::model::auth::Notification;
use crate::model::communities::{Poll, Question}; use crate::model::communities::{CommunityMembership, CommunityReadAccess, Poll, Question};
use crate::model::communities_permissions::CommunityPermission; use crate::model::communities_permissions::CommunityPermission;
use crate::model::moderation::AuditLogEntry; use crate::model::moderation::AuditLogEntry;
use crate::model::stacks::{StackMode, StackSort, UserStack}; use crate::model::stacks::{StackMode, StackSort, UserStack};
@ -16,7 +15,7 @@ use tetratto_shared::unix_epoch_timestamp;
use crate::{auto_method, DataManager}; use crate::{auto_method, DataManager};
use oiseau::{PostgresRow, cache::redis::Commands}; use oiseau::{PostgresRow, cache::redis::Commands};
use oiseau::{execute, get, query_row, query_rows, params}; use oiseau::{execute, get, query_row, query_rows, params, cache::Cache};
pub type FullPost = ( pub type FullPost = (
Post, Post,
@ -479,6 +478,7 @@ impl DataManager {
let mut seen_user_follow_statuses: HashMap<(usize, usize), bool> = HashMap::new(); let mut seen_user_follow_statuses: HashMap<(usize, usize), bool> = HashMap::new();
let mut seen_stacks: HashMap<usize, UserStack> = HashMap::new(); let mut seen_stacks: HashMap<usize, UserStack> = HashMap::new();
let mut replying_posts: HashMap<usize, Post> = HashMap::new(); let mut replying_posts: HashMap<usize, Post> = HashMap::new();
let mut memberships: HashMap<usize, CommunityMembership> = HashMap::new();
for post in posts { for post in posts {
if post.is_deleted { if post.is_deleted {
@ -493,6 +493,30 @@ impl DataManager {
continue; continue;
} }
// check membership
if community.read_access == CommunityReadAccess::Joined {
if let Some(user) = user {
if let Some(membership) = memberships.get(&community.id) {
if !membership.role.check(CommunityPermission::MEMBER) {
continue;
}
} else {
if let Ok(membership) = self
.get_membership_by_owner_community_no_void(user.id, community.id)
.await
{
if !membership.role.check(CommunityPermission::MEMBER) {
continue;
}
} else {
continue;
}
}
} else {
continue;
}
}
// stack // stack
let (can_view, stack) = self let (can_view, stack) = self
.get_post_stack( .get_post_stack(
@ -537,6 +561,32 @@ impl DataManager {
continue; continue;
} }
// check membership
let community = self.get_community_by_id(community).await?;
if community.read_access == CommunityReadAccess::Joined {
if let Some(user) = user {
if let Some(membership) = memberships.get(&community.id) {
if !membership.role.check(CommunityPermission::MEMBER) {
continue;
}
} else {
if let Ok(membership) = self
.get_membership_by_owner_community_no_void(user.id, community.id)
.await
{
memberships.insert(owner, membership.clone());
if !membership.role.check(CommunityPermission::MEMBER) {
continue;
}
} else {
continue;
}
}
} else {
continue;
}
}
// check relationship // check relationship
if ua.settings.private_profile && ua.id != user_id { if ua.settings.private_profile && ua.id != user_id {
if user_id == 0 { if user_id == 0 {
@ -587,7 +637,6 @@ impl DataManager {
} }
// ... // ...
let community = self.get_community_by_id(community).await?;
seen_before.insert((owner, community.id), (ua.clone(), community.clone())); seen_before.insert((owner, community.id), (ua.clone(), community.clone()));
out.push(( out.push((
post.clone(), post.clone(),

View file

@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
use tetratto_shared::{snow::Snowflake, unix_epoch_timestamp}; use tetratto_shared::{snow::Snowflake, unix_epoch_timestamp};
use super::communities_permissions::CommunityPermission; use super::communities_permissions::CommunityPermission;
#[derive(Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Community { pub struct Community {
pub id: usize, pub id: usize,
pub created: usize, pub created: usize,