aboutsummaryrefslogtreecommitdiff
path: root/cmd/mimi/modules
diff options
context:
space:
mode:
authorXe Iaso <me@xeiaso.net>2024-05-22 23:55:31 -0400
committerXe Iaso <me@xeiaso.net>2024-05-22 23:55:31 -0400
commitc08378f99472294130e70600e3305ef1a70acb58 (patch)
treee5342bd2932fda0cc23811ec192b54107a5ec09c /cmd/mimi/modules
parent035a53c46055458ccf2e90172fb1166848eaa254 (diff)
downloadx-c08378f99472294130e70600e3305ef1a70acb58.tar.xz
x-c08378f99472294130e70600e3305ef1a70acb58.zip
cmd/mi{,mi}: rejigger logic around
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'cmd/mimi/modules')
-rw-r--r--cmd/mimi/modules/irc/announcer.go70
-rw-r--r--cmd/mimi/modules/irc/irc.go7
2 files changed, 10 insertions, 67 deletions
diff --git a/cmd/mimi/modules/irc/announcer.go b/cmd/mimi/modules/irc/announcer.go
index 08d507d..81befdf 100644
--- a/cmd/mimi/modules/irc/announcer.go
+++ b/cmd/mimi/modules/irc/announcer.go
@@ -3,16 +3,11 @@ package irc
import (
"context"
"flag"
- "log/slog"
+ "fmt"
"net/http"
- "os"
- "path/filepath"
- "sync"
"github.com/twitchtv/twirp"
"google.golang.org/protobuf/types/known/emptypb"
- "tailscale.com/jsondb"
- "within.website/x/cmd/mimi/internal"
xinternal "within.website/x/internal"
"within.website/x/proto/external/jsonfeed"
"within.website/x/proto/mimi/announce"
@@ -32,75 +27,18 @@ func (m *Module) RegisterHTTP(mux *http.ServeMux) {
type AnnounceService struct {
*Module
- db *jsondb.DB[State]
- lock sync.Mutex
}
type State struct {
Announced map[string]struct{}
}
-func (a *AnnounceService) initDB() error {
- if a.db != nil {
- slog.Debug("db already initialized")
- return nil
- }
-
- fname := filepath.Join(internal.DataDir(), "announced.json")
- slog.Debug("opening jsondb for announced items", "fname", fname)
-
- if _, err := os.Stat(fname); os.IsNotExist(err) {
- os.WriteFile(fname, []byte(`{"Announced":{}}`), 0644)
- }
-
- db, err := jsondb.Open[State](fname)
- if err != nil {
- return err
- }
- db.Save()
-
- if db.Data == nil {
- slog.Debug("creating empty state")
- db.Data = &State{
- Announced: map[string]struct{}{
- "https://within.website/": struct{}{},
- },
- }
- if err := db.Save(); err != nil {
- return err
- }
- }
-
- a.db = db
-
- return nil
-}
-
func (a *AnnounceService) Announce(ctx context.Context, msg *jsonfeed.Item) (*emptypb.Empty, error) {
- a.lock.Lock()
- defer a.lock.Unlock()
-
- if err := a.initDB(); err != nil {
- return nil, twirp.InternalErrorWith(err)
- }
-
- if msg.Title == "" {
- return nil, twirp.NewError(twirp.InvalidArgument, "missing title")
- }
- if msg.Url == "" {
- return nil, twirp.NewError(twirp.InvalidArgument, "missing url")
- }
-
- if _, ok := a.db.Data.Announced[msg.Url]; ok {
- return &emptypb.Empty{}, nil
- }
+ a.conn.Privmsgf(*ircChannel, "New article :: %s :: %s", msg.Title, msg.Url)
- a.db.Data.Announced[msg.Url] = struct{}{}
- if err := a.db.Save(); err != nil {
- return nil, twirp.InternalErrorWith(err)
+ if _, err := a.dg.ChannelMessageSend(*discordAnnounceChannel, fmt.Sprintf("New article :: %s :: %s", msg.Title, msg.Url)); err != nil {
+ return nil, twirp.InternalErrorWith(err).WithMeta("step", "discord")
}
- a.conn.Privmsgf(*ircChannel, "New article :: %s :: %s", msg.Title, msg.Url)
-
return &emptypb.Empty{}, nil
}
diff --git a/cmd/mimi/modules/irc/irc.go b/cmd/mimi/modules/irc/irc.go
index 03d2bb8..3f7edbb 100644
--- a/cmd/mimi/modules/irc/irc.go
+++ b/cmd/mimi/modules/irc/irc.go
@@ -7,10 +7,13 @@ import (
"fmt"
"log/slog"
+ "github.com/bwmarrin/discordgo"
ircevent "github.com/thoj/go-ircevent"
)
var (
+ discordAnnounceChannel = flag.String("discord-announce-channel", "", "Discord channel to announce in")
+
ircServer = flag.String("irc-server", "irc.libera.chat:6697", "IRC server to connect to")
ircUsername = flag.String("irc-username", "[Mara]", "IRC username")
ircIdent = flag.String("irc-ident", "mara", "IRC ident")
@@ -20,9 +23,10 @@ var (
type Module struct {
conn *ircevent.Connection
+ dg *discordgo.Session
}
-func New(ctx context.Context) (*Module, error) {
+func New(ctx context.Context, dg *discordgo.Session) (*Module, error) {
conn := ircevent.IRC(*ircUsername, *ircIdent)
conn.UseTLS = true
conn.UseSASL = false
@@ -64,5 +68,6 @@ func New(ctx context.Context) (*Module, error) {
return &Module{
conn: conn,
+ dg: dg,
}, nil
}