person.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import celery
  2. import json
  3. import pagure
  4. import rdflib
  5. from .. import activitypub
  6. from .. import model
  7. from .. import settings
  8. from . import broker
  9. from . import broker_url
  10. from . import database_session
  11. log = celery.utils.log.get_task_logger(__name__)
  12. log.setLevel(settings.LOG_LEVEL)
  13. @broker.task
  14. def handle_incoming_activity(person_id, activity):
  15. """
  16. A person has received a new Activity in its INBOX. This task is scheduled
  17. to look at that activity and update the Pagure database based on the
  18. Activity content.
  19. """
  20. with database_session() as (pagure_db, forgefed_graph):
  21. person = pagure_db.query(model.Person) \
  22. .filter(model.Person.id == person_id) \
  23. .one_or_none()
  24. # If the person doesn't exist, it means it was deleted after it received
  25. # an Activity but before the Activity was handled.
  26. if not person:
  27. log.info('Actor id(' + person_id + ') doesn\'t exist. Incoming '
  28. 'Activity will be ignored.')
  29. return
  30. if activity['type'] == 'Create':
  31. """
  32. The Person has received a Create Activity.
  33. """
  34. # Dereference the actor
  35. # actor = activitypub.fetch(activity['actor'])
  36. # Retrieve the object of the Activity
  37. object = forgefed_graph.get_json_node(activity['object'])
  38. if object['type'] == 'Note':
  39. log.debug('Somebody has Created a new Note.')
  40. model.test_or_set_remote_comment(pagure_db, forgefed_graph, activity['object'])
  41. if activity['type'] == 'Follow':
  42. """
  43. Somebody wants to follow this Person.
  44. """
  45. if activity['object'] != person.local_uri:
  46. log.info('Actor ' + person.local_uri + ' has received a Follow '
  47. 'request but the "object" doesn\'t match. '
  48. 'Activity will be ignored.')
  49. return
  50. if forgefed_graph.collection_contains(person.followers_uri,
  51. activity['actor']):
  52. log.info('Actor ' + person.local_uri + ' is already following '
  53. + activity['actor'] + '. Will ignore Follow request.')
  54. return
  55. # Add the remote object to our followers collection
  56. forgefed_graph.add_collection_item(person.followers_uri, activity['actor'])
  57. # Commit before accepting the Activity because we want the new
  58. # follower in the Followers collection before sending out the
  59. # Accept Activity.
  60. forgefed_graph.commit()
  61. # Automatically accept Follow requests.
  62. person.accept(activity['id'])
  63. if activity['type'] == 'Accept':
  64. """
  65. Somebody has sent us an Accept activity.
  66. """
  67. # What was accepted? Check if we have the "object" in our graph
  68. accepted_object = forgefed_graph.get_json_node(activity['object'])
  69. if not accepted_object:
  70. return
  71. if accepted_object['type'] == 'Offer':
  72. # Retrieve the "object" node of the Offer
  73. accepted_object['object'] = forgefed_graph.get_json_node(accepted_object['object'])
  74. # The remote actor has accepted our Ticket offer
  75. if accepted_object['type'] == 'Offer' \
  76. and accepted_object['actor'] == person.local_uri \
  77. and accepted_object['object']['type'] == 'Ticket' \
  78. and 'result' in activity \
  79. and activity['actor'] == accepted_object['to']:
  80. ticket = accepted_object['object']
  81. forgefed_graph.set((
  82. rdflib.URIRef(ticket['id']),
  83. rdflib.OWL.sameAs,
  84. rdflib.URIRef(activity['result'])))
  85. log.debug('The ticket {} was accepted. A new ticket was created at {}'.format(ticket['id'], activity['result']))