bloat/service/service.go

936 lines
21 KiB
Go
Raw Normal View History

2019-12-13 18:08:26 +00:00
package service
import (
"context"
"errors"
2019-12-25 04:39:34 +00:00
"fmt"
2020-10-17 16:25:08 +00:00
"html/template"
2020-10-19 06:51:23 +00:00
"mime/multipart"
2019-12-13 18:08:26 +00:00
"net/url"
"strings"
2020-02-01 11:31:44 +00:00
"bloat/mastodon"
2020-01-01 15:58:27 +00:00
"bloat/model"
"bloat/renderer"
"bloat/util"
2019-12-13 18:08:26 +00:00
)
var (
2020-05-24 04:38:34 +00:00
ctx = context.Background()
2020-01-28 17:51:00 +00:00
errInvalidArgument = errors.New("invalid argument")
2019-12-13 18:08:26 +00:00
)
type service struct {
2020-04-19 08:18:36 +00:00
clientName string
clientScope string
clientWebsite string
customCSS string
postFormats []model.PostFormat
renderer renderer.Renderer
sessionRepo model.SessionRepo
appRepo model.AppRepo
singleInstance string
2019-12-13 18:08:26 +00:00
}
2020-01-28 17:51:00 +00:00
func NewService(clientName string,
clientScope string,
clientWebsite string,
customCSS string,
postFormats []model.PostFormat,
renderer renderer.Renderer,
sessionRepo model.SessionRepo,
appRepo model.AppRepo,
2020-04-19 08:18:36 +00:00
singleInstance string,
) *service {
2019-12-13 18:08:26 +00:00
return &service{
2020-04-19 08:18:36 +00:00
clientName: clientName,
clientScope: clientScope,
clientWebsite: clientWebsite,
customCSS: customCSS,
postFormats: postFormats,
renderer: renderer,
sessionRepo: sessionRepo,
appRepo: appRepo,
singleInstance: singleInstance,
2019-12-13 18:08:26 +00:00
}
}
func getRendererContext(c *client) *renderer.Context {
2020-01-25 10:07:06 +00:00
var settings model.Settings
var session model.Session
var referrer string
2020-01-25 10:07:06 +00:00
if c != nil {
settings = c.Session.Settings
session = c.Session
referrer = c.url()
2020-01-25 10:07:06 +00:00
} else {
settings = *model.NewSettings()
}
2020-01-14 16:57:16 +00:00
return &renderer.Context{
HideAttachments: settings.HideAttachments,
MaskNSFW: settings.MaskNSFW,
ThreadInNewTab: settings.ThreadInNewTab,
FluorideMode: settings.FluorideMode,
DarkMode: settings.DarkMode,
CSRFToken: session.CSRFToken,
UserID: session.UserID,
2020-09-02 17:50:48 +00:00
AntiDopamineMode: settings.AntiDopamineMode,
Referrer: referrer,
2020-01-14 16:57:16 +00:00
}
}
2020-01-28 17:51:00 +00:00
func addToReplyMap(m map[string][]mastodon.ReplyInfo, key interface{},
val string, number int) {
if key == nil {
2019-12-13 18:08:26 +00:00
return
}
2020-01-28 17:51:00 +00:00
keyStr, ok := key.(string)
if !ok {
2019-12-13 18:08:26 +00:00
return
}
2020-01-28 17:51:00 +00:00
_, ok = m[keyStr]
if !ok {
m[keyStr] = []mastodon.ReplyInfo{}
}
m[keyStr] = append(m[keyStr], mastodon.ReplyInfo{val, number})
2019-12-13 18:08:26 +00:00
}
func (s *service) getCommonData(c *client, title string) (data *renderer.CommonData) {
2020-02-18 22:15:37 +00:00
data = &renderer.CommonData{
Title: title + " - " + s.clientName,
CustomCSS: s.customCSS,
2019-12-13 18:08:26 +00:00
}
2020-02-18 22:15:37 +00:00
if c != nil && c.Session.IsLoggedIn() {
data.CSRFToken = c.Session.CSRFToken
2019-12-13 18:08:26 +00:00
}
2020-01-28 17:51:00 +00:00
return
2019-12-13 18:08:26 +00:00
}
func (s *service) ErrorPage(c *client, err error) {
2019-12-26 09:11:24 +00:00
var errStr string
if err != nil {
errStr = err.Error()
}
commonData := s.getCommonData(nil, "error")
2019-12-26 09:11:24 +00:00
data := &renderer.ErrorData{
CommonData: commonData,
Error: errStr,
}
2020-01-25 10:07:06 +00:00
rCtx := getRendererContext(c)
s.renderer.Render(rCtx, c, renderer.ErrorPage, data)
2019-12-13 18:08:26 +00:00
}
func (s *service) SigninPage(c *client) (err error) {
commonData := s.getCommonData(nil, "signin")
2019-12-26 09:11:24 +00:00
data := &renderer.SigninData{
CommonData: commonData,
}
2020-01-25 10:07:06 +00:00
rCtx := getRendererContext(nil)
return s.renderer.Render(rCtx, c, renderer.SigninPage, data)
2019-12-13 18:08:26 +00:00
}
func (s *service) RootPage(c *client) (err error) {
2020-02-18 22:15:37 +00:00
data := &renderer.RootData{
Title: s.clientName,
2020-02-18 22:15:37 +00:00
}
rCtx := getRendererContext(c)
return s.renderer.Render(rCtx, c, renderer.RootPage, data)
2020-02-18 22:15:37 +00:00
}
func (s *service) NavPage(c *client) (err error) {
2020-02-18 22:15:37 +00:00
u, err := c.GetAccountCurrentUser(ctx)
if err != nil {
return
}
postContext := model.PostContext{
DefaultVisibility: c.Session.Settings.DefaultVisibility,
2020-10-19 06:51:23 +00:00
DefaultFormat: c.Session.Settings.DefaultFormat,
Formats: s.postFormats,
2020-02-18 22:15:37 +00:00
}
commonData := s.getCommonData(c, "nav")
2020-02-23 13:26:39 +00:00
commonData.Target = "main"
2020-02-18 22:15:37 +00:00
data := &renderer.NavData{
User: u,
CommonData: commonData,
PostContext: postContext,
}
rCtx := getRendererContext(c)
return s.renderer.Render(rCtx, c, renderer.NavPage, data)
2020-02-18 22:15:37 +00:00
}
2021-01-23 08:44:05 +00:00
func (s *service) TimelinePage(c *client, tType string, instance string,
2020-05-24 04:38:34 +00:00
maxID string, minID string) (err error) {
2019-12-13 18:08:26 +00:00
2020-01-28 17:51:00 +00:00
var nextLink, prevLink, title string
var statuses []*mastodon.Status
2019-12-13 18:08:26 +00:00
var pg = mastodon.Pagination{
2019-12-15 06:55:13 +00:00
MaxID: maxID,
MinID: minID,
Limit: 20,
2019-12-13 18:08:26 +00:00
}
2020-01-28 17:51:00 +00:00
switch tType {
2019-12-25 04:30:21 +00:00
default:
2020-01-28 17:51:00 +00:00
return errInvalidArgument
2019-12-25 04:30:21 +00:00
case "home":
statuses, err = c.GetTimelineHome(ctx, &pg)
title = "Timeline"
2020-01-28 20:56:15 +00:00
case "direct":
statuses, err = c.GetTimelineDirect(ctx, &pg)
2020-07-28 13:36:57 +00:00
title = "Direct Timeline"
2019-12-25 04:30:21 +00:00
case "local":
2021-01-23 08:44:05 +00:00
statuses, err = c.GetTimelinePublic(ctx, true, "", &pg)
2019-12-25 04:30:21 +00:00
title = "Local Timeline"
2021-01-23 08:44:05 +00:00
case "remote":
if len(instance) > 0 {
statuses, err = c.GetTimelinePublic(ctx, false, instance, &pg)
}
title = "Remote Timeline"
2019-12-25 04:30:21 +00:00
case "twkn":
2021-01-23 08:44:05 +00:00
statuses, err = c.GetTimelinePublic(ctx, false, "", &pg)
2019-12-25 04:30:21 +00:00
title = "The Whole Known Network"
}
2019-12-13 18:08:26 +00:00
if err != nil {
return err
}
2019-12-29 11:32:24 +00:00
for i := range statuses {
2019-12-31 11:00:21 +00:00
if statuses[i].Reblog != nil {
statuses[i].Reblog.RetweetedByID = statuses[i].ID
2019-12-31 11:00:21 +00:00
}
2019-12-29 11:32:24 +00:00
}
2021-01-23 07:02:12 +00:00
if (len(maxID) > 0 || len(minID) > 0) && len(statuses) > 0 {
2021-01-23 08:44:05 +00:00
v := make(url.Values)
v.Set("min_id", statuses[0].ID)
if len(instance) > 0 {
v.Set("instance", instance)
}
prevLink = "/timeline/" + tType + "?" + v.Encode()
2019-12-15 06:55:13 +00:00
}
2020-01-28 17:51:00 +00:00
2021-01-23 07:02:12 +00:00
if len(minID) > 0 || (len(pg.MaxID) > 0 && len(statuses) == 20) {
2021-01-23 08:44:05 +00:00
v := make(url.Values)
v.Set("max_id", pg.MaxID)
if len(instance) > 0 {
v.Set("instance", instance)
}
nextLink = "/timeline/" + tType + "?" + v.Encode()
2019-12-13 18:08:26 +00:00
}
commonData := s.getCommonData(c, tType+" timeline ")
2019-12-25 14:13:59 +00:00
data := &renderer.TimelineData{
2020-02-18 22:15:37 +00:00
Title: title,
2021-01-23 08:44:05 +00:00
Type: tType,
Instance: instance,
2020-02-18 22:15:37 +00:00
Statuses: statuses,
NextLink: nextLink,
PrevLink: prevLink,
CommonData: commonData,
2019-12-25 14:13:59 +00:00
}
2020-01-28 17:51:00 +00:00
2020-01-25 10:07:06 +00:00
rCtx := getRendererContext(c)
return s.renderer.Render(rCtx, c, renderer.TimelinePage, data)
2020-01-28 17:51:00 +00:00
}
2019-12-25 14:13:59 +00:00
func (s *service) ThreadPage(c *client, id string, reply bool) (err error) {
2020-01-28 17:51:00 +00:00
var postContext model.PostContext
2019-12-13 18:08:26 +00:00
status, err := c.GetStatus(ctx, id)
if err != nil {
return
}
if reply {
var content string
2020-01-28 17:51:00 +00:00
var visibility string
if c.Session.UserID != status.Account.ID {
2019-12-14 17:47:14 +00:00
content += "@" + status.Account.Acct + " "
}
2019-12-18 22:14:02 +00:00
for i := range status.Mentions {
if status.Mentions[i].ID != c.Session.UserID &&
2020-01-28 17:51:00 +00:00
status.Mentions[i].ID != status.Account.ID {
2019-12-18 22:14:02 +00:00
content += "@" + status.Mentions[i].Acct + " "
2019-12-14 17:47:14 +00:00
}
}
2020-07-28 13:36:57 +00:00
isDirect := status.Visibility == "direct"
if isDirect || c.Session.Settings.CopyScope {
visibility = status.Visibility
2019-12-27 08:06:43 +00:00
} else {
visibility = c.Session.Settings.DefaultVisibility
}
postContext = model.PostContext{
2019-12-27 08:06:43 +00:00
DefaultVisibility: visibility,
2020-10-19 06:51:23 +00:00
DefaultFormat: c.Session.Settings.DefaultFormat,
Formats: s.postFormats,
ReplyContext: &model.ReplyContext{
InReplyToID: id,
InReplyToName: status.Account.Acct,
ReplyContent: content,
ForceVisibility: isDirect,
},
}
}
2019-12-18 22:14:02 +00:00
context, err := c.GetStatusContext(ctx, id)
if err != nil {
return
}
statuses := append(append(context.Ancestors, status), context.Descendants...)
2020-01-28 17:51:00 +00:00
replies := make(map[string][]mastodon.ReplyInfo)
2020-06-05 06:27:59 +00:00
idNumbers := make(map[string]int)
2019-12-18 22:14:02 +00:00
for i := range statuses {
statuses[i].ShowReplies = true
2020-06-05 06:27:59 +00:00
statuses[i].IDNumbers = idNumbers
idNumbers[statuses[i].ID] = i + 1
statuses[i].IDReplies = replies
addToReplyMap(replies, statuses[i].InReplyToID, statuses[i].ID, i+1)
2019-12-18 22:14:02 +00:00
}
commonData := s.getCommonData(c, "post by "+status.Account.DisplayName)
2019-12-25 14:13:59 +00:00
data := &renderer.ThreadData{
Statuses: statuses,
PostContext: postContext,
2020-01-28 17:51:00 +00:00
ReplyMap: replies,
CommonData: commonData,
2019-12-25 14:13:59 +00:00
}
2020-01-28 17:51:00 +00:00
rCtx := getRendererContext(c)
return s.renderer.Render(rCtx, c, renderer.ThreadPage, data)
2019-12-13 18:08:26 +00:00
}
func (s *service) LikedByPage(c *client, id string) (err error) {
2020-01-28 17:51:00 +00:00
likers, err := c.GetFavouritedBy(ctx, id, nil)
if err != nil {
return
2019-12-15 17:37:58 +00:00
}
commonData := s.getCommonData(c, "likes")
2020-01-28 17:51:00 +00:00
data := &renderer.LikedByData{
CommonData: commonData,
Users: likers,
2019-12-15 17:37:58 +00:00
}
2020-01-28 17:51:00 +00:00
rCtx := getRendererContext(c)
return s.renderer.Render(rCtx, c, renderer.LikedByPage, data)
2020-01-28 17:51:00 +00:00
}
2019-12-15 17:37:58 +00:00
func (s *service) RetweetedByPage(c *client, id string) (err error) {
2020-01-28 17:51:00 +00:00
retweeters, err := c.GetRebloggedBy(ctx, id, nil)
2019-12-15 17:37:58 +00:00
if err != nil {
return
}
commonData := s.getCommonData(c, "retweets")
2020-01-28 17:51:00 +00:00
data := &renderer.RetweetedByData{
CommonData: commonData,
Users: retweeters,
}
rCtx := getRendererContext(c)
return s.renderer.Render(rCtx, c, renderer.RetweetedByPage, data)
2019-12-15 17:37:58 +00:00
}
func (s *service) NotificationPage(c *client, maxID string,
2020-05-24 04:38:34 +00:00
minID string) (err error) {
2020-01-28 17:51:00 +00:00
var nextLink string
var unreadCount int
2020-02-18 22:15:37 +00:00
var readID string
2020-09-02 17:50:48 +00:00
var excludes []string
2020-01-28 17:51:00 +00:00
var pg = mastodon.Pagination{
MaxID: maxID,
MinID: minID,
Limit: 20,
2019-12-26 09:11:24 +00:00
}
if c.Session.Settings.AntiDopamineMode {
excludes = []string{"follow", "favourite", "reblog"}
2020-09-02 17:50:48 +00:00
}
notifications, err := c.GetNotifications(ctx, &pg, excludes)
2019-12-26 09:11:24 +00:00
if err != nil {
return
}
2020-01-28 17:51:00 +00:00
for i := range notifications {
if notifications[i].Pleroma != nil && !notifications[i].Pleroma.IsSeen {
unreadCount++
}
2019-12-26 09:11:24 +00:00
}
2020-01-28 17:51:00 +00:00
if unreadCount > 0 {
2020-02-18 22:15:37 +00:00
readID = notifications[0].ID
2019-12-26 09:11:24 +00:00
}
2020-02-18 22:15:37 +00:00
if len(notifications) == 20 && len(pg.MaxID) > 0 {
2020-01-28 17:51:00 +00:00
nextLink = "/notifications?max_id=" + pg.MaxID
2019-12-26 09:11:24 +00:00
}
commonData := s.getCommonData(c, "notifications")
commonData.RefreshInterval = c.Session.Settings.NotificationInterval
2020-02-23 13:26:39 +00:00
commonData.Target = "main"
2020-02-26 10:27:17 +00:00
commonData.Count = unreadCount
2020-01-28 17:51:00 +00:00
data := &renderer.NotificationData{
Notifications: notifications,
2020-02-18 22:15:37 +00:00
UnreadCount: unreadCount,
ReadID: readID,
2020-01-28 17:51:00 +00:00
NextLink: nextLink,
CommonData: commonData,
2019-12-26 09:11:24 +00:00
}
2020-01-25 10:07:06 +00:00
rCtx := getRendererContext(c)
return s.renderer.Render(rCtx, c, renderer.NotificationPage, data)
2019-12-26 09:11:24 +00:00
}
2019-12-26 19:18:09 +00:00
func (s *service) UserPage(c *client, id string, pageType string,
2020-05-24 04:38:34 +00:00
maxID string, minID string) (err error) {
2020-01-28 17:51:00 +00:00
2019-12-29 03:43:57 +00:00
var nextLink string
var statuses []*mastodon.Status
var users []*mastodon.Account
2019-12-29 03:43:57 +00:00
var pg = mastodon.Pagination{
MaxID: maxID,
MinID: minID,
Limit: 20,
}
2020-01-28 17:51:00 +00:00
user, err := c.GetAccount(ctx, id)
2019-12-29 03:43:57 +00:00
if err != nil {
return
}
isCurrent := c.Session.UserID == user.ID
2019-12-29 03:43:57 +00:00
switch pageType {
case "":
statuses, err = c.GetAccountStatuses(ctx, id, false, &pg)
if err != nil {
return
}
if len(statuses) == 20 && len(pg.MaxID) > 0 {
nextLink = fmt.Sprintf("/user/%s?max_id=%s", id,
pg.MaxID)
}
case "following":
users, err = c.GetAccountFollowing(ctx, id, &pg)
if err != nil {
return
}
if len(users) == 20 && len(pg.MaxID) > 0 {
nextLink = fmt.Sprintf("/user/%s/following?max_id=%s",
id, pg.MaxID)
}
case "followers":
users, err = c.GetAccountFollowers(ctx, id, &pg)
if err != nil {
return
}
if len(users) == 20 && len(pg.MaxID) > 0 {
nextLink = fmt.Sprintf("/user/%s/followers?max_id=%s",
id, pg.MaxID)
}
case "media":
statuses, err = c.GetAccountStatuses(ctx, id, true, &pg)
if err != nil {
return
}
if len(statuses) == 20 && len(pg.MaxID) > 0 {
nextLink = fmt.Sprintf("/user/%s/media?max_id=%s",
id, pg.MaxID)
}
case "bookmarks":
if !isCurrent {
return errInvalidArgument
}
statuses, err = c.GetBookmarks(ctx, &pg)
if err != nil {
return
}
if len(statuses) == 20 && len(pg.MaxID) > 0 {
nextLink = fmt.Sprintf("/user/%s/bookmarks?max_id=%s",
id, pg.MaxID)
}
2020-09-27 09:44:52 +00:00
case "mutes":
if !isCurrent {
return errInvalidArgument
}
users, err = c.GetMutes(ctx, &pg)
if err != nil {
return
}
if len(users) == 20 && len(pg.MaxID) > 0 {
nextLink = fmt.Sprintf("/user/%s/mutes?max_id=%s",
id, pg.MaxID)
}
2020-09-27 09:50:04 +00:00
case "blocks":
if !isCurrent {
return errInvalidArgument
}
users, err = c.GetBlocks(ctx, &pg)
if err != nil {
return
}
if len(users) == 20 && len(pg.MaxID) > 0 {
nextLink = fmt.Sprintf("/user/%s/blocks?max_id=%s",
id, pg.MaxID)
}
2020-09-27 10:04:56 +00:00
case "likes":
if !isCurrent {
return errInvalidArgument
}
statuses, err = c.GetFavourites(ctx, &pg)
if err != nil {
return
}
if len(statuses) == 20 && len(pg.MaxID) > 0 {
nextLink = fmt.Sprintf("/user/%s/likes?max_id=%s",
id, pg.MaxID)
}
2021-01-16 09:10:02 +00:00
case "requests":
if !isCurrent {
return errInvalidArgument
}
users, err = c.GetFollowRequests(ctx, &pg)
if err != nil {
return
}
if len(users) == 20 && len(pg.MaxID) > 0 {
nextLink = fmt.Sprintf("/user/%s/requests?max_id=%s",
id, pg.MaxID)
}
default:
return errInvalidArgument
2019-12-29 03:43:57 +00:00
}
2020-10-19 10:01:59 +00:00
for i := range statuses {
if statuses[i].Reblog != nil {
statuses[i].Reblog.RetweetedByID = statuses[i].ID
}
}
commonData := s.getCommonData(c, user.DisplayName+" @"+user.Acct)
2020-01-28 17:51:00 +00:00
data := &renderer.UserData{
User: user,
IsCurrent: isCurrent,
Type: pageType,
Users: users,
2020-01-28 17:51:00 +00:00
Statuses: statuses,
2019-12-29 03:43:57 +00:00
NextLink: nextLink,
2020-01-28 17:51:00 +00:00
CommonData: commonData,
2019-12-29 03:43:57 +00:00
}
2020-01-25 10:07:06 +00:00
rCtx := getRendererContext(c)
return s.renderer.Render(rCtx, c, renderer.UserPage, data)
2020-01-28 17:51:00 +00:00
}
2019-12-29 03:43:57 +00:00
func (s *service) UserSearchPage(c *client,
2020-01-30 15:32:37 +00:00
id string, q string, offset int) (err error) {
var nextLink string
var title = "search"
user, err := c.GetAccount(ctx, id)
if err != nil {
return
}
2020-10-19 06:05:59 +00:00
var results *mastodon.Results
if len(q) > 0 {
results, err = c.Search(ctx, q, "statuses", 20, true, offset, id)
if err != nil {
return err
}
} else {
results = &mastodon.Results{}
2020-01-30 15:32:37 +00:00
}
if len(results.Statuses) == 20 {
offset += 20
nextLink = fmt.Sprintf("/usersearch/%s?q=%s&offset=%d", id,
url.QueryEscape(q), offset)
2020-01-30 15:32:37 +00:00
}
2020-10-17 16:25:08 +00:00
qq := template.HTMLEscapeString(q)
2020-01-30 15:32:37 +00:00
if len(q) > 0 {
2020-10-17 16:25:08 +00:00
title += " \"" + qq + "\""
2020-01-30 15:32:37 +00:00
}
commonData := s.getCommonData(c, title)
2020-01-30 15:32:37 +00:00
data := &renderer.UserSearchData{
CommonData: commonData,
User: user,
2020-10-17 16:25:08 +00:00
Q: qq,
2020-01-30 15:32:37 +00:00
Statuses: results.Statuses,
NextLink: nextLink,
}
rCtx := getRendererContext(c)
return s.renderer.Render(rCtx, c, renderer.UserSearchPage, data)
2020-01-30 15:32:37 +00:00
}
func (s *service) AboutPage(c *client) (err error) {
commonData := s.getCommonData(c, "about")
2020-01-28 17:51:00 +00:00
data := &renderer.AboutData{
CommonData: commonData,
2019-12-29 03:43:57 +00:00
}
2020-01-28 17:51:00 +00:00
rCtx := getRendererContext(c)
return s.renderer.Render(rCtx, c, renderer.AboutPage, data)
2020-01-28 17:51:00 +00:00
}
func (s *service) EmojiPage(c *client) (err error) {
2020-01-28 17:51:00 +00:00
emojis, err := c.GetInstanceEmojis(ctx)
2019-12-29 03:43:57 +00:00
if err != nil {
return
}
commonData := s.getCommonData(c, "emojis")
2020-01-28 17:51:00 +00:00
data := &renderer.EmojiData{
Emojis: emojis,
2019-12-29 03:43:57 +00:00
CommonData: commonData,
}
2020-01-28 17:51:00 +00:00
rCtx := getRendererContext(c)
return s.renderer.Render(rCtx, c, renderer.EmojiPage, data)
2019-12-29 03:43:57 +00:00
}
func (s *service) SearchPage(c *client,
2020-01-28 17:51:00 +00:00
q string, qType string, offset int) (err error) {
2019-12-26 19:18:09 +00:00
var nextLink string
2020-01-28 17:51:00 +00:00
var title = "search"
2019-12-26 19:18:09 +00:00
2020-10-19 06:05:59 +00:00
var results *mastodon.Results
if len(q) > 0 {
results, err = c.Search(ctx, q, qType, 20, true, offset, "")
if err != nil {
return err
}
} else {
results = &mastodon.Results{}
2019-12-26 19:18:09 +00:00
}
2020-01-28 17:51:00 +00:00
if (qType == "accounts" && len(results.Accounts) == 20) ||
(qType == "statuses" && len(results.Statuses) == 20) {
2019-12-26 19:18:09 +00:00
offset += 20
nextLink = fmt.Sprintf("/search?q=%s&type=%s&offset=%d",
url.QueryEscape(q), qType, offset)
2019-12-26 19:18:09 +00:00
}
2020-10-17 16:25:08 +00:00
qq := template.HTMLEscapeString(q)
2020-01-10 03:53:02 +00:00
if len(q) > 0 {
2020-10-17 16:25:08 +00:00
title += " \"" + qq + "\""
2020-01-10 03:53:02 +00:00
}
2020-01-28 17:51:00 +00:00
commonData := s.getCommonData(c, title)
2019-12-26 19:18:09 +00:00
data := &renderer.SearchData{
CommonData: commonData,
2020-10-17 16:25:08 +00:00
Q: qq,
2019-12-26 19:18:09 +00:00
Type: qType,
Users: results.Accounts,
Statuses: results.Statuses,
NextLink: nextLink,
}
2020-01-28 17:51:00 +00:00
rCtx := getRendererContext(c)
return s.renderer.Render(rCtx, c, renderer.SearchPage, data)
2019-12-26 19:18:09 +00:00
}
func (s *service) SettingsPage(c *client) (err error) {
commonData := s.getCommonData(c, "settings")
2019-12-27 08:06:43 +00:00
data := &renderer.SettingsData{
2020-10-19 06:51:23 +00:00
CommonData: commonData,
Settings: &c.Session.Settings,
PostFormats: s.postFormats,
2019-12-27 08:06:43 +00:00
}
2020-01-25 10:07:06 +00:00
rCtx := getRendererContext(c)
return s.renderer.Render(rCtx, c, renderer.SettingsPage, data)
2020-01-28 17:51:00 +00:00
}
2019-12-27 08:06:43 +00:00
2021-01-30 16:51:09 +00:00
func (svc *service) FiltersPage(c *client) (err error) {
filters, err := c.GetFilters(ctx)
if err != nil {
return
}
commonData := svc.getCommonData(c, "filters")
data := &renderer.FiltersData{
CommonData: commonData,
Filters: filters,
}
rCtx := getRendererContext(c)
return svc.renderer.Render(rCtx, c, renderer.FiltersPage, data)
}
func (s *service) SingleInstance() (instance string, ok bool) {
if len(s.singleInstance) > 0 {
instance = s.singleInstance
2020-04-19 08:18:36 +00:00
ok = true
}
return
}
func (s *service) NewSession(instance string) (rurl string, sid string, err error) {
2020-01-28 17:51:00 +00:00
var instanceURL string
if strings.HasPrefix(instance, "https://") {
instanceURL = instance
instance = strings.TrimPrefix(instance, "https://")
} else {
instanceURL = "https://" + instance
}
sid, err = util.NewSessionID()
2019-12-27 08:06:43 +00:00
if err != nil {
return
}
2020-01-28 17:51:00 +00:00
csrfToken, err := util.NewCSRFToken()
2019-12-27 08:06:43 +00:00
if err != nil {
return
}
2020-01-28 17:51:00 +00:00
session := model.Session{
ID: sid,
2020-01-28 17:51:00 +00:00
InstanceDomain: instance,
CSRFToken: csrfToken,
Settings: *model.NewSettings(),
}
err = s.sessionRepo.Add(session)
2019-12-27 08:06:43 +00:00
if err != nil {
return
}
app, err := s.appRepo.Get(instance)
2020-01-28 17:51:00 +00:00
if err != nil {
if err != model.ErrAppNotFound {
return
}
mastoApp, err := mastodon.RegisterApp(ctx, &mastodon.AppConfig{
Server: instanceURL,
ClientName: s.clientName,
Scopes: s.clientScope,
Website: s.clientWebsite,
RedirectURIs: s.clientWebsite + "/oauth_callback",
2020-01-28 17:51:00 +00:00
})
if err != nil {
2020-01-28 17:51:00 +00:00
return "", "", err
2019-12-15 17:37:58 +00:00
}
2020-01-28 17:51:00 +00:00
app = model.App{
InstanceDomain: instance,
InstanceURL: instanceURL,
ClientID: mastoApp.ClientID,
ClientSecret: mastoApp.ClientSecret,
}
err = s.appRepo.Add(app)
if err != nil {
2020-01-28 17:51:00 +00:00
return "", "", err
}
2019-12-25 14:13:59 +00:00
}
2019-12-15 17:37:58 +00:00
2020-01-28 17:51:00 +00:00
u, err := url.Parse("/oauth/authorize")
2020-01-08 18:16:06 +00:00
if err != nil {
return
}
2019-12-13 18:08:26 +00:00
2020-01-28 17:51:00 +00:00
q := make(url.Values)
q.Set("scope", "read write follow")
q.Set("client_id", app.ClientID)
q.Set("response_type", "code")
q.Set("redirect_uri", s.clientWebsite+"/oauth_callback")
2020-01-28 17:51:00 +00:00
u.RawQuery = q.Encode()
rurl = instanceURL + u.String()
2019-12-13 18:08:26 +00:00
return
}
func (s *service) Signin(c *client, code string) (token string,
userID string, err error) {
2020-01-28 17:51:00 +00:00
if len(code) < 1 {
err = errInvalidArgument
2020-01-08 18:16:06 +00:00
return
}
err = c.AuthenticateToken(ctx, code, s.clientWebsite+"/oauth_callback")
2020-01-08 18:16:06 +00:00
if err != nil {
return
}
2020-01-28 17:51:00 +00:00
token = c.GetAccessToken(ctx)
2020-02-02 08:30:40 +00:00
u, err := c.GetAccountCurrentUser(ctx)
if err != nil {
return
}
userID = u.ID
2019-12-13 18:08:26 +00:00
return
}
func (s *service) Signout(c *client) (err error) {
s.sessionRepo.Remove(c.Session.ID)
2020-03-04 15:59:59 +00:00
return
}
func (s *service) Post(c *client, content string, replyToID string,
format string, visibility string, isNSFW bool,
2020-01-28 17:51:00 +00:00
files []*multipart.FileHeader) (id string, err error) {
var mediaIDs []string
2019-12-14 20:19:02 +00:00
for _, f := range files {
a, err := c.UploadMediaFromMultipartFileHeader(ctx, f)
if err != nil {
return "", err
}
2020-01-28 17:51:00 +00:00
mediaIDs = append(mediaIDs, a.ID)
2019-12-14 20:19:02 +00:00
}
2019-12-13 18:08:26 +00:00
tweet := &mastodon.Toot{
Status: content,
InReplyToID: replyToID,
2020-01-28 17:51:00 +00:00
MediaIDs: mediaIDs,
2019-12-26 11:25:29 +00:00
ContentType: format,
Visibility: visibility,
2019-12-22 16:27:49 +00:00
Sensitive: isNSFW,
2019-12-13 18:08:26 +00:00
}
st, err := c.PostStatus(ctx, tweet)
if err != nil {
return
}
return st.ID, nil
2019-12-13 18:08:26 +00:00
}
2019-12-18 22:14:02 +00:00
func (s *service) Like(c *client, id string) (count int64, err error) {
st, err := c.Favourite(ctx, id)
2020-01-28 17:51:00 +00:00
if err != nil {
return
}
count = st.FavouritesCount
return
}
func (s *service) UnLike(c *client, id string) (count int64, err error) {
st, err := c.Unfavourite(ctx, id)
2020-01-28 17:51:00 +00:00
if err != nil {
return
}
count = st.FavouritesCount
return
}
func (s *service) Retweet(c *client, id string) (count int64, err error) {
st, err := c.Reblog(ctx, id)
2020-01-28 17:51:00 +00:00
if err != nil {
2019-12-18 22:14:02 +00:00
return
}
if st.Reblog != nil {
count = st.Reblog.ReblogsCount
2020-01-28 17:51:00 +00:00
}
return
}
2019-12-21 11:13:21 +00:00
func (s *service) UnRetweet(c *client, id string) (
2020-01-28 17:51:00 +00:00
count int64, err error) {
st, err := c.Unreblog(ctx, id)
2020-01-28 17:51:00 +00:00
if err != nil {
2019-12-18 22:14:02 +00:00
return
}
count = st.ReblogsCount
2020-01-28 17:51:00 +00:00
return
}
func (s *service) Vote(c *client, id string, choices []string) (err error) {
_, err = c.Vote(ctx, id, choices)
return
}
func (s *service) Follow(c *client, id string, reblogs *bool) (err error) {
2020-04-19 05:57:40 +00:00
_, err = c.AccountFollow(ctx, id, reblogs)
2020-01-28 17:51:00 +00:00
return
}
func (s *service) UnFollow(c *client, id string) (err error) {
2020-01-28 17:51:00 +00:00
_, err = c.AccountUnfollow(ctx, id)
return
}
2021-01-16 09:10:02 +00:00
func (s *service) Accept(c *client, id string) (err error) {
return c.FollowRequestAuthorize(ctx, id)
}
func (s *service) Reject(c *client, id string) (err error) {
return c.FollowRequestReject(ctx, id)
}
func (s *service) Mute(c *client, id string) (err error) {
2020-02-08 10:49:06 +00:00
_, err = c.AccountMute(ctx, id)
return
}
func (s *service) UnMute(c *client, id string) (err error) {
2020-02-08 10:49:06 +00:00
_, err = c.AccountUnmute(ctx, id)
return
}
func (s *service) Block(c *client, id string) (err error) {
2020-02-08 10:49:06 +00:00
_, err = c.AccountBlock(ctx, id)
return
}
func (s *service) UnBlock(c *client, id string) (err error) {
2020-02-08 10:49:06 +00:00
_, err = c.AccountUnblock(ctx, id)
return
}
func (s *service) Subscribe(c *client, id string) (err error) {
2020-04-17 17:19:11 +00:00
_, err = c.Subscribe(ctx, id)
return
}
func (s *service) UnSubscribe(c *client, id string) (err error) {
2020-04-17 17:19:11 +00:00
_, err = c.UnSubscribe(ctx, id)
return
}
func (s *service) SaveSettings(c *client, settings *model.Settings) (err error) {
switch settings.NotificationInterval {
case 0, 30, 60, 120, 300, 600:
default:
return errInvalidArgument
}
session, err := s.sessionRepo.Get(c.Session.ID)
2020-01-28 17:51:00 +00:00
if err != nil {
return
2019-12-18 22:14:02 +00:00
}
session.Settings = *settings
return s.sessionRepo.Add(session)
2019-12-18 22:14:02 +00:00
}
2020-02-02 07:24:06 +00:00
func (s *service) MuteConversation(c *client, id string) (err error) {
2020-02-02 07:24:06 +00:00
_, err = c.MuteConversation(ctx, id)
return
}
func (s *service) UnMuteConversation(c *client, id string) (err error) {
2020-02-02 07:24:06 +00:00
_, err = c.UnmuteConversation(ctx, id)
return
}
2020-02-02 08:30:40 +00:00
func (s *service) Delete(c *client, id string) (err error) {
2020-02-02 08:30:40 +00:00
return c.DeleteStatus(ctx, id)
}
2020-02-18 22:15:37 +00:00
func (s *service) ReadNotifications(c *client, maxID string) (err error) {
2020-02-18 22:15:37 +00:00
return c.ReadNotifications(ctx, maxID)
}
func (s *service) Bookmark(c *client, id string) (err error) {
_, err = c.Bookmark(ctx, id)
return
}
func (s *service) UnBookmark(c *client, id string) (err error) {
_, err = c.Unbookmark(ctx, id)
return
}
2021-01-30 16:51:09 +00:00
func (svc *service) Filter(c *client, phrase string, wholeWord bool) (err error) {
fctx := []string{"home", "notifications", "public", "thread"}
return c.AddFilter(ctx, phrase, fctx, true, wholeWord, nil)
}
func (svc *service) UnFilter(c *client, id string) (err error) {
return c.RemoveFilter(ctx, id)
}