Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix threaded reply, threaded react, and some private message things #181

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ You can also [run it with Docker](#run-it-with-docker).
| --- | --- | --- | --- | --- |
| from me | works | works | doesn't work ([#168](https://github.com/insomniacslk/irc-slack/issues/168)) | works |
| to me | works | works | works | works |
| thread from me | doesn't work ([#168](https://github.com/insomniacslk/irc-slack/issues/168)) | doesn't work ([#168](https://github.com/insomniacslk/irc-slack/issues/168)) | untested | doesn't work ([#166](https://github.com/insomniacslk/irc-slack/issues/166)) |
| thread from me | works | works | untested | doesn't work ([#166](https://github.com/insomniacslk/irc-slack/issues/166)) |
| thread to me | works | works | untested | works but sends in the IM chat ([#167](https://github.com/insomniacslk/irc-slack/issues/167)) |

## Encryption
Expand Down
2 changes: 1 addition & 1 deletion pkg/ircslack/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (c *Channel) IsPublicChannel() bool {

// IsPrivateChannel returns true if the channel is private.
func (c *Channel) IsPrivateChannel() bool {
return c.IsGroup && c.IsPrivate
return (c.IsGroup || c.IsChannel) && c.IsPrivate
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm often missing messages from others (reactions come in fine) in these such private channels (that it had trouble joining before this change), so I wonder if something else needs to change here.

}

// IsMP returns true if it is a multi-party conversation.
Expand Down
32 changes: 24 additions & 8 deletions pkg/ircslack/event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,36 @@ func getConversationDetails(
ctx *IrcContext,
channelID string,
timestamp string,
) (slack.Message, error) {
) (slack.Message, error, string) {
message, err := ctx.SlackClient.GetConversationHistory(&slack.GetConversationHistoryParameters{
ChannelID: channelID,
Latest: timestamp,
Limit: 1,
Inclusive: true,
})
if err != nil {
return slack.Message{}, err
return slack.Message{}, err, ""
}
if len(message.Messages) > 0 {
return message.Messages[0], nil
parent := message.Messages[0]
// If the timestamps are not equal, we're looking for a threaded message
if parent.Timestamp != timestamp {
msgs, _, _, err := ctx.SlackClient.GetConversationReplies(&slack.GetConversationRepliesParameters{ChannelID: channelID, Timestamp: parent.Timestamp})
if err == nil {
for _, msg := range msgs {
if msg.Timestamp == timestamp {
channame := resolveChannelName(ctx, channelID, parent.Timestamp)
return msg, nil, channame
}
}
}
// TODO: Always find the message, or return better fallback
log.Warningf("Did not find threaded message with timestamp %v from %v", timestamp, parent)
}
channame := resolveChannelName(ctx, channelID, "")
return parent, nil, channame
}
return slack.Message{}, fmt.Errorf("No such message found")
return slack.Message{}, fmt.Errorf("No such message found"), ""
}

func replacePermalinkWithText(ctx *IrcContext, text string) string {
Expand All @@ -169,7 +185,7 @@ func replacePermalinkWithText(ctx *IrcContext, text string) string {
}
channel := matches[1]
timestamp := matches[2] + "." + matches[3]
message, err := getConversationDetails(ctx, channel, timestamp)
message, err, _ := getConversationDetails(ctx, channel, timestamp)
if err != nil {
log.Printf("could not get message details from permalink %s %s %s %v", matches[0], channel, timestamp, err)
return text
Expand Down Expand Up @@ -267,7 +283,7 @@ func eventHandler(ctx *IrcContext, rtm *slack.RTM) {
switch message.SubType {
case "message_changed":
// https://api.slack.com/events/message/message_changed
editedMessage, err := getConversationDetails(ctx, message.Channel, message.Timestamp)
editedMessage, err, _ := getConversationDetails(ctx, message.Channel, message.Timestamp)
if err != nil {
fmt.Printf("could not get changed conversation details %s", err)
continue
Expand Down Expand Up @@ -351,7 +367,6 @@ func eventHandler(ctx *IrcContext, rtm *slack.RTM) {
// and slack.MemberLeftChannelEvent.
case *slack.ReactionAddedEvent:
// https://api.slack.com/events/reaction_added
channame := resolveChannelName(ctx, ev.Item.Channel, "")
user := ctx.GetUserInfo(ev.User)
name := ""
if user == nil {
Expand All @@ -360,7 +375,8 @@ func eventHandler(ctx *IrcContext, rtm *slack.RTM) {
} else {
name = user.Name
}
msg, err := getConversationDetails(ctx, ev.Item.Channel, ev.Item.Timestamp)
msg, err, channame := getConversationDetails(ctx, ev.Item.Channel, ev.Item.Timestamp)

if err != nil {
fmt.Printf("could not get Conversation details %s", err)
continue
Expand Down
7 changes: 6 additions & 1 deletion pkg/ircslack/irc_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,14 @@ func (ic *IrcContext) Start() {
opts = append(opts, slack.MsgOptionText(strings.TrimSpace(text), false))
if message.TargetTs != "" {
opts = append(opts, slack.MsgOptionTS(message.TargetTs))
if target[1] == ChannelPrefixThread[0] {
idx := strings.Index(target, message.TargetTs)
// strip the prefixes and the '-timestamp'
target = target[2 : idx-1]
}
}
if _, _, err := ic.SlackClient.PostMessage(target, opts...); err != nil {
log.Warningf("Failed to post message to Slack to target %s: %v", target, err)
log.Warningf("Failed to post message to Slack to target %s: %v (targetTs: %v)", target, err, message.TargetTs)
}
}
textBuffer = make(map[string]string)
Expand Down