got the relationship working, and the mark_as_read working

This commit is contained in:
Alan Roberts 2024-09-27 12:36:51 -04:00
parent c59556fab5
commit ccbc0b4b22
2 changed files with 26 additions and 10 deletions

View file

@ -699,11 +699,10 @@ class User(UserMixin, db.Model):
roles = db.relationship('Role', secondary=user_role, lazy='dynamic', cascade="all, delete")
hide_read_posts = db.Column(db.Boolean, default=False)
read_post = db.relationship(
'Post', secondary=read_posts,
primaryjoin=(read_posts.c.user_id == id),
secondaryjoin=(read_posts.c.read_post_id == id),
backref=db.backref('read_by', lazy='dynamic'), lazy='dynamic')
# db relationship tracked by the "read_posts" table
# this is the User side, so its referencing the Post side
# read_by is the corresponding Post object variable
read_post = db.relationship('Post', secondary=read_posts, back_populates='read_by', lazy='dynamic')
def __repr__(self):
return '<User {}_{}>'.format(self.user_name, self.id)
@ -1031,15 +1030,15 @@ class User(UserMixin, db.Model):
return str(e)
# mark a post as 'read' for this user
def mark_post_as_read(self, post_id):
def mark_post_as_read(self, post):
# check if its already marked as read, if not, mark it as read
if not self.has_read_post(post_id):
self.read_post.append(post_id)
if not self.has_read_post(post):
self.read_post.append(post)
# check if post has been read by this user
# returns true if the post has been read, false if not
def has_read_post(self, post_id):
return self.read_post.filter(read_posts.c.read_post_id == post_id).count() > 0
def has_read_post(self, post):
return self.read_post.filter(read_posts.c.read_post_id == post.id).count() > 0
@ -1104,6 +1103,11 @@ class Post(db.Model):
replies = db.relationship('PostReply', lazy='dynamic', backref='post')
language = db.relationship('Language', foreign_keys=[language_id])
# db relationship tracked by the "read_posts" table
# this is the Post side, so its referencing the User side
# read_post is the corresponding User object variable
read_by = db.relationship('User', secondary=read_posts, back_populates='read_post', lazy='dynamic')
def is_local(self):
return self.ap_id is None or self.ap_id.startswith('https://' + current_app.config['SERVER_NAME'])

View file

@ -247,6 +247,12 @@ def show_post(post_id: int):
if post.type == POST_TYPE_LINK and body_has_no_archive_link(post.body_html) and url_needs_archive(post.url):
archive_link = generate_archive_link(post.url)
# for logged in users who have the 'hide read posts' function enabled
# mark this post as read
if current_user.is_authenticated and current_user.hide_read_posts:
current_user.mark_post_as_read(post)
db.session.commit()
response = render_template('post/post.html', title=post.title, post=post, is_moderator=is_moderator, is_owner=community.is_owner(),
community=post.community,
breadcrumbs=breadcrumbs, related_communities=related_communities, mods=mod_list,
@ -330,6 +336,12 @@ def post_vote(post_id: int, vote_direction):
cache.delete_memoized(recently_upvoted_posts, current_user.id)
cache.delete_memoized(recently_downvoted_posts, current_user.id)
# for logged in users who have the 'hide read posts' function enabled
# mark this post as read
if current_user.is_authenticated and current_user.hide_read_posts:
current_user.mark_post_as_read(post)
db.session.commit()
template = 'post/_post_voting_buttons.html' if request.args.get('style', '') == '' else 'post/_post_voting_buttons_masonry.html'
return render_template(template, post=post, community=post.community, recently_upvoted=recently_upvoted,
recently_downvoted=recently_downvoted)