+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
defmodule Pleroma.Web.TwitterAPI.Controller do
use Pleroma.Web, :controller
import Pleroma.Web.ControllerHelper, only: [json_response: 3]
- alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView, ActivityView, NotificationView}
- alias Pleroma.Web.CommonAPI
- alias Pleroma.{Repo, Activity, Object, User, Notification}
- alias Pleroma.Web.ActivityPub.ActivityPub
- alias Pleroma.Web.ActivityPub.Utils
alias Ecto.Changeset
+ alias Pleroma.Activity
+ alias Pleroma.Notification
+ alias Pleroma.Object
+ alias Pleroma.Repo
+ alias Pleroma.User
+ alias Pleroma.Web.ActivityPub.ActivityPub
+ alias Pleroma.Web.ActivityPub.Visibility
+ alias Pleroma.Web.CommonAPI
+ alias Pleroma.Web.CommonAPI.Utils
+ alias Pleroma.Web.OAuth.Token
+ alias Pleroma.Web.TwitterAPI.ActivityView
+ alias Pleroma.Web.TwitterAPI.NotificationView
+ alias Pleroma.Web.TwitterAPI.TokenView
+ alias Pleroma.Web.TwitterAPI.TwitterAPI
+ alias Pleroma.Web.TwitterAPI.UserView
require Logger
plug(:only_if_public_instance when action in [:public_timeline, :public_and_external_timeline])
- plug(:fetch_flash when action in [:confirm_email, :resend_confirmation_email])
action_fallback(:errors)
def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
conn
|> put_view(UserView)
- |> render("show.json", %{user: user, token: token})
+ |> render("show.json", %{user: user, token: token, for: user})
end
def status_update(%{assigns: %{user: user}} = conn, %{"status" => _} = status_data) do
for_user = conn.assigns.user
with {:ok, shown} <- TwitterAPI.get_user(params),
- true <- User.auth_active?(shown) || for_user && (for_user.id == shown.id || User.superuser?(for_user)) do
+ true <-
+ User.auth_active?(shown) ||
+ (for_user && (for_user.id == shown.id || User.superuser?(for_user))) do
params =
if for_user do
%{user: shown, for: for_user}
def user_timeline(%{assigns: %{user: user}} = conn, params) do
case TwitterAPI.get_user(user, params) do
{:ok, target_user} ->
+ # Twitter and ActivityPub use a different name and sense for this parameter.
+ {include_rts, params} = Map.pop(params, "include_rts")
+
+ params =
+ case include_rts do
+ x when x == "false" or x == "0" -> Map.put(params, "exclude_reblogs", "true")
+ _ -> params
+ end
+
activities = ActivityPub.fetch_user_activities(target_user, user, params)
conn
params
|> Map.put("type", ["Create", "Announce", "Follow", "Like"])
|> Map.put("blocking_user", user)
+ |> Map.put(:visibility, ~w[unlisted public private])
activities = ActivityPub.fetch_activities([user.ap_id], params)
end
def dm_timeline(%{assigns: %{user: user}} = conn, params) do
- query =
- ActivityPub.fetch_activities_query(
- [user.ap_id],
- Map.merge(params, %{"type" => "Create", "user" => user, visibility: "direct"})
- )
+ params =
+ params
+ |> Map.put("type", "Create")
+ |> Map.put("blocking_user", user)
+ |> Map.put("user", user)
+ |> Map.put(:visibility, "direct")
- activities = Repo.all(query)
+ activities =
+ ActivityPub.fetch_activities_query([user.ap_id], params)
+ |> Repo.all()
conn
|> put_view(ActivityView)
end
def fetch_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
- with %Activity{} = activity <- Repo.get(Activity, id),
- true <- ActivityPub.visible_for_user?(activity, user) do
+ with %Activity{} = activity <- Activity.get_by_id(id),
+ true <- Visibility.visible_for_user?(activity, user) do
conn
|> put_view(ActivityView)
|> render("activity.json", %{activity: activity, for: user})
end
def fetch_conversation(%{assigns: %{user: user}} = conn, %{"id" => id}) do
- id = String.to_integer(id)
-
- with context when is_binary(context) <- TwitterAPI.conversation_id_to_context(id),
+ with context when is_binary(context) <- Utils.conversation_id_to_context(id),
activities <-
ActivityPub.fetch_activities_for_context(context, %{
"blocking_user" => user,
end
def get_by_id_or_ap_id(id) do
- activity = Repo.get(Activity, id) || Activity.get_create_activity_by_object_ap_id(id)
+ activity = Activity.get_by_id(id) || Activity.get_create_by_object_ap_id(id)
if activity.data["type"] == "Create" do
activity
else
- Activity.get_create_activity_by_object_ap_id(activity.data["object"])
+ Activity.get_create_by_object_ap_id(activity.data["object"])
end
end
def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
- with {_, {:ok, id}} <- {:param_cast, Ecto.Type.cast(:integer, id)},
- {:ok, activity} <- TwitterAPI.fav(user, id) do
+ with {:ok, activity} <- TwitterAPI.fav(user, id) do
conn
|> put_view(ActivityView)
|> render("activity.json", %{activity: activity, for: user})
+ else
+ _ -> json_reply(conn, 400, Jason.encode!(%{}))
end
end
def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
- with {_, {:ok, id}} <- {:param_cast, Ecto.Type.cast(:integer, id)},
- {:ok, activity} <- TwitterAPI.unfav(user, id) do
+ with {:ok, activity} <- TwitterAPI.unfav(user, id) do
conn
|> put_view(ActivityView)
|> render("activity.json", %{activity: activity, for: user})
+ else
+ _ -> json_reply(conn, 400, Jason.encode!(%{}))
end
end
def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
- with {_, {:ok, id}} <- {:param_cast, Ecto.Type.cast(:integer, id)},
- {:ok, activity} <- TwitterAPI.repeat(user, id) do
+ with {:ok, activity} <- TwitterAPI.repeat(user, id) do
conn
|> put_view(ActivityView)
|> render("activity.json", %{activity: activity, for: user})
+ else
+ _ -> json_reply(conn, 400, Jason.encode!(%{}))
end
end
def unretweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
- with {_, {:ok, id}} <- {:param_cast, Ecto.Type.cast(:integer, id)},
- {:ok, activity} <- TwitterAPI.unrepeat(user, id) do
+ with {:ok, activity} <- TwitterAPI.unrepeat(user, id) do
+ conn
+ |> put_view(ActivityView)
+ |> render("activity.json", %{activity: activity, for: user})
+ else
+ _ -> json_reply(conn, 400, Jason.encode!(%{}))
+ end
+ end
+
+ def pin(%{assigns: %{user: user}} = conn, %{"id" => id}) do
+ with {:ok, activity} <- TwitterAPI.pin(user, id) do
+ conn
+ |> put_view(ActivityView)
+ |> render("activity.json", %{activity: activity, for: user})
+ else
+ {:error, message} -> bad_request_reply(conn, message)
+ err -> err
+ end
+ end
+
+ def unpin(%{assigns: %{user: user}} = conn, %{"id" => id}) do
+ with {:ok, activity} <- TwitterAPI.unpin(user, id) do
conn
|> put_view(ActivityView)
|> render("activity.json", %{activity: activity, for: user})
+ else
+ {:error, message} -> bad_request_reply(conn, message)
+ err -> err
end
end
end
end
- def confirm_email(conn, %{"token" => token}) do
- with %User{} = user <- User.get_by_confirmation_token(token),
+ def confirm_email(conn, %{"user_id" => uid, "token" => token}) do
+ with %User{} = user <- User.get_cached_by_id(uid),
true <- user.local,
+ true <- user.info.confirmation_pending,
+ true <- user.info.confirmation_token == token,
info_change <- User.Info.confirmation_changeset(user.info, :confirmed),
changeset <- Changeset.change(user) |> Changeset.put_embed(:info, info_change),
{:ok, _} <- User.update_and_set_cache(changeset) do
conn
- |> put_flash(:info, "Email confirmed. Please sign in.")
|> redirect(to: "/")
end
end
with %User{} = user <- User.get_by_nickname_or_email(nickname_or_email),
{:ok, _} <- User.try_send_confirmation_email(user) do
conn
- |> put_flash(:info, "Email confirmation has been sent.")
|> json_response(:no_content, "")
end
end
end
def followers(%{assigns: %{user: for_user}} = conn, params) do
+ {:ok, page} = Ecto.Type.cast(:integer, params["page"] || 1)
+
with {:ok, user} <- TwitterAPI.get_user(for_user, params),
- {:ok, followers} <- User.get_followers(user) do
+ {:ok, followers} <- User.get_followers(user, page) do
followers =
cond do
for_user && user.id == for_user.id -> followers
- user.info.hide_network -> []
+ user.info.hide_followers -> []
true -> followers
end
end
def friends(%{assigns: %{user: for_user}} = conn, params) do
+ {:ok, page} = Ecto.Type.cast(:integer, params["page"] || 1)
+ {:ok, export} = Ecto.Type.cast(:boolean, params["all"] || false)
+
+ page = if export, do: nil, else: page
+
with {:ok, user} <- TwitterAPI.get_user(conn.assigns[:user], params),
- {:ok, friends} <- User.get_friends(user) do
+ {:ok, friends} <- User.get_friends(user, page) do
friends =
cond do
for_user && user.id == for_user.id -> friends
- user.info.hide_network -> []
+ user.info.hide_follows -> []
true -> friends
end
end
end
+ def oauth_tokens(%{assigns: %{user: user}} = conn, _params) do
+ with oauth_tokens <- Token.get_user_tokens(user) do
+ conn
+ |> put_view(TokenView)
+ |> render("index.json", %{tokens: oauth_tokens})
+ end
+ end
+
+ def revoke_token(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
+ Token.delete_user_token(user, id)
+
+ json_reply(conn, 201, "")
+ end
+
+ def blocks(%{assigns: %{user: user}} = conn, _params) do
+ with blocked_users <- User.blocked_users(user) do
+ conn
+ |> put_view(UserView)
+ |> render("index.json", %{users: blocked_users, for: user})
+ end
+ end
+
def friend_requests(conn, params) do
with {:ok, user} <- TwitterAPI.get_user(conn.assigns[:user], params),
{:ok, friend_requests} <- User.get_follow_requests(user) do
def approve_friend_request(conn, %{"user_id" => uid} = _params) do
with followed <- conn.assigns[:user],
- uid when is_number(uid) <- String.to_integer(uid),
- %User{} = follower <- Repo.get(User, uid),
- {:ok, follower} <- User.maybe_follow(follower, followed),
- %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
- {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "accept"),
- {:ok, _activity} <-
- ActivityPub.accept(%{
- to: [follower.ap_id],
- actor: followed.ap_id,
- object: follow_activity.data["id"],
- type: "Accept"
- }) do
+ %User{} = follower <- User.get_cached_by_id(uid),
+ {:ok, follower} <- CommonAPI.accept_follow_request(follower, followed) do
conn
|> put_view(UserView)
|> render("show.json", %{user: follower, for: followed})
def deny_friend_request(conn, %{"user_id" => uid} = _params) do
with followed <- conn.assigns[:user],
- uid when is_number(uid) <- String.to_integer(uid),
- %User{} = follower <- Repo.get(User, uid),
- %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
- {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "reject"),
- {:ok, _activity} <-
- ActivityPub.reject(%{
- to: [follower.ap_id],
- actor: followed.ap_id,
- object: follow_activity.data["id"],
- type: "Reject"
- }) do
+ %User{} = follower <- User.get_cached_by_id(uid),
+ {:ok, follower} <- CommonAPI.reject_follow_request(follower, followed) do
conn
|> put_view(UserView)
|> render("show.json", %{user: follower, for: followed})
defp build_info_cng(user, params) do
info_params =
- ["no_rich_text", "locked", "hide_network"]
+ ["no_rich_text", "locked", "hide_followers", "hide_follows", "hide_favorites", "show_role"]
|> Enum.reduce(%{}, fn key, res ->
if value = params[key] do
Map.put(res, key, value == "true")
end
def search_user(%{assigns: %{user: user}} = conn, %{"query" => query}) do
- users = User.search(query, true)
+ users = User.search(query, resolve: true, for_user: user)
conn
|> put_view(UserView)
json_reply(conn, 403, json)
end
- def only_if_public_instance(conn = %{conn: %{assigns: %{user: _user}}}, _), do: conn
+ def only_if_public_instance(%{assigns: %{user: %User{}}} = conn, _), do: conn
def only_if_public_instance(conn, _) do
if Keyword.get(Application.get_env(:pleroma, :instance), :public) do