Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Send, Post, Share, Crosspost #51

Merged
merged 16 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion artworks/twitter/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package twitter

import (
"net/url"
"regexp"
"strconv"
"strings"

Expand All @@ -16,7 +17,7 @@ func (twitterMatcher) Match(s string) (string, bool) {
return "", false
}

if !strings.Contains(u.Host, "twitter.com") && u.Host != "x.com" {
if ok, _ := regexp.MatchString("^(?:mobile\\.)?(?:(?:fix(?:up|v))?x|(?:[fv]x)?twitter)\\.com$", u.Host); !ok {
return "", false
}

Expand Down
6 changes: 5 additions & 1 deletion artworks/twitter/twitter_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@ var _ = DescribeTable(
},
Entry("Valid artwork", "https://twitter.com/watsonameliaEN/status/1371674594675937282", "1371674594675937282", true),
Entry("Query params", "https://twitter.com/watsonameliaEN/status/1371674594675937282?param=1", "1371674594675937282", true),
Entry("Mobile URL", "https://mobile.twitter.com/watsonameliaEN/status/1371674594675937282", "1371674594675937282", true),
Entry("Mobile Twitter URL", "https://mobile.twitter.com/watsonameliaEN/status/1371674594675937282", "1371674594675937282", true),
Entry("Mobile X URL", "https://mobile.x.com/watsonameliaEN/status/1371674594675937282", "1371674594675937282", true),
Entry("No username", "https://twitter.com/i/status/1371674594675937282", "1371674594675937282", true),
Entry("iweb URL", "https://twitter.com/i/web/status/1371674594675937282", "1371674594675937282", true),
Entry("With photo suffix", "https://twitter.com/i/web/status/1371674594675937282/photo/1", "1371674594675937282", true),
Entry("Not artwork pixiv URL", "https://pixiv.net/users/123456", "", false),
Entry("ID with letters", "https://twitter.com/i/web/status/1371674594675937282f", "", false),
Entry("Different domain", "https://google.com/i/status/123456", "", false),
Entry("Invalid URL", "efe", "", false),
Entry("fxtwitter link", "https://fxtwitter.com/i/status/1234", "1234", true),
Entry("vxtwitter link", "https://vxtwitter.com/i/status/1234", "1234", true),
Entry("X link", "https://x.com/i/status/1234", "1234", true),
Entry("fixupx link", "https://fixupx.com/i/status/1234", "1234", true),
Entry("fixvx link", "https://fixvx.com/i/status/1234", "1234", true),
)
29 changes: 21 additions & 8 deletions bot/bot.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bot

import (
"context"
"fmt"
"time"

Expand All @@ -27,6 +28,7 @@ type Bot struct {
Stats *stats.Stats
StartTime time.Time
Router *gumi.Router
Context context.Context

// caches
BannedUsers *ttlcache.Cache
Expand All @@ -43,7 +45,12 @@ type Bot struct {
Store store.Store
}

func New(config *config.Config, store store.Store, logger *zap.SugaredLogger, rd repost.Detector) (*Bot, error) {
func New(
config *config.Config,
store store.Store,
logger *zap.SugaredLogger,
rd repost.Detector,
) (*Bot, error) {
mgr, err := shards.New("Bot " + config.Discord.Token)
if err != nil {
return nil, fmt.Errorf("failed to init a shard manager: %w", err)
Expand Down Expand Up @@ -85,25 +92,31 @@ func (b *Bot) AddProvider(provider artworks.Provider) {
b.ArtworkProviders = append(b.ArtworkProviders, provider)
}

func (b *Bot) AddHandler(handler interface{}) {
func (b *Bot) AddHandler(handler any) {
b.ShardManager.AddHandler(handler)
}

func (b *Bot) Open() error {
func (b *Bot) Start(ctx context.Context) error {
b.ShardManager.AddHandler(b.Router.Handler())

b.StartTime = time.Now()
b.Stats = stats.New(b.Router, b.ArtworkProviders)
b.Context = ctx

b.Log.Debug("starting a bot")
if err := b.ShardManager.Start(); err != nil {
return err
}

b.Log.Info("Started a bot.")
select {
case <-ctx.Done():
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

return nil
}
b.Store.Close(shutdownCtx)
b.RepostDetector.Close()
b.ShardManager.Shutdown()

func (b *Bot) Close() error {
return b.ShardManager.Shutdown()
return ctx.Err()
}
}
19 changes: 7 additions & 12 deletions cmd/boetea/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
"go.uber.org/zap"
)

func initStore(mongoURI, database string) (store.Store, error) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
func initStore(ctx context.Context, mongoURI, database string) (store.Store, error) {
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()

mongo, err := mongo.New(ctx, mongoURI, database)
Expand Down Expand Up @@ -69,7 +69,10 @@ func main() {

log := zapLogger.Sugar()

store, err := initStore(cfg.Mongo.URI, cfg.Mongo.Database)
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
defer cancel()

store, err := initStore(ctx, cfg.Mongo.URI, cfg.Mongo.Database)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -117,15 +120,7 @@ func main() {
handlers.RegisterHandlers(b)
commands.RegisterCommands(b)

if err := b.Open(); err != nil {
if err := b.Start(ctx); err != nil {
log.Fatal(err)
}

sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc

store.Close(context.Background())
repostDetector.Close()
b.Close()
}
Loading
Loading