Skip to content

Commit

Permalink
[GH-47] [FIX] Agenda item number should get updated if the items of t…
Browse files Browse the repository at this point in the history
…he agenda are deleted (#90)
  • Loading branch information
sanjaydemansol authored Dec 15, 2021
1 parent 6b914f0 commit bc0e65d
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 11 deletions.
64 changes: 53 additions & 11 deletions server/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"regexp"
"strings"
"time"

Expand All @@ -16,6 +17,13 @@ const (
wsEventList = "list"
)

// ParsedMeetingMessage is meeting message after being parsed
type ParsedMeetingMessage struct {
date string
number string
textMessage string
}

const helpCommandText = "###### Mattermost Agenda Plugin - Slash Command Help\n" +
"The Agenda plugin lets you queue up meeting topics for channel discussion at a later time. When your meeting happens, you can click on the Hashtag to see all agenda items in the RHS. \n" +
"To configure the agenda for this channel, click on the Channel Name in Mattermost to access the channel options menu and select `Agenda Settings`" +
Expand Down Expand Up @@ -132,6 +140,11 @@ func (p *Plugin) executeCommandQueue(args *model.CommandArgs) *model.CommandResp
return responsef("Missing parameters for queue command")
}

meeting, err := p.GetMeeting(args.ChannelId)
if err != nil {
p.API.LogError("failed to get meeting for channel", "err", err.Error(), "channel_id", args.ChannelId)
}

nextWeek := false
weekday := -1
message := strings.Join(split[2:], " ")
Expand All @@ -147,25 +160,21 @@ func (p *Plugin) executeCommandQueue(args *model.CommandArgs) *model.CommandResp
message = strings.Join(split[3:], " ")
}

hashtag, error := p.GenerateHashtag(args.ChannelId, nextWeek, weekday)
if error != nil {
hashtag, err := p.GenerateHashtag(args.ChannelId, nextWeek, weekday)
if err != nil {
return responsef("Error calculating hashtags. Check the meeting settings for this channel.")
}

searchResults, appErr := p.API.SearchPostsInTeamForUser(args.TeamId, args.UserId, model.SearchParameter{Terms: &hashtag})

if appErr != nil {
return responsef("Error calculating list number")
numQueueItems, itemErr := p.calculateQueueItemNumberAndUpdateOldItems(meeting, args, hashtag)
if itemErr != nil {
return responsef(itemErr.Error())
}

postList := *searchResults.PostList
numQueueItems := len(postList.Posts)

_, appErr = p.API.CreatePost(&model.Post{
_, appErr := p.API.CreatePost(&model.Post{
UserId: args.UserId,
ChannelId: args.ChannelId,
RootId: args.RootId,
Message: fmt.Sprintf("#### %v %v) %v", hashtag, numQueueItems+1, message),
Message: fmt.Sprintf("#### %v %v) %v", hashtag, numQueueItems, message),
})
if appErr != nil {
return responsef("Error creating post: %s", appErr.Message)
Expand All @@ -174,6 +183,39 @@ func (p *Plugin) executeCommandQueue(args *model.CommandArgs) *model.CommandResp
return &model.CommandResponse{}
}

func parseMeetingPost(meeting *Meeting, post *model.Post) (string, ParsedMeetingMessage, error) {
var (
prefix string
hashtagDateFormat string
)
if matchGroups := meetingDateFormatRegex.FindStringSubmatch(meeting.HashtagFormat); len(matchGroups) == 4 {
prefix = matchGroups[1]
hashtagDateFormat = strings.TrimSpace(matchGroups[2])
} else {
return "", ParsedMeetingMessage{}, errors.New("error Parsing meeting post")
}

var (
messageRegexFormat, err = regexp.Compile(fmt.Sprintf(`(?m)^#### #%s(?P<date>.*) ([0-9]+)\) (?P<message>.*)?$`, prefix))
)

if err != nil {
return "", ParsedMeetingMessage{}, err
}
matchGroups := messageRegexFormat.FindStringSubmatch(post.Message)
if len(matchGroups) == 4 {
parsedMeetingMessage := ParsedMeetingMessage{
date: matchGroups[1],
number: matchGroups[2],
textMessage: matchGroups[3],
}

return hashtagDateFormat, parsedMeetingMessage, nil
}

return hashtagDateFormat, ParsedMeetingMessage{}, errors.New("failed to parse meeting post's header")
}

func (p *Plugin) executeCommandHelp(args *model.CommandArgs) *model.CommandResponse {
return responsef(helpCommandText)
}
Expand Down
51 changes: 51 additions & 0 deletions server/meeting.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import (
"encoding/json"
"fmt"
"regexp"
"sort"
"strings"
"time"

"github.com/mattermost/mattermost-server/v5/model"
"github.com/pkg/errors"
)

var (
Expand Down Expand Up @@ -61,6 +65,53 @@ func (p *Plugin) SaveMeeting(meeting *Meeting) error {
return nil
}

func (p *Plugin) calculateQueueItemNumberAndUpdateOldItems(meeting *Meeting, args *model.CommandArgs, hashtag string) (int, error) {
c, appErr := p.API.GetChannel(args.ChannelId)
if appErr != nil {
return 0, appErr
}
terms := fmt.Sprintf("in:%s %s", c.Name, hashtag)
searchResults, appErr := p.API.SearchPostsInTeamForUser(args.TeamId, args.UserId, model.SearchParameter{Terms: &terms})
if appErr != nil {
return 0, errors.Wrap(appErr, "Error searching posts to find hashtags")
}

counter := 1

var sortedPosts []*model.Post
// TODO we won't need to do this once we fix https://github.com/mattermost/mattermost-server/issues/11006
for _, post := range searchResults.PostList.Posts {
sortedPosts = append(sortedPosts, post)
}

sort.Slice(sortedPosts, func(i, j int) bool {
return sortedPosts[i].CreateAt < sortedPosts[j].CreateAt
})

for _, post := range sortedPosts {
_, parsedMessage, err := parseMeetingPost(meeting, post)
if err != nil {
p.API.LogDebug(err.Error())
return 0, errors.New(err.Error())
}

_, updateErr := p.API.UpdatePost(&model.Post{
Id: post.Id,
UserId: post.UserId,
ChannelId: post.ChannelId,
RootId: post.RootId,
Message: fmt.Sprintf("#### %v %v) %v", hashtag, counter, parsedMessage.textMessage),
})
if updateErr != nil {
return 0, errors.Wrap(updateErr, "Error updating post")
}

counter++
}

return counter, nil
}

// GenerateHashtag returns a meeting hashtag
func (p *Plugin) GenerateHashtag(channelID string, nextWeek bool, weekday int) (string, error) {
meeting, err := p.GetMeeting(channelID)
Expand Down

0 comments on commit bc0e65d

Please sign in to comment.