diff --git a/app/community/routes.py b/app/community/routes.py index 95002e95..5cc64d20 100644 --- a/app/community/routes.py +++ b/app/community/routes.py @@ -29,7 +29,7 @@ from app.inoculation import inoculation from app.models import User, Community, CommunityMember, CommunityJoinRequest, CommunityBan, Post, \ File, PostVote, utcnow, Report, Notification, InstanceBlock, ActivityPubLog, Topic, Conversation, PostReply, \ NotificationSubscription, UserFollower, Instance, Language, Poll, PollChoice, ModLog, CommunityWikiPage, \ - CommunityWikiPageRevision + CommunityWikiPageRevision, read_posts from app.community import bp from app.user.utils import search_for_user from app.utils import get_setting, render_template, allowlist_html, markdown_to_html, validation_required, \ @@ -229,12 +229,8 @@ def show_community(community: Community): if current_user.hide_nsfw == 1: posts = posts.filter(Post.nsfw == False) if current_user.hide_read_posts: - cu_rp = current_user.read_post.all() - cu_rp_ids = [] - for p in cu_rp: - cu_rp_ids.append(p.id) - for p_id in cu_rp_ids: - posts = posts.filter(Post.id != p_id) + posts = posts.outerjoin(read_posts, (Post.id == read_posts.c.read_post_id) & (read_posts.c.user_id == current_user.id)) + posts = posts.filter(read_posts.c.read_post_id.is_(None)) # Filter where there is no corresponding read post for the current user content_filters = user_filters_posts(current_user.id) posts = posts.filter(Post.deleted == False) diff --git a/app/domain/routes.py b/app/domain/routes.py index d04828bc..bd40aa22 100644 --- a/app/domain/routes.py +++ b/app/domain/routes.py @@ -6,7 +6,7 @@ from flask_babel import _ from app import db, constants, cache from app.inoculation import inoculation -from app.models import Post, Domain, Community, DomainBlock +from app.models import Post, Domain, Community, DomainBlock, read_posts from app.domain import bp from app.utils import render_template, permission_required, joined_communities, moderating_communities, \ user_filters_posts, blocked_domains, blocked_instances, menu_topics @@ -41,12 +41,8 @@ def show_domain(domain_id): # don't show posts a user has already interacted with if current_user.hide_read_posts: - cu_rp = current_user.read_post.all() - cu_rp_ids = [] - for p in cu_rp: - cu_rp_ids.append(p.id) - for p_id in cu_rp_ids: - posts = posts.filter(Post.id != p_id) + posts = posts.outerjoin(read_posts, (Post.id == read_posts.c.read_post_id) & (read_posts.c.user_id == current_user.id)) + posts = posts.filter(read_posts.c.read_post_id.is_(None)) # Filter where there is no corresponding read post for the current user # pagination posts = posts.paginate(page=page, per_page=100, error_out=False) diff --git a/app/main/routes.py b/app/main/routes.py index df8479a1..d80c1d00 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -24,7 +24,7 @@ from app.utils import render_template, get_setting, request_etag_matches, return blocked_instances, communities_banned_from, topic_tree, recently_upvoted_posts, recently_downvoted_posts, \ blocked_users, menu_topics, languages_for_form, blocked_communities, get_request from app.models import Community, CommunityMember, Post, Site, User, utcnow, Topic, Instance, \ - Notification, Language, community_language, ModLog + Notification, Language, community_language, ModLog, read_posts @bp.route('/', methods=['HEAD', 'GET', 'POST']) @@ -72,12 +72,8 @@ def home_page(sort, view_filter): if current_user.hide_nsfw == 1: posts = posts.filter(Post.nsfw == False) if current_user.hide_read_posts: - cu_rp = current_user.read_post.all() - cu_rp_ids = [] - for p in cu_rp: - cu_rp_ids.append(p.id) - for p_id in cu_rp_ids: - posts = posts.filter(Post.id != p_id) + posts = posts.outerjoin(read_posts, (Post.id == read_posts.c.read_post_id) & (read_posts.c.user_id == current_user.id)) + posts = posts.filter(read_posts.c.read_post_id.is_(None)) # Filter where there is no corresponding read post for the current user domains_ids = blocked_domains(current_user.id) if domains_ids: diff --git a/app/models.py b/app/models.py index 31e1fbde..3dbea4b4 100644 --- a/app/models.py +++ b/app/models.py @@ -617,9 +617,10 @@ user_role = db.Table('user_role', read_posts = db.Table('read_posts', db.Column('user_id', db.Integer, db.ForeignKey('user.id'), index=True), db.Column('read_post_id', db.Integer, db.ForeignKey('post.id'), index=True), - db.Column('interacted_at', db.DateTime, index=True, default=utcnow) # this is when the content is interacte with + db.Column('interacted_at', db.DateTime, index=True, default=utcnow) # this is when the content is interacted with ) + class User(UserMixin, db.Model): query_class = FullTextSearchQuery id = db.Column(db.Integer, primary_key=True) diff --git a/app/templates/user/read_posts.html b/app/templates/user/read_posts.html index 8427646d..f52a3cf0 100644 --- a/app/templates/user/read_posts.html +++ b/app/templates/user/read_posts.html @@ -12,10 +12,10 @@
-