forgefed_model.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import re
  2. from activitypub.database import ListDatabase
  3. from activitypub.manager import Manager
  4. from forgefed_constants import AP_NS_URLS , AP_SEC_URL , HOSTNAME , FOREIGN_ID_REGEX , \
  5. LOCAL_CONFIG , LOCAL_ID_REGEX , PATH_PREFIX , PROTOCOL , \
  6. PUBLIC_KEY , URL_REGEX
  7. ## getters/setters ##
  8. def CreatePerson(person_id , actor_url='' , inbox_url=''):
  9. if actor_url == '' and inbox_url == '' : # local actor
  10. person_id = re.sub(LOCAL_ID_REGEX , '' , person_id) + '@' + HOSTNAME
  11. person_params = { 'id' : person_id }
  12. elif actor_url != '' and inbox_url != '' and \
  13. re.search(FOREIGN_ID_REGEX , person_id) : # foreign actor
  14. person_params = { 'id' : person_id , 'actor' : actor_url , 'inbox' : inbox_url }
  15. else: raise ValueError("invalid Person params: '" + "','".join([person_id , actor_url , inbox_url]) + "'")
  16. person = GetPerson(person_id)
  17. if person == None:
  18. # TODO: publicKey and preferredUsername belong in the activitypub library
  19. person = ApManager.Person(**person_params)
  20. person.preferredUsername = person_id
  21. person.publicKey = \
  22. { \
  23. "id" : person.url + '#rsa-pub-key' , \
  24. "owner" : person.url , \
  25. "publicKeyPem" : PUBLIC_KEY.decode() \
  26. }
  27. #Db.actors.insert_one(person.to_dict())
  28. Actors[person_id] = person
  29. if GetPerson(person_id) != None: print("created Person(" + person.id + ")")
  30. db_person = GetPerson(person_id)
  31. #print("person.publicKey=" + str(person.publicKey))
  32. #print("db_person.publicKey=" + str(db_person.publicKey))
  33. #dict_person = person.to_dict()
  34. #dict_person['publicKey'] = person.publicKey
  35. #print("dict_person.publicKey=" + str(dict_person['publicKey']))
  36. return person
  37. def NewNote(from_id , to_id , body , media_type='text/plain'):
  38. note_params = \
  39. { \
  40. 'from_id' : from_id , \
  41. 'to' : [ to_id ] , \
  42. 'cc' : [ from_id + "/followers" ] , \
  43. 'tag' : [] , \
  44. 'source' : { 'mediaType' : media_type , \
  45. 'content' : body } , \
  46. 'sensitive' : False , \
  47. 'temp_uuid' : "$UUID" , \
  48. 'temp_text' : body \
  49. }
  50. note = ApManager.Note(**note_params)
  51. print("created Note(" + note.id + ") " + from_id + " -> " + to_id)
  52. return note
  53. def GetPerson(person_id):
  54. #person_id = PROTOCOL + '://' + HOSTNAME + '/' + person_id
  55. #return Db.actors.find_one({'id' : person_id})
  56. return Actors[person_id] if person_id in Actors else None
  57. def GetActivity(activity_id):
  58. return Db.activities.find_one({'id' : activity_id})
  59. ## validations ##
  60. def IsValidNoteActivity(ap_dict):
  61. # DEBUG BEGIN
  62. #DbgTraceIsValidActivity(ap_dict)
  63. # DEBUG END
  64. return '@context' in ap_dict and ap_dict['@context'] == AP_NS_URLS and \
  65. 'attributedTo' in ap_dict and \
  66. 'cc' in ap_dict and \
  67. 'content' in ap_dict and \
  68. 'id' in ap_dict and \
  69. 'published' in ap_dict and \
  70. 'sensitive' in ap_dict and \
  71. 'source' in ap_dict and \
  72. 'mediaType' in ap_dict['source'] and \
  73. 'content' in ap_dict['source'] and \
  74. 'tag' in ap_dict and \
  75. 'to' in ap_dict and len(ap_dict['to']) == 1 and \
  76. 'type' in ap_dict and ap_dict['type'] == 'Note' and \
  77. 'url' in ap_dict
  78. def IsValidActivity(ap_dict):
  79. # DEBUG BEGIN
  80. #import json ; print("IsValidActivity() ap_dict=" + json.dumps(ap_dict))
  81. print("IsValidActivity() TODO:")
  82. # DEBUG END
  83. # TODO: validate baseline AP message
  84. return '@context' in ap_dict and ap_dict['@context'] == AP_NS_URLS and \
  85. 'attributedTo' in ap_dict and \
  86. 'content' in ap_dict and \
  87. 'id' in ap_dict and \
  88. 'to' in ap_dict and len(ap_dict['to']) == 1
  89. ## helpers ##
  90. def ParsePersonId(person_id):
  91. return re.sub(URL_REGEX , '' , person_id)
  92. ## setup ##
  93. Db = ListDatabase()
  94. ApManager = Manager(defaults=LOCAL_CONFIG , database=Db)
  95. Actors = {}
  96. # DEBUG BEGIN
  97. def DbgTraceIsValidActivity(ap_dict):
  98. if '@context' in ap_dict:
  99. print("'@context' in")
  100. else: print("context not in")
  101. if ap_dict['@context'] == AP_NS_URL:
  102. print("'@context' is")
  103. else: print("context not is")
  104. if 'attributedTo' in ap_dict:
  105. print("'attributedTo' in")
  106. else: print("attributedTo not in") ;
  107. if 'cc' in ap_dict:
  108. print("'cc' in")
  109. else: print("cc not in")
  110. if 'content' in ap_dict:
  111. print("'content' in")
  112. else: print("content not in")
  113. if 'id' in ap_dict:
  114. print("'id' in")
  115. else: print("id not in")
  116. if 'published' in ap_dict:
  117. print("'published' in")
  118. else: print("published not in")
  119. if 'sensitive' in ap_dict:
  120. print("'sensitive' in")
  121. else: print("sensitive not in")
  122. if 'source' in ap_dict:
  123. print("'source' in")
  124. else: print("source not in")
  125. if 'mediaType' in ap_dict['source']:
  126. print("'media_type' in")
  127. else: print("media not in")
  128. if 'content' in ap_dict['source']:
  129. print("'content' in")
  130. else: print("content not in")
  131. if 'tag' in ap_dict:
  132. print("'tag' in")
  133. else: print("tag not in")
  134. if 'to' in ap_dict:
  135. print("'to' in")
  136. else: print("to not in")
  137. if len(ap_dict['to']) == 1:
  138. print("to len ok")
  139. else: print("to len Nok")
  140. if 'type' in ap_dict:
  141. print("'type' in")
  142. else: print("'type' not in")
  143. if ap_dict['type'] == 'Note':
  144. print("'type'" + ap_dict['type'] + " is")
  145. else: print("'type'" + ap_dict['type'] + " not is")
  146. if 'url' in ap_dict:
  147. print("'url' in")
  148. else: print("url not in")
  149. # DEBUG END