ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mentions.ts 1.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { uniqify } from '@peertube/peertube-core-utils'
  2. import { WEBSERVER } from '@server/initializers/constants.js'
  3. import { actorNameAlphabet } from './custom-validators/activitypub/actor.js'
  4. import { regexpCapture } from './regexp.js'
  5. export function extractMentions (text: string, isOwned: boolean) {
  6. let result: string[] = []
  7. const localMention = `@(${actorNameAlphabet}+)`
  8. const remoteMention = `${localMention}@${WEBSERVER.HOST}`
  9. const mentionRegex = isOwned
  10. ? '(?:(?:' + remoteMention + ')|(?:' + localMention + '))' // Include local mentions?
  11. : '(?:' + remoteMention + ')'
  12. const firstMentionRegex = new RegExp(`^${mentionRegex} `, 'g')
  13. const endMentionRegex = new RegExp(` ${mentionRegex}$`, 'g')
  14. const remoteMentionsRegex = new RegExp(' ' + remoteMention + ' ', 'g')
  15. result = result.concat(
  16. regexpCapture(text, firstMentionRegex)
  17. .map(([ , username1, username2 ]) => username1 || username2),
  18. regexpCapture(text, endMentionRegex)
  19. .map(([ , username1, username2 ]) => username1 || username2),
  20. regexpCapture(text, remoteMentionsRegex)
  21. .map(([ , username ]) => username)
  22. )
  23. // Include local mentions
  24. if (isOwned) {
  25. const localMentionsRegex = new RegExp(' ' + localMention + ' ', 'g')
  26. result = result.concat(
  27. regexpCapture(text, localMentionsRegex)
  28. .map(([ , username ]) => username)
  29. )
  30. }
  31. return uniqify(result)
  32. }