activity_pub.ex 9.27 KB
Newer Older
lain's avatar
lain committed
1
defmodule Pleroma.Web.ActivityPub.ActivityPub do
Thog's avatar
Thog committed
2
  alias Pleroma.{Activity, Repo, Object, Upload, User, Notification}
lain's avatar
lain committed
3
  alias Pleroma.Web.ActivityPub.Transmogrifier
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 9
  @httpoison Application.get_env(:pleroma, :httpoison)

lain's avatar
lain committed
10 11 12 13
  def get_recipients(data) do
    (data["to"] || []) ++ (data["cc"] || [])
  end

lain's avatar
lain committed
14
  def insert(map, local \\ true) when is_map(map) do
lain's avatar
lain committed
15 16 17
    with nil <- Activity.get_by_ap_id(map["id"]),
         map <- lazy_put_activity_defaults(map),
         :ok <- insert_full_object(map) do
lain's avatar
lain committed
18
      {:ok, activity} = Repo.insert(%Activity{data: map, local: local, actor: map["actor"], recipients: get_recipients(map)})
19
      Notification.create_notifications(activity)
lain's avatar
lain committed
20
      stream_out(activity)
21
      {:ok, activity}
lain's avatar
lain committed
22 23 24
    else
      %Activity{} = activity -> {:ok, activity}
      error -> {:error, error}
lain's avatar
lain committed
25
    end
lain's avatar
lain committed
26
  end
lain's avatar
lain committed
27

lain's avatar
lain committed
28 29 30 31
  def stream_out(activity) do
    if activity.data["type"] in ["Create", "Announce"] do
      Pleroma.Web.Streamer.stream("user", activity)
      if Enum.member?(activity.data["to"], "https://www.w3.org/ns/activitystreams#Public") do
lain's avatar
lain committed
32
        Pleroma.Web.Streamer.stream("public", activity)
lain's avatar
lain committed
33
        if activity.local do
Roger Braun's avatar
Roger Braun committed
34 35
          Pleroma.Web.Streamer.stream("public:local", activity)
        end
lain's avatar
lain committed
36
      end
lain's avatar
lain committed
37 38 39
    end
  end

lain's avatar
lain committed
40 41 42 43 44
  def create(%{to: to, actor: actor, context: context, object: object} = params) do
    additional = params[:additional] || %{}
    local = !(params[:local] == false) # only accept false as false value
    published = params[:published]

lain's avatar
lain committed
45 46 47
    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
48 49
      {:ok, activity}
    end
lain's avatar
lain committed
50
  end
lain's avatar
lain committed
51

52 53 54 55 56 57 58 59 60 61
  def accept(%{to: to, actor: actor, object: object} = params) do
    local = !(params[:local] == false) # only accept false as false value

    with data <- %{"to" => to, "type" => "Accept", "actor" => actor, "object" => object},
         {:ok, activity} <- insert(data, local),
         :ok <- maybe_federate(activity) do
      {:ok, activity}
    end
  end

lain's avatar
lain committed
62
  # TODO: This is weird, maybe we shouldn't check here if we can make the activity.
Thog's avatar
Thog committed
63
  def like(%User{ap_id: ap_id} = user, %Object{data: %{"id" => _}} = object, activity_id \\ nil, local \\ true) do
lain's avatar
lain committed
64 65 66 67 68 69 70 71 72
    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
73
    end
lain's avatar
lain committed
74 75
  end

lain's avatar
lain committed
76 77 78 79
  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
80
      {:ok, object}
lain's avatar
lain committed
81
      else _e -> {:ok, object}
lain's avatar
lain committed
82 83 84
    end
  end

Thog's avatar
Thog committed
85
  def announce(%User{ap_id: _} = user, %Object{data: %{"id" => _}} = object, activity_id \\ nil, local \\ true) do
lain's avatar
lain committed
86 87 88 89 90 91 92 93
    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
94 95
  end

lain's avatar
lain committed
96 97 98 99 100 101
  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
102 103
  end

lain's avatar
lain committed
104 105 106 107 108 109 110
  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
111 112
  end

lain's avatar
lain committed
113 114 115 116 117 118 119 120
  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
121 122 123
    with Repo.delete(object),
         Repo.delete_all(Activity.all_non_create_by_object_ap_id_q(id)),
         {:ok, activity} <- insert(data, local),
lain's avatar
lain committed
124 125 126 127 128
         :ok <- maybe_federate(activity) do
      {:ok, activity}
    end
  end

129
  def fetch_activities_for_context(context, opts \\ %{}) do
lain's avatar
lain committed
130
    query = from activity in Activity,
lain's avatar
lain committed
131
      where: fragment("?->>'type' = ? and ?->>'context' = ?", activity.data, "Create", activity.data, ^context),
132
      order_by: [desc: :id]
133
    query = restrict_blocked(query, opts)
lain's avatar
lain committed
134
    Repo.all(query)
lain's avatar
lain committed
135 136
  end

lain's avatar
lain committed
137
  def fetch_public_activities(opts \\ %{}) do
lain's avatar
lain committed
138 139 140 141
    public = ["https://www.w3.org/ns/activitystreams#Public"]
    fetch_activities(public, opts)
  end

lain's avatar
lain committed
142 143 144 145
  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
146

Roger Braun's avatar
Roger Braun committed
147 148 149 150 151 152
  defp restrict_tag(query, %{"tag" => tag}) do
    from activity in query,
      where: fragment("? <@ (? #> '{\"object\",\"tag\"}')", ^tag, activity.data)
  end
  defp restrict_tag(query, _), do: query

lain's avatar
lain committed
153
  defp restrict_recipients(query, []), do: query
