activity_representer.ex 7.62 KB
Newer Older
1
2
# THIS MODULE IS DEPRECATED! DON'T USE IT!
# USE THE Pleroma.Web.TwitterAPI.Views.ActivityView MODULE!
lain's avatar
lain committed
3
4
defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
  use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter
dtluna's avatar
dtluna committed
5
  alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
6
  alias Pleroma.{Activity, User}
7
  alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView, ActivityView}
8
  alias Pleroma.Web.CommonAPI.Utils
lain's avatar
lain committed
9
  alias Pleroma.Formatter
10
  alias Pleroma.HTML
lain's avatar
lain committed
11

lain's avatar
lain committed
12
  defp user_by_ap_id(user_list, ap_id) do
lain's avatar
lain committed
13
    Enum.find(user_list, fn %{ap_id: user_id} -> ap_id == user_id end)
lain's avatar
lain committed
14
15
  end

lain's avatar
lain committed
16
17
18
19
20
  def to_map(
        %Activity{data: %{"type" => "Announce", "actor" => actor, "published" => created_at}} =
          activity,
        %{users: users, announced_activity: announced_activity} = opts
      ) do
lain's avatar
lain committed
21
    user = user_by_ap_id(users, actor)
lain's avatar
lain committed
22
    created_at = created_at |> Utils.date_to_asctime()
lain's avatar
lain committed
23
24
25
26
27

    text = "#{user.nickname} retweeted a status."

    announced_user = user_by_ap_id(users, announced_activity.data["actor"])
    retweeted_status = to_map(announced_activity, Map.merge(%{user: announced_user}, opts))
lain's avatar
lain committed
28

lain's avatar
lain committed
29
30
    %{
      "id" => activity.id,
dtluna's avatar
dtluna committed
31
      "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
lain's avatar
lain committed
32
33
      "statusnet_html" => text,
      "text" => text,
lain's avatar
lain committed
34
      "is_local" => activity.local,
lain's avatar
lain committed
35
36
37
      "is_post_verb" => false,
      "uri" => "tag:#{activity.data["id"]}:objectType=note",
      "created_at" => created_at,
38
      "retweeted_status" => retweeted_status,
lain's avatar
lain committed
39
      "statusnet_conversation_id" => conversation_id(announced_activity),
40
41
      "external_url" => activity.data["id"],
      "activity_type" => "repeat"
lain's avatar
lain committed
42
43
44
    }
  end

lain's avatar
lain committed
45
46
47
48
49
  def to_map(
        %Activity{data: %{"type" => "Like", "published" => created_at}} = activity,
        %{user: user, liked_activity: liked_activity} = opts
      ) do
    created_at = created_at |> Utils.date_to_asctime()
lain's avatar
lain committed
50
51
52
53
54

    text = "#{user.nickname} favorited a status."

    %{
      "id" => activity.id,
dtluna's avatar
dtluna committed
55
      "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
56
      "statusnet_html" => text,
lain's avatar
lain committed
57
      "text" => text,
lain's avatar
lain committed
58
      "is_local" => activity.local,
lain's avatar
lain committed
59
60
61
62
      "is_post_verb" => false,
      "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
      "created_at" => created_at,
      "in_reply_to_status_id" => liked_activity.id,
63
64
      "external_url" => activity.data["id"],
      "activity_type" => "like"
lain's avatar
lain committed
65
66
67
    }
  end

lain's avatar
lain committed
68
69
70
71
72
73
  def to_map(
        %Activity{data: %{"type" => "Follow", "object" => followed_id}} = activity,
        %{user: user} = opts
      ) do
    created_at = activity.data["published"] || DateTime.to_iso8601(activity.inserted_at)
    created_at = created_at |> Utils.date_to_asctime()
lain's avatar
lain committed
74

75
76
    followed = User.get_cached_by_ap_id(followed_id)
    text = "#{user.nickname} started following #{followed.nickname}"
lain's avatar
lain committed
77

dtluna's avatar
dtluna committed
78
79
    %{
      "id" => activity.id,
dtluna's avatar
dtluna committed
80
      "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
dtluna's avatar
dtluna committed
81
      "attentions" => [],
82
83
      "statusnet_html" => text,
      "text" => text,
lain's avatar
lain committed
84
      "is_local" => activity.local,
dtluna's avatar
dtluna committed
85
      "is_post_verb" => false,
lain's avatar
lain committed
86
      "created_at" => created_at,
dtluna's avatar
dtluna committed
87
      "in_reply_to_status_id" => nil,
88
89
      "external_url" => activity.data["id"],
      "activity_type" => "follow"
lain's avatar
lain committed
90
91
92
93
94
    }
  end

  # TODO:
  # Make this more proper. Just a placeholder to not break the frontend.
lain's avatar
lain committed
95
96
97
98
99
100
101
  def to_map(
        %Activity{
          data: %{"type" => "Undo", "published" => created_at, "object" => undid_activity}
        } = activity,
        %{user: user} = opts
      ) do
    created_at = created_at |> Utils.date_to_asctime()
lain's avatar
lain committed
102

normandy's avatar
normandy committed
103
    text = "#{user.nickname} undid the action at #{undid_activity["id"]}"
lain's avatar
lain committed
104

