Add pinned and locked to thread table and setup routes
This commit is contained in:
+60
-12
@@ -428,6 +428,8 @@ func main() {
|
||||
ThumbPath: op.ThumbPath,
|
||||
ReplyCount: len(posts),
|
||||
IpCount: len(uniqueIpHashes),
|
||||
Pinned: thread.Pinned,
|
||||
Locked: thread.Locked,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -526,19 +528,50 @@ func main() {
|
||||
r.Route("/admin", func(r chi.Router) {
|
||||
r.Use(AdminOnlyMiddleware)
|
||||
|
||||
r.Post("/logout", func(w http.ResponseWriter, r *http.Request) {
|
||||
if c, err := r.Cookie("comfy_admin"); err == nil {
|
||||
util.DeleteAdminSession(c.Value)
|
||||
r.Patch("/threads/{threadId}/lock", func(w http.ResponseWriter, r *http.Request) {
|
||||
threadIdStr := chi.URLParam(r, "threadId")
|
||||
threadId, err := strconv.Atoi(threadIdStr)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid thread id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
lockedStr := r.URL.Query().Get("locked")
|
||||
locked, err := strconv.ParseBool(lockedStr)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid values for 'locked'", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = db.Exec(`UPDATE threads SET locked = ? WHERE id = ?`, locked, threadId)
|
||||
if err != nil {
|
||||
log.Println("Updating thread 'locked': ", err)
|
||||
http.Error(w, "Failed to lock thread: "+threadIdStr, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
r.Patch("/threads/{threadId}/pin", func(w http.ResponseWriter, r *http.Request) {
|
||||
threadIdStr := chi.URLParam(r, "threadId")
|
||||
threadId, err := strconv.Atoi(threadIdStr)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid thread id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
pinnedStr := r.URL.Query().Get("pinned")
|
||||
pinned, err := strconv.ParseBool(pinnedStr)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid values for 'pinned'", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = db.Exec(`UPDATE threads SET pinned = ? WHERE id = ?`, pinned, threadId)
|
||||
if err != nil {
|
||||
log.Println("Updating thread 'pinned': ", err)
|
||||
http.Error(w, "Failed to pin thread: "+threadIdStr, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: "comfy_admin",
|
||||
Value: "",
|
||||
HttpOnly: true,
|
||||
Secure: !util.DevMode,
|
||||
Expires: time.Now(),
|
||||
SameSite: http.SameSiteStrictMode,
|
||||
Path: "/",
|
||||
})
|
||||
})
|
||||
|
||||
r.Delete("/threads/{threadId}", func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -612,6 +645,21 @@ func main() {
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
r.Post("/logout", func(w http.ResponseWriter, r *http.Request) {
|
||||
if c, err := r.Cookie("comfy_admin"); err == nil {
|
||||
util.DeleteAdminSession(c.Value)
|
||||
}
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: "comfy_admin",
|
||||
Value: "",
|
||||
HttpOnly: true,
|
||||
Secure: !util.DevMode,
|
||||
Expires: time.Now(),
|
||||
SameSite: http.SameSiteStrictMode,
|
||||
Path: "/",
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
// -----------------
|
||||
|
||||
+42
-1
@@ -37,7 +37,7 @@ templ Board(board database.Board, isAdmin bool) {
|
||||
<option value="large">Large</option>
|
||||
</select>
|
||||
<input style="margin-left: 10px;" oninput="applyCatalogSearch()" id="catalogSearch" placeholder="Search"/>
|
||||
<a style="margin-left: 10px;" _="on click trigger refreshPosts on body" class="link-button">[Refresh]</a>
|
||||
<a style _="on click trigger refreshPosts on body" class="link-button">[Refresh]</a>
|
||||
</div>
|
||||
<hr/>
|
||||
<div
|
||||
@@ -58,6 +58,8 @@ type CatalogThreadPreview struct {
|
||||
ThumbPath string
|
||||
ReplyCount int
|
||||
IpCount int
|
||||
Pinned bool
|
||||
Locked bool
|
||||
}
|
||||
|
||||
type CatalogContext struct {
|
||||
@@ -95,6 +97,12 @@ templ ThreadsCatalog(previews []CatalogThreadPreview, catalogContext CatalogCont
|
||||
</strong>
|
||||
<h1>
|
||||
<a href={ templ.URL(preview.ThreadURL) } class="catalog-preview-link">{ preview.Subject }</a>
|
||||
if preview.Locked {
|
||||
<span>Locked</span>
|
||||
}
|
||||
if preview.Pinned {
|
||||
<span>Pinned</span>
|
||||
}
|
||||
</h1>
|
||||
<p>
|
||||
@templ.Raw(util.EnrichPost(preview.Body))
|
||||
@@ -111,6 +119,39 @@ templ ThreadsCatalog(previews []CatalogThreadPreview, catalogContext CatalogCont
|
||||
_="on htmx:afterRequest trigger refreshPosts on body"
|
||||
hx-confirm="Are you sure you wish to delete this thread?"
|
||||
>Delete</button>
|
||||
<button
|
||||
class="link-button"
|
||||
hx-patch={ fmt.Sprintf("/admin/threads/%d/pin?pinned=%t", preview.ThreadId, !preview.Pinned) }
|
||||
hx-swap="none"
|
||||
_="on htmx:afterRequest trigger refreshPosts on body"
|
||||
hx-confirm="Are you sure you wish to toggle pin this thread?"
|
||||
>
|
||||
if preview.Pinned {
|
||||
Unpin
|
||||
} else {
|
||||
Pin
|
||||
}
|
||||
</button>
|
||||
<button
|
||||
class="link-button"
|
||||
hx-patch={ fmt.Sprintf("/admin/threads/%d/lock?locked=%t", preview.ThreadId, !preview.Locked) }
|
||||
hx-swap="none"
|
||||
_="on htmx:afterRequest trigger refreshPosts on body"
|
||||
hx-confirm="Are you sure you wish to toggle lock this thread?"
|
||||
>
|
||||
if preview.Locked {
|
||||
Unlock
|
||||
} else {
|
||||
Lock
|
||||
}
|
||||
</button>
|
||||
<button
|
||||
class="link-button"
|
||||
hx-delete={ fmt.Sprintf("/admin/threads/%d", preview.ThreadId) }
|
||||
hx-swap="none"
|
||||
_="on htmx:afterRequest trigger refreshPosts on body"
|
||||
hx-confirm="Are you sure you wish to delete this thread?"
|
||||
>Delete</button>
|
||||
<button
|
||||
class="admin-dialog-close-btn link-button"
|
||||
_={ fmt.Sprintf("on click call #%s-dialog.close()", elThreadId) }
|
||||
|
||||
Reference in New Issue
Block a user