-
Notifications
You must be signed in to change notification settings - Fork 103
/
channel_hooks.go
95 lines (80 loc) · 2.83 KB
/
channel_hooks.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
package main
import (
"fmt"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/plugin"
)
// ChannelHasBeenCreated is invoked after the channel has been committed to the database.
//
// This demo implementation logs a message to the demo channel whenever a channel is created.
func (p *Plugin) ChannelHasBeenCreated(c *plugin.Context, channel *model.Channel) {
configuration := p.getConfiguration()
if configuration.disabled {
return
}
msg := fmt.Sprintf("ChannelHasBeenCreated: ~%s", channel.Name)
if err := p.postPluginMessage(channel.TeamId, msg); err != nil {
p.API.LogError(
"failed to post ChannelHasBeenCreated message",
"channel_id", channel.Id,
"error", err.Error(),
)
}
}
// UserHasJoinedChannel is invoked after the membership has been committed to the database. If
// actor is not nil, the user was invited to the channel by the actor.
//
// This demo implementation logs a message to the demo channel whenever a user joins a channel.
func (p *Plugin) UserHasJoinedChannel(c *plugin.Context, channelMember *model.ChannelMember, actor *model.User) {
configuration := p.getConfiguration()
if configuration.disabled {
return
}
user, err := p.API.GetUser(channelMember.UserId)
if err != nil {
p.API.LogError("failed to query user", "user_id", channelMember.UserId)
return
}
channel, err := p.API.GetChannel(channelMember.ChannelId)
if err != nil {
p.API.LogError("failed to query channel", "channel_id", channelMember.ChannelId)
return
}
msg := fmt.Sprintf("UserHasJoinedChannel: @%s, ~%s", user.Username, channel.Name)
if err := p.postPluginMessage(channel.TeamId, msg); err != nil {
p.API.LogError(
"failed to post UserHasJoinedChannel message",
"user_id", channelMember.UserId,
"error", err.Error(),
)
}
}
// UserHasLeftChannel is invoked after the membership has been removed from the database. If
// actor is not nil, the user was removed from the channel by the actor.
//
// This demo implementation logs a message to the demo channel whenever a user leaves a
// channel.
func (p *Plugin) UserHasLeftChannel(c *plugin.Context, channelMember *model.ChannelMember, actor *model.User) {
configuration := p.getConfiguration()
if configuration.disabled {
return
}
user, err := p.API.GetUser(channelMember.UserId)
if err != nil {
p.API.LogError("failed to query user", "user_id", channelMember.UserId)
return
}
channel, err := p.API.GetChannel(channelMember.ChannelId)
if err != nil {
p.API.LogError("failed to query channel", "channel_id", channelMember.ChannelId)
return
}
msg := fmt.Sprintf("UserHasLeftChannel: @%s, ~%s", user.Username, channel.Name)
if err := p.postPluginMessage(channel.TeamId, msg); err != nil {
p.API.LogError(
"failed to post UserHasLeftChannel message",
"user_id", channelMember.UserId,
"error", err.Error(),
)
}
}