Skip to content

Commit

Permalink
[MM-46]: Updated the message for exporting channels (#48)
Browse files Browse the repository at this point in the history
* [MM-46]: Updated the msg for exporting channels

* [MM-46]: Updated the msg for GM channel export

* [MM-46]: minor review fixes

* [MM-46]: Fixed ci

* [MM-46]: fixed ci and testcases

* [GH-46] Fix issue reported by QA

---------

Co-authored-by: raghavaggarwal2308 <raghav.aggarwal@brightscout.com>
  • Loading branch information
Kshitij-Katiyar and raghavaggarwal2308 authored Sep 19, 2024
1 parent 3c052b6 commit 3f0b671
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 11 deletions.
64 changes: 58 additions & 6 deletions server/command_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,59 @@ func (p *Plugin) executeCommandExport(args *model.CommandArgs) *model.CommandRes
}
}

var channelToExportName, exportSuccessMsg, exportProcessMsg string
switch channelToExport.Type {
case model.ChannelTypeOpen, model.ChannelTypePrivate:
channelToExportName = channelToExport.Name
exportSuccessMsg = fmt.Sprintf("Channel ~%s exported:", channelToExportName)
exportProcessMsg = fmt.Sprintf("Exporting ~%s. @%s will send you a direct message when the export is ready.", channelToExportName, botUsername)
case model.ChannelTypeGroup:
channelToExportNameID := channelToExport.Name
groupChannelMembers := strings.Split(channelToExport.DisplayName, ", ")
membersCount := len(groupChannelMembers)

for i := 0; i < 2 && i < membersCount; i++ {
channelToExportName += groupChannelMembers[i] + ", "
}

if membersCount > 2 {
channelToExportName += fmt.Sprintf("and %d more", (membersCount - 2))
}

team, appErr := p.API.GetTeam(args.TeamId)
if appErr != nil {
p.client.Log.Error("error occurred while getting the details of team for the channel.", "GMChannelID", args.ChannelId, "TeamID", args.TeamId, "Error", appErr)

exportProcessMsg = fmt.Sprintf("Exporting GM channel ~%s. @%s will send you a direct message when the export is ready.", channelToExportName, botUsername)
exportSuccessMsg = fmt.Sprintf("GM Channel ~%s exported:", channelToExportName)
} else {
link := fmt.Sprintf("%s/%s/messages/%s", args.SiteURL, team.Name, channelToExportNameID)
exportProcessMsg = fmt.Sprintf("Exporting GM channel ~[%s](%s). @%s will send you a direct message when the export is ready.", channelToExportName, link, botUsername)
exportSuccessMsg = fmt.Sprintf("GM Channel ~[%s](%s) exported:", channelToExportName, link)
}
case model.ChannelTypeDirect:
var DMUserID string
userIDs := strings.Split(channelToExport.Name, "__")
if userIDs[0] == args.UserId {
DMUserID = userIDs[1]
} else {
DMUserID = userIDs[0]
}

var user *model.User
user, err = p.client.User.Get(DMUserID)
if err != nil {
p.client.Log.Error("error occurred while getting the details of user for the DM.", "DMChannelID", args.ChannelId, "TeamID", args.TeamId, "Error", err)

exportSuccessMsg = fmt.Sprintf("DM %s exported:", channelToExportName)
exportProcessMsg = fmt.Sprintf("Exporting the DM ~%s. @%s will send you a direct message when the export is ready.", channelToExportName, botUsername)
} else {
channelToExportName = user.Username
exportSuccessMsg = fmt.Sprintf("DM with @%s exported:", channelToExportName)
exportProcessMsg = fmt.Sprintf("Exporting DM with @%s. @%s will send you a direct message when the export is ready.", channelToExportName, botUsername)
}
}

