Storing hashtags for incoming posts

merge-requests/6/head
Bob Mottram 2019-12-12 17:34:31 +00:00
parent 95f46cf276
commit 8ef0a72489
1 changed files with 42 additions and 0 deletions

View File

@ -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')