Skip to content

Commit

Permalink
twitter: add block list support to REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Jul 21, 2017
1 parent 98737f3 commit 7f2fbbe
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -99,7 +99,7 @@ They return the authenticated user's Twitter stream, ie tweets from the people t
}
```

The request parameters are the same for both, all optional: `USER_ID` is a source-specific id or `@me` for the authenticated user. `GROUP_ID` may be `@all`, `@friends` (currently identical to `@all`), `@self`, or `@search`; `APP_ID` is currently ignored; best practice is to use `@app` as a placeholder.
The request parameters are the same for both, all optional: `USER_ID` is a source-specific id or `@me` for the authenticated user. `GROUP_ID` may be `@all`, `@friends` (currently identical to `@all`), `@self`, `@search`, or `@blocks`; `APP_ID` is currently ignored; best practice is to use `@app` as a placeholder.

Paging is supported via the `startIndex` and `count` parameters. They're self explanatory, and described in detail in the [OpenSearch spec](http://www.opensearch.org/Specifications/OpenSearch/1.1#The_.22count.22_parameter) and [OpenSocial spec](https://opensocial.github.io/spec/2.0.1/Social-API-Server.xml#ActivityStreams-Service).

Expand Down
7 changes: 5 additions & 2 deletions activitystreams.py
Expand Up @@ -139,9 +139,12 @@ def get(self):
for a, defaults in zip(args, PATH_DEFAULTS)]
user_id = args[0] if args else None

# get activities
# get activities (etc)
try:
response = src.get_activities_response(*args, **self.get_kwargs(src))
if len(args) >= 2 and args[1] == '@blocks':
response = {'items': src.get_blocklist()}
else:
response = src.get_activities_response(*args, **self.get_kwargs(src))
except (NotImplementedError, ValueError) as e:
self.abort(400, str(e))
# other exceptions are handled by webutil.handlers.handle_exception(),
Expand Down
5 changes: 2 additions & 3 deletions app.py
Expand Up @@ -75,12 +75,11 @@ def get(self):
group = self.request.get('group_id') or source.ALL
user = self.request.get('user_id') or source.ME

activity_id = search_query = ''
if group == source.SEARCH:
search_query = self.request.get('search_query', '').encode('utf-8')
activity_id = ''
else:
elif group != source.BLOCKS:
activity_id = self.request.get('activity_id', '').encode('utf-8')
search_query = ''

params = {
'plaintext': 'true',
Expand Down
1 change: 1 addition & 0 deletions granary/source.py
Expand Up @@ -34,6 +34,7 @@
FRIENDS = '@friends'
APP = '@app'
SEARCH = '@search'
BLOCKS = '@blocks'

# values for create's include_link param
OMIT_LINK = 'omit'
Expand Down
5 changes: 3 additions & 2 deletions granary/templates/index.html
Expand Up @@ -33,7 +33,7 @@
}
window.addEventListener('hashchange', onHashChange);
document.addEventListener('DOMContentLoaded', onHashChange);
window.onload = update_search();
window.onload = update_form();
</script>

<!--
Expand Down Expand Up @@ -173,12 +173,13 @@ <h1>
<label> @self </label>
<input type="hidden" id="group_id" name="group_id" value="@self" />
{% else %}
<select id="group_id" name="group_id" onchange="update_search()">
<select id="group_id" name="group_id" onchange="update_form()">
<option value="@all">@all</option>
<option value="@self">@self</option>
<option value="@friends">@friends</option>
{% if site == 'twitter' %}
<option value="@search">@search</option>
<option value="@blocks">@blocks</option>
{% endif %}
</select>
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion granary/twitter.py
Expand Up @@ -36,7 +36,7 @@
from oauth_dropins.webutil import util

API_BASE = 'https://api.twitter.com/1.1/'
API_BLOCKS = 'blocks/list.json?skip_status=true&cursor=%s'
API_BLOCKS = 'blocks/list.json?skip_status=true&count=5000&cursor=%s'
API_CURRENT_USER = 'account/verify_credentials.json'
API_FAVORITES = 'favorites/list.json?screen_name=%s&include_entities=true&tweet_mode=extended'
API_LIST_TIMELINE = 'lists/statuses.json?include_entities=true&tweet_mode=extended&count=%(count)d&slug=%(slug)s&owner_screen_name=%(owner_screen_name)s'
Expand Down
22 changes: 14 additions & 8 deletions static/demo.js
Expand Up @@ -40,14 +40,20 @@ function render_url_request() {
'GET <a href="' + url + '">' + url + '</a>';
}

function update_search() {
group_id = document.getElementById('group_id');
if (group_id) {
searching = group_id.value == '@search';
document.getElementById('activity_id_span').style.display =
searching ? 'none' : 'inline';
document.getElementById('search_query_span').style.display =
searching ? 'inline' : 'none';
function update_form() {
group = document.getElementById('group_id');
if (group) {
activity = document.getElementById('activity_id_span');
search = document.getElementById('search_query_span');
if (group.value == '@search') {
search.style.display = 'inline';
activity.style.display = 'none';
} else if (group.value == '@blocks') {
search.style.display = activity.style.display = 'none';
} else {
activity.style.display = 'inline';
search.style.display = 'none';
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions test_activitystreams.py
Expand Up @@ -40,6 +40,7 @@ def reset(self):
self.mox.ResetAll()
activitystreams.SOURCE = FakeSource
self.mox.StubOutWithMock(FakeSource, 'get_activities_response')
self.mox.StubOutWithMock(FakeSource, 'get_blocklist')

def get_response(self, url, *args, **kwargs):
start_index = kwargs.setdefault('start_index', 0)
Expand Down Expand Up @@ -94,6 +95,16 @@ def test_friends(self):
def test_self(self):
self.check_request('/123/@self/', '123', '@self')

def test_blocks(self):
blocks = [{'blockee': '1'}, {'blockee': '2'}]
FakeSource.get_blocklist().AndReturn(blocks)
self.mox.ReplayAll()

resp = activitystreams.application.get_response('/fake/123/@blocks/')
self.assertEquals(200, resp.status_int)
self.assert_equals({'items': blocks},
json.loads(resp.body))

def test_group_id(self):
self.check_request('/123/456', '123', '456')

Expand Down

0 comments on commit 7f2fbbe

Please sign in to comment.