activity_pub.ex 5.99 KB
Newer Older
lain's avatar
lain committed
1
2
defmodule Pleroma.Web.ActivityPub.ActivityPub do
  alias Pleroma.Repo
lain's avatar
lain committed
3
  alias Pleroma.{Activity, Object, Upload, User}
lain's avatar
lain committed
4
  import Ecto.Query
lain's avatar
lain committed
5

lain's avatar
lain committed
6
  def insert(map, local \\ true) when is_map(map) do
lain's avatar
lain committed
7
8
9
    map = map
    |> Map.put_new_lazy("id", &generate_activity_id/0)
    |> Map.put_new_lazy("published", &make_date/0)
lain's avatar
lain committed
10

dtluna's avatar
dtluna committed
11
    map = if is_map(map["object"]) do
lain's avatar
lain committed
12
      object = Map.put_new_lazy(map["object"], "id", &generate_object_id/0)
13
      Repo.insert!(%Object{data: object})
lain's avatar
lain committed
14
15
16
17
18
      Map.put(map, "object", object)
    else
      map
    end

lain's avatar
lain committed
19
    Repo.insert(%Activity{data: map, local: local})
lain's avatar
lain committed
20
  end
lain's avatar
lain committed
21

lain's avatar
lain committed
22
  def create(to, actor, context, object, additional \\ %{}, published \\ nil, local \\ true) do
23
24
25
26
    published = published || make_date()

    activity = %{
      "type" => "Create",
lain's avatar
lain committed
27
      "to" => to |> Enum.uniq,
28
29
30
31
32
33
34
      "actor" => actor.ap_id,
      "object" => object,
      "published" => published,
      "context" => context
    }
    |> Map.merge(additional)

lain's avatar
lain committed
35
    with {:ok, activity} <- insert(activity, local) do
36
      if actor.local do
37
        Pleroma.Web.Federator.enqueue(:publish, activity)
38
39
40
41
42
43
       end

      {:ok, activity}
    end
  end

lain's avatar
lain committed
44
45
46
47
48
  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,
lain's avatar
lain committed
49
          where: fragment("? @> ?", activity.data, ^%{actor: ap_id, object: id, type: "Like"})
lain's avatar
lain committed
50

lain's avatar
lain committed
51
52
53
54
55
56
57
        activity = Repo.one(query)
        {:ok, activity, object}
      true ->
        data = %{
          "type" => "Like",
          "actor" => ap_id,
          "object" => id,
lain's avatar
lain committed
58
59
          "to" => [User.ap_followers(user), object.data["actor"]],
          "context" => object.data["context"]
lain's avatar
lain committed
60
        }
lain's avatar
lain committed
61

lain's avatar
lain committed
62
        {:ok, activity} = insert(data)
lain's avatar
lain committed
63

lain's avatar
lain committed
64
65
66
67
68
69
70
71
72
        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)

lain's avatar
lain committed
73
74
        update_object_in_activities(object)

lain's avatar
lain committed
75
76
77
78
        if user.local do
          Pleroma.Web.Federator.enqueue(:publish, activity)
        end

lain's avatar
lain committed
79
80
        {:ok, activity, object}
    end
lain's avatar
lain committed
81
82
  end

lain's avatar
lain committed
83
84
85
86
87
88
89
90
91
92
  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

lain's avatar
lain committed
93
  def unlike(%User{ap_id: ap_id}, %Object{data: %{ "id" => id}} = object) do
lain's avatar
lain committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
    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

lain's avatar
lain committed
120
  def generate_activity_id do
lain's avatar
lain committed
121
122
123
124
125
    generate_id("activities")
  end

  def generate_context_id do
    generate_id("contexts")
lain's avatar
lain committed
126
127
  end

lain's avatar
lain committed
128
  def generate_object_id do
129
    Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :object, Ecto.UUID.generate)
lain's avatar
lain committed
130
131
132
  end

  def generate_id(type) do
lain's avatar
lain committed
133
    "#{Pleroma.Web.base_url()}/#{type}/#{Ecto.UUID.generate}"
lain's avatar
lain committed
134
135
  end

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

  def fetch_activities(recipients, opts \\ %{}) do
lain's avatar
lain committed
142
    since_id = opts["since_id"] || 0
lain's avatar
lain committed
143

lain's avatar
lain committed
144
    query = from activity in Activity,
lain's avatar
lain committed
145
146
      limit: 20,
      order_by: [desc: :inserted_at]
lain's avatar
lain committed
147

lain's avatar
lain committed
148
149
150
151
152
153
154
155
156
    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

lain's avatar
lain committed
157
158
159
160
161
162
    query = if opts["local_only"] do
      from activity in query, where: activity.local == true
    else
      query
    end

163
164
165
166
167
168
    query = if opts["max_id"] do
      from activity in query, where: activity.id < ^opts["max_id"]
    else
      query
    end

dtluna's avatar
dtluna committed
169
170
171
172
173
174
175
    query = if opts["actor_id"] do
      from activity in query,
        where: fragment("? @> ?", activity.data, ^%{actor: opts["actor_id"]})
    else
      query
    end

lain's avatar
lain committed
176
    Repo.all(query)
lain's avatar
lain committed
177
    |> Enum.reverse
lain's avatar
lain committed
178
  end
lain's avatar
lain committed
179

lain's avatar
lain committed
180
  def announce(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object, local \\ true) do
lain's avatar
lain committed
181
182
183
184
    data = %{
      "type" => "Announce",
      "actor" => ap_id,
      "object" => id,
lain's avatar
lain committed
185
186
      "to" => [User.ap_followers(user), object.data["actor"]],
      "context" => object.data["context"]
lain's avatar
lain committed
187
188
    }

lain's avatar
lain committed
189
    {:ok, activity} = insert(data, local)
lain's avatar
lain committed
190
191
192
193
194
195
196
197
198
199
200
201

    announcements = [ap_id | (object.data["announcements"] || [])] |> Enum.uniq

    new_data = object.data
    |> Map.put("announcement_count", length(announcements))
    |> Map.put("announcements", announcements)

    changeset = Ecto.Changeset.change(object, data: new_data)
    {:ok, object} = Repo.update(changeset)

    update_object_in_activities(object)

lain's avatar
lain committed
202
203
204
205
    if user.local do
      Pleroma.Web.Federator.enqueue(:publish, activity)
    end

lain's avatar
lain committed
206
207
208
    {:ok, activity, object}
  end

lain's avatar
lain committed
209
210
211
212
213
  def fetch_activities_for_context(context) do
    query = from activity in Activity,
      where: fragment("? @> ?", activity.data, ^%{ context: context })
    Repo.all(query)
  end
lain's avatar
lain committed
214

lain's avatar
lain committed
215
  def upload(file) do
lain's avatar
lain committed
216
217
218
    data = Upload.store(file)
    Repo.insert(%Object{data: data})
  end
lain's avatar
lain committed
219
220
221
222

  defp make_date do
    DateTime.utc_now() |> DateTime.to_iso8601
  end
lain's avatar
lain committed
223
end