2017-03-21 02:21:52 -06:00
|
|
|
defmodule Pleroma.Web.ActivityPub.ActivityPub do
|
|
|
|
alias Pleroma.Repo
|
2017-04-13 07:50:05 -06:00
|
|
|
alias Pleroma.{Activity, Object, Upload, User}
|
2017-03-21 10:53:20 -06:00
|
|
|
import Ecto.Query
|
2017-03-21 02:21:52 -06:00
|
|
|
|
|
|
|
def insert(map) when is_map(map) do
|
2017-04-13 07:50:05 -06:00
|
|
|
map = map
|
|
|
|
|> Map.put_new_lazy("id", &generate_activity_id/0)
|
|
|
|
|> Map.put_new_lazy("published", &make_date/0)
|
2017-03-23 10:56:49 -06:00
|
|
|
|
2017-04-03 18:30:07 -06:00
|
|
|
map = if is_map(map["object"]) do
|
2017-03-23 14:22:49 -06:00
|
|
|
object = Map.put_new_lazy(map["object"], "id", &generate_object_id/0)
|
2017-03-30 10:07:01 -06:00
|
|
|
Repo.insert!(%Object{data: object})
|
2017-03-23 14:22:49 -06:00
|
|
|
Map.put(map, "object", object)
|
|
|
|
else
|
|
|
|
map
|
|
|
|
end
|
|
|
|
|
2017-03-21 02:21:52 -06:00
|
|
|
Repo.insert(%Activity{data: map})
|
|
|
|
end
|
2017-03-21 10:53:20 -06:00
|
|
|
|
2017-04-14 07:07:24 -06:00
|
|
|
def like(%User{ap_id: ap_id} = user, %Object{data: %{ "id" => id}} = object) do
|
|
|
|
cond do
|
|
|
|
# There's already a like here, so return the original activity.
|
|
|
|
ap_id in (object.data["likes"] || []) ->
|
|
|
|
query = from activity in Activity,
|
2017-04-14 10:08:47 -06:00
|
|
|
where: fragment("? @> ?", activity.data, ^%{actor: ap_id, object: id, type: "Like"})
|
2017-04-13 07:50:05 -06:00
|
|
|
|
2017-04-14 07:07:24 -06:00
|
|
|
activity = Repo.one(query)
|
|
|
|
{:ok, activity, object}
|
|
|
|
true ->
|
|
|
|
data = %{
|
|
|
|
"type" => "Like",
|
|
|
|
"actor" => ap_id,
|
|
|
|
"object" => id,
|
|
|
|
"to" => [User.ap_followers(user)]
|
|
|
|
}
|
2017-04-13 07:50:05 -06:00
|
|
|
|
2017-04-14 07:07:24 -06:00
|
|
|
{:ok, activity} = insert(data)
|
2017-04-13 07:50:05 -06:00
|
|
|
|
2017-04-14 07:07:24 -06:00
|
|
|
likes = [ap_id | (object.data["likes"] || [])] |> Enum.uniq
|
|
|
|
|
|
|
|
new_data = object.data
|
|
|
|
|> Map.put("like_count", length(likes))
|
|
|
|
|> Map.put("likes", likes)
|
|
|
|
|
|
|
|
changeset = Ecto.Changeset.change(object, data: new_data)
|
|
|
|
{:ok, object} = Repo.update(changeset)
|
|
|
|
|
2017-04-14 10:08:47 -06:00
|
|
|
update_object_in_activities(object)
|
|
|
|
|
2017-04-14 07:07:24 -06:00
|
|
|
{:ok, activity, object}
|
|
|
|
end
|
2017-04-13 07:50:05 -06:00
|
|
|
end
|
|
|
|
|
2017-04-14 10:08:47 -06:00
|
|
|
defp update_object_in_activities(%{data: %{"id" => id}} = object) do
|
|
|
|
# Update activities that already had this. Could be done in a seperate process.
|
|
|
|
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)
|
|
|
|
Repo.update(changeset)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
def unlike(%User{ap_id: ap_id} = user, %Object{data: %{ "id" => id}} = object) do
|
|
|
|
query = from activity in Activity,
|
|
|
|
where: fragment("? @> ?", activity.data, ^%{actor: ap_id, object: id, type: "Like"})
|
|
|
|
|
|
|
|
activity = Repo.one(query)
|
|
|
|
|
|
|
|
if activity do
|
|
|
|
# just delete for now...
|
|
|
|
{:ok, _activity} = Repo.delete(activity)
|
|
|
|
|
|
|
|
likes = (object.data["likes"] || []) |> List.delete(ap_id)
|
|
|
|
|
|
|
|
new_data = object.data
|
|
|
|
|> Map.put("like_count", length(likes))
|
|
|
|
|> Map.put("likes", likes)
|
|
|
|
|
|
|
|
changeset = Ecto.Changeset.change(object, data: new_data)
|
|
|
|
{:ok, object} = Repo.update(changeset)
|
|
|
|
|
|
|
|
update_object_in_activities(object)
|
|
|
|
|
|
|
|
{:ok, object}
|
|
|
|
else
|
|
|
|
{:ok, object}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-23 10:56:49 -06:00
|
|
|
def generate_activity_id do
|
2017-03-23 16:34:10 -06:00
|
|
|
generate_id("activities")
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_context_id do
|
|
|
|
generate_id("contexts")
|
2017-03-23 10:56:49 -06:00
|
|
|
end
|
|
|
|
|
2017-03-23 14:22:49 -06:00
|
|
|
def generate_object_id do
|
2017-03-23 16:34:10 -06:00
|
|
|
generate_id("objects")
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_id(type) do
|
2017-03-30 06:32:36 -06:00
|
|
|
"#{Pleroma.Web.base_url()}/#{type}/#{Ecto.UUID.generate}"
|
2017-03-23 14:22:49 -06:00
|
|
|
end
|
|
|
|
|
2017-03-21 13:31:48 -06:00
|
|
|
def fetch_public_activities(opts \\ %{}) do
|
2017-03-22 07:45:17 -06:00
|
|
|
public = ["https://www.w3.org/ns/activitystreams#Public"]
|
|
|
|
fetch_activities(public, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_activities(recipients, opts \\ %{}) do
|
2017-03-21 14:09:20 -06:00
|
|
|
since_id = opts["since_id"] || 0
|
2017-03-21 13:31:48 -06:00
|
|
|
|
2017-03-21 10:53:20 -06:00
|
|
|
query = from activity in Activity,
|
2017-03-21 13:22:05 -06:00
|
|
|
limit: 20,
|
|
|
|
order_by: [desc: :inserted_at]
|
2017-03-21 10:53:20 -06:00
|
|
|
|
2017-03-22 07:45:17 -06:00
|
|
|
query = Enum.reduce(recipients, query, fn (recipient, q) ->
|
|
|
|
map = %{ to: [recipient] }
|
|
|
|
from activity in q,
|
|
|
|
or_where: fragment(~s(? @> ?), activity.data, ^map)
|
|
|
|
end)
|
|
|
|
|
|
|
|
query = from activity in query,
|
|
|
|
where: activity.id > ^since_id
|
|
|
|
|
2017-04-12 09:11:01 -06:00
|
|
|
query = if opts["max_id"] do
|
|
|
|
from activity in query, where: activity.id < ^opts["max_id"]
|
|
|
|
else
|
|
|
|
query
|
|
|
|
end
|
|
|
|
|
2017-03-21 10:53:20 -06:00
|
|
|
Repo.all(query)
|
2017-03-21 13:22:05 -06:00
|
|
|
|> Enum.reverse
|
2017-03-21 10:53:20 -06:00
|
|
|
end
|
2017-03-23 17:09:08 -06:00
|
|
|
|
|
|
|
def fetch_activities_for_context(context) do
|
|
|
|
query = from activity in Activity,
|
|
|
|
where: fragment("? @> ?", activity.data, ^%{ context: context })
|
|
|
|
Repo.all(query)
|
|
|
|
end
|
2017-03-28 18:05:51 -06:00
|
|
|
|
|
|
|
def upload(%Plug.Upload{} = file) do
|
|
|
|
data = Upload.store(file)
|
|
|
|
Repo.insert(%Object{data: data})
|
|
|
|
end
|
2017-04-13 07:50:05 -06:00
|
|
|
|
|
|
|
defp make_date do
|
|
|
|
DateTime.utc_now() |> DateTime.to_iso8601
|
|
|
|
end
|
2017-03-21 02:21:52 -06:00
|
|
|
end
|