replace email processing reactions; update deps
This commit is contained in:
@@ -135,15 +135,10 @@ func (b *Bot) Start(statusMsg string) error {
|
||||
|
||||
b.initSync()
|
||||
b.log.Info().Msg("Postmoogle has been started")
|
||||
return b.lp.Start(statusMsg)
|
||||
return b.lp.Start(ctx, statusMsg)
|
||||
}
|
||||
|
||||
// Stop the bot
|
||||
func (b *Bot) Stop() {
|
||||
ctx := context.Background()
|
||||
err := b.lp.GetClient().SetPresence(ctx, event.PresenceOffline)
|
||||
if err != nil {
|
||||
b.log.Error().Err(err).Msg("cannot set presence = offline")
|
||||
}
|
||||
b.lp.GetClient().StopSync()
|
||||
b.lp.Stop(context.Background())
|
||||
}
|
||||
|
||||
@@ -624,7 +624,6 @@ func (b *Bot) runSendCommand(ctx context.Context, cfg config.Room, tos []string,
|
||||
from := cfg.Mailbox() + "@" + domain
|
||||
ID := email.MessageID(evt.ID, domain)
|
||||
for _, to := range tos {
|
||||
recipients := []string{to}
|
||||
eml := email.New(ID, "", " "+ID, subject, from, to, to, "", body, htmlBody, nil, nil)
|
||||
data := eml.Compose(b.cfg.GetBot(ctx).DKIMPrivateKey())
|
||||
if data == "" {
|
||||
@@ -634,14 +633,14 @@ func (b *Bot) runSendCommand(ctx context.Context, cfg config.Room, tos []string,
|
||||
queued, err := b.Sendmail(ctx, evt.ID, from, to, data)
|
||||
if queued {
|
||||
b.log.Warn().Err(err).Msg("email has been queued")
|
||||
b.saveSentMetadata(ctx, queued, evt.ID, recipients, eml, cfg)
|
||||
b.saveSentMetadata(ctx, queued, evt.ID, to, eml, cfg)
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
b.Error(ctx, "cannot send email to %s: %v", to, err)
|
||||
continue
|
||||
}
|
||||
b.saveSentMetadata(ctx, false, evt.ID, recipients, eml, cfg)
|
||||
b.saveSentMetadata(ctx, false, evt.ID, to, eml, cfg)
|
||||
}
|
||||
if len(tos) > 1 {
|
||||
b.lp.SendNotice(ctx, evt.RoomID, "All emails were sent.", linkpearl.RelatesTo(evt.ID, cfg.NoThreads()))
|
||||
|
||||
15
bot/email.go
15
bot/email.go
@@ -280,7 +280,7 @@ func (b *Bot) sendAutoreply(ctx context.Context, roomID id.RoomID, threadID id.E
|
||||
queued, err = b.Sendmail(ctx, evt.ID, meta.From, to, data)
|
||||
if queued {
|
||||
b.log.Info().Err(err).Str("from", meta.From).Str("to", to).Msg("email has been queued")
|
||||
b.saveSentMetadata(ctx, queued, meta.ThreadID, recipients, eml, cfg, "Autoreply has been sent to "+to+" (queued)")
|
||||
b.saveSentMetadata(ctx, queued, meta.ThreadID, to, eml, cfg, "Autoreply has been sent to "+to+" (queued)")
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -289,7 +289,7 @@ func (b *Bot) sendAutoreply(ctx context.Context, roomID id.RoomID, threadID id.E
|
||||
continue
|
||||
}
|
||||
|
||||
b.saveSentMetadata(ctx, queued, meta.ThreadID, recipients, eml, cfg, "Autoreply has been sent to "+to)
|
||||
b.saveSentMetadata(ctx, queued, meta.ThreadID, to, eml, cfg, "Autoreply has been sent to "+to)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -364,7 +364,7 @@ func (b *Bot) SendEmailReply(ctx context.Context) {
|
||||
queued, err = b.Sendmail(ctx, evt.ID, meta.From, to, data)
|
||||
if queued {
|
||||
b.log.Info().Err(err).Str("from", meta.From).Str("to", to).Msg("email has been queued")
|
||||
b.saveSentMetadata(ctx, queued, meta.ThreadID, recipients, eml, cfg)
|
||||
b.saveSentMetadata(ctx, queued, meta.ThreadID, to, eml, cfg)
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -373,7 +373,7 @@ func (b *Bot) SendEmailReply(ctx context.Context) {
|
||||
continue
|
||||
}
|
||||
|
||||
b.saveSentMetadata(ctx, queued, meta.ThreadID, recipients, eml, cfg)
|
||||
b.saveSentMetadata(ctx, queued, meta.ThreadID, to, eml, cfg)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -538,11 +538,10 @@ func (b *Bot) getParentEmail(ctx context.Context, evt *event.Event, newFromMailb
|
||||
|
||||
// saveSentMetadata used to save metadata from !pm sent and thread reply events to a separate notice message
|
||||
// because that metadata is needed to determine email thread relations
|
||||
func (b *Bot) saveSentMetadata(ctx context.Context, queued bool, threadID id.EventID, recipients []string, eml *email.Email, cfg config.Room, textOverride ...string) {
|
||||
addrs := strings.Join(recipients, ", ")
|
||||
text := "Email has been sent to " + addrs
|
||||
func (b *Bot) saveSentMetadata(ctx context.Context, queued bool, threadID id.EventID, to string, eml *email.Email, cfg config.Room, textOverride ...string) {
|
||||
text := "Email has been sent to " + to
|
||||
if queued {
|
||||
text = "Email to " + addrs + " has been queued"
|
||||
text = "Email to " + to + " has been queued"
|
||||
}
|
||||
if len(textOverride) > 0 {
|
||||
text = textOverride[0]
|
||||
|
||||
26
bot/mutex.go
26
bot/mutex.go
@@ -6,26 +6,22 @@ import (
|
||||
"maunium.net/go/mautrix/id"
|
||||
)
|
||||
|
||||
func (b *Bot) lock(ctx context.Context, roomID id.RoomID, optionalEventID ...id.EventID) {
|
||||
const (
|
||||
reactionLock = "📨"
|
||||
reactionUnlock = "✅"
|
||||
)
|
||||
|
||||
func (b *Bot) lock(ctx context.Context, roomID id.RoomID, eventID id.EventID) {
|
||||
b.mu.Lock(roomID.String())
|
||||
|
||||
if len(optionalEventID) == 0 {
|
||||
return
|
||||
}
|
||||
evtID := optionalEventID[0]
|
||||
if _, err := b.lp.GetClient().SendReaction(ctx, roomID, evtID, "📨"); err != nil {
|
||||
b.log.Error().Err(err).Str("roomID", roomID.String()).Str("eventID", evtID.String()).Msg("cannot send reaction on lock")
|
||||
if err := b.lp.SendReaction(ctx, roomID, eventID, reactionLock); err != nil {
|
||||
b.log.Error().Err(err).Str("roomID", roomID.String()).Str("eventID", eventID.String()).Msg("cannot send reaction on lock")
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bot) unlock(ctx context.Context, roomID id.RoomID, optionalEventID ...id.EventID) {
|
||||
func (b *Bot) unlock(ctx context.Context, roomID id.RoomID, eventID id.EventID) {
|
||||
b.mu.Unlock(roomID.String())
|
||||
|
||||
if len(optionalEventID) == 0 {
|
||||
return
|
||||
}
|
||||
evtID := optionalEventID[0]
|
||||
if _, err := b.lp.GetClient().SendReaction(ctx, roomID, evtID, "✅"); err != nil {
|
||||
b.log.Error().Err(err).Str("roomID", roomID.String()).Str("eventID", evtID.String()).Msg("cannot send reaction on unlock")
|
||||
if err := b.lp.ReplaceReaction(ctx, roomID, eventID, reactionLock, reactionUnlock); err != nil {
|
||||
b.log.Error().Err(err).Str("roomID", roomID.String()).Str("eventID", eventID.String()).Msg("cannot send reaction on unlock")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user