clacks/lib/clacks_web/controllers/frontend_controller.ex

327 lines
9.2 KiB
Elixir
Raw Normal View History

2019-10-06 23:41:18 +00:00
defmodule ClacksWeb.FrontendController do
use ClacksWeb, :controller
alias Clacks.{Actor, User, Timeline, Repo, ActivityPub, Activity, Object, UserActionsHelper}
2019-10-06 23:41:18 +00:00
alias ClacksWeb.Router.Helpers, as: Routes
alias ClacksWeb.Endpoint
2020-04-26 22:55:44 +00:00
import Ecto.Query
2019-10-06 23:41:18 +00:00
2020-04-23 02:54:46 +00:00
@public "https://www.w3.org/ns/activitystreams#Public"
def index(%Plug.Conn{assigns: %{user: user}} = conn, params) do
2019-10-06 23:41:18 +00:00
user = Repo.preload(user, :actor)
render(conn, "home.html", %{
user: user,
actor: user.actor,
statuses_with_authors: Timeline.home_timeline(user, params)
2019-10-06 23:41:18 +00:00
})
end
def index(conn, params) do
Application.get_env(:clacks, :frontend, %{})
|> Keyword.get(:unauthenticated_homepage, :local_timeline)
2019-10-06 23:41:18 +00:00
|> index(conn, params)
end
defp index(:local_timeline, conn, params) do
render(conn, "local_timeline.html", %{
statuses_with_authors: Timeline.local_timeline(params)
})
2019-10-06 23:41:18 +00:00
end
defp index({:profile, nickname}, conn, params) do
case Actor.get_by_nickname(nickname) do
2020-05-23 15:53:27 +00:00
# only local profiles are shown
2019-10-06 23:41:18 +00:00
%Actor{local: true} = actor ->
2020-05-23 15:53:27 +00:00
statuses_with_actor =
actor_statuses(actor, params, only_public: true)
|> Enum.map(fn {activity, announce} -> {activity, actor, announce} end)
2019-10-06 23:41:18 +00:00
render(conn, "profile.html", %{
actor: actor,
2020-05-23 15:53:27 +00:00
statuses_with_actor: statuses_with_actor
2019-10-06 23:41:18 +00:00
})
_ ->
# otherwise show local timeline
2020-04-27 02:57:47 +00:00
index(:local_timeline, conn, params)
2019-10-06 23:41:18 +00:00
end
end
defp actor_statuses(actor, params, only_public: only_public) do
Timeline.actor_timeline(actor, params, only_public)
2019-10-06 23:41:18 +00:00
end
def status(conn, %{"id" => id}) do
current_user = conn.assigns[:user] |> Repo.preload(:actor)
with %Activity{
local: true,
2020-04-23 02:54:46 +00:00
data:
%{
"type" => "Create",
"object" => %{"type" => "Note", "attributedTo" => author_id}
2020-04-23 02:54:46 +00:00
} = data
2020-04-25 16:30:47 +00:00
} = activity <- Activity.get(id),
2020-04-23 02:54:46 +00:00
%Actor{} = author <- Actor.get_by_ap_id(author_id) do
case conn.assigns[:format] do
"activity+json" ->
json(conn, data)
"html" ->
render(conn, "status.html", %{
current_user: current_user,
2020-04-25 16:30:47 +00:00
status: activity,
2020-04-23 02:54:46 +00:00
author: author
})
end
else
nil ->
case conn.assigns[:format] do
"activity+json" ->
conn
|> put_status(404)
|> json(%{error: "Not Found"})
"html" ->
resp(conn, 404, "Not Found")
end
%Activity{local: false, data: %{"id" => ap_id}} ->
case conn.assigns[:format] do
"activity+json" ->
conn
|> put_status(404)
|> json(%{error: "Not Found"})
"html" ->
redirect(conn, external: ap_id)
end
end
end
def reply(conn, %{"id" => id}) do
current_user = conn.assigns[:user]
with %Activity{
2019-10-06 23:41:18 +00:00
data: %{
"type" => "Create",
2020-04-25 16:30:47 +00:00
"object" => %{"type" => "Note", "attributedTo" => author_id}
2019-10-06 23:41:18 +00:00
}
2020-04-25 16:30:47 +00:00
} = activity <- Activity.get(id),
2019-10-06 23:41:18 +00:00
%Actor{} = author <- Actor.get_by_ap_id(author_id) do
2020-04-25 16:30:47 +00:00
render(conn, "status.html", %{
2019-10-06 23:41:18 +00:00
current_user: current_user,
2020-04-25 16:30:47 +00:00
status: activity,
2019-10-06 23:41:18 +00:00
author: author
})
else
2020-04-23 02:54:46 +00:00
_ ->
resp(conn, 404, "Not Found")
2019-10-06 23:41:18 +00:00
end
end
def profile(conn, %{"username" => username} = params) do
current_user = conn.assigns[:user] |> Repo.preload(:actor)
case User.get_by_username(username) do
nil ->
2020-04-26 18:45:51 +00:00
resp(conn, 404, "Not Found")
2019-10-06 23:41:18 +00:00
user ->
user = Repo.preload(user, :actor)
2020-05-23 15:53:27 +00:00
statuses_with_actor =
actor_statuses(user.actor, params, only_public: true)
|> Enum.map(fn {activity, announce} -> {activity, user.actor, announce} end)
2019-10-06 23:41:18 +00:00
render(conn, "profile.html", %{
current_user: current_user,
actor: user.actor,
2020-05-23 15:53:27 +00:00
statuses_with_actor: statuses_with_actor,
2020-04-26 22:55:44 +00:00
following_state: following_state(current_user.actor, user.actor)
2019-10-06 23:41:18 +00:00
})
end
end
2020-04-26 18:45:51 +00:00
def actor(conn, %{"id" => id} = params) do
current_user = conn.assigns[:user] |> Repo.preload(:actor)
case Repo.get(Actor, id) do
nil ->
resp(conn, 404, "Not Found")
actor ->
2020-05-23 15:53:27 +00:00
statuses_with_actor =
actor_statuses(actor, params, only_public: true)
|> Enum.map(fn {activity, announce} -> {activity, actor, announce} end)
2020-04-26 18:45:51 +00:00
render(conn, "profile.html", %{
current_user: current_user,
actor: actor,
2020-05-23 15:53:27 +00:00
statuses_with_actor: statuses_with_actor,
2020-04-26 22:55:44 +00:00
following_state: following_state(current_user.actor, actor)
2020-04-26 18:45:51 +00:00
})
end
end
2020-04-26 22:55:44 +00:00
def follow(conn, %{"id" => id}) do
current_user = conn.assigns[:user] |> Repo.preload(:actor)
case Repo.get(Actor, id) do
nil ->
resp(conn, 404, "Not Found")
followee ->
if current_user.actor.ap_id in followee.followers do
redirect(conn, to: ClacksWeb.FrontendView.local_actor_link(followee))
else
follow = ActivityPub.follow(current_user.actor.ap_id, followee.ap_id)
ActivityPub.Helper.save_and_federate(follow, current_user.actor)
conn
|> put_flash(:info, "Follow request sent")
|> redirect(to: ClacksWeb.FrontendView.local_actor_link(followee))
end
end
end
2020-04-26 23:28:22 +00:00
def unfollow(conn, %{"id" => id}) do
current_user = conn.assigns[:user] |> Repo.preload(:actor)
case Repo.get(Actor, id) do
nil ->
resp(conn, 404, "Not Found")
followee ->
unless current_user.actor.ap_id in followee.followers do
redirect(conn, to: ClacksWeb.FrontendView.local_actor_link(followee))
else
new_followers = List.delete(followee.followers, current_user.actor.ap_id)
changeset = Actor.changeset(followee, %{followers: new_followers})
{:ok, followee} = Repo.update(changeset)
follow_activity = follow_activity(current_user.actor, followee)
changeset =
Activity.changeset(follow_activity, %{
data: %{follow_activity.data | "state" => "unfollowed"}
})
{:ok, follow_activity} = Repo.update(changeset)
undo_follow = ActivityPub.undo_follow(current_user.actor.ap_id, follow_activity.data)
ActivityPub.Helper.save_and_federate(undo_follow, current_user.actor)
conn
|> put_flash(:info, "Unfollowed")
|> redirect(to: ClacksWeb.FrontendView.local_actor_link(followee))
end
end
end
2020-04-26 02:28:30 +00:00
def search(conn, %{"q" => q}) when is_binary(q) do
current_user = conn.assigns[:user]
2020-04-26 20:15:20 +00:00
actor_results =
case Actor.fetch(q) do
%Actor{} = actor ->
[actor]
_ ->
[]
end
2020-04-26 02:28:30 +00:00
status_results =
with %Activity{
actor: actor_id,
data: %{"type" => "Create", "object" => %{"type" => "Note"}}
2020-04-26 20:15:20 +00:00
} = activity <- Object.fetch(q, true, :activity),
2020-04-26 02:28:30 +00:00
actor <- Actor.get_by_ap_id(actor_id) do
[{activity, actor}]
else
_ ->
[]
end
render(conn, "search.html", %{
current_user: current_user,
q: q,
2020-04-26 20:15:20 +00:00
status_results: status_results,
actor_results: actor_results
2020-04-26 02:28:30 +00:00
})
end
def search(conn, _params) do
current_user = conn.assigns[:user]
render(conn, "search.html", %{
current_user: current_user,
2020-04-26 20:15:20 +00:00
q: "",
status_results: [],
actor_results: []
2020-04-26 02:28:30 +00:00
})
end
2020-05-24 20:26:10 +00:00
def notifications(conn, params) do
current_user = conn.assigns[:user] |> Repo.preload(:actor)
notifications = Timeline.notifications(current_user.actor, params)
render(conn, "notifications.html", %{
current_user: current_user,
notifications: notifications
})
end
def post_status(conn, %{"content" => content} = params) do
2019-10-06 23:41:18 +00:00
current_user = conn.assigns[:user] |> Repo.preload(:actor)
2020-05-23 02:56:02 +00:00
UserActionsHelper.post_status(
current_user,
content,
"text/plain",
Map.get(params, "in_reply_to")
)
|> case do
{:ok, activity} ->
path = Map.get(params, "continue", Routes.frontend_path(Endpoint, :status, activity.id))
redirect(conn, to: path)
2019-10-06 23:41:18 +00:00
{:error, reason} ->
conn
|> put_flash(:error, "Unable to post status: #{inspect(reason)}")
|> redirect(to: Map.get(params, "continue", Routes.frontend_path(Endpoint, :index)))
2020-04-23 02:54:46 +00:00
end
end
2020-04-26 23:28:22 +00:00
@spec follow_activity(follower :: Actor.t(), followee :: Actor.t()) :: map()
def follow_activity(follower, followee) do
# todo: get latest
2020-04-26 22:55:44 +00:00
query =
Activity
|> where([a], fragment("?->>'type'", a.data) == "Follow")
|> where([a], fragment("?->>'actor'", a.data) == ^follower.ap_id)
|> where([a], fragment("?->>'object'", a.data) == ^followee.ap_id)
2020-04-26 23:28:22 +00:00
|> order_by(desc: :inserted_at)
|> limit(1)
2020-04-26 22:55:44 +00:00
2020-04-26 23:28:22 +00:00
Repo.one(query)
end
2020-04-26 22:55:44 +00:00
2020-04-27 02:57:47 +00:00
@spec following_state(follower :: Actor.t(), followee :: Actor.t()) ::
:following | :not_following | :pending
2020-04-26 23:28:22 +00:00
defp following_state(follower, followee) do
case follow_activity(follower, followee) do
2020-04-26 22:55:44 +00:00
%Activity{data: %{"state" => "pending"}} ->
:pending
%Activity{data: %{"state" => "accepted"}} ->
:following
2020-04-26 23:28:22 +00:00
_ ->
:not_following
2020-04-26 22:55:44 +00:00
end
end
2019-10-06 23:41:18 +00:00
end