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 settings #56

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
35 changes: 32 additions & 3 deletions artworks/artworks.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,36 @@ import (
)

type Provider interface {
Name() string // Name gets a name of the provider for referencing in settings and the database or identifying it without using reflection
Match(url string) (string, bool)
Find(id string) (Artwork, error)
Enabled(*store.Guild) bool
}

type ProviderBase struct {
name string
}

func (pb *ProviderBase) Name() string {
return pb.name
}

func (pb *ProviderBase) Enabled(g *store.Guild) bool {
provider, ok := g.Providers[pb.name]
if !ok {
// All providers are enabled by default, if provider entry doesn't exist then
// it wasn't created in the database yet because no one attempted to change it.
// Therefore, we return true.
return true
}

return !provider.Disabled
}

func NewProviderBase(name string) ProviderBase {
return ProviderBase{name: name}
}

type Artwork interface {
StoreArtwork() *store.Artwork
MessageSends(footer string, tags bool) ([]*discordgo.MessageSend, error)
Expand All @@ -24,11 +49,15 @@ type Artwork interface {

func EscapeMarkdown(content string) string {
contents := strings.Split(content, "\n")
regex := regexp.MustCompile("^#{1,3}")
escape := regexp.MustCompile(`[._~|#<>*-]`)

for i, line := range contents {
if regex.MatchString(line) {
contents[i] = "\\" + line
if index := escape.FindAllStringIndex(line, -1); index != nil {
newLine := line
for j, s := range index {
newLine = newLine[:s[0]+j] + "\\" + newLine[s[0]+j:]
}
contents[i] = newLine
}
}
return strings.Join(contents, "\n")
Expand Down
9 changes: 4 additions & 5 deletions artworks/bluesky/bluesky.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
)

type Bluesky struct {
artworks.ProviderBase

regex *regexp.Regexp
client *http.Client
}
Expand Down Expand Up @@ -88,16 +90,13 @@ type Artwork struct {

func New() *Bluesky {
return &Bluesky{
ProviderBase: artworks.NewProviderBase("bluesky"),

regex: regexp.MustCompile(`(?i)https://(?:www\.)?bsky\.app/profile/(\w.+)/post/([\w\-]+)`),
client: http.DefaultClient,
}
}

// Enabled implements artworks.Provider.
func (*Bluesky) Enabled(g *store.Guild) bool {
return g.Bluesky
}

// Find implements artworks.Provider.
func (b *Bluesky) Find(id string) (artworks.Artwork, error) {
return artworks.WrapError(b, func() (artworks.Artwork, error) {
Expand Down
8 changes: 4 additions & 4 deletions artworks/deviant/deviant.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
)

type DeviantArt struct {
artworks.ProviderBase

regex *regexp.Regexp
}

Expand Down Expand Up @@ -67,6 +69,8 @@ type deviantEmbed struct {

func New() artworks.Provider {
return &DeviantArt{
ProviderBase: artworks.NewProviderBase("deviant"),

regex: regexp.MustCompile(`(?i)https?://(?:www\.)?deviantart\.com/\w.+/art/([\w\-]+)`),
}
}
Expand Down Expand Up @@ -119,10 +123,6 @@ func (d *DeviantArt) Match(s string) (string, bool) {
return res[1], true
}

func (*DeviantArt) Enabled(g *store.Guild) bool {
return g.Deviant
}

func (a *Artwork) MessageSends(footer string, tagsEnabled bool) ([]*discordgo.MessageSend, error) {
eb := embeds.NewBuilder()

Expand Down
8 changes: 4 additions & 4 deletions artworks/pixiv/pixiv.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
)

type Pixiv struct {
artworks.ProviderBase

app *pixiv.AppPixivAPI
proxyHost string
regex *regexp.Regexp
Expand Down Expand Up @@ -59,6 +61,8 @@ func New(proxyHost string) artworks.Provider {
}

return &Pixiv{
ProviderBase: artworks.NewProviderBase("pixiv"),

app: pixiv.NewApp(),
proxyHost: proxyHost,
regex: regexp.MustCompile(`(?i)https?://(?:www\.)?pixiv\.net/(?:en/)?(?:artworks/|member_illust\.php\?)(?:mode=medium&)?(?:illust_id=)?([0-9]+)`),
Expand Down Expand Up @@ -164,10 +168,6 @@ func (p *Pixiv) Find(id string) (artworks.Artwork, error) {
})
}

func (*Pixiv) Enabled(g *store.Guild) bool {
return g.Pixiv
}

func (a *Artwork) StoreArtwork() *store.Artwork {
return &store.Artwork{
Title: a.Title,
Expand Down
50 changes: 1 addition & 49 deletions artworks/twitter/twitter.go → artworks/twitter/artwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package twitter

import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"time"
Expand All @@ -20,26 +18,15 @@ import (
"github.com/bwmarrin/discordgo"
)

// Common Twitter errors
var (
ErrTweetNotFound = errors.New("tweet not found")
ErrPrivateAccount = errors.New("unable to view this tweet because account is private")
)

type Twitter struct {
twitterMatcher
providers []artworks.Provider
}

type Artwork struct {
Videos []Video
Photos []string
Timestamp time.Time
id string
FullName string
Username string
Content string
Permalink string
Timestamp time.Time
Likes int
Replies int
Retweets int
Expand All @@ -52,41 +39,6 @@ type Video struct {
Preview string
}

func New() artworks.Provider {
return &Twitter{
providers: []artworks.Provider{newFxTwitter()},
twitterMatcher: twitterMatcher{
regex: regexp.MustCompile(`^(?:mobile\.)?(?:(?:fix(?:up|v))?x|(?:[fv]x)?twitter)\.com$`),
},
}
}

func (t *Twitter) Find(id string) (artworks.Artwork, error) {
return artworks.WrapError(t, func() (artworks.Artwork, error) {
var (
artwork artworks.Artwork
errs []error
)

for _, provider := range t.providers {
var err error
artwork, err = provider.Find(id)
if errors.Is(err, ErrTweetNotFound) || errors.Is(err, ErrPrivateAccount) {
return nil, err
}

if err != nil {
errs = append(errs, err)
continue
}

return artwork, nil
}

return &Artwork{}, errors.Join(errs...)
})
}

func (a *Artwork) StoreArtwork() *store.Artwork {
media := make([]string, 0, len(a.Photos)+len(a.Videos))

Expand Down
Loading
Loading