feat: domain blocker plugin
This commit is contained in:
parent
c9edb57505
commit
7dd02c0056
25 changed files with 898 additions and 63 deletions
|
@ -41,7 +41,7 @@ func New(creator ReminderCreator) *Reminder {
|
|||
}
|
||||
|
||||
// OnMessage processes incoming messages
|
||||
func (r *Reminder) OnMessage(msg *model.Message, config map[string]interface{}) []*model.Message {
|
||||
func (r *Reminder) OnMessage(msg *model.Message, config map[string]interface{}) []*model.MessageAction {
|
||||
// Only process replies to messages
|
||||
if msg.ReplyTo == "" {
|
||||
return nil
|
||||
|
@ -56,15 +56,22 @@ func (r *Reminder) OnMessage(msg *model.Message, config map[string]interface{})
|
|||
// Parse the duration
|
||||
amount, err := strconv.Atoi(match[1])
|
||||
if err != nil {
|
||||
return []*model.Message{
|
||||
errorMsg := &model.Message{
|
||||
Text: "Invalid duration format. Please use a number followed by y (years), mo (months), d (days), h (hours), m (minutes), or s (seconds).",
|
||||
Chat: msg.Chat,
|
||||
Channel: msg.Channel,
|
||||
Author: "bot",
|
||||
FromBot: true,
|
||||
Date: time.Now(),
|
||||
ReplyTo: msg.ID,
|
||||
}
|
||||
|
||||
return []*model.MessageAction{
|
||||
{
|
||||
Text: "Invalid duration format. Please use a number followed by y (years), mo (months), d (days), h (hours), m (minutes), or s (seconds).",
|
||||
Type: model.ActionSendMessage,
|
||||
Message: errorMsg,
|
||||
Chat: msg.Chat,
|
||||
Channel: msg.Channel,
|
||||
Author: "bot",
|
||||
FromBot: true,
|
||||
Date: time.Now(),
|
||||
ReplyTo: msg.ID,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -86,15 +93,22 @@ func (r *Reminder) OnMessage(msg *model.Message, config map[string]interface{})
|
|||
case "s":
|
||||
duration = time.Duration(amount) * time.Second
|
||||
default:
|
||||
return []*model.Message{
|
||||
errorMsg := &model.Message{
|
||||
Text: "Invalid duration unit. Please use y (years), mo (months), d (days), h (hours), m (minutes), or s (seconds).",
|
||||
Chat: msg.Chat,
|
||||
Channel: msg.Channel,
|
||||
Author: "bot",
|
||||
FromBot: true,
|
||||
Date: time.Now(),
|
||||
ReplyTo: msg.ID,
|
||||
}
|
||||
|
||||
return []*model.MessageAction{
|
||||
{
|
||||
Text: "Invalid duration unit. Please use y (years), mo (months), d (days), h (hours), m (minutes), or s (seconds).",
|
||||
Type: model.ActionSendMessage,
|
||||
Message: errorMsg,
|
||||
Chat: msg.Chat,
|
||||
Channel: msg.Channel,
|
||||
Author: "bot",
|
||||
FromBot: true,
|
||||
Date: time.Now(),
|
||||
ReplyTo: msg.ID,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -127,15 +141,22 @@ func (r *Reminder) OnMessage(msg *model.Message, config map[string]interface{})
|
|||
)
|
||||
|
||||
if err != nil {
|
||||
return []*model.Message{
|
||||
errorMsg := &model.Message{
|
||||
Text: fmt.Sprintf("Failed to create reminder: %v", err),
|
||||
Chat: msg.Chat,
|
||||
Channel: msg.Channel,
|
||||
Author: "bot",
|
||||
FromBot: true,
|
||||
Date: time.Now(),
|
||||
ReplyTo: msg.ID,
|
||||
}
|
||||
|
||||
return []*model.MessageAction{
|
||||
{
|
||||
Text: fmt.Sprintf("Failed to create reminder: %v", err),
|
||||
Type: model.ActionSendMessage,
|
||||
Message: errorMsg,
|
||||
Chat: msg.Chat,
|
||||
Channel: msg.Channel,
|
||||
Author: "bot",
|
||||
FromBot: true,
|
||||
Date: time.Now(),
|
||||
ReplyTo: msg.ID,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -157,15 +178,23 @@ func (r *Reminder) OnMessage(msg *model.Message, config map[string]interface{})
|
|||
confirmText = fmt.Sprintf("I'll remind you about this message in %d second(s)", amount)
|
||||
}
|
||||
|
||||
return []*model.Message{
|
||||
// Create confirmation message
|
||||
confirmMsg := &model.Message{
|
||||
Text: confirmText,
|
||||
Chat: msg.Chat,
|
||||
Channel: msg.Channel,
|
||||
Author: "bot",
|
||||
FromBot: true,
|
||||
Date: time.Now(),
|
||||
ReplyTo: msg.ID,
|
||||
}
|
||||
|
||||
return []*model.MessageAction{
|
||||
{
|
||||
Text: confirmText,
|
||||
Type: model.ActionSendMessage,
|
||||
Message: confirmMsg,
|
||||
Chat: msg.Chat,
|
||||
Channel: msg.Channel,
|
||||
Author: "bot",
|
||||
FromBot: true,
|
||||
Date: time.Now(),
|
||||
ReplyTo: msg.ID,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
|
@ -142,14 +142,25 @@ func TestReminderOnMessage(t *testing.T) {
|
|||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
initialCount := len(creator.reminders)
|
||||
responses := plugin.OnMessage(tt.message, nil)
|
||||
actions := plugin.OnMessage(tt.message, nil)
|
||||
|
||||
if tt.expectResponse && len(responses) == 0 {
|
||||
t.Errorf("Expected response, but got none")
|
||||
if tt.expectResponse && len(actions) == 0 {
|
||||
t.Errorf("Expected response action, but got none")
|
||||
}
|
||||
|
||||
if !tt.expectResponse && len(responses) > 0 {
|
||||
t.Errorf("Expected no response, but got %d", len(responses))
|
||||
if !tt.expectResponse && len(actions) > 0 {
|
||||
t.Errorf("Expected no actions, but got %d", len(actions))
|
||||
}
|
||||
|
||||
// Verify action type is correct when actions are returned
|
||||
if len(actions) > 0 {
|
||||
if actions[0].Type != model.ActionSendMessage {
|
||||
t.Errorf("Expected action type to be %s, but got %s", model.ActionSendMessage, actions[0].Type)
|
||||
}
|
||||
|
||||
if actions[0].Message == nil {
|
||||
t.Errorf("Expected message in action to not be nil")
|
||||
}
|
||||
}
|
||||
|
||||
if tt.expectReminder && len(creator.reminders) != initialCount+1 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue