Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add until_id param

- Add until_version param to GET /posts/:id/versions
  • Loading branch information...
commit 8750fb8c3e8152a68e68ddeaebbfd6122481ebee 1 parent 5aad22f
Jesse Stuart jvatic authored
2  lib/tentd/api/followers.rb
View
@@ -23,7 +23,7 @@ def action(env)
class GetActualId < Middleware
def action(env)
return env if env.skip_id_lookup
- [:follower_id, :before_id, :since_id].select { |k| env.params.has_key?(k) }.each do |id_key|
+ [:follower_id, :before_id, :since_id, :until_id].select { |k| env.params.has_key?(k) }.each do |id_key|
if env.params[id_key] && (f = Model::Follower.unfiltered.select(:id).first(:user_id => Model::User.current.id, :public_id => env.params[id_key]))
env.params[id_key] = f.id
else
2  lib/tentd/api/followings.rb
View
@@ -23,7 +23,7 @@ def action(env)
class GetActualId < Middleware
def action(env)
return env if env.skip_id_lookup
- id_mapping = [:following_id, :since_id, :before_id].select { |key| env.params.has_key?(key) }.inject({}) { |memo, key|
+ id_mapping = [:following_id, :since_id, :before_id, :until_id].select { |key| env.params.has_key?(key) }.inject({}) { |memo, key|
memo[env.params[key]] = key
env.params[key] = nil
memo
3  lib/tentd/api/groups.rb
View
@@ -19,7 +19,7 @@ def action(env)
class GetActualId < Middleware
def action(env)
- [:group_id, :before_id, :since_id].select { |k| env.params.has_key?(k) }.each do |id_key|
+ [:group_id, :before_id, :since_id, :until_id].select { |k| env.params.has_key?(k) }.each do |id_key|
if env.params[id_key]
if g = Model::Group.unfiltered.first(:user_id => Model::User.current.id, :public_id => env.params[id_key])
env.params[id_key] = g.id
@@ -49,6 +49,7 @@ def action(env)
query = Model::Group.where(:user_id => Model::User.current.id)
query = query.where { id < env.params.before_id } if env.params.before_id
query = query.where { id > env.params.since_id } if env.params.since_id
+ query = query.where { id > env.params.until_id } if env.params.until_id
limit = [(env.params.limit ? env.params.limit.to_i : PER_PAGE), MAX_PER_PAGE].min
query = query.limit(limit) if limit != 0
4 lib/tentd/api/posts.rb
View
@@ -5,7 +5,7 @@ class Posts
class GetActualId < Middleware
def action(env)
- id_mapping = [:post_id, :since_id, :before_id].select { |key| env.params.has_key?(key) }.inject({}) { |memo, key|
+ id_mapping = [:post_id, :since_id, :before_id, :until_id].select { |key| env.params.has_key?(key) }.inject({}) { |memo, key|
memo[env.params[key]] = key
env.params[key] = nil
memo
@@ -422,7 +422,7 @@ class GetMentions < Middleware
def action(env)
return env unless post = env.response
env.post = post
- env.response = post.public_mentions(env.params.slice(:before_id, :since_id, :limit, :post_types, :return_count))
+ env.response = post.public_mentions(env.params.slice(:before_id, :since_id, :until_id, :limit, :post_types, :return_count))
env
end
end
5 lib/tentd/model/permissible.rb
View
@@ -210,6 +210,11 @@ def build_fetch_params(params, query_conditions, query_bindings)
query_bindings << params.since_id.to_i
end
+ if params.until_id
+ query_conditions << "#{table_name}.id > ?"
+ query_bindings << params.until_id
+ end
+
if params.before_id
query_conditions << "#{table_name}.id < ?"
query_bindings << params.before_id.to_i
7 lib/tentd/model/permissible_post_version.rb
View
@@ -26,7 +26,7 @@ def fetch_with_permissions(params, current_auth)
def slice_params(params)
params = Hashie::Mash.new(params) unless params.kind_of?(Hashie::Mash)
- params.slice(:before_version, :since_version, :limit, :return_count, :order)
+ params.slice(:before_version, :since_version, :until_version, :limit, :return_count, :order)
end
private
@@ -37,6 +37,11 @@ def build_common_fetch_post_versions_query(params, query, query_conditions, quer
query_bindings << params.since_version
end
+ if params.until_version
+ query_conditions << "#{table_name}.version > ?"
+ query_bindings << params.until_version
+ end
+
if params.before_version
query_conditions << "#{table_name}.version < ?"
query_bindings << params.before_version
5 lib/tentd/model/post.rb
View
@@ -156,6 +156,11 @@ def public_mentions(params = {})
sql_bindings << params[:before_id]
end
+ if params.has_key?(:until_id)
+ sql << "AND posts.id < ?"
+ sql_bindings << params[:until_id]
+ end
+
if params.has_key?(:since_id)
sql << "AND posts.id > ?"
sql_bindings << params[:since_id]
30 spec/integration/api/followings_spec.rb
View
@@ -91,6 +91,36 @@ def stub_notification_http!
end
end
+ context '[:until_id]' do
+ it 'should return followings with id > :until_id' do
+ until_following = Fabricate(:following, :public => !create_permissions?)
+ other_following = Fabricate(:following, :public => !create_permissions?)
+ following = Fabricate(:following, :public => !create_permissions?)
+ before_following = Fabricate(:following, :public => !create_permissions?)
+
+ if create_permissions?
+ [until_following, other_following, following, before_following].each { |f| @create_permission.call(f) }
+ end
+
+ params[:until_id] = until_following.public_id
+ params[:before_id] = before_following.public_id
+
+ with_constants "TentD::API::MAX_PER_PAGE" => 3 do
+ json_get "/followings", params, env
+ expect(last_response.status).to eql(200)
+ body_ids = Yajl::Parser.parse(last_response.body).map { |i| i['id'] }
+ expect(body_ids).to eql([following.public_id, other_following.public_id])
+ end
+
+ with_constants "TentD::API::MAX_PER_PAGE" => 1 do
+ json_get "/followings", params, env
+ expect(last_response.status).to eql(200)
+ body_ids = Yajl::Parser.parse(last_response.body).map { |i| i['id'] }
+ expect(body_ids).to eql([following.public_id])
+ end
+ end
+ end
+
context '[:since_id]' do
it 'should only return followings with id > :since_id' do
since_following = Fabricate(:following, :public => !create_permissions?, :deleted_at => Time.now)
28 spec/integration/api/groups_spec.rb
View
@@ -94,6 +94,34 @@ def authorize!(*scopes)
end
end
+ it 'should filter by until_id' do
+ until_group = Fabricate(:group)
+ other_group = Fabricate(:group)
+ group = Fabricate(:group)
+ before_group = Fabricate(:group)
+
+ params[:until_id] = until_group.public_id
+ params[:before_id] = before_group.public_id
+
+ with_constants "TentD::API::MAX_PER_PAGE" => 3 do
+ json_get '/groups', params, env
+ expect(last_response.status).to eq(200)
+
+ body = JSON.parse(last_response.body)
+ body_ids = body.map { |i| i['id'] }
+ expect(body_ids).to eq([group.public_id, other_group.public_id])
+ end
+
+ with_constants "TentD::API::MAX_PER_PAGE" => 1 do
+ json_get '/groups', params, env
+ expect(last_response.status).to eq(200)
+
+ body = JSON.parse(last_response.body)
+ body_ids = body.map { |i| i['id'] }
+ expect(body_ids).to eq([group.public_id])
+ end
+ end
+
it 'should support limit' do
2.times { Fabricate(:group) }
params[:limit] = 1
82 spec/integration/api/posts_spec.rb
View
@@ -479,6 +479,27 @@ def authorize!(*scopes)
end
end
+ context '[:until_version]' do
+ it 'should return versions > :until_version' do
+ until_post_version = post.latest_version
+ other_post_version = post.create_version!
+ post_version = post.create_version!
+ before_post_version = post.create_version!
+
+ params = {
+ :before_version => before_post_version.version,
+ :until_version => until_post_version.version
+ }
+
+ with_constants "TentD::API::MAX_PER_PAGE" => 3 do
+ json_get "/posts/#{post.public_id}/versions", params, env
+ expect(last_response.status).to eql(200)
+ body_versions = Yajl::Parser.parse(last_response.body).map { |i| i['version'] }
+ expect(body_versions).to eql([post_version.version, other_post_version.version])
+ end
+ end
+ end
+
context '[:order] = asc' do
it 'should return versions in asc order' do
version_12 = Fabricate(:post_version, :post_id => post.id, :public_id => post.public_id, :version => 12)
@@ -632,6 +653,12 @@ def authorize!(*scopes)
describe 'GET/HEAD /posts/:post_id/mentions' do
let(:post) { Fabricate(:post, :public => true) }
+ create_post_and_mention = lambda { |post_attrs={}, mention_attrs={}|
+ post = Fabricate(:post, { :public => true, :original => false}.merge(post_attrs))
+ mention = Fabricate(:mention, { :post_id => post.id, :mentioned_post_id => post.public_id, :entity => post.entity }.merge(mention_attrs))
+ [post, mention]
+ }
+
let!(:known_post) { Fabricate(:post, :public => true, :original => false) }
let!(:known_mention) { Fabricate(:mention, :post_id => known_post.id, :mentioned_post_id => post.public_id, :entity => post.entity) }
@@ -691,6 +718,35 @@ def authorize!(*scopes)
end
end
+ context 'with [:until_id] param' do
+ it 'should return mentions with id < :until_id' do
+ other2_known_mention # create
+ until_post, until_mention = create_post_and_mention.call
+
+ params = {
+ :until_id => until_post.public_id,
+ :until_id_entity => until_post.entity,
+
+ :since_id => known_post.public_id,
+ :since_id_entity => known_post.entity
+ }
+
+ with_constants "TentD::API::MAX_PER_PAGE" => 3 do
+ json_get "/posts/#{post.public_id}/mentions", params, env
+ expect(last_response.status).to eq(200)
+ body_ids = Yajl::Parser.parse(last_response.body).map { |i| i['post'] }
+ expect(body_ids).to eql([other_known_post.public_id, other2_known_post.public_id])
+ end
+
+ with_constants "TentD::API::MAX_PER_PAGE" => 1 do
+ json_get "/posts/#{post.public_id}/mentions", params, env
+ expect(last_response.status).to eq(200)
+ body_ids = Yajl::Parser.parse(last_response.body).map { |i| i['post'] }
+ expect(body_ids).to eql([other_known_post.public_id])
+ end
+ end
+ end
+
context 'with [:since_id] param' do
it 'should return mentions with id > :since_id' do
other2_known_mention # create
@@ -932,6 +988,32 @@ def authorize!(*scopes)
end
end
+ it "should filter by params[:until_id]" do
+ until_post = Fabricate(:post, :public => post_public?)
+ post = Fabricate(:post, :public => post_public?)
+ other_post = Fabricate(:post, :public => post_public?)
+ before_post = Fabricate(:post, :public => post_public?)
+
+ params[:before_id] = before_post.public_id
+ params[:until_id] = until_post.public_id
+
+ with_constants "TentD::API::MAX_PER_PAGE" => 3 do
+ json_get "/posts", params, env
+ body = JSON.parse(last_response.body)
+ expect(body.size).to eq(2)
+ body_ids = body.map { |i| i['id'] }
+ expect(body_ids).to eq([other_post.public_id, post.public_id])
+ end
+
+ with_constants "TentD::API::MAX_PER_PAGE" => 1 do
+ json_get "/posts", params, env
+ body = JSON.parse(last_response.body)
+ expect(body.size).to eq(1)
+ body_ids = body.map { |i| i['id'] }
+ expect(body_ids).to eq([other_post.public_id])
+ end
+ end
+
it "should filter by both since_id and before_id" do
post1 = Fabricate(:post, :public => post_public?, :received_at => Time.at(Time.now.to_i - 4))
post2 = Fabricate(:post, :public => post_public?, :received_at => Time.at(Time.now.to_i - 3))
Please sign in to comment.
Something went wrong with that request. Please try again.