activity_pub.ex 4.56 KB
Newer Older
lain's avatar
lain committed
1
defmodule Pleroma.Web.ActivityPub.ActivityPub do
2
3
  alias Pleroma.{Activity, Repo, Object, Upload, User, Web}
  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
lain's avatar
lain committed
12
      Repo.insert(%Activity{data: map, local: local})
lain's avatar
lain committed
13
14
15
    else
      %Activity{} = activity -> {:ok, activity}
      error -> {:error, error}
lain's avatar
lain committed
16
    end
lain's avatar
lain committed
17
  end
lain's avatar
lain committed
18

lain's avatar
lain committed
19
  def create(to, actor, context, object, additional \\ %{}, published \\ nil, local \\ true) do
lain's avatar
lain committed
20
21
22
    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
23
24
      {:ok, activity}
    end
lain's avatar
lain committed
25
  end
lain's avatar
lain committed
26

lain's avatar
lain committed
27
  # TODO: This is weird, maybe we shouldn't check here if we can make the activity.
lain's avatar
lain committed
28
  def like(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object, activity_id \\ nil, local \\ true) do
lain's avatar
lain committed
29
30
31
32
33
34
35
36
37
    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
38
    end
lain's avatar
lain committed
39
40
  end

lain's avatar
lain committed
41
42
43
44
  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
45
      {:ok, object}
lain's avatar
lain committed
46
      else _e -> {:ok, object}
lain's avatar
lain committed
47
48
49
    end
  end

lain's avatar
lain committed
50
51
52
53
54
55
56
57
58
  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
59
60
  end

lain's avatar
lain committed
61
62
63
64
65
66
  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
67
68
  end

lain's avatar
lain committed
69
70
71
72
73
74
75
  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
76
77
  end

lain's avatar
lain committed
78
79
80
81
  def fetch_activities_for_context(context) do
    query = from activity in Activity,
      where: fragment("? @> ?", activity.data, ^%{ context: context })
    Repo.all(query)
lain's avatar
lain committed
82
83
  end

lain's avatar
lain committed
84
  def fetch_public_activities(opts \\ %{}) do
lain's avatar
lain committed
85
86
87
88
    public = ["https://www.w3.org/ns/activitystreams#Public"]
    fetch_activities(public, opts)
  end

lain's avatar
lain committed
89
90
91
92
  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
93

lain's avatar
lain committed
94
95
  defp restrict_recipients(query, recipients) do
    Enum.reduce(recipients, query, fn (recipient, q) ->
lain's avatar
lain committed
96
97
98
99
      map = %{ to: [recipient] }
      from activity in q,
      or_where: fragment(~s(? @> ?), activity.data, ^map)
    end)
lain's avatar
lain committed
100
  end
lain's avatar
lain committed
101

lain's avatar
lain committed
102
103
  defp restrict_local(query, %{"local_only" => true}) do
    from activity in query, where: activity.local == true
lain's avatar
lain committed
104
  end
lain's avatar
lain committed
105
  defp restrict_local(query, _), do: query
lain's avatar
lain committed
106

lain's avatar
lain committed
107
108
  defp restrict_max(query, %{"max_id" => max_id}) do
    from activity in query, where: activity.id < ^max_id
109
  end
lain's avatar
lain committed
110
  defp restrict_max(query, _), do: query
111

lain's avatar
lain committed
112
113
114
  defp restrict_actor(query, %{"actor_id" => actor_id}) do
    from activity in query,
      where: fragment("? @> ?", activity.data, ^%{actor: actor_id})
115
  end
lain's avatar
lain committed
116
  defp restrict_actor(query, _), do: query
117

lain's avatar
lain committed
118
119
120
121
  def fetch_activities(recipients, opts \\ %{}) do
    base_query = from activity in Activity,
      limit: 20,
      order_by: [desc: :inserted_at]
lain's avatar
lain committed
122

lain's avatar
lain committed
123
124
125
126
127
128
129
130
    base_query
    |> restrict_recipients(recipients)
    |> restrict_since(opts)
    |> restrict_local(opts)
    |> restrict_max(opts)
    |> restrict_actor(opts)
    |> Repo.all
    |> Enum.reverse
dtluna's avatar
dtluna committed
131
132
  end

lain's avatar
lain committed
133
  def upload(file) do
lain's avatar
lain committed
134
135
136
    data = Upload.store(file)
    Repo.insert(%Object{data: data})
  end
lain's avatar
lain committed
137
end