Redirect object URLs in browser to status page

This commit is contained in:
Shadowfacts 2020-04-20 22:42:39 -04:00
parent fbbd287fc1
commit 2972bad192
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
4 changed files with 45 additions and 8 deletions

View File

@ -25,6 +25,7 @@ defmodule Clacks.Activity do
|> validate_required([:data, :local, :actor]) |> validate_required([:data, :local, :actor])
end end
@spec changeset_for_creating(activity :: map(), local :: boolean()) :: Ecto.Changeset.t()
def changeset_for_creating(activity, local \\ false) do def changeset_for_creating(activity, local \\ false) do
changeset(%__MODULE__{}, %{ changeset(%__MODULE__{}, %{
data: activity, data: activity,
@ -52,6 +53,14 @@ defmodule Clacks.Activity do
Repo.one(from a in __MODULE__, where: fragment("?->>'id'", a.data) == ^ap_id) Repo.one(from a in __MODULE__, where: fragment("?->>'id'", a.data) == ^ap_id)
end 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 @spec fetch(ap_id :: String.t()) :: t() | nil
def fetch(ap_id) do def fetch(ap_id) do
case Clacks.ActivityPub.Fetcher.fetch_activity(ap_id) do case Clacks.ActivityPub.Fetcher.fetch_activity(ap_id) do

View File

@ -19,6 +19,11 @@ defmodule Clacks.Object do
|> validate_required([:data]) |> validate_required([:data])
end 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( @spec get_by_ap_id(
ap_id :: String.t(), ap_id :: String.t(),
force_refetch :: boolean(), force_refetch :: boolean(),

View File

@ -1,16 +1,27 @@
defmodule ClacksWeb.ObjectsController do defmodule ClacksWeb.ObjectsController do
use ClacksWeb, :controller 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 import Ecto.Query
def get(conn, %{"id" => id}) do def get(conn, params) do
object_id = current_url(conn) case conn.assigns[:format] do
query = from(o in Object, where: fragment("?->>'id'", o.data) == ^object_id) "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 -> nil ->
conn conn
|> put_status(404) |> resp(404, "Not Found")
object -> object ->
conn conn
@ -18,4 +29,17 @@ defmodule ClacksWeb.ObjectsController do
|> json(object.data) |> json(object.data)
end end
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 end

View File

@ -63,8 +63,6 @@ defmodule ClacksWeb.Router do
scope "/", ClacksWeb do scope "/", ClacksWeb do
pipe_through :activitypub pipe_through :activitypub
get "/objects/:id", ObjectsController, :get
get "/users/:username/followers", ActorController, :followers get "/users/:username/followers", ActorController, :followers
get "/users/:username/following", ActorController, :following get "/users/:username/following", ActorController, :following
get "/users/:username/outbox", OutboxController, :outbox get "/users/:username/outbox", OutboxController, :outbox
@ -81,6 +79,7 @@ defmodule ClacksWeb.Router do
get "/users/:username", ActorController, :get get "/users/:username", ActorController, :get
get "/activities/:id", ActivitiesController, :get get "/activities/:id", ActivitiesController, :get
get "/objects/:id", ObjectsController, :get
end end
# Other scopes may use custom stacks. # Other scopes may use custom stacks.