From: Jessica Tallon Date: Tue, 9 Dec 2014 19:04:55 +0000 (+0000) Subject: Fix #1026 - Add inbox feed with major, minor and direct endpoints X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=f2698759cd291512b458c9d785c7c67ec9630ee2;p=mediagoblin.git Fix #1026 - Add inbox feed with major, minor and direct endpoints --- diff --git a/mediagoblin/federation/routing.py b/mediagoblin/federation/routing.py index 2f8ed799..dddb5df5 100644 --- a/mediagoblin/federation/routing.py +++ b/mediagoblin/federation/routing.py @@ -45,7 +45,37 @@ add_route( add_route( "mediagoblin.federation.inbox", "/api/user//inbox", - "mediagoblin.federation.views:feed_endpoint" + "mediagoblin.federation.views:inbox_endpoint" +) + +add_route( + "mediagoblin.federation.inbox_minor", + "/api/user//inbox/minor", + "mediagoblin.federation.views:inbox_minor_endpoint" +) + +add_route( + "mediagoblin.federation.inbox_major", + "/api/user//inbox/major", + "mediagoblin.federation.views:inbox_major_endpoint" +) + +add_route( + "mediagoblin.federation.inbox_direct", + "/api/user//inbox/direct", + "mediagoblin.federation.views:inbox_endpoint" +) + +add_route( + "mediagoblin.federation.inbox_direct_minor", + "/api/user//inbox/direct/minor", + "mediagoblin.federation.views:inbox_minor_endpoint" +) + +add_route( + "mediagoblin.federation.inbox_direct_major", + "/api/user//inbox/direct/major", + "mediagoblin.federation.views:inbox_major_endpoint" ) # object endpoints @@ -88,4 +118,4 @@ add_route( "mediagoblin.federation.activity_view", "//activity/", "mediagoblin.federation.views:activity_view" -) \ No newline at end of file +) diff --git a/mediagoblin/federation/views.py b/mediagoblin/federation/views.py index 26187266..fadf3294 100644 --- a/mediagoblin/federation/views.py +++ b/mediagoblin/federation/views.py @@ -1,4 +1,4 @@ -# GN MediaGoblin -- federated, autonomous media hosting +# GNU MediaGoblin -- federated, autonomous media hosting # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. # # This program is free software: you can redistribute it and/or modify @@ -130,6 +130,84 @@ def uploads_endpoint(request): return json_error("Not yet implemented", 501) +@oauth_required +@csrf_exempt +def inbox_endpoint(request, inbox=None): + """ This is the user's inbox + + Currently because we don't have the ability to represent the inbox in the + database this is not a "real" inbox in the pump.io/Activity streams 1.0 + sense but instead just gives back all the data on the website + + inbox: allows you to pass a query in to limit inbox scope + """ + username = request.matchdict["username"] + user = User.query.filter_by(username=username).first() + + if user is None: + return json_error("No such 'user' with id '{0}'".format(username), 404) + + + # Only the user who's authorized should be able to read their inbox + if user.id != request.user.id: + return json_error( + "Only '{0}' can read this inbox.".format(user.username), + 403 + ) + + if inbox is None: + inbox = Activity.query.all() + + # We want to make a query for all media on the site and then apply GET + # limits where we can. + inbox = inbox.order_by(Activity.published.desc()) + + # Limit by the "count" (default: 20) + inbox = inbox.limit(request.args.get("count", 20)) + + # Offset (default: no offset - first results) + inbox = inbox.offset(request.args.get("offset", 0)) + + # build the inbox feed + feed = { + "displayName": "Activities for {0}".format(user.username), + "author": user.serialize(request), + "objectTypes": ["activity"], + "url": request.base_url, + "links": {"self": {"href": request.url}}, + "items": [], + } + + for activity in inbox: + try: + feed["items"].append(activity.serialize(request)) + except AttributeError: + # As with the feed endpint this occurs because of how we our + # hard-deletion method. Some activites might exist where the + # Activity object and/or target no longer exist, for this case we + # should just skip them. + pass + + feed["totalItems"] = len(feed["items"]) + return json_response(feed) + +@oauth_required +@csrf_exempt +def inbox_minor_endpoint(request): + """ Inbox subset for less important Activities """ + inbox = Activity.query.filter( + (Activity.verb == "update") | (Activity.verb == "delete") + ) + + return inbox_endpoint(request=request, inbox=inbox) + +@oauth_required +@csrf_exempt +def inbox_major_endpoint(request): + """ Inbox subset for most important Activities """ + inbox = Activity.query.filter_by(verb="post") + return inbox_endpoint(request=request, inbox=inbox) + @oauth_required @csrf_exempt def feed_endpoint(request):