|
|
@@ -4,23 +4,22 @@ |
|
|
|
|
|
|
|
defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |
|
|
|
use Pleroma.Web, :controller |
|
|
|
alias Pleroma.{Repo, Object, Activity, User, Notification, Stats} |
|
|
|
alias Pleroma.{Activity, Config, Filter, Notification, Object, Repo, Stats, User} |
|
|
|
alias Pleroma.Web |
|
|
|
alias Pleroma.Web.{CommonAPI, MediaProxy, Push} |
|
|
|
alias Push.Subscription |
|
|
|
|
|
|
|
alias Pleroma.Web.MastodonAPI.{ |
|
|
|
StatusView, |
|
|
|
AccountView, |
|
|
|
MastodonView, |
|
|
|
ListView, |
|
|
|
FilterView, |
|
|
|
PushSubscriptionView |
|
|
|
ListView, |
|
|
|
MastodonView, |
|
|
|
PushSubscriptionView, |
|
|
|
StatusView |
|
|
|
} |
|
|
|
|
|
|
|
alias Pleroma.Web.ActivityPub.ActivityPub |
|
|
|
alias Pleroma.Web.ActivityPub.Utils |
|
|
|
alias Pleroma.Web.CommonAPI |
|
|
|
alias Pleroma.Web.OAuth.{Authorization, Token, App} |
|
|
|
alias Pleroma.Web.MediaProxy |
|
|
|
alias Pleroma.Web.ActivityPub.{ActivityPub, Utils} |
|
|
|
alias Pleroma.Web.OAuth.{App, Authorization, Token} |
|
|
|
|
|
|
|
import Ecto.Query |
|
|
|
require Logger |
|
|
@@ -131,7 +130,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |
|
|
|
@mastodon_api_level "2.5.0" |
|
|
|
|
|
|
|
def masto_instance(conn, _params) do |
|
|
|
instance = Pleroma.Config.get(:instance) |
|
|
|
instance = Config.get(:instance) |
|
|
|
|
|
|
|
response = %{ |
|
|
|
uri: Web.base_url(), |
|
|
@@ -227,7 +226,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |
|
|
|
|> Map.put("user", user) |
|
|
|
|
|
|
|
activities = |
|
|
|
ActivityPub.fetch_activities([user.ap_id | user.following], params) |
|
|
|
[user.ap_id | user.following] |
|
|
|
|> ActivityPub.fetch_activities(params) |
|
|
|
|> ActivityPub.contain_timeline(user) |
|
|
|
|> Enum.reverse() |
|
|
|
|
|
|
@@ -240,14 +240,12 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |
|
|
|
def public_timeline(%{assigns: %{user: user}} = conn, params) do |
|
|
|
local_only = params["local"] in [true, "True", "true", "1"] |
|
|
|
|
|
|
|
params = |
|
|
|
activities = |
|
|
|
params |
|
|
|
|> Map.put("type", ["Create", "Announce"]) |
|
|
|
|> Map.put("local_only", local_only) |
|
|
|
|> Map.put("blocking_user", user) |
|
|
|
|
|
|
|
activities = |
|
|
|
ActivityPub.fetch_public_activities(params) |
|
|
|
|> ActivityPub.fetch_public_activities() |
|
|
|
|> Enum.reverse() |
|
|
|
|
|
|
|
conn |
|
|
@@ -316,6 +314,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |
|
|
|
as: :activity |
|
|
|
) |
|
|
|
|> Enum.reverse(), |
|
|
|
# credo:disable-for-previous-line Credo.Check.Refactor.PipeChainStart |
|
|
|
descendants: |
|
|
|
StatusView.render( |
|
|
|
"index.json", |
|
|
@@ -324,6 +323,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |
|
|
|
as: :activity |
|
|
|
) |
|
|
|
|> Enum.reverse() |
|
|
|
# credo:disable-for-previous-line Credo.Check.Refactor.PipeChainStart |
|
|
|
} |
|
|
|
|
|
|
|
json(conn, result) |
|
|
@@ -451,9 +451,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |
|
|
|
notifications = Notification.for_user(user, params) |
|
|
|
|
|
|
|
result = |
|
|
|
Enum.map(notifications, fn x -> |
|
|
|
render_notification(user, x) |
|
|
|
end) |
|
|
|
notifications |
|
|
|
|> Enum.map(fn x -> render_notification(user, x) end) |
|
|
|
|> Enum.filter(& &1) |
|
|
|
|
|
|
|
conn |
|
|
@@ -582,7 +581,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |
|
|
|
[] |
|
|
|
|> Enum.map(&String.downcase(&1)) |
|
|
|
|
|
|
|
query_params = |
|
|
|
activities = |
|
|
|
params |
|
|
|
|> Map.put("type", "Create") |
|
|
|
|> Map.put("local_only", local_only) |
|
|
@@ -590,9 +589,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |
|
|
|
|> Map.put("tag", tags) |
|
|
|
|> Map.put("tag_all", tag_all) |
|
|
|
|> Map.put("tag_reject", tag_reject) |
|
|
|
|
|
|
|
activities = |
|
|
|
ActivityPub.fetch_public_activities(query_params) |
|
|
|
|> ActivityPub.fetch_public_activities() |
|
|
|
|> Enum.reverse() |
|
|
|
|
|
|
|
conn |
|
|
@@ -692,7 +689,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |
|
|
|
{:ok, _activity} <- ActivityPub.follow(follower, followed), |
|
|
|
{:ok, follower, followed} <- |
|
|
|
User.wait_and_refresh( |
|
|
|
Pleroma.Config.get([:activitypub, :follow_handshake_timeout]), |
|
|
|
Config.get([:activitypub, :follow_handshake_timeout]), |
|
|
|
follower, |
|
|
|
followed |
|
|
|
) do |
|
|
@@ -821,7 +818,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |
|
|
|
tags_path = Web.base_url() <> "/tag/" |
|
|
|
|
|
|
|
tags = |
|
|
|
String.split(query) |
|
|
|
query |
|
|
|
|> String.split() |
|
|
|
|> Enum.uniq() |
|
|
|
|> Enum.filter(fn tag -> String.starts_with?(tag, "#") end) |
|
|
|
|> Enum.map(fn tag -> String.slice(tag, 1..-1) end) |
|
|
@@ -843,7 +841,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |
|
|
|
statuses = status_search(user, query) |
|
|
|
|
|
|
|
tags = |
|
|
|
String.split(query) |
|
|
|
query |
|
|
|
|> String.split() |
|
|
|
|> Enum.uniq() |
|
|
|
|> Enum.filter(fn tag -> String.starts_with?(tag, "#") end) |
|
|
|
|> Enum.map(fn tag -> String.slice(tag, 1..-1) end) |
|
|
@@ -867,14 +866,12 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |
|
|
|
end |
|
|
|
|
|
|
|
def favourites(%{assigns: %{user: user}} = conn, params) do |
|
|
|
params = |
|
|
|
activities = |
|
|
|
params |
|
|
|
|> Map.put("type", "Create") |
|
|
|
|> Map.put("favorited_by", user.ap_id) |
|
|
|
|> Map.put("blocking_user", user) |
|
|
|
|
|
|
|
activities = |
|
|
|
ActivityPub.fetch_public_activities(params) |
|
|
|
|> ActivityPub.fetch_public_activities() |
|
|
|
|> Enum.reverse() |
|
|
|
|
|
|
|
conn |
|
|
@@ -990,12 +987,10 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |
|
|
|
|
|
|
|
# we must filter the following list for the user to avoid leaking statuses the user |
|
|
|
# does not actually have permission to see (for more info, peruse security issue #270). |
|
|
|
following_to = |
|
|
|
activities = |
|
|
|
following |
|
|
|
|> Enum.filter(fn x -> x in user.following end) |
|
|
|
|
|
|
|
activities = |
|
|
|
ActivityPub.fetch_activities_bounded(following_to, following, params) |
|
|
|
|> ActivityPub.fetch_activities_bounded(following, params) |
|
|
|
|> Enum.reverse() |
|
|
|
|
|
|
|
conn |
|
|
@@ -1017,7 +1012,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |
|
|
|
if user && token do |
|
|
|
mastodon_emoji = mastodonized_emoji() |
|
|
|
|
|
|
|
limit = Pleroma.Config.get([:instance, :limit]) |
|
|
|
limit = Config.get([:instance, :limit]) |
|
|
|
|
|
|
|
accounts = |
|
|
|
Map.put(%{}, user.id, AccountView.render("account.json", %{user: user, for: user})) |
|
|
@@ -1041,8 +1036,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |
|
|
|
max_toot_chars: limit |
|
|
|
}, |
|
|
|
rights: %{ |
|
|
|
delete_others_notice: !!user.info.is_moderator, |
|
|
|
admin: !!user.info.is_admin |
|
|
|
delete_others_notice: present?(user.info.is_moderator), |
|
|
|
admin: present?(user.info.is_admin) |
|
|
|
}, |
|
|
|
compose: %{ |
|
|
|
me: "#{user.id}", |
|
|
@@ -1234,7 +1229,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |
|
|
|
end |
|
|
|
|
|
|
|
def get_filters(%{assigns: %{user: user}} = conn, _) do |
|
|
|
filters = Pleroma.Filter.get_filters(user) |
|
|
|
filters = Filter.get_filters(user) |
|
|
|
res = FilterView.render("filters.json", filters: filters) |
|
|
|
json(conn, res) |
|
|
|
end |
|
|
@@ -1243,7 +1238,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |
|
|
|
%{assigns: %{user: user}} = conn, |
|
|
|
%{"phrase" => phrase, "context" => context} = params |
|
|
|
) do |
|
|
|
query = %Pleroma.Filter{ |
|
|
|
query = %Filter{ |
|
|
|
user_id: user.id, |
|
|
|
phrase: phrase, |
|
|
|
context: context, |
|
|
@@ -1252,13 +1247,13 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |
|
|
|
# expires_at |
|
|
|
} |
|
|
|
|
|
|
|
{:ok, response} = Pleroma.Filter.create(query) |
|
|
|
{:ok, response} = Filter.create(query) |
|
|
|
res = FilterView.render("filter.json", filter: response) |
|
|
|
json(conn, res) |
|
|
|
end |
|
|
|
|
|
|
|
def get_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do |
|
|
|
filter = Pleroma.Filter.get(filter_id, user) |
|
|
|
filter = Filter.get(filter_id, user) |
|
|
|
res = FilterView.render("filter.json", filter: filter) |
|
|
|
json(conn, res) |
|
|
|
end |
|
|
@@ -1267,7 +1262,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |
|
|
|
%{assigns: %{user: user}} = conn, |
|
|
|
%{"phrase" => phrase, "context" => context, "id" => filter_id} = params |
|
|
|
) do |
|
|
|
query = %Pleroma.Filter{ |
|
|
|
query = %Filter{ |
|
|
|
user_id: user.id, |
|
|
|
filter_id: filter_id, |
|
|
|
phrase: phrase, |
|
|
@@ -1277,32 +1272,32 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |
|
|
|
# expires_at |
|
|
|
} |
|
|
|
|
|
|
|
{:ok, response} = Pleroma.Filter.update(query) |
|
|
|
{:ok, response} = Filter.update(query) |
|
|
|
res = FilterView.render("filter.json", filter: response) |
|
|
|
json(conn, res) |
|
|
|
end |
|
|
|
|
|
|
|
def delete_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do |
|
|
|
query = %Pleroma.Filter{ |
|
|
|
query = %Filter{ |
|
|
|
user_id: user.id, |
|
|
|
filter_id: filter_id |
|
|
|
} |
|
|
|
|
|
|
|
{:ok, _} = Pleroma.Filter.delete(query) |
|
|
|
{:ok, _} = Filter.delete(query) |
|
|
|
json(conn, %{}) |
|
|
|
end |
|
|
|
|
|
|
|
def create_push_subscription(%{assigns: %{user: user, token: token}} = conn, params) do |
|
|
|
true = Pleroma.Web.Push.enabled() |
|
|
|
Pleroma.Web.Push.Subscription.delete_if_exists(user, token) |
|
|
|
{:ok, subscription} = Pleroma.Web.Push.Subscription.create(user, token, params) |
|
|
|
true = Push.enabled() |
|
|
|
Subscription.delete_if_exists(user, token) |
|
|
|
{:ok, subscription} = Subscription.create(user, token, params) |
|
|
|
view = PushSubscriptionView.render("push_subscription.json", subscription: subscription) |
|
|
|
json(conn, view) |
|
|
|
end |
|
|
|
|
|
|
|
def get_push_subscription(%{assigns: %{user: user, token: token}} = conn, _params) do |
|
|
|
true = Pleroma.Web.Push.enabled() |
|
|
|
subscription = Pleroma.Web.Push.Subscription.get(user, token) |
|
|
|
true = Push.enabled() |
|
|
|
subscription = Subscription.get(user, token) |
|
|
|
view = PushSubscriptionView.render("push_subscription.json", subscription: subscription) |
|
|
|
json(conn, view) |
|
|
|
end |
|
|
@@ -1311,15 +1306,15 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |
|
|
|
%{assigns: %{user: user, token: token}} = conn, |
|
|
|
params |
|
|
|
) do |
|
|
|
true = Pleroma.Web.Push.enabled() |
|
|
|
{:ok, subscription} = Pleroma.Web.Push.Subscription.update(user, token, params) |
|
|
|
true = Push.enabled() |
|
|
|
{:ok, subscription} = Subscription.update(user, token, params) |
|
|
|
view = PushSubscriptionView.render("push_subscription.json", subscription: subscription) |
|
|
|
json(conn, view) |
|
|
|
end |
|
|
|
|
|
|
|
def delete_push_subscription(%{assigns: %{user: user, token: token}} = conn, _params) do |
|
|
|
true = Pleroma.Web.Push.enabled() |
|
|
|
{:ok, _response} = Pleroma.Web.Push.Subscription.delete(user, token) |
|
|
|
true = Push.enabled() |
|
|
|
{:ok, _response} = Subscription.delete(user, token) |
|
|
|
json(conn, %{}) |
|
|
|
end |
|
|
|
|
|
|
@@ -1330,17 +1325,21 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |
|
|
|
end |
|
|
|
|
|
|
|
def suggestions(%{assigns: %{user: user}} = conn, _) do |
|
|
|
suggestions = Pleroma.Config.get(:suggestions) |
|
|
|
suggestions = Config.get(:suggestions) |
|
|
|
|
|
|
|
if Keyword.get(suggestions, :enabled, false) do |
|
|
|
api = Keyword.get(suggestions, :third_party_engine, "") |
|
|
|
timeout = Keyword.get(suggestions, :timeout, 5000) |
|
|
|
limit = Keyword.get(suggestions, :limit, 23) |
|
|
|
|
|
|
|
host = Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host]) |
|
|
|
host = Config.get([Pleroma.Web.Endpoint, :url, :host]) |
|
|
|
|
|
|
|
user = user.nickname |
|
|
|
url = String.replace(api, "{{host}}", host) |> String.replace("{{user}}", user) |
|
|
|
|
|
|
|
url = |
|
|
|
api |
|
|
|
|> String.replace("{{host}}", host) |
|
|
|
|> String.replace("{{user}}", user) |
|
|
|
|
|
|
|
with {:ok, %{status: 200, body: body}} <- |
|
|
|
@httpoison.get( |
|
|
@@ -1353,8 +1352,9 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |
|
|
|
] |
|
|
|
), |
|
|
|
{:ok, data} <- Jason.decode(body) do |
|
|
|
data2 = |
|
|
|
Enum.slice(data, 0, limit) |
|
|
|
data = |
|
|
|
data |
|
|
|
|> Enum.slice(0, limit) |
|
|
|
|> Enum.map(fn x -> |
|
|
|
Map.put( |
|
|
|
x, |
|
|
@@ -1373,7 +1373,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |
|
|
|
end) |
|
|
|
|
|
|
|
conn |
|
|
|
|> json(data2) |
|
|
|
|> json(data) |
|
|
|
else |
|
|
|
e -> Logger.error("Could not retrieve suggestions at fetch #{url}, #{inspect(e)}") |
|
|
|
end |
|
|
@@ -1416,4 +1416,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |
|
|
|
|> put_status(501) |
|
|
|
|> json(%{error: "Can't display this activity"}) |
|
|
|
end |
|
|
|
|
|
|
|
defp present?(nil), do: false |
|
|
|
defp present?(false), do: false |
|
|
|
defp present?(_), do: true |
|
|
|
end |