Function to extract mentions as a string

main
Bob Mottram 2020-06-26 09:11:47 +00:00
parent 4064a317bc
commit 125ead6f8b
2 changed files with 42 additions and 0 deletions

View File

@ -14,6 +14,31 @@ from utils import fileLastModified
from utils import getLinkPrefixes
def getMentionsString(content: str) -> []:
"""Returns the initial string containing mentions from the post content
and the content string without the initial mentions
"""
if ' ' not in content:
# Doesn't contain any distinct words
return ['', content]
if '@' not in content:
# Nothing which could be a mention
return ['', content]
messageStr = content.strip()
if not messageStr.startswith('@'):
# There are no mentions
return ['', content]
words = messageStr.split(' ')
# get each mentioned handle
mentions = ''
for handle in words:
if not handle.startswith('@'):
break
mentions += handle + ' '
messageStr = content.replace(mentions, '')
return [mentions.strip(), messageStr]
def switchWords(baseDir: str, nickname: str, domain: str, content: str) -> str:
"""Performs word replacements. eg. Trump -> The Orange Menace
"""

View File

@ -64,6 +64,7 @@ from media import getAttachmentMediaType
from delete import sendDeleteViaServer
from inbox import validInbox
from inbox import validInboxFilenames
from content import getMentionsString
from content import addWebLinks
from content import replaceEmojiFromTags
from content import addHtmlTags
@ -1865,8 +1866,24 @@ def testSiteIsActive():
assert(not siteIsActive('https://notarealwebsite.a.b.c'))
def testGetMentionsString():
print('testGetMentionsString')
content = 'This post has no mentions'
result = getMentionsString(content)
assert len(result) == 2
assert not result[0]
assert result[1] == 'This post has no mentions'
content = '@nick@abc @sue@def This post has no mentions'
result = getMentionsString(content)
assert len(result) == 2
assert result[0] == '@nick@abc @sue@def'
assert result[1] == 'This post has no mentions'
def runAllTests():
print('Running tests...')
testGetMentionsString()
testSiteIsActive()
testJsonld()
testRemoveTextFormatting()