Add basic comment support and flesh out some other endpoints
authorJessica Tallon <zslrd@inboxen.co.uk>
Wed, 4 Sep 2013 18:34:29 +0000 (19:34 +0100)
committerJessica Tallon <jessica@megworld.co.uk>
Tue, 22 Jul 2014 22:13:15 +0000 (23:13 +0100)
mediagoblin/db/models.py
mediagoblin/decorators.py
mediagoblin/federation/routing.py
mediagoblin/federation/views.py

index cc22450f0f62081993a0f662d1d9f2ae8fded054..215e7552f242173302224c82d34026caba8699b3 100644 (file)
@@ -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,
index 040a11fa88989d18cb34c853fa45362fc4661a46..5cba6fee8c6a5c4cdf1fa12e7ac8337a67da7cb6 100644 (file)
@@ -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
index be6451e0009695d165e15aacd9aa992ea6b0e57f..123067666c12576dc574cebef12e472e6d2a1703 100644 (file)
@@ -39,7 +39,7 @@ add_route(
 add_route(
     "mediagoblin.federation.inbox",
     "/api/user/<string:username>/inbox",
-    "mediagoblin.federation.views:inbox"
+    "mediagoblin.federation.views:feed"
     )
 
 # object endpoints
index c84956c371ba31d005593037cf3f730844adf06b..cff3d499e3a4d32a111c8546497ab079d25c741c 100644 (file)
@@ -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/<username>/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