From 125ead6f8b07f107821031af264682926a26c1ae Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Fri, 26 Jun 2020 09:11:47 +0000 Subject: [PATCH] Function to extract mentions as a string --- content.py | 25 +++++++++++++++++++++++++ tests.py | 17 +++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/content.py b/content.py index 6fae4567..938837ab 100644 --- a/content.py +++ b/content.py @@ -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 """ diff --git a/tests.py b/tests.py index 9cd7d21d..9fbb7374 100644 --- a/tests.py +++ b/tests.py @@ -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()