clacks/lib/clacks_web/controllers/objects_controller.ex

46 lines
1.0 KiB
Elixir
Raw Normal View History

2019-09-29 01:57:11 +00:00
defmodule ClacksWeb.ObjectsController do
use ClacksWeb, :controller
alias Clacks.{Repo, Activity, Object, ActivityPub}
alias ClacksWeb.Router.Helpers, as: Routes
alias ClacksWeb.Endpoint
2019-09-29 01:57:11 +00:00
import Ecto.Query
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