Create thread to send new posts

main2
Bob Mottram 2019-09-29 14:30:24 +01:00
parent 3f8e4e0b2f
commit 795d6b6f53
1 changed files with 289 additions and 269 deletions

120
daemon.py
View File

@ -2370,33 +2370,24 @@ class PubServer(BaseHTTPRequestHandler):
def do_HEAD(self): def do_HEAD(self):
self._set_headers('application/json',0,None) self._set_headers('application/json',0,None)
def _receiveNewPost(self,authorized: bool,postType: str,path: str) -> (int,int): def _receiveNewPostThread(self,authorized: bool,postType: str,path: str,headers: []) -> int:
# 0 = this is not a new post # 0 = this is not a new post
# 1 = new post success # 1 = new post success
# -1 = new post failed # -1 = new post failed
# 2 = new post canceled # 2 = new post canceled
pageNumber=1 if ' boundary=' in headers['Content-type']:
if authorized and '/users/' in path and '?'+postType+'?' in path:
if '?page=' in path:
pageNumberStr=path.split('?page=')[1]
if '?' in pageNumberStr:
pageNumberStr=pageNumberStr.split('?')[0]
if pageNumberStr.isdigit():
pageNumber=int(pageNumberStr)
path=path.split('?page=')[0]
if ' boundary=' in self.headers['Content-type']:
nickname=None nickname=None
nicknameStr=path.split('/users/')[1] nicknameStr=path.split('/users/')[1]
if '/' in nicknameStr: if '/' in nicknameStr:
nickname=nicknameStr.split('/')[0] nickname=nicknameStr.split('/')[0]
else: else:
return -1,pageNumber return -1
length = int(self.headers['Content-length']) length = int(headers['Content-length'])
if length>self.server.maxPostLength: if length>self.server.maxPostLength:
print('POST size too large') print('POST size too large')
return -1,pageNumber return -1
boundary=self.headers['Content-type'].split('boundary=')[1] boundary=headers['Content-type'].split('boundary=')[1]
if ';' in boundary: if ';' in boundary:
boundary=boundary.split(';')[0] boundary=boundary.split(';')[0]
@ -2476,12 +2467,12 @@ class PubServer(BaseHTTPRequestHandler):
# send the post # send the post
if not fields.get('message') and not fields.get('imageDescription'): if not fields.get('message') and not fields.get('imageDescription'):
return -1,pageNumber return -1
if fields.get('submitPost'): if fields.get('submitPost'):
if fields['submitPost']!='Submit': if fields['submitPost']!='Submit':
return -1,pageNumber return -1
else: else:
return 2,pageNumber return 2
if not fields.get('imageDescription'): if not fields.get('imageDescription'):
fields['imageDescription']=None fields['imageDescription']=None
@ -2508,9 +2499,9 @@ class PubServer(BaseHTTPRequestHandler):
messageJson, \ messageJson, \
self.server.maxReplies, \ self.server.maxReplies, \
self.server.debug) self.server.debug)
return 1,pageNumber return 1
else: else:
return -1,pageNumber return -1
if postType=='newunlisted': if postType=='newunlisted':
messageJson= \ messageJson= \
@ -2530,9 +2521,9 @@ class PubServer(BaseHTTPRequestHandler):
messageJson, \ messageJson, \
self.server.maxReplies, \ self.server.maxReplies, \
self.server.debug) self.server.debug)
return 1,pageNumber return 1
else: else:
return -1,pageNumber return -1
if postType=='newfollowers': if postType=='newfollowers':
messageJson= \ messageJson= \
@ -2552,9 +2543,9 @@ class PubServer(BaseHTTPRequestHandler):
messageJson, \ messageJson, \
self.server.maxReplies, \ self.server.maxReplies, \
self.server.debug) self.server.debug)
return 1,pageNumber return 1
else: else:
return -1,pageNumber return -1
if postType=='newdm': if postType=='newdm':
messageJson=None messageJson=None
@ -2581,14 +2572,14 @@ class PubServer(BaseHTTPRequestHandler):
messageJson, \ messageJson, \
self.server.maxReplies, \ self.server.maxReplies, \
self.server.debug) self.server.debug)
return 1,pageNumber return 1
else: else:
return -1,pageNumber return -1
if postType=='newreport': if postType=='newreport':
if attachmentMediaType: if attachmentMediaType:
if attachmentMediaType!='image': if attachmentMediaType!='image':
return -1,pageNumber return -1
# So as to be sure that this only goes to moderators # So as to be sure that this only goes to moderators
# and not accounts being reported we disable any # and not accounts being reported we disable any
# included fediverse addresses by replacing '@' with '-at-' # included fediverse addresses by replacing '@' with '-at-'
@ -2605,22 +2596,22 @@ class PubServer(BaseHTTPRequestHandler):
if messageJson: if messageJson:
self.postToNickname=nickname self.postToNickname=nickname
if self._postToOutbox(messageJson,__version__): if self._postToOutbox(messageJson,__version__):
return 1,pageNumber return 1
else: else:
return -1,pageNumber return -1
if postType=='newshare': if postType=='newshare':
if not fields.get('itemType'): if not fields.get('itemType'):
return -1,pageNumber return -1
if not fields.get('category'): if not fields.get('category'):
return -1,pageNumber return -1
if not fields.get('location'): if not fields.get('location'):
return -1,pageNumber return -1
if not fields.get('duration'): if not fields.get('duration'):
return -1,pageNumber return -1
if attachmentMediaType: if attachmentMediaType:
if attachmentMediaType!='image': if attachmentMediaType!='image':
return -1,pageNumber return -1
addShare(self.server.baseDir, \ addShare(self.server.baseDir, \
self.server.httpPrefix, \ self.server.httpPrefix, \
nickname, \ nickname, \
@ -2637,10 +2628,44 @@ class PubServer(BaseHTTPRequestHandler):
if os.path.isfile(filename): if os.path.isfile(filename):
os.remove(filename) os.remove(filename)
self.postToNickname=nickname self.postToNickname=nickname
return 1,pageNumber return 1
return -1,pageNumber return -1
else:
return 0,pageNumber def _receiveNewPost(self,authorized: bool,postType: str,path: str) -> bool:
"""A new post has been created
This creates a thread to send the new post
"""
pageNumber=1
if authorized and '/users/' in path and '?'+postType+'?' in path:
if '?page=' in path:
pageNumberStr=path.split('?page=')[1]
if '?' in pageNumberStr:
pageNumberStr=pageNumberStr.split('?')[0]
if pageNumberStr.isdigit():
pageNumber=int(pageNumberStr)
path=path.split('?page=')[0]
newPostThreadName=self.postToNickname
if not newPostThreadName:
newPostThreadName='*'
if self.server.newPostThread.get(newPostThreadName):
print('Waiting for previous new post thread to end')
waitCtr=0
while self.server.newPostThread[newPostThreadName].isAlive() and waitCtr<8:
time.sleep(1)
waitCtr+=1
if waitCtr>=8:
self.server.newPostThread[newPostThreadName].kill()
print('Creating new post thread')
self.server.newPostThread[newPostThreadName]= \
threadWithTrace(target=self._receiveNewPostThread, \
args=(authorized,postType,path,self.headers.copy()),daemon=True)
print('Starting new post thread')
self.server.newPostThread[newPostThreadName].start()
return True
return False
def do_POST(self): def do_POST(self):
if not self.server.session: if not self.server.session:
@ -3637,48 +3662,42 @@ class PubServer(BaseHTTPRequestHandler):
self.server.POSTbusy=False self.server.POSTbusy=False
return return
postState,pageNumber=self._receiveNewPost(authorized,'newpost',self.path) if self._receiveNewPost(authorized,'newpost',self.path):
if postState!=0:
nickname=self.path.split('/users/')[1] nickname=self.path.split('/users/')[1]
if '/' in nickname: if '/' in nickname:
nickname=nickname.split('/')[0] nickname=nickname.split('/')[0]
self._redirect_headers('/users/'+nickname+'/inbox?page='+str(pageNumber),cookie) self._redirect_headers('/users/'+nickname+'/inbox?page='+str(pageNumber),cookie)
self.server.POSTbusy=False self.server.POSTbusy=False
return return
postState,pageNumber=self._receiveNewPost(authorized,'newunlisted',self.path) if self._receiveNewPost(authorized,'newunlisted',self.path):
if postState!=0:
nickname=self.path.split('/users/')[1] nickname=self.path.split('/users/')[1]
if '/' in nickname: if '/' in nickname:
nickname=nickname.split('/')[0] nickname=nickname.split('/')[0]
self._redirect_headers('/users/'+nickname+'/inbox?page='+str(pageNumber),cookie) self._redirect_headers('/users/'+nickname+'/inbox?page='+str(pageNumber),cookie)
self.server.POSTbusy=False self.server.POSTbusy=False
return return
postState,pageNumber=self._receiveNewPost(authorized,'newfollowers',self.path) if self._receiveNewPost(authorized,'newfollowers',self.path):
if postState!=0:
nickname=self.path.split('/users/')[1] nickname=self.path.split('/users/')[1]
if '/' in nickname: if '/' in nickname:
nickname=nickname.split('/')[0] nickname=nickname.split('/')[0]
self._redirect_headers('/users/'+nickname+'/inbox?page='+str(pageNumber),cookie) self._redirect_headers('/users/'+nickname+'/inbox?page='+str(pageNumber),cookie)
self.server.POSTbusy=False self.server.POSTbusy=False
return return
postState,pageNumber=self._receiveNewPost(authorized,'newdm',self.path) if self._receiveNewPost(authorized,'newdm',self.path):
if postState!=0:
nickname=self.path.split('/users/')[1] nickname=self.path.split('/users/')[1]
if '/' in nickname: if '/' in nickname:
nickname=nickname.split('/')[0] nickname=nickname.split('/')[0]
self._redirect_headers('/users/'+nickname+'/inbox?page='+str(pageNumber),cookie) self._redirect_headers('/users/'+nickname+'/inbox?page='+str(pageNumber),cookie)
self.server.POSTbusy=False self.server.POSTbusy=False
return return
postState,pageNumber=self._receiveNewPost(authorized,'newreport',self.path) if self._receiveNewPost(authorized,'newreport',self.path):
if postState!=0:
nickname=self.path.split('/users/')[1] nickname=self.path.split('/users/')[1]
if '/' in nickname: if '/' in nickname:
nickname=nickname.split('/')[0] nickname=nickname.split('/')[0]
self._redirect_headers('/users/'+nickname+'/inbox?page='+str(pageNumber),cookie) self._redirect_headers('/users/'+nickname+'/inbox?page='+str(pageNumber),cookie)
self.server.POSTbusy=False self.server.POSTbusy=False
return return
postState,pageNumber=self._receiveNewPost(authorized,'newshare',self.path) if self._receiveNewPost(authorized,'newshare',self.path):
if postState!=0:
nickname=self.path.split('/users/')[1] nickname=self.path.split('/users/')[1]
if '/' in nickname: if '/' in nickname:
nickname=nickname.split('/')[0] nickname=nickname.split('/')[0]
@ -3948,6 +3967,7 @@ def runDaemon(projectVersion, \
print(e) print(e)
httpd.outboxThread={} httpd.outboxThread={}
httpd.newPostThread={}
httpd.projectVersion=projectVersion httpd.projectVersion=projectVersion
httpd.authenticatedFetch=authenticatedFetch httpd.authenticatedFetch=authenticatedFetch
# max POST size of 30M # max POST size of 30M