activity_pub.ex 5.5 KB
Newer Older
lain's avatar
lain committed
1
defmodule Pleroma.Web.ActivityPub.ActivityPub do
2
  alias Pleroma.{Activity, Repo, Object, Upload, User, Web, Notification}
3
  alias Ecto.{Changeset, UUID}
lain's avatar
lain committed
4
  import Ecto.Query
lain's avatar
lain committed
5
  import Pleroma.Web.ActivityPub.Utils
lain's avatar
lain committed
6
  require Logger
lain's avatar
lain committed
7

lain's avatar
lain committed
8
  def insert(map, local \\ true) when is_map(map) do
lain's avatar
lain committed
9 10 11
    with nil <- Activity.get_by_ap_id(map["id"]),
         map <- lazy_put_activity_defaults(map),
         :ok <- insert_full_object(map) do
12 13 14
      {:ok, activity} = Repo.insert(%Activity{data: map, local: local})
      Notification.create_notifications(activity)
      {:ok, activity}
lain's avatar
lain committed
15 16 17
    else
      %Activity{} = activity -> {:ok, activity}
      error -> {:error, error}
lain's avatar
lain committed
18
    end
lain's avatar
lain committed
19
  end
lain's avatar
lain committed
20

lain's avatar
lain committed
21
  def create(to, actor, context, object, additional \\ %{}, published \\ nil, local \\ true) do
lain's avatar
lain committed
22 23 24
    with create_data <- make_create_data(%{to: to, actor: actor, published: published, context: context, object: object}, additional),
         {:ok, activity} <- insert(create_data, local),
         :ok <- maybe_federate(activity) do
25 26
      {:ok, activity}
    end
lain's avatar
lain committed
27
  end
lain's avatar
lain committed
28

lain's avatar
lain committed
29
  # TODO: This is weird, maybe we shouldn't check here if we can make the activity.
lain's avatar
lain committed
30
  def like(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object, activity_id \\ nil, local \\ true) do
lain's avatar
lain committed
31 32 33 34 35 36 37 38 39
    with nil <- get_existing_like(ap_id, object),
         like_data <- make_like_data(user, object, activity_id),
         {:ok, activity} <- insert(like_data, local),
         {:ok, object} <- add_like_to_object(activity, object),
         :ok <- maybe_federate(activity) do
      {:ok, activity, object}
    else
      %Activity{} = activity -> {:ok, activity, object}
      error -> {:error, error}
lain's avatar
lain committed
40
    end
lain's avatar
lain committed
41 42
  end

lain's avatar
lain committed
43 44 45 46
  def unlike(%User{} = actor, %Object{} = object) do
    with %Activity{} = activity <- get_existing_like(actor.ap_id, object),
         {:ok, _activity} <- Repo.delete(activity),
         {:ok, object} <- remove_like_from_object(activity, object) do
lain's avatar
lain committed
47
      {:ok, object}
lain's avatar
lain committed
48
      else _e -> {:ok, object}
lain's avatar
lain committed
49 50 51
    end
  end

lain's avatar
lain committed
52 53 54 55 56 57 58 59 60
  def announce(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object, activity_id \\ nil, local \\ true) do
    with announce_data <- make_announce_data(user, object, activity_id),
         {:ok, activity} <- insert(announce_data, local),
         {:ok, object} <- add_announce_to_object(activity, object),
         :ok <- maybe_federate(activity) do
      {:ok, activity, object}
    else
      error -> {:error, error}
    end
lain's avatar
lain committed
61 62
  end

lain's avatar
lain committed
63 64 65 66 67 68
  def follow(follower, followed, activity_id \\ nil, local \\ true) do
    with data <- make_follow_data(follower, followed, activity_id),
         {:ok, activity} <- insert(data, local),
         :ok <- maybe_federate(activity) do
      {:ok, activity}
    end
lain's avatar
lain committed
69 70
  end

lain's avatar
lain committed
71 72 73 74 75 76 77
  def unfollow(follower, followed, local \\ true) do
    with %Activity{} = follow_activity <- fetch_latest_follow(follower, followed),
         unfollow_data <- make_unfollow_data(follower, followed, follow_activity),
         {:ok, activity} <- insert(unfollow_data, local),
         :ok, maybe_federate(activity) do
      {:ok, activity}
    end
