Skip to content

Commit bb0c562

Browse files
Add support for specified spoiler text (#526)
* Add support for specified spoiler text * go fmt * Fix duplicate response when URL is missing
1 parent 8991856 commit bb0c562

File tree

8 files changed

+101
-4
lines changed

8 files changed

+101
-4
lines changed

config/config_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,60 @@ func TestMastodon(t *testing.T) {
10931093
},
10941094
want: "foo",
10951095
},
1096+
{
1097+
name: "default mastodon cw",
1098+
envs: map[string]string{
1099+
"WAYBACK_MASTODON_CW": "true",
1100+
},
1101+
call: func(t *testing.T, opts *Options, want string) {
1102+
called := strconv.FormatBool(opts.MastodonCW())
1103+
if called != want {
1104+
t.Errorf(`Unexpected get the mastodon cw status, got %v instead of %s`, called, want)
1105+
}
1106+
},
1107+
want: "true",
1108+
},
1109+
{
1110+
name: "specified mastodon cw",
1111+
envs: map[string]string{
1112+
"WAYBACK_MASTODON_CW": "false",
1113+
},
1114+
call: func(t *testing.T, opts *Options, want string) {
1115+
called := strconv.FormatBool(opts.MastodonCW())
1116+
if called != want {
1117+
t.Errorf(`Unexpected get the mastodon cw status, got %v instead of %s`, called, want)
1118+
}
1119+
},
1120+
want: "false",
1121+
},
1122+
{
1123+
name: "default mastodon cw text",
1124+
envs: map[string]string{
1125+
"WAYBACK_MASTODON_CWTEXT": "",
1126+
},
1127+
call: func(t *testing.T, opts *Options, want string) {
1128+
opts.mastodon.cw = true
1129+
called := opts.MastodonCWText()
1130+
if called != want {
1131+
t.Errorf(`Unexpected get the mastodon cw text, got %v instead of %s`, called, want)
1132+
}
1133+
},
1134+
want: defMastodonCWText,
1135+
},
1136+
{
1137+
name: "specified mastodon cw text",
1138+
envs: map[string]string{
1139+
"WAYBACK_MASTODON_CWTEXT": "foo",
1140+
},
1141+
call: func(t *testing.T, opts *Options, want string) {
1142+
opts.mastodon.cw = true
1143+
called := opts.MastodonCWText()
1144+
if called != want {
1145+
t.Errorf(`Unexpected get the mastodon cw text, got %v instead of %s`, called, want)
1146+
}
1147+
},
1148+
want: "foo",
1149+
},
10961150
{
10971151
name: "publish to mastodon enabled",
10981152
envs: map[string]string{

config/options.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ const (
4848
defMastodonClientKey = ""
4949
defMastodonClientSecret = ""
5050
defMastodonAccessToken = ""
51+
defMastodonCW = false
52+
defMastodonCWText = "Wayback archive links"
5153
defTwitterConsumerKey = ""
5254
defTwitterConsumerSecret = ""
5355
defTwitterAccessToken = ""
@@ -177,6 +179,8 @@ type mastodon struct {
177179
clientKey string
178180
clientSecret string
179181
accessToken string
182+
cw bool
183+
cwText string
180184
}
181185

182186
type discord struct {
@@ -293,6 +297,8 @@ func NewOptions() *Options {
293297
clientKey: defMastodonClientKey,
294298
clientSecret: defMastodonClientSecret,
295299
accessToken: defMastodonAccessToken,
300+
cw: defMastodonCW,
301+
cwText: defMastodonCWText,
296302
},
297303
discord: &discord{
298304
appID: defDiscordBotToken,
@@ -529,6 +535,19 @@ func (o *Options) MastodonAccessToken() string {
529535
return o.mastodon.accessToken
530536
}
531537

538+
// MastodonCW returns whether to enable content warning for publish to Mastodon.
539+
func (o *Options) MastodonCW() bool {
540+
return o.mastodon.cw
541+
}
542+
543+
// MastodonCWText returns the CW text for publish to Mastodon.
544+
func (o *Options) MastodonCWText() string {
545+
if o.MastodonCW() {
546+
return o.mastodon.cwText
547+
}
548+
return ""
549+
}
550+
532551
// PublishToMastodon returns whether to publish result to Mastodon.
533552
func (o *Options) PublishToMastodon() bool {
534553
return o.MastodonServer() != "" &&

config/parser.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ func (p *Parser) parseLines(lines []string) (err error) {
131131
p.opts.mastodon.clientSecret = parseString(val, defMastodonClientSecret)
132132
case "WAYBACK_MASTODON_TOKEN":
133133
p.opts.mastodon.accessToken = parseString(val, defMastodonAccessToken)
134+
case "WAYBACK_MASTODON_CW":
135+
p.opts.mastodon.cw = parseBool(val, defMastodonCW)
136+
case "WAYBACK_MASTODON_CWTEXT":
137+
p.opts.mastodon.cwText = parseString(val, defMastodonCWText)
134138
case "WAYBACK_TWITTER_CONSUMER_KEY":
135139
p.opts.twitter.consumerKey = parseString(val, defTwitterConsumerKey)
136140
case "WAYBACK_TWITTER_CONSUMER_SECRET":

docs/environment.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ Use the `-c` / `--config` option to specify the build definition file to use.
6363
| - | `WAYBACK_MASTODON_KEY` | - | The client key of your Mastodon application |
6464
| - | `WAYBACK_MASTODON_SECRET` | - | The client secret of your Mastodon application |
6565
| - | `WAYBACK_MASTODON_TOKEN` | - | The access token of your Mastodon application |
66+
| - | `WAYBACK_MASTODON_CW` | `false` | Whether specified content warning |
67+
| - | `WAYBACK_MASTODON_CWTEXT` | `Wayback archive links` | The text of content warning |
6668
| - | `WAYBACK_TWITTER_CONSUMER_KEY` | - | The customer key of your Twitter application |
6769
| - | `WAYBACK_TWITTER_CONSUMER_SECRET` | - | The customer secret of your Twitter application |
6870
| - | `WAYBACK_TWITTER_ACCESS_TOKEN` | - | The access token of your Twitter application |

publish/mastodon/mastodon.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ func (m *Mastodon) toMastodon(ctx context.Context, text, id string) bool {
8484
}
8585

8686
toot := &mastodon.Toot{
87-
Status: text,
88-
Visibility: mastodon.VisibilityPublic,
87+
Status: text,
88+
SpoilerText: m.opts.MastodonCWText(),
89+
Visibility: mastodon.VisibilityPublic,
8990
}
9091
if id != "" {
9192
toot.InReplyToID = mastodon.ID(id)

service/mastodon/mastodon.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ type Mastodon struct {
4343
store *storage.Storage
4444
pub *publish.Publish
4545

46-
archiving map[mastodon.ID]bool
46+
archiving map[mastodon.ID]bool
47+
processing map[mastodon.ID]int
4748

4849
clearTick *time.Ticker
4950
fetchTick *time.Ticker
@@ -104,6 +105,7 @@ func (m *Mastodon) Serve() error {
104105

105106
go func() {
106107
m.archiving = make(map[mastodon.ID]bool)
108+
m.processing = make(map[mastodon.ID]int)
107109
for {
108110
select {
109111
case <-m.clearTick.C:
@@ -139,6 +141,9 @@ func (m *Mastodon) Serve() error {
139141
metrics.IncrementWayback(metrics.ServiceMastodon, metrics.StatusRequest)
140142
bucket := pooling.Bucket{
141143
Request: func(ctx context.Context) error {
144+
m.Lock()
145+
m.processing[n.Status.ID] += 1
146+
m.Unlock()
142147
if err := m.process(ctx, n.ID, n.Status); err != nil {
143148
logger.Error("process failure, notification: %#v, error: %v", n, err)
144149
return err
@@ -155,6 +160,7 @@ func (m *Mastodon) Serve() error {
155160
m.pool.Put(bucket)
156161
m.Lock()
157162
delete(m.archiving, n.ID)
163+
delete(m.processing, n.ID)
158164
m.Unlock()
159165
}()
160166
}
@@ -206,7 +212,10 @@ func (m *Mastodon) process(ctx context.Context, id mastodon.ID, status *mastodon
206212
urls := service.MatchURL(m.opts, text)
207213
if len(urls) == 0 {
208214
logger.Warn("archives failure, URL no found.")
209-
m.ToMastodon(ctx, "URL no found", string(status.ID))
215+
// Ensure response once
216+
if m.processing[status.ID] == m.opts.PoolingSize() {
217+
m.ToMastodon(ctx, "URL no found", string(status.ID))
218+
}
210219
return errors.New("Mastodon: URL no found")
211220
}
212221

wayback.1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,12 @@ The client secret of your Mastodon application\&.
225225
.B WAYBACK_MASTODON_TOKEN
226226
The access token of your Mastodon application\&.
227227
.TP
228+
.B WAYBACK_MASTODON_CW
229+
Whether specified content warning\&.
230+
.TP
231+
.B WAYBACK_MASTODON_CWTEXT
232+
The text of content warning\&.
233+
.TP
228234
.B WAYBACK_TWITTER_CONSUMER_KEY
229235
The customer key of your Twitter application\&.
230236
.TP

wayback.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ WAYBACK_MASTODON_SERVER=
2222
WAYBACK_MASTODON_KEY=
2323
WAYBACK_MASTODON_SECRET=
2424
WAYBACK_MASTODON_TOKEN=
25+
WAYBACK_MASTODON_CW=false
26+
WAYBACK_MASTODON_CWTEXT=Wayback archive links
2527
WAYBACK_TWITTER_CONSUMER_KEY=
2628
WAYBACK_TWITTER_CONSUMER_SECRE=
2729
WAYBACK_TWITTER_ACCESS_TOKEN=

0 commit comments

Comments
 (0)