diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..620cb07 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM xena/go:1.13.5 AS build +WORKDIR /mi +COPY go.mod . +COPY go.sum . +RUN go mod download +COPY . . +RUN GOBIN=/mi/bin go install ./cmd/... + +FROM xena/alpine +COPY --from=build /mi/bin /usr/local/bin +COPY ./run /run +RUN mkdir -p /mi +CMD ["/bin/sh", "/run/start.sh"] diff --git a/cmd/mi-init/main.go b/cmd/mi-init/main.go new file mode 100644 index 0000000..17eb6cf --- /dev/null +++ b/cmd/mi-init/main.go @@ -0,0 +1,55 @@ +package main + +import ( + "flag" + "log" + + "github.com/facebookgo/flagenv" + "github.com/hashicorp/go-multierror" + _ "github.com/joho/godotenv/autoload" + r "gopkg.in/rethinkdb/rethinkdb-go.v6" + "within.website/mi/rethink" +) + +var ( + rethinkDBURL = flag.String("rethinkdb-url", "rethinkdb2://admin@127.0.0.1:28015/mi", "RethinkDB URL") +) + +func main() { + flagenv.Parse() + flag.Parse() + + session, err := rethink.GetSession(*rethinkDBURL) + if err != nil { + log.Fatal(err) + } + + err = r.DB("mi").Info().Exec(session) + if err == nil { + return + } + + if err := r.DBCreate("mi").Exec(session); err != nil { + log.Fatal(err) + } + + tables := []string{ + "mastodon", + "blogposts", + "reddit_comments", + "reddit_posts", + "twitter", + } + + var result error + + for _, tbl := range tables { + if err := r.DB("mi").TableCreate(tbl).Exec(session); err != nil { + result = multierror.Append(result, err) + } + } + + if result != nil { + log.Fatal(result) + } +} diff --git a/cmd/mi/main.go b/cmd/mi/main.go index 0d05894..526b0b1 100644 --- a/cmd/mi/main.go +++ b/cmd/mi/main.go @@ -5,16 +5,15 @@ import ( "flag" "fmt" "net/http" - "net/url" "os" "os/signal" "time" "github.com/facebookarchive/flagenv" _ "github.com/joho/godotenv/autoload" - r "gopkg.in/rethinkdb/rethinkdb-go.v6" "within.website/ln" "within.website/ln/ex" + "within.website/mi/rethink" "within.website/x/web/useragent" ) @@ -31,9 +30,6 @@ var ( // RethinkDB rethinkDBURL = flag.String("rethinkdb-url", "rethinkdb2://admin@127.0.0.1:28015/mi", "RethinkDB URL") - // Blog - blogURL = flag.String("blog-url", "https://christine.website/blog.json", "JSONFeed to monitor for new posts") - // Port port = flag.String("port", "5000", "HTTP port") domain = flag.String("domain", "mi.within.website", "domain this is being served on") @@ -43,29 +39,11 @@ func main() { flagenv.Parse() flag.Parse() - http.DefaultTransport = useragent.Transport("mi-posse", "https://" + *domain + "/.within/botinfo", http.DefaultTransport) + http.DefaultTransport = useragent.Transport("mi-posse", "https://"+*domain+"/.within/botinfo", http.DefaultTransport) ctx := context.Background() - r.SetTags("rethinkdb", "json") - - u, err := url.Parse(*rethinkDBURL) - if err != nil { - ln.FatalErr(ctx, err) - } - - pw, _ := u.User.Password() - db := u.Path[1:] - - session, err := r.Connect(r.ConnectOpts{ - Address: u.Host, - Database: db, - Username: u.User.Username(), - Password: pw, - }) - if err != nil { - ln.FatalErr(ctx, err) - } + session, err := rethink.GetSession(*rethinkDBURL) mastodonClient, err := makeMastodon() if err != nil { @@ -82,6 +60,11 @@ func main() { ln.FatalErr(ctx, err) } + patreonClient, err := makePatreon() + if err != nil { + ln.FatalErr(ctx, err) + } + mux := http.NewServeMux() pm, err := MakeMiddleware(*pasetoPublicKey, *pasetoPrivateKey, mux) if err != nil { @@ -104,6 +87,7 @@ func main() { session: session, mastodonClient: mastodonClient, twitterClient: twitterClient, + patreonClient: patreonClient, redditBot: redditBot, mux: mux, } diff --git a/cmd/mi/mastodon.go b/cmd/mi/mastodon.go index d2c6808..e1873f0 100644 --- a/cmd/mi/mastodon.go +++ b/cmd/mi/mastodon.go @@ -83,8 +83,9 @@ type MastodonStatus struct { Account struct { Acct string `json:"acct"` } `json:"account"` - URL string `json:"url"` - Content string `json:"content"` + URL string `json:"url"` + Content string `json:"content"` + Sensitive bool `json:"sensitive"` } type NewStatus struct { @@ -128,6 +129,10 @@ func (mi *Mi) StreamMastodonToTwitter(ctx context.Context) { } st := ns.NewVal + if st.Sensitive { + continue + } + if st.Account.Acct != *mastodonAccount { continue } diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..7dd1316 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,17 @@ +version: '3.7' + +services: + mi: + restart: always + build: . + env_file: .env + depends_on: rethinkdb + ports: "127.0.0.1:39243:5000" + + rethinkdb: + image: "rethinkdb:2.4.0" + volumes: + - "rethink-data:/data" + +volumes: + rethink-data: diff --git a/rethink/session.go b/rethink/session.go new file mode 100644 index 0000000..25ac253 --- /dev/null +++ b/rethink/session.go @@ -0,0 +1,31 @@ +package rethink + +import ( + "net/url" + + r "gopkg.in/rethinkdb/rethinkdb-go.v6" +) + +func GetSession(dbURL string) (*r.Session, error) { + r.SetTags("rethinkdb", "json") + + u, err := url.Parse(dbURL) + if err != nil { + return nil, err + } + + pw, _ := u.User.Password() + db := u.Path[1:] + + session, err := r.Connect(r.ConnectOpts{ + Address: u.Host, + Database: db, + Username: u.User.Username(), + Password: pw, + }) + if err != nil { + return nil, err + } + + return session, nil +} diff --git a/run/start.sh b/run/start.sh new file mode 100755 index 0000000..4a5d71d --- /dev/null +++ b/run/start.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +set -e + +mi-init +mi