channelDM, err := p.client.Channel.GetDirect(args.UserId, p.botID)
if err != nil {
p.client.Log.Error("unable to create a direct message channel between the bot and the user",
Expand All @@ -100,7 +153,7 @@ func (p *Plugin) executeCommandExport(args *model.CommandArgs) *model.CommandRes
}

exporter := CSV{}
fileName := exporter.FileName(channelToExport.Name)
fileName := exporter.FileName(channelToExportName)

exportError := errors.New("failed to export channel")

Expand All @@ -125,7 +178,7 @@ func (p *Plugin) executeCommandExport(args *model.CommandArgs) *model.CommandRes
err = p.client.Post.CreatePost(&model.Post{
UserId: p.botID,
ChannelId: channelDM.Id,
Message: fmt.Sprintf("An error occurred exporting channel ~%s.", channelToExport.Name),
Message: fmt.Sprintf("An error occurred exporting channel ~%s.", channelToExportName),
})
if err != nil {
logger.WithError(err).Warn("failed to post message about failure to export channel")
Expand All @@ -148,7 +201,7 @@ func (p *Plugin) executeCommandExport(args *model.CommandArgs) *model.CommandRes
err = p.client.Post.CreatePost(&model.Post{
UserId: p.botID,
ChannelId: channelDM.Id,
Message: fmt.Sprintf("An error occurred uploading the exported channel ~%s.", channelToExport.Name),
Message: fmt.Sprintf("An error occurred uploading the exported channel ~%s.", channelToExportName),
})
if err != nil {
logger.WithError(err).Warn("failed to post message about failure to upload exported channel")
Expand All @@ -161,7 +214,7 @@ func (p *Plugin) executeCommandExport(args *model.CommandArgs) *model.CommandRes
err = p.client.Post.CreatePost(&model.Post{
UserId: p.botID,
ChannelId: channelDM.Id,
Message: fmt.Sprintf("Channel ~%s exported:", channelToExport.Name),
Message: exportSuccessMsg,
FileIds: []string{file.Id},
})
if err != nil {
Expand All @@ -177,8 +230,7 @@ func (p *Plugin) executeCommandExport(args *model.CommandArgs) *model.CommandRes

return &model.CommandResponse{
ResponseType: model.CommandResponseTypeEphemeral,
Text: fmt.Sprintf("Exporting ~%s. @%s will send you a direct message when the export is ready.",
channelToExport.Name, botUsername),
Text: exportProcessMsg,
}
}

Expand Down
10 changes: 5 additions & 5 deletions server/command_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestExecuteCommand(t *testing.T) {
EnableDeveloper: &trueValue,
},
}).Times(1)
mockChannel.EXPECT().Get("channel_id").Return(&model.Channel{Id: "channel_id", Name: "channel_name"}, nil)
mockChannel.EXPECT().Get("channel_id").Return(&model.Channel{Id: "channel_id", Name: "channel_name", Type: model.ChannelTypeOpen}, nil)
mockChannel.EXPECT().GetDirect("user_id", "bot_id").Return(&model.Channel{Id: "direct"}, nil)
mockUser.EXPECT().HasPermissionTo("user_id", model.PermissionManageSystem).Return(false).Times(1)
mockConfiguration.EXPECT().GetConfig().Return(&model.Config{
Expand Down Expand Up @@ -217,7 +217,7 @@ func TestExecuteCommand(t *testing.T) {
FutureFeatures: &trueValue,
}}).Times(2)
mockConfiguration.EXPECT().GetConfig().Return(&model.Config{}).Times(1)
mockChannel.EXPECT().Get("channel_id").Return(&model.Channel{Id: "channel_id"}, nil)
mockChannel.EXPECT().Get("channel_id").Return(&model.Channel{Id: "channel_id", Type: model.ChannelTypeOpen}, nil)
mockChannel.EXPECT().GetDirect("user_id", "bot_id").Return(nil, errors.New("failed"))
mockLog.EXPECT().Error("unable to create a direct message channel between the bot and the user", "Bot ID", "bot_id", "User ID", "user_id", "Error", gomock.Any())

Expand Down Expand Up @@ -255,7 +255,7 @@ func TestExecuteCommand(t *testing.T) {
FutureFeatures: &trueValue,
}}).Times(2)
mockConfiguration.EXPECT().GetConfig().Return(&model.Config{}).Times(1)
mockChannel.EXPECT().Get("channel_id").Return(&model.Channel{Id: "channel_id", Name: "channel_name"}, nil)
mockChannel.EXPECT().Get("channel_id").Return(&model.Channel{Id: "channel_id", Name: "channel_name", Type: model.ChannelTypeOpen}, nil)
mockChannel.EXPECT().GetDirect("user_id", "bot_id").Return(&model.Channel{Id: "direct"}, nil)
mockUser.EXPECT().HasPermissionTo("user_id", model.PermissionManageSystem).Return(false).Times(1)
mockConfiguration.EXPECT().GetConfig().Return(&model.Config{}).Times(1)
Expand Down Expand Up @@ -313,7 +313,7 @@ func TestExecuteCommand(t *testing.T) {
FutureFeatures: &trueValue,
}}).Times(2)
mockConfiguration.EXPECT().GetConfig().Return(&model.Config{}).Times(1)
mockChannel.EXPECT().Get("channel_id").Return(&model.Channel{Id: "channel_id", Name: "channel_name"}, nil)
mockChannel.EXPECT().Get("channel_id").Return(&model.Channel{Id: "channel_id", Name: "channel_name", Type: model.ChannelTypeOpen}, nil)
mockChannel.EXPECT().GetDirect("user_id", "bot_id").Return(&model.Channel{Id: "direct"}, nil)
mockUser.EXPECT().HasPermissionTo("user_id", model.PermissionManageSystem).Return(false).Times(1)
mockConfiguration.EXPECT().GetConfig().Return(&model.Config{
Expand Down Expand Up @@ -375,7 +375,7 @@ func TestExecuteCommand(t *testing.T) {
FutureFeatures: &trueValue,
}}).Times(2)
mockConfiguration.EXPECT().GetConfig().Return(&model.Config{}).Times(1)
mockChannel.EXPECT().Get("channel_id").Return(&model.Channel{Id: "channel_id", Name: "channel_name"}, nil)
mockChannel.EXPECT().Get("channel_id").Return(&model.Channel{Id: "channel_id", Name: "channel_name", Type: model.ChannelTypeOpen}, nil)
mockChannel.EXPECT().GetDirect("user_id", "bot_id").Return(&model.Channel{Id: "direct"}, nil)
mockUser.EXPECT().HasPermissionTo("user_id", model.PermissionManageSystem).Return(false).Times(1)
mockConfiguration.EXPECT().GetConfig().Return(&model.Config{
Expand Down

0 comments on commit 3f0b671

Please sign in to comment.