From 9db7343370193287f9731d70f17c1445378e479b Mon Sep 17 00:00:00 2001
From: Noah Petherbridge
Date: Sat, 11 May 2024 12:23:06 -0700
Subject: [PATCH] Private forums (admin only for now)
---
pkg/controller/forum/add_edit.go | 9 ++++++++-
pkg/controller/forum/forum.go | 6 ++++++
pkg/controller/forum/thread.go | 6 ++++++
pkg/models/forum.go | 8 +++++++-
pkg/models/forum_recent.go | 5 +++++
pkg/models/forum_search.go | 5 +++++
web/templates/forum/add_edit.html | 13 +++++++++++++
web/templates/forum/admin.html | 11 +++++++++--
web/templates/forum/index.html | 7 +++++++
web/templates/forum/newest.html | 4 ++++
10 files changed, 70 insertions(+), 4 deletions(-)
diff --git a/pkg/controller/forum/add_edit.go b/pkg/controller/forum/add_edit.go
index 7196b8c..939d3df 100644
--- a/pkg/controller/forum/add_edit.go
+++ b/pkg/controller/forum/add_edit.go
@@ -65,12 +65,14 @@ func AddEdit() http.HandlerFunc {
isPrivileged = r.PostFormValue("privileged") == "true"
isPermitPhotos = r.PostFormValue("permit_photos") == "true"
isInnerCircle = r.PostFormValue("inner_circle") == "true"
+ isPrivate = r.PostFormValue("private") == "true"
)
// Sanity check admin-only settings.
if !currentUser.IsAdmin {
isPrivileged = false
isPermitPhotos = false
+ isPrivate = false
}
// Were we editing an existing forum?
@@ -83,6 +85,7 @@ func AddEdit() http.HandlerFunc {
models.NewFieldDiff("Privileged", forum.Privileged, isPrivileged),
models.NewFieldDiff("PermitPhotos", forum.PermitPhotos, isPermitPhotos),
models.NewFieldDiff("InnerCircle", forum.InnerCircle, isInnerCircle),
+ models.NewFieldDiff("Private", forum.Private, isPrivate),
}
forum.Title = title
@@ -92,6 +95,7 @@ func AddEdit() http.HandlerFunc {
forum.Privileged = isPrivileged
forum.PermitPhotos = isPermitPhotos
forum.InnerCircle = isInnerCircle
+ forum.Private = isPrivate
// Save it.
if err := forum.Save(); err == nil {
@@ -128,6 +132,7 @@ func AddEdit() http.HandlerFunc {
Privileged: isPrivileged,
PermitPhotos: isPermitPhotos,
InnerCircle: isInnerCircle,
+ Private: isPrivate,
}
if err := models.CreateForum(forum); err == nil {
@@ -144,7 +149,8 @@ func AddEdit() http.HandlerFunc {
"* Explicit: %v\n"+
"* Privileged: %v\n"+
"* Photos: %v\n"+
- "* Inner Circle: %v",
+ "* Inner Circle: %v\n"+
+ "* Private: %v",
forum.Category,
forum.Title,
forum.Fragment,
@@ -153,6 +159,7 @@ func AddEdit() http.HandlerFunc {
forum.Privileged,
forum.PermitPhotos,
forum.InnerCircle,
+ forum.Private,
))
return
diff --git a/pkg/controller/forum/forum.go b/pkg/controller/forum/forum.go
index 4ff1ec1..045db0d 100644
--- a/pkg/controller/forum/forum.go
+++ b/pkg/controller/forum/forum.go
@@ -41,6 +41,12 @@ func Forum() http.HandlerFunc {
return
}
+ // Is it a private forum?
+ if forum.Private && !currentUser.IsAdmin {
+ templates.NotFoundPage(w, r)
+ return
+ }
+
// Get the pinned threads.
pinned, err := models.PinnedThreads(forum)
if err != nil {
diff --git a/pkg/controller/forum/thread.go b/pkg/controller/forum/thread.go
index dc51e5e..b047f7c 100644
--- a/pkg/controller/forum/thread.go
+++ b/pkg/controller/forum/thread.go
@@ -57,6 +57,12 @@ func Thread() http.HandlerFunc {
return
}
+ // Is it a private forum?
+ if forum.Private && !currentUser.IsAdmin {
+ templates.NotFoundPage(w, r)
+ return
+ }
+
// Ping the view count on this thread.
if err := thread.View(currentUser.ID); err != nil {
log.Error("Couldn't ping view count on thread %d: %s", thread.ID, err)
diff --git a/pkg/models/forum.go b/pkg/models/forum.go
index 4632aa4..3b02c3b 100644
--- a/pkg/models/forum.go
+++ b/pkg/models/forum.go
@@ -20,7 +20,8 @@ type Forum struct {
Explicit bool `gorm:"index"`
Privileged bool
PermitPhotos bool
- InnerCircle bool
+ InnerCircle bool `gorm:"index"`
+ Private bool `gorm:"index"`
CreatedAt time.Time
UpdatedAt time.Time
}
@@ -101,6 +102,11 @@ func PaginateForums(user *User, categories []string, pager *Pagination) ([]*Foru
wheres = append(wheres, "inner_circle is not true")
}
+ // Hide private forums except for admins.
+ if !user.IsAdmin {
+ wheres = append(wheres, "private is not true")
+ }
+
// Filters?
if len(wheres) > 0 {
query = query.Where(
diff --git a/pkg/models/forum_recent.go b/pkg/models/forum_recent.go
index e7ec5f4..9f412ae 100644
--- a/pkg/models/forum_recent.go
+++ b/pkg/models/forum_recent.go
@@ -52,6 +52,11 @@ func PaginateRecentPosts(user *User, categories []string, allComments bool, page
wheres = append(wheres, "forums.inner_circle is not true")
}
+ // Private forums.
+ if !user.IsAdmin {
+ wheres = append(wheres, "forums.private is not true")
+ }
+
// Blocked users?
if len(blockedUserIDs) > 0 {
comment_wheres = append(comment_wheres, "comments.user_id NOT IN ?")
diff --git a/pkg/models/forum_search.go b/pkg/models/forum_search.go
index d2d0ff7..7906f95 100644
--- a/pkg/models/forum_search.go
+++ b/pkg/models/forum_search.go
@@ -100,6 +100,11 @@ func SearchForum(user *User, search *Search, filters ForumSearchFilters, pager *
wheres = append(wheres, "forums.inner_circle is not true")
}
+ // Private forums.
+ if !user.IsAdmin {
+ wheres = append(wheres, "forums.private is not true")
+ }
+
// Blocked users?
if len(blockedUserIDs) > 0 {
wheres = append(wheres, "comments.user_id NOT IN ?")
diff --git a/web/templates/forum/add_edit.html b/web/templates/forum/add_edit.html
index 2645174..3b0f4d7 100644
--- a/web/templates/forum/add_edit.html
+++ b/web/templates/forum/add_edit.html
@@ -140,6 +140,19 @@
This forum is only available to inner circle members.
{{end}}
+
+ {{if .CurrentUser.IsAdmin}}
+
+
+ This forum is only visible to admins or approved subscribers.
+
+ {{end}}
diff --git a/web/templates/forum/admin.html b/web/templates/forum/admin.html
index 7e4d080..4f15bbe 100644
--- a/web/templates/forum/admin.html
+++ b/web/templates/forum/admin.html
@@ -87,10 +87,17 @@
{{end}}
-
+ {{if .PermitPhotos}}
+
+
+ Private
+
+ {{end}}
+
+
Created {{.CreatedAt.Format "2006-01-02 15:04:05"}}
-
+
Updated {{.UpdatedAt.Format "2006-01-02 15:04:05"}}
diff --git a/web/templates/forum/index.html b/web/templates/forum/index.html
index 0affced..fc747bc 100644
--- a/web/templates/forum/index.html
+++ b/web/templates/forum/index.html
@@ -102,6 +102,13 @@
Photos
{{end}}
+
+ {{if .Private}}
+
+
+ Private
+
+ {{end}}
diff --git a/web/templates/forum/newest.html b/web/templates/forum/newest.html
index e7c1b11..512c2ca 100644
--- a/web/templates/forum/newest.html
+++ b/web/templates/forum/newest.html
@@ -116,6 +116,10 @@
{{if .Forum.Explicit}}
{{end}}
+
+ {{if .Forum.Private}}
+
+ {{end}}
–
{{SincePrettyCoarse .Thread.Comment.UpdatedAt}} ago