def read_inbox(%{assigns: %{user: user}} = conn, %{"nickname" => nickname} = params) do
if nickname == user.nickname do
- Logger.info("read inbox #{inspect(params)}")
-
conn
|> put_resp_header("content-type", "application/activity+json")
- |> json("ok!")
+ |> json(UserView.render("inbox.json", %{user: user, max_id: params["max_id"]}))
else
conn
|> put_status(:forbidden)
end
end
+ def render("inbox.json", %{user: user, max_id: max_qid}) do
+ params = %{
+ "limit" => "10"
+ }
+
+ params =
+ if max_qid != nil do
+ Map.put(params, "max_id", max_qid)
+ else
+ params
+ end
+
+ activities = ActivityPub.fetch_activities([user.ap_id | user.following], params)
+
+ min_id = Enum.at(Enum.reverse(activities), 0).id
+ max_id = Enum.at(activities, 0).id
+
+ collection =
+ Enum.map(activities, fn act ->
+ {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
+ data
+ end)
+
+ iri = "#{user.ap_id}/inbox"
+
+ page = %{
+ "id" => "#{iri}?max_id=#{max_id}",
+ "type" => "OrderedCollectionPage",
+ "partOf" => iri,
+ "totalItems" => -1,
+ "orderedItems" => collection,
+ "next" => "#{iri}?max_id=#{min_id - 1}"
+ }
+
+ if max_qid == nil do
+ %{
+ "id" => iri,
+ "type" => "OrderedCollection",
+ "totalItems" => -1,
+ "first" => page
+ }
+ |> Map.merge(Utils.make_json_ld_header())
+ else
+ page |> Map.merge(Utils.make_json_ld_header())
+ end
+ end
+
def collection(collection, iri, page, show_items \\ true, total \\ nil) do
offset = (page - 1) * 10
items = Enum.slice(collection, offset, 10)
assert json_response(conn, 403)
end
+
+ test "it returns a note activity in a collection", %{conn: conn} do
+ note_activity = insert(:direct_note_activity)
+ user = User.get_cached_by_ap_id(hd(note_activity.data["to"]))
+
+ conn =
+ conn
+ |> assign(:user, user)
+ |> put_req_header("accept", "application/activity+json")
+ |> get("/users/#{user.nickname}/inbox")
+
+ assert response(conn, 200) =~ note_activity.data["object"]["content"]
+ end
end
describe "/users/:nickname/outbox" do