bloat/service/transport.go

861 lines
23 KiB
Go
Raw Normal View History

2019-12-13 18:08:26 +00:00
package service
import (
2020-01-08 18:16:06 +00:00
"encoding/json"
"io"
2019-12-14 20:19:02 +00:00
"mime/multipart"
2019-12-13 18:08:26 +00:00
"net/http"
2019-12-26 19:18:09 +00:00
"strconv"
2019-12-29 05:59:31 +00:00
"time"
2020-01-01 15:58:27 +00:00
"bloat/model"
2019-12-13 18:08:26 +00:00
"github.com/gorilla/mux"
)
2020-04-19 08:18:36 +00:00
const (
sessionExp = 365 * 24 * time.Hour
)
2020-05-24 04:38:34 +00:00
func newClient(w io.Writer, req *http.Request, csrfToken string) *model.Client {
var sessionID string
if req != nil {
c, err := req.Cookie("session_id")
if err == nil {
sessionID = c.Value
}
}
2020-01-28 17:51:00 +00:00
return &model.Client{
Writer: w,
2020-05-24 04:38:34 +00:00
Ctx: model.ClientCtx{
SessionID: sessionID,
CSRFToken: csrfToken,
},
2020-01-28 17:51:00 +00:00
}
}
2020-04-19 08:18:36 +00:00
func setSessionCookie(w http.ResponseWriter, sessionID string, exp time.Duration) {
http.SetCookie(w, &http.Cookie{
Name: "session_id",
Value: sessionID,
Expires: time.Now().Add(exp),
})
}
2020-01-28 17:51:00 +00:00
func getMultipartFormValue(mf *multipart.Form, key string) (val string) {
vals, ok := mf.Value[key]
if !ok {
return ""
}
if len(vals) < 1 {
return ""
}
return vals[0]
}
func serveJson(w io.Writer, data interface{}) (err error) {
var d = make(map[string]interface{})
d["data"] = data
return json.NewEncoder(w).Encode(d)
}
2019-12-13 18:08:26 +00:00
func serveJsonError(w http.ResponseWriter, err error) {
var d = make(map[string]interface{})
d["error"] = err.Error()
2020-11-09 12:10:29 +00:00
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(d)
return
}
2019-12-13 18:08:26 +00:00
func NewHandler(s Service, staticDir string) http.Handler {
r := mux.NewRouter()
2020-01-28 17:51:00 +00:00
rootPage := func(w http.ResponseWriter, req *http.Request) {
sessionID, _ := req.Cookie("session_id")
2019-12-13 20:23:15 +00:00
if sessionID != nil && len(sessionID.Value) > 0 {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, "")
err := s.ServeRootPage(c)
2020-02-18 22:15:37 +00:00
if err != nil {
if err == errInvalidAccessToken {
w.Header().Add("Location", "/signin")
w.WriteHeader(http.StatusFound)
return
}
2020-02-18 22:15:37 +00:00
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2020-02-18 22:15:37 +00:00
return
}
} else {
w.Header().Add("Location", "/signin")
w.WriteHeader(http.StatusFound)
2019-12-13 18:08:26 +00:00
}
2020-02-18 22:15:37 +00:00
}
2019-12-13 20:23:15 +00:00
2020-02-18 22:15:37 +00:00
navPage := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, "")
err := s.ServeNavPage(c)
2020-02-18 22:15:37 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2020-02-18 22:15:37 +00:00
return
}
2020-01-28 17:51:00 +00:00
}
2019-12-13 18:08:26 +00:00
2020-01-28 17:51:00 +00:00
signinPage := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, nil, "")
instance, ok := s.SingleInstance()
2020-04-19 08:18:36 +00:00
if ok {
2020-05-24 04:38:34 +00:00
url, sessionID, err := s.NewSession(instance)
2020-04-19 08:18:36 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2020-04-19 08:18:36 +00:00
return
}
setSessionCookie(w, sessionID, sessionExp)
w.Header().Add("Location", url)
w.WriteHeader(http.StatusFound)
} else {
2020-05-24 04:38:34 +00:00
err := s.ServeSigninPage(c)
2020-04-19 08:18:36 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2020-04-19 08:18:36 +00:00
return
}
2019-12-13 18:08:26 +00:00
}
2020-01-28 17:51:00 +00:00
}
2019-12-13 18:08:26 +00:00
2020-01-28 17:51:00 +00:00
timelinePage := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, "")
2020-01-28 17:51:00 +00:00
tType, _ := mux.Vars(req)["type"]
maxID := req.URL.Query().Get("max_id")
minID := req.URL.Query().Get("min_id")
2019-12-13 18:08:26 +00:00
2020-05-24 04:38:34 +00:00
err := s.ServeTimelinePage(c, tType, maxID, minID)
2019-12-13 18:08:26 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2019-12-13 18:08:26 +00:00
return
}
2020-01-28 17:51:00 +00:00
}
2019-12-13 18:08:26 +00:00
2020-05-24 04:38:34 +00:00
defaultTimelinePage := func(w http.ResponseWriter, req *http.Request) {
2019-12-25 04:30:21 +00:00
w.Header().Add("Location", "/timeline/home")
w.WriteHeader(http.StatusFound)
2020-01-28 17:51:00 +00:00
}
2019-12-13 18:08:26 +00:00
2020-01-28 17:51:00 +00:00
threadPage := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, "")
2019-12-13 18:08:26 +00:00
id, _ := mux.Vars(req)["id"]
reply := req.URL.Query().Get("reply")
2020-01-28 17:51:00 +00:00
2020-05-24 04:38:34 +00:00
err := s.ServeThreadPage(c, id, len(reply) > 1)
2019-12-13 18:08:26 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2019-12-13 18:08:26 +00:00
return
}
2020-01-28 17:51:00 +00:00
}
2019-12-13 18:08:26 +00:00
2020-01-28 17:51:00 +00:00
likedByPage := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, "")
2019-12-26 09:11:24 +00:00
id, _ := mux.Vars(req)["id"]
2020-05-24 04:38:34 +00:00
err := s.ServeLikedByPage(c, id)
2019-12-26 09:11:24 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2019-12-26 09:11:24 +00:00
return
}
2020-01-28 17:51:00 +00:00
}
2019-12-26 09:11:24 +00:00
2020-01-28 17:51:00 +00:00
retweetedByPage := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, "")
2019-12-26 09:11:24 +00:00
id, _ := mux.Vars(req)["id"]
2020-05-24 04:38:34 +00:00
err := s.ServeRetweetedByPage(c, id)
2019-12-26 09:11:24 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2019-12-26 09:11:24 +00:00
return
}
2020-01-28 17:51:00 +00:00
}
2019-12-29 03:43:57 +00:00
2020-01-28 17:51:00 +00:00
notificationsPage := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, "")
2020-01-28 17:51:00 +00:00
maxID := req.URL.Query().Get("max_id")
minID := req.URL.Query().Get("min_id")
2020-05-24 04:38:34 +00:00
err := s.ServeNotificationPage(c, maxID, minID)
2019-12-13 18:08:26 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2019-12-13 18:08:26 +00:00
return
}
2020-01-28 17:51:00 +00:00
}
2019-12-13 18:08:26 +00:00
2020-01-28 17:51:00 +00:00
userPage := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, "")
2019-12-13 18:08:26 +00:00
id, _ := mux.Vars(req)["id"]
pageType, _ := mux.Vars(req)["type"]
2020-01-28 17:51:00 +00:00
maxID := req.URL.Query().Get("max_id")
minID := req.URL.Query().Get("min_id")
2020-05-24 04:38:34 +00:00
err := s.ServeUserPage(c, id, pageType, maxID, minID)
2019-12-13 18:08:26 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2019-12-13 18:08:26 +00:00
return
}
2020-01-28 17:51:00 +00:00
}
2019-12-13 18:08:26 +00:00
2020-01-30 15:32:37 +00:00
userSearchPage := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, "")
2020-01-30 15:32:37 +00:00
id, _ := mux.Vars(req)["id"]
q := req.URL.Query().Get("q")
offsetStr := req.URL.Query().Get("offset")
var offset int
var err error
if len(offsetStr) > 1 {
offset, err = strconv.Atoi(offsetStr)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2020-01-30 15:32:37 +00:00
return
}
}
2020-05-24 04:38:34 +00:00
err = s.ServeUserSearchPage(c, id, q, offset)
2020-01-30 15:32:37 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2020-01-30 15:32:37 +00:00
return
}
}
2020-01-28 17:51:00 +00:00
aboutPage := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, "")
2020-05-24 04:38:34 +00:00
err := s.ServeAboutPage(c)
2019-12-13 18:08:26 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2019-12-13 18:08:26 +00:00
return
}
2020-01-28 17:51:00 +00:00
}
2019-12-13 18:08:26 +00:00
2020-01-28 17:51:00 +00:00
emojisPage := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, "")
2020-01-25 10:07:06 +00:00
2020-05-24 04:38:34 +00:00
err := s.ServeEmojiPage(c)
2019-12-13 18:08:26 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2019-12-13 18:08:26 +00:00
return
}
2020-01-28 17:51:00 +00:00
}
2019-12-13 18:08:26 +00:00
2020-01-28 17:51:00 +00:00
searchPage := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, "")
2020-01-28 17:51:00 +00:00
q := req.URL.Query().Get("q")
qType := req.URL.Query().Get("type")
offsetStr := req.URL.Query().Get("offset")
2020-01-25 10:07:06 +00:00
2020-01-28 17:51:00 +00:00
var offset int
var err error
if len(offsetStr) > 1 {
offset, err = strconv.Atoi(offsetStr)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2020-01-28 17:51:00 +00:00
return
}
2020-01-08 18:16:06 +00:00
}
2020-05-24 04:38:34 +00:00
err = s.ServeSearchPage(c, q, qType, offset)
2020-01-08 18:16:06 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2020-01-08 18:16:06 +00:00
return
}
2020-01-28 17:51:00 +00:00
}
2020-01-25 10:07:06 +00:00
2020-01-28 17:51:00 +00:00
settingsPage := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, "")
2020-01-08 18:16:06 +00:00
2020-05-24 04:38:34 +00:00
err := s.ServeSettingsPage(c)
2020-01-08 18:16:06 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2020-01-08 18:16:06 +00:00
return
}
2020-01-28 17:51:00 +00:00
}
2020-01-08 18:16:06 +00:00
2020-01-28 17:51:00 +00:00
signin := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, nil, "")
2020-01-28 17:51:00 +00:00
instance := req.FormValue("instance")
2020-01-25 10:07:06 +00:00
2020-05-24 04:38:34 +00:00
url, sessionID, err := s.NewSession(instance)
2020-01-08 18:16:06 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2020-01-08 18:16:06 +00:00
return
}
2020-04-19 08:18:36 +00:00
setSessionCookie(w, sessionID, sessionExp)
2020-01-28 17:51:00 +00:00
w.Header().Add("Location", url)
w.WriteHeader(http.StatusFound)
}
2020-01-25 10:07:06 +00:00
2020-01-28 17:51:00 +00:00
oauthCallback := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, "")
2020-01-28 17:51:00 +00:00
token := req.URL.Query().Get("code")
2020-01-08 18:16:06 +00:00
2020-05-24 04:38:34 +00:00
_, _, err := s.Signin(c, "", token)
2020-01-08 18:16:06 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2020-01-08 18:16:06 +00:00
return
}
2020-02-18 22:15:37 +00:00
w.Header().Add("Location", "/")
2020-01-28 17:51:00 +00:00
w.WriteHeader(http.StatusFound)
}
post := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, "")
2019-12-14 20:19:02 +00:00
err := req.ParseMultipartForm(4 << 20)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2019-12-14 20:19:02 +00:00
return
}
2020-05-24 04:38:34 +00:00
c = newClient(w, req,
2020-01-25 10:07:06 +00:00
getMultipartFormValue(req.MultipartForm, "csrf_token"))
2019-12-14 20:19:02 +00:00
content := getMultipartFormValue(req.MultipartForm, "content")
replyToID := getMultipartFormValue(req.MultipartForm, "reply_to_id")
2019-12-26 11:25:29 +00:00
format := getMultipartFormValue(req.MultipartForm, "format")
visibility := getMultipartFormValue(req.MultipartForm, "visibility")
2019-12-22 16:27:49 +00:00
isNSFW := "on" == getMultipartFormValue(req.MultipartForm, "is_nsfw")
2019-12-14 20:19:02 +00:00
files := req.MultipartForm.File["attachments"]
2020-05-24 04:38:34 +00:00
id, err := s.Post(c, content, replyToID, format, visibility, isNSFW, files)
2019-12-13 18:08:26 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2019-12-13 18:08:26 +00:00
return
}
2020-02-18 22:15:37 +00:00
location := req.Header.Get("Referer")
2019-12-13 20:23:15 +00:00
if len(replyToID) > 0 {
location = "/thread/" + replyToID + "#status-" + id
2019-12-13 20:23:15 +00:00
}
w.Header().Add("Location", location)
w.WriteHeader(http.StatusFound)
2020-01-28 17:51:00 +00:00
}
2020-01-28 17:51:00 +00:00
like := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, req.FormValue("csrf_token"))
id, _ := mux.Vars(req)["id"]
2020-01-28 17:51:00 +00:00
retweetedByID := req.FormValue("retweeted_by_id")
2020-05-24 04:38:34 +00:00
_, err := s.Like(c, id)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
return
}
2020-01-28 17:51:00 +00:00
rID := id
if len(retweetedByID) > 0 {
rID = retweetedByID
}
w.Header().Add("Location", req.Header.Get("Referer")+"#status-"+rID)
w.WriteHeader(http.StatusFound)
}
2020-01-28 17:51:00 +00:00
unlike := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, req.FormValue("csrf_token"))
id, _ := mux.Vars(req)["id"]
2020-01-28 17:51:00 +00:00
retweetedByID := req.FormValue("retweeted_by_id")
2020-05-24 04:38:34 +00:00
_, err := s.UnLike(c, id)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
return
}
2020-01-28 17:51:00 +00:00
rID := id
if len(retweetedByID) > 0 {
rID = retweetedByID
}
w.Header().Add("Location", req.Header.Get("Referer")+"#status-"+rID)
w.WriteHeader(http.StatusFound)
2020-01-28 17:51:00 +00:00
}
2020-01-28 17:51:00 +00:00
retweet := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, req.FormValue("csrf_token"))
id, _ := mux.Vars(req)["id"]
2020-01-28 17:51:00 +00:00
retweetedByID := req.FormValue("retweeted_by_id")
2020-05-24 04:38:34 +00:00
_, err := s.Retweet(c, id)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
return
}
2020-01-28 17:51:00 +00:00
rID := id
if len(retweetedByID) > 0 {
rID = retweetedByID
}
w.Header().Add("Location", req.Header.Get("Referer")+"#status-"+rID)
w.WriteHeader(http.StatusFound)
2020-01-28 17:51:00 +00:00
}
2020-01-28 17:51:00 +00:00
unretweet := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, req.FormValue("csrf_token"))
2020-01-28 17:51:00 +00:00
id, _ := mux.Vars(req)["id"]
retweetedByID := req.FormValue("retweeted_by_id")
2019-12-21 05:48:48 +00:00
2020-05-24 04:38:34 +00:00
_, err := s.UnRetweet(c, id)
2019-12-21 05:48:48 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2019-12-21 05:48:48 +00:00
return
}
2020-01-28 17:51:00 +00:00
rID := id
if len(retweetedByID) > 0 {
rID = retweetedByID
2019-12-22 18:10:42 +00:00
}
2019-12-26 19:18:09 +00:00
2020-01-28 17:51:00 +00:00
w.Header().Add("Location", req.Header.Get("Referer")+"#status-"+rID)
w.WriteHeader(http.StatusFound)
}
2019-12-26 19:18:09 +00:00
vote := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, req.FormValue("csrf_token"))
id, _ := mux.Vars(req)["id"]
statusID := req.FormValue("status_id")
choices, _ := req.PostForm["choices"]
2020-05-24 04:38:34 +00:00
err := s.Vote(c, id, choices)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
return
}
w.Header().Add("Location", req.Header.Get("Referer")+"#status-"+statusID)
w.WriteHeader(http.StatusFound)
}
2020-01-28 17:51:00 +00:00
follow := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, req.FormValue("csrf_token"))
2020-01-28 17:51:00 +00:00
id, _ := mux.Vars(req)["id"]
2019-12-26 19:18:09 +00:00
2020-04-19 05:57:40 +00:00
var reblogs *bool
r, ok := req.URL.Query()["reblogs"]
if ok && len(r) > 0 {
reblogs = new(bool)
*reblogs = r[0] == "true"
}
2020-05-24 04:38:34 +00:00
err := s.Follow(c, id, reblogs)
2019-12-26 19:18:09 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2019-12-26 19:18:09 +00:00
return
}
2020-01-28 17:51:00 +00:00
w.Header().Add("Location", req.Header.Get("Referer"))
w.WriteHeader(http.StatusFound)
}
unfollow := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, req.FormValue("csrf_token"))
2020-01-28 17:51:00 +00:00
id, _ := mux.Vars(req)["id"]
2019-12-27 08:06:43 +00:00
2020-05-24 04:38:34 +00:00
err := s.UnFollow(c, id)
2019-12-27 08:06:43 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2019-12-27 08:06:43 +00:00
return
}
2020-01-28 17:51:00 +00:00
w.Header().Add("Location", req.Header.Get("Referer"))
w.WriteHeader(http.StatusFound)
}
2019-12-27 08:06:43 +00:00
2020-02-08 10:49:06 +00:00
mute := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, req.FormValue("csrf_token"))
2020-02-08 10:49:06 +00:00
id, _ := mux.Vars(req)["id"]
2020-05-24 04:38:34 +00:00
err := s.Mute(c, id)
2020-02-08 10:49:06 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2020-02-08 10:49:06 +00:00
return
}
w.Header().Add("Location", req.Header.Get("Referer"))
w.WriteHeader(http.StatusFound)
}
unMute := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, req.FormValue("csrf_token"))
2020-02-08 10:49:06 +00:00
id, _ := mux.Vars(req)["id"]
2020-05-24 04:38:34 +00:00
err := s.UnMute(c, id)
2020-02-08 10:49:06 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2020-02-08 10:49:06 +00:00
return
}
w.Header().Add("Location", req.Header.Get("Referer"))
w.WriteHeader(http.StatusFound)
}
block := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, req.FormValue("csrf_token"))
2020-02-08 10:49:06 +00:00
id, _ := mux.Vars(req)["id"]
2020-05-24 04:38:34 +00:00
err := s.Block(c, id)
2020-02-08 10:49:06 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2020-02-08 10:49:06 +00:00
return
}
w.Header().Add("Location", req.Header.Get("Referer"))
w.WriteHeader(http.StatusFound)
}
unBlock := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, req.FormValue("csrf_token"))
2020-02-08 10:49:06 +00:00
id, _ := mux.Vars(req)["id"]
2020-05-24 04:38:34 +00:00
err := s.UnBlock(c, id)
2020-02-08 10:49:06 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2020-02-08 10:49:06 +00:00
return
}
w.Header().Add("Location", req.Header.Get("Referer"))
w.WriteHeader(http.StatusFound)
}
2020-04-17 17:19:11 +00:00
subscribe := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, req.FormValue("csrf_token"))
2020-04-17 17:19:11 +00:00
id, _ := mux.Vars(req)["id"]
2020-05-24 04:38:34 +00:00
err := s.Subscribe(c, id)
2020-04-17 17:19:11 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2020-04-17 17:19:11 +00:00
return
}
w.Header().Add("Location", req.Header.Get("Referer"))
w.WriteHeader(http.StatusFound)
}
unSubscribe := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, req.FormValue("csrf_token"))
2020-04-17 17:19:11 +00:00
id, _ := mux.Vars(req)["id"]
2020-05-24 04:38:34 +00:00
err := s.UnSubscribe(c, id)
2020-04-17 17:19:11 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2020-04-17 17:19:11 +00:00
return
}
w.Header().Add("Location", req.Header.Get("Referer"))
w.WriteHeader(http.StatusFound)
}
2020-01-28 17:51:00 +00:00
settings := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, req.FormValue("csrf_token"))
2019-12-27 08:06:43 +00:00
visibility := req.FormValue("visibility")
2020-10-19 06:51:23 +00:00
format := req.FormValue("format")
2019-12-27 08:06:43 +00:00
copyScope := req.FormValue("copy_scope") == "true"
2019-12-29 11:32:24 +00:00
threadInNewTab := req.FormValue("thread_in_new_tab") == "true"
2020-04-25 09:35:18 +00:00
hideAttachments := req.FormValue("hide_attachments") == "true"
2019-12-31 11:00:21 +00:00
maskNSFW := req.FormValue("mask_nsfw") == "true"
2020-02-18 22:15:37 +00:00
arn := req.FormValue("auto_refresh_notifications") == "true"
2020-01-08 18:16:06 +00:00
fluorideMode := req.FormValue("fluoride_mode") == "true"
2020-01-12 17:16:57 +00:00
darkMode := req.FormValue("dark_mode") == "true"
2020-09-02 17:50:48 +00:00
antiDopamineMode := req.FormValue("anti_dopamine_mode") == "true"
2020-01-28 17:51:00 +00:00
2019-12-27 08:06:43 +00:00
settings := &model.Settings{
2020-02-18 22:15:37 +00:00
DefaultVisibility: visibility,
2020-10-19 06:51:23 +00:00
DefaultFormat: format,
2020-02-18 22:15:37 +00:00
CopyScope: copyScope,
ThreadInNewTab: threadInNewTab,
2020-04-25 09:35:18 +00:00
HideAttachments: hideAttachments,
2020-02-18 22:15:37 +00:00
MaskNSFW: maskNSFW,
AutoRefreshNotifications: arn,
FluorideMode: fluorideMode,
DarkMode: darkMode,
2020-09-02 17:50:48 +00:00
AntiDopamineMode: antiDopamineMode,
2019-12-27 08:06:43 +00:00
}
2020-05-24 04:38:34 +00:00
err := s.SaveSettings(c, settings)
2019-12-27 08:06:43 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2019-12-27 08:06:43 +00:00
return
}
2020-02-18 22:15:37 +00:00
w.Header().Add("Location", "/")
2019-12-27 08:06:43 +00:00
w.WriteHeader(http.StatusFound)
2020-01-28 17:51:00 +00:00
}
2019-12-27 08:06:43 +00:00
2020-02-02 07:24:06 +00:00
muteConversation := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, req.FormValue("csrf_token"))
2020-02-02 07:24:06 +00:00
id, _ := mux.Vars(req)["id"]
2020-05-24 04:38:34 +00:00
err := s.MuteConversation(c, id)
2020-02-02 07:24:06 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2020-02-02 07:24:06 +00:00
return
}
w.Header().Add("Location", req.Header.Get("Referer"))
w.WriteHeader(http.StatusFound)
}
unMuteConversation := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, req.FormValue("csrf_token"))
2020-02-02 07:24:06 +00:00
id, _ := mux.Vars(req)["id"]
2020-05-24 04:38:34 +00:00
err := s.UnMuteConversation(c, id)
2020-02-02 07:24:06 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2020-02-02 07:24:06 +00:00
return
}
w.Header().Add("Location", req.Header.Get("Referer"))
w.WriteHeader(http.StatusFound)
}
2020-02-02 08:30:40 +00:00
delete := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, req.FormValue("csrf_token"))
2020-02-02 08:30:40 +00:00
id, _ := mux.Vars(req)["id"]
2020-05-24 04:38:34 +00:00
err := s.Delete(c, id)
2020-02-02 08:30:40 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2020-02-02 08:30:40 +00:00
return
}
w.Header().Add("Location", req.Header.Get("Referer"))
w.WriteHeader(http.StatusFound)
}
2020-02-18 22:15:37 +00:00
readNotifications := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, req.FormValue("csrf_token"))
2020-02-18 22:15:37 +00:00
maxID := req.URL.Query().Get("max_id")
2020-05-24 04:38:34 +00:00
err := s.ReadNotifications(c, maxID)
2020-02-18 22:15:37 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-05-24 04:38:34 +00:00
s.ServeErrorPage(c, err)
2020-02-18 22:15:37 +00:00
return
}
w.Header().Add("Location", req.Header.Get("Referer"))
w.WriteHeader(http.StatusFound)
}
bookmark := func(w http.ResponseWriter, req *http.Request) {
c := newClient(w, req, req.FormValue("csrf_token"))
id, _ := mux.Vars(req)["id"]
retweetedByID := req.FormValue("retweeted_by_id")
err := s.Bookmark(c, id)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(c, err)
return
}
rID := id
if len(retweetedByID) > 0 {
rID = retweetedByID
}
w.Header().Add("Location", req.Header.Get("Referer")+"#status-"+rID)
w.WriteHeader(http.StatusFound)
}
unBookmark := func(w http.ResponseWriter, req *http.Request) {
c := newClient(w, req, req.FormValue("csrf_token"))
id, _ := mux.Vars(req)["id"]
retweetedByID := req.FormValue("retweeted_by_id")
err := s.UnBookmark(c, id)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(c, err)
return
}
rID := id
if len(retweetedByID) > 0 {
rID = retweetedByID
}
w.Header().Add("Location", req.Header.Get("Referer")+"#status-"+rID)
w.WriteHeader(http.StatusFound)
}
2020-01-28 17:51:00 +00:00
signout := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, req.FormValue("csrf_token"))
2020-03-04 15:59:59 +00:00
2020-05-24 04:38:34 +00:00
s.Signout(c)
2020-03-04 15:59:59 +00:00
2020-04-19 08:18:36 +00:00
setSessionCookie(w, "", 0)
2019-12-13 20:23:15 +00:00
w.Header().Add("Location", "/")
w.WriteHeader(http.StatusFound)
2020-01-28 17:51:00 +00:00
}
2019-12-13 20:23:15 +00:00
2020-01-28 17:51:00 +00:00
fLike := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, req.FormValue("csrf_token"))
2020-01-28 17:51:00 +00:00
id, _ := mux.Vars(req)["id"]
2019-12-14 20:19:02 +00:00
2020-05-24 04:38:34 +00:00
count, err := s.Like(c, id)
2020-01-28 17:51:00 +00:00
if err != nil {
serveJsonError(w, err)
2020-01-28 17:51:00 +00:00
return
}
err = serveJson(w, count)
if err != nil {
serveJsonError(w, err)
2020-01-28 17:51:00 +00:00
return
}
}
2020-01-28 17:51:00 +00:00
fUnlike := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, req.FormValue("csrf_token"))
2020-01-28 17:51:00 +00:00
id, _ := mux.Vars(req)["id"]
2020-05-24 04:38:34 +00:00
count, err := s.UnLike(c, id)
2020-01-28 17:51:00 +00:00
if err != nil {
serveJsonError(w, err)
2020-01-28 17:51:00 +00:00
return
}
err = serveJson(w, count)
if err != nil {
serveJsonError(w, err)
2020-01-28 17:51:00 +00:00
return
}
2019-12-14 20:19:02 +00:00
}
2020-01-28 17:51:00 +00:00
fRetweet := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, req.FormValue("csrf_token"))
2020-01-28 17:51:00 +00:00
id, _ := mux.Vars(req)["id"]
2020-05-24 04:38:34 +00:00
count, err := s.Retweet(c, id)
2020-01-28 17:51:00 +00:00
if err != nil {
serveJsonError(w, err)
2020-01-28 17:51:00 +00:00
return
}
err = serveJson(w, count)
if err != nil {
serveJsonError(w, err)
2020-01-28 17:51:00 +00:00
return
}
2019-12-14 20:19:02 +00:00
}
2020-01-08 18:16:06 +00:00
2020-01-28 17:51:00 +00:00
fUnretweet := func(w http.ResponseWriter, req *http.Request) {
2020-05-24 04:38:34 +00:00
c := newClient(w, req, req.FormValue("csrf_token"))
2020-01-28 17:51:00 +00:00
id, _ := mux.Vars(req)["id"]
2020-05-24 04:38:34 +00:00
count, err := s.UnRetweet(c, id)
2020-01-28 17:51:00 +00:00
if err != nil {
serveJsonError(w, err)
2020-01-28 17:51:00 +00:00
return
}
err = serveJson(w, count)
if err != nil {
serveJsonError(w, err)
2020-01-28 17:51:00 +00:00
return
}
}
2020-11-09 12:10:29 +00:00
statusPopup := func(w http.ResponseWriter, req *http.Request) {
c := newClient(w, req, "")
id, _ := mux.Vars(req)["id"]
err := s.ServeStatusPopup(c, id)
if err != nil {
serveJsonError(w, err)
return
}
}
2020-01-28 17:51:00 +00:00
r.HandleFunc("/", rootPage).Methods(http.MethodGet)
2020-02-18 22:15:37 +00:00
r.HandleFunc("/nav", navPage).Methods(http.MethodGet)
2020-01-28 17:51:00 +00:00
r.HandleFunc("/signin", signinPage).Methods(http.MethodGet)
r.HandleFunc("/timeline/{type}", timelinePage).Methods(http.MethodGet)
2020-05-24 04:38:34 +00:00
r.HandleFunc("/timeline", defaultTimelinePage).Methods(http.MethodGet)
2020-01-28 17:51:00 +00:00
r.HandleFunc("/thread/{id}", threadPage).Methods(http.MethodGet)
r.HandleFunc("/likedby/{id}", likedByPage).Methods(http.MethodGet)
r.HandleFunc("/retweetedby/{id}", retweetedByPage).Methods(http.MethodGet)
r.HandleFunc("/notifications", notificationsPage).Methods(http.MethodGet)
r.HandleFunc("/user/{id}", userPage).Methods(http.MethodGet)
r.HandleFunc("/user/{id}/{type}", userPage).Methods(http.MethodGet)
2020-01-30 15:32:37 +00:00
r.HandleFunc("/usersearch/{id}", userSearchPage).Methods(http.MethodGet)
2020-01-28 17:51:00 +00:00
r.HandleFunc("/about", aboutPage).Methods(http.MethodGet)
r.HandleFunc("/emojis", emojisPage).Methods(http.MethodGet)
r.HandleFunc("/search", searchPage).Methods(http.MethodGet)
r.HandleFunc("/settings", settingsPage).Methods(http.MethodGet)
r.HandleFunc("/signin", signin).Methods(http.MethodPost)
r.HandleFunc("/oauth_callback", oauthCallback).Methods(http.MethodGet)
r.HandleFunc("/post", post).Methods(http.MethodPost)
r.HandleFunc("/like/{id}", like).Methods(http.MethodPost)
r.HandleFunc("/unlike/{id}", unlike).Methods(http.MethodPost)
r.HandleFunc("/retweet/{id}", retweet).Methods(http.MethodPost)
r.HandleFunc("/unretweet/{id}", unretweet).Methods(http.MethodPost)
r.HandleFunc("/vote/{id}", vote).Methods(http.MethodPost)
2020-01-28 17:51:00 +00:00
r.HandleFunc("/follow/{id}", follow).Methods(http.MethodPost)
r.HandleFunc("/unfollow/{id}", unfollow).Methods(http.MethodPost)
2020-02-08 10:49:06 +00:00
r.HandleFunc("/mute/{id}", mute).Methods(http.MethodPost)
r.HandleFunc("/unmute/{id}", unMute).Methods(http.MethodPost)
r.HandleFunc("/block/{id}", block).Methods(http.MethodPost)
r.HandleFunc("/unblock/{id}", unBlock).Methods(http.MethodPost)
2020-04-17 17:19:11 +00:00
r.HandleFunc("/subscribe/{id}", subscribe).Methods(http.MethodPost)
r.HandleFunc("/unsubscribe/{id}", unSubscribe).Methods(http.MethodPost)
2020-01-28 17:51:00 +00:00
r.HandleFunc("/settings", settings).Methods(http.MethodPost)
2020-02-02 07:24:06 +00:00
r.HandleFunc("/muteconv/{id}", muteConversation).Methods(http.MethodPost)
r.HandleFunc("/unmuteconv/{id}", unMuteConversation).Methods(http.MethodPost)
2020-02-02 08:30:40 +00:00
r.HandleFunc("/delete/{id}", delete).Methods(http.MethodPost)
2020-02-18 22:15:37 +00:00
r.HandleFunc("/notifications/read", readNotifications).Methods(http.MethodPost)
r.HandleFunc("/bookmark/{id}", bookmark).Methods(http.MethodPost)
r.HandleFunc("/unbookmark/{id}", unBookmark).Methods(http.MethodPost)
2020-03-04 15:59:59 +00:00
r.HandleFunc("/signout", signout).Methods(http.MethodPost)
2020-01-28 17:51:00 +00:00
r.HandleFunc("/fluoride/like/{id}", fLike).Methods(http.MethodPost)
r.HandleFunc("/fluoride/unlike/{id}", fUnlike).Methods(http.MethodPost)
r.HandleFunc("/fluoride/retweet/{id}", fRetweet).Methods(http.MethodPost)
r.HandleFunc("/fluoride/unretweet/{id}", fUnretweet).Methods(http.MethodPost)
2020-11-09 12:10:29 +00:00
r.HandleFunc("/fluoride/status/{id}", statusPopup).Methods(http.MethodGet)
2020-01-28 17:51:00 +00:00
r.PathPrefix("/static").Handler(http.StripPrefix("/static",
http.FileServer(http.Dir(staticDir))))
2020-01-28 17:51:00 +00:00
return r
2020-01-08 18:16:06 +00:00
}