clacks/lib/clacks_web/controllers/objects_controller.ex

45 lines
1005 B
Elixir
Raw Normal View History

2019-09-29 01:57:11 +00:00
defmodule ClacksWeb.ObjectsController do
use ClacksWeb, :controller
2020-04-27 02:57:47 +00:00
alias Clacks.{Activity, Object}
alias ClacksWeb.Router.Helpers, as: Routes
alias ClacksWeb.Endpoint
2019-09-29 01:57:11 +00:00
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
2019-09-29 01:57:11 +00:00
object_id = current_url(conn)
case Object.get_cached_by_ap_id(object_id) do
2019-09-29 01:57:11 +00:00
nil ->
conn
|> resp(404, "Not Found")
2019-09-29 01:57:11 +00:00
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
2019-09-29 01:57:11 +00:00
end