Browse Source

Redirect object URLs in browser to status page

master
Shadowfacts 1 year ago
parent
commit
2972bad192
Signed by: shadowfacts GPG Key ID: 94A5AB95422746E5
4 changed files with 44 additions and 7 deletions
  1. +9
    -0
      lib/clacks/activity.ex
  2. +5
    -0
      lib/clacks/object.ex
  3. +29
    -5
      lib/clacks_web/controllers/objects_controller.ex
  4. +1
    -2
      lib/clacks_web/router.ex

+ 9
- 0
lib/clacks/activity.ex View File

@ -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


+ 5
- 0
lib/clacks/object.ex View File

@ -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(),


+ 29
- 5
lib/clacks_web/controllers/objects_controller.ex View File

@ -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
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)
query = from(o in Object, where: fragment("?->>'id'", o.data) == ^object_id)
case Repo.one(query) do
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

+ 1
- 2
lib/clacks_web/router.ex View File

@ -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…
Cancel
Save