bloat/service/auth.go

248 lines
6.9 KiB
Go
Raw Normal View History

2019-12-13 18:08:26 +00:00
package service
import (
"context"
"errors"
"io"
2019-12-14 20:19:02 +00:00
"mime/multipart"
2020-01-01 15:58:27 +00:00
"bloat/model"
"mastodon"
2019-12-13 18:08:26 +00:00
)
var (
ErrInvalidSession = errors.New("invalid session")
)
type authService struct {
sessionRepo model.SessionRepository
appRepo model.AppRepository
Service
}
func NewAuthService(sessionRepo model.SessionRepository, appRepo model.AppRepository, s Service) Service {
return &authService{sessionRepo, appRepo, s}
}
func (s *authService) getClient(ctx context.Context) (c *model.Client, err error) {
2019-12-13 18:08:26 +00:00
sessionID, ok := ctx.Value("session_id").(string)
if !ok || len(sessionID) < 1 {
return nil, ErrInvalidSession
}
session, err := s.sessionRepo.Get(sessionID)
if err != nil {
return nil, ErrInvalidSession
}
client, err := s.appRepo.Get(session.InstanceDomain)
2019-12-13 18:08:26 +00:00
if err != nil {
return
}
2019-12-21 11:13:21 +00:00
mc := mastodon.NewClient(&mastodon.Config{
Server: client.InstanceURL,
2019-12-13 18:08:26 +00:00
ClientID: client.ClientID,
ClientSecret: client.ClientSecret,
AccessToken: session.AccessToken,
})
c = &model.Client{Client: mc, Session: session}
2019-12-13 18:08:26 +00:00
return c, nil
}
func (s *authService) GetAuthUrl(ctx context.Context, instance string) (
redirectUrl string, sessionID string, err error) {
return s.Service.GetAuthUrl(ctx, instance)
}
2019-12-21 11:13:21 +00:00
func (s *authService) GetUserToken(ctx context.Context, sessionID string, c *model.Client,
2019-12-13 18:08:26 +00:00
code string) (token string, err error) {
c, err = s.getClient(ctx)
if err != nil {
return
}
token, err = s.Service.GetUserToken(ctx, c.Session.ID, c, code)
2019-12-13 18:08:26 +00:00
if err != nil {
return
}
c.Session.AccessToken = token
err = s.sessionRepo.Add(c.Session)
2019-12-13 18:08:26 +00:00
if err != nil {
return
}
return
}
func (s *authService) ServeHomePage(ctx context.Context, client io.Writer) (err error) {
return s.Service.ServeHomePage(ctx, client)
}
func (s *authService) ServeErrorPage(ctx context.Context, client io.Writer, err error) {
s.Service.ServeErrorPage(ctx, client, err)
}
func (s *authService) ServeSigninPage(ctx context.Context, client io.Writer) (err error) {
return s.Service.ServeSigninPage(ctx, client)
}
func (s *authService) ServeTimelinePage(ctx context.Context, client io.Writer,
2019-12-25 04:30:21 +00:00
c *model.Client, timelineType string, maxID string, sinceID string, minID string) (err error) {
2019-12-13 18:08:26 +00:00
c, err = s.getClient(ctx)
if err != nil {
return
}
2019-12-25 04:30:21 +00:00
return s.Service.ServeTimelinePage(ctx, client, c, timelineType, maxID, sinceID, minID)
2019-12-13 18:08:26 +00:00
}
2019-12-21 11:13:21 +00:00
func (s *authService) ServeThreadPage(ctx context.Context, client io.Writer, c *model.Client, id string, reply bool) (err error) {
2019-12-13 18:08:26 +00:00
c, err = s.getClient(ctx)
if err != nil {
return
}
return s.Service.ServeThreadPage(ctx, client, c, id, reply)
}
2019-12-21 11:13:21 +00:00
func (s *authService) ServeNotificationPage(ctx context.Context, client io.Writer, c *model.Client, maxID string, minID string) (err error) {
2019-12-15 17:37:58 +00:00
c, err = s.getClient(ctx)
if err != nil {
return
}
return s.Service.ServeNotificationPage(ctx, client, c, maxID, minID)
}
2019-12-21 11:13:21 +00:00
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
}
return s.Service.ServeUserPage(ctx, client, c, id, maxID, minID)
}
2019-12-21 11:13:21 +00:00
func (s *authService) ServeAboutPage(ctx context.Context, client io.Writer, c *model.Client) (err error) {
2019-12-21 05:48:48 +00:00
c, err = s.getClient(ctx)
if err != nil {
return
}
return s.Service.ServeAboutPage(ctx, client, c)
}
2019-12-22 18:10:42 +00:00
func (s *authService) ServeEmojiPage(ctx context.Context, client io.Writer, c *model.Client) (err error) {
c, err = s.getClient(ctx)
if err != nil {
return
}
return s.Service.ServeEmojiPage(ctx, client, c)
}
2019-12-26 09:11:24 +00:00
func (s *authService) ServeLikedByPage(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) {
c, err = s.getClient(ctx)
if err != nil {
return
}
return s.Service.ServeLikedByPage(ctx, client, c, id)
}
func (s *authService) ServeRetweetedByPage(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) {
c, err = s.getClient(ctx)
if err != nil {
return
}
return s.Service.ServeRetweetedByPage(ctx, client, c, id)
}
2019-12-29 03:43:57 +00:00
func (s *authService) ServeFollowingPage(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
}
return s.Service.ServeFollowingPage(ctx, client, c, id, maxID, minID)
}
func (s *authService) ServeFollowersPage(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
}
return s.Service.ServeFollowersPage(ctx, client, c, id, maxID, minID)
}
2019-12-26 19:18:09 +00:00
func (s *authService) ServeSearchPage(ctx context.Context, client io.Writer, c *model.Client, q string, qType string, offset int) (err error) {
c, err = s.getClient(ctx)
if err != nil {
return
}
return s.Service.ServeSearchPage(ctx, client, c, q, qType, offset)
}
2019-12-27 08:06:43 +00:00
func (s *authService) ServeSettingsPage(ctx context.Context, client io.Writer, c *model.Client) (err error) {
c, err = s.getClient(ctx)
if err != nil {
return
}
return s.Service.ServeSettingsPage(ctx, client, c)
}
func (s *authService) SaveSettings(ctx context.Context, client io.Writer, c *model.Client, settings *model.Settings) (err error) {
c, err = s.getClient(ctx)
if err != nil {
return
}
return s.Service.SaveSettings(ctx, client, c, settings)
}
2019-12-21 11:13:21 +00:00
func (s *authService) Like(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) {
2019-12-13 18:08:26 +00:00
c, err = s.getClient(ctx)
if err != nil {
return
}
return s.Service.Like(ctx, client, c, id)
}
2019-12-21 11:13:21 +00:00
func (s *authService) UnLike(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) {
2019-12-13 18:08:26 +00:00
c, err = s.getClient(ctx)
if err != nil {
return
}
return s.Service.UnLike(ctx, client, c, id)
}
2019-12-21 11:13:21 +00:00
func (s *authService) Retweet(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) {
2019-12-13 18:08:26 +00:00
c, err = s.getClient(ctx)
if err != nil {
return
}
return s.Service.Retweet(ctx, client, c, id)
}
2019-12-21 11:13:21 +00:00
func (s *authService) UnRetweet(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) {
2019-12-13 18:08:26 +00:00
c, err = s.getClient(ctx)
if err != nil {
return
}
return s.Service.UnRetweet(ctx, client, c, id)
}
2019-12-26 11:25:29 +00:00
func (s *authService) PostTweet(ctx context.Context, client io.Writer, c *model.Client, content string, replyToID string, format string, visibility string, isNSFW bool, files []*multipart.FileHeader) (id string, err error) {
2019-12-13 18:08:26 +00:00
c, err = s.getClient(ctx)
if err != nil {
return
}
2019-12-26 11:25:29 +00:00
return s.Service.PostTweet(ctx, client, c, content, replyToID, format, visibility, isNSFW, files)
2019-12-13 18:08:26 +00:00
}
2019-12-21 11:13:21 +00:00
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
}
return s.Service.Follow(ctx, client, c, id)
}
2019-12-21 11:13:21 +00:00
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
}
return s.Service.UnFollow(ctx, client, c, id)
}