- def fetch_activities(recipients, opts \\ %{}) do
- since_id = opts["since_id"] || 0
-
- query = from activity in Activity,
- limit: 20,
- order_by: [desc: :inserted_at]
-
- query = Enum.reduce(recipients, query, fn (recipient, q) ->
- map = %{ to: [recipient] }
- from activity in q,
- or_where: fragment(~s(? @> ?), activity.data, ^map)
- end)
-
- query = from activity in query,
- where: activity.id > ^since_id
-
- query = if opts["local_only"] do
- from activity in query, where: activity.local == true
- else
- query
- end
-
- query = if opts["max_id"] do
- from activity in query, where: activity.id < ^opts["max_id"]
- else
- query
- end
-
- query = if opts["actor_id"] do
- from activity in query,
- where: fragment("? @> ?", activity.data, ^%{actor: opts["actor_id"]})
- else
- query
- end
+ defp restrict_since(query, %{"since_id" => since_id}) do
+ from activity in query, where: activity.id > ^since_id
+ end
+ defp restrict_since(query, _), do: query