defmodule Pleroma.Web.ActivityPub.ActivityPub do
- alias Pleroma.Repo
- alias Pleroma.{Activity, Object, Upload, User}
+ alias Pleroma.{Activity, Repo, Object, Upload, User, Web}
+ alias Ecto.{Changeset, UUID}
import Ecto.Query
- def insert(map) when is_map(map) do
+ def insert(map, local \\ true) when is_map(map) do
map = map
|> Map.put_new_lazy("id", &generate_activity_id/0)
|> Map.put_new_lazy("published", &make_date/0)
map
end
- Repo.insert(%Activity{data: map})
+ Repo.insert(%Activity{data: map, local: local})
end
- def like(%User{ap_id: ap_id} = user, %Object{data: %{ "id" => id}} = object) do
+ def create(to, actor, context, object, additional \\ %{}, published \\ nil, local \\ true) do
+ published = published || make_date()
+
+ activity = %{
+ "type" => "Create",
+ "to" => to |> Enum.uniq,
+ "actor" => actor.ap_id,
+ "object" => object,
+ "published" => published,
+ "context" => context
+ }
+ |> Map.merge(additional)
+
+ with {:ok, activity} <- insert(activity, local) do
+ if actor.local do
+ Pleroma.Web.Federator.enqueue(:publish, activity)
+ end
+
+ {:ok, activity}
+ end
+ end
+
+ def like(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object, local \\ true) do
cond do
# There's already a like here, so return the original activity.
ap_id in (object.data["likes"] || []) ->
"type" => "Like",
"actor" => ap_id,
"object" => id,
- "to" => [User.ap_followers(user)]
+ "to" => [User.ap_followers(user), object.data["actor"]],
+ "context" => object.data["context"]
}
- {:ok, activity} = insert(data)
+ {:ok, activity} = insert(data, local)
likes = [ap_id | (object.data["likes"] || [])] |> Enum.uniq
|> Map.put("like_count", length(likes))
|> Map.put("likes", likes)
- changeset = Ecto.Changeset.change(object, data: new_data)
+ changeset = Changeset.change(object, data: new_data)
{:ok, object} = Repo.update(changeset)
update_object_in_activities(object)
+ if user.local do
+ Pleroma.Web.Federator.enqueue(:publish, activity)
+ end
+
{:ok, activity, object}
end
end
relevant_activities = Activity.all_by_object_ap_id(id)
Enum.map(relevant_activities, fn (activity) ->
new_activity_data = activity.data |> Map.put("object", object.data)
- changeset = Ecto.Changeset.change(activity, data: new_activity_data)
+ changeset = Changeset.change(activity, data: new_activity_data)
Repo.update(changeset)
end)
end
- def unlike(%User{ap_id: ap_id} = user, %Object{data: %{ "id" => id}} = object) do
+ def unlike(%User{ap_id: ap_id}, %Object{data: %{ "id" => id}} = object) do
query = from activity in Activity,
where: fragment("? @> ?", activity.data, ^%{actor: ap_id, object: id, type: "Like"})
|> Map.put("like_count", length(likes))
|> Map.put("likes", likes)
- changeset = Ecto.Changeset.change(object, data: new_data)
+ changeset = Changeset.change(object, data: new_data)
{:ok, object} = Repo.update(changeset)
update_object_in_activities(object)
end
def generate_object_id do
- generate_id("objects")
+ Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :object, Ecto.UUID.generate)
end
def generate_id(type) do
- "#{Pleroma.Web.base_url()}/#{type}/#{Ecto.UUID.generate}"
+ "#{Web.base_url()}/#{type}/#{UUID.generate}"
end
def fetch_public_activities(opts \\ %{}) do
query = from activity in query,
where: activity.id > ^since_id
+ query = if opts["local_only"] do
+ from activity in query, where: activity.local == true
+ else
+ query
+ end
+
query = if opts["max_id"] do
from activity in query, where: activity.id < ^opts["max_id"]
else
query
end
- Repo.all(query)
- |> Enum.reverse
+ query = if opts["actor_id"] do
+ from activity in query,
+ where: fragment("? @> ?", activity.data, ^%{actor: opts["actor_id"]})
+ else
+ query
+ end
+
+ Enum.reverse(Repo.all(query))
+ end
+
+ def announce(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object, local \\ true) do
+ data = %{
+ "type" => "Announce",
+ "actor" => ap_id,
+ "object" => id,
+ "to" => [User.ap_followers(user), object.data["actor"]],
+ "context" => object.data["context"]
+ }
+
+ {:ok, activity} = insert(data, local)
+
+ announcements = [ap_id | (object.data["announcements"] || [])] |> Enum.uniq
+
+ new_data = object.data
+ |> Map.put("announcement_count", length(announcements))
+ |> Map.put("announcements", announcements)
+
+ changeset = Changeset.change(object, data: new_data)
+ {:ok, object} = Repo.update(changeset)
+
+ update_object_in_activities(object)
+
+ if user.local do
+ Pleroma.Web.Federator.enqueue(:publish, activity)
+ end
+
+ {:ok, activity, object}
end
def fetch_activities_for_context(context) do
Repo.all(query)
end
- def upload(%Plug.Upload{} = file) do
+ def upload(file) do
data = Upload.store(file)
Repo.insert(%Object{data: data})
end