manualapprove.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. __filename__ = "manualapprove.py"
  2. __author__ = "Bob Mottram"
  3. __license__ = "AGPL3+"
  4. __version__ = "0.0.1"
  5. __maintainer__ = "Bob Mottram"
  6. __email__ = "bob@freedombone.net"
  7. __status__ = "Production"
  8. import os
  9. import json
  10. import commentjson
  11. from follow import followedAccountAccepts
  12. def manualDenyFollowRequest(baseDir: str,nickname: str,domain: str,denyHandle: str) -> None:
  13. """Manually deny a follow request
  14. """
  15. handle=nickname+'@'+domain
  16. accountsDir=baseDir+'/accounts/'+handle
  17. approveFollowsFilename=accountsDir+'/followrequests.txt'
  18. if not os.path.isfile(approveFollowsFilename):
  19. if debug:
  20. print('WARN: Follow requests file '+approveFollowsFilename+' not found')
  21. return
  22. if denyHandle not in open(approveFollowsFilename).read():
  23. return
  24. approvefilenew = open(approveFollowsFilename+'.new', 'w+')
  25. with open(approveFollowsFilename, 'r') as approvefile:
  26. for approveHandle in approvefile:
  27. if not approveHandle.startswith(denyHandle):
  28. approvefilenew.write(approveHandle)
  29. approvefilenew.close()
  30. os.rename(approveFollowsFilename+'.new',approveFollowsFilename)
  31. print('Follow request from '+denyHandle+' was denied.')
  32. def manualApproveFollowRequest(session,baseDir: str, \
  33. httpPrefix: str,
  34. nickname: str,domain: str,port: int, \
  35. approveHandle: str, \
  36. federationList: [], \
  37. sendThreads: [],postLog: [], \
  38. cachedWebfingers: {},personCache: {}, \
  39. acceptedCaps: [], \
  40. debug: bool, \
  41. projectVersion: str) -> None:
  42. """Manually approve a follow request
  43. """
  44. handle=nickname+'@'+domain
  45. accountsDir=baseDir+'/accounts/'+handle
  46. approveFollowsFilename=accountsDir+'/followrequests.txt'
  47. if not os.path.isfile(approveFollowsFilename):
  48. if debug:
  49. print('WARN: Follow requests file '+approveFollowsFilename+' not found')
  50. return
  51. if approveHandle not in open(approveFollowsFilename).read():
  52. if debug:
  53. print(handle+' not in '+approveFollowsFilename)
  54. return
  55. approvefilenew = open(approveFollowsFilename+'.new', 'w+')
  56. with open(approveFollowsFilename, 'r') as approvefile:
  57. for handle in approvefile:
  58. if handle.startswith(approveHandle):
  59. handle=handle.replace('\n','')
  60. port2=port
  61. if ':' in handle:
  62. port2=int(handle.split(':')[1])
  63. requestsDir=accountsDir+'/requests'
  64. followActivityfilename=requestsDir+'/'+handle+'.follow'
  65. if os.path.isfile(followActivityfilename):
  66. with open(followActivityfilename, 'r') as fp:
  67. followJson=commentjson.load(fp)
  68. approveNickname=approveHandle.split('@')[0]
  69. approveDomain=approveHandle.split('@')[1].replace('\n','')
  70. approvePort=port2
  71. if ':' in approveDomain:
  72. approvePort=approveDomain.split(':')[1]
  73. approveDomain=approveDomain.split(':')[0]
  74. followedAccountAccepts(session,baseDir,httpPrefix, \
  75. nickname,domain,port, \
  76. approveNickname,approveDomain,approvePort, \
  77. followJson['actor'],federationList, \
  78. followJson,acceptedCaps, \
  79. sendThreads,postLog, \
  80. cachedWebfingers,personCache, \
  81. debug,projectVersion)
  82. os.remove(followActivityfilename)
  83. else:
  84. approvefilenew.write(handle)
  85. approvefilenew.close()
  86. os.rename(approveFollowsFilename+'.new',approveFollowsFilename)