Redirect object URLs in browser to status page
This commit is contained in:
parent
fbbd287fc1
commit
2972bad192
|
@ -25,6 +25,7 @@ defmodule Clacks.Activity do
|
|||
|> validate_required([:data, :local, :actor])
|
||||
end
|
||||
|
||||
@spec changeset_for_creating(activity :: map(), local :: boolean()) :: Ecto.Changeset.t()
|
||||
def changeset_for_creating(activity, local \\ false) do
|
||||
changeset(%__MODULE__{}, %{
|
||||
data: activity,
|
||||
|
@ -52,6 +53,14 @@ defmodule Clacks.Activity do
|
|||
Repo.one(from a in __MODULE__, where: fragment("?->>'id'", a.data) == ^ap_id)
|
||||
end
|
||||
|
||||
@spec get_by_object_ap_id(object_id :: String.t()) :: t() | nil
|
||||
def get_by_object_ap_id(object_id) do
|
||||
Repo.one(
|
||||
from a in __MODULE__,
|
||||
where: fragment("?->'object'->>'id'", a.data) == ^object_id
|
||||
)
|
||||
end
|
||||
|
||||
@spec fetch(ap_id :: String.t()) :: t() | nil
|
||||
def fetch(ap_id) do
|
||||
case Clacks.ActivityPub.Fetcher.fetch_activity(ap_id) do
|
||||
|
|
|
@ -19,6 +19,11 @@ defmodule Clacks.Object do
|
|||
|> validate_required([:data])
|
||||
end
|
||||
|
||||
@spec changeset_for_creating(data :: map()) :: Ecto.Changeset.t()
|
||||
def changeset_for_creating(data) do
|
||||
changeset(%__MODULE__{}, %{data: data})
|
||||
end
|
||||
|
||||
@spec get_by_ap_id(
|
||||
ap_id :: String.t(),
|
||||
force_refetch :: boolean(),
|
||||
|
|
|
@ -1,16 +1,27 @@
|
|||
defmodule ClacksWeb.ObjectsController do
|
||||
use ClacksWeb, :controller
|
||||
alias Clacks.{Repo, Object, ActivityPub}
|
||||
alias Clacks.{Repo, Activity, Object, ActivityPub}
|
||||
alias ClacksWeb.Router.Helpers, as: Routes
|
||||
alias ClacksWeb.Endpoint
|
||||
import Ecto.Query
|
||||
|
||||
def get(conn, %{"id" => id}) do
|
||||
object_id = current_url(conn)
|
||||
query = from(o in Object, where: fragment("?->>'id'", o.data) == ^object_id)
|
||||
def get(conn, params) do
|
||||
case conn.assigns[:format] do
|
||||
"activity+json" ->
|
||||
get_object(conn, params)
|
||||
|
||||
case Repo.one(query) do
|
||||
"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
|
||||
|> put_status(404)
|
||||
|> resp(404, "Not Found")
|
||||
|
||||
object ->
|
||||
conn
|
||||
|
@ -18,4 +29,17 @@ defmodule ClacksWeb.ObjectsController do
|
|||
|> 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
|
||||
|
|
|
@ -63,8 +63,6 @@ defmodule ClacksWeb.Router do
|
|||
scope "/", ClacksWeb do
|
||||
pipe_through :activitypub
|
||||
|
||||
get "/objects/:id", ObjectsController, :get
|
||||
|
||||
get "/users/:username/followers", ActorController, :followers
|
||||
get "/users/:username/following", ActorController, :following
|
||||
get "/users/:username/outbox", OutboxController, :outbox
|
||||
|
@ -81,6 +79,7 @@ defmodule ClacksWeb.Router do
|
|||
|
||||
get "/users/:username", ActorController, :get
|
||||
get "/activities/:id", ActivitiesController, :get
|
||||
get "/objects/:id", ObjectsController, :get
|
||||
end
|
||||
|
||||
# Other scopes may use custom stacks.
|
||||
|
|
Loading…
Reference in New Issue