1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 19:56:16 +00:00
mycorrhiza/web/history.go

96 lines
3.0 KiB
Go
Raw Normal View History

package web
import (
"fmt"
"log"
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/bouncepaw/mycorrhiza/history"
2021-09-06 17:46:34 +00:00
"github.com/bouncepaw/mycorrhiza/l18n"
2021-01-24 07:30:14 +00:00
"github.com/bouncepaw/mycorrhiza/user"
"github.com/bouncepaw/mycorrhiza/util"
2021-02-20 16:14:33 +00:00
"github.com/bouncepaw/mycorrhiza/views"
)
func initHistory(r *mux.Router) {
r.PathPrefix("/history/").HandlerFunc(handlerHistory)
2021-07-25 13:16:34 +00:00
r.HandleFunc("/recent-changes/{count:[0-9]+}", handlerRecentChanges)
r.HandleFunc("/recent-changes/", func(w http.ResponseWriter, rq *http.Request) {
http.Redirect(w, rq, "/recent-changes/20", http.StatusSeeOther)
})
r.HandleFunc("/recent-changes-rss", handlerRecentChangesRSS)
r.HandleFunc("/recent-changes-atom", handlerRecentChangesAtom)
r.HandleFunc("/recent-changes-json", handlerRecentChangesJSON)
}
// handlerHistory lists all revisions of a hypha.
func handlerHistory(w http.ResponseWriter, rq *http.Request) {
hyphaName := util.HyphaNameFromRq(rq, "history")
var list string
// History can be found for files that do not exist anymore.
revs, err := history.Revisions(hyphaName)
if err == nil {
list = history.WithRevisions(hyphaName, revs)
}
log.Println("Found", len(revs), "revisions for", hyphaName)
2021-09-06 17:46:34 +00:00
var lc = l18n.FromRequest(rq)
util.HTTP200Page(w, views.BaseHTML(
2021-09-06 17:46:34 +00:00
fmt.Sprintf(lc.Get("ui.history_title"), util.BeautifulName(hyphaName)),
views.HistoryHTML(rq, hyphaName, list, lc),
lc,
user.FromRequest(rq)))
}
// handlerRecentChanges displays the /recent-changes/ page.
func handlerRecentChanges(w http.ResponseWriter, rq *http.Request) {
2021-07-25 13:16:34 +00:00
// Error ignored: filtered by regex
n, _ := strconv.Atoi(mux.Vars(rq)["count"])
2021-12-30 21:10:00 +00:00
if n > 100 {
return
}
2021-09-06 17:46:34 +00:00
var lc = l18n.FromRequest(rq)
util.HTTP200Page(w, views.BaseHTML(
lc.GetPlural("ui.recent_title", n),
2021-09-29 14:56:17 +00:00
views.RecentChangesHTML(n, lc),
2021-09-06 17:46:34 +00:00
lc,
user.FromRequest(rq)))
}
// genericHandlerOfFeeds is a helper function for the web feed handlers.
2021-10-25 21:51:36 +00:00
func genericHandlerOfFeeds(w http.ResponseWriter, rq *http.Request, f func(history.FeedOptions) (string, error), name string, contentType string) {
opts, err := history.ParseFeedOptions(rq.URL.Query())
var content string
if err == nil {
content, err = f(opts)
}
if 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", fmt.Sprintf("%s;charset=utf-8", contentType))
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, content)
}
}
func handlerRecentChangesRSS(w http.ResponseWriter, rq *http.Request) {
genericHandlerOfFeeds(w, rq, history.RecentChangesRSS, "RSS", "application/rss+xml")
}
func handlerRecentChangesAtom(w http.ResponseWriter, rq *http.Request) {
genericHandlerOfFeeds(w, rq, history.RecentChangesAtom, "Atom", "application/atom+xml")
}
func handlerRecentChangesJSON(w http.ResponseWriter, rq *http.Request) {
genericHandlerOfFeeds(w, rq, history.RecentChangesJSON, "JSON feed", "application/json")
}