From c894b4246a1211e7d8e63e7c51b8d7095482a10c Mon Sep 17 00:00:00 2001 From: Jessica Tallon Date: Wed, 4 Sep 2013 19:34:29 +0100 Subject: [PATCH] Add basic comment support and flesh out some other endpoints --- mediagoblin/db/models.py | 9 +--- mediagoblin/decorators.py | 9 +++- mediagoblin/federation/routing.py | 2 +- mediagoblin/federation/views.py | 82 +++++++++++++++++++++++++++++-- 4 files changed, 89 insertions(+), 13 deletions(-) diff --git a/mediagoblin/db/models.py b/mediagoblin/db/models.py index cc22450f..215e7552 100644 --- a/mediagoblin/db/models.py +++ b/mediagoblin/db/models.py @@ -445,15 +445,8 @@ class MediaEntry(Base, MediaEntryMixin): qualified=True ) - id = request.urlgen( - "mediagoblin.federation.object", - objectType=self.objectType, - uuid=self.slug, - qualified=True - ) - context = { - "id": id, + "id": self.id, "author": author.serialize(request), "displayName": self.title, "objectType": self.objectType, diff --git a/mediagoblin/decorators.py b/mediagoblin/decorators.py index 040a11fa..5cba6fee 100644 --- a/mediagoblin/decorators.py +++ b/mediagoblin/decorators.py @@ -22,7 +22,7 @@ from oauthlib.oauth1 import ResourceEndpoint from mediagoblin import mg_globals as mgg from mediagoblin import messages -from mediagoblin.db.models import MediaEntry, User, MediaComment +from mediagoblin.db.models import MediaEntry, User, MediaComment, AccessToken from mediagoblin.tools.response import ( redirect, render_404, render_user_banned, json_response) @@ -412,6 +412,13 @@ def oauth_required(controller): error = "Invalid oauth prarameter." return json_response({"error": error}, status=400) + # 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.user = User.query.filter_by(id=user_id).first() + return controller(request, *args, **kwargs) return wrapper diff --git a/mediagoblin/federation/routing.py b/mediagoblin/federation/routing.py index be6451e0..12306766 100644 --- a/mediagoblin/federation/routing.py +++ b/mediagoblin/federation/routing.py @@ -39,7 +39,7 @@ add_route( add_route( "mediagoblin.federation.inbox", "/api/user//inbox", - "mediagoblin.federation.views:inbox" + "mediagoblin.federation.views:feed" ) # object endpoints diff --git a/mediagoblin/federation/views.py b/mediagoblin/federation/views.py index c84956c3..cff3d499 100644 --- a/mediagoblin/federation/views.py +++ b/mediagoblin/federation/views.py @@ -1,6 +1,9 @@ +import json + from mediagoblin.decorators import oauth_required -from mediagoblin.db.models import User, MediaEntry +from mediagoblin.db.models import User, MediaEntry, MediaComment from mediagoblin.tools.response import redirect, json_response +from mediagoblin.meddleware.csrf import csrf_exempt #@oauth_required def profile(request, raw=False): @@ -34,8 +37,10 @@ def user(request): return json_response(data) @oauth_required +@csrf_exempt def feed(request): """ Handles the user's outbox - /api/user//feed """ + print request.user user = request.matchdict["username"] requested_user = User.query.filter_by(username=user) @@ -44,10 +49,76 @@ def feed(request): error = "No such 'user' with id '{0}'".format(user) return json_response({"error": error}, status=404) - user = request_user[0] + user = requested_user[0] + + if request.method == "POST": + data = json.loads(request.data) + obj = data.get("object", None) + if obj is None: + error = {"error": "Could not find 'object' element."} + return json_response(error, status=400) + + if obj.get("objectType", None) == "comment": + # post a comment + media = int(data["object"]["inReplyTo"]["id"]) + author = request.user + comment = MediaComment( + media_entry=media, + author=request.user.id, + content=data["object"]["content"] + ) + comment.save() + elif obj.get("objectType", None) is None: + error = {"error": "No objectType specified."} + return json_response(error, status=400) + else: + error = {"error": "Unknown object type '{0}'.".format(obj.get("objectType", None))} + return json_response(error, status=400) + + feed_url = request.urlgen( + "mediagoblin.federation.feed", + username=user.username, + qualified=True + ) + + feed = { + "displayName": "Activities by {0}@{1}".format(user.username, request.host), + "objectTypes": ["activity"], + "url": feed_url, + "links": { + "first": { + "href": feed_url, + }, + "self": { + "href": request.url, + }, + "prev": { + "href": feed_url, + }, + "next": { + "href": feed_url, + } + }, + "author": user.serialize(request), + "items": [], + } + # Now lookup the user's feed. - raise NotImplemented("Yet to implement looking up user's feed") + for media in MediaEntry.query.all(): + feed["items"].append({ + "verb": "post", + "object": media.serialize(request), + "actor": user.serialize(request), + "content": "{0} posted a picture".format(user.username), + "id": 1, + }) + feed["items"][-1]["updated"] = feed["items"][-1]["object"]["updated"] + feed["items"][-1]["published"] = feed["items"][-1]["object"]["published"] + feed["items"][-1]["url"] = feed["items"][-1]["object"]["url"] + feed["totalItems"] = len(feed["items"]) + + return json_response(feed) @oauth_required def inbox(request): @@ -90,6 +161,11 @@ def object_comments(request): uuid=media.slug, qualified=True) }) + comments["displayName"] = "Replies to {0}".format(comments["url"]) + comments["links"] = { + "first": comments["url"], + "self": comments["url"], + } response = json_response(comments) return response -- 2.25.1