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
+ require Logger
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 = if is_map(map["object"]) do
- object = Map.put_new_lazy(map["object"], "id", &generate_object_id/0)
- Repo.insert!(%Object{data: object})
- Map.put(map, "object", object)
- else
- map
+ with %Activity{} = activity <- Activity.get_by_ap_id(map["id"]) do
+ Logger.debug(fn -> "Already have activity, #{activity.id}, not inserting." end)
+ {:ok, activity}
+ else _e ->
+ map = if is_map(map["object"]) do
+ object = Map.put_new_lazy(map["object"], "id", &generate_object_id/0)
+ Repo.insert!(%Object{data: object})
+ Map.put(map, "object", object)
+ else
+ map
+ end
+
+ Repo.insert(%Activity{data: map, local: local})
end
-
- Repo.insert(%Activity{data: map, local: local})
end
def create(to, actor, context, object, additional \\ %{}, published \\ nil, local \\ true) do
end
end
- def like(%User{ap_id: ap_id} = user, %Object{data: %{ "id" => id}} = object) do
+ def like(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object, activity_id \\ nil, local \\ true) do
cond do
# There's already a like here, so return the original activity.
ap_id in (object.data["likes"] || []) ->
"context" => object.data["context"]
}
- {:ok, activity} = insert(data)
+ data = if activity_id, do: Map.put(data, "id", activity_id), else: 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)
end
defp update_object_in_activities(%{data: %{"id" => id}} = object) do
+ # TODO
# Update activities that already had this. Could be done in a seperate process.
+ # Alternatively, just don't do this and fetch the current object each time. Most
+ # could probably be taken from cache.
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
|> 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_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
end
- Repo.all(query)
- |> Enum.reverse
+ Enum.reverse(Repo.all(query))
end
- def announce(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object) do
+ def announce(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object, activity_id \\ nil, local \\ true) do
data = %{
"type" => "Announce",
"actor" => ap_id,
"context" => object.data["context"]
}
- {:ok, activity} = insert(data)
+ data = if activity_id, do: Map.put(data, "id", activity_id), else: data
+
+ {:ok, activity} = insert(data, local)
announcements = [ap_id | (object.data["announcements"] || [])] |> Enum.uniq
|> Map.put("announcement_count", length(announcements))
|> Map.put("announcements", announcements)
- 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
+ def follow(%User{ap_id: follower_id, local: actor_local}, %User{ap_id: followed_id}, activity_id \\ nil, local \\ true) do
+ data = %{
+ "type" => "Follow",
+ "actor" => follower_id,
+ "to" => [followed_id],
+ "object" => followed_id,
+ "published" => make_date()
+ }
+
+ data = if activity_id, do: Map.put(data, "id", activity_id), else: data
+
+ with {:ok, activity} <- insert(data, local) do
+ if actor_local do
+ Pleroma.Web.Federator.enqueue(:publish, activity)
+ end
+
+ {:ok, activity}
+ end
+ end
+
+ def unfollow(follower, followed, local \\ true) do
+ with follow_activity when not is_nil(follow_activity) <- fetch_latest_follow(follower, followed) do
+ data = %{
+ "type" => "Undo",
+ "actor" => follower.ap_id,
+ "to" => [followed.ap_id],
+ "object" => follow_activity.data["id"],
+ "published" => make_date()
+ }
+
+ with {:ok, activity} <- insert(data, local) do
+ if follower.local do
+ Pleroma.Web.Federator.enqueue(:publish, activity)
+ end
+
+ {:ok, activity}
+ end
+ end
+ end
+
def fetch_activities_for_context(context) do
query = from activity in Activity,
where: fragment("? @> ?", activity.data, ^%{ context: context })
Repo.all(query)
end
+ def fetch_latest_follow(%User{ap_id: follower_id},
+ %User{ap_id: followed_id}) do
+ query = from activity in Activity,
+ where: fragment("? @> ?", activity.data, ^%{type: "Follow", actor: follower_id,
+ object: followed_id}),
+ order_by: [desc: :inserted_at],
+ limit: 1
+ Repo.one(query)
+ end
+
def upload(file) do
data = Upload.store(file)
Repo.insert(%Object{data: data})