Fix #1024 - Correctly set generator on Activities
authorJessica Tallon <jessica@megworld.co.uk>
Tue, 2 Dec 2014 14:50:35 +0000 (14:50 +0000)
committerJessica Tallon <jessica@megworld.co.uk>
Tue, 2 Dec 2014 14:51:24 +0000 (14:51 +0000)
mediagoblin/decorators.py
mediagoblin/federation/views.py
mediagoblin/submit/lib.py
mediagoblin/tools/federation.py

index f3be679d86fe493a575623b540ff333db1e9c82d..eab09ec38840d184e6c44c5b08de9e2e062fbc69 100644 (file)
@@ -419,9 +419,9 @@ def oauth_required(controller):
 
         # Fill user if not already
         token = authorization[u"oauth_token"]
-        access_token = AccessToken.query.filter_by(token=token).first()
-        if access_token is not None and request.user is None:
-            user_id = access_token.user
+        request.access_token = AccessToken.query.filter_by(token=token).first()
+        if request.access_token is not None and request.user is None:
+            user_id = request.access_token.user
             request.user = User.query.filter_by(id=user_id).first()
 
         return controller(request, *args, **kwargs)
index 13f93985c175603bf5a565055afbf18533d5ebf4..9823fffe3696d689f290e5dd04556456a8b62596 100644 (file)
@@ -23,6 +23,7 @@ from werkzeug.datastructures import FileStorage
 from mediagoblin.decorators import oauth_required, require_active_login
 from mediagoblin.federation.decorators import user_has_privilege
 from mediagoblin.db.models import User, MediaEntry, MediaComment, Activity
+from mediagoblin.tools.federation import create_activity, create_generator
 from mediagoblin.tools.routing import extract_url_arguments
 from mediagoblin.tools.response import redirect, json_response, json_error, \
                                        render_404, render_to_response
@@ -180,6 +181,17 @@ def feed_endpoint(request):
                 comment = MediaComment(author=request.user.id)
                 comment.unserialize(data["object"], request)
                 comment.save()
+                
+                # Create activity for comment
+                generator = create_generator(request)
+                activity = create_activity(
+                    verb="post",
+                    actor=request.user,
+                    obj=comment,
+                    target=comment.get_entry,
+                    generator=generator
+                )
+
                 data = {
                     "verb": "post",
                     "object": comment.serialize(request)
index ea05e00fa24dfe363099ec8d7080521a333016f5..1469bc6a5e8a4e6c8d12d1e0b7eb778807fa6279 100644 (file)
@@ -26,7 +26,7 @@ from werkzeug.datastructures import FileStorage
 from mediagoblin import mg_globals
 from mediagoblin.tools.response import json_response
 from mediagoblin.tools.text import convert_to_tag_list_of_dicts
-from mediagoblin.tools.federation import create_activity
+from mediagoblin.tools.federation import create_activity, create_generator
 from mediagoblin.db.models import MediaEntry, ProcessingMetaData
 from mediagoblin.processing import mark_entry_failed
 from mediagoblin.processing.task import ProcessMedia
@@ -294,7 +294,12 @@ def api_add_to_feed(request, entry):
     add_comment_subscription(request.user, entry)
 
     # Create activity
-    create_activity("post", entry, entry.uploader)
+    create_activity(
+        verb="post",
+        obj=entry,
+        actor=entry.uploader,
+        generator=create_generator(request)
+    )
     entry.save()
     run_process_media(entry, feed_url)
 
index 890e88010bb1c369ac837adb61cb65bfa188a13d..6c2d27da1cc0cde6d72f1dadd54519d9498b5c88 100644 (file)
 
 from mediagoblin.db.models import Activity, Generator, User
 
-def create_activity(verb, obj, actor, target=None):
+def create_generator(request):
+    """
+    This creates a Generator object based on the Client associated with the
+    OAuth credentials used. If the request has invalid OAuth credentials or
+    no OAuth credentials None is returned.
+    """
+    if not hasattr(request, "access_token"):
+        return None
+
+    client = request.access_token.get_requesttoken.get_client
+    
+    # Check if there is a generator already
+    generator = Generator.query.filter_by(
+        name=client.application_name,
+        object_type="client"
+    ).first()
+    if generator is None:
+        generator = Generator(
+            name=client.application_name,
+            object_type="client"
+        )
+        generator.save()
+
+    return generator
+    
+     
+
+def create_activity(verb, obj, actor, target=None, generator=None):
     """
     This will create an Activity object which for the obj if possible
     and save it. The verb should be one of the following:
@@ -34,14 +61,15 @@ def create_activity(verb, obj, actor, target=None):
     if verb not in Activity.VALID_VERBS:
         raise ValueError("A invalid verb type has been supplied.")
 
-    # This should exist as we're creating it by the migration for Generator
-    generator = Generator.query.filter_by(name="GNU MediaGoblin").first()
     if generator is None:
-        generator = Generator(
-            name="GNU MediaGoblin",
-            object_type="service"
-        )
-        generator.save()
+        # This should exist as we're creating it by the migration for Generator
+        generator = Generator.query.filter_by(name="GNU MediaGoblin").first()
+        if generator is None:
+            generator = Generator(
+                name="GNU MediaGoblin",
+                object_type="service"
+            )
+            generator.save()
 
     activity = Activity(verb=verb)
     activity.set_object(obj)