Skip to content

Commit

Permalink
feat: Add support for automatically updating users
Browse files Browse the repository at this point in the history
- Although there is a `user_updated` event we can bind to, it fires
  whenever a user updates their status, which might be overkill.
  Instead, we'll automatically update users every four hours.
  • Loading branch information
jordanleven committed Mar 10, 2021
1 parent 5b06a74 commit 9310c27
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
4 changes: 2 additions & 2 deletions reactionbot/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ func getReactionItem(reactionEvent *slackevents.ReactionAddedEvent) slackevents.
// PostReactedMessageToChannel is the function used to post a reaction
func (bot ReactionBot) PostReactedMessageToChannel(reactionEvent *slackevents.ReactionAddedEvent) (channelID string, timetamp string, error error) {
allUsers := bot.Users
reactedByUser := GetUserByUserID(allUsers, reactionEvent.User)
reactedByUser := GetUserByUserID(*allUsers, reactionEvent.User)
reactedByName := reactedByUser.DisplayName
reactedToUser := GetUserByUserID(allUsers, reactionEvent.ItemUser)
reactedToUser := GetUserByUserID(*allUsers, reactionEvent.ItemUser)
reactedToName := reactedToUser.Username

reactionEmoji := getReactionEmoji(reactionEvent)
Expand Down
21 changes: 19 additions & 2 deletions reactionbot/reactionbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ package reactionbot
import (
"log"
"os"
"time"

"github.com/fatih/color"
"github.com/slack-go/slack"
)

const refreshIntervalInHours = 4

// ReactionBot is our main structure
type ReactionBot struct {
Slack *slack.Client
IsDevelopment bool
RegisteredEmoji RegisteredReactions
Users SlackUsers
Users *SlackUsers
}

// RegistrationOptions the list of options to init the package
Expand All @@ -31,6 +35,17 @@ func getSlackInstance(options RegistrationOptions) *slack.Client {
)
}

func (bot *ReactionBot) setUpdateTicker() {
ticker := time.NewTicker(time.Hour * refreshIntervalInHours)
go func() {
for range ticker.C {
color.White("Updating users...")
slackUsers := GetSlackWorkspaceUsers(bot.Slack)
*bot.Users = *slackUsers
}
}()
}

func getReactionBot(options RegistrationOptions) ReactionBot {
slack := getSlackInstance(options)
slackUsers := GetSlackWorkspaceUsers(slack)
Expand All @@ -40,11 +55,13 @@ func getReactionBot(options RegistrationOptions) ReactionBot {
RegisteredEmoji: options.RegisteredEmoji,
Users: slackUsers,
}

return bot
}

// New function to init the package
func New(options RegistrationOptions) {
bot := getReactionBot(options)
RegisterSlackBot(bot)
go bot.setUpdateTicker()
bot.RegisterSlackBot()
}
2 changes: 1 addition & 1 deletion reactionbot/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// RegisterSlackBot is the function used to start the bot and listen for reactions
func RegisterSlackBot(bot ReactionBot) {
func (bot ReactionBot) RegisterSlackBot() {

client := socketmode.New(bot.Slack)

Expand Down
4 changes: 2 additions & 2 deletions reactionbot/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func GetUserByUserID(users SlackUsers, userID string) SlackUser {

// GetSlackWorkspaceUsers is a function to return all
// users of the workspace
func GetSlackWorkspaceUsers(slackInstance *slack.Client) SlackUsers {
func GetSlackWorkspaceUsers(slackInstance *slack.Client) *SlackUsers {
users, err := slackInstance.GetUsers()
if err != nil {
fmt.Printf("%s\n", err)
Expand All @@ -50,5 +50,5 @@ func GetSlackWorkspaceUsers(slackInstance *slack.Client) SlackUsers {
}
}

return userDictionary
return &userDictionary
}

0 comments on commit 9310c27

Please sign in to comment.