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
|
/*
GoToSocial
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package typeutils
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/gorilla/feeds"
"github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/superseriousbusiness/gotosocial/internal/text"
)
const (
rssMaxTitleChars = 128
rssDescriptionMaxChars = 256
)
func (c *converter) StatusToRSSItem(ctx context.Context, s *gtsmodel.Status) (*feeds.Item, error) {
// see https://cyber.harvard.edu/rss/rss.html
// Title -- The title of the item.
// example: Venice Film Festival Tries to Quit Sinking
var title string
if s.ContentWarning != "" {
title = trimTo(s.ContentWarning, rssMaxTitleChars)
} else {
title = trimTo(s.Text, rssMaxTitleChars)
}
// Link -- The URL of the item.
// example: http://nytimes.com/2004/12/07FEST.html
link := &feeds.Link{
Href: s.URL,
}
// Author -- Email address of the author of the item.
// example: oprah\@oxygen.net
if s.Account == nil {
a, err := c.db.GetAccountByID(ctx, s.AccountID)
if err != nil {
return nil, fmt.Errorf("error getting status author: %s", err)
}
s.Account = a
}
authorName := "@" + s.Account.Username + "@" + config.GetAccountDomain()
author := &feeds.Author{
Name: authorName,
}
// Source -- The RSS channel that the item came from.
source := &feeds.Link{
Href: s.Account.URL + "/feed.rss",
}
// Description -- The item synopsis.
// example: Some of the most heated chatter at the Venice Film Festival this week was about the way that the arrival of the stars at the Palazzo del Cinema was being staged.
descriptionBuilder := strings.Builder{}
descriptionBuilder.WriteString(authorName + " ")
attachmentCount := len(s.Attachments)
if len(s.AttachmentIDs) > attachmentCount {
attachmentCount = len(s.AttachmentIDs)
}
switch {
case attachmentCount > 1:
descriptionBuilder.WriteString(fmt.Sprintf("posted [%d] attachments", attachmentCount))
case attachmentCount == 1:
descriptionBuilder.WriteString("posted 1 attachment")
default:
descriptionBuilder.WriteString("made a new post")
}
if s.Text != "" {
descriptionBuilder.WriteString(": \"")
descriptionBuilder.WriteString(s.Text)
descriptionBuilder.WriteString("\"")
}
description := trimTo(descriptionBuilder.String(), rssDescriptionMaxChars)
// ID -- A string that uniquely identifies the item.
// example: http://inessential.com/2002/09/01.php#a2
id := s.URL
// Enclosure -- Describes a media object that is attached to the item.
enclosure := &feeds.Enclosure{}
// get first attachment if present
var attachment *gtsmodel.MediaAttachment
if len(s.Attachments) > 0 {
attachment = s.Attachments[0]
} else if len(s.AttachmentIDs) > 0 {
a, err := c.db.GetAttachmentByID(ctx, s.AttachmentIDs[0])
if err == nil {
attachment = a
}
}
if attachment != nil {
enclosure.Type = attachment.File.ContentType
enclosure.Length = strconv.Itoa(attachment.File.FileSize)
enclosure.Url = attachment.URL
}
// Content
apiEmojis := []model.Emoji{}
// the status might already have some gts emojis on it if it's not been pulled directly from the database
// if so, we can directly convert the gts emojis into api ones
if s.Emojis != nil {
for _, gtsEmoji := range s.Emojis {
apiEmoji, err := c.EmojiToAPIEmoji(ctx, gtsEmoji)
if err != nil {
log.Errorf("error converting emoji with id %s: %s", gtsEmoji.ID, err)
continue
}
apiEmojis = append(apiEmojis, apiEmoji)
}
// the status doesn't have gts emojis on it, but it does have emoji IDs
// in this case, we need to pull the gts emojis from the db to convert them into api ones
} else {
for _, e := range s.EmojiIDs {
gtsEmoji := >smodel.Emoji{}
if err := c.db.GetByID(ctx, e, gtsEmoji); err != nil {
log.Errorf("error getting emoji with id %s: %s", e, err)
continue
}
apiEmoji, err := c.EmojiToAPIEmoji(ctx, gtsEmoji)
if err != nil {
log.Errorf("error converting emoji with id %s: %s", gtsEmoji.ID, err)
continue
}
apiEmojis = append(apiEmojis, apiEmoji)
}
}
content := text.Emojify(apiEmojis, s.Content)
return &feeds.Item{
Title: title,
Link: link,
Author: author,
Source: source,
Description: description,
Id: id,
Updated: s.UpdatedAt,
Created: s.CreatedAt,
Enclosure: enclosure,
Content: content,
}, nil
}
func trimTo(in string, to int) string {
if len(in) <= to {
return in
}
return in[:to-3] + "..."
}
|