alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
alias Pleroma.Web.TwitterAPI.UserView
- alias Pleroma.Web.OStatus
- alias Pleroma.Formatter
-
- import Pleroma.Web.TwitterAPI.Utils
+ alias Pleroma.Web.{OStatus, CommonAPI}
+ import Ecto.Query
@httpoison Application.get_env(:pleroma, :httpoison)
- def to_for_user_and_mentions(user, mentions, inReplyTo) do
- default_to = [
- user.follower_address,
- "https://www.w3.org/ns/activitystreams#Public"
- ]
-
- to = default_to ++ Enum.map(mentions, fn ({_, %{ap_id: ap_id}}) -> ap_id end)
- if inReplyTo do
- Enum.uniq([inReplyTo.data["actor"] | to])
- else
- to
- end
- end
-
- def get_replied_to_activity(id) when not is_nil(id) do
- Repo.get(Activity, id)
- end
-
- def get_replied_to_activity(_), do: nil
-
- def create_status(%User{} = user, %{"status" => status} = data) do
- with attachments <- attachments_from_ids(data["media_ids"]),
- mentions <- Formatter.parse_mentions(status),
- inReplyTo <- get_replied_to_activity(data["in_reply_to_status_id"]),
- to <- to_for_user_and_mentions(user, mentions, inReplyTo),
- content_html <- make_content_html(status, mentions, attachments),
- context <- make_context(inReplyTo),
- tags <- Formatter.parse_tags(status),
- object <- make_note_data(user.ap_id, to, context, content_html, attachments, inReplyTo, tags) do
- res = ActivityPub.create(to, user, context, object)
- User.update_note_count(user)
- res
- end
+ def create_status(%User{} = user, %{"status" => _} = data) do
+ CommonAPI.post(user, data)
end
def fetch_friend_statuses(user, opts \\ %{}) do
+ opts = opts
+ |> Map.put("blocking_user", user)
+ |> Map.put("user", user)
+ |> Map.put("type", ["Create", "Announce", "Follow"])
+
ActivityPub.fetch_activities([user.ap_id | user.following], opts)
|> activities_to_statuses(%{for: user})
end
def fetch_public_statuses(user, opts \\ %{}) do
- opts = Map.put(opts, "local_only", true)
+ opts = opts
+ |> Map.put("local_only", true)
+ |> Map.put("blocking_user", user)
+ |> Map.put("type", ["Create", "Announce", "Follow"])
+
ActivityPub.fetch_public_activities(opts)
|> activities_to_statuses(%{for: user})
end
def fetch_public_and_external_statuses(user, opts \\ %{}) do
+ opts = opts
+ |> Map.put("blocking_user", user)
+ |> Map.put("type", ["Create", "Announce", "Follow"])
+
ActivityPub.fetch_public_activities(opts)
|> activities_to_statuses(%{for: user})
end
def fetch_user_statuses(user, opts \\ %{}) do
ActivityPub.fetch_activities([], opts)
+ |> Map.put("type", ["Create", "Announce", "Follow"])
|> activities_to_statuses(%{for: user})
end
def fetch_conversation(user, id) do
with context when is_binary(context) <- conversation_id_to_context(id),
- activities <- ActivityPub.fetch_activities_for_context(context),
+ activities <- ActivityPub.fetch_activities_for_context(context, %{"blocking_user" => user, "user" => user}),
statuses <- activities |> activities_to_statuses(%{for: user})
do
statuses
end
def fetch_status(user, id) do
- with %Activity{} = activity <- Repo.get(Activity, id) do
+ with %Activity{} = activity <- Repo.get(Activity, id),
+ true <- ActivityPub.visible_for_user?(activity, user) do
activity_to_status(activity, %{for: user})
end
end
end
end
- def favorite(%User{} = user, %Activity{data: %{"object" => object}} = activity) do
- object = Object.get_by_ap_id(object["id"])
-
- {:ok, _like_activity, object} = ActivityPub.like(user, object)
- new_data = activity.data
- |> Map.put("object", object.data)
-
- status = %{activity | data: new_data}
- |> activity_to_status(%{for: user})
-
- {:ok, status}
+ def block(%User{} = blocker, params) do
+ with {:ok, %User{} = blocked} <- get_user(params),
+ {:ok, blocker} <- User.block(blocker, blocked)
+ do
+ {:ok, blocker, blocked}
+ else
+ err -> err
+ end
end
- def unfavorite(%User{} = user, %Activity{data: %{"object" => object}} = activity) do
- object = Object.get_by_ap_id(object["id"])
-
- {:ok, object} = ActivityPub.unlike(user, object)
- new_data = activity.data
- |> Map.put("object", object.data)
-
- status = %{activity | data: new_data}
- |> activity_to_status(%{for: user})
-
- {:ok, status}
+ def unblock(%User{} = blocker, params) do
+ with {:ok, %User{} = blocked} <- get_user(params),
+ {:ok, blocker} <- User.unblock(blocker, blocked)
+ do
+ {:ok, blocker, blocked}
+ else
+ err -> err
+ end
end
- def retweet(%User{} = user, %Activity{data: %{"object" => object}} = activity) do
- object = Object.get_by_ap_id(object["id"])
-
- {:ok, _announce_activity, object} = ActivityPub.announce(user, object)
- new_data = activity.data
- |> Map.put("object", object.data)
+ def repeat(%User{} = user, ap_id_or_id) do
+ with {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(ap_id_or_id, user),
+ %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id),
+ status <- activity_to_status(activity, %{for: user}) do
+ {:ok, status}
+ end
+ end
- status = %{activity | data: new_data}
- |> activity_to_status(%{for: user})
+ def fav(%User{} = user, ap_id_or_id) do
+ with {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.favorite(ap_id_or_id, user),
+ %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id),
+ status <- activity_to_status(activity, %{for: user}) do
+ {:ok, status}
+ end
+ end
- {:ok, status}
+ def unfav(%User{} = user, ap_id_or_id) do
+ with {:ok, %{data: %{"id" => id}}} = CommonAPI.unfavorite(ap_id_or_id, user),
+ %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id),
+ status <- activity_to_status(activity, %{for: user}) do
+ {:ok, status}
+ end
end
def upload(%Plug.Upload{} = file, format \\ "xml") do
end
end
+ defp parse_int(string, default)
+ defp parse_int(string, default) when is_binary(string) do
+ with {n, _} <- Integer.parse(string) do
+ n
+ else
+ _e -> default
+ end
+ end
+ defp parse_int(_, default), do: default
+
+ def search(user, %{"q" => query} = params) do
+ limit = parse_int(params["rpp"], 20)
+ page = parse_int(params["page"], 1)
+ offset = (page - 1) * limit
+
+ q = from a in Activity,
+ where: fragment("?->>'type' = 'Create'", a.data),
+ where: fragment("to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)", a.data, ^query),
+ limit: ^limit,
+ offset: ^offset,
+ order_by: [desc: :inserted_at] # this one isn't indexed so psql won't take the wrong index.
+
+ activities = Repo.all(q)
+ activities_to_statuses(activities, %{for: user})
+ end
+
defp activities_to_statuses(activities, opts) do
Enum.map(activities, fn(activity) ->
activity_to_status(activity, opts)
ActivityRepresenter.to_map(activity, Map.merge(opts, %{users: [user, announced_actor], announced_activity: announced_activity}))
end
+ defp activity_to_status(%Activity{data: %{"type" => "Delete"}} = activity, opts) do
+ actor = get_in(activity.data, ["actor"])
+ user = User.get_cached_by_ap_id(actor)
+ ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user}))
+ end
+
defp activity_to_status(activity, opts) do
actor = get_in(activity.data, ["actor"])
user = User.get_cached_by_ap_id(actor)
def get_external_profile(for_user, uri) do
with {:ok, %User{} = user} <- OStatus.find_or_make_user(uri) do
- with url <- user.info["topic"],
- {:ok, %{body: body}} <- @httpoison.get(url, [], follow_redirect: true, timeout: 10000, recv_timeout: 20000) do
- OStatus.handle_incoming(body)
- end
+ spawn(fn ->
+ with url <- user.info["topic"],
+ {:ok, %{body: body}} <- @httpoison.get(url, [], follow_redirect: true, timeout: 10000, recv_timeout: 20000) do
+ OStatus.handle_incoming(body)
+ end
+ end)
{:ok, UserView.render("show.json", %{user: user, for: for_user})}
else _e ->
{:error, "Couldn't find user"}