bloat/service/transport.go

572 lines
15 KiB
Go
Raw Normal View History

2019-12-13 20:08:26 +02:00
package service
import (
"context"
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"
"path"
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-01-28 19:51:00 +02: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 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 20:08:26 +02:00
2019-12-13 22:23:15 +02:00
location := "/signin"
if sessionID != nil && len(sessionID.Value) > 0 {
2019-12-25 06:30:21 +02:00
location = "/timeline/home"
2019-12-13 20:08:26 +02:00
}
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
}
2019-12-13 20:08:26 +02:00
2020-01-28 19:51:00 +02:00
signinPage := func(w http.ResponseWriter, req *http.Request) {
c := newClient(w)
ctx := context.Background()
err := s.ServeSigninPage(ctx, c)
2019-12-13 20:08:26 +02:00
if err != nil {
2020-01-28 19:51:00 +02:00
s.ServeErrorPage(ctx, 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
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 20:08:26 +02:00
2020-01-28 19:51:00 +02:00
err := s.ServeTimelinePage(ctx, c, tType, maxID, minID)
2019-12-13 20:08:26 +02:00
if err != nil {
2020-01-28 19:51:00 +02:00
s.ServeErrorPage(ctx, 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
timelineOldPage := 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) {
c := newClient(w)
ctx := newCtxWithSesion(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
err := s.ServeThreadPage(ctx, c, id, len(reply) > 1)
2019-12-13 20:08:26 +02:00
if err != nil {
2020-01-28 19:51:00 +02:00
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
ctx := newCtxWithSesion(req)
2019-12-26 11:11:24 +02:00
id, _ := mux.Vars(req)["id"]
2020-01-28 19:51:00 +02:00
err := s.ServeLikedByPage(ctx, c, id)
2019-12-26 11:11:24 +02:00
if err != nil {
2020-01-28 19:51:00 +02:00
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
ctx := newCtxWithSesion(req)
2019-12-26 11:11:24 +02:00
id, _ := mux.Vars(req)["id"]
2020-01-28 19:51:00 +02:00
err := s.ServeRetweetedByPage(ctx, c, id)
2019-12-26 11:11:24 +02:00
if err != nil {
2020-01-28 19:51:00 +02:00
s.ServeErrorPage(ctx, 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
followingPage := func(w http.ResponseWriter, req *http.Request) {
c := newClient(w)
ctx := newCtxWithSesion(req)
2019-12-29 05:43:57 +02:00
id, _ := mux.Vars(req)["id"]
maxID := req.URL.Query().Get("max_id")
minID := req.URL.Query().Get("min_id")
2020-01-28 19:51:00 +02:00
err := s.ServeFollowingPage(ctx, c, id, maxID, minID)
2019-12-29 05:43:57 +02:00
if err != nil {
2020-01-28 19:51:00 +02:00
s.ServeErrorPage(ctx, c, err)
2019-12-29 05:43:57 +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
followersPage := func(w http.ResponseWriter, req *http.Request) {
c := newClient(w)
ctx := newCtxWithSesion(req)
2019-12-29 05:43:57 +02:00
id, _ := mux.Vars(req)["id"]
maxID := req.URL.Query().Get("max_id")
minID := req.URL.Query().Get("min_id")
2020-01-28 19:51:00 +02:00
err := s.ServeFollowersPage(ctx, c, id, maxID, minID)
2019-12-29 05:43:57 +02:00
if err != nil {
2020-01-28 19:51:00 +02:00
s.ServeErrorPage(ctx, c, err)
2019-12-29 05:43:57 +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
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 19:51:00 +02:00
err := s.ServeNotificationPage(ctx, c, maxID, minID)
2019-12-13 20:08:26 +02:00
if err != nil {
2020-01-28 19:51:00 +02:00
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
ctx := newCtxWithSesion(req)
2019-12-13 20:08:26 +02:00
id, _ := mux.Vars(req)["id"]
2020-01-28 19:51:00 +02:00
maxID := req.URL.Query().Get("max_id")
minID := req.URL.Query().Get("min_id")
2020-01-28 19:51:00 +02:00
err := s.ServeUserPage(ctx, c, id, maxID, minID)
2019-12-13 20:08:26 +02:00
if err != nil {
2020-01-28 19:51:00 +02:00
s.ServeErrorPage(ctx, 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
aboutPage := func(w http.ResponseWriter, req *http.Request) {
c := newClient(w)
ctx := newCtxWithSesion(req)
2020-01-28 19:51:00 +02:00
err := s.ServeAboutPage(ctx, c)
2019-12-13 20:08:26 +02:00
if err != nil {
2020-01-28 19:51:00 +02:00
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
ctx := newCtxWithSesion(req)
2020-01-25 12:07:06 +02:00
2020-01-28 19:51:00 +02:00
err := s.ServeEmojiPage(ctx, c)
2019-12-13 20:08:26 +02:00
if err != nil {
2020-01-28 19:51:00 +02:00
s.ServeErrorPage(ctx, 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) {
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 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 {
s.ServeErrorPage(ctx, c, err)
return
}
2020-01-08 20:16:06 +02:00
}
2020-01-28 19:51:00 +02:00
err = s.ServeSearchPage(ctx, c, q, qType, offset)
2020-01-08 20:16:06 +02:00
if err != nil {
2020-01-28 19:51:00 +02:00
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
ctx := newCtxWithSesion(req)
2020-01-08 20:16:06 +02:00
2020-01-28 19:51:00 +02:00
err := s.ServeSettingsPage(ctx, c)
2020-01-08 20:16:06 +02:00
if err != nil {
2020-01-28 19:51:00 +02:00
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
ctx := context.Background()
instance := req.FormValue("instance")
2020-01-25 12:07:06 +02:00
2020-01-28 19:51:00 +02:00
url, sessionID, err := s.NewSession(ctx, instance)
2020-01-08 20:16:06 +02:00
if err != nil {
2020-01-28 19:51:00 +02:00
s.ServeErrorPage(ctx, c, err)
2020-01-08 20:16:06 +02:00
return
}
2020-01-28 19:51:00 +02:00
http.SetCookie(w, &http.Cookie{
Name: "session_id",
Value: sessionID,
Expires: time.Now().Add(365 * 24 * time.Hour),
})
2020-01-08 20:16:06 +02:00
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) {
c := newClient(w)
ctx := newCtxWithSesion(req)
token := req.URL.Query().Get("code")
2020-01-08 20:16:06 +02:00
2020-01-28 19:51:00 +02:00
_, err := s.Signin(ctx, c, "", token)
2020-01-08 20:16:06 +02:00
if err != nil {
2020-01-28 19:51:00 +02:00
s.ServeErrorPage(ctx, c, err)
2020-01-08 20:16:06 +02:00
return
}
2020-01-28 19:51:00 +02:00
w.Header().Add("Location", "/timeline/home")
w.WriteHeader(http.StatusFound)
}
post := func(w http.ResponseWriter, req *http.Request) {
c := newClient(w)
2019-12-14 22:19:02 +02:00
err := req.ParseMultipartForm(4 << 20)
if err != nil {
2020-01-28 19:51:00 +02:00
s.ServeErrorPage(context.Background(), c, err)
2019-12-14 22:19:02 +02:00
return
}
2020-01-28 19:51:00 +02:00
ctx := newCtxWithSesionCSRF(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-01-28 19:51:00 +02:00
id, err := s.Post(ctx, c, content, replyToID, format, visibility, isNSFW, files)
2019-12-13 20:08:26 +02:00
if err != nil {
2020-01-28 19:51:00 +02:00
s.ServeErrorPage(ctx, c, err)
2019-12-13 20:08:26 +02:00
return
}
2019-12-25 06:30:21 +02:00
location := "/timeline/home" + "#status-" + id
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) {
c := newClient(w)
ctx := newCtxWithSesionCSRF(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-01-28 19:51:00 +02:00
_, err := s.Like(ctx, c, id)
if err != nil {
2020-01-28 19:51:00 +02:00
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
ctx := newCtxWithSesionCSRF(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-01-28 19:51:00 +02:00
_, err := s.UnLike(ctx, c, id)
if err != nil {
2020-01-28 19:51:00 +02:00
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
ctx := newCtxWithSesionCSRF(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-01-28 19:51:00 +02:00
_, err := s.Retweet(ctx, c, id)
if err != nil {
2020-01-28 19:51:00 +02:00
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token"))
id, _ := mux.Vars(req)["id"]
retweetedByID := req.FormValue("retweeted_by_id")
2019-12-21 07:48:48 +02:00
2020-01-28 19:51:00 +02:00
_, err := s.UnRetweet(ctx, c, id)
2019-12-21 07:48:48 +02:00
if err != nil {
2020-01-28 19:51:00 +02:00
s.ServeErrorPage(ctx, 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
2020-01-28 19:51:00 +02: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 21:18:09 +02:00
2020-01-28 19:51:00 +02:00
err := s.Follow(ctx, c, id)
2019-12-26 21:18:09 +02:00
if err != nil {
2020-01-28 19:51:00 +02:00
s.ServeErrorPage(ctx, 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) {
c := newClient(w)
ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token"))
id, _ := mux.Vars(req)["id"]
2019-12-27 10:06:43 +02:00
2020-01-28 19:51:00 +02:00
err := s.UnFollow(ctx, c, id)
2019-12-27 10:06:43 +02:00
if err != nil {
2020-01-28 19:51:00 +02:00
s.ServeErrorPage(ctx, 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-01-28 19:51:00 +02:00
settings := func(w http.ResponseWriter, req *http.Request) {
c := newClient(w)
ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token"))
2019-12-27 10:06:43 +02:00
visibility := req.FormValue("visibility")
copyScope := req.FormValue("copy_scope") == "true"
2019-12-29 13:32:24 +02:00
threadInNewTab := req.FormValue("thread_in_new_tab") == "true"
2019-12-31 13:00:21 +02:00
maskNSFW := req.FormValue("mask_nsfw") == "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-01-28 19:51:00 +02:00
2019-12-27 10:06:43 +02:00
settings := &model.Settings{
DefaultVisibility: visibility,
2019-12-29 05:43:57 +02:00
CopyScope: copyScope,
2019-12-29 13:32:24 +02:00
ThreadInNewTab: threadInNewTab,
2019-12-31 13:00:21 +02:00
MaskNSFW: maskNSFW,
2020-01-08 20:16:06 +02:00
FluorideMode: fluorideMode,
2020-01-14 18:57:16 +02:00
DarkMode: darkMode,
2019-12-27 10:06:43 +02:00
}
2020-01-28 19:51:00 +02:00
err := s.SaveSettings(ctx, c, settings)
2019-12-27 10:06:43 +02:00
if err != nil {
2020-01-28 19:51:00 +02:00
s.ServeErrorPage(ctx, c, err)
2019-12-27 10:06:43 +02:00
return
}
w.Header().Add("Location", req.Header.Get("Referer"))
w.WriteHeader(http.StatusFound)
2020-01-28 19:51:00 +02:00
}
2019-12-27 10:06:43 +02:00
2020-01-28 19:51:00 +02:00
signout := func(w http.ResponseWriter, req *http.Request) {
2019-12-13 22:23:15 +02:00
// TODO remove session from database
2019-12-29 07:59:31 +02:00
http.SetCookie(w, &http.Cookie{
2019-12-29 13:32:24 +02:00
Name: "session_id",
Value: "",
Expires: time.Now(),
2019-12-29 07:59:31 +02:00
})
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) {
c := newClient(w)
ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token"))
id, _ := mux.Vars(req)["id"]
2019-12-14 22:19:02 +02:00
2020-01-28 19:51:00 +02:00
count, err := s.Like(ctx, c, id)
if err != nil {
s.ServeErrorPage(ctx, c, err)
return
}
err = serveJson(w, count)
if err != nil {
s.ServeErrorPage(ctx, c, err)
return
}
}
2020-01-28 19:51:00 +02: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 {
s.ServeErrorPage(ctx, c, err)
return
}
err = serveJson(w, count)
if err != nil {
s.ServeErrorPage(ctx, c, err)
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) {
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 {
s.ServeErrorPage(ctx, c, err)
return
}
err = serveJson(w, count)
if err != nil {
s.ServeErrorPage(ctx, c, err)
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) {
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 {
s.ServeErrorPage(ctx, c, err)
return
}
err = serveJson(w, count)
if err != nil {
s.ServeErrorPage(ctx, c, err)
return
}
}
r.HandleFunc("/", rootPage).Methods(http.MethodGet)
r.HandleFunc("/signin", signinPage).Methods(http.MethodGet)
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("/following/{id}", followingPage).Methods(http.MethodGet)
r.HandleFunc("/followers/{id}", followersPage).Methods(http.MethodGet)
r.HandleFunc("/notifications", notificationsPage).Methods(http.MethodGet)
r.HandleFunc("/user/{id}", userPage).Methods(http.MethodGet)
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("/follow/{id}", follow).Methods(http.MethodPost)
r.HandleFunc("/unfollow/{id}", unfollow).Methods(http.MethodPost)
r.HandleFunc("/settings", settings).Methods(http.MethodPost)
r.HandleFunc("/signout", signout).Methods(http.MethodGet)
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(path.Join(".", staticDir)))))
return r
2020-01-08 20:16:06 +02:00
}