main

mattermost/focalboard

Last updated at: 29/12/2023 09:48

mentions.go

TLDR

The mentions.go file in the notifymentions package contains a function called extractMentions that extracts any mentions in a specified block and returns a map of usernames.

Methods

extractMentions

This method takes a model.Block as a parameter and extracts any mentions in the block's title. It then returns a map containing the extracted usernames.

Classes

There are no classes in this file.

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

package notifymentions

import (
	"regexp"
	"strings"

	"github.com/mattermost/focalboard/server/model"

	mm_model "github.com/mattermost/mattermost-server/v6/model"
)

var atMentionRegexp = regexp.MustCompile(`\B@[[:alnum:]][[:alnum:]\.\-_:]*`)

// extractMentions extracts any mentions in the specified block and returns
// a slice of usernames.
func extractMentions(block *model.Block) map[string]struct{} {
	mentions := make(map[string]struct{})
	if block == nil || !strings.Contains(block.Title, "@") {
		return mentions
	}

	str := block.Title

	for _, match := range atMentionRegexp.FindAllString(str, -1) {
		name := mm_model.NormalizeUsername(match[1:])
		if mm_model.IsValidUsernameAllowRemote(name) {
			mentions[name] = struct{}{}
		}
	}
	return mentions
}