Remove <br/> from safe string indicating that CSRF cooking is missing.
[larjonas-mediagoblin.git] / mediagoblin / tools / federation.py
blobf2ee468cbb57caca86f7b948a6762329d4310a17
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2014 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
17 from mediagoblin.db.models import Activity, Generator, User
19 def create_generator(request):
20 """
21 This creates a Generator object based on the Client associated with the
22 OAuth credentials used. If the request has invalid OAuth credentials or
23 no OAuth credentials None is returned.
24 """
25 if not hasattr(request, "access_token"):
26 return None
28 client = request.access_token.get_requesttoken.get_client
30 # Check if there is a generator already
31 generator = Generator.query.filter_by(
32 name=client.application_name,
33 object_type="client"
34 ).first()
35 if generator is None:
36 generator = Generator(
37 name=client.application_name,
38 object_type="client"
40 generator.save()
42 return generator
46 def create_activity(verb, obj, actor, target=None, generator=None):
47 """
48 This will create an Activity object which for the obj if possible
49 and save it. The verb should be one of the following:
50 add, author, create, delete, dislike, favorite, follow
51 like, post, share, unfollow, unfavorite, unlike, unshare,
52 update, tag.
54 If none of those fit you might not want/need to create an activity for
55 the object. The list is in mediagoblin.db.models.Activity.VALID_VERBS
56 """
57 # exception when we try and generate an activity with an unknow verb
58 # could change later to allow arbitrary verbs but at the moment we'll play
59 # it safe.
61 if verb not in Activity.VALID_VERBS:
62 raise ValueError("A invalid verb type has been supplied.")
64 if generator is None:
65 # This should exist as we're creating it by the migration for Generator
66 generator = Generator.query.filter_by(name="GNU MediaGoblin").first()
67 if generator is None:
68 generator = Generator(
69 name="GNU MediaGoblin",
70 object_type="service"
72 generator.save()
74 # Ensure the object has an ID which is needed by the activity.
75 obj.save(commit=False)
77 # Create the activity
78 activity = Activity(verb=verb)
79 activity.object = obj
81 if target is not None:
82 activity.target = target
84 # If they've set it override the actor from the obj.
85 activity.actor = actor.id if isinstance(actor, User) else actor
86 activity.generator = generator.id
87 activity.save()
89 # Sigh want to do this prior to save but I can't figure a way to get
90 # around relationship() not looking up object when model isn't saved.
91 if activity.generate_content():
92 activity.save()
94 return activity