From 2411053d55d72469e220e897b959aec11e34bd89 Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Fri, 14 Oct 2022 20:28:20 +0200 Subject: [PATCH] add tiered pins --- ...nt.js => submission+submission_listing.js} | 27 ++++++++++- files/routes/admin.py | 48 +++++++++++-------- files/templates/post_actions.html | 2 +- .../templates/post_admin_actions_mobile.html | 2 +- files/templates/submission.html | 2 +- files/templates/submission_listing.html | 2 +- 6 files changed, 59 insertions(+), 24 deletions(-) rename files/assets/js/{new_comments_count.js => submission+submission_listing.js} (68%) diff --git a/files/assets/js/new_comments_count.js b/files/assets/js/submission+submission_listing.js similarity index 68% rename from files/assets/js/new_comments_count.js rename to files/assets/js/submission+submission_listing.js index 02f7a4e23..549291a3b 100644 --- a/files/assets/js/new_comments_count.js +++ b/files/assets/js/submission+submission_listing.js @@ -38,4 +38,29 @@ if (typeof showNewCommentCounts === 'undefined') { } setTimeout(cleanCache, 500) -} \ No newline at end of file +} + +function pin_post(t, id) { + t.disabled = true; + t.classList.add("disabled"); + post_toast_callback(`/sticky/${id}`, + { + }, + (xhr) => { + if(xhr.status == 200) { + t.innerHTML = t.innerHTML.replace(t.textContent, 'Pin permanently') + } + else if(xhr.status == 201) { + t.innerHTML = t.innerHTML.replace(t.textContent, 'Pin for 1 hour') + t.classList.add('d-none') + } + t.nextElementSibling.classList.remove('d-none') + t.disabled = false; + t.classList.remove("disabled"); + } + ); + setTimeout(() => { + t.disabled = false; + t.classList.remove("disabled"); + }, 2000); +} diff --git a/files/routes/admin.py b/files/routes/admin.py index 878f62c9d..7ec9a8f62 100644 --- a/files/routes/admin.py +++ b/files/routes/admin.py @@ -1205,30 +1205,40 @@ def distinguish_post(post_id, v): @feature_required('PINS') def sticky_post(post_id, v): + pins = g.db.query(Submission).filter(Submission.stickied != None, Submission.is_banned == False).count() + + if pins >= PIN_LIMIT and v.admin_level < PERMS['BYPASS_PIN_LIMIT']: + abort(403, f"Can't exceed {PIN_LIMIT} pinned posts limit!") post = get_post(post_id) - if not post.stickied: - pins = g.db.query(Submission).filter(Submission.stickied != None, Submission.is_banned == False).count() - if pins >= PIN_LIMIT: - if v.admin_level >= PERMS['BYPASS_PIN_LIMIT']: - post.stickied = v.username - post.stickied_utc = int(time.time()) + 3600 - else: abort(403, f"Can't exceed {PIN_LIMIT} pinned posts limit!") - else: post.stickied = v.username - g.db.add(post) - ma=ModAction( - kind="pin_post", - user_id=v.id, - target_submission_id=post.id - ) - g.db.add(ma) + if not post.stickied_utc: + post.stickied_utc = int(time.time()) + 3600 + pin_time = 'for 1 hour' + code = 200 + else: + post.stickied_utc = None + pin_time = 'permantently' + code = 201 - if v.id != post.author_id: - send_repeatable_notification(post.author_id, f"@{v.username} (Admin) has pinned [{post.title}](/post/{post_id})!") + post.stickied = v.username - cache.delete_memoized(frontlist) - return {"message": "Post pinned!"} + g.db.add(post) + + ma=ModAction( + kind="pin_post", + user_id=v.id, + target_submission_id=post.id, + _note=pin_time + ) + g.db.add(ma) + + if v.id != post.author_id: + send_repeatable_notification(post.author_id, f"@{v.username} (Admin) has pinned [{post.title}](/post/{post_id}) {pin_time}!") + + cache.delete_memoized(frontlist) + + return {"message": f"Post pinned {pin_time}!"}, code @app.post("/unsticky/") @admin_level_required(PERMS['POST_COMMENT_MODERATION']) diff --git a/files/templates/post_actions.html b/files/templates/post_actions.html index 44f2cac80..1dfda5da7 100644 --- a/files/templates/post_actions.html +++ b/files/templates/post_actions.html @@ -46,7 +46,7 @@ {% endif %} {% if v.admin_level >= PERMS['POST_COMMENT_MODERATION'] %} - Pin + Pin {% if p.stickied_utc %}permanently{% else %}for 1 hour{% endif %} Unpin {% endif %} diff --git a/files/templates/post_admin_actions_mobile.html b/files/templates/post_admin_actions_mobile.html index b90992940..043e8add8 100644 --- a/files/templates/post_admin_actions_mobile.html +++ b/files/templates/post_admin_actions_mobile.html @@ -23,7 +23,7 @@ {% endif %} {% if v.admin_level >= PERMS['POST_COMMENT_MODERATION'] %} - + {% if "/reported/" in request.path %} diff --git a/files/templates/submission.html b/files/templates/submission.html index 88215c36c..2ec933caa 100644 --- a/files/templates/submission.html +++ b/files/templates/submission.html @@ -1099,7 +1099,7 @@ {% endif %} {% if not v or v.highlightcomments %} - + {% endif %} diff --git a/files/templates/submission_listing.html b/files/templates/submission_listing.html index 2ff3ec73f..2800a93ea 100644 --- a/files/templates/submission_listing.html +++ b/files/templates/submission_listing.html @@ -11,7 +11,7 @@ {% endif %} {% if not v or v.highlightcomments %} - + {% endif %} {% include "popover.html" %}