Private forums (admin only for now)

This commit is contained in:
Noah Petherbridge 2024-05-11 12:23:06 -07:00
parent 0f6dd58c54
commit 9db7343370
10 changed files with 70 additions and 4 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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)

View File

@ -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(

View File

@ -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 ?")

View File

@ -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 ?")

View File

@ -140,6 +140,19 @@
This forum is only available to inner circle members.
</p>
{{end}}
{{if .CurrentUser.IsAdmin}}
<label class="checkbox mt-3">
<input type="checkbox"
name="private"
value="true"
{{if and .EditForum .EditForum.Private}}checked{{end}}>
Private forum
</label>
<p class="help">
This forum is only visible to admins or approved subscribers.
</p>
{{end}}
</div>
<div class="field">

View File

@ -87,10 +87,17 @@
</div>
{{end}}
<div class="tag is-light">
{{if .PermitPhotos}}
<div class="tag is-private is-light">
<span class="icon"><i class="fa fa-lock"></i></span>
Private
</div>
{{end}}
<div class="tag">
Created {{.CreatedAt.Format "2006-01-02 15:04:05"}}
</div>
<div class="tag is-light">
<div class="tag">
Updated {{.UpdatedAt.Format "2006-01-02 15:04:05"}}
</div>
</div>

View File

@ -102,6 +102,13 @@
<span>Photos</span>
</span>
{{end}}
{{if .Private}}
<span class="tag is-private is-light">
<span class="icon"><i class="fa fa-lock"></i></span>
<span>Private</span>
</span>
{{end}}
</div>
</div>

View File

@ -116,6 +116,10 @@
{{if .Forum.Explicit}}
<small class="has-text-danger fa fa-fire"></small>
{{end}}
{{if .Forum.Private}}
<small class="has-text-private fa fa-lock"></small>
{{end}}
</a>
&ndash;
{{SincePrettyCoarse .Thread.Comment.UpdatedAt}} ago