From 8ef0a72489f6252778e2d69b87bb5271c3658521 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Thu, 12 Dec 2019 17:34:31 +0000 Subject: [PATCH] Storing hashtags for incoming posts --- inbox.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/inbox.py b/inbox.py index 571601463..da34f3bf9 100644 --- a/inbox.py +++ b/inbox.py @@ -12,6 +12,7 @@ import datetime import time import json from shutil import copyfile +from utils import isPublicPost from utils import getCachedPostFilename from utils import removePostFromCache from utils import urlPermitted @@ -59,6 +60,45 @@ from webinterface import individualPostAsHtml from webinterface import getIconsDir from question import questionUpdateVotes +def storeHashTags(baseDir: str,postJsonObject: {}) -> None: + """Extracts hashtags from an incoming post and updates the + relevant tags files. + """ + if not isPublicPost(postJsonObject): + return + if not postJsonObject.get('object'): + return + if not isinstance(postJsonObject['object'], dict): + return + if not postJsonObject['object'].get('tag'): + return + if not postJsonObject.get('id'): + return + if not isinstance(postJsonObject['object']['tag'], list): + return + tagsDir=baseDir+'/tags' + for tag in postJsonObject['object']['tag']: + if not tag.get('type'): + continue + if tag['type']!='Hashtag': + continue + if not tag.get('name'): + continue + tagName=tag['name'].replace('#','') + tagsFilename=tagsDir+'/'+tag['name']+'.txt' + postUrl=postJsonObject['id'].replace('/activity','').replace('/','#') + if not os.path.isfile(tagsFilename): + tagsFile=open(tagsFilename, "w+") + if tagsFile: + tagsFile.write(postUrl+'\n') + tagsFile.close() + else: + if postUrl not in open(tagsFilename).read(): + tagsFile=open(tagsFilename, "a+") + if tagsFile: + tagsFile.write(postUrl+'\n') + tagsFile.close() + def inboxStorePostToHtmlCache(recentPostsCache: {},maxRecentPosts: int, \ translate: {}, \ baseDir: str,httpPrefix: str, \ @@ -1876,6 +1916,8 @@ def inboxAfterCapabilities(recentPostsCache: {},maxRecentPosts: int, \ inboxUpdateCalendar(baseDir,handle,postJsonObject) + storeHashTags(baseDir,postJsonObject) + if not unitTest: if debug: print('DEBUG: saving inbox post as html to cache')