lain's avatar
lain committed
78 79
  end

lain's avatar
lain committed
80 81 82 83 84 85 86 87
  def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, local \\ true) do
    user = User.get_cached_by_ap_id(actor)
    data = %{
      "type" => "Delete",
      "actor" => actor,
      "object" => id,
      "to" => [user.follower_address, "https://www.w3.org/ns/activitystreams#Public"]
    }
lain's avatar
lain committed
88 89 90 91
    with Repo.delete(object),
         Repo.delete_all(Activity.all_non_create_by_object_ap_id_q(id)),
         Repo.delete_all(Activity.all_by_object_ap_id_q(id)),
         {:ok, activity} <- insert(data, local),
lain's avatar
lain committed
92 93 94 95 96
         :ok <- maybe_federate(activity) do
      {:ok, activity}
    end
  end

lain's avatar
lain committed
97 98
  def fetch_activities_for_context(context) do
    query = from activity in Activity,
99
      where: fragment("? @> ?", activity.data, ^%{ type: "Create", context: context }),
100
      order_by: [desc: :inserted_at]
lain's avatar
lain committed
101
    Repo.all(query)
lain's avatar
lain committed
102 103
  end

lain's avatar
lain committed
104
  def fetch_public_activities(opts \\ %{}) do
lain's avatar
lain committed
105 106 107 108
    public = ["https://www.w3.org/ns/activitystreams#Public"]
    fetch_activities(public, opts)
  end

lain's avatar
lain committed
109 110 111 112
  defp restrict_since(query, %{"since_id" => since_id}) do
    from activity in query, where: activity.id > ^since_id
  end
  defp restrict_since(query, _), do: query
lain's avatar
lain committed
113

lain's avatar
lain committed
114 115
  defp restrict_recipients(query, recipients) do
    Enum.reduce(recipients, query, fn (recipient, q) ->
lain's avatar
lain committed
116 117 118 119
      map = %{ to: [recipient] }
      from activity in q,
      or_where: fragment(~s(? @> ?), activity.data, ^map)
    end)
lain's avatar
lain committed
120
  end
lain's avatar
lain committed
121

lain's avatar
lain committed
122 123
  defp restrict_local(query, %{"local_only" => true}) do
    from activity in query, where: activity.local == true
lain's avatar
lain committed
124
  end
lain's avatar
lain committed
125
  defp restrict_local(query, _), do: query
lain's avatar
lain committed
126

lain's avatar
lain committed
127 128
  defp restrict_max(query, %{"max_id" => max_id}) do
    from activity in query, where: activity.id < ^max_id
129
  end
lain's avatar
lain committed
130
  defp restrict_max(query, _), do: query
131

lain's avatar
lain committed
132 133
  defp restrict_actor(query, %{"actor_id" => actor_id}) do
    from activity in query,
134
      where: fragment("?->>'actor' = ?", activity.data, ^actor_id)
135
  end
lain's avatar
lain committed
136
  defp restrict_actor(query, _), do: query
137

138 139 140 141 142 143
  defp restrict_type(query, %{"type" => type}) do
    from activity in query,
      where: fragment("?->>'type' = ?", activity.data, ^type)
  end
  defp restrict_type(query, _), do: query

lain's avatar
lain committed
144 145 146
  def fetch_activities(recipients, opts \\ %{}) do
    base_query = from activity in Activity,
      limit: 20,
147
      order_by: [desc: :id]
lain's avatar
lain committed
148

lain's avatar
lain committed
149 150 151 152 153 154
    base_query
    |> restrict_recipients(recipients)
    |> restrict_since(opts)
    |> restrict_local(opts)
    |> restrict_max(opts)
    |> restrict_actor(opts)
155
    |> restrict_type(opts)
lain's avatar
lain committed
156 157
    |> Repo.all
    |> Enum.reverse
dtluna's avatar
dtluna committed
158 159
  end

lain's avatar
lain committed
160
  def upload(file) do
lain's avatar
lain committed
161 162 163
    data = Upload.store(file)
    Repo.insert(%Object{data: data})
  end
lain's avatar
lain committed
164
end