Skip to content

Commit

Permalink
Bump Slack API to 0.10.0. Better logging on fatal errors.
Browse files Browse the repository at this point in the history
Simplified the conversation marking code a bit too.
  • Loading branch information
nolanlum committed Dec 13, 2021
1 parent 3f58528 commit 6ae11a3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 58 deletions.
58 changes: 19 additions & 39 deletions gateway/conversation_marking.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,23 @@ func (cm *ConversationMarker) Reset() {
cm.conversationToReadCursorMap = make(map[string]string)
}

func (cm *ConversationMarker) markConversation(conversationID, timestamp string, mark func(string, string) error) {
cm.conversationToReadCursorMap[conversationID] = timestamp

go func() {
time.Sleep(5 * time.Second)

cm.Lock()
defer cm.Unlock()
if cm.conversationToReadCursorMap[conversationID] == timestamp {
err := mark(conversationID, timestamp)
if err != nil {
log.Printf("error while marking conversation %v: %v", conversationID, err)
func (cm *ConversationMarker) markConversationFunc(sc *slack.Client, conversationID string) func(string) {
return func(timestamp string) {
cm.conversationToReadCursorMap[conversationID] = timestamp

go func() {
time.Sleep(5 * time.Second)

cm.Lock()
defer cm.Unlock()
if cm.conversationToReadCursorMap[conversationID] == timestamp {
err := sc.MarkConversation(conversationID, timestamp)
if err != nil {
log.Printf("error while marking conversation %v: %v", conversationID, err)
}
}
}
}()
}()
}
}

// HandleRTMAck handles an ACK from the RTM channel, scheduling a read marker update if possible
Expand All @@ -62,32 +64,10 @@ func (cm *ConversationMarker) HandleRTMAck(messageID int, timestamp string) {
}
}

// MarkChannel prepares a channel marker to be updated upon receipt of an ack for the given message ID
func (cm *ConversationMarker) MarkChannel(sc *slack.Client, channelID string, messageID int) {
cm.Lock()
defer cm.Unlock()

cm.rtmIDToMarkFuncMap[messageID] = func(timestamp string) {
cm.markConversation(channelID, timestamp, sc.SetChannelReadMark)
}
}

// MarkGroup prepares a group (private channel) marker to be updated upon receipt of an ack for the given message ID
func (cm *ConversationMarker) MarkGroup(sc *slack.Client, groupID string, messageID int) {
cm.Lock()
defer cm.Unlock()

cm.rtmIDToMarkFuncMap[messageID] = func(timestamp string) {
cm.markConversation(groupID, timestamp, sc.SetGroupReadMark)
}
}

// MarkDM prepares a DM marker to be updated upon receipt of an ack for the given message ID
func (cm *ConversationMarker) MarkDM(sc *slack.Client, dmID string, messageID int) {
// MarkConversation prepares a conversation marker to be updated upon receipt of an ack for the given message ID
func (cm *ConversationMarker) MarkConversation(sc *slack.Client, conversationID string, messageID int) {
cm.Lock()
defer cm.Unlock()

cm.rtmIDToMarkFuncMap[messageID] = func(timestamp string) {
cm.markConversation(dmID, timestamp, sc.MarkIMChannel)
}
cm.rtmIDToMarkFuncMap[messageID] = cm.markConversationFunc(sc, conversationID)
}
22 changes: 8 additions & 14 deletions gateway/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (sc *SlackClient) bootstrapMappings() {

hasMore := true
gcp := &slack.GetConversationsParameters{
ExcludeArchived: "true",
ExcludeArchived: true,
Limit: 1000,
Types: []string{"public_channel", "private_channel"},
}
Expand All @@ -140,7 +140,7 @@ func (sc *SlackClient) bootstrapMappings() {
var err error
channels, gcp.Cursor, err = sc.client.GetConversations(gcp)
if err != nil {
log.Fatalln(err)
log.Fatalf("%s [fatal] slack:init %s err: %v", sc.Tag(), "GetConversations", err)
}

for _, channel := range channels {
Expand All @@ -157,7 +157,7 @@ func (sc *SlackClient) bootstrapMappings() {

users, err := sc.client.GetUsers()
if err != nil {
log.Fatalln(err)
log.Fatalf("%s [fatal] slack:init %s err: %v", sc.Tag(), "GetUsers", err)
}
for _, user := range users {
userInfo[user.ID] = slackUserFromDto(&user)
Expand All @@ -167,11 +167,11 @@ func (sc *SlackClient) bootstrapMappings() {
Cursor: "",
Types: []string{"im"},
Limit: 0,
ExcludeArchived: "true",
ExcludeArchived: true,
}
ims, _, err := sc.client.GetConversations(ucParams)
if err != nil {
log.Fatalln(err)
log.Fatalf("%s [fatal] slack:init %s err: %v", sc.Tag(), "GetConversations", err)
}
for _, im := range ims {
dmInfo[im.ID] = userInfo[im.User]
Expand Down Expand Up @@ -376,7 +376,7 @@ func (sc *SlackClient) ResolveDMToUser(dmChannelID string) (*SlackUser, error) {
Cursor: "",
Types: []string{"im"},
Limit: 0,
ExcludeArchived: "true",
ExcludeArchived: true,
}
ims, _, err := sc.client.GetConversations(ucParams)
if err != nil {
Expand Down Expand Up @@ -439,13 +439,7 @@ func (sc *SlackClient) Initialize(token string, debug bool) {
func (sc *SlackClient) SendMessage(channel *SlackChannel, msg string) error {
msg = sc.UnparseMessageText(msg)
outgoingMessage := sc.rtm.NewOutgoingMessage(msg, channel.SlackID)

if channel.Private {
sc.conversationMarker.MarkGroup(sc.client, channel.SlackID, outgoingMessage.ID)
} else {
sc.conversationMarker.MarkChannel(sc.client, channel.SlackID, outgoingMessage.ID)
}

sc.conversationMarker.MarkConversation(sc.client, channel.SlackID, outgoingMessage.ID)
sc.inflightMessageMap[outgoingMessage.ID] = newInflightMessage(msg, channel.SlackID, outgoingMessage.ID)
sc.rtm.SendMessage(outgoingMessage)
return nil
Expand All @@ -460,7 +454,7 @@ func (sc *SlackClient) SendDirectMessage(user *SlackUser, msg string) error {

msg = sc.UnparseMessageText(msg)
outgoingMessage := sc.rtm.NewOutgoingMessage(msg, imChannelID)
sc.conversationMarker.MarkDM(sc.client, imChannelID, outgoingMessage.ID)
sc.conversationMarker.MarkConversation(sc.client, imChannelID, outgoingMessage.ID)
sc.inflightMessageMap[outgoingMessage.ID] = newInflightMessage(msg, imChannelID, outgoingMessage.ID)
sc.rtm.SendMessage(outgoingMessage)
return nil
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ go 1.13

require (
github.com/BurntSushi/toml v0.3.1
github.com/slack-go/slack v0.8.1
github.com/slack-go/slack v0.10.0
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d
)

replace github.com/slack-go/slack => github.com/nolanlum/slack v0.2.1-0.20210225003417-b0474e47058a
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho=
github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/nolanlum/slack v0.2.1-0.20210225003417-b0474e47058a h1:XAA94FydObqpDxMNBdoq1lzmvaNY/x8IFxiQotz7gp0=
github.com/nolanlum/slack v0.2.1-0.20210225003417-b0474e47058a/go.mod h1:FGqNzJBmxIsZURAxh2a8D21AnOVvvXZvGligs4npPUM=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/slack-go/slack v0.10.0 h1:L16Eqg3QZzRKGXIVsFSZdJdygjOphb2FjRUwH6VrFu8=
github.com/slack-go/slack v0.10.0/go.mod h1:wWL//kk0ho+FcQXcBTmEafUI5dz4qz5f4mMk8oIkioQ=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
Expand Down

0 comments on commit 6ae11a3

Please sign in to comment.