lain's avatar
lain committed
105
106
107
108
109
110
111
112
113
114
    %{
      "id" => activity.id,
      "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
      "attentions" => [],
      "statusnet_html" => text,
      "text" => text,
      "is_local" => activity.local,
      "is_post_verb" => false,
      "created_at" => created_at,
      "in_reply_to_status_id" => nil,
115
116
      "external_url" => activity.data["id"],
      "activity_type" => "undo"
dtluna's avatar
dtluna committed
117
118
119
    }
  end

lain's avatar
lain committed
120
121
122
123
124
125
  def to_map(
        %Activity{data: %{"type" => "Delete", "published" => created_at, "object" => _}} =
          activity,
        %{user: user} = opts
      ) do
    created_at = created_at |> Utils.date_to_asctime()
lain's avatar
lain committed
126
127

    %{
128
129
      "id" => activity.id,
      "uri" => activity.data["object"],
lain's avatar
lain committed
130
131
132
      "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
      "attentions" => [],
      "statusnet_html" => "deleted notice {{tag",
lain's avatar
lain committed
133
      "text" => "deleted notice {{tag",
lain's avatar
lain committed
134
135
136
137
138
139
140
141
142
      "is_local" => activity.local,
      "is_post_verb" => false,
      "created_at" => created_at,
      "in_reply_to_status_id" => nil,
      "external_url" => activity.data["id"],
      "activity_type" => "delete"
    }
  end

lain's avatar
lain committed
143
  def to_map(
Maksim's avatar
Maksim committed
144
        %Activity{data: %{"object" => %{"content" => _content} = object}} = activity,
lain's avatar
lain committed
145
146
147
        %{user: user} = opts
      ) do
    created_at = object["published"] |> Utils.date_to_asctime()
148
149
150
151
    like_count = object["like_count"] || 0
    announcement_count = object["announcement_count"] || 0
    favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
    repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
152
153
154

    mentions = opts[:mentioned] || []

lain's avatar
lain committed
155
156
157
158
159
    attentions =
      activity.recipients
      |> Enum.map(fn ap_id -> Enum.find(mentions, fn user -> ap_id == user.ap_id end) end)
      |> Enum.filter(& &1)
      |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
160

161
    conversation_id = conversation_id(activity)
162

163
    tags = activity.data["object"]["tag"] || []
lain's avatar
lain committed
164
165
    possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")

lain's avatar
lain committed
166
    tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
167

Maksim's avatar
Maksim committed
168
    {_summary, content} = ActivityView.render_content(object)
lain's avatar
lain committed
169
170

    html =
171
      HTML.filter_tags(content, User.html_filter_policy(opts[:for]))
lain's avatar
lain committed
172
      |> Formatter.emojify(object["emoji"])
lain's avatar
lain committed
173

174
    attachments = object["attachment"] || []
Haelwenn's avatar
Haelwenn committed
175

176
177
    reply_parent = Activity.get_in_reply_to_activity(activity)

178
    reply_user = reply_parent && User.get_cached_by_ap_id(reply_parent.actor)
179

lain's avatar
lain committed
180
181
    %{
      "id" => activity.id,
182
      "uri" => activity.data["object"]["id"],
dtluna's avatar
dtluna committed
183
      "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
lain's avatar
lain committed
184
      "statusnet_html" => html,
185
      "text" => HTML.strip_tags(content),
lain's avatar
lain committed
186
      "is_local" => activity.local,
lain's avatar
lain committed
187
      "is_post_verb" => true,
lain's avatar
lain committed
188
      "created_at" => created_at,
189
      "in_reply_to_status_id" => object["inReplyToStatusId"],
190
191
192
193
      "in_reply_to_screen_name" => reply_user && reply_user.nickname,
      "in_reply_to_profileurl" => User.profile_url(reply_user),
      "in_reply_to_ostatus_uri" => reply_user && reply_user.ap_id,
      "in_reply_to_user_id" => reply_user && reply_user.id,
194
      "statusnet_conversation_id" => conversation_id,
Haelwenn's avatar
Haelwenn committed
195
      "attachments" => attachments |> ObjectRepresenter.enum_to_list(opts),
lain's avatar
lain committed
196
      "attentions" => attentions,
lain's avatar
lain committed
197
      "fave_num" => like_count,
lain's avatar
lain committed
198
      "repeat_num" => announcement_count,
199
200
      "favorited" => to_boolean(favorited),
      "repeated" => to_boolean(repeated),
lain's avatar
lain committed
201
      "external_url" => object["external_url"] || object["id"],
202
203
      "tags" => tags,
      "activity_type" => "post",
lain's avatar
lain committed
204
      "possibly_sensitive" => possibly_sensitive,
205
206
      "visibility" => Pleroma.Web.MastodonAPI.StatusView.get_visibility(object),
      "summary" => object["summary"]
lain's avatar
lain committed
207
208
    }
  end
lain's avatar
lain committed
209

210
211
212
  def conversation_id(activity) do
    with context when not is_nil(context) <- activity.data["context"] do
      TwitterAPI.context_to_conversation_id(context)
lain's avatar
lain committed
213
214
    else
      _e -> nil
215
216
217
    end
  end

218
219
220
221
222
223
224
225
226
227
228
  defp to_boolean(false) do
    false
  end

  defp to_boolean(nil) do
    false
  end

  defp to_boolean(_) do
    true
  end
lain's avatar
lain committed
229
end