Use a custom client for settings

This commit is contained in:
r 2019-12-21 11:13:21 +00:00
parent e73eb1162a
commit 3af4361927
6 changed files with 74 additions and 59 deletions

8
model/client.go Normal file
View file

@ -0,0 +1,8 @@
package model
import "mastodon"
type Client struct {
*mastodon.Client
Session Session
}

View file

@ -9,9 +9,10 @@ var (
)
type Session struct {
ID string `json:"id"`
InstanceDomain string `json:"instance_domain"`
AccessToken string `json:"access_token"`
ID string `json:"id"`
InstanceDomain string `json:"instance_domain"`
AccessToken string `json:"access_token"`
Settings Settings `json:"settings"`
}
type SessionRepository interface {

4
model/settings.go Normal file
View file

@ -0,0 +1,4 @@
package model
type Settings struct {
}

View file

@ -31,7 +31,7 @@ func getSessionID(ctx context.Context) (sessionID string, err error) {
return sessionID, nil
}
func (s *authService) getClient(ctx context.Context) (c *mastodon.Client, err error) {
func (s *authService) getClient(ctx context.Context) (c *model.Client, err error) {
sessionID, err := getSessionID(ctx)
if err != nil {
return nil, ErrInvalidSession
@ -44,12 +44,13 @@ func (s *authService) getClient(ctx context.Context) (c *mastodon.Client, err er
if err != nil {
return
}
c = mastodon.NewClient(&mastodon.Config{
mc := mastodon.NewClient(&mastodon.Config{
Server: client.InstanceURL,
ClientID: client.ClientID,
ClientSecret: client.ClientSecret,
AccessToken: session.AccessToken,
})
c = &model.Client{Client: mc}
return c, nil
}
@ -58,7 +59,7 @@ func (s *authService) GetAuthUrl(ctx context.Context, instance string) (
return s.Service.GetAuthUrl(ctx, instance)
}
func (s *authService) GetUserToken(ctx context.Context, sessionID string, c *mastodon.Client,
func (s *authService) GetUserToken(ctx context.Context, sessionID string, c *model.Client,
code string) (token string, err error) {
sessionID, err = getSessionID(ctx)
if err != nil {
@ -95,7 +96,7 @@ func (s *authService) ServeSigninPage(ctx context.Context, client io.Writer) (er
}
func (s *authService) ServeTimelinePage(ctx context.Context, client io.Writer,
c *mastodon.Client, maxID string, sinceID string, minID string) (err error) {
c *model.Client, maxID string, sinceID string, minID string) (err error) {
c, err = s.getClient(ctx)
if err != nil {
return
@ -103,7 +104,7 @@ func (s *authService) ServeTimelinePage(ctx context.Context, client io.Writer,
return s.Service.ServeTimelinePage(ctx, client, c, maxID, sinceID, minID)
}
func (s *authService) ServeThreadPage(ctx context.Context, client io.Writer, c *mastodon.Client, id string, reply bool) (err error) {
func (s *authService) ServeThreadPage(ctx context.Context, client io.Writer, c *model.Client, id string, reply bool) (err error) {
c, err = s.getClient(ctx)
if err != nil {
return
@ -111,7 +112,7 @@ func (s *authService) ServeThreadPage(ctx context.Context, client io.Writer, c *
return s.Service.ServeThreadPage(ctx, client, c, id, reply)
}
func (s *authService) ServeNotificationPage(ctx context.Context, client io.Writer, c *mastodon.Client, maxID string, minID string) (err error) {
func (s *authService) ServeNotificationPage(ctx context.Context, client io.Writer, c *model.Client, maxID string, minID string) (err error) {
c, err = s.getClient(ctx)
if err != nil {
return
@ -119,7 +120,7 @@ func (s *authService) ServeNotificationPage(ctx context.Context, client io.Write
return s.Service.ServeNotificationPage(ctx, client, c, maxID, minID)
}
func (s *authService) ServeUserPage(ctx context.Context, client io.Writer, c *mastodon.Client, id string, maxID string, minID string) (err error) {
func (s *authService) ServeUserPage(ctx context.Context, client io.Writer, c *model.Client, id string, maxID string, minID string) (err error) {
c, err = s.getClient(ctx)
if err != nil {
return
@ -127,7 +128,7 @@ func (s *authService) ServeUserPage(ctx context.Context, client io.Writer, c *ma
return s.Service.ServeUserPage(ctx, client, c, id, maxID, minID)
}
func (s *authService) ServeAboutPage(ctx context.Context, client io.Writer, c *mastodon.Client) (err error) {
func (s *authService) ServeAboutPage(ctx context.Context, client io.Writer, c *model.Client) (err error) {
c, err = s.getClient(ctx)
if err != nil {
return
@ -135,7 +136,7 @@ func (s *authService) ServeAboutPage(ctx context.Context, client io.Writer, c *m
return s.Service.ServeAboutPage(ctx, client, c)
}
func (s *authService) Like(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error) {
func (s *authService) Like(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) {
c, err = s.getClient(ctx)
if err != nil {
return
@ -143,7 +144,7 @@ func (s *authService) Like(ctx context.Context, client io.Writer, c *mastodon.Cl
return s.Service.Like(ctx, client, c, id)
}
func (s *authService) UnLike(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error) {
func (s *authService) UnLike(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) {
c, err = s.getClient(ctx)
if err != nil {
return
@ -151,7 +152,7 @@ func (s *authService) UnLike(ctx context.Context, client io.Writer, c *mastodon.
return s.Service.UnLike(ctx, client, c, id)
}
func (s *authService) Retweet(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error) {
func (s *authService) Retweet(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) {
c, err = s.getClient(ctx)
if err != nil {
return
@ -159,7 +160,7 @@ func (s *authService) Retweet(ctx context.Context, client io.Writer, c *mastodon
return s.Service.Retweet(ctx, client, c, id)
}
func (s *authService) UnRetweet(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error) {
func (s *authService) UnRetweet(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) {
c, err = s.getClient(ctx)
if err != nil {
return
@ -167,7 +168,7 @@ func (s *authService) UnRetweet(ctx context.Context, client io.Writer, c *mastod
return s.Service.UnRetweet(ctx, client, c, id)
}
func (s *authService) PostTweet(ctx context.Context, client io.Writer, c *mastodon.Client, content string, replyToID string, files []*multipart.FileHeader) (id string, err error) {
func (s *authService) PostTweet(ctx context.Context, client io.Writer, c *model.Client, content string, replyToID string, files []*multipart.FileHeader) (id string, err error) {
c, err = s.getClient(ctx)
if err != nil {
return
@ -175,7 +176,7 @@ func (s *authService) PostTweet(ctx context.Context, client io.Writer, c *mastod
return s.Service.PostTweet(ctx, client, c, content, replyToID, files)
}
func (s *authService) Follow(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error) {
func (s *authService) Follow(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) {
c, err = s.getClient(ctx)
if err != nil {
return
@ -183,7 +184,7 @@ func (s *authService) Follow(ctx context.Context, client io.Writer, c *mastodon.
return s.Service.Follow(ctx, client, c, id)
}
func (s *authService) UnFollow(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error) {
func (s *authService) UnFollow(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) {
c, err = s.getClient(ctx)
if err != nil {
return

View file

@ -4,9 +4,9 @@ import (
"context"
"io"
"log"
"mastodon"
"mime/multipart"
"time"
"web/model"
)
type loggingService struct {
@ -27,7 +27,7 @@ func (s *loggingService) GetAuthUrl(ctx context.Context, instance string) (
return s.Service.GetAuthUrl(ctx, instance)
}
func (s *loggingService) GetUserToken(ctx context.Context, sessionID string, c *mastodon.Client,
func (s *loggingService) GetUserToken(ctx context.Context, sessionID string, c *model.Client,
code string) (token string, err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, session_id=%v, code=%v, took=%v, err=%v\n",
@ -61,7 +61,7 @@ func (s *loggingService) ServeSigninPage(ctx context.Context, client io.Writer)
}
func (s *loggingService) ServeTimelinePage(ctx context.Context, client io.Writer,
c *mastodon.Client, maxID string, sinceID string, minID string) (err error) {
c *model.Client, maxID string, sinceID string, minID string) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, max_id=%v, since_id=%v, min_id=%v, took=%v, err=%v\n",
"ServeTimelinePage", maxID, sinceID, minID, time.Since(begin), err)
@ -69,7 +69,7 @@ func (s *loggingService) ServeTimelinePage(ctx context.Context, client io.Writer
return s.Service.ServeTimelinePage(ctx, client, c, maxID, sinceID, minID)
}
func (s *loggingService) ServeThreadPage(ctx context.Context, client io.Writer, c *mastodon.Client, id string, reply bool) (err error) {
func (s *loggingService) ServeThreadPage(ctx context.Context, client io.Writer, c *model.Client, id string, reply bool) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, id=%v, reply=%v, took=%v, err=%v\n",
"ServeThreadPage", id, reply, time.Since(begin), err)
@ -77,7 +77,7 @@ func (s *loggingService) ServeThreadPage(ctx context.Context, client io.Writer,
return s.Service.ServeThreadPage(ctx, client, c, id, reply)
}
func (s *loggingService) ServeNotificationPage(ctx context.Context, client io.Writer, c *mastodon.Client, maxID string, minID string) (err error) {
func (s *loggingService) ServeNotificationPage(ctx context.Context, client io.Writer, c *model.Client, maxID string, minID string) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, max_id=%v, min_id=%v, took=%v, err=%v\n",
"ServeNotificationPage", maxID, minID, time.Since(begin), err)
@ -85,7 +85,7 @@ func (s *loggingService) ServeNotificationPage(ctx context.Context, client io.Wr
return s.Service.ServeNotificationPage(ctx, client, c, maxID, minID)
}
func (s *loggingService) ServeUserPage(ctx context.Context, client io.Writer, c *mastodon.Client, id string, maxID string, minID string) (err error) {
func (s *loggingService) ServeUserPage(ctx context.Context, client io.Writer, c *model.Client, id string, maxID string, minID string) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, id=%v, max_id=%v, min_id=%v, took=%v, err=%v\n",
"ServeUserPage", id, maxID, minID, time.Since(begin), err)
@ -93,7 +93,7 @@ func (s *loggingService) ServeUserPage(ctx context.Context, client io.Writer, c
return s.Service.ServeUserPage(ctx, client, c, id, maxID, minID)
}
func (s *loggingService) ServeAboutPage(ctx context.Context, client io.Writer, c *mastodon.Client) (err error) {
func (s *loggingService) ServeAboutPage(ctx context.Context, client io.Writer, 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)
@ -101,7 +101,7 @@ func (s *loggingService) ServeAboutPage(ctx context.Context, client io.Writer, c
return s.Service.ServeAboutPage(ctx, client, c)
}
func (s *loggingService) Like(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error) {
func (s *loggingService) Like(ctx context.Context, client io.Writer, c *model.Client, id string) (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)
@ -109,7 +109,7 @@ func (s *loggingService) Like(ctx context.Context, client io.Writer, c *mastodon
return s.Service.Like(ctx, client, c, id)
}
func (s *loggingService) UnLike(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error) {
func (s *loggingService) UnLike(ctx context.Context, client io.Writer, c *model.Client, id string) (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)
@ -117,7 +117,7 @@ func (s *loggingService) UnLike(ctx context.Context, client io.Writer, c *mastod
return s.Service.UnLike(ctx, client, c, id)
}
func (s *loggingService) Retweet(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error) {
func (s *loggingService) Retweet(ctx context.Context, client io.Writer, c *model.Client, id string) (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)
@ -125,7 +125,7 @@ func (s *loggingService) Retweet(ctx context.Context, client io.Writer, c *masto
return s.Service.Retweet(ctx, client, c, id)
}
func (s *loggingService) UnRetweet(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error) {
func (s *loggingService) UnRetweet(ctx context.Context, client io.Writer, c *model.Client, id string) (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)
@ -133,7 +133,7 @@ func (s *loggingService) UnRetweet(ctx context.Context, client io.Writer, c *mas
return s.Service.UnRetweet(ctx, client, c, id)
}
func (s *loggingService) PostTweet(ctx context.Context, client io.Writer, c *mastodon.Client, content string, replyToID string, files []*multipart.FileHeader) (id string, err error) {
func (s *loggingService) PostTweet(ctx context.Context, client io.Writer, c *model.Client, content string, replyToID string, files []*multipart.FileHeader) (id string, err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, content=%v, reply_to_id=%v, took=%v, err=%v\n",
"PostTweet", content, replyToID, time.Since(begin), err)
@ -141,7 +141,7 @@ func (s *loggingService) PostTweet(ctx context.Context, client io.Writer, c *mas
return s.Service.PostTweet(ctx, client, c, content, replyToID, files)
}
func (s *loggingService) Follow(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error) {
func (s *loggingService) Follow(ctx context.Context, client io.Writer, c *model.Client, id string) (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)
@ -149,7 +149,7 @@ func (s *loggingService) Follow(ctx context.Context, client io.Writer, c *mastod
return s.Service.Follow(ctx, client, c, id)
}
func (s *loggingService) UnFollow(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error) {
func (s *loggingService) UnFollow(ctx context.Context, client io.Writer, 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)

View file

@ -26,21 +26,21 @@ var (
type Service interface {
ServeHomePage(ctx context.Context, client io.Writer) (err error)
GetAuthUrl(ctx context.Context, instance string) (url string, sessionID string, err error)
GetUserToken(ctx context.Context, sessionID string, c *mastodon.Client, token string) (accessToken string, err error)
GetUserToken(ctx context.Context, sessionID string, c *model.Client, token string) (accessToken string, err error)
ServeErrorPage(ctx context.Context, client io.Writer, err error)
ServeSigninPage(ctx context.Context, client io.Writer) (err error)
ServeTimelinePage(ctx context.Context, client io.Writer, c *mastodon.Client, maxID string, sinceID string, minID string) (err error)
ServeThreadPage(ctx context.Context, client io.Writer, c *mastodon.Client, id string, reply bool) (err error)
ServeNotificationPage(ctx context.Context, client io.Writer, c *mastodon.Client, maxID string, minID string) (err error)
ServeUserPage(ctx context.Context, client io.Writer, c *mastodon.Client, id string, maxID string, minID string) (err error)
ServeAboutPage(ctx context.Context, client io.Writer, c *mastodon.Client) (err error)
Like(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error)
UnLike(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error)
Retweet(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error)
UnRetweet(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error)
PostTweet(ctx context.Context, client io.Writer, c *mastodon.Client, content string, replyToID string, files []*multipart.FileHeader) (id string, err error)
Follow(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error)
UnFollow(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error)
ServeTimelinePage(ctx context.Context, client io.Writer, c *model.Client, maxID string, sinceID string, minID string) (err error)
ServeThreadPage(ctx context.Context, client io.Writer, c *model.Client, id string, reply bool) (err error)
ServeNotificationPage(ctx context.Context, client io.Writer, c *model.Client, maxID string, minID string) (err error)
ServeUserPage(ctx context.Context, client io.Writer, c *model.Client, id string, maxID string, minID string) (err error)
ServeAboutPage(ctx context.Context, client io.Writer, c *model.Client) (err error)
Like(ctx context.Context, client io.Writer, c *model.Client, id string) (err error)
UnLike(ctx context.Context, client io.Writer, c *model.Client, id string) (err error)
Retweet(ctx context.Context, client io.Writer, c *model.Client, id string) (err error)
UnRetweet(ctx context.Context, client io.Writer, c *model.Client, id string) (err error)
PostTweet(ctx context.Context, client io.Writer, c *model.Client, content string, replyToID string, files []*multipart.FileHeader) (id string, err error)
Follow(ctx context.Context, client io.Writer, c *model.Client, id string) (err error)
UnFollow(ctx context.Context, client io.Writer, c *model.Client, id string) (err error)
}
type service struct {
@ -132,7 +132,7 @@ func (svc *service) GetAuthUrl(ctx context.Context, instance string) (
return
}
func (svc *service) GetUserToken(ctx context.Context, sessionID string, c *mastodon.Client,
func (svc *service) GetUserToken(ctx context.Context, sessionID string, c *model.Client,
code string) (token string, err error) {
if len(code) < 1 {
err = ErrInvalidArgument
@ -209,7 +209,7 @@ func (svc *service) ServeSigninPage(ctx context.Context, client io.Writer) (err
}
func (svc *service) ServeTimelinePage(ctx context.Context, client io.Writer,
c *mastodon.Client, maxID string, sinceID string, minID string) (err error) {
c *model.Client, maxID string, sinceID string, minID string) (err error) {
var hasNext, hasPrev bool
var nextLink, prevLink string
@ -265,7 +265,7 @@ func (svc *service) ServeTimelinePage(ctx context.Context, client io.Writer,
return
}
func (svc *service) ServeThreadPage(ctx context.Context, client io.Writer, c *mastodon.Client, id string, reply bool) (err error) {
func (svc *service) ServeThreadPage(ctx context.Context, client io.Writer, c *model.Client, id string, reply bool) (err error) {
status, err := c.GetStatus(ctx, id)
if err != nil {
return
@ -323,7 +323,7 @@ func (svc *service) ServeThreadPage(ctx context.Context, client io.Writer, c *ma
return
}
func (svc *service) ServeNotificationPage(ctx context.Context, client io.Writer, c *mastodon.Client, maxID string, minID string) (err error) {
func (svc *service) ServeNotificationPage(ctx context.Context, client io.Writer, c *model.Client, maxID string, minID string) (err error) {
var hasNext bool
var nextLink string
@ -377,7 +377,7 @@ func (svc *service) ServeNotificationPage(ctx context.Context, client io.Writer,
return
}
func (svc *service) ServeUserPage(ctx context.Context, client io.Writer, c *mastodon.Client, id string, maxID string, minID string) (err error) {
func (svc *service) ServeUserPage(ctx context.Context, client io.Writer, c *model.Client, id string, maxID string, minID string) (err error) {
user, err := c.GetAccount(ctx, id)
if err != nil {
return
@ -416,7 +416,7 @@ func (svc *service) ServeUserPage(ctx context.Context, client io.Writer, c *mast
return
}
func (svc *service) ServeAboutPage(ctx context.Context, client io.Writer, c *mastodon.Client) (err error) {
func (svc *service) ServeAboutPage(ctx context.Context, client io.Writer, c *model.Client) (err error) {
navbarData, err := svc.getNavbarTemplateData(ctx, client, c)
if err != nil {
return
@ -431,7 +431,7 @@ func (svc *service) ServeAboutPage(ctx context.Context, client io.Writer, c *mas
return
}
func (svc *service) getNavbarTemplateData(ctx context.Context, client io.Writer, c *mastodon.Client) (data *renderer.NavbarTemplateData, err error) {
func (svc *service) getNavbarTemplateData(ctx context.Context, client io.Writer, c *model.Client) (data *renderer.NavbarTemplateData, err error) {
notifications, err := c.GetNotifications(ctx, nil)
if err != nil {
return
@ -449,27 +449,27 @@ func (svc *service) getNavbarTemplateData(ctx context.Context, client io.Writer,
return
}
func (svc *service) Like(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error) {
func (svc *service) Like(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) {
_, err = c.Favourite(ctx, id)
return
}
func (svc *service) UnLike(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error) {
func (svc *service) UnLike(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) {
_, err = c.Unfavourite(ctx, id)
return
}
func (svc *service) Retweet(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error) {
func (svc *service) Retweet(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) {
_, err = c.Reblog(ctx, id)
return
}
func (svc *service) UnRetweet(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error) {
func (svc *service) UnRetweet(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) {
_, err = c.Unreblog(ctx, id)
return
}
func (svc *service) PostTweet(ctx context.Context, client io.Writer, c *mastodon.Client, content string, replyToID string, files []*multipart.FileHeader) (id string, err error) {
func (svc *service) PostTweet(ctx context.Context, client io.Writer, c *model.Client, content string, replyToID string, files []*multipart.FileHeader) (id string, err error) {
var mediaIds []string
for _, f := range files {
a, err := c.UploadMediaFromMultipartFileHeader(ctx, f)
@ -493,12 +493,12 @@ func (svc *service) PostTweet(ctx context.Context, client io.Writer, c *mastodon
return s.ID, nil
}
func (svc *service) Follow(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error) {
func (svc *service) Follow(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) {
_, err = c.AccountFollow(ctx, id)
return
}
func (svc *service) UnFollow(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error) {
func (svc *service) UnFollow(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) {
_, err = c.AccountUnfollow(ctx, id)
return
}
@ -507,6 +507,7 @@ func addToReplyMap(m map[string][]mastodon.ReplyInfo, key interface{}, val strin
if key == nil {
return
}
keyStr, ok := key.(string)
if !ok {
return