Add spoiler feature
@@ -29,6 +29,8 @@ var (
codeBlockRegex = regexp.MustCompile("(?s)\x60\x60\x60(.+?)\n(.+?)\x60\x60\x60(?:$|\n)")
channelMentionRegex = regexp.MustCompile("<#\\d*>")
urlRegex = regexp.MustCompile("<?https?://(.+?)(?:(?:/.*)|\\s|$|>)")
+ spoilerRegex = regexp.MustCompile("(?s)\\|\\|(.+?)\\|\\|")
+
shortener = linkshortener.NewShortener(51726)
userColor = "green"
)
@@ -46,6 +48,8 @@ type ChatView struct {
selection int
selectionMode bool
+ showSpoilerContent map[string]bool
+
onMessageAction func(message *discordgo.Message, event *tcell.EventKey) *tcell.EventKey
}
@@ -57,6 +61,7 @@ func NewChatView(session *discordgo.Session, ownUserID string) *ChatView {
ownUserID: ownUserID,
selection: -1,
selectionMode: true,
+ showSpoilerContent: make(map[string]bool, 0),
}
if config.GetConfig().ShortenLinks {
@@ -119,6 +124,18 @@ func NewChatView(session *discordgo.Session, ownUserID string) *ChatView {
return nil
}
+ if chatView.selection > 0 && chatView.selection < len(chatView.data) && event.Rune() == 's' {
+ messageID := chatView.data[chatView.selection].ID
+ currentValue, contains := chatView.showSpoilerContent[messageID]
+ if contains {
+ chatView.showSpoilerContent[messageID] = !currentValue
+ } else {
+ chatView.showSpoilerContent[messageID] = true
+ }
+ chatView.SetMessages(chatView.data)
+ return nil
+ }
+
if chatView.selection > 0 && chatView.selection < len(chatView.data) && chatView.onMessageAction != nil {
return chatView.onMessageAction(chatView.data[chatView.selection], event)
}
@@ -217,6 +234,11 @@ func (chatView *ChatView) AddMessages(messages []*discordgo.Message) {
messageText = "[gray]removed " + message.Mentions[0].Username + " from the group."
}
+ shouldShow, contains := chatView.showSpoilerContent[message.ID]
+ if !contains || !shouldShow {
+ messageText = spoilerRegex.ReplaceAllString(messageText, "[red]!SPOILER![white]")
+ }
+
messageText = channelMentionRegex.
ReplaceAllStringFunc(messageText, func(data string) string {
channelID := strings.TrimSuffix(strings.TrimPrefix(data, "<#"), ">")
@@ -336,11 +358,14 @@ func (chatView *ChatView) AddMessages(messages []*discordgo.Message) {
}
}
+// ClearSelection clears the current selection of messages.
func (chatView *ChatView) ClearSelection() {
chatView.selection = -1
chatView.updateHighlights()
}
+// SignalSelectionDeleted notifies the ChatView that its currently selected
+// message doesn't exist anymore, moving the selection up by a row if possible.
func (chatView *ChatView) SignalSelectionDeleted() {
if chatView.selection > 0 {
chatView.selection--
