Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Fix mentions extractions with punctuations

Chocobozzz09fabc2a3ceb
diff --git a/packages/tests/src/server-helpers/mentions.ts b/packages/tests/src/server-helpers/mentions.ts
index 0307ae5fdf..b9ce9562bd 100644
--- a/packages/tests/src/server-helpers/mentions.ts
+++ b/packages/tests/src/server-helpers/mentions.ts
@@ -1,7 +1,7 @@
/* oxlint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
import { expect } from 'chai'
-import { extractMentions } from '@peertube/peertube-server/core/helpers/mentions.js'
+import { extractLocalMentions } from '@peertube/peertube-server/core/helpers/mentions.js'
describe('Comment model', function () {
it('Should correctly extract mentions', async function () {
@@ -10,8 +10,13 @@ describe('Comment model', function () {
const isLocal = true
- const result = extractMentions(text, isLocal).sort((a, b) => a.localeCompare(b))
+ const result = extractLocalMentions(text, isLocal)
- expect(result).to.deep.equal([ 'another', 'chocobozzz', 'end', 'flo', 'florian', 'jean' ])
+ expect(result).to.deep.equal([ 'florian', 'jean', 'flo', 'another', 'chocobozzz', 'end' ])
+ })
+
+ it('Should correctly extract mentions with adjacent punctuations and newlines', async function () {
+ expect(extractLocalMentions('thanks @bob!', true)).to.deep.equal([ 'bob' ])
+ expect(extractLocalMentions('@bob\n', true)).to.deep.equal([ 'bob' ])
})
})
diff --git a/server/core/helpers/mentions.ts b/server/core/helpers/mentions.ts
index 829cf276f6..1caf68726e 100644
--- a/server/core/helpers/mentions.ts
+++ b/server/core/helpers/mentions.ts
@@ -3,38 +3,16 @@ 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, isLocal: boolean) {
- let result: string[] = []
+export function extractLocalMentions (text: string, fromLocalEntity: boolean) {
+ const setHostOptionalChar = fromLocalEntity
+ ? '?'
+ : ''
- const localMention = `@(${actorNameAlphabet}+)`
- const remoteMention = `${localMention}@${WEBSERVER.HOST}`
+ // FIXME: Use RegExp.escape() when NodeJS 24 is the minimum version required by peertube
+ const mentionRegex = new RegExp(`(?<=^|\\s)@(${actorNameAlphabet}+)(?:@${WEBSERVER.HOST})${setHostOptionalChar}(?=[\\s.,!?)]|$)`, 'gmu')
- const mentionRegex = isLocal
- ? '(?:(?:' + remoteMention + ')|(?:' + localMention + '))' // Include local mentions?
- : '(?:' + remoteMention + ')'
+ const results = regexpCapture(text, mentionRegex)
+ .map(([ , username ]) => username)
- 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 (isLocal) {
- const localMentionsRegex = new RegExp(' ' + localMention + ' ', 'g')
-
- result = result.concat(
- regexpCapture(text, localMentionsRegex)
- .map(([ , username ]) => username)
- )
- }
-
- return uniqify(result)
+ return uniqify(results)
}
diff --git a/server/core/models/video/video-comment.ts b/server/core/models/video/video-comment.ts
index f7dcd1c455..a2d0b47f62 100644
--- a/server/core/models/video/video-comment.ts
+++ b/server/core/models/video/video-comment.ts
@@ -9,7 +9,7 @@ import {
} from '@peertube/peertube-models'
import { afterCommitIfTransaction, retryTransactionWrapper } from '@server/helpers/database-utils.js'
import { logger } from '@server/helpers/logger.js'
-import { extractMentions } from '@server/helpers/mentions.js'
+import { extractLocalMentions } from '@server/helpers/mentions.js'
import { sequelizeTypescript } from '@server/initializers/database.js'
import { getLocalApproveReplyActivityPubUrl } from '@server/lib/activitypub/url.js'
import { getServerActor } from '@server/models/application/application.js'
@@ -698,7 +698,7 @@ export class VideoCommentModel extends SequelizeModel<VideoCommentModel> {
}
extractMentions () {
- return extractMentions(this.text, this.isLocal())
+ return extractLocalMentions(this.text, this.isLocal())
}
toFormattedJSON (this: MCommentFormattable) {