aboutsummaryrefslogtreecommitdiff
path: root/service/auth.go
blob: 2f637175f9f464b2ea33b61025120f2f9eb7c99c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package service

import (
	"context"
	"errors"
	"io"
	"mime/multipart"

	"bloat/model"
	"mastodon"
)

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) {
	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)
	if err != nil {
		return
	}
	mc := mastodon.NewClient(&mastodon.Config{
		Server:       client.InstanceURL,
		ClientID:     client.ClientID,
		ClientSecret: client.ClientSecret,
		AccessToken:  session.AccessToken,
	})
	c = &model.Client{Client: mc, Session: session}
	return c, nil
}

func (s *authService) GetAuthUrl(ctx context.Context, instance string) (
	redirectUrl string, sessionID string, err error) {
	return s.Service.GetAuthUrl(ctx, instance)
}

func (s *authService) GetUserToken(ctx context.Context, sessionID string, c *model.Client,
	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)
	if err != nil {
		return
	}

	c.Session.AccessToken = token
	err = s.sessionRepo.Add(c.Session)
	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,
	c *model.Client, timelineType string, maxID string, sinceID string, minID string) (err error) {
	c, err = s.getClient(ctx)
	if err != nil {
		return
	}
	return s.Service.ServeTimelinePage(ctx, client, c, timelineType, maxID, sinceID, minID)
}

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
	}
	return s.Service.ServeThreadPage(ctx, client, c, id, reply)
}

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
	}
	return s.Service.ServeNotificationPage(ctx, client, c, maxID, minID)
}

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)
}

func (s *authService) ServeAboutPage(ctx context.Context, client io.Writer, c *model.Client) (err error) {
	c, err = s.getClient(ctx)
	if err != nil {
		return
	}
	return s.Service.ServeAboutPage(ctx, client, c)
}

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)
}

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)
}

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)
}

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)
}

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)
}

func (s *authService) Like(ctx context.Context, client io.Writer, c *model.Client, id string) (count int64, err error) {
	c, err = s.getClient(ctx)
	if err != nil {
		return
	}
	return s.Service.Like(ctx, client, c, id)
}

func (s *authService) UnLike(ctx context.Context, client io.Writer, c *model.Client, id string) (count int64, err error) {
	c, err = s.getClient(ctx)
	if err != nil {
		return
	}
	return s.Service.UnLike(ctx, client, c, id)
}

func (s *authService) Retweet(ctx context.Context, client io.Writer, c *model.Client, id string) (count int64, err error) {
	c, err = s.getClient(ctx)
	if err != nil {
		return
	}
	return s.Service.Retweet(ctx, client, c, id)
}

func (s *authService) UnRetweet(ctx context.Context, client io.Writer, c *model.Client, id string) (count int64, err error) {
	c, err = s.getClient(ctx)
	if err != nil {
		return
	}
	return s.Service.UnRetweet(ctx, client, c, id)
}

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) {
	c, err = s.getClient(ctx)
	if err != nil {
		return
	}
	return s.Service.PostTweet(ctx, client, c, content, replyToID, format, visibility, isNSFW, files)
}

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)
}

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)
}