Files
team/server/public/model/channel_mentions.go
2bb8d01100 Mattermost Blocks (#36338)
* Mattermost Blocks

* Rethink some block props and styles

* Remove obsolete comment

* Style fixes and add examples

* Add mention, search, and image metadata support

* Several UI improvements

* Validate blocks by translating them

* Add initial e2e tests

* Fix lint

* Fix tests and lint

* Fix lint

* Improve e2e and simplify doPostAction

* server side cleanup

* Fix typo

* Fix style

* Fix some tests

* Fix more tests

* Fix cypress tests

* Fix playwhright tests

* Fix flaky system console test

* Frontend cleanup

* Add blocks editor

* Fix CI

* Limit all blocks width

* Fix test

* Enable webhook service on playwhright CI

* fix flaky test

* Fix style

* Cleanups

* Address coderabbit comments

* Address coderabbit comments

* Address CI errors

* Fix tests

* Address changes in valid actions

* UX fixes

* Fix test

* Fix danger buttons and add allow emojis in buttons

* style tweaks

use semantic color tokens for buttons and accents, update max-heights for scollables,  adjustments to containers, removed btn-sm (using default button size for mm blocks with height and padding adjustments for use within blocks. removed hover selector from utils to prevent hover state issue with primary button, refined semantic-color-warning.

* Address feedback

* Add interactive content to post previews

* Add collapse transitions

* Address autocomplete position

* Handle columns within column sets

* Fix css lint

* Fix tests

* Use feature flag

* Address feedback

* Fix lint and add missing tests

* Fix e2e tests

* Fix e2e test

* Allow interactive posts without a message

* Fix lint

* Address feedback

* Fix ci

* Fix lint

* Add limits to block translations

* Fix lint

* Address feedback

* Address feedback

* Update snapshot

* Fix collapsible not defaulting to the right value

* Add total text limit

* Sync translations with mobile

* Fix snapshot

---------

Co-authored-by: Matthew Birtch <[email protected]>
Co-authored-by: Mattermost Build <[email protected]>
2026-06-29 20:33:46 +02:00

97 lines
2.5 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package model
import (
"regexp"
"strings"
)
var channelMentionRegexp = regexp.MustCompile(`\B~[a-zA-Z0-9\-_]+`)
func ChannelMentions(message string) []string {
return ChannelMentionsFromStrings([]string{message})
}
// ChannelMentionsFromStrings extracts ~channel-name mentions from each string, deduplicating across
// all inputs. Callers typically pass post.AllStrings() so mentions are found in the message, message
// attachments, and interactive payloads (mm_blocks, blocks, cards) consistently with other post text.
func ChannelMentionsFromStrings(strs []string) []string {
alreadyMentioned := make(map[string]bool)
var names []string
for _, s := range strs {
if !strings.Contains(s, "~") {
continue
}
for _, match := range channelMentionRegexp.FindAllString(s, -1) {
name := match[1:]
if !alreadyMentioned[name] {
names = append(names, name)
alreadyMentioned[name] = true
}
}
}
return names
}
// ChannelMentionsFromAttachments extracts channel mentions from attachment fields.
// It scans pretext, text, and field values (but not titles, as titles are labels).
func ChannelMentionsFromAttachments(attachments []*MessageAttachment) []string {
alreadyMentioned := make(map[string]bool)
var names []string
for _, attachment := range attachments {
if attachment == nil {
continue
}
// Scan pretext
for _, match := range channelMentionRegexp.FindAllString(attachment.Pretext, -1) {
name := match[1:]
if !alreadyMentioned[name] {
names = append(names, name)
alreadyMentioned[name] = true
}
}
// Scan text
for _, match := range channelMentionRegexp.FindAllString(attachment.Text, -1) {
name := match[1:]
if !alreadyMentioned[name] {
names = append(names, name)
alreadyMentioned[name] = true
}
}
// Scan field values (not titles - titles are labels)
for _, field := range attachment.Fields {
if field == nil {
continue
}
// Field value can be any type, convert to string
var valueStr string
switch v := field.Value.(type) {
case string:
valueStr = v
default:
// For non-string values, we don't scan for mentions
continue
}
for _, match := range channelMentionRegexp.FindAllString(valueStr, -1) {
name := match[1:]
if !alreadyMentioned[name] {
names = append(names, name)
alreadyMentioned[name] = true
}
}
}
}
return names
}