Allow watchers to see private threads, add list of users able to see thread

This commit is contained in:
rubenwardy 2022-04-23 20:42:58 +01:00
parent b8e40b166d
commit ee2311025c
2 changed files with 36 additions and 6 deletions

@ -20,7 +20,7 @@ from typing import Tuple, List
from flask import url_for from flask import url_for
from . import db from . import db
from .users import Permission, UserRank from .users import Permission, UserRank, User
from .packages import Package from .packages import Package
watchers = db.Table("watchers", watchers = db.Table("watchers",
@ -88,7 +88,7 @@ class Thread(db.Model):
if self.package: if self.package:
isMaintainer = isMaintainer or user in self.package.maintainers isMaintainer = isMaintainer or user in self.package.maintainers
canSee = not self.private or isMaintainer or user.rank.atLeast(UserRank.APPROVER) canSee = not self.private or isMaintainer or user.rank.atLeast(UserRank.APPROVER) or user in self.watchers
if perm == Permission.SEE_THREAD: if perm == Permission.SEE_THREAD:
return canSee return canSee
@ -107,6 +107,20 @@ class Thread(db.Model):
else: else:
raise Exception("Permission {} is not related to threads".format(perm.name)) raise Exception("Permission {} is not related to threads".format(perm.name))
def get_visible_to(self) -> list[User]:
retval = {
self.author.username: self.author
}
for user in self.watchers:
retval[user.username] = user
if self.package:
for user in self.package.maintainers:
retval[user.username] = user
return list(retval.values())
def get_latest_reply(self): def get_latest_reply(self):
return ThreadReply.query.filter_by(thread_id=self.id).order_by(db.desc(ThreadReply.id)).first() return ThreadReply.query.filter_by(thread_id=self.id).order_by(db.desc(ThreadReply.id)).first()

@ -81,14 +81,30 @@
{% if thread.package %} {% if thread.package %}
<p> <p>
{{ _("Package") }}: <a href="{{ thread.package.getURL("packages.view") }}">{{ thread.package.title }}</a> {{ _("Package") }}: <a href="{{ thread.package.getURL('packages.view') }}">{{ thread.package.title }}</a>
</p> </p>
{% endif %} {% endif %}
{% if thread.private %} {% if thread.private %}
<i> <aside class="row">
{{ _("This thread is only visible to its creator, the package owner, and users of Approver rank or above.") }} <div class="col-md-9">
</i> <i>
{{ _("This thread is only visible to its creator, the package owner, and users of Approver rank or above.") }}
</i>
</div>
<div class="col-md-3">
<div class="d-flex flex-row justify-content-end flex-wrap align-items-center" style="gap: 0.5em;">
{% for viewer in thread.get_visible_to() %}
<a href="{{ url_for('users.profile', username=viewer.username) }}" title="{{ viewer.display_name }}">
<img style="max-height: 2em;" src="{{ viewer.getProfilePicURL() }}" alt="{{ viewer.display_name }}" />
</a>
{% endfor %}
<a href="{{ url_for('users.list_all') }}" title="{{ _('Plus approvers and editors') }}">
+ <i class="fas fa-user-check"></i>
</a>
</div>
</div>
</aside>
{% endif %} {% endif %}
{% from "macros/threads.html" import render_thread %} {% from "macros/threads.html" import render_thread %}