Sending posts to group members

main2
Bob Mottram 2019-10-04 13:22:56 +01:00
parent cf854d0039
commit fa1f51a177
2 changed files with 87 additions and 29 deletions

112
inbox.py
View File

@ -760,7 +760,7 @@ def receiveUpdate(session,baseDir: str, \
return True return True
return False return False
def receiveLike(session,handle: str,baseDir: str, \ def receiveLike(session,handle: str,isGroup: bool,baseDir: str, \
httpPrefix: str,domain :str,port: int, \ httpPrefix: str,domain :str,port: int, \
sendThreads: [],postLog: [],cachedWebfingers: {}, \ sendThreads: [],postLog: [],cachedWebfingers: {}, \
personCache: {},messageJson: {},federationList: [], \ personCache: {},messageJson: {},federationList: [], \
@ -807,7 +807,7 @@ def receiveLike(session,handle: str,baseDir: str, \
updateLikesCollection(postFilename,messageJson['object'],messageJson['actor'],debug) updateLikesCollection(postFilename,messageJson['object'],messageJson['actor'],debug)
return True return True
def receiveUndoLike(session,handle: str,baseDir: str, \ def receiveUndoLike(session,handle: str,isGroup: bool,baseDir: str, \
httpPrefix: str,domain :str,port: int, \ httpPrefix: str,domain :str,port: int, \
sendThreads: [],postLog: [],cachedWebfingers: {}, \ sendThreads: [],postLog: [],cachedWebfingers: {}, \
personCache: {},messageJson: {},federationList: [], \ personCache: {},messageJson: {},federationList: [], \
@ -856,7 +856,7 @@ def receiveUndoLike(session,handle: str,baseDir: str, \
undoLikesCollectionEntry(postFilename,messageJson['object'],messageJson['actor'],debug) undoLikesCollectionEntry(postFilename,messageJson['object'],messageJson['actor'],debug)
return True return True
def receiveDelete(session,handle: str,baseDir: str, \ def receiveDelete(session,handle: str,isGroup: bool,baseDir: str, \
httpPrefix: str,domain :str,port: int, \ httpPrefix: str,domain :str,port: int, \
sendThreads: [],postLog: [],cachedWebfingers: {}, \ sendThreads: [],postLog: [],cachedWebfingers: {}, \
personCache: {},messageJson: {},federationList: [], \ personCache: {},messageJson: {},federationList: [], \
@ -922,7 +922,7 @@ def receiveDelete(session,handle: str,baseDir: str, \
print('DEBUG: post deleted - '+postFilename) print('DEBUG: post deleted - '+postFilename)
return True return True
def receiveAnnounce(session,handle: str,baseDir: str, \ def receiveAnnounce(session,handle: str,isGroup: bool,baseDir: str, \
httpPrefix: str,domain :str,port: int, \ httpPrefix: str,domain :str,port: int, \
sendThreads: [],postLog: [],cachedWebfingers: {}, \ sendThreads: [],postLog: [],cachedWebfingers: {}, \
personCache: {},messageJson: {},federationList: [], \ personCache: {},messageJson: {},federationList: [], \
@ -1023,7 +1023,7 @@ def receiveAnnounce(session,handle: str,baseDir: str, \
print('DEBUG: announced/repeated post arrived in inbox') print('DEBUG: announced/repeated post arrived in inbox')
return True return True
def receiveUndoAnnounce(session,handle: str,baseDir: str, \ def receiveUndoAnnounce(session,handle: str,isGroup: bool,baseDir: str, \
httpPrefix: str,domain :str,port: int, \ httpPrefix: str,domain :str,port: int, \
sendThreads: [],postLog: [],cachedWebfingers: {}, \ sendThreads: [],postLog: [],cachedWebfingers: {}, \
personCache: {},messageJson: {},federationList: [], \ personCache: {},messageJson: {},federationList: [], \
@ -1243,6 +1243,53 @@ def replyNotify(baseDir: str,handle: str) -> None:
with open(replyFile, 'w') as fp: with open(replyFile, 'w') as fp:
fp.write('\n') fp.write('\n')
def groupHandle(baseDir: str,handle: str) -> bool:
"""Is the given account handle a group?
"""
actorFile=baseDir+'/accounts/'+handle+'.json'
if not os.path.isfile(actorFile):
return False
actorJson=None
try:
with open(actorFile, 'r') as fp:
actorJson=commentjson.load(fp)
except Exception as e:
print(e)
if not actorJson:
return False
return actorJson['type']=='Group'
def sendToGroupMembers(session,baseDir: str,handle: str,port: int,postJsonObject: {}, \
httpPrefix: str,federationList: [], \
sendThreads: [],postLog: [],cachedWebfingers: {}, \
personCache: {},debug: bool) -> None:
"""When a post arrives for a group send it out to the group members
"""
followersFile=baseDir+'/accounts/'+handle+'/followers.txt'
if not os.path.isfile(followersFile):
return
nickname=handle.split('@')[0]
domain=handle.split('@')[1]
if ':' in domain:
domain=domain.split(':')[0]
with open(followersFile, 'r') as groupMembers:
for memberHandle in groupMembers:
if memberHandle!=handle:
memberNickname=memberHandle.split('@')[0]
memberDomain=memberHandle.split('@')[0]
memberPort=port
if ':' in memberDomain:
memberPortStr=memberDomain.split(':')[1]
if memberPortStr.isdigit():
memberPort=int(memberPortStr)
memberDomain=memberDomain.split(':')[0]
sendSignedJson(postJsonObject,session,baseDir, \
nickname,domain,port, \
memberNickname,memberDomain,memberPort,None, \
httpPrefix,False,False,federationList, \
sendThreads,postLog,cachedWebfingers, \
personCache,debug,projectVersion)
def inboxAfterCapabilities(session,keyId: str,handle: str,messageJson: {}, \ def inboxAfterCapabilities(session,keyId: str,handle: str,messageJson: {}, \
baseDir: str,httpPrefix: str,sendThreads: [], \ baseDir: str,httpPrefix: str,sendThreads: [], \
postLog: [],cachedWebfingers: {},personCache: {}, \ postLog: [],cachedWebfingers: {},personCache: {}, \
@ -1258,7 +1305,9 @@ def inboxAfterCapabilities(session,keyId: str,handle: str,messageJson: {}, \
if '#' in actor: if '#' in actor:
actor=keyId.split('#')[0] actor=keyId.split('#')[0]
if receiveLike(session,handle, \ isGroup=groupHandle(baseDir,handle)
if receiveLike(session,handle,isGroup, \
baseDir,httpPrefix, \ baseDir,httpPrefix, \
domain,port, \ domain,port, \
sendThreads,postLog, \ sendThreads,postLog, \
@ -1271,7 +1320,7 @@ def inboxAfterCapabilities(session,keyId: str,handle: str,messageJson: {}, \
print('DEBUG: Like accepted from '+actor) print('DEBUG: Like accepted from '+actor)
return False return False
if receiveUndoLike(session,handle, \ if receiveUndoLike(session,handle,isGroup, \
baseDir,httpPrefix, \ baseDir,httpPrefix, \
domain,port, \ domain,port, \
sendThreads,postLog, \ sendThreads,postLog, \
@ -1284,7 +1333,7 @@ def inboxAfterCapabilities(session,keyId: str,handle: str,messageJson: {}, \
print('DEBUG: Undo like accepted from '+actor) print('DEBUG: Undo like accepted from '+actor)
return False return False
if receiveAnnounce(session,handle, \ if receiveAnnounce(session,handle,isGroup, \
baseDir,httpPrefix, \ baseDir,httpPrefix, \
domain,port, \ domain,port, \
sendThreads,postLog, \ sendThreads,postLog, \
@ -1296,7 +1345,7 @@ def inboxAfterCapabilities(session,keyId: str,handle: str,messageJson: {}, \
if debug: if debug:
print('DEBUG: Announce accepted from '+actor) print('DEBUG: Announce accepted from '+actor)
if receiveUndoAnnounce(session,handle, \ if receiveUndoAnnounce(session,handle,isGroup, \
baseDir,httpPrefix, \ baseDir,httpPrefix, \
domain,port, \ domain,port, \
sendThreads,postLog, \ sendThreads,postLog, \
@ -1309,7 +1358,7 @@ def inboxAfterCapabilities(session,keyId: str,handle: str,messageJson: {}, \
print('DEBUG: Undo announce accepted from '+actor) print('DEBUG: Undo announce accepted from '+actor)
return False return False
if receiveDelete(session,handle, \ if receiveDelete(session,handle,isGroup, \
baseDir,httpPrefix, \ baseDir,httpPrefix, \
domain,port, \ domain,port, \
sendThreads,postLog, \ sendThreads,postLog, \
@ -1332,37 +1381,44 @@ def inboxAfterCapabilities(session,keyId: str,handle: str,messageJson: {}, \
return True return True
if messageJson.get('postNickname'): if messageJson.get('postNickname'):
postJson=messageJson['post'] postJsonObject=messageJson['post']
else: else:
postJson=messageJson postJsonObject=messageJson
if validPostContent(postJson,maxMentions): if validPostContent(postJsonObject,maxMentions):
# create a DM notification file if needed if not isGroup:
if isDM(postJson): # create a DM notification file if needed
dmNotify(baseDir,handle) if isDM(postJsonObject):
dmNotify(baseDir,handle)
# get the actor being replied to # get the actor being replied to
domainFull=domain domainFull=domain
if port: if port:
if ':' not in domain: if ':' not in domain:
if port!=80 and port!=443: if port!=80 and port!=443:
domainFull=domainFull+':'+str(port) domainFull=domainFull+':'+str(port)
actor=httpPrefix+'://'+domainFull+'/users/'+handle.split('@')[0] actor=httpPrefix+'://'+domainFull+'/users/'+handle.split('@')[0]
# create a reply notification file if needed # create a reply notification file if needed
if isReply(postJson,actor): if isReply(postJsonObject,actor):
replyNotify(baseDir,handle) replyNotify(baseDir,handle)
# get the avatar for a reply/announce # get the avatar for a reply/announce
obtainAvatarForReplyPost(session,baseDir,httpPrefix,domain,personCache,postJson,debug) obtainAvatarForReplyPost(session,baseDir,httpPrefix,domain,personCache,postJsonObject,debug)
# save the post to file # save the post to file
try: try:
with open(destinationFilename, 'w+') as fp: with open(destinationFilename, 'w+') as fp:
commentjson.dump(postJson, fp, indent=4, sort_keys=False) commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False)
except Exception as e: except Exception as e:
print(e) print(e)
# send the post out to group members
if isGroup:
sendToGroupMembers(session,baseDir,handle,port,postJsonObject, \
httpPrefix,federationList,sendThreads, \
postLog,cachedWebfingers,personCache,debug)
# if the post wasn't saved # if the post wasn't saved
if not os.path.isfile(destinationFilename): if not os.path.isfile(destinationFilename):
return False return False

View File

@ -1314,9 +1314,11 @@ def sendSignedJson(postJsonObject: {},session,baseDir: str, \
postPath,httpPrefix,withDigest,postJsonStr) postPath,httpPrefix,withDigest,postJsonStr)
# Keep the number of threads being used small # Keep the number of threads being used small
while len(sendThreads)>10: while len(sendThreads)>20:
print('WARN: Maximum threads reached - killing send thread')
sendThreads[0].kill() sendThreads[0].kill()
sendThreads.pop(0) sendThreads.pop(0)
print('WARN: thread killed')
if debug: if debug:
print('DEBUG: starting thread to send post') print('DEBUG: starting thread to send post')
pprint(postJsonObject) pprint(postJsonObject)