From 60694d9fbdf9e13cea138768bb77fba4c84b0a61 Mon Sep 17 00:00:00 2001 From: Dominik Picheta Date: Wed, 16 May 2018 15:13:41 +0100 Subject: [PATCH] Moderated posts are now shown to correct people in correct circumstances. --- forum.nim | 10 ++-------- redesign/forum.nim | 2 +- redesign/header.nim | 6 ++++-- redesign/postlist.nim | 12 ++++++++++++ redesign/threadlist.nim | 42 ++++++++++++++++++++++++++--------------- 5 files changed, 46 insertions(+), 26 deletions(-) diff --git a/forum.nim b/forum.nim index 85fd345..3a6f689 100644 --- a/forum.nim +++ b/forum.nim @@ -1205,19 +1205,13 @@ routes: let threadRow = getRow(db, threadsQuery, id) let thread = selectThread(threadRow) - let modClause = - if c.rank >= Moderator: - "(1 or u.id = ?)" - else: - "(u.status <> 'Moderated' or p.author = ?)" let postsQuery = sql( """select p.id, p.content, strftime('%s', p.creation), p.author, u.name, u.email, strftime('%s', u.lastOnline), u.status from post p, person u - where u.id = p.author and p.thread = ? and $# - and (u.status <> 'Spammer' or p.author = ?) - order by p.id""" % modClause + where u.id = p.author and p.thread = ? + order by p.id""" ) var list = PostList( diff --git a/redesign/forum.nim b/redesign/forum.nim index 1c08ec4..42d979a 100644 --- a/redesign/forum.nim +++ b/redesign/forum.nim @@ -67,7 +67,7 @@ proc render(): VNode = ) ) ), - r("/", (params: Params) => renderThreadList()) + r("/", (params: Params) => renderThreadList(getLoggedInUser())) ]) window.onPopState = onPopState diff --git a/redesign/header.nim b/redesign/header.nim index d4d764f..a09221a 100644 --- a/redesign/header.nim +++ b/redesign/header.nim @@ -68,9 +68,11 @@ when defined(js): let uri = makeUri("status.json", [("logout", $logout)]) ajaxGet(uri, @[], onStatus) + proc getLoggedInUser*(): Option[User] = + state.data.map(x => x.user).flatten + proc isLoggedIn*(): bool = - let user = state.data.map(x => x.user).flatten - not user.isNone + not getLoggedInUser().isNone proc renderHeader*(): VNode = if state.data.isNone: diff --git a/redesign/postlist.nim b/redesign/postlist.nim index ee41d12..5f90fd8 100644 --- a/redesign/postlist.nim +++ b/redesign/postlist.nim @@ -223,6 +223,18 @@ when defined(js): section(class="container grid-xl"): tdiv(class="title"): p(): text list.thread.topic + if list.thread.isLocked: + italic(class="fas fa-lock fa-xs", + title="Thread cannot be replied to") + text "Locked" + if list.thread.isModerated: + italic(class="fas fa-eye-slash fa-xs", + title="Thread is moderated") + text "Moderated" + if list.thread.isSolved: + italic(class="fas fa-check-square fa-xs", + title="Thread has a solution") + text "Solved" render(list.thread.category) tdiv(class="posts"): var prevPost: Option[Post] = none[Post]() diff --git a/redesign/threadlist.nim b/redesign/threadlist.nim index dbc1dac..9f2465e 100644 --- a/redesign/threadlist.nim +++ b/redesign/threadlist.nim @@ -22,7 +22,7 @@ type lastVisit*: int64 ## Unix timestamp moreCount*: int ## How many more threads are left -proc isInvisible*(thread: Thread): bool = +proc isModerated*(thread: Thread): bool = ## Determines whether the specified thread is under moderation. thread.author.rank <= Moderated @@ -30,7 +30,7 @@ when defined(js): include karax/prelude import karax / [vstyles, kajax, kdom] - import karaxutils, error + import karaxutils, error, user type State = ref object @@ -38,7 +38,6 @@ when defined(js): loading: bool status: HttpCode - proc onNewThread(threadId, postId: int) proc newState(): State = State( list: none[ThreadList](), @@ -49,10 +48,20 @@ when defined(js): var state = newState() - proc onNewThread(threadId, postId: int) = - discard + proc visibleTo(thread: Thread, user: Option[User]): bool = + ## Determines whether the specified thread should be shown to the user. + ## + ## The rules for this are determined by the rank of the user, their + ## settings (TODO), and whether the thread's creator is moderated or not. + if user.isNone(): return not thread.isModerated - proc genTopButtons(): VNode = + let rank = user.get().rank + if rank < Moderator and thread.isModerated: + return thread.author == user.get() + + return true + + proc genTopButtons(currentUser: Option[User]): VNode = result = buildHtml(): section(class="navbar container grid-xl", id="main-buttons"): section(class="navbar-section"): @@ -67,10 +76,11 @@ when defined(js): button(class="btn btn-link"): text "Most Active" button(class="btn btn-link"): text "Categories" section(class="navbar-section"): - a(href=makeUri("/newthread"), onClick=anchorCB): - button(class="btn btn-secondary"): - italic(class="fas fa-plus") - text " New Thread" + if currentUser.isSome(): + a(href=makeUri("/newthread"), onClick=anchorCB): + button(class="btn btn-secondary"): + italic(class="fas fa-plus") + text " New Thread" proc genUserAvatars(users: seq[User]): VNode = result = buildHtml(td): @@ -102,7 +112,7 @@ when defined(js): if thread.isLocked: italic(class="fas fa-lock fa-xs", title="Thread cannot be replied to") - if thread.isInvisible: + if thread.isModerated: italic(class="fas fa-eye-slash fa-xs", title="Thread is moderated") if thread.isSolved: @@ -147,7 +157,7 @@ when defined(js): let start = state.list.get().threads.len ajaxGet(makeUri("threads.json?start=" & $start), @[], onThreadList) - proc genThreadList(): VNode = + proc genThreadList(currentUser: Option[User]): VNode = if state.status != Http200: return renderError("Couldn't retrieve threads.") @@ -171,6 +181,8 @@ when defined(js): tbody(): for i in 0 ..< list.threads.len: let thread = list.threads[i] + if not visibleTo(thread, currentUser): continue + let isLastVisit = i+1 < list.threads.len and list.threads[i].activity < list.lastVisit @@ -191,7 +203,7 @@ when defined(js): td(colspan="6", onClick=onLoadMore): span(text "load more threads") - proc renderThreadList*(): VNode = + proc renderThreadList*(currentUser: Option[User]): VNode = result = buildHtml(tdiv): - genTopButtons() - genThreadList() \ No newline at end of file + genTopButtons(currentUser) + genThreadList(currentUser) \ No newline at end of file