Skip to content

Commit

Permalink
add reaction action for rescheduling repeating events
Browse files Browse the repository at this point in the history
  • Loading branch information
CubicrootXYZ committed Sep 4, 2024
1 parent ad20a3d commit c35a27b
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ func assembleMatrixConfig(config *Config, icalConnector ical.Service) *matrix.Co
&reaction.DeleteEventAction{},
&reaction.AddTimeAction{},
&reaction.MarkDoneAction{},
&reaction.RescheduleRepeatingAction{},
)

cfg.BridgeServices = &matrix.BridgeServices{
Expand Down
91 changes: 91 additions & 0 deletions internal/connectors/matrix/actions/reaction/reschdule_repeating.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package reaction

import (
"time"

"github.com/CubicrootXYZ/gologger"
"github.com/CubicrootXYZ/matrix-reminder-and-calendar-bot/internal/connectors/matrix"
matrixdb "github.com/CubicrootXYZ/matrix-reminder-and-calendar-bot/internal/connectors/matrix/database"
"github.com/CubicrootXYZ/matrix-reminder-and-calendar-bot/internal/connectors/matrix/mautrixcl"
"github.com/CubicrootXYZ/matrix-reminder-and-calendar-bot/internal/connectors/matrix/messenger"
"github.com/CubicrootXYZ/matrix-reminder-and-calendar-bot/internal/database"
)

// RescheduleRepeatingAction takes care of rescheduling a repeating event.
type RescheduleRepeatingAction struct {
logger gologger.Logger
client mautrixcl.Client
messenger messenger.Messenger
matrixDB matrixdb.Service
db database.Service
}

// Configure is called on startup and sets all dependencies.
func (action *RescheduleRepeatingAction) Configure(logger gologger.Logger, client mautrixcl.Client, messenger messenger.Messenger, matrixDB matrixdb.Service, db database.Service, _ *matrix.BridgeServices) {
action.logger = logger
action.client = client
action.matrixDB = matrixDB
action.db = db
action.messenger = messenger
}

// Name of the action.
func (action *RescheduleRepeatingAction) Name() string {
return "Reschedule Repeating Event"
}

// GetDocu returns the documentation for the action.
func (action *RescheduleRepeatingAction) GetDocu() (title, explaination string, examples []string) {
return "Reschedule Repeating Event",
"React with a 🔂 to get reminded again in 1 hour without changing the next repeat cycle.",
[]string{"🔂"}
}

// Selector defines on which reactions this action should be called.
func (action *RescheduleRepeatingAction) Selector() []string {
return []string{"🔂"}
}

// HandleEvent is where the reaction event and the related message get's send to if it matches the Selector.
func (action *RescheduleRepeatingAction) HandleEvent(event *matrix.ReactionEvent, reactionToMessage *matrixdb.MatrixMessage) {
// TODO tests & add new reaction
l := action.logger.WithFields(
map[string]any{
"reaction": event.Content.RelatesTo.Key,
"room": reactionToMessage.RoomID,
"related_message": reactionToMessage.ID,
"user": event.Event.Sender,
},
)
if reactionToMessage.EventID == nil || reactionToMessage.Event == nil {
l.Infof("skipping because message does not relate to any event")
return
}

// Clone the event without being repetitive.
newEvt := &database.Event{
Time: time.Now().Add(time.Hour),
Duration: reactionToMessage.Event.Duration,
Message: reactionToMessage.Event.Message,
Active: true,
ChannelID: reactionToMessage.Event.ChannelID,
InputID: *&reactionToMessage.Event.InputID,

Check failure on line 72 in internal/connectors/matrix/actions/reaction/reschdule_repeating.go

View workflow job for this annotation

GitHub Actions / golang_quality / golangci

SA4001: *&x will be simplified to x. It will not copy x. (staticcheck)
}
newEvt, err := action.db.NewEvent(newEvt)

Check failure on line 74 in internal/connectors/matrix/actions/reaction/reschdule_repeating.go

View workflow job for this annotation

GitHub Actions / golang_quality / golangci

ineffectual assignment to newEvt (ineffassign)
if err != nil {
l.Err(err)
_ = action.messenger.SendMessageAsync(messenger.PlainTextMessage(
"Whoopsie, can not update the event as requested.",
event.Room.RoomID,
))
return
}

err = action.messenger.DeleteMessageAsync(&messenger.Delete{
ExternalIdentifier: reactionToMessage.ID,
ChannelExternalIdentifier: reactionToMessage.Room.RoomID,
})
if err != nil {
l.Err(err)
}
}

0 comments on commit c35a27b

Please sign in to comment.