clacks/lib/clacks_web/controllers/objects_controller.ex

45 lines
1005 B
Elixir

defmodule ClacksWeb.ObjectsController do
use ClacksWeb, :controller
alias Clacks.{Activity, Object}
alias ClacksWeb.Router.Helpers, as: Routes
alias ClacksWeb.Endpoint
def get(conn, params) do
case conn.assigns[:format] do
"activity+json" ->
get_object(conn, params)
"html" ->
redirect_to_status(conn, params)
end
end
defp get_object(conn, _params) do
object_id = current_url(conn)
case Object.get_cached_by_ap_id(object_id) do
nil ->
conn
|> resp(404, "Not Found")
object ->
conn
|> put_resp_header("content-type", "application/activity+json")
|> json(object.data)
end
end
defp redirect_to_status(conn, _params) do
object_id = current_url(conn)
case Activity.get_by_object_ap_id(object_id) do
nil ->
conn
|> resp(404, "Not Found")
activity ->
redirect(conn, to: Routes.frontend_path(Endpoint, :status, activity.id))
end
end
end