-
Notifications
You must be signed in to change notification settings - Fork 13
/
command_copy_thread.go
121 lines (102 loc) · 4.2 KB
/
command_copy_thread.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"fmt"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/pkg/errors"
)
const copyThreadUsage = `/wrangler copy thread [MESSAGE_ID] [CHANNEL_ID]
Copy a given message, along with the thread it belongs to, to a given channel
- This can be on any channel in any team that you have joined
- Obtain the message ID by running '/wrangler list messages' or via the 'Permalink' message dropdown option (it's the last part of the URL)
- Obtain the channel ID by running '/wrangler list channels' or via the channel 'View Info' option`
func getCopyThreadMessage() string {
return codeBlock(fmt.Sprintf("`Error: missing arguments\n\n%s", copyThreadUsage))
}
func (p *Plugin) runCopyThreadCommand(args []string, extra *model.CommandArgs) (*model.CommandResponse, bool, error) {
if len(args) < 2 {
return getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, getCopyThreadMessage()), true, nil
}
postID := args[0]
channelID := args[1]
postListResponse, appErr := p.API.GetPostThread(postID)
if appErr != nil {
return getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, fmt.Sprintf("Error: unable to get post with ID %s; ensure this is correct", postID)), true, nil
}
wpl := buildWranglerPostList(postListResponse)
originalChannel, appErr := p.API.GetChannel(extra.ChannelId)
if appErr != nil {
return nil, false, fmt.Errorf("unable to get channel with ID %s", extra.ChannelId)
}
_, appErr = p.API.GetChannelMember(channelID, extra.UserId)
if appErr != nil {
return getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, fmt.Sprintf("Error: channel with ID %s doesn't exist or you are not a member", channelID)), true, nil
}
targetChannel, appErr := p.API.GetChannel(channelID)
if appErr != nil {
return nil, false, fmt.Errorf("unable to get channel with ID %s", channelID)
}
response, userErr, err := p.validateMoveOrCopy(wpl, originalChannel, targetChannel, extra)
if response != nil || err != nil {
return response, userErr, err
}
targetTeam, appErr := p.API.GetTeam(targetChannel.TeamId)
if appErr != nil {
return nil, false, fmt.Errorf("unable to get team with ID %s", targetChannel.TeamId)
}
p.API.LogInfo("Wrangler is copying a thread",
"user_id", extra.UserId,
"original_post_id", wpl.RootPost().Id,
"original_channel_id", originalChannel.Id,
)
newRootPost, err := p.copyWranglerPostlist(wpl, targetChannel)
if err != nil {
return nil, false, err
}
_, appErr = p.API.CreatePost(&model.Post{
UserId: p.BotUserID,
RootId: newRootPost.Id,
ParentId: newRootPost.Id,
ChannelId: targetChannel.Id,
Message: "This thread was copied from another channel",
})
if appErr != nil {
return nil, false, errors.Wrap(appErr, "unable to create new bot post")
}
newPostLink := makePostLink(*p.API.GetConfig().ServiceSettings.SiteURL, targetTeam.Name, newRootPost.Id)
_, appErr = p.API.CreatePost(&model.Post{
UserId: p.BotUserID,
RootId: wpl.RootPost().Id,
ParentId: wpl.RootPost().Id,
ChannelId: originalChannel.Id,
Message: fmt.Sprintf("A copy of this thread has been made: %s", newPostLink),
})
if appErr != nil {
return nil, false, errors.Wrap(appErr, "unable to create new bot post")
}
p.API.LogInfo("Wrangler thread copy complete",
"user_id", extra.UserId,
"new_post_id", newRootPost.Id,
"new_channel_id", channelID,
)
executor, execError := p.API.GetUser(extra.UserId)
if execError != nil {
return nil, false, errors.Wrap(appErr, "unable to find executor")
}
if extra.UserId != wpl.RootPost().UserId {
// The wrangled thread was not started by the user running the command.
// Send a DM to the user who created the root message to let them know.
err := p.postCopyThreadBotDM(wpl.RootPost().UserId, newPostLink, executor.Username)
if err != nil {
p.API.LogError("Unable to send copy-thread DM to user",
"error", err.Error(),
"user_id", wpl.RootPost().UserId,
)
}
}
return getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, "Thread copy complete"), false, nil
}
func (p *Plugin) postCopyThreadBotDM(userID, newPostLink, executor string) error {
config := p.getConfiguration()
message := makeBotDM(config.CopyThreadMessage, newPostLink, executor)
return p.PostBotDM(userID, message)
}