Skip to content
This repository has been archived by the owner on Dec 6, 2023. It is now read-only.

Commit

Permalink
convert missing target into 'me'. Fixies Issue #118
Browse files Browse the repository at this point in the history
  • Loading branch information
scottleedavis committed Jun 7, 2019
1 parent f13d07b commit 12dcaab
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 44 deletions.
4 changes: 2 additions & 2 deletions plugin.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"id": "com.github.scottleedavis.mattermost-plugin-remind",
"name": "Remind Bot Mattermost Plugin",
"name": "Remind Bot Plugin",
"description": "Sets Reminders for users and channels",
"version": "0.3.1",
"version": "0.3.2",
"server": {
"executables": {
"linux-amd64": "server/dist/plugin-linux-amd64",
Expand Down
55 changes: 19 additions & 36 deletions server/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,37 +62,6 @@ func (p *Plugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*mo
return &model.CommandResponse{}, nil
}

payload := strings.Trim(strings.Replace(command, "/"+CommandTrigger, "", -1), " ")

if strings.HasPrefix(payload, T("me")) ||
strings.HasPrefix(payload, "@") ||
strings.HasPrefix(payload, "~") {

request := ReminderRequest{
TeamId: args.TeamId,
Username: user.Username,
Payload: payload,
Reminder: Reminder{},
}
reminder, err := p.ScheduleReminder(&request, args.ChannelId)

if err != nil {
post := model.Post{
ChannelId: args.ChannelId,
UserId: p.remindUserId,
Message: T("exception.response"),
}
p.API.SendEphemeralPost(user.Id, &post)
return &model.CommandResponse{}, nil
}

p.API.SendEphemeralPost(user.Id, reminder)
return &model.CommandResponse{}, nil

}

// debug & troubleshooting commands

// clear all reminders for current user
if strings.HasSuffix(command, "__clear") {
post := model.Post{
Expand Down Expand Up @@ -126,12 +95,26 @@ func (p *Plugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*mo
return &model.CommandResponse{}, nil
}

post := model.Post{
ChannelId: args.ChannelId,
UserId: p.remindUserId,
Message: T("exception.response"),
payload := strings.Trim(strings.Replace(command, "/"+CommandTrigger, "", -1), " ")
request := ReminderRequest{
TeamId: args.TeamId,
Username: user.Username,
Payload: payload,
Reminder: Reminder{},
}
reminder, err := p.ScheduleReminder(&request, args.ChannelId)

if err != nil {
post := model.Post{
ChannelId: args.ChannelId,
UserId: p.remindUserId,
Message: T("exception.response"),
}
p.API.SendEphemeralPost(user.Id, &post)
return &model.CommandResponse{}, nil
}
p.API.SendEphemeralPost(user.Id, &post)

p.API.SendEphemeralPost(user.Id, reminder)
return &model.CommandResponse{}, nil

}
2 changes: 1 addition & 1 deletion server/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ var manifest = struct {
Version string
}{
Id: "com.github.scottleedavis.mattermost-plugin-remind",
Version: "0.3.1",
Version: "0.3.2",
}
44 changes: 41 additions & 3 deletions server/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,47 @@ func (p *Plugin) ParseRequest(request *ReminderRequest) error {
message := request.Payload[firstIndex : lastIndex+1]

when := strings.Replace(request.Payload, message, "", -1)
when = strings.Replace(when, commandSplit[0], "", 1)
when = strings.Replace(when, request.Reminder.Target, "", 1)
when = strings.Trim(when, " ")

message = strings.Replace(message, "\"", "", -1)

request.Reminder.When = when
request.Reminder.Message = message
return nil
}

if wErr := p.findWhen(request); wErr != nil {
return wErr
}

toIndex := strings.Index(request.Reminder.When, T("to")+" ")
if toIndex > -1 {
request.Reminder.When = request.Reminder.When[0:toIndex]
}

message := strings.Replace(request.Payload, request.Reminder.When, "", -1)
message = strings.Replace(message, request.Reminder.Target, "", 1)
message = strings.Trim(message, " \"")

if message == "" {
return errors.New("no message parsed")
}
request.Reminder.Message = message

return nil

} else {
request.Reminder.Target = T("me")

firstIndex := strings.Index(request.Payload, "\"")
lastIndex := strings.LastIndex(request.Payload, "\"")

if firstIndex > -1 && lastIndex > -1 && firstIndex != lastIndex { // has quotes

message := request.Payload[firstIndex : lastIndex+1]

when := strings.Replace(request.Payload, message, "", -1)
when = strings.Trim(when, " ")

message = strings.Replace(message, "\"", "", -1)
Expand All @@ -55,7 +95,6 @@ func (p *Plugin) ParseRequest(request *ReminderRequest) error {
}

message := strings.Replace(request.Payload, request.Reminder.When, "", -1)
message = strings.Replace(message, commandSplit[0], "", 1)
message = strings.Trim(message, " \"")

if message == "" {
Expand All @@ -67,7 +106,6 @@ func (p *Plugin) ParseRequest(request *ReminderRequest) error {

}

return errors.New("unrecognized target")
}

func (p *Plugin) findWhen(request *ReminderRequest) error {
Expand Down
54 changes: 52 additions & 2 deletions server/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,32 @@ func TestParseRequest(t *testing.T) {
},
}

assert.NotNil(t, p.ParseRequest(request))
assert.Nil(t, p.ParseRequest(request))
})

t.Run("if no quotes with target", func(t *testing.T) {
api := setupAPI()
defer api.AssertExpectations(t)

p := &Plugin{}
p.API = api

request := &ReminderRequest{
TeamId: model.NewId(),
Username: user.Username,
Payload: "me Hello in one minute",
Reminder: Reminder{
Id: model.NewId(),
TeamId: model.NewId(),
Username: user.Username,
Message: "Hello",
Completed: p.emptyTime,
Target: "me",
When: "in one minute",
},
}

assert.Nil(t, p.ParseRequest(request))
})

t.Run("if with quotes", func(t *testing.T) {
Expand All @@ -76,7 +101,32 @@ func TestParseRequest(t *testing.T) {
},
}

assert.NotNil(t, p.ParseRequest(request))
assert.Nil(t, p.ParseRequest(request))
})

t.Run("if with quotes with target", func(t *testing.T) {
api := setupAPI()
defer api.AssertExpectations(t)

p := &Plugin{}
p.API = api

request := &ReminderRequest{
TeamId: model.NewId(),
Username: user.Username,
Payload: "me \"Hello\" in one minute",
Reminder: Reminder{
Id: model.NewId(),
TeamId: model.NewId(),
Username: user.Username,
Message: "Hello",
Completed: p.emptyTime,
Target: "me",
When: "in one minute",
},
}

assert.Nil(t, p.ParseRequest(request))
})
}

Expand Down

0 comments on commit 12dcaab

Please sign in to comment.