2018-12-24 18:45:15 +01:00
|
|
|
/*
|
2022-11-11 05:49:16 +01:00
|
|
|
* Copyright © 2018-2021 Musing Studio LLC.
|
2018-12-24 18:45:15 +01:00
|
|
|
*
|
|
|
|
* This file is part of WriteFreely.
|
|
|
|
*
|
|
|
|
* WriteFreely is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, included
|
|
|
|
* in the LICENSE file in this source code package.
|
|
|
|
*/
|
2018-12-31 07:05:26 +01:00
|
|
|
|
2018-11-14 21:03:22 +01:00
|
|
|
package writefreely
|
|
|
|
|
|
|
|
import (
|
2019-04-11 19:56:07 +02:00
|
|
|
"database/sql"
|
2018-11-14 21:03:22 +01:00
|
|
|
"fmt"
|
2019-08-12 21:35:17 +02:00
|
|
|
"net/http"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2019-10-03 22:41:50 +02:00
|
|
|
"strings"
|
2019-08-12 21:35:17 +02:00
|
|
|
"time"
|
|
|
|
|
2018-11-19 03:58:50 +01:00
|
|
|
"github.com/gorilla/mux"
|
2018-11-14 21:03:22 +01:00
|
|
|
"github.com/writeas/impart"
|
|
|
|
"github.com/writeas/web-core/auth"
|
2018-12-10 22:02:42 +01:00
|
|
|
"github.com/writeas/web-core/log"
|
2019-10-03 22:41:50 +02:00
|
|
|
"github.com/writeas/web-core/passgen"
|
2021-04-06 23:24:07 +02:00
|
|
|
"github.com/writefreely/writefreely/appstats"
|
|
|
|
"github.com/writefreely/writefreely/config"
|
2018-11-14 21:03:22 +01:00
|
|
|
)
|
|
|
|
|
2018-11-19 02:18:22 +01:00
|
|
|
var (
|
|
|
|
appStartTime = time.Now()
|
|
|
|
sysStatus systemStatus
|
|
|
|
)
|
|
|
|
|
2019-01-05 15:37:53 +01:00
|
|
|
const adminUsersPerPage = 30
|
|
|
|
|
2018-11-19 02:18:22 +01:00
|
|
|
type systemStatus struct {
|
|
|
|
Uptime string
|
|
|
|
NumGoroutine int
|
|
|
|
|
|
|
|
// General statistics.
|
|
|
|
MemAllocated string // bytes allocated and still in use
|
|
|
|
MemTotal string // bytes allocated (even if freed)
|
|
|
|
MemSys string // bytes obtained from system (sum of XxxSys below)
|
|
|
|
Lookups uint64 // number of pointer lookups
|
|
|
|
MemMallocs uint64 // number of mallocs
|
|
|
|
MemFrees uint64 // number of frees
|
|
|
|
|
|
|
|
// Main allocation heap statistics.
|
|
|
|
HeapAlloc string // bytes allocated and still in use
|
|
|
|
HeapSys string // bytes obtained from system
|
|
|
|
HeapIdle string // bytes in idle spans
|
|
|
|
HeapInuse string // bytes in non-idle span
|
|
|
|
HeapReleased string // bytes released to the OS
|
|
|
|
HeapObjects uint64 // total number of allocated objects
|
|
|
|
|
|
|
|
// Low-level fixed-size structure allocator statistics.
|
|
|
|
// Inuse is bytes used now.
|
|
|
|
// Sys is bytes obtained from system.
|
|
|
|
StackInuse string // bootstrap stacks
|
|
|
|
StackSys string
|
|
|
|
MSpanInuse string // mspan structures
|
|
|
|
MSpanSys string
|
|
|
|
MCacheInuse string // mcache structures
|
|
|
|
MCacheSys string
|
|
|
|
BuckHashSys string // profiling bucket hash table
|
|
|
|
GCSys string // GC metadata
|
|
|
|
OtherSys string // other system allocations
|
|
|
|
|
|
|
|
// Garbage collector statistics.
|
|
|
|
NextGC string // next run in HeapAlloc time (bytes)
|
|
|
|
LastGC string // last run in absolute time (ns)
|
|
|
|
PauseTotalNs string
|
|
|
|
PauseNs string // circular buffer of recent GC pause times, most recent at [(NumGC+255)%256]
|
|
|
|
NumGC uint32
|
|
|
|
}
|
|
|
|
|
2019-01-05 04:28:29 +01:00
|
|
|
type inspectedCollection struct {
|
|
|
|
CollectionObj
|
|
|
|
Followers int
|
|
|
|
LastPost string
|
|
|
|
}
|
|
|
|
|
2019-04-06 19:23:22 +02:00
|
|
|
type instanceContent struct {
|
|
|
|
ID string
|
|
|
|
Type string
|
2019-04-11 19:56:07 +02:00
|
|
|
Title sql.NullString
|
2019-04-06 19:23:22 +02:00
|
|
|
Content string
|
|
|
|
Updated time.Time
|
|
|
|
}
|
|
|
|
|
2020-02-15 18:53:47 +01:00
|
|
|
type AdminPage struct {
|
|
|
|
UpdateAvailable bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAdminPage(app *App) *AdminPage {
|
2020-02-25 21:00:58 +01:00
|
|
|
ap := &AdminPage{}
|
|
|
|
if app.updates != nil {
|
|
|
|
ap.UpdateAvailable = app.updates.AreAvailableNoCheck()
|
2020-02-15 18:53:47 +01:00
|
|
|
}
|
2020-02-25 21:00:58 +01:00
|
|
|
return ap
|
2020-02-15 18:53:47 +01:00
|
|
|
}
|
|
|
|
|
2019-04-06 19:23:22 +02:00
|
|
|
func (c instanceContent) UpdatedFriendly() string {
|
|
|
|
/*
|
|
|
|
// TODO: accept a locale in this method and use that for the format
|
|
|
|
var loc monday.Locale = monday.LocaleEnUS
|
|
|
|
return monday.Format(u.Created, monday.DateTimeFormatsByLocale[loc], loc)
|
|
|
|
*/
|
|
|
|
return c.Updated.Format("January 2, 2006, 3:04 PM")
|
|
|
|
}
|
|
|
|
|
2019-05-12 22:55:30 +02:00
|
|
|
func handleViewAdminDash(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
|
2020-02-14 19:55:24 +01:00
|
|
|
p := struct {
|
|
|
|
*UserPage
|
2020-02-15 18:53:47 +01:00
|
|
|
*AdminPage
|
2020-02-14 19:55:24 +01:00
|
|
|
Message string
|
|
|
|
|
|
|
|
UsersCount, CollectionsCount, PostsCount int64
|
|
|
|
}{
|
2020-02-15 18:53:47 +01:00
|
|
|
UserPage: NewUserPage(app, r, u, "Admin", nil),
|
|
|
|
AdminPage: NewAdminPage(app),
|
|
|
|
Message: r.FormValue("m"),
|
2020-02-14 19:55:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get user stats
|
|
|
|
p.UsersCount = app.db.GetAllUsersCount()
|
|
|
|
var err error
|
|
|
|
p.CollectionsCount, err = app.db.GetTotalCollections()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.PostsCount, err = app.db.GetTotalPosts()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
showUserPage(w, "admin", p)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleViewAdminMonitor(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
|
2018-11-19 02:18:22 +01:00
|
|
|
updateAppStats()
|
|
|
|
p := struct {
|
|
|
|
*UserPage
|
2020-02-15 18:53:47 +01:00
|
|
|
*AdminPage
|
2018-11-19 02:18:22 +01:00
|
|
|
SysStatus systemStatus
|
2018-12-03 23:30:31 +01:00
|
|
|
Config config.AppCfg
|
|
|
|
|
|
|
|
Message, ConfigMessage string
|
2018-11-19 02:18:22 +01:00
|
|
|
}{
|
2018-11-19 03:58:50 +01:00
|
|
|
UserPage: NewUserPage(app, r, u, "Admin", nil),
|
2020-02-15 18:53:47 +01:00
|
|
|
AdminPage: NewAdminPage(app),
|
2018-11-19 03:58:50 +01:00
|
|
|
SysStatus: sysStatus,
|
2018-12-03 23:30:31 +01:00
|
|
|
Config: app.cfg.App,
|
|
|
|
|
|
|
|
Message: r.FormValue("m"),
|
|
|
|
ConfigMessage: r.FormValue("cm"),
|
2018-11-19 03:58:50 +01:00
|
|
|
}
|
|
|
|
|
2020-02-14 19:55:24 +01:00
|
|
|
showUserPage(w, "monitor", p)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleViewAdminSettings(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
|
|
|
|
p := struct {
|
|
|
|
*UserPage
|
2020-02-15 18:53:47 +01:00
|
|
|
*AdminPage
|
2020-02-14 19:55:24 +01:00
|
|
|
Config config.AppCfg
|
|
|
|
|
|
|
|
Message, ConfigMessage string
|
|
|
|
}{
|
2020-02-15 18:53:47 +01:00
|
|
|
UserPage: NewUserPage(app, r, u, "Admin", nil),
|
|
|
|
AdminPage: NewAdminPage(app),
|
|
|
|
Config: app.cfg.App,
|
2020-02-14 19:55:24 +01:00
|
|
|
|
|
|
|
Message: r.FormValue("m"),
|
|
|
|
ConfigMessage: r.FormValue("cm"),
|
|
|
|
}
|
|
|
|
|
|
|
|
showUserPage(w, "app-settings", p)
|
2018-11-19 02:18:22 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-12 22:55:30 +02:00
|
|
|
func handleViewAdminUsers(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
|
2019-01-05 04:28:29 +01:00
|
|
|
p := struct {
|
|
|
|
*UserPage
|
2020-02-15 18:53:47 +01:00
|
|
|
*AdminPage
|
2019-01-05 04:28:29 +01:00
|
|
|
Config config.AppCfg
|
|
|
|
Message string
|
2019-11-05 19:44:06 +01:00
|
|
|
Flashes []string
|
2019-01-05 04:28:29 +01:00
|
|
|
|
2019-01-05 15:37:53 +01:00
|
|
|
Users *[]User
|
|
|
|
CurPage int
|
|
|
|
TotalUsers int64
|
|
|
|
TotalPages []int
|
2019-01-05 04:28:29 +01:00
|
|
|
}{
|
2020-02-15 18:53:47 +01:00
|
|
|
UserPage: NewUserPage(app, r, u, "Users", nil),
|
|
|
|
AdminPage: NewAdminPage(app),
|
|
|
|
Config: app.cfg.App,
|
|
|
|
Message: r.FormValue("m"),
|
2019-01-05 04:28:29 +01:00
|
|
|
}
|
|
|
|
|
2019-11-05 19:44:06 +01:00
|
|
|
p.Flashes, _ = getSessionFlashes(app, w, r, nil)
|
2019-01-05 15:37:53 +01:00
|
|
|
p.TotalUsers = app.db.GetAllUsersCount()
|
|
|
|
ttlPages := p.TotalUsers / adminUsersPerPage
|
|
|
|
p.TotalPages = []int{}
|
|
|
|
for i := 1; i <= int(ttlPages); i++ {
|
|
|
|
p.TotalPages = append(p.TotalPages, i)
|
|
|
|
}
|
|
|
|
|
2019-01-05 04:28:29 +01:00
|
|
|
var err error
|
2019-01-05 15:37:53 +01:00
|
|
|
p.CurPage, err = strconv.Atoi(r.FormValue("p"))
|
|
|
|
if err != nil || p.CurPage < 1 {
|
|
|
|
p.CurPage = 1
|
|
|
|
} else if p.CurPage > int(ttlPages) {
|
|
|
|
p.CurPage = int(ttlPages)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Users, err = app.db.GetAllUsers(uint(p.CurPage))
|
2019-01-05 04:28:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get users: %v", err)}
|
|
|
|
}
|
|
|
|
|
|
|
|
showUserPage(w, "users", p)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-12 22:55:30 +02:00
|
|
|
func handleViewAdminUser(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
|
2019-01-05 04:28:29 +01:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
username := vars["username"]
|
|
|
|
if username == "" {
|
|
|
|
return impart.HTTPError{http.StatusFound, "/admin/users"}
|
|
|
|
}
|
|
|
|
|
|
|
|
p := struct {
|
|
|
|
*UserPage
|
2020-02-15 18:53:47 +01:00
|
|
|
*AdminPage
|
2019-01-05 04:28:29 +01:00
|
|
|
Config config.AppCfg
|
|
|
|
Message string
|
|
|
|
|
2019-10-03 22:41:50 +02:00
|
|
|
User *User
|
|
|
|
Colls []inspectedCollection
|
|
|
|
LastPost string
|
|
|
|
NewPassword string
|
|
|
|
TotalPosts int64
|
2019-11-11 10:03:19 +01:00
|
|
|
ClearEmail string
|
2019-01-05 04:28:29 +01:00
|
|
|
}{
|
2020-02-25 21:03:29 +01:00
|
|
|
AdminPage: NewAdminPage(app),
|
|
|
|
Config: app.cfg.App,
|
|
|
|
Message: r.FormValue("m"),
|
|
|
|
Colls: []inspectedCollection{},
|
2019-01-05 04:28:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
p.User, err = app.db.GetUserForAuth(username)
|
|
|
|
if err != nil {
|
2020-02-08 20:46:05 +01:00
|
|
|
if err == ErrUserNotFound {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Error("Could not get user: %v", err)
|
|
|
|
return impart.HTTPError{http.StatusInternalServerError, err.Error()}
|
2019-01-05 04:28:29 +01:00
|
|
|
}
|
2019-11-11 13:40:49 +01:00
|
|
|
|
2019-10-03 22:41:50 +02:00
|
|
|
flashes, _ := getSessionFlashes(app, w, r, nil)
|
|
|
|
for _, flash := range flashes {
|
|
|
|
if strings.HasPrefix(flash, "SUCCESS: ") {
|
|
|
|
p.NewPassword = strings.TrimPrefix(flash, "SUCCESS: ")
|
2019-11-11 13:40:49 +01:00
|
|
|
p.ClearEmail = p.User.EmailClear(app.keys)
|
2019-10-03 22:41:50 +02:00
|
|
|
}
|
2019-01-05 04:28:29 +01:00
|
|
|
}
|
|
|
|
p.UserPage = NewUserPage(app, r, u, p.User.Username, nil)
|
|
|
|
p.TotalPosts = app.db.GetUserPostsCount(p.User.ID)
|
|
|
|
lp, err := app.db.GetUserLastPostTime(p.User.ID)
|
|
|
|
if err != nil {
|
|
|
|
return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get user's last post time: %v", err)}
|
|
|
|
}
|
|
|
|
if lp != nil {
|
|
|
|
p.LastPost = lp.Format("January 2, 2006, 3:04 PM")
|
|
|
|
}
|
|
|
|
|
2019-08-12 21:35:17 +02:00
|
|
|
colls, err := app.db.GetCollections(p.User, app.cfg.App.Host)
|
2019-01-05 04:28:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get user's collections: %v", err)}
|
|
|
|
}
|
|
|
|
for _, c := range *colls {
|
|
|
|
ic := inspectedCollection{
|
|
|
|
CollectionObj: CollectionObj{Collection: c},
|
|
|
|
}
|
|
|
|
|
|
|
|
if app.cfg.App.Federation {
|
|
|
|
folls, err := app.db.GetAPFollowers(&c)
|
|
|
|
if err == nil {
|
|
|
|
// TODO: handle error here (at least log it)
|
|
|
|
ic.Followers = len(*folls)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
app.db.GetPostsCount(&ic.CollectionObj, true)
|
|
|
|
|
|
|
|
lp, err := app.db.GetCollectionLastPostTime(c.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Didn't get last post time for collection %d: %v", c.ID, err)
|
|
|
|
}
|
|
|
|
if lp != nil {
|
|
|
|
ic.LastPost = lp.Format("January 2, 2006, 3:04 PM")
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Colls = append(p.Colls, ic)
|
|
|
|
}
|
|
|
|
|
|
|
|
showUserPage(w, "view-user", p)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-05 19:44:06 +01:00
|
|
|
func handleAdminDeleteUser(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
|
|
|
|
if !u.IsAdmin() {
|
|
|
|
return impart.HTTPError{http.StatusForbidden, "Administrator privileges required for this action"}
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
username := vars["username"]
|
|
|
|
confirmUsername := r.PostFormValue("confirm-username")
|
|
|
|
|
|
|
|
if confirmUsername != username {
|
|
|
|
return impart.HTTPError{http.StatusBadRequest, "Username was not confirmed"}
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := app.db.GetUserForAuth(username)
|
|
|
|
if err == ErrUserNotFound {
|
|
|
|
return impart.HTTPError{http.StatusNotFound, fmt.Sprintf("User '%s' was not found", username)}
|
|
|
|
} else if err != nil {
|
|
|
|
log.Error("get user for deletion: %v", err)
|
|
|
|
return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get user with username '%s': %v", username, err)}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = app.db.DeleteAccount(user.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("delete user %s: %v", user.Username, err)
|
|
|
|
return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not delete user account for '%s': %v", username, err)}
|
|
|
|
}
|
|
|
|
|
2021-04-22 16:11:18 +02:00
|
|
|
_ = addSessionFlash(app, w, r, fmt.Sprintf("User \"%s\" was deleted successfully.", username), nil)
|
2019-11-05 19:44:06 +01:00
|
|
|
return impart.HTTPError{http.StatusFound, "/admin/users"}
|
|
|
|
}
|
|
|
|
|
2019-10-25 21:04:24 +02:00
|
|
|
func handleAdminToggleUserStatus(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
|
2019-08-28 21:37:45 +02:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
username := vars["username"]
|
|
|
|
if username == "" {
|
|
|
|
return impart.HTTPError{http.StatusFound, "/admin/users"}
|
|
|
|
}
|
|
|
|
|
2019-10-25 21:04:24 +02:00
|
|
|
user, err := app.db.GetUserForAuth(username)
|
2019-08-28 21:37:45 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to get user: %v", err)
|
|
|
|
return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get user from username: %v", err)}
|
|
|
|
}
|
2019-11-11 16:40:16 +01:00
|
|
|
if user.IsSilenced() {
|
2019-10-25 21:04:24 +02:00
|
|
|
err = app.db.SetUserStatus(user.ID, UserActive)
|
2019-08-28 21:37:45 +02:00
|
|
|
} else {
|
2019-11-11 16:40:16 +01:00
|
|
|
err = app.db.SetUserStatus(user.ID, UserSilenced)
|
2020-12-06 12:45:45 +01:00
|
|
|
|
|
|
|
// reset the cache to removed silence user posts
|
|
|
|
updateTimelineCache(app.timeline, true)
|
2019-08-28 21:37:45 +02:00
|
|
|
}
|
|
|
|
if err != nil {
|
2019-11-12 00:21:45 +01:00
|
|
|
log.Error("toggle user silenced: %v", err)
|
2019-12-19 17:48:04 +01:00
|
|
|
return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not toggle user status: %v", err)}
|
2019-08-28 21:37:45 +02:00
|
|
|
}
|
|
|
|
return impart.HTTPError{http.StatusFound, fmt.Sprintf("/admin/user/%s#status", username)}
|
|
|
|
}
|
|
|
|
|
2019-10-03 22:41:50 +02:00
|
|
|
func handleAdminResetUserPass(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
username := vars["username"]
|
|
|
|
if username == "" {
|
|
|
|
return impart.HTTPError{http.StatusFound, "/admin/users"}
|
|
|
|
}
|
2019-11-11 17:46:37 +01:00
|
|
|
|
2019-10-03 22:41:50 +02:00
|
|
|
// Generate new random password since none supplied
|
2019-11-11 10:01:08 +01:00
|
|
|
pass := passgen.NewWordish()
|
2019-10-03 22:41:50 +02:00
|
|
|
hashedPass, err := auth.HashPass([]byte(pass))
|
|
|
|
if err != nil {
|
|
|
|
return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not create password hash: %v", err)}
|
|
|
|
}
|
|
|
|
|
|
|
|
userIDVal := r.FormValue("user")
|
|
|
|
log.Info("ADMIN: Changing user %s password", userIDVal)
|
|
|
|
id, err := strconv.Atoi(userIDVal)
|
|
|
|
if err != nil {
|
|
|
|
return impart.HTTPError{http.StatusBadRequest, fmt.Sprintf("Invalid user ID: %v", err)}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = app.db.ChangePassphrase(int64(id), true, "", hashedPass)
|
|
|
|
if err != nil {
|
|
|
|
return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not update passphrase: %v", err)}
|
|
|
|
}
|
|
|
|
log.Info("ADMIN: Successfully changed.")
|
|
|
|
|
|
|
|
addSessionFlash(app, w, r, fmt.Sprintf("SUCCESS: %s", pass), nil)
|
|
|
|
|
|
|
|
return impart.HTTPError{http.StatusFound, fmt.Sprintf("/admin/user/%s", username)}
|
|
|
|
}
|
|
|
|
|
2019-05-12 22:55:30 +02:00
|
|
|
func handleViewAdminPages(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
|
2019-04-06 19:23:22 +02:00
|
|
|
p := struct {
|
|
|
|
*UserPage
|
2020-02-15 18:53:47 +01:00
|
|
|
*AdminPage
|
2019-04-06 19:23:22 +02:00
|
|
|
Config config.AppCfg
|
|
|
|
Message string
|
|
|
|
|
|
|
|
Pages []*instanceContent
|
|
|
|
}{
|
2020-02-15 18:53:47 +01:00
|
|
|
UserPage: NewUserPage(app, r, u, "Pages", nil),
|
|
|
|
AdminPage: NewAdminPage(app),
|
|
|
|
Config: app.cfg.App,
|
|
|
|
Message: r.FormValue("m"),
|
2019-04-06 19:23:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
p.Pages, err = app.db.GetInstancePages()
|
|
|
|
if err != nil {
|
|
|
|
return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get pages: %v", err)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add in default pages
|
|
|
|
var hasAbout, hasPrivacy bool
|
2019-04-11 19:56:07 +02:00
|
|
|
for i, c := range p.Pages {
|
2019-04-06 19:23:22 +02:00
|
|
|
if hasAbout && hasPrivacy {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if c.ID == "about" {
|
|
|
|
hasAbout = true
|
2019-04-11 19:56:07 +02:00
|
|
|
if !c.Title.Valid {
|
|
|
|
p.Pages[i].Title = defaultAboutTitle(app.cfg)
|
|
|
|
}
|
2019-04-06 19:23:22 +02:00
|
|
|
} else if c.ID == "privacy" {
|
|
|
|
hasPrivacy = true
|
2019-04-11 19:56:07 +02:00
|
|
|
if !c.Title.Valid {
|
|
|
|
p.Pages[i].Title = defaultPrivacyTitle()
|
|
|
|
}
|
2019-04-06 19:23:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !hasAbout {
|
|
|
|
p.Pages = append(p.Pages, &instanceContent{
|
|
|
|
ID: "about",
|
2019-04-11 19:56:07 +02:00
|
|
|
Title: defaultAboutTitle(app.cfg),
|
2019-04-06 19:23:22 +02:00
|
|
|
Content: defaultAboutPage(app.cfg),
|
|
|
|
Updated: defaultPageUpdatedTime,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if !hasPrivacy {
|
|
|
|
p.Pages = append(p.Pages, &instanceContent{
|
|
|
|
ID: "privacy",
|
2019-04-11 19:56:07 +02:00
|
|
|
Title: defaultPrivacyTitle(),
|
2019-04-06 19:23:22 +02:00
|
|
|
Content: defaultPrivacyPolicy(app.cfg),
|
|
|
|
Updated: defaultPageUpdatedTime,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
showUserPage(w, "pages", p)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-12 22:55:30 +02:00
|
|
|
func handleViewAdminPage(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
|
2019-04-06 19:23:22 +02:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
slug := vars["slug"]
|
|
|
|
if slug == "" {
|
|
|
|
return impart.HTTPError{http.StatusFound, "/admin/pages"}
|
|
|
|
}
|
|
|
|
|
|
|
|
p := struct {
|
|
|
|
*UserPage
|
2020-02-15 18:53:47 +01:00
|
|
|
*AdminPage
|
2019-04-06 19:23:22 +02:00
|
|
|
Config config.AppCfg
|
|
|
|
Message string
|
|
|
|
|
2019-06-27 23:06:37 +02:00
|
|
|
Banner *instanceContent
|
2019-04-06 19:23:22 +02:00
|
|
|
Content *instanceContent
|
|
|
|
}{
|
2020-02-25 21:01:24 +01:00
|
|
|
AdminPage: NewAdminPage(app),
|
|
|
|
Config: app.cfg.App,
|
|
|
|
Message: r.FormValue("m"),
|
2019-04-06 19:23:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
// Get pre-defined pages, or select slug
|
|
|
|
if slug == "about" {
|
|
|
|
p.Content, err = getAboutPage(app)
|
|
|
|
} else if slug == "privacy" {
|
|
|
|
p.Content, err = getPrivacyPage(app)
|
2019-06-27 23:06:37 +02:00
|
|
|
} else if slug == "landing" {
|
|
|
|
p.Banner, err = getLandingBanner(app)
|
|
|
|
if err != nil {
|
|
|
|
return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get banner: %v", err)}
|
|
|
|
}
|
2019-06-28 04:22:21 +02:00
|
|
|
p.Content, err = getLandingBody(app)
|
2019-06-27 23:06:37 +02:00
|
|
|
p.Content.ID = "landing"
|
2019-08-09 17:16:38 +02:00
|
|
|
} else if slug == "reader" {
|
|
|
|
p.Content, err = getReaderSection(app)
|
2019-04-06 19:23:22 +02:00
|
|
|
} else {
|
|
|
|
p.Content, err = app.db.GetDynamicContent(slug)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not get page: %v", err)}
|
|
|
|
}
|
2019-04-11 19:56:07 +02:00
|
|
|
title := "New page"
|
|
|
|
if p.Content != nil {
|
|
|
|
title = "Edit " + p.Content.ID
|
|
|
|
} else {
|
|
|
|
p.Content = &instanceContent{}
|
|
|
|
}
|
|
|
|
p.UserPage = NewUserPage(app, r, u, title, nil)
|
2019-04-06 19:23:22 +02:00
|
|
|
|
|
|
|
showUserPage(w, "view-page", p)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-12 22:55:30 +02:00
|
|
|
func handleAdminUpdateSite(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
|
2018-11-19 03:58:50 +01:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
id := vars["page"]
|
|
|
|
|
|
|
|
// Validate
|
2019-08-09 17:16:38 +02:00
|
|
|
if id != "about" && id != "privacy" && id != "landing" && id != "reader" {
|
2018-11-19 03:58:50 +01:00
|
|
|
return impart.HTTPError{http.StatusNotFound, "No such page."}
|
|
|
|
}
|
|
|
|
|
2019-06-27 23:06:37 +02:00
|
|
|
var err error
|
2018-11-19 03:58:50 +01:00
|
|
|
m := ""
|
2019-06-27 23:06:37 +02:00
|
|
|
if id == "landing" {
|
|
|
|
// Handle special landing page
|
|
|
|
err = app.db.UpdateDynamicContent("landing-banner", "", r.FormValue("banner"), "section")
|
|
|
|
if err != nil {
|
|
|
|
m = "?m=" + err.Error()
|
|
|
|
return impart.HTTPError{http.StatusFound, "/admin/page/" + id + m}
|
|
|
|
}
|
|
|
|
err = app.db.UpdateDynamicContent("landing-body", "", r.FormValue("content"), "section")
|
2019-08-09 17:16:38 +02:00
|
|
|
} else if id == "reader" {
|
|
|
|
// Update sections with titles
|
|
|
|
err = app.db.UpdateDynamicContent(id, r.FormValue("title"), r.FormValue("content"), "section")
|
2019-06-27 23:06:37 +02:00
|
|
|
} else {
|
|
|
|
// Update page
|
|
|
|
err = app.db.UpdateDynamicContent(id, r.FormValue("title"), r.FormValue("content"), "page")
|
|
|
|
}
|
2018-11-19 03:58:50 +01:00
|
|
|
if err != nil {
|
|
|
|
m = "?m=" + err.Error()
|
|
|
|
}
|
2019-04-06 19:23:22 +02:00
|
|
|
return impart.HTTPError{http.StatusFound, "/admin/page/" + id + m}
|
2018-11-19 03:58:50 +01:00
|
|
|
}
|
|
|
|
|
2019-06-14 03:56:13 +02:00
|
|
|
func handleAdminUpdateConfig(apper Apper, u *User, w http.ResponseWriter, r *http.Request) error {
|
|
|
|
apper.App().cfg.App.SiteName = r.FormValue("site_name")
|
|
|
|
apper.App().cfg.App.SiteDesc = r.FormValue("site_desc")
|
2019-06-16 23:38:34 +02:00
|
|
|
apper.App().cfg.App.Landing = r.FormValue("landing")
|
2019-06-14 03:56:13 +02:00
|
|
|
apper.App().cfg.App.OpenRegistration = r.FormValue("open_registration") == "on"
|
2021-04-22 19:13:47 +02:00
|
|
|
apper.App().cfg.App.OpenDeletion = r.FormValue("open_deletion") == "on"
|
2018-12-03 23:30:31 +01:00
|
|
|
mul, err := strconv.Atoi(r.FormValue("min_username_len"))
|
|
|
|
if err == nil {
|
2019-06-14 03:56:13 +02:00
|
|
|
apper.App().cfg.App.MinUsernameLen = mul
|
2018-12-03 23:30:31 +01:00
|
|
|
}
|
|
|
|
mb, err := strconv.Atoi(r.FormValue("max_blogs"))
|
|
|
|
if err == nil {
|
2019-06-14 03:56:13 +02:00
|
|
|
apper.App().cfg.App.MaxBlogs = mb
|
2018-12-03 23:30:31 +01:00
|
|
|
}
|
2019-06-14 03:56:13 +02:00
|
|
|
apper.App().cfg.App.Federation = r.FormValue("federation") == "on"
|
|
|
|
apper.App().cfg.App.PublicStats = r.FormValue("public_stats") == "on"
|
2020-09-30 21:18:21 +02:00
|
|
|
apper.App().cfg.App.Monetization = r.FormValue("monetization") == "on"
|
2019-06-14 03:56:13 +02:00
|
|
|
apper.App().cfg.App.Private = r.FormValue("private") == "on"
|
|
|
|
apper.App().cfg.App.LocalTimeline = r.FormValue("local_timeline") == "on"
|
|
|
|
if apper.App().cfg.App.LocalTimeline && apper.App().timeline == nil {
|
2018-12-10 22:02:42 +01:00
|
|
|
log.Info("Initializing local timeline...")
|
2019-06-14 03:56:13 +02:00
|
|
|
initLocalTimeline(apper.App())
|
2018-12-10 22:02:42 +01:00
|
|
|
}
|
2019-06-14 03:56:13 +02:00
|
|
|
apper.App().cfg.App.UserInvites = r.FormValue("user_invites")
|
|
|
|
if apper.App().cfg.App.UserInvites == "none" {
|
|
|
|
apper.App().cfg.App.UserInvites = ""
|
2019-01-18 06:05:50 +01:00
|
|
|
}
|
2019-08-01 04:20:00 +02:00
|
|
|
apper.App().cfg.App.DefaultVisibility = r.FormValue("default_visibility")
|
2018-12-03 23:30:31 +01:00
|
|
|
|
|
|
|
m := "?cm=Configuration+saved."
|
2019-06-14 03:56:13 +02:00
|
|
|
err = apper.SaveConfig(apper.App().cfg)
|
2018-12-03 23:30:31 +01:00
|
|
|
if err != nil {
|
|
|
|
m = "?cm=" + err.Error()
|
|
|
|
}
|
2020-02-14 19:55:24 +01:00
|
|
|
return impart.HTTPError{http.StatusFound, "/admin/settings" + m + "#config"}
|
2018-12-03 23:30:31 +01:00
|
|
|
}
|
|
|
|
|
2018-11-19 02:18:22 +01:00
|
|
|
func updateAppStats() {
|
2019-09-13 17:22:38 +02:00
|
|
|
sysStatus.Uptime = appstats.TimeSincePro(appStartTime)
|
2018-11-19 02:18:22 +01:00
|
|
|
|
|
|
|
m := new(runtime.MemStats)
|
|
|
|
runtime.ReadMemStats(m)
|
|
|
|
sysStatus.NumGoroutine = runtime.NumGoroutine()
|
|
|
|
|
2019-09-13 17:22:38 +02:00
|
|
|
sysStatus.MemAllocated = appstats.FileSize(int64(m.Alloc))
|
|
|
|
sysStatus.MemTotal = appstats.FileSize(int64(m.TotalAlloc))
|
|
|
|
sysStatus.MemSys = appstats.FileSize(int64(m.Sys))
|
2018-11-19 02:18:22 +01:00
|
|
|
sysStatus.Lookups = m.Lookups
|
|
|
|
sysStatus.MemMallocs = m.Mallocs
|
|
|
|
sysStatus.MemFrees = m.Frees
|
|
|
|
|
2019-09-13 17:22:38 +02:00
|
|
|
sysStatus.HeapAlloc = appstats.FileSize(int64(m.HeapAlloc))
|
|
|
|
sysStatus.HeapSys = appstats.FileSize(int64(m.HeapSys))
|
|
|
|
sysStatus.HeapIdle = appstats.FileSize(int64(m.HeapIdle))
|
|
|
|
sysStatus.HeapInuse = appstats.FileSize(int64(m.HeapInuse))
|
|
|
|
sysStatus.HeapReleased = appstats.FileSize(int64(m.HeapReleased))
|
2018-11-19 02:18:22 +01:00
|
|
|
sysStatus.HeapObjects = m.HeapObjects
|
|
|
|
|
2019-09-13 17:22:38 +02:00
|
|
|
sysStatus.StackInuse = appstats.FileSize(int64(m.StackInuse))
|
|
|
|
sysStatus.StackSys = appstats.FileSize(int64(m.StackSys))
|
|
|
|
sysStatus.MSpanInuse = appstats.FileSize(int64(m.MSpanInuse))
|
|
|
|
sysStatus.MSpanSys = appstats.FileSize(int64(m.MSpanSys))
|
|
|
|
sysStatus.MCacheInuse = appstats.FileSize(int64(m.MCacheInuse))
|
|
|
|
sysStatus.MCacheSys = appstats.FileSize(int64(m.MCacheSys))
|
|
|
|
sysStatus.BuckHashSys = appstats.FileSize(int64(m.BuckHashSys))
|
|
|
|
sysStatus.GCSys = appstats.FileSize(int64(m.GCSys))
|
|
|
|
sysStatus.OtherSys = appstats.FileSize(int64(m.OtherSys))
|
|
|
|
|
|
|
|
sysStatus.NextGC = appstats.FileSize(int64(m.NextGC))
|
2018-11-19 02:18:22 +01:00
|
|
|
sysStatus.LastGC = fmt.Sprintf("%.1fs", float64(time.Now().UnixNano()-int64(m.LastGC))/1000/1000/1000)
|
|
|
|
sysStatus.PauseTotalNs = fmt.Sprintf("%.1fs", float64(m.PauseTotalNs)/1000/1000/1000)
|
|
|
|
sysStatus.PauseNs = fmt.Sprintf("%.3fs", float64(m.PauseNs[(m.NumGC+255)%256])/1000/1000/1000)
|
|
|
|
sysStatus.NumGC = m.NumGC
|
|
|
|
}
|
|
|
|
|
2019-05-12 22:55:30 +02:00
|
|
|
func adminResetPassword(app *App, u *User, newPass string) error {
|
2018-11-14 21:03:22 +01:00
|
|
|
hashedPass, err := auth.HashPass([]byte(newPass))
|
|
|
|
if err != nil {
|
|
|
|
return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not create password hash: %v", err)}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = app.db.ChangePassphrase(u.ID, true, "", hashedPass)
|
|
|
|
if err != nil {
|
|
|
|
return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not update passphrase: %v", err)}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-08-30 00:05:59 +02:00
|
|
|
|
|
|
|
func handleViewAdminUpdates(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
|
|
|
|
check := r.URL.Query().Get("check")
|
|
|
|
|
|
|
|
if check == "now" && app.cfg.App.UpdateChecks {
|
|
|
|
app.updates.CheckNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
p := struct {
|
|
|
|
*UserPage
|
2020-02-15 18:53:47 +01:00
|
|
|
*AdminPage
|
2020-02-15 18:19:06 +01:00
|
|
|
CurReleaseNotesURL string
|
2020-02-14 22:13:54 +01:00
|
|
|
LastChecked string
|
2020-02-15 17:57:55 +01:00
|
|
|
LastChecked8601 string
|
2020-02-14 22:13:54 +01:00
|
|
|
LatestVersion string
|
2020-02-15 17:59:47 +01:00
|
|
|
LatestReleaseURL string
|
2020-02-14 22:13:54 +01:00
|
|
|
LatestReleaseNotesURL string
|
2020-02-15 18:56:33 +01:00
|
|
|
CheckFailed bool
|
2019-08-30 00:05:59 +02:00
|
|
|
}{
|
2020-02-15 18:53:47 +01:00
|
|
|
UserPage: NewUserPage(app, r, u, "Updates", nil),
|
|
|
|
AdminPage: NewAdminPage(app),
|
2019-08-30 00:05:59 +02:00
|
|
|
}
|
2020-02-15 18:19:06 +01:00
|
|
|
p.CurReleaseNotesURL = wfReleaseNotesURL(p.Version)
|
2019-08-30 00:05:59 +02:00
|
|
|
if app.cfg.App.UpdateChecks {
|
|
|
|
p.LastChecked = app.updates.lastCheck.Format("January 2, 2006, 3:04 PM")
|
2020-02-15 17:57:55 +01:00
|
|
|
p.LastChecked8601 = app.updates.lastCheck.Format("2006-01-02T15:04:05Z")
|
2019-08-30 00:05:59 +02:00
|
|
|
p.LatestVersion = app.updates.LatestVersion()
|
2020-02-15 17:59:47 +01:00
|
|
|
p.LatestReleaseURL = app.updates.ReleaseURL()
|
2020-02-14 22:13:54 +01:00
|
|
|
p.LatestReleaseNotesURL = app.updates.ReleaseNotesURL()
|
2019-08-30 00:05:59 +02:00
|
|
|
p.UpdateAvailable = app.updates.AreAvailable()
|
2020-02-15 18:56:33 +01:00
|
|
|
p.CheckFailed = app.updates.checkError != nil
|
2019-08-30 00:05:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
showUserPage(w, "app-updates", p)
|
|
|
|
return nil
|
|
|
|
}
|