From 708ade4dbfb781030893c1559cbe3310add77d35 Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Fri, 11 Feb 2022 15:57:52 -0800 Subject: [PATCH] fix: allow co-maintainers to [un]pin comments on a package Closes #279 Signed-off-by: Kevin Morris --- aurweb/models/package_comment.py | 8 +++++++ aurweb/pkgbase/util.py | 8 ++++--- aurweb/routers/pkgbase.py | 4 ++-- templates/partials/comment_actions.html | 4 ++-- templates/partials/packages/details.html | 2 +- test/test_pkgbase_routes.py | 29 ++++++++++++++++++++++++ 6 files changed, 47 insertions(+), 8 deletions(-) diff --git a/aurweb/models/package_comment.py b/aurweb/models/package_comment.py index 2a529c9c..7ab1f218 100644 --- a/aurweb/models/package_comment.py +++ b/aurweb/models/package_comment.py @@ -52,3 +52,11 @@ class PackageComment(Base): if self.RenderedComment is None: self.RenderedComment = str() + + def maintainers(self): + return list(filter( + lambda e: e is not None, + [self.PackageBase.Maintainer] + [ + c.User for c in self.PackageBase.comaintainers + ] + )) diff --git a/aurweb/pkgbase/util.py b/aurweb/pkgbase/util.py index fbfcd869..18af3df0 100644 --- a/aurweb/pkgbase/util.py +++ b/aurweb/pkgbase/util.py @@ -25,9 +25,11 @@ def make_context(request: Request, pkgbase: PackageBase) -> Dict[str, Any]: context["git_clone_uri_anon"] = config.get("options", "git_clone_uri_anon") context["git_clone_uri_priv"] = config.get("options", "git_clone_uri_priv") context["pkgbase"] = pkgbase - context["comaintainers"] = pkgbase.comaintainers.order_by( - PackageComaintainer.Priority.asc() - ).all() + context["comaintainers"] = [ + c.User for c in pkgbase.comaintainers.order_by( + PackageComaintainer.Priority.asc() + ).all() + ] context["packages_count"] = pkgbase.packages.count() context["keywords"] = pkgbase.keywords context["comments"] = pkgbase.comments.order_by( diff --git a/aurweb/routers/pkgbase.py b/aurweb/routers/pkgbase.py index cd35a7f8..7825ad7b 100644 --- a/aurweb/routers/pkgbase.py +++ b/aurweb/routers/pkgbase.py @@ -318,7 +318,7 @@ async def pkgbase_comment_pin(request: Request, name: str, id: int, comment = get_pkgbase_comment(pkgbase, id) has_cred = request.user.has_credential(creds.COMMENT_PIN, - approved=[pkgbase.Maintainer]) + approved=comment.maintainers()) if not has_cred: _ = l10n.get_translator_for_request(request) raise HTTPException( @@ -353,7 +353,7 @@ async def pkgbase_comment_unpin(request: Request, name: str, id: int, comment = get_pkgbase_comment(pkgbase, id) has_cred = request.user.has_credential(creds.COMMENT_PIN, - approved=[pkgbase.Maintainer]) + approved=comment.maintainers()) if not has_cred: _ = l10n.get_translator_for_request(request) raise HTTPException( diff --git a/templates/partials/comment_actions.html b/templates/partials/comment_actions.html index 78c4cc22..bf6103d7 100644 --- a/templates/partials/comment_actions.html +++ b/templates/partials/comment_actions.html @@ -47,13 +47,13 @@ {% endif %} - {% if request.user.has_credential(creds.COMMENT_PIN, approved=[comment.PackageBase.Maintainer]) %} + {% if request.user.has_credential(creds.COMMENT_PIN, approved=comment.maintainers()) %} {% if comment.PinnedTS %}
-
+
{% set len = comaintainers | length %} {% if comaintainers %} - ({% for co in comaintainers %}{{ co.User }}{% if loop.index < len %}, {% endif %}{% endfor %}) + ({% for co in comaintainers %}{{ co }}{% if loop.index < len %}, {% endif %}{% endfor %}) {% endif %} {% else %} {{ pkgbase.Maintainer.Username | default("None" | tr) }} diff --git a/test/test_pkgbase_routes.py b/test/test_pkgbase_routes.py index 03b55063..5edae592 100644 --- a/test/test_pkgbase_routes.py +++ b/test/test_pkgbase_routes.py @@ -534,6 +534,35 @@ def test_pkgbase_comment_undelete_not_found(client: TestClient, assert resp.status_code == int(HTTPStatus.NOT_FOUND) +def test_pkgbase_comment_pin_as_co(client: TestClient, package: Package, + comment: PackageComment): + comaint = create_user("comaint1") + + with db.begin(): + db.create(PackageComaintainer, PackageBase=package.PackageBase, + User=comaint, Priority=1) + + # Pin the comment. + pkgbasename = package.PackageBase.Name + endpoint = f"/pkgbase/{pkgbasename}/comments/{comment.ID}/pin" + cookies = {"AURSID": comaint.login(Request(), "testPassword")} + with client as request: + resp = request.post(endpoint, cookies=cookies) + assert resp.status_code == int(HTTPStatus.SEE_OTHER) + + # Assert that PinnedTS got set. + assert comment.PinnedTS > 0 + + # Unpin the comment we just pinned. + endpoint = f"/pkgbase/{pkgbasename}/comments/{comment.ID}/unpin" + with client as request: + resp = request.post(endpoint, cookies=cookies) + assert resp.status_code == int(HTTPStatus.SEE_OTHER) + + # Let's assert that PinnedTS was unset. + assert comment.PinnedTS == 0 + + def test_pkgbase_comment_pin(client: TestClient, maintainer: User, package: Package,