Fix mention notification with a remote account
This commit is contained in:
parent
72be63e56f
commit
1f6d57e354
|
@ -148,6 +148,8 @@ class Notifier {
|
||||||
|
|
||||||
private async notifyOfCommentMention (comment: VideoCommentModel) {
|
private async notifyOfCommentMention (comment: VideoCommentModel) {
|
||||||
const usernames = comment.extractMentions()
|
const usernames = comment.extractMentions()
|
||||||
|
logger.debug('Extracted %d username from comment %s.', usernames.length, comment.url, { usernames, text: comment.text })
|
||||||
|
|
||||||
let users = await UserModel.listByUsernames(usernames)
|
let users = await UserModel.listByUsernames(usernames)
|
||||||
|
|
||||||
if (comment.Video.isOwned()) {
|
if (comment.Video.isOwned()) {
|
||||||
|
|
|
@ -466,31 +466,41 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
|
||||||
}
|
}
|
||||||
|
|
||||||
extractMentions () {
|
extractMentions () {
|
||||||
if (!this.text) return []
|
let result: string[] = []
|
||||||
|
|
||||||
const localMention = `@(${actorNameAlphabet}+)`
|
const localMention = `@(${actorNameAlphabet}+)`
|
||||||
const remoteMention = `${localMention}@${CONFIG.WEBSERVER.HOST}`
|
const remoteMention = `${localMention}@${CONFIG.WEBSERVER.HOST}`
|
||||||
|
|
||||||
|
const mentionRegex = this.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')
|
const remoteMentionsRegex = new RegExp(' ' + remoteMention + ' ', 'g')
|
||||||
const localMentionsRegex = new RegExp(' ' + localMention + ' ', 'g')
|
|
||||||
const firstMentionRegex = new RegExp('^(?:(?:' + remoteMention + ')|(?:' + localMention + ')) ', 'g')
|
|
||||||
const endMentionRegex = new RegExp(' (?:(?:' + remoteMention + ')|(?:' + localMention + '))$', 'g')
|
|
||||||
|
|
||||||
return uniq(
|
result = result.concat(
|
||||||
[].concat(
|
regexpCapture(this.text, firstMentionRegex)
|
||||||
regexpCapture(this.text, remoteMentionsRegex)
|
.map(([ , username1, username2 ]) => username1 || username2),
|
||||||
.map(([ , username ]) => username),
|
|
||||||
|
|
||||||
regexpCapture(this.text, localMentionsRegex)
|
regexpCapture(this.text, endMentionRegex)
|
||||||
.map(([ , username ]) => username),
|
.map(([ , username1, username2 ]) => username1 || username2),
|
||||||
|
|
||||||
regexpCapture(this.text, firstMentionRegex)
|
regexpCapture(this.text, remoteMentionsRegex)
|
||||||
.map(([ , username1, username2 ]) => username1 || username2),
|
.map(([ , username ]) => username)
|
||||||
|
|
||||||
regexpCapture(this.text, endMentionRegex)
|
|
||||||
.map(([ , username1, username2 ]) => username1 || username2)
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Include local mentions
|
||||||
|
if (this.isOwned()) {
|
||||||
|
const localMentionsRegex = new RegExp(' ' + localMention + ' ', 'g')
|
||||||
|
|
||||||
|
result = result.concat(
|
||||||
|
regexpCapture(this.text, localMentionsRegex)
|
||||||
|
.map(([ , username ]) => username)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return uniq(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
toFormattedJSON () {
|
toFormattedJSON () {
|
||||||
|
|
|
@ -508,6 +508,20 @@ describe('Test users notifications', function () {
|
||||||
await removeAccountFromAccountBlocklist(servers[ 0 ].url, userAccessToken, 'root')
|
await removeAccountFromAccountBlocklist(servers[ 0 ].url, userAccessToken, 'root')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('Should not send a new mention notification if the remote account mention a local account', async function () {
|
||||||
|
this.timeout(20000)
|
||||||
|
|
||||||
|
const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
|
||||||
|
const uuid = resVideo.body.video.uuid
|
||||||
|
|
||||||
|
await waitJobs(servers)
|
||||||
|
const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, '@user_1 hello')
|
||||||
|
const threadId = resThread.body.comment.id
|
||||||
|
|
||||||
|
await waitJobs(servers)
|
||||||
|
await checkCommentMention(baseParams, uuid, threadId, threadId, 'super root 2 name', 'absence')
|
||||||
|
})
|
||||||
|
|
||||||
it('Should send a new mention notification after local comments', async function () {
|
it('Should send a new mention notification after local comments', async function () {
|
||||||
this.timeout(10000)
|
this.timeout(10000)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue