Browse Source

Refactor things

master
r 2 years ago
parent
commit
fd2a353770
  1. 3
      Makefile
  2. 6
      main.go
  3. 85
      migrations/csrfToken/main.go
  4. 6
      model/client.go
  5. 283
      service/auth.go
  6. 149
      service/logging.go
  7. 219
      service/service.go
  8. 317
      service/transport.go

3
Makefile

@ -35,9 +35,6 @@ install: bloat
cp -r static/* $(SHAREPATH)/static
chmod 0644 $(SHAREPATH)/static/*
tags: $(SRC)
gotags $(SRC) > tags
uninstall:
rm -f $(BINPATH)/bloat
rm -fr $(SHAREPATH)/templates

6
main.go

@ -4,12 +4,10 @@ import (
"errors"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"bloat/config"
"bloat/kv"
@ -23,10 +21,6 @@ var (
configFile = "/etc/bloat.conf"
)
func init() {
rand.Seed(time.Now().Unix())
}
func errExit(err error) {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)

85
migrations/csrfToken/main.go

@ -1,85 +0,0 @@
package main
import (
"log"
"math/rand"
"os"
"path/filepath"
"time"
"bloat/config"
"bloat/kv"
"bloat/repository"
"bloat/util"
)
var (
configFile = "bloat.conf"
)
func init() {
rand.Seed(time.Now().Unix())
}
func getKeys(sessionRepoPath string) (keys []string, err error) {
f, err := os.Open(sessionRepoPath)
if err != nil {
return
}
return f.Readdirnames(0)
}
func main() {
opts, _, err := util.Getopts(os.Args, "f:")
if err != nil {
log.Fatal(err)
}
for _, opt := range opts {
switch opt.Option {
case 'f':
configFile = opt.Value
}
}
config, err := config.ParseFile(configFile)
if err != nil {
log.Fatal(err)
}
if !config.IsValid() {
log.Fatal("invalid config")
}
sessionRepoPath := filepath.Join(config.DatabasePath, "session")
sessionDB, err := kv.NewDatabse(sessionRepoPath)
if err != nil {
log.Fatal(err)
}
sessionRepo := repository.NewSessionRepository(sessionDB)
sessionIDs, err := getKeys(sessionRepoPath)
if err != nil {
log.Fatal(err)
}
for _, id := range sessionIDs {
s, err := sessionRepo.Get(id)
if err != nil {
log.Println(id, err)
continue
}
s.CSRFToken, err = util.NewCSRFToken()
if err != nil {
log.Println(id, err)
continue
}
err = sessionRepo.Add(s)
if err != nil {
log.Println(id, err)
continue
}
}
}

6
model/client.go

@ -6,8 +6,14 @@ import (
"bloat/mastodon"
)
type ClientCtx struct {
SessionID string
CSRFToken string
}
type Client struct {
*mastodon.Client
Writer io.Writer
Ctx ClientCtx
Session Session
}

283
service/auth.go

@ -1,7 +1,6 @@
package service
import (
"context"
"errors"
"mime/multipart"
@ -24,178 +23,174 @@ func NewAuthService(sessionRepo model.SessionRepo, appRepo model.AppRepo, s Serv
return &as{sessionRepo, appRepo, s}
}
func (s *as) authenticateClient(ctx context.Context, c *model.Client) (err error) {
sessionID, ok := ctx.Value("session_id").(string)
if !ok || len(sessionID) < 1 {
func (s *as) authenticateClient(c *model.Client) (err error) {
if len(c.Ctx.SessionID) < 1 {
return errInvalidSession
}
session, err := s.sessionRepo.Get(sessionID)
session, err := s.sessionRepo.Get(c.Ctx.SessionID)
if err != nil {
return errInvalidSession
}
client, err := s.appRepo.Get(session.InstanceDomain)
app, err := s.appRepo.Get(session.InstanceDomain)
if err != nil {
return
}
mc := mastodon.NewClient(&mastodon.Config{
Server: client.InstanceURL,
ClientID: client.ClientID,
ClientSecret: client.ClientSecret,
Server: app.InstanceURL,
ClientID: app.ClientID,
ClientSecret: app.ClientSecret,
AccessToken: session.AccessToken,
})
if c == nil {
c = &model.Client{}
}
c.Client = mc
c.Session = session
return nil
}
func checkCSRF(ctx context.Context, c *model.Client) (err error) {
token, ok := ctx.Value("csrf_token").(string)
if !ok || token != c.Session.CSRFToken {
func checkCSRF(c *model.Client) (err error) {
if c.Ctx.CSRFToken != c.Session.CSRFToken {
return errInvalidCSRFToken
}
return nil
}
func (s *as) ServeErrorPage(ctx context.Context, c *model.Client, err error) {
s.authenticateClient(ctx, c)
s.Service.ServeErrorPage(ctx, c, err)
func (s *as) ServeErrorPage(c *model.Client, err error) {
s.authenticateClient(c)
s.Service.ServeErrorPage(c, err)
}
func (s *as) ServeSigninPage(ctx context.Context, c *model.Client) (err error) {
return s.Service.ServeSigninPage(ctx, c)
func (s *as) ServeSigninPage(c *model.Client) (err error) {
return s.Service.ServeSigninPage(c)
}
func (s *as) ServeRootPage(ctx context.Context, c *model.Client) (err error) {
err = s.authenticateClient(ctx, c)
func (s *as) ServeRootPage(c *model.Client) (err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
return s.Service.ServeRootPage(ctx, c)
return s.Service.ServeRootPage(c)
}
func (s *as) ServeNavPage(ctx context.Context, c *model.Client) (err error) {
err = s.authenticateClient(ctx, c)
func (s *as) ServeNavPage(c *model.Client) (err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
return s.Service.ServeNavPage(ctx, c)
return s.Service.ServeNavPage(c)
}
func (s *as) ServeTimelinePage(ctx context.Context, c *model.Client, tType string,
func (s *as) ServeTimelinePage(c *model.Client, tType string,
maxID string, minID string) (err error) {
err = s.authenticateClient(ctx, c)
err = s.authenticateClient(c)
if err != nil {
return
}
return s.Service.ServeTimelinePage(ctx, c, tType, maxID, minID)
return s.Service.ServeTimelinePage(c, tType, maxID, minID)
}
func (s *as) ServeThreadPage(ctx context.Context, c *model.Client, id string, reply bool) (err error) {
err = s.authenticateClient(ctx, c)
func (s *as) ServeThreadPage(c *model.Client, id string, reply bool) (err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
return s.Service.ServeThreadPage(ctx, c, id, reply)
return s.Service.ServeThreadPage(c, id, reply)
}
func (s *as) ServeLikedByPage(ctx context.Context, c *model.Client, id string) (err error) {
err = s.authenticateClient(ctx, c)
func (s *as) ServeLikedByPage(c *model.Client, id string) (err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
return s.Service.ServeLikedByPage(ctx, c, id)
return s.Service.ServeLikedByPage(c, id)
}
func (s *as) ServeRetweetedByPage(ctx context.Context, c *model.Client, id string) (err error) {
err = s.authenticateClient(ctx, c)
func (s *as) ServeRetweetedByPage(c *model.Client, id string) (err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
return s.Service.ServeRetweetedByPage(ctx, c, id)
return s.Service.ServeRetweetedByPage(c, id)
}
func (s *as) ServeNotificationPage(ctx context.Context, c *model.Client,
func (s *as) ServeNotificationPage(c *model.Client,
maxID string, minID string) (err error) {
err = s.authenticateClient(ctx, c)
err = s.authenticateClient(c)
if err != nil {
return
}
return s.Service.ServeNotificationPage(ctx, c, maxID, minID)
return s.Service.ServeNotificationPage(c, maxID, minID)
}
func (s *as) ServeUserPage(ctx context.Context, c *model.Client, id string,
func (s *as) ServeUserPage(c *model.Client, id string,
pageType string, maxID string, minID string) (err error) {
err = s.authenticateClient(ctx, c)
err = s.authenticateClient(c)
if err != nil {
return
}
return s.Service.ServeUserPage(ctx, c, id, pageType, maxID, minID)
return s.Service.ServeUserPage(c, id, pageType, maxID, minID)
}
func (s *as) ServeAboutPage(ctx context.Context, c *model.Client) (err error) {
err = s.authenticateClient(ctx, c)
func (s *as) ServeAboutPage(c *model.Client) (err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
return s.Service.ServeAboutPage(ctx, c)
return s.Service.ServeAboutPage(c)
}
func (s *as) ServeEmojiPage(ctx context.Context, c *model.Client) (err error) {
err = s.authenticateClient(ctx, c)
func (s *as) ServeEmojiPage(c *model.Client) (err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
return s.Service.ServeEmojiPage(ctx, c)
return s.Service.ServeEmojiPage(c)
}
func (s *as) ServeSearchPage(ctx context.Context, c *model.Client, q string,
func (s *as) ServeSearchPage(c *model.Client, q string,
qType string, offset int) (err error) {
err = s.authenticateClient(ctx, c)
err = s.authenticateClient(c)
if err != nil {
return
}
return s.Service.ServeSearchPage(ctx, c, q, qType, offset)
return s.Service.ServeSearchPage(c, q, qType, offset)
}
func (s *as) ServeUserSearchPage(ctx context.Context, c *model.Client,
func (s *as) ServeUserSearchPage(c *model.Client,
id string, q string, offset int) (err error) {
err = s.authenticateClient(ctx, c)
err = s.authenticateClient(c)
if err != nil {
return
}
return s.Service.ServeUserSearchPage(ctx, c, id, q, offset)
return s.Service.ServeUserSearchPage(c, id, q, offset)
}
func (s *as) ServeSettingsPage(ctx context.Context, c *model.Client) (err error) {
err = s.authenticateClient(ctx, c)
func (s *as) ServeSettingsPage(c *model.Client) (err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
return s.Service.ServeSettingsPage(ctx, c)
return s.Service.ServeSettingsPage(c)
}
func (s *as) NewSession(ctx context.Context, instance string) (redirectUrl string,
func (s *as) NewSession(instance string) (redirectUrl string,
sessionID string, err error) {
return s.Service.NewSession(ctx, instance)
return s.Service.NewSession(instance)
}
func (s *as) Signin(ctx context.Context, c *model.Client, sessionID string,
func (s *as) Signin(c *model.Client, sessionID string,
code string) (token string, userID string, err error) {
err = s.authenticateClient(ctx, c)
err = s.authenticateClient(c)
if err != nil {
return
}
token, userID, err = s.Service.Signin(ctx, c, c.Session.ID, code)
token, userID, err = s.Service.Signin(c, c.Session.ID, code)
if err != nil {
return
}
c.Session.AccessToken = token
c.Session.UserID = userID
err = s.sessionRepo.Add(c.Session)
if err != nil {
return
@ -204,247 +199,247 @@ func (s *as) Signin(ctx context.Context, c *model.Client, sessionID string,
return
}
func (s *as) Signout(ctx context.Context, c *model.Client) (err error) {
err = s.authenticateClient(ctx, c)
func (s *as) Signout(c *model.Client) (err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
err = checkCSRF(ctx, c)
err = checkCSRF(c)
if err != nil {
return
}
s.Service.Signout(ctx, c)
s.Service.Signout(c)
return
}
func (s *as) Post(ctx context.Context, c *model.Client, content string,
func (s *as) Post(c *model.Client, content string,
replyToID string, format string, visibility string, isNSFW bool,
files []*multipart.FileHeader) (id string, err error) {
err = s.authenticateClient(ctx, c)
err = s.authenticateClient(c)
if err != nil {
return
}
err = checkCSRF(ctx, c)
err = checkCSRF(c)
if err != nil {
return
}
return s.Service.Post(ctx, c, content, replyToID, format, visibility, isNSFW, files)
return s.Service.Post(c, content, replyToID, format, visibility, isNSFW, files)
}
func (s *as) Like(ctx context.Context, c *model.Client, id string) (count int64, err error) {
err = s.authenticateClient(ctx, c)
func (s *as) Like(c *model.Client, id string) (count int64, err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
err = checkCSRF(ctx, c)
err = checkCSRF(c)
if err != nil {
return
}
return s.Service.Like(ctx, c, id)
return s.Service.Like(c, id)
}
func (s *as) UnLike(ctx context.Context, c *model.Client, id string) (count int64, err error) {
err = s.authenticateClient(ctx, c)
func (s *as) UnLike(c *model.Client, id string) (count int64, err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
err = checkCSRF(ctx, c)
err = checkCSRF(c)
if err != nil {
return
}
return s.Service.UnLike(ctx, c, id)
return s.Service.UnLike(c, id)
}
func (s *as) Retweet(ctx context.Context, c *model.Client, id string) (count int64, err error) {
err = s.authenticateClient(ctx, c)
func (s *as) Retweet(c *model.Client, id string) (count int64, err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
err = checkCSRF(ctx, c)
err = checkCSRF(c)
if err != nil {
return
}
return s.Service.Retweet(ctx, c, id)
return s.Service.Retweet(c, id)
}
func (s *as) UnRetweet(ctx context.Context, c *model.Client, id string) (count int64, err error) {
err = s.authenticateClient(ctx, c)
func (s *as) UnRetweet(c *model.Client, id string) (count int64, err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
err = checkCSRF(ctx, c)
err = checkCSRF(c)
if err != nil {
return
}
return s.Service.UnRetweet(ctx, c, id)
return s.Service.UnRetweet(c, id)
}
func (s *as) Vote(ctx context.Context, c *model.Client, id string,
func (s *as) Vote(c *model.Client, id string,
choices []string) (err error) {
err = s.authenticateClient(ctx, c)
err = s.authenticateClient(c)
if err != nil {
return
}
err = checkCSRF(ctx, c)
err = checkCSRF(c)
if err != nil {
return
}
return s.Service.Vote(ctx, c, id, choices)
return s.Service.Vote(c, id, choices)
}
func (s *as) Follow(ctx context.Context, c *model.Client, id string, reblogs *bool) (err error) {
err = s.authenticateClient(ctx, c)
func (s *as) Follow(c *model.Client, id string, reblogs *bool) (err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
err = checkCSRF(ctx, c)
err = checkCSRF(c)
if err != nil {
return
}
return s.Service.Follow(ctx, c, id, reblogs)
return s.Service.Follow(c, id, reblogs)
}
func (s *as) UnFollow(ctx context.Context, c *model.Client, id string) (err error) {
err = s.authenticateClient(ctx, c)
func (s *as) UnFollow(c *model.Client, id string) (err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
err = checkCSRF(ctx, c)
err = checkCSRF(c)
if err != nil {
return
}
return s.Service.UnFollow(ctx, c, id)
return s.Service.UnFollow(c, id)
}
func (s *as) Mute(ctx context.Context, c *model.Client, id string) (err error) {
err = s.authenticateClient(ctx, c)
func (s *as) Mute(c *model.Client, id string) (err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
err = checkCSRF(ctx, c)
err = checkCSRF(c)
if err != nil {
return
}
return s.Service.Mute(ctx, c, id)
return s.Service.Mute(c, id)
}
func (s *as) UnMute(ctx context.Context, c *model.Client, id string) (err error) {
err = s.authenticateClient(ctx, c)
func (s *as) UnMute(c *model.Client, id string) (err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
err = checkCSRF(ctx, c)
err = checkCSRF(c)
if err != nil {
return
}
return s.Service.UnMute(ctx, c, id)
return s.Service.UnMute(c, id)
}
func (s *as) Block(ctx context.Context, c *model.Client, id string) (err error) {
err = s.authenticateClient(ctx, c)
func (s *as) Block(c *model.Client, id string) (err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
err = checkCSRF(ctx, c)
err = checkCSRF(c)
if err != nil {
return
}
return s.Service.Block(ctx, c, id)
return s.Service.Block(c, id)
}
func (s *as) UnBlock(ctx context.Context, c *model.Client, id string) (err error) {
err = s.authenticateClient(ctx, c)
func (s *as) UnBlock(c *model.Client, id string) (err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
err = checkCSRF(ctx, c)
err = checkCSRF(c)
if err != nil {
return
}
return s.Service.UnBlock(ctx, c, id)
return s.Service.UnBlock(c, id)
}
func (s *as) Subscribe(ctx context.Context, c *model.Client, id string) (err error) {
err = s.authenticateClient(ctx, c)
func (s *as) Subscribe(c *model.Client, id string) (err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
err = checkCSRF(ctx, c)
err = checkCSRF(c)
if err != nil {
return
}
return s.Service.Subscribe(ctx, c, id)
return s.Service.Subscribe(c, id)
}
func (s *as) UnSubscribe(ctx context.Context, c *model.Client, id string) (err error) {
err = s.authenticateClient(ctx, c)
func (s *as) UnSubscribe(c *model.Client, id string) (err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
err = checkCSRF(ctx, c)
err = checkCSRF(c)
if err != nil {
return
}
return s.Service.UnSubscribe(ctx, c, id)
return s.Service.UnSubscribe(c, id)
}
func (s *as) SaveSettings(ctx context.Context, c *model.Client, settings *model.Settings) (err error) {
err = s.authenticateClient(ctx, c)
func (s *as) SaveSettings(c *model.Client, settings *model.Settings) (err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
err = checkCSRF(ctx, c)
err = checkCSRF(c)
if err != nil {
return
}
return s.Service.SaveSettings(ctx, c, settings)
return s.Service.SaveSettings(c, settings)
}
func (s *as) MuteConversation(ctx context.Context, c *model.Client, id string) (err error) {
err = s.authenticateClient(ctx, c)
func (s *as) MuteConversation(c *model.Client, id string) (err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
err = checkCSRF(ctx, c)
err = checkCSRF(c)
if err != nil {
return
}
return s.Service.MuteConversation(ctx, c, id)
return s.Service.MuteConversation(c, id)
}
func (s *as) UnMuteConversation(ctx context.Context, c *model.Client, id string) (err error) {
err = s.authenticateClient(ctx, c)
func (s *as) UnMuteConversation(c *model.Client, id string) (err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
err = checkCSRF(ctx, c)
err = checkCSRF(c)
if err != nil {
return
}
return s.Service.UnMuteConversation(ctx, c, id)
return s.Service.UnMuteConversation(c, id)
}
func (s *as) Delete(ctx context.Context, c *model.Client, id string) (err error) {
err = s.authenticateClient(ctx, c)
func (s *as) Delete(c *model.Client, id string) (err error) {
err = s.authenticateClient(c)
if err != nil {
return
}
err = checkCSRF(ctx, c)
err = checkCSRF(c)
if err != nil {
return
}
return s.Service.Delete(ctx, c, id)
return s.Service.Delete(c, id)
}
func (s *as) ReadNotifications(ctx context.Context, c *model.Client,
func (s *as) ReadNotifications(c *model.Client,
maxID string) (err error) {
err = s.authenticateClient(ctx, c)
err = s.authenticateClient(c)
if err != nil {
return
}
err = checkCSRF(ctx, c)
err = checkCSRF(c)
if err != nil {
return
}
return s.Service.ReadNotifications(ctx, c, maxID)
return s.Service.ReadNotifications(c, maxID)
}

149
service/logging.go

@ -1,7 +1,6 @@
package service
import (
"context"
"log"
"mime/multipart"
"time"
@ -18,310 +17,310 @@ func NewLoggingService(logger *log.Logger, s Service) Service {
return &ls{logger, s}
}
func (s *ls) ServeErrorPage(ctx context.Context, c *model.Client, err error) {
func (s *ls) ServeErrorPage(c *model.Client, err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, err=%v, took=%v\n",
"ServeErrorPage", err, time.Since(begin))
}(time.Now())
s.Service.ServeErrorPage(ctx, c, err)
s.Service.ServeErrorPage(c, err)
}
func (s *ls) ServeSigninPage(ctx context.Context, c *model.Client) (err error) {
func (s *ls) ServeSigninPage(c *model.Client) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, took=%v, err=%v\n",
"ServeSigninPage", time.Since(begin), err)
}(time.Now())
return s.Service.ServeSigninPage(ctx, c)
return s.Service.ServeSigninPage(c)
}
func (s *ls) ServeRootPage(ctx context.Context, c *model.Client) (err error) {
func (s *ls) ServeRootPage(c *model.Client) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, took=%v, err=%v\n",
"ServeRootPage", time.Since(begin), err)
}(time.Now())
return s.Service.ServeRootPage(ctx, c)
return s.Service.ServeRootPage(c)
}
func (s *ls) ServeNavPage(ctx context.Context, c *model.Client) (err error) {
func (s *ls) ServeNavPage(c *model.Client) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, took=%v, err=%v\n",
"ServeNavPage", time.Since(begin), err)
}(time.Now())
return s.Service.ServeNavPage(ctx, c)
return s.Service.ServeNavPage(c)
}
func (s *ls) ServeTimelinePage(ctx context.Context, c *model.Client, tType string,
func (s *ls) ServeTimelinePage(c *model.Client, tType string,
maxID string, minID string) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, type=%v, took=%v, err=%v\n",
"ServeTimelinePage", tType, time.Since(begin), err)
}(time.Now())
return s.Service.ServeTimelinePage(ctx, c, tType, maxID, minID)
return s.Service.ServeTimelinePage(c, tType, maxID, minID)
}
func (s *ls) ServeThreadPage(ctx context.Context, c *model.Client, id string,
func (s *ls) ServeThreadPage(c *model.Client, id string,
reply bool) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n",
"ServeThreadPage", id, time.Since(begin), err)
}(time.Now())
return s.Service.ServeThreadPage(ctx, c, id, reply)
return s.Service.ServeThreadPage(c, id, reply)
}
func (s *ls) ServeLikedByPage(ctx context.Context, c *model.Client, id string) (err error) {
func (s *ls) ServeLikedByPage(c *model.Client, id string) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n",
"ServeLikedByPage", id, time.Since(begin), err)
}(time.Now())
return s.Service.ServeLikedByPage(ctx, c, id)
return s.Service.ServeLikedByPage(c, id)
}
func (s *ls) ServeRetweetedByPage(ctx context.Context, c *model.Client, id string) (err error) {
func (s *ls) ServeRetweetedByPage(c *model.Client, id string) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n",
"ServeRetweetedByPage", id, time.Since(begin), err)
}(time.Now())
return s.Service.ServeRetweetedByPage(ctx, c, id)
return s.Service.ServeRetweetedByPage(c, id)
}
func (s *ls) ServeNotificationPage(ctx context.Context, c *model.Client,
func (s *ls) ServeNotificationPage(c *model.Client,
maxID string, minID string) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, took=%v, err=%v\n",
"ServeNotificationPage", time.Since(begin), err)
}(time.Now())
return s.Service.ServeNotificationPage(ctx, c, maxID, minID)
return s.Service.ServeNotificationPage(c, maxID, minID)
}
func (s *ls) ServeUserPage(ctx context.Context, c *model.Client, id string,
func (s *ls) ServeUserPage(c *model.Client, id string,
pageType string, maxID string, minID string) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, id=%v, type=%v, took=%v, err=%v\n",
"ServeUserPage", id, pageType, time.Since(begin), err)
}(time.Now())
return s.Service.ServeUserPage(ctx, c, id, pageType, maxID, minID)
return s.Service.ServeUserPage(c, id, pageType, maxID, minID)
}
func (s *ls) ServeAboutPage(ctx context.Context, c *model.Client) (err error) {
func (s *ls) ServeAboutPage(c *model.Client) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, took=%v, err=%v\n",
"ServeAboutPage", time.Since(begin), err)
}(time.Now())
return s.Service.ServeAboutPage(ctx, c)
return s.Service.ServeAboutPage(c)
}
func (s *ls) ServeEmojiPage(ctx context.Context, c *model.Client) (err error) {
func (s *ls) ServeEmojiPage(c *model.Client) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, took=%v, err=%v\n",
"ServeEmojiPage", time.Since(begin), err)
}(time.Now())
return s.Service.ServeEmojiPage(ctx, c)
return s.Service.ServeEmojiPage(c)
}
func (s *ls) ServeSearchPage(ctx context.Context, c *model.Client, q string,
func (s *ls) ServeSearchPage(c *model.Client, q string,
qType string, offset int) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, took=%v, err=%v\n",
"ServeSearchPage", time.Since(begin), err)
}(time.Now())
return s.Service.ServeSearchPage(ctx, c, q, qType, offset)
return s.Service.ServeSearchPage(c, q, qType, offset)
}
func (s *ls) ServeUserSearchPage(ctx context.Context, c *model.Client,
func (s *ls) ServeUserSearchPage(c *model.Client,
id string, q string, offset int) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, took=%v, err=%v\n",
"ServeUserSearchPage", time.Since(begin), err)
}(time.Now())
return s.Service.ServeUserSearchPage(ctx, c, id, q, offset)
return s.Service.ServeUserSearchPage(c, id, q, offset)
}
func (s *ls) ServeSettingsPage(ctx context.Context, c *model.Client) (err error) {
func (s *ls) ServeSettingsPage(c *model.Client) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, took=%v, err=%v\n",
"ServeSettingsPage", time.Since(begin), err)
}(time.Now())
return s.Service.ServeSettingsPage(ctx, c)
return s.Service.ServeSettingsPage(c)
}
func (s *ls) NewSession(ctx context.Context, instance string) (redirectUrl string,
func (s *ls) NewSession(instance string) (redirectUrl string,
sessionID string, err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, instance=%v, took=%v, err=%v\n",
"NewSession", instance, time.Since(begin), err)
}(time.Now())
return s.Service.NewSession(ctx, instance)
return s.Service.NewSession(instance)
}
func (s *ls) Signin(ctx context.Context, c *model.Client, sessionID string,
func (s *ls) Signin(c *model.Client, sessionID string,
code string) (token string, userID string, err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, session_id=%v, took=%v, err=%v\n",
"Signin", sessionID, time.Since(begin), err)
}(time.Now())
return s.Service.Signin(ctx, c, sessionID, code)
return s.Service.Signin(c, sessionID, code)
}
func (s *ls) Signout(ctx context.Context, c *model.Client) (err error) {
func (s *ls) Signout(c *model.Client) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, took=%v, err=%v\n",
"Signout", time.Since(begin), err)
}(time.Now())
return s.Service.Signout(ctx, c)
return s.Service.Signout(c)
}
func (s *ls) Post(ctx context.Context, c *model.Client, content string,
func (s *ls) Post(c *model.Client, content string,
replyToID string, format string, visibility string, isNSFW bool,
files []*multipart.FileHeader) (id string, err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, took=%v, err=%v\n",
"Post", time.Since(begin), err)
}(time.Now())
return s.Service.Post(ctx, c, content, replyToID, format,
return s.Service.Post(c, content, replyToID, format,
visibility, isNSFW, files)
}
func (s *ls) Like(ctx context.Context, c *model.Client, id string) (count int64, err error) {
func (s *ls) Like(c *model.Client, id string) (count int64, err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n",
"Like", id, time.Since(begin), err)
}(time.Now())
return s.Service.Like(ctx, c, id)
return s.Service.Like(c, id)
}
func (s *ls) UnLike(ctx context.Context, c *model.Client, id string) (count int64, err error) {
func (s *ls) UnLike(c *model.Client, id string) (count int64, err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n",
"UnLike", id, time.Since(begin), err)
}(time.Now())
return s.Service.UnLike(ctx, c, id)
return s.Service.UnLike(c, id)
}
func (s *ls) Retweet(ctx context.Context, c *model.Client, id string) (count int64, err error) {
func (s *ls) Retweet(c *model.Client, id string) (count int64, err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n",
"Retweet", id, time.Since(begin), err)
}(time.Now())
return s.Service.Retweet(ctx, c, id)
return s.Service.Retweet(c, id)
}
func (s *ls) UnRetweet(ctx context.Context, c *model.Client, id string) (count int64, err error) {
func (s *ls) UnRetweet(c *model.Client, id string) (count int64, err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n",
"UnRetweet", id, time.Since(begin), err)
}(time.Now())
return s.Service.UnRetweet(ctx, c, id)
return s.Service.UnRetweet(c, id)
}
func (s *ls) Vote(ctx context.Context, c *model.Client, id string, choices []string) (err error) {
func (s *ls) Vote(c *model.Client, id string, choices []string) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n",
"Vote", id, time.Since(begin), err)
}(time.Now())
return s.Service.Vote(ctx, c, id, choices)
return s.Service.Vote(c, id, choices)
}
func (s *ls) Follow(ctx context.Context, c *model.Client, id string, reblogs *bool) (err error) {
func (s *ls) Follow(c *model.Client, id string, reblogs *bool) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n",
"Follow", id, time.Since(begin), err)
}(time.Now())
return s.Service.Follow(ctx, c, id, reblogs)
return s.Service.Follow(c, id, reblogs)
}
func (s *ls) UnFollow(ctx context.Context, c *model.Client, id string) (err error) {
func (s *ls) UnFollow(c *model.Client, id string) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n",
"UnFollow", id, time.Since(begin), err)
}(time.Now())
return s.Service.UnFollow(ctx, c, id)
return s.Service.UnFollow(c, id)
}
func (s *ls) Mute(ctx context.Context, c *model.Client, id string) (err error) {
func (s *ls) Mute(c *model.Client, id string) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n",
"Mute", id, time.Since(begin), err)
}(time.Now())
return s.Service.Mute(ctx, c, id)
return s.Service.Mute(c, id)
}
func (s *ls) UnMute(ctx context.Context, c *model.Client, id string) (err error) {
func (s *ls) UnMute(c *model.Client, id string) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n",
"UnMute", id, time.Since(begin), err)
}(time.Now())
return s.Service.UnMute(ctx, c, id)
return s.Service.UnMute(c, id)
}
func (s *ls) Block(ctx context.Context, c *model.Client, id string) (err error) {
func (s *ls) Block(c *model.Client, id string) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n",
"Block", id, time.Since(begin), err)
}(time.Now())
return s.Service.Block(ctx, c, id)
return s.Service.Block(c, id)
}
func (s *ls) UnBlock(ctx context.Context, c *model.Client, id string) (err error) {
func (s *ls) UnBlock(c *model.Client, id string) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n",
"UnBlock", id, time.Since(begin), err)
}(time.Now())
return s.Service.UnBlock(ctx, c, id)
return s.Service.UnBlock(c, id)
}
func (s *ls) Subscribe(ctx context.Context, c *model.Client, id string) (err error) {
func (s *ls) Subscribe(c *model.Client, id string) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n",
"Subscribe", id, time.Since(begin), err)
}(time.Now())
return s.Service.Subscribe(ctx, c, id)
return s.Service.Subscribe(c, id)
}
func (s *ls) UnSubscribe(ctx context.Context, c *model.Client, id string) (err error) {
func (s *ls) UnSubscribe(c *model.Client, id string) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n",
"UnSubscribe", id, time.Since(begin), err)
}(time.Now())
return s.Service.UnSubscribe(ctx, c, id)
return s.Service.UnSubscribe(c, id)
}
func (s *ls) SaveSettings(ctx context.Context, c *model.Client, settings *model.Settings) (err error) {
func (s *ls) SaveSettings(c *model.Client, settings *model.Settings) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, took=%v, err=%v\n",
"SaveSettings", time.Since(begin), err)
}(time.Now())
return s.Service.SaveSettings(ctx, c, settings)
return s.Service.SaveSettings(c, settings)
}
func (s *ls) MuteConversation(ctx context.Context, c *model.Client, id string) (err error) {
func (s *ls) MuteConversation(c *model.Client, id string) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n",
"MuteConversation", id, time.Since(begin), err)
}(time.Now())
return s.Service.MuteConversation(ctx, c, id)
return s.Service.MuteConversation(c, id)
}
func (s *ls) UnMuteConversation(ctx context.Context, c *model.Client, id string) (err error) {
func (s *ls) UnMuteConversation(c *model.Client, id string) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n",
"UnMuteConversation", id, time.Since(begin), err)
}(time.Now())
return s.Service.UnMuteConversation(ctx, c, id)
return s.Service.UnMuteConversation(c, id)
}
func (s *ls) Delete(ctx context.Context, c *model.Client, id string) (err error) {
func (s *ls) Delete(c *model.Client, id string) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n",
"Delete", id, time.Since(begin), err)
}(time.Now())
return s.Service.Delete(ctx, c, id)
return s.Service.Delete(c, id)
}
func (s *ls) ReadNotifications(ctx context.Context, c *model.Client,
func (s *ls) ReadNotifications(c *model.Client,
maxID string) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, max_id=%v, took=%v, err=%v\n",
"ReadNotifications", maxID, time.Since(begin), err)
}(time.Now())
return s.Service.ReadNotifications(ctx, c, maxID)
return s.Service.ReadNotifications(c, maxID)
}

219
service/service.go

@ -15,51 +15,53 @@ import (
)
var (
ctx = context.Background()
errInvalidArgument = errors.New("invalid argument")
)
type Service interface {
ServeErrorPage(ctx context.Context, c *model.Client, err error)
ServeSigninPage(ctx context.Context, c *model.Client) (err error)
ServeRootPage(ctx context.Context, c *model.Client) (err error)
ServeNavPage(ctx context.Context, c *model.Client) (err error)
ServeTimelinePage(ctx context.Context, c *model.Client, tType string, maxID string, minID string) (err error)
ServeThreadPage(ctx context.Context, c *model.Client, id string, reply bool) (err error)
ServeLikedByPage(ctx context.Context, c *model.Client, id string) (err error)
ServeRetweetedByPage(ctx context.Context, c *model.Client, id string) (err error)
ServeNotificationPage(ctx context.Context, c *model.Client, maxID string, minID string) (err error)
ServeUserPage(ctx context.Context, c *model.Client, id string, pageType string,
maxID string, minID string) (err error)
ServeAboutPage(ctx context.Context, c *model.Client) (err error)
ServeEmojiPage(ctx context.Context, c *model.Client) (err error)
ServeSearchPage(ctx context.Context, c *model.Client, q string, qType string, offset int) (err error)
ServeUserSearchPage(ctx context.Context, c *model.Client, id string, q string, offset int) (err error)
ServeSettingsPage(ctx context.Context, c *model.Client) (err error)
SingleInstance(ctx context.Context) (instance string, ok bool)
NewSession(ctx context.Context, instance string) (redirectUrl string, sessionID string, err error)
Signin(ctx context.Context, c *model.Client, sessionID string,
code string) (token string, userID string, err error)
Signout(ctx context.Context, c *model.Client) (err error)
Post(ctx context.Context, c *model.Client, content string, replyToID string, format string,
visibility string, isNSFW bool, files []*multipart.FileHeader) (id string, err error)
Like(ctx context.Context, c *model.Client, id string) (count int64, err error)
UnLike(ctx context.Context, c *model.Client, id string) (count int64, err error)
Retweet(ctx context.Context, c *model.Client, id string) (count int64, err error)
UnRetweet(ctx context.Context, c *model.Client, id string) (count int64, err error)
Vote(ctx context.Context, c *model.Client, id string, choices []string) (err error)
Follow(ctx context.Context, c *model.Client, id string, reblogs *bool) (err error)
UnFollow(ctx context.Context, c *model.Client, id string) (err error)
Mute(ctx context.Context, c *model.Client, id string) (err error)
UnMute(ctx context.Context, c *model.Client, id string) (err error)
Block(ctx context.Context, c *model.Client, id string) (err error)
UnBlock(ctx context.Context, c *model.Client, id string) (err error)
Subscribe(ctx context.Context, c *model.Client, id string) (err error)
UnSubscribe(ctx context.Context, c *model.Client, id string) (err error)
SaveSettings(ctx context.Context, c *model.Client, settings *model.Settings) (err error)
MuteConversation(ctx context.Context, c *model.Client, id string) (err error)
UnMuteConversation(ctx context.Context, c *model.Client, id string) (err error)
Delete(ctx context.Context, c *model.Client, id string) (err error)
ReadNotifications(ctx context.Context, c *model.Client, maxID string) (err error)
ServeErrorPage(c *model.Client, err error)
ServeSigninPage(c *model.Client) (err error)
ServeRootPage(c *model.Client) (err error)
ServeNavPage(c *model.Client) (err error)
ServeTimelinePage(c *model.Client, tType string, maxID string,
minID string) (err error)
ServeThreadPage(c *model.Client, id string, reply bool) (err error)
ServeLikedByPage(c *model.Client, id string) (err error)
ServeRetweetedByPage(c *model.Client, id string) (err error)
ServeNotificationPage(c *model.Client, maxID string, minID string) (err error)
ServeUserPage(c *model.Client, id string, pageType string, maxID string,
minID string) (err error)
ServeAboutPage(c *model.Client) (err error)
ServeEmojiPage(c *model.Client) (err error)
ServeSearchPage(c *model.Client, q string, qType string, offset int) (err error)
ServeUserSearchPage(c *model.Client, id string, q string, offset int) (err error)
ServeSettingsPage(c *model.Client) (err error)
SingleInstance() (instance string, ok bool)
NewSession(instance string) (redirectUrl string, sessionID string, err error)
Signin(c *model.Client, sessionID string, code string) (token string,
userID string, err error)
Signout(c *model.Client) (err error)
Post(c *model.Client, content string, replyToID string, format string, visibility string,
isNSFW bool, files []*multipart.FileHeader) (id string, err error)
Like(c *model.Client, id string) (count int64, err error)
UnLike(c *model.Client, id string) (count int64, err error)
Retweet(c *model.Client, id string) (count int64, err error)
UnRetweet(c *model.Client, id string) (count int64, err error)
Vote(c *model.Client, id string, choices []string) (err error)
Follow(c *model.Client, id string, reblogs *bool) (err error)
UnFollow(c *model.Client, id string) (err error)
Mute(c *model.Client, id string) (err error)
UnMute(c *model.Client, id string) (err error)
Block(c *model.Client, id string) (err error)
UnBlock(c *model.Client, id string) (err error)
Subscribe(c *model.Client, id string) (err error)
UnSubscribe(c *model.Client, id string) (err error)
SaveSettings(c *model.Client, settings *model.Settings) (err error)
MuteConversation(c *model.Client, id string) (err error)
UnMuteConversation(c *model.Client, id string) (err error)
Delete(c *model.Client, id string) (err error)
ReadNotifications(c *model.Client, maxID string) (err error)
}
type service struct {
@ -136,8 +138,9 @@ func addToReplyMap(m map[string][]mastodon.ReplyInfo, key interface{},
m[keyStr] = append(m[keyStr], mastodon.ReplyInfo{val, number})