bloat/service/transport.go

780 lines
21 KiB
Go
Raw Normal View History

2019-12-13 18:08:26 +00:00
package service
import (
"context"
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-01-28 17:51:00 +00:00
func newClient(w io.Writer) *model.Client {
return &model.Client{
Writer: w,
}
}
func newCtxWithSesion(req *http.Request) context.Context {
ctx := context.Background()
sessionID, err := req.Cookie("session_id")
if err != nil {
return ctx
}
return context.WithValue(ctx, "session_id", sessionID.Value)
}
func newCtxWithSesionCSRF(req *http.Request, csrfToken string) context.Context {
ctx := newCtxWithSesion(req)
return context.WithValue(ctx, "csrf_token", csrfToken)
}
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()
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-02-18 22:15:37 +00:00
c := newClient(w)
ctx := newCtxWithSesion(req)
err := s.ServeRootPage(ctx, c)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err)
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) {
c := newClient(w)
ctx := newCtxWithSesion(req)
err := s.ServeNavPage(ctx, c)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err)
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) {
c := newClient(w)
ctx := context.Background()
err := s.ServeSigninPage(ctx, c)
2019-12-13 18:08:26 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-01-28 17:51:00 +00:00
s.ServeErrorPage(ctx, 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
timelinePage := func(w http.ResponseWriter, req *http.Request) {
c := newClient(w)
ctx := newCtxWithSesion(req)
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-01-28 17:51:00 +00:00
err := s.ServeTimelinePage(ctx, c, tType, maxID, minID)
2019-12-13 18:08:26 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-01-28 17:51:00 +00:00
s.ServeErrorPage(ctx, 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
timelineOldPage := 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) {
c := newClient(w)
ctx := newCtxWithSesion(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
err := s.ServeThreadPage(ctx, c, id, len(reply) > 1)
2019-12-13 18:08:26 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-01-28 17:51:00 +00:00
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
ctx := newCtxWithSesion(req)
2019-12-26 09:11:24 +00:00
id, _ := mux.Vars(req)["id"]
2020-01-28 17:51:00 +00:00
err := s.ServeLikedByPage(ctx, c, id)
2019-12-26 09:11:24 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-01-28 17:51:00 +00:00
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
ctx := newCtxWithSesion(req)
2019-12-26 09:11:24 +00:00
id, _ := mux.Vars(req)["id"]
2020-01-28 17:51:00 +00:00
err := s.ServeRetweetedByPage(ctx, c, id)
2019-12-26 09:11:24 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-01-28 17:51:00 +00:00
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
ctx := newCtxWithSesion(req)
maxID := req.URL.Query().Get("max_id")
minID := req.URL.Query().Get("min_id")
2020-01-28 17:51:00 +00:00
err := s.ServeNotificationPage(ctx, c, maxID, minID)
2019-12-13 18:08:26 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-01-28 17:51:00 +00:00
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
ctx := newCtxWithSesion(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")
err := s.ServeUserPage(ctx, c, id, pageType, maxID, minID)
2019-12-13 18:08:26 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-01-28 17:51:00 +00:00
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
ctx := newCtxWithSesion(req)
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-01-30 15:32:37 +00:00
s.ServeErrorPage(ctx, c, err)
return
}
}
err = s.ServeUserSearchPage(ctx, c, id, q, offset)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-01-30 15:32:37 +00:00
s.ServeErrorPage(ctx, c, err)
return
}
}
2020-01-28 17:51:00 +00:00
aboutPage := func(w http.ResponseWriter, req *http.Request) {
c := newClient(w)
ctx := newCtxWithSesion(req)
2020-01-28 17:51:00 +00:00
err := s.ServeAboutPage(ctx, c)
2019-12-13 18:08:26 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-01-28 17:51:00 +00:00
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
ctx := newCtxWithSesion(req)
2020-01-25 10:07:06 +00:00
2020-01-28 17:51:00 +00:00
err := s.ServeEmojiPage(ctx, c)
2019-12-13 18:08:26 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-01-28 17:51:00 +00:00
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
ctx := newCtxWithSesion(req)
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-01-28 17:51:00 +00:00
s.ServeErrorPage(ctx, c, err)
return
}
2020-01-08 18:16:06 +00:00
}
2020-01-28 17:51:00 +00:00
err = s.ServeSearchPage(ctx, c, q, qType, offset)
2020-01-08 18:16:06 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-01-28 17:51:00 +00:00
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
ctx := newCtxWithSesion(req)
2020-01-08 18:16:06 +00:00
2020-01-28 17:51:00 +00:00
err := s.ServeSettingsPage(ctx, c)
2020-01-08 18:16:06 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-01-28 17:51:00 +00:00
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
ctx := context.Background()
instance := req.FormValue("instance")
2020-01-25 10:07:06 +00:00
2020-01-28 17:51:00 +00:00
url, sessionID, err := s.NewSession(ctx, instance)
2020-01-08 18:16:06 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-01-28 17:51:00 +00:00
s.ServeErrorPage(ctx, c, err)
2020-01-08 18:16:06 +00:00
return
}
2020-01-28 17:51:00 +00:00
http.SetCookie(w, &http.Cookie{
Name: "session_id",
Value: sessionID,
Expires: time.Now().Add(365 * 24 * time.Hour),
})
2020-01-08 18:16:06 +00:00
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) {
c := newClient(w)
ctx := newCtxWithSesion(req)
token := req.URL.Query().Get("code")
2020-01-08 18:16:06 +00:00
2020-02-02 08:30:40 +00:00
_, _, err := s.Signin(ctx, c, "", token)
2020-01-08 18:16:06 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-01-28 17:51:00 +00:00
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
2019-12-14 20:19:02 +00:00
err := req.ParseMultipartForm(4 << 20)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-01-28 17:51:00 +00:00
s.ServeErrorPage(context.Background(), c, err)
2019-12-14 20:19:02 +00:00
return
}
2020-01-28 17:51:00 +00:00
ctx := newCtxWithSesionCSRF(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-01-28 17:51:00 +00:00
id, err := s.Post(ctx, c, content, replyToID, format, visibility, isNSFW, files)
2019-12-13 18:08:26 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-01-28 17:51:00 +00:00
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
ctx := newCtxWithSesionCSRF(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-01-28 17:51:00 +00:00
_, err := s.Like(ctx, c, id)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-01-28 17:51:00 +00:00
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
ctx := newCtxWithSesionCSRF(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-01-28 17:51:00 +00:00
_, err := s.UnLike(ctx, c, id)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-01-28 17:51:00 +00:00
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
ctx := newCtxWithSesionCSRF(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-01-28 17:51:00 +00:00
_, err := s.Retweet(ctx, c, id)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-01-28 17:51:00 +00:00
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token"))
id, _ := mux.Vars(req)["id"]
retweetedByID := req.FormValue("retweeted_by_id")
2019-12-21 05:48:48 +00:00
2020-01-28 17:51:00 +00:00
_, err := s.UnRetweet(ctx, c, id)
2019-12-21 05:48:48 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-01-28 17:51:00 +00:00
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token"))
id, _ := mux.Vars(req)["id"]
statusID := req.FormValue("status_id")
choices, _ := req.PostForm["choices"]
err := s.Vote(ctx, c, id, choices)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token"))
id, _ := mux.Vars(req)["id"]
2019-12-26 19:18:09 +00:00
2020-01-28 17:51:00 +00:00
err := s.Follow(ctx, c, id)
2019-12-26 19:18:09 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-01-28 17:51:00 +00:00
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token"))
id, _ := mux.Vars(req)["id"]
2019-12-27 08:06:43 +00:00
2020-01-28 17:51:00 +00:00
err := s.UnFollow(ctx, c, id)
2019-12-27 08:06:43 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-01-28 17:51:00 +00:00
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token"))
id, _ := mux.Vars(req)["id"]
err := s.Mute(ctx, c, id)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err)
return
}
w.Header().Add("Location", req.Header.Get("Referer"))
w.WriteHeader(http.StatusFound)
}
unMute := func(w http.ResponseWriter, req *http.Request) {
c := newClient(w)
ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token"))
id, _ := mux.Vars(req)["id"]
err := s.UnMute(ctx, c, id)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err)
return
}
w.Header().Add("Location", req.Header.Get("Referer"))
w.WriteHeader(http.StatusFound)
}
block := func(w http.ResponseWriter, req *http.Request) {
c := newClient(w)
ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token"))
id, _ := mux.Vars(req)["id"]
err := s.Block(ctx, c, id)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err)
return
}
w.Header().Add("Location", req.Header.Get("Referer"))
w.WriteHeader(http.StatusFound)
}
unBlock := func(w http.ResponseWriter, req *http.Request) {
c := newClient(w)
ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token"))
id, _ := mux.Vars(req)["id"]
err := s.UnBlock(ctx, c, id)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err)
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) {
c := newClient(w)
ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token"))
2019-12-27 08:06:43 +00:00
visibility := req.FormValue("visibility")
copyScope := req.FormValue("copy_scope") == "true"
2019-12-29 11:32:24 +00:00
threadInNewTab := req.FormValue("thread_in_new_tab") == "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-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,
CopyScope: copyScope,
ThreadInNewTab: threadInNewTab,
MaskNSFW: maskNSFW,
AutoRefreshNotifications: arn,
FluorideMode: fluorideMode,
DarkMode: darkMode,
2019-12-27 08:06:43 +00:00
}
2020-01-28 17:51:00 +00:00
err := s.SaveSettings(ctx, c, settings)
2019-12-27 08:06:43 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2020-01-28 17:51:00 +00:00
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token"))
id, _ := mux.Vars(req)["id"]
err := s.MuteConversation(ctx, c, id)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err)
return
}
w.Header().Add("Location", req.Header.Get("Referer"))
w.WriteHeader(http.StatusFound)
}
unMuteConversation := func(w http.ResponseWriter, req *http.Request) {
c := newClient(w)
ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token"))
id, _ := mux.Vars(req)["id"]
err := s.UnMuteConversation(ctx, c, id)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err)
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) {
c := newClient(w)
ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token"))
id, _ := mux.Vars(req)["id"]
err := s.Delete(ctx, c, id)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err)
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) {
c := newClient(w)
ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token"))
maxID := req.URL.Query().Get("max_id")
err := s.ReadNotifications(ctx, c, maxID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err)
return
}
w.Header().Add("Location", req.Header.Get("Referer"))
w.WriteHeader(http.StatusFound)
}
2020-01-28 17:51:00 +00:00
signout := func(w http.ResponseWriter, req *http.Request) {
2020-03-04 15:59:59 +00:00
c := newClient(w)
ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token"))
s.Signout(ctx, c)
2019-12-29 05:59:31 +00:00
http.SetCookie(w, &http.Cookie{
2019-12-29 11:32:24 +00:00
Name: "session_id",
Value: "",
Expires: time.Now(),
2019-12-29 05:59:31 +00:00
})
2020-03-04 15:59:59 +00:00
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) {
c := newClient(w)
ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token"))
id, _ := mux.Vars(req)["id"]
2019-12-14 20:19:02 +00:00
2020-01-28 17:51:00 +00:00
count, err := s.Like(ctx, c, id)
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) {
c := newClient(w)
ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token"))
id, _ := mux.Vars(req)["id"]
count, err := s.UnLike(ctx, c, id)
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) {
c := newClient(w)
ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token"))
id, _ := mux.Vars(req)["id"]
count, err := s.Retweet(ctx, c, id)
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) {
c := newClient(w)
ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token"))
id, _ := mux.Vars(req)["id"]
count, err := s.UnRetweet(ctx, c, id)
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
}
}
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)
2020-02-18 22:15:37 +00:00
r.HandleFunc("//{type}", timelinePage).Methods(http.MethodGet)
2020-01-28 17:51:00 +00:00
r.HandleFunc("/timeline/{type}", timelinePage).Methods(http.MethodGet)
r.HandleFunc("/timeline", timelineOldPage).Methods(http.MethodGet)
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-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)
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)
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
}