Skip to content

Commit

Permalink
Add support for specified spoiler text (#526)
Browse files Browse the repository at this point in the history
* Add support for specified spoiler text

* go fmt

* Fix duplicate response when URL is missing
  • Loading branch information
waybackarchiver authored Jun 22, 2024
1 parent 8991856 commit bb0c562
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 4 deletions.
54 changes: 54 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,60 @@ func TestMastodon(t *testing.T) {
},
want: "foo",
},
{
name: "default mastodon cw",
envs: map[string]string{
"WAYBACK_MASTODON_CW": "true",
},
call: func(t *testing.T, opts *Options, want string) {
called := strconv.FormatBool(opts.MastodonCW())
if called != want {
t.Errorf(`Unexpected get the mastodon cw status, got %v instead of %s`, called, want)
}
},
want: "true",
},
{
name: "specified mastodon cw",
envs: map[string]string{
"WAYBACK_MASTODON_CW": "false",
},
call: func(t *testing.T, opts *Options, want string) {
called := strconv.FormatBool(opts.MastodonCW())
if called != want {
t.Errorf(`Unexpected get the mastodon cw status, got %v instead of %s`, called, want)
}
},
want: "false",
},
{
name: "default mastodon cw text",
envs: map[string]string{
"WAYBACK_MASTODON_CWTEXT": "",
},
call: func(t *testing.T, opts *Options, want string) {
opts.mastodon.cw = true
called := opts.MastodonCWText()
if called != want {
t.Errorf(`Unexpected get the mastodon cw text, got %v instead of %s`, called, want)
}
},
want: defMastodonCWText,
},
{
name: "specified mastodon cw text",
envs: map[string]string{
"WAYBACK_MASTODON_CWTEXT": "foo",
},
call: func(t *testing.T, opts *Options, want string) {
opts.mastodon.cw = true
called := opts.MastodonCWText()
if called != want {
t.Errorf(`Unexpected get the mastodon cw text, got %v instead of %s`, called, want)
}
},
want: "foo",
},
{
name: "publish to mastodon enabled",
envs: map[string]string{
Expand Down
19 changes: 19 additions & 0 deletions config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const (
defMastodonClientKey = ""
defMastodonClientSecret = ""
defMastodonAccessToken = ""
defMastodonCW = false
defMastodonCWText = "Wayback archive links"
defTwitterConsumerKey = ""
defTwitterConsumerSecret = ""
defTwitterAccessToken = ""
Expand Down Expand Up @@ -177,6 +179,8 @@ type mastodon struct {
clientKey string
clientSecret string
accessToken string
cw bool
cwText string
}

type discord struct {
Expand Down Expand Up @@ -293,6 +297,8 @@ func NewOptions() *Options {
clientKey: defMastodonClientKey,
clientSecret: defMastodonClientSecret,
accessToken: defMastodonAccessToken,
cw: defMastodonCW,
cwText: defMastodonCWText,
},
discord: &discord{
appID: defDiscordBotToken,
Expand Down Expand Up @@ -529,6 +535,19 @@ func (o *Options) MastodonAccessToken() string {
return o.mastodon.accessToken
}

// MastodonCW returns whether to enable content warning for publish to Mastodon.
func (o *Options) MastodonCW() bool {
return o.mastodon.cw
}

// MastodonCWText returns the CW text for publish to Mastodon.
func (o *Options) MastodonCWText() string {
if o.MastodonCW() {
return o.mastodon.cwText
}
return ""
}

// PublishToMastodon returns whether to publish result to Mastodon.
func (o *Options) PublishToMastodon() bool {
return o.MastodonServer() != "" &&
Expand Down
4 changes: 4 additions & 0 deletions config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ func (p *Parser) parseLines(lines []string) (err error) {
p.opts.mastodon.clientSecret = parseString(val, defMastodonClientSecret)
case "WAYBACK_MASTODON_TOKEN":
p.opts.mastodon.accessToken = parseString(val, defMastodonAccessToken)
case "WAYBACK_MASTODON_CW":
p.opts.mastodon.cw = parseBool(val, defMastodonCW)
case "WAYBACK_MASTODON_CWTEXT":
p.opts.mastodon.cwText = parseString(val, defMastodonCWText)
case "WAYBACK_TWITTER_CONSUMER_KEY":
p.opts.twitter.consumerKey = parseString(val, defTwitterConsumerKey)
case "WAYBACK_TWITTER_CONSUMER_SECRET":
Expand Down
2 changes: 2 additions & 0 deletions docs/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ Use the `-c` / `--config` option to specify the build definition file to use.
| - | `WAYBACK_MASTODON_KEY` | - | The client key of your Mastodon application |
| - | `WAYBACK_MASTODON_SECRET` | - | The client secret of your Mastodon application |
| - | `WAYBACK_MASTODON_TOKEN` | - | The access token of your Mastodon application |
| - | `WAYBACK_MASTODON_CW` | `false` | Whether specified content warning |
| - | `WAYBACK_MASTODON_CWTEXT` | `Wayback archive links` | The text of content warning |
| - | `WAYBACK_TWITTER_CONSUMER_KEY` | - | The customer key of your Twitter application |
| - | `WAYBACK_TWITTER_CONSUMER_SECRET` | - | The customer secret of your Twitter application |
| - | `WAYBACK_TWITTER_ACCESS_TOKEN` | - | The access token of your Twitter application |
Expand Down
5 changes: 3 additions & 2 deletions publish/mastodon/mastodon.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ func (m *Mastodon) toMastodon(ctx context.Context, text, id string) bool {
}

toot := &mastodon.Toot{
Status: text,
Visibility: mastodon.VisibilityPublic,
Status: text,
SpoilerText: m.opts.MastodonCWText(),
Visibility: mastodon.VisibilityPublic,
}
if id != "" {
toot.InReplyToID = mastodon.ID(id)
Expand Down
13 changes: 11 additions & 2 deletions service/mastodon/mastodon.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ type Mastodon struct {
store *storage.Storage
pub *publish.Publish

archiving map[mastodon.ID]bool
archiving map[mastodon.ID]bool
processing map[mastodon.ID]int

clearTick *time.Ticker
fetchTick *time.Ticker
Expand Down Expand Up @@ -104,6 +105,7 @@ func (m *Mastodon) Serve() error {

go func() {
m.archiving = make(map[mastodon.ID]bool)
m.processing = make(map[mastodon.ID]int)
for {
select {
case <-m.clearTick.C:
Expand Down Expand Up @@ -139,6 +141,9 @@ func (m *Mastodon) Serve() error {
metrics.IncrementWayback(metrics.ServiceMastodon, metrics.StatusRequest)
bucket := pooling.Bucket{
Request: func(ctx context.Context) error {
m.Lock()
m.processing[n.Status.ID] += 1
m.Unlock()
if err := m.process(ctx, n.ID, n.Status); err != nil {
logger.Error("process failure, notification: %#v, error: %v", n, err)
return err
Expand All @@ -155,6 +160,7 @@ func (m *Mastodon) Serve() error {
m.pool.Put(bucket)
m.Lock()
delete(m.archiving, n.ID)
delete(m.processing, n.ID)
m.Unlock()
}()
}
Expand Down Expand Up @@ -206,7 +212,10 @@ func (m *Mastodon) process(ctx context.Context, id mastodon.ID, status *mastodon
urls := service.MatchURL(m.opts, text)
if len(urls) == 0 {
logger.Warn("archives failure, URL no found.")
m.ToMastodon(ctx, "URL no found", string(status.ID))
// Ensure response once
if m.processing[status.ID] == m.opts.PoolingSize() {
m.ToMastodon(ctx, "URL no found", string(status.ID))
}
return errors.New("Mastodon: URL no found")
}

Expand Down
6 changes: 6 additions & 0 deletions wayback.1
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@ The client secret of your Mastodon application\&.
.B WAYBACK_MASTODON_TOKEN
The access token of your Mastodon application\&.
.TP
.B WAYBACK_MASTODON_CW
Whether specified content warning\&.
.TP
.B WAYBACK_MASTODON_CWTEXT
The text of content warning\&.
.TP
.B WAYBACK_TWITTER_CONSUMER_KEY
The customer key of your Twitter application\&.
.TP
Expand Down
2 changes: 2 additions & 0 deletions wayback.conf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ WAYBACK_MASTODON_SERVER=
WAYBACK_MASTODON_KEY=
WAYBACK_MASTODON_SECRET=
WAYBACK_MASTODON_TOKEN=
WAYBACK_MASTODON_CW=false
WAYBACK_MASTODON_CWTEXT=Wayback archive links
WAYBACK_TWITTER_CONSUMER_KEY=
WAYBACK_TWITTER_CONSUMER_SECRE=
WAYBACK_TWITTER_ACCESS_TOKEN=
Expand Down

0 comments on commit bb0c562

Please sign in to comment.