Merge branch 'feature/nodeinfo' into 'develop'
[akkoma] / lib / pleroma / web / mastodon_api / mastodon_api_controller.ex
1 defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
2 use Pleroma.Web, :controller
3 alias Pleroma.{Repo, Activity, User, Notification, Stats}
4 alias Pleroma.Web
5 alias Pleroma.Web.MastodonAPI.{StatusView, AccountView, MastodonView}
6 alias Pleroma.Web.ActivityPub.ActivityPub
7 alias Pleroma.Web.{CommonAPI, OStatus}
8 alias Pleroma.Web.OAuth.{Authorization, Token, App}
9 alias Comeonin.Pbkdf2
10 import Ecto.Query
11 require Logger
12
13 def create_app(conn, params) do
14 with cs <- App.register_changeset(%App{}, params) |> IO.inspect(),
15 {:ok, app} <- Repo.insert(cs) |> IO.inspect() do
16 res = %{
17 id: app.id,
18 client_id: app.client_id,
19 client_secret: app.client_secret
20 }
21
22 json(conn, res)
23 end
24 end
25
26 def update_credentials(%{assigns: %{user: user}} = conn, params) do
27 original_user = user
28
29 params =
30 if bio = params["note"] do
31 Map.put(params, "bio", bio)
32 else
33 params
34 end
35
36 params =
37 if name = params["display_name"] do
38 Map.put(params, "name", name)
39 else
40 params
41 end
42
43 user =
44 if avatar = params["avatar"] do
45 with %Plug.Upload{} <- avatar,
46 {:ok, object} <- ActivityPub.upload(avatar),
47 change = Ecto.Changeset.change(user, %{avatar: object.data}),
48 {:ok, user} = User.update_and_set_cache(change) do
49 user
50 else
51 _e -> user
52 end
53 else
54 user
55 end
56
57 user =
58 if banner = params["header"] do
59 with %Plug.Upload{} <- banner,
60 {:ok, object} <- ActivityPub.upload(banner),
61 new_info <- Map.put(user.info, "banner", object.data),
62 change <- User.info_changeset(user, %{info: new_info}),
63 {:ok, user} <- User.update_and_set_cache(change) do
64 user
65 else
66 _e -> user
67 end
68 else
69 user
70 end
71
72 with changeset <- User.update_changeset(user, params),
73 {:ok, user} <- User.update_and_set_cache(changeset) do
74 if original_user != user do
75 CommonAPI.update(user)
76 end
77
78 json(conn, AccountView.render("account.json", %{user: user}))
79 else
80 _e ->
81 conn
82 |> put_status(403)
83 |> json(%{error: "Invalid request"})
84 end
85 end
86
87 def verify_credentials(%{assigns: %{user: user}} = conn, _) do
88 account = AccountView.render("account.json", %{user: user})
89 json(conn, account)
90 end
91
92 def user(conn, %{"id" => id}) do
93 with %User{} = user <- Repo.get(User, id) do
94 account = AccountView.render("account.json", %{user: user})
95 json(conn, account)
96 else
97 _e ->
98 conn
99 |> put_status(404)
100 |> json(%{error: "Can't find user"})
101 end
102 end
103
104 @instance Application.get_env(:pleroma, :instance)
105 @mastodon_api_level "2.3.3"
106
107 def masto_instance(conn, _params) do
108 response = %{
109 uri: Web.base_url(),
110 title: Keyword.get(@instance, :name),
111 description: "A Pleroma instance, an alternative fediverse server",
112 version: "#{@mastodon_api_level} (compatible; #{Keyword.get(@instance, :version)})",
113 email: Keyword.get(@instance, :email),
114 urls: %{
115 streaming_api: String.replace(Web.base_url(), ["http", "https"], "wss")
116 },
117 stats: Stats.get_stats(),
118 thumbnail: Web.base_url() <> "/instance/thumbnail.jpeg",
119 max_toot_chars: Keyword.get(@instance, :limit)
120 }
121
122 json(conn, response)
123 end
124
125 def peers(conn, _params) do
126 json(conn, Stats.get_peers())
127 end
128
129 defp mastodonized_emoji do
130 Pleroma.Formatter.get_custom_emoji()
131 |> Enum.map(fn {shortcode, relative_url} ->
132 url = to_string(URI.merge(Web.base_url(), relative_url))
133
134 %{
135 "shortcode" => shortcode,
136 "static_url" => url,
137 "url" => url
138 }
139 end)
140 end
141
142 def custom_emojis(conn, _params) do
143 mastodon_emoji = mastodonized_emoji()
144 json(conn, mastodon_emoji)
145 end
146
147 defp add_link_headers(conn, method, activities, param \\ false) do
148 last = List.last(activities)
149 first = List.first(activities)
150
151 if last do
152 min = last.id
153 max = first.id
154
155 {next_url, prev_url} =
156 if param do
157 {
158 mastodon_api_url(Pleroma.Web.Endpoint, method, param, max_id: min),
159 mastodon_api_url(Pleroma.Web.Endpoint, method, param, since_id: max)
160 }
161 else
162 {
163 mastodon_api_url(Pleroma.Web.Endpoint, method, max_id: min),
164 mastodon_api_url(Pleroma.Web.Endpoint, method, since_id: max)
165 }
166 end
167
168 conn
169 |> put_resp_header("link", "<#{next_url}>; rel=\"next\", <#{prev_url}>; rel=\"prev\"")
170 else
171 conn
172 end
173 end
174
175 def home_timeline(%{assigns: %{user: user}} = conn, params) do
176 params =
177 params
178 |> Map.put("type", ["Create", "Announce"])
179 |> Map.put("blocking_user", user)
180 |> Map.put("user", user)
181
182 activities =
183 ActivityPub.fetch_activities([user.ap_id | user.following], params)
184 |> Enum.reverse()
185
186 conn
187 |> add_link_headers(:home_timeline, activities)
188 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
189 end
190
191 def public_timeline(%{assigns: %{user: user}} = conn, params) do
192 params =
193 params
194 |> Map.put("type", ["Create", "Announce"])
195 |> Map.put("local_only", params["local"] in [true, "True", "true", "1"])
196 |> Map.put("blocking_user", user)
197
198 activities =
199 ActivityPub.fetch_public_activities(params)
200 |> Enum.reverse()
201
202 conn
203 |> add_link_headers(:public_timeline, activities)
204 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
205 end
206
207 def user_statuses(%{assigns: %{user: user}} = conn, params) do
208 with %User{ap_id: ap_id} <- Repo.get(User, params["id"]) do
209 params =
210 params
211 |> Map.put("type", ["Create", "Announce"])
212 |> Map.put("actor_id", ap_id)
213 |> Map.put("whole_db", true)
214
215 if params["pinned"] == "true" do
216 # Since Pleroma has no "pinned" posts feature, we'll just set an empty list here
217 activities = []
218 else
219 activities =
220 ActivityPub.fetch_public_activities(params)
221 |> Enum.reverse()
222 end
223
224 conn
225 |> add_link_headers(:user_statuses, activities, params["id"])
226 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
227 end
228 end
229
230 def get_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
231 with %Activity{} = activity <- Repo.get(Activity, id),
232 true <- ActivityPub.visible_for_user?(activity, user) do
233 render(conn, StatusView, "status.json", %{activity: activity, for: user})
234 end
235 end
236
237 def get_context(%{assigns: %{user: user}} = conn, %{"id" => id}) do
238 with %Activity{} = activity <- Repo.get(Activity, id),
239 activities <-
240 ActivityPub.fetch_activities_for_context(activity.data["context"], %{
241 "blocking_user" => user,
242 "user" => user
243 }),
244 activities <-
245 activities |> Enum.filter(fn %{id: aid} -> to_string(aid) != to_string(id) end),
246 activities <-
247 activities |> Enum.filter(fn %{data: %{"type" => type}} -> type == "Create" end),
248 grouped_activities <- Enum.group_by(activities, fn %{id: id} -> id < activity.id end) do
249 result = %{
250 ancestors:
251 StatusView.render(
252 "index.json",
253 for: user,
254 activities: grouped_activities[true] || [],
255 as: :activity
256 )
257 |> Enum.reverse(),
258 descendants:
259 StatusView.render(
260 "index.json",
261 for: user,
262 activities: grouped_activities[false] || [],
263 as: :activity
264 )
265 |> Enum.reverse()
266 }
267
268 json(conn, result)
269 end
270 end
271
272 def post_status(%{assigns: %{user: user}} = conn, %{"status" => _} = params) do
273 params =
274 params
275 |> Map.put("in_reply_to_status_id", params["in_reply_to_id"])
276 |> Map.put("no_attachment_links", true)
277
278 idempotency_key =
279 case get_req_header(conn, "idempotency-key") do
280 [key] -> key
281 _ -> Ecto.UUID.generate()
282 end
283
284 {:ok, activity} =
285 Cachex.get!(
286 :user_cache,
287 "idem:#{idempotency_key}",
288 fallback: fn _ -> CommonAPI.post(user, params) end
289 )
290
291 Cachex.expire(:user_cache, "idem:#{idempotency_key}", :timer.seconds(5 * 60))
292
293 render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
294 end
295
296 def delete_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
297 with {:ok, %Activity{}} <- CommonAPI.delete(id, user) do
298 json(conn, %{})
299 else
300 _e ->
301 conn
302 |> put_status(403)
303 |> json(%{error: "Can't delete this post"})
304 end
305 end
306
307 def reblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
308 with {:ok, announce, _activity} = CommonAPI.repeat(ap_id_or_id, user) do
309 render(conn, StatusView, "status.json", %{activity: announce, for: user, as: :activity})
310 end
311 end
312
313 def fav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
314 with {:ok, _fav, %{data: %{"id" => id}}} = CommonAPI.favorite(ap_id_or_id, user),
315 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
316 render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
317 end
318 end
319
320 def unfav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
321 with {:ok, %{data: %{"id" => id}}} = CommonAPI.unfavorite(ap_id_or_id, user),
322 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
323 render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
324 end
325 end
326
327 def notifications(%{assigns: %{user: user}} = conn, params) do
328 notifications = Notification.for_user(user, params)
329
330 result =
331 Enum.map(notifications, fn x ->
332 render_notification(user, x)
333 end)
334 |> Enum.filter(& &1)
335
336 conn
337 |> add_link_headers(:notifications, notifications)
338 |> json(result)
339 end
340
341 def get_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
342 with {:ok, notification} <- Notification.get(user, id) do
343 json(conn, render_notification(user, notification))
344 else
345 {:error, reason} ->
346 conn
347 |> put_resp_content_type("application/json")
348 |> send_resp(403, Jason.encode!(%{"error" => reason}))
349 end
350 end
351
352 def clear_notifications(%{assigns: %{user: user}} = conn, _params) do
353 Notification.clear(user)
354 json(conn, %{})
355 end
356
357 def dismiss_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
358 with {:ok, _notif} <- Notification.dismiss(user, id) do
359 json(conn, %{})
360 else
361 {:error, reason} ->
362 conn
363 |> put_resp_content_type("application/json")
364 |> send_resp(403, Jason.encode!(%{"error" => reason}))
365 end
366 end
367
368 def relationships(%{assigns: %{user: user}} = conn, %{"id" => id}) do
369 id = List.wrap(id)
370 q = from(u in User, where: u.id in ^id)
371 targets = Repo.all(q)
372 render(conn, AccountView, "relationships.json", %{user: user, targets: targets})
373 end
374
375 def upload(%{assigns: %{user: _}} = conn, %{"file" => file}) do
376 with {:ok, object} <- ActivityPub.upload(file) do
377 data =
378 object.data
379 |> Map.put("id", object.id)
380
381 render(conn, StatusView, "attachment.json", %{attachment: data})
382 end
383 end
384
385 def favourited_by(conn, %{"id" => id}) do
386 with %Activity{data: %{"object" => %{"likes" => likes}}} <- Repo.get(Activity, id) do
387 q = from(u in User, where: u.ap_id in ^likes)
388 users = Repo.all(q)
389 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
390 else
391 _ -> json(conn, [])
392 end
393 end
394
395 def reblogged_by(conn, %{"id" => id}) do
396 with %Activity{data: %{"object" => %{"announcements" => announces}}} <- Repo.get(Activity, id) do
397 q = from(u in User, where: u.ap_id in ^announces)
398 users = Repo.all(q)
399 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
400 else
401 _ -> json(conn, [])
402 end
403 end
404
405 def hashtag_timeline(%{assigns: %{user: user}} = conn, params) do
406 params =
407 params
408 |> Map.put("type", "Create")
409 |> Map.put("local_only", !!params["local"])
410 |> Map.put("blocking_user", user)
411
412 activities =
413 ActivityPub.fetch_public_activities(params)
414 |> Enum.reverse()
415
416 conn
417 |> add_link_headers(:hashtag_timeline, activities, params["tag"])
418 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
419 end
420
421 # TODO: Pagination
422 def followers(conn, %{"id" => id}) do
423 with %User{} = user <- Repo.get(User, id),
424 {:ok, followers} <- User.get_followers(user) do
425 render(conn, AccountView, "accounts.json", %{users: followers, as: :user})
426 end
427 end
428
429 def following(conn, %{"id" => id}) do
430 with %User{} = user <- Repo.get(User, id),
431 {:ok, followers} <- User.get_friends(user) do
432 render(conn, AccountView, "accounts.json", %{users: followers, as: :user})
433 end
434 end
435
436 def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
437 with %User{} = followed <- Repo.get(User, id),
438 {:ok, follower} <- User.follow(follower, followed),
439 {:ok, _activity} <- ActivityPub.follow(follower, followed) do
440 render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
441 else
442 {:error, message} ->
443 conn
444 |> put_resp_content_type("application/json")
445 |> send_resp(403, Jason.encode!(%{"error" => message}))
446 end
447 end
448
449 def follow(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do
450 with %User{} = followed <- Repo.get_by(User, nickname: uri),
451 {:ok, follower} <- User.follow(follower, followed),
452 {:ok, _activity} <- ActivityPub.follow(follower, followed) do
453 render(conn, AccountView, "account.json", %{user: followed})
454 else
455 {:error, message} ->
456 conn
457 |> put_resp_content_type("application/json")
458 |> send_resp(403, Jason.encode!(%{"error" => message}))
459 end
460 end
461
462 # TODO: Clean up and unify
463 def unfollow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
464 with %User{} = followed <- Repo.get(User, id),
465 {:ok, follower, follow_activity} <- User.unfollow(follower, followed),
466 {:ok, _activity} <-
467 ActivityPub.insert(%{
468 "type" => "Undo",
469 "actor" => follower.ap_id,
470 # get latest Follow for these users
471 "object" => follow_activity.data["id"]
472 }) do
473 render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
474 end
475 end
476
477 def block(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
478 with %User{} = blocked <- Repo.get(User, id),
479 {:ok, blocker} <- User.block(blocker, blocked) do
480 render(conn, AccountView, "relationship.json", %{user: blocker, target: blocked})
481 else
482 {:error, message} ->
483 conn
484 |> put_resp_content_type("application/json")
485 |> send_resp(403, Jason.encode!(%{"error" => message}))
486 end
487 end
488
489 def unblock(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
490 with %User{} = blocked <- Repo.get(User, id),
491 {:ok, blocker} <- User.unblock(blocker, blocked) do
492 render(conn, AccountView, "relationship.json", %{user: blocker, target: blocked})
493 else
494 {:error, message} ->
495 conn
496 |> put_resp_content_type("application/json")
497 |> send_resp(403, Jason.encode!(%{"error" => message}))
498 end
499 end
500
501 # TODO: Use proper query
502 def blocks(%{assigns: %{user: user}} = conn, _) do
503 with blocked_users <- user.info["blocks"] || [],
504 accounts <- Enum.map(blocked_users, fn ap_id -> User.get_cached_by_ap_id(ap_id) end) do
505 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
506 json(conn, res)
507 end
508 end
509
510 def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
511 accounts = User.search(query, params["resolve"] == "true")
512
513 fetched =
514 if Regex.match?(~r/https?:/, query) do
515 with {:ok, activities} <- OStatus.fetch_activity_from_url(query) do
516 activities
517 |> Enum.filter(fn
518 %{data: %{"type" => "Create"}} -> true
519 _ -> false
520 end)
521 else
522 _e -> []
523 end
524 end || []
525
526 q =
527 from(
528 a in Activity,
529 where: fragment("?->>'type' = 'Create'", a.data),
530 where: "https://www.w3.org/ns/activitystreams#Public" in a.recipients,
531 where:
532 fragment(
533 "to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)",
534 a.data,
535 ^query
536 ),
537 limit: 20,
538 order_by: [desc: :id]
539 )
540
541 statuses = Repo.all(q) ++ fetched
542
543 tags =
544 String.split(query)
545 |> Enum.uniq()
546 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
547 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
548
549 res = %{
550 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
551 "statuses" =>
552 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
553 "hashtags" => tags
554 }
555
556 json(conn, res)
557 end
558
559 def account_search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
560 accounts = User.search(query, params["resolve"] == "true")
561
562 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
563
564 json(conn, res)
565 end
566
567 def favourites(%{assigns: %{user: user}} = conn, _) do
568 params =
569 %{}
570 |> Map.put("type", "Create")
571 |> Map.put("favorited_by", user.ap_id)
572 |> Map.put("blocking_user", user)
573
574 activities =
575 ActivityPub.fetch_public_activities(params)
576 |> Enum.reverse()
577
578 conn
579 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
580 end
581
582 def index(%{assigns: %{user: user}} = conn, _params) do
583 token =
584 conn
585 |> get_session(:oauth_token)
586
587 if user && token do
588 mastodon_emoji = mastodonized_emoji()
589 accounts = Map.put(%{}, user.id, AccountView.render("account.json", %{user: user}))
590
591 initial_state =
592 %{
593 meta: %{
594 streaming_api_base_url:
595 String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws"),
596 access_token: token,
597 locale: "en",
598 domain: Pleroma.Web.Endpoint.host(),
599 admin: "1",
600 me: "#{user.id}",
601 unfollow_modal: false,
602 boost_modal: false,
603 delete_modal: true,
604 auto_play_gif: false,
605 reduce_motion: false
606 },
607 compose: %{
608 me: "#{user.id}",
609 default_privacy: "public",
610 default_sensitive: false
611 },
612 media_attachments: %{
613 accept_content_types: [
614 ".jpg",
615 ".jpeg",
616 ".png",
617 ".gif",
618 ".webm",
619 ".mp4",
620 ".m4v",
621 "image\/jpeg",
622 "image\/png",
623 "image\/gif",
624 "video\/webm",
625 "video\/mp4"
626 ]
627 },
628 settings:
629 Map.get(user.info, "settings") ||
630 %{
631 onboarded: true,
632 home: %{
633 shows: %{
634 reblog: true,
635 reply: true
636 }
637 },
638 notifications: %{
639 alerts: %{
640 follow: true,
641 favourite: true,
642 reblog: true,
643 mention: true
644 },
645 shows: %{
646 follow: true,
647 favourite: true,
648 reblog: true,
649 mention: true
650 },
651 sounds: %{
652 follow: true,
653 favourite: true,
654 reblog: true,
655 mention: true
656 }
657 }
658 },
659 push_subscription: nil,
660 accounts: accounts,
661 custom_emojis: mastodon_emoji,
662 char_limit: Keyword.get(@instance, :limit)
663 }
664 |> Jason.encode!()
665
666 conn
667 |> put_layout(false)
668 |> render(MastodonView, "index.html", %{initial_state: initial_state})
669 else
670 conn
671 |> redirect(to: "/web/login")
672 end
673 end
674
675 def put_settings(%{assigns: %{user: user}} = conn, %{"data" => settings} = _params) do
676 with new_info <- Map.put(user.info, "settings", settings),
677 change <- User.info_changeset(user, %{info: new_info}),
678 {:ok, _user} <- User.update_and_set_cache(change) do
679 conn
680 |> json(%{})
681 else
682 e ->
683 conn
684 |> json(%{error: inspect(e)})
685 end
686 end
687
688 def login(conn, _) do
689 conn
690 |> render(MastodonView, "login.html", %{error: false})
691 end
692
693 defp get_or_make_app() do
694 with %App{} = app <- Repo.get_by(App, client_name: "Mastodon-Local") do
695 {:ok, app}
696 else
697 _e ->
698 cs =
699 App.register_changeset(%App{}, %{
700 client_name: "Mastodon-Local",
701 redirect_uris: ".",
702 scopes: "read,write,follow"
703 })
704
705 Repo.insert(cs)
706 end
707 end
708
709 def login_post(conn, %{"authorization" => %{"name" => name, "password" => password}}) do
710 with %User{} = user <- User.get_by_nickname_or_email(name),
711 true <- Pbkdf2.checkpw(password, user.password_hash),
712 {:ok, app} <- get_or_make_app(),
713 {:ok, auth} <- Authorization.create_authorization(app, user),
714 {:ok, token} <- Token.exchange_token(app, auth) do
715 conn
716 |> put_session(:oauth_token, token.token)
717 |> redirect(to: "/web/getting-started")
718 else
719 _e ->
720 conn
721 |> render(MastodonView, "login.html", %{error: "Wrong username or password"})
722 end
723 end
724
725 def logout(conn, _) do
726 conn
727 |> clear_session
728 |> redirect(to: "/")
729 end
730
731 def relationship_noop(%{assigns: %{user: user}} = conn, %{"id" => id}) do
732 Logger.debug("Unimplemented, returning unmodified relationship")
733
734 with %User{} = target <- Repo.get(User, id) do
735 render(conn, AccountView, "relationship.json", %{user: user, target: target})
736 end
737 end
738
739 def empty_array(conn, _) do
740 Logger.debug("Unimplemented, returning an empty array")
741 json(conn, [])
742 end
743
744 def empty_object(conn, _) do
745 Logger.debug("Unimplemented, returning an empty object")
746 json(conn, %{})
747 end
748
749 def render_notification(user, %{id: id, activity: activity, inserted_at: created_at} = _params) do
750 actor = User.get_cached_by_ap_id(activity.data["actor"])
751
752 created_at =
753 NaiveDateTime.to_iso8601(created_at)
754 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
755
756 case activity.data["type"] do
757 "Create" ->
758 %{
759 id: id,
760 type: "mention",
761 created_at: created_at,
762 account: AccountView.render("account.json", %{user: actor}),
763 status: StatusView.render("status.json", %{activity: activity, for: user})
764 }
765
766 "Like" ->
767 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
768
769 %{
770 id: id,
771 type: "favourite",
772 created_at: created_at,
773 account: AccountView.render("account.json", %{user: actor}),
774 status: StatusView.render("status.json", %{activity: liked_activity, for: user})
775 }
776
777 "Announce" ->
778 announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
779
780 %{
781 id: id,
782 type: "reblog",
783 created_at: created_at,
784 account: AccountView.render("account.json", %{user: actor}),
785 status: StatusView.render("status.json", %{activity: announced_activity, for: user})
786 }
787
788 "Follow" ->
789 %{
790 id: id,
791 type: "follow",
792 created_at: created_at,
793 account: AccountView.render("account.json", %{user: actor})
794 }
795
796 _ ->
797 nil
798 end
799 end
800 end