-
Notifications
You must be signed in to change notification settings - Fork 17
/
team_join.go
94 lines (74 loc) · 2.14 KB
/
team_join.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
package handler
import (
"fmt"
"github.com/gobridge/gopherbot/mparser"
"github.com/gobridge/gopherbot/workqueue"
"github.com/rs/zerolog"
"github.com/slack-go/slack"
)
// TeamJoiner is the interface to represent an incoming team join event.
type TeamJoiner interface {
User() slack.User
}
type teamJoiner slack.User
func (j teamJoiner) User() slack.User { return slack.User(j) }
// TeamJoinActionFn is a function for handlers to take actions against team join
// events.
type TeamJoinActionFn func(ctx workqueue.Context, tj TeamJoiner, r Responder) error
type teamJoinAction struct {
name string
fn TeamJoinActionFn
}
// TeamJoinActions represents actions to be taken on a team join event.
type TeamJoinActions struct {
shadow bool
actions []teamJoinAction
l zerolog.Logger
}
// NewTeamJoinActions returns a TeamJoinActions for use.
func NewTeamJoinActions(shadowMode bool, l zerolog.Logger) *TeamJoinActions {
return &TeamJoinActions{shadow: shadowMode, l: l}
}
// Handler satisfies workqueue.TeamJoinHandler.
func (t *TeamJoinActions) Handler(ctx workqueue.Context, tj *slack.TeamJoinEvent) (bool, bool, error) {
j := teamJoiner(tj.User)
mention := mparser.Mention{
Type: mparser.TypeUser,
ID: j.ID,
}
msg := NewMessage(j.ID, "im", j.ID, "", "", "", "", nil)
msg.allMentions = []mparser.Mention{mention}
msg.userMentions = []mparser.Mention{mention}
resp := response{
sc: ctx.Slack(),
m: msg,
}
var someWorked bool
for _, a := range t.actions {
if t.shadow {
t.l.Info().
Str("user_id", tj.User.ID).
Bool("shadow_mode", true).
Msg("would welcome user")
continue
}
err := a.fn(ctx, j, resp)
if err != nil {
if someWorked {
t.l.Error().
Err(err).
Str("join_action", a.name).
Msg("failed to take action")
return false, false, nil
}
// force a retry
return true, false, fmt.Errorf("failed to take join action: %w", err)
}
someWorked = true
}
return false, false, nil
}
// Handle registers a TeamJoinActionFn to be taken on new join events.
func (t *TeamJoinActions) Handle(name string, fn TeamJoinActionFn) {
t.actions = append(t.actions, teamJoinAction{name, fn})
}