Class: TentD::API::Posts::GetFeed

Inherits:
Middleware show all
Defined in:
lib/tentd/api/posts.rb

Instance Method Summary collapse

Methods inherited from Middleware

#call, #initialize

Methods included from Authorizable

#authorize_env!, #authorize_env?

Constructor Details

This class inherits a constructor from TentD::API::Middleware

Instance Method Details

#action(env) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/tentd/api/posts.rb', line 61

def action(env)
  if authorize_env?(env, :read_posts)
    conditions = {}
    non_public_conditions = {}
    conditions[:id.gt] = env.params.since_id if env.params.since_id
    conditions[:id.lt] = env.params.before_id if env.params.before_id
    conditions[:published_at.gt] = Time.at(env.params.since_time.to_i) if env.params.since_time
    conditions[:published_at.lt] = Time.at(env.params.before_time.to_i) if env.params.before_time
    conditions[:entity] = env.params.entity if env.params.entity
    if env.params.mentioned_post && env.params.mentioned_entity
      conditions[:mentions] = {
        :mentioned_post_id => env.params.mentioned_post,
        :entity => env.params.mentioned_entity
      }
    end
    if env.params.post_types
      types = env.params.post_types.split(',').map { |t| TentType.new(URI.unescape(t)) }

      non_public_conditions[:type_base] = types.select do |type|
        env.current_auth.post_types.include?('all') ||
        env.current_auth.post_types.include?(type.uri)
      end.map(&:base)
      non_public_conditions[:type_base] = nil unless non_public_conditions[:type_base].any?

      conditions[:type_base] = types.map(&:base)

    elsif !env.current_auth.post_types.include?('all')
      non_public_conditions[:type_base] = env.current_auth.post_types.map { |t| TentType.new(t).base }
    end
    if env.params.limit
      conditions[:limit] = [env.params.limit.to_i, TentD::API::MAX_PER_PAGE].min
    else
      conditions[:limit] = TentD::API::PER_PAGE
    end

    if env.params.return_count
      conditions[:original] = true
      env.response = Model::Post.all(conditions.merge(non_public_conditions))
      env.response += Model::Post.all(conditions.merge(:public => true))
      env.response = env.response.count
    else
      conditions[:order] = :published_at.desc
      if conditions[:limit] == 0
        env.response = []
      else
        env.response = Model::Post.all(conditions.merge(non_public_conditions))
        env.response += Model::Post.all(conditions.merge(:public => true))
      end
    end
  else
    env.response = Model::Post.fetch_with_permissions(env.params, env.current_auth)
  end
  env
end