bloat/service/transport.go

848 lines
22 KiB
Go
Raw Normal View History

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