From 5436d980a104b5c20264f44ab9ae881250bdf1b0 Mon Sep 17 00:00:00 2001 From: Jessica Tallon Date: Tue, 2 Dec 2014 14:50:35 +0000 Subject: [PATCH] Fix #1024 - Correctly set generator on Activities --- mediagoblin/decorators.py | 6 ++--- mediagoblin/federation/views.py | 12 +++++++++ mediagoblin/submit/lib.py | 9 +++++-- mediagoblin/tools/federation.py | 44 +++++++++++++++++++++++++++------ 4 files changed, 58 insertions(+), 13 deletions(-) diff --git a/mediagoblin/decorators.py b/mediagoblin/decorators.py index f3be679d..eab09ec3 100644 --- a/mediagoblin/decorators.py +++ b/mediagoblin/decorators.py @@ -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) diff --git a/mediagoblin/federation/views.py b/mediagoblin/federation/views.py index 13f93985..9823fffe 100644 --- a/mediagoblin/federation/views.py +++ b/mediagoblin/federation/views.py @@ -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) diff --git a/mediagoblin/submit/lib.py b/mediagoblin/submit/lib.py index ea05e00f..1469bc6a 100644 --- a/mediagoblin/submit/lib.py +++ b/mediagoblin/submit/lib.py @@ -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) diff --git a/mediagoblin/tools/federation.py b/mediagoblin/tools/federation.py index 890e8801..6c2d27da 100644 --- a/mediagoblin/tools/federation.py +++ b/mediagoblin/tools/federation.py @@ -16,7 +16,34 @@ 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) -- 2.25.1