mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-03 09:49:20 +02:00
server/server -> server/core
This commit is contained in:
parent
114327d4ce
commit
5a3d0650c9
838 changed files with 111 additions and 111 deletions
42
server/core/helpers/mentions.ts
Normal file
42
server/core/helpers/mentions.ts
Normal file
|
@ -0,0 +1,42 @@
|
|||
import { uniqify } from '@peertube/peertube-core-utils'
|
||||
import { WEBSERVER } from '@server/initializers/constants.js'
|
||||
import { actorNameAlphabet } from './custom-validators/activitypub/actor.js'
|
||||
import { regexpCapture } from './regexp.js'
|
||||
|
||||
export function extractMentions (text: string, isOwned: boolean) {
|
||||
let result: string[] = []
|
||||
|
||||
const localMention = `@(${actorNameAlphabet}+)`
|
||||
const remoteMention = `${localMention}@${WEBSERVER.HOST}`
|
||||
|
||||
const mentionRegex = isOwned
|
||||
? '(?:(?:' + remoteMention + ')|(?:' + localMention + '))' // Include local mentions?
|
||||
: '(?:' + remoteMention + ')'
|
||||
|
||||
const firstMentionRegex = new RegExp(`^${mentionRegex} `, 'g')
|
||||
const endMentionRegex = new RegExp(` ${mentionRegex}$`, 'g')
|
||||
const remoteMentionsRegex = new RegExp(' ' + remoteMention + ' ', 'g')
|
||||
|
||||
result = result.concat(
|
||||
regexpCapture(text, firstMentionRegex)
|
||||
.map(([ , username1, username2 ]) => username1 || username2),
|
||||
|
||||
regexpCapture(text, endMentionRegex)
|
||||
.map(([ , username1, username2 ]) => username1 || username2),
|
||||
|
||||
regexpCapture(text, remoteMentionsRegex)
|
||||
.map(([ , username ]) => username)
|
||||
)
|
||||
|
||||
// Include local mentions
|
||||
if (isOwned) {
|
||||
const localMentionsRegex = new RegExp(' ' + localMention + ' ', 'g')
|
||||
|
||||
result = result.concat(
|
||||
regexpCapture(text, localMentionsRegex)
|
||||
.map(([ , username ]) => username)
|
||||
)
|
||||
}
|
||||
|
||||
return uniqify(result)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue