2021-05-09 10:42:12 +00:00
|
|
|
package web
|
2020-12-08 09:04:24 +00:00
|
|
|
|
|
|
|
import (
|
2020-12-08 15:15:32 +00:00
|
|
|
"fmt"
|
2020-12-08 09:04:24 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/bouncepaw/mycorrhiza/history"
|
2021-01-24 07:30:14 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/user"
|
2020-12-08 09:04:24 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/util"
|
2021-02-20 16:14:33 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/views"
|
2020-12-08 09:04:24 +00:00
|
|
|
)
|
|
|
|
|
2021-05-09 11:09:27 +00:00
|
|
|
func initHistory() {
|
2020-12-08 09:04:24 +00:00
|
|
|
http.HandleFunc("/history/", handlerHistory)
|
|
|
|
http.HandleFunc("/recent-changes/", handlerRecentChanges)
|
2020-12-08 15:15:32 +00:00
|
|
|
http.HandleFunc("/recent-changes-rss", handlerRecentChangesRSS)
|
|
|
|
http.HandleFunc("/recent-changes-atom", handlerRecentChangesAtom)
|
|
|
|
http.HandleFunc("/recent-changes-json", handlerRecentChangesJSON)
|
2020-12-08 09:04:24 +00:00
|
|
|
}
|
|
|
|
|
2021-05-09 11:09:27 +00:00
|
|
|
// handlerHistory lists all revisions of a hypha.
|
2020-12-08 09:04:24 +00:00
|
|
|
func handlerHistory(w http.ResponseWriter, rq *http.Request) {
|
2021-05-09 10:42:12 +00:00
|
|
|
util.PrepareRq(rq)
|
2021-07-10 19:04:21 +00:00
|
|
|
if shown := user.FromRequest(rq).ShowLockMaybe(w, rq); shown {
|
|
|
|
return
|
|
|
|
}
|
2021-05-09 10:42:12 +00:00
|
|
|
hyphaName := util.HyphaNameFromRq(rq, "history")
|
2020-12-08 09:04:24 +00:00
|
|
|
var list string
|
|
|
|
|
|
|
|
// History can be found for files that do not exist anymore.
|
|
|
|
revs, err := history.Revisions(hyphaName)
|
|
|
|
if err == nil {
|
|
|
|
list = history.HistoryWithRevisions(hyphaName, revs)
|
|
|
|
}
|
|
|
|
log.Println("Found", len(revs), "revisions for", hyphaName)
|
|
|
|
|
|
|
|
util.HTTP200Page(w,
|
2021-05-09 10:42:12 +00:00
|
|
|
views.BaseHTML(hyphaName, views.HistoryHTML(rq, hyphaName, list), user.FromRequest(rq)))
|
2020-12-08 09:04:24 +00:00
|
|
|
}
|
|
|
|
|
2021-05-09 11:09:27 +00:00
|
|
|
// handlerRecentChanges displays the /recent-changes/ page.
|
2020-12-08 09:04:24 +00:00
|
|
|
func handlerRecentChanges(w http.ResponseWriter, rq *http.Request) {
|
2021-05-09 10:42:12 +00:00
|
|
|
util.PrepareRq(rq)
|
2021-07-10 19:04:21 +00:00
|
|
|
if shown := user.FromRequest(rq).ShowLockMaybe(w, rq); shown {
|
|
|
|
return
|
|
|
|
}
|
2020-12-08 09:04:24 +00:00
|
|
|
var (
|
|
|
|
noPrefix = strings.TrimPrefix(rq.URL.String(), "/recent-changes/")
|
|
|
|
n, err = strconv.Atoi(noPrefix)
|
|
|
|
)
|
|
|
|
if err == nil && n < 101 {
|
2021-05-09 10:42:12 +00:00
|
|
|
util.HTTP200Page(w, views.BaseHTML(strconv.Itoa(n)+" recent changes", views.RecentChangesHTML(n), user.FromRequest(rq)))
|
2020-12-08 09:04:24 +00:00
|
|
|
} else {
|
|
|
|
http.Redirect(w, rq, "/recent-changes/20", http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
}
|
2020-12-08 15:15:32 +00:00
|
|
|
|
2021-05-09 11:09:27 +00:00
|
|
|
// genericHandlerOfFeeds is a helper function for the web feed handlers.
|
2020-12-08 15:15:32 +00:00
|
|
|
func genericHandlerOfFeeds(w http.ResponseWriter, rq *http.Request, f func() (string, error), name string) {
|
2021-05-09 10:42:12 +00:00
|
|
|
util.PrepareRq(rq)
|
2021-07-10 19:04:21 +00:00
|
|
|
if shown := user.FromRequest(rq).ShowLockMaybe(w, rq); shown {
|
|
|
|
return
|
|
|
|
}
|
2020-12-08 15:15:32 +00:00
|
|
|
if content, err := f(); err != nil {
|
|
|
|
w.Header().Set("Content-Type", "text/plain;charset=utf-8")
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
fmt.Fprint(w, "An error while generating "+name+": "+err.Error())
|
|
|
|
} else {
|
|
|
|
w.Header().Set("Content-Type", "application/rss+xml")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
fmt.Fprint(w, content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlerRecentChangesRSS(w http.ResponseWriter, rq *http.Request) {
|
|
|
|
genericHandlerOfFeeds(w, rq, history.RecentChangesRSS, "RSS")
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlerRecentChangesAtom(w http.ResponseWriter, rq *http.Request) {
|
|
|
|
genericHandlerOfFeeds(w, rq, history.RecentChangesAtom, "Atom")
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlerRecentChangesJSON(w http.ResponseWriter, rq *http.Request) {
|
|
|
|
genericHandlerOfFeeds(w, rq, history.RecentChangesJSON, "JSON feed")
|
|
|
|
}
|