Fix some problems with activity mixins and migrations
[mediagoblin.git] / mediagoblin / tools / federation.py
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
17 from mediagoblin.db.models import Activity, Generator, User
18
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
27
28 client = request.access_token.get_requesttoken.get_client
29
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"
39 )
40 generator.save()
41
42 return generator
43
44
45
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.
53
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.
60
61 if verb not in Activity.VALID_VERBS:
62 raise ValueError("A invalid verb type has been supplied.")
63
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"
71 )
72 generator.save()
73
74 activity = Activity(verb=verb)
75 activity.object = obj
76
77 if target is not None:
78 activity.target = target
79
80 # If they've set it override the actor from the obj.
81 activity.actor = actor.id if isinstance(actor, User) else actor
82
83 activity.generator = generator.id
84 activity.save()
85
86 # Sigh want to do this prior to save but I can't figure a way to get
87 # around relationship() not looking up object when model isn't saved.
88 if activity.generate_content():
89 activity.save()
90
91 return activity