rss.go (3778B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package account 19 20 import ( 21 "context" 22 "errors" 23 "fmt" 24 "time" 25 26 "github.com/gorilla/feeds" 27 "github.com/superseriousbusiness/gotosocial/internal/config" 28 "github.com/superseriousbusiness/gotosocial/internal/db" 29 "github.com/superseriousbusiness/gotosocial/internal/gtserror" 30 ) 31 32 const rssFeedLength = 20 33 34 // GetRSSFeedForUsername returns RSS feed for the given local username. 35 func (p *Processor) GetRSSFeedForUsername(ctx context.Context, username string) (func() (string, gtserror.WithCode), time.Time, gtserror.WithCode) { 36 account, err := p.state.DB.GetAccountByUsernameDomain(ctx, username, "") 37 if err != nil { 38 if err == db.ErrNoEntries { 39 return nil, time.Time{}, gtserror.NewErrorNotFound(errors.New("GetRSSFeedForUsername: account not found")) 40 } 41 return nil, time.Time{}, gtserror.NewErrorInternalError(fmt.Errorf("GetRSSFeedForUsername: db error: %s", err)) 42 } 43 44 if !*account.EnableRSS { 45 return nil, time.Time{}, gtserror.NewErrorNotFound(errors.New("GetRSSFeedForUsername: account RSS feed not enabled")) 46 } 47 48 lastModified, err := p.state.DB.GetAccountLastPosted(ctx, account.ID, true) 49 if err != nil { 50 return nil, time.Time{}, gtserror.NewErrorInternalError(fmt.Errorf("GetRSSFeedForUsername: db error: %s", err)) 51 } 52 53 return func() (string, gtserror.WithCode) { 54 statuses, err := p.state.DB.GetAccountWebStatuses(ctx, account.ID, rssFeedLength, "") 55 if err != nil && err != db.ErrNoEntries { 56 return "", gtserror.NewErrorInternalError(fmt.Errorf("GetRSSFeedForUsername: db error: %s", err)) 57 } 58 59 author := "@" + account.Username + "@" + config.GetAccountDomain() 60 title := "Posts from " + author 61 description := "Posts from " + author 62 link := &feeds.Link{Href: account.URL} 63 64 var image *feeds.Image 65 if account.AvatarMediaAttachmentID != "" { 66 if account.AvatarMediaAttachment == nil { 67 avatar, err := p.state.DB.GetAttachmentByID(ctx, account.AvatarMediaAttachmentID) 68 if err != nil { 69 return "", gtserror.NewErrorInternalError(fmt.Errorf("GetRSSFeedForUsername: db error fetching avatar attachment: %s", err)) 70 } 71 account.AvatarMediaAttachment = avatar 72 } 73 image = &feeds.Image{ 74 Url: account.AvatarMediaAttachment.Thumbnail.URL, 75 Title: "Avatar for " + author, 76 Link: account.URL, 77 } 78 } 79 80 feed := &feeds.Feed{ 81 Title: title, 82 Description: description, 83 Link: link, 84 Image: image, 85 } 86 87 for i, s := range statuses { 88 // take the date of the first (ie., latest) status as feed updated value 89 if i == 0 { 90 feed.Updated = s.UpdatedAt 91 } 92 93 item, err := p.tc.StatusToRSSItem(ctx, s) 94 if err != nil { 95 return "", gtserror.NewErrorInternalError(fmt.Errorf("GetRSSFeedForUsername: error converting status to feed item: %s", err)) 96 } 97 98 feed.Add(item) 99 } 100 101 rss, err := feed.ToRss() 102 if err != nil { 103 return "", gtserror.NewErrorInternalError(fmt.Errorf("GetRSSFeedForUsername: error converting feed to rss string: %s", err)) 104 } 105 106 return rss, nil 107 }, lastModified, nil 108 }