From ad660260209415002be40ff52e2db0cf01f63daa Mon Sep 17 00:00:00 2001 From: Eggbertx Date: Fri, 16 Jun 2023 09:48:59 -0700 Subject: [PATCH] Appropriately show staff dropdown options according to rank --- frontend/ts/management/manage.ts | 60 +++++++++++++++++--------------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/frontend/ts/management/manage.ts b/frontend/ts/management/manage.ts index 8cb36d5c..ada07cbb 100755 --- a/frontend/ts/management/manage.ts +++ b/frontend/ts/management/manage.ts @@ -16,8 +16,7 @@ const notAStaff: StaffInfo = { const reportsTextRE = /^Reports( \(\d+\))?/; export let staffActions: StaffAction[] = []; -export let staffInfo = notAStaff; -let loginChecked = false; +let staffInfo: StaffInfo = null; /** * The menu shown when the Staff button on the top bar is clicked @@ -34,21 +33,22 @@ function dropdownHasItem(dropdown: any, item: string) { return [...dropdown.children].filter(v => v.text === item).length > 0; } -function setupManagementEvents() { - $("select.post-actions").each((_i, el) => { - const $el = $(el); - const $post = $(el.parentElement); - const isLocked = isThreadLocked($post); - if(!dropdownHasItem(el, "Staff Actions")) { - $el.append(''); - } - if($post.hasClass("op-post")) { - if(isLocked) { - $el.append(""); - } else { - $el.append(""); - } +function addManageEvents(_i: number, el: HTMLSelectElement) { + if(staffInfo === null || staffInfo.Rank < 2) return; + const $el = $(el); + const $post = $(el.parentElement); + const isLocked = isThreadLocked($post); + if(!dropdownHasItem(el, "Staff Actions")) { + $el.append(''); + } + if(staffInfo.Rank === 3 && $post.hasClass("op-post")) { + if(isLocked) { + $el.append(""); + } else { + $el.append(""); } + } + if(staffInfo.Rank >= 2) { if(!dropdownHasItem(el, "Posts from this IP")) { $el.append(""); } @@ -62,12 +62,18 @@ function setupManagementEvents() { "" ); } - }); - $(document).on("postDropdownAdded", function(_e, data) { - if(!data.dropdown) return; - data.dropdown.append(""); - data.dropdown.append("") - }); + } +} + +function setupManagementEvents() { + getStaffInfo().then(() => { + $("select.post-actions").each(addManageEvents); + $(document).on("postDropdownAdded", function(_e, data) { + if(!data.dropdown) return; + data.dropdown.append(""); + data.dropdown.append("") + }); + }) } interface BanFileJSON { @@ -138,16 +144,14 @@ export async function initStaff() { } -export async function getStaffInfo() { - if(loginChecked) - // don't make multiple unnecessary AJAX requests if we're already logged in +export async function getStaffInfo(): Promise { + if(staffInfo !== null) + // don't make multiple unnecessary AJAX requests return staffInfo; - loginChecked = true; return $.ajax({ method: "GET", url: `${webroot}manage/staffinfo`, async: true, - cache: true, dataType: "json" }).catch(() => { return notAStaff; @@ -267,7 +271,7 @@ export function createStaffMenu(staff = staffInfo) { } function createStaffButton() { - if($staffBtn !== null || staffInfo.Rank === 0) + if($staffBtn !== null || staffInfo == null || staffInfo.Rank === 0) return; $staffBtn = new TopBarButton("Staff", () => { $topbar.trigger("menuButtonClick", [$staffMenu, $(document).find($staffMenu).length === 0]);