federation.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # GNU MediaGoblin -- federated, autonomous media hosting
  2. # Copyright (C) 2014 MediaGoblin contributors. See AUTHORS.
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. from mediagoblin.db.models import Activity, Generator, User
  17. def create_generator(request):
  18. """
  19. This creates a Generator object based on the Client associated with the
  20. OAuth credentials used. If the request has invalid OAuth credentials or
  21. no OAuth credentials None is returned.
  22. """
  23. if not hasattr(request, "access_token"):
  24. return None
  25. client = request.access_token.get_requesttoken.get_client
  26. # Check if there is a generator already
  27. generator = Generator.query.filter_by(
  28. name=client.application_name,
  29. object_type="client"
  30. ).first()
  31. if generator is None:
  32. generator = Generator(
  33. name=client.application_name,
  34. object_type="client"
  35. )
  36. generator.save()
  37. return generator
  38. def create_activity(verb, obj, actor, target=None, generator=None):
  39. """
  40. This will create an Activity object which for the obj if possible
  41. and save it. The verb should be one of the following:
  42. add, author, create, delete, dislike, favorite, follow
  43. like, post, share, unfollow, unfavorite, unlike, unshare,
  44. update, tag.
  45. If none of those fit you might not want/need to create an activity for
  46. the object. The list is in mediagoblin.db.models.Activity.VALID_VERBS
  47. """
  48. # exception when we try and generate an activity with an unknow verb
  49. # could change later to allow arbitrary verbs but at the moment we'll play
  50. # it safe.
  51. if verb not in Activity.VALID_VERBS:
  52. raise ValueError("A invalid verb type has been supplied.")
  53. if generator is None:
  54. # This should exist as we're creating it by the migration for Generator
  55. generator = Generator.query.filter_by(name="GNU MediaGoblin").first()
  56. if generator is None:
  57. generator = Generator(
  58. name="GNU MediaGoblin",
  59. object_type="service"
  60. )
  61. generator.save()
  62. # Ensure the object has an ID which is needed by the activity.
  63. obj.save(commit=False)
  64. # Create the activity
  65. activity = Activity(verb=verb)
  66. activity.object = obj
  67. if target is not None:
  68. activity.target = target
  69. # If they've set it override the actor from the obj.
  70. activity.actor = actor.id if isinstance(actor, User) else actor
  71. activity.generator = generator.id
  72. activity.save()
  73. # Sigh want to do this prior to save but I can't figure a way to get
  74. # around relationship() not looking up object when model isn't saved.
  75. if activity.generate_content():
  76. activity.save()
  77. return activity