lain's avatar
lain committed
154
  defp restrict_recipients(query, recipients) do
lain's avatar
lain committed
155 156
    from activity in query,
     where: fragment("? && ?", ^recipients, activity.recipients)
lain's avatar
lain committed
157
  end
lain's avatar
lain committed
158

lain's avatar
lain committed
159 160
  defp restrict_local(query, %{"local_only" => true}) do
    from activity in query, where: activity.local == true
lain's avatar
lain committed
161
  end
lain's avatar
lain committed
162
  defp restrict_local(query, _), do: query
lain's avatar
lain committed
163

lain's avatar
lain committed
164 165
  defp restrict_max(query, %{"max_id" => max_id}) do
    from activity in query, where: activity.id < ^max_id
166
  end
lain's avatar
lain committed
167
  defp restrict_max(query, _), do: query
168

lain's avatar
lain committed
169 170
  defp restrict_actor(query, %{"actor_id" => actor_id}) do
    from activity in query,
lain's avatar
lain committed
171
      where: activity.actor == ^actor_id
172
  end
lain's avatar
lain committed
173
  defp restrict_actor(query, _), do: query
174

lain's avatar
lain committed
175 176 177
  defp restrict_type(query, %{"type" => type}) when is_binary(type) do
    restrict_type(query, %{"type" => [type]})
  end
178 179
  defp restrict_type(query, %{"type" => type}) do
    from activity in query,
lain's avatar
lain committed
180
      where: fragment("?->>'type' = ANY(?)", activity.data, ^type)
181 182 183
  end
  defp restrict_type(query, _), do: query

184 185 186 187 188 189
  defp restrict_favorited_by(query, %{"favorited_by" => ap_id}) do
    from activity in query,
      where: fragment("? <@ (? #> '{\"object\",\"likes\"}')", ^ap_id, activity.data)
  end
  defp restrict_favorited_by(query, _), do: query

eal's avatar
eal committed
190
  defp restrict_media(query, %{"only_media" => val}) when val == "true" or val == "1" do
eal's avatar
eal committed
191 192 193 194 195
    from activity in query,
      where: fragment("not (? #> '{\"object\",\"attachment\"}' = ?)", activity.data, ^[])
  end
  defp restrict_media(query, _), do: query

196
  # Only search through last 100_000 activities by default
197
  defp restrict_recent(query, %{"whole_db" => true}), do: query
198
  defp restrict_recent(query, _) do
lain's avatar
lain committed
199
    since = (Repo.aggregate(Activity, :max, :id) || 0) - 100_000
200 201 202 203 204

    from activity in query,
      where: activity.id > ^since
  end

lain's avatar
lain committed
205 206
  defp restrict_blocked(query, %{"blocking_user" => %User{info: info}}) do
    blocks = info["blocks"] || []
207
    from activity in query,
lain's avatar
lain committed
208
      where: fragment("not (? = ANY(?))", activity.actor, ^blocks)
209 210 211
  end
  defp restrict_blocked(query, _), do: query

lain's avatar
lain committed
212 213 214
  def fetch_activities(recipients, opts \\ %{}) do
    base_query = from activity in Activity,
      limit: 20,
lain's avatar
lain committed
215
      order_by: [fragment("? desc nulls last", activity.id)]
lain's avatar
lain committed
216

lain's avatar
lain committed
217 218
    base_query
    |> restrict_recipients(recipients)
Roger Braun's avatar
Roger Braun committed
219
    |> restrict_tag(opts)
lain's avatar
lain committed
220 221 222 223
    |> restrict_since(opts)
    |> restrict_local(opts)
    |> restrict_max(opts)
    |> restrict_actor(opts)
224
    |> restrict_type(opts)
225
    |> restrict_favorited_by(opts)
226
    |> restrict_recent(opts)
227
    |> restrict_blocked(opts)
eal's avatar
eal committed
228
    |> restrict_media(opts)
lain's avatar
lain committed
229 230
    |> Repo.all
    |> Enum.reverse
dtluna's avatar
dtluna committed
231 232
  end

lain's avatar
lain committed
233
  def upload(file) do
lain's avatar
lain committed
234 235 236
    data = Upload.store(file)
    Repo.insert(%Object{data: data})
  end
lain's avatar
lain committed
237

lain's avatar
lain committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
  def make_user_from_ap_id(ap_id) do
    with {:ok, %{status_code: 200, body: body}} <- @httpoison.get(ap_id, ["Accept": "application/activity+json"]),
    {:ok, data} <- Poison.decode(body)
      do
      user_data = %{
        ap_id: data["id"],
        info: %{
          "ap_enabled" => true,
          "source_data" => data
        },
        nickname: "#{data["preferredUsername"]}@#{URI.parse(ap_id).host}",
        name: data["name"]
      }

      User.insert_or_update_user(user_data)
    end
  end
255 256

  def publish(actor, activity) do
257 258 259 260 261
    {:ok, followers} = User.get_followers(actor)

    remote_users = Pleroma.Web.Salmon.remote_users(activity) ++ followers
    |> Enum.uniq

lain's avatar
lain committed
262
    {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
263 264 265 266 267 268 269 270 271 272
    Enum.each remote_users, fn(user) ->
      if user.info["ap_enabled"] do
        inbox = user.info["source_data"]["inbox"]
        Logger.info("Federating #{activity.data["id"]} to #{inbox}")
        host = URI.parse(inbox).host
        signature = Pleroma.Web.HTTPSignatures.sign(actor, %{host: host})
        @httpoison.post(inbox, Poison.encode!(data), [{"Content-Type", "application/activity+json"}, {"signature", signature}])
      end
    end
  end
lain's avatar
lain committed
273
end