85f9c5b7bcdeb6d9cf1a649ebccf764851a040de
[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(Pleroma.Web.Endpoint.static_url(), "http", "ws")
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: reading_user}} = conn, params) do
208 with %User{} = user <- Repo.get(User, params["id"]) do
209 # Since Pleroma has no "pinned" posts feature, we'll just set an empty list here
210 activities =
211 if params["pinned"] == "true" do
212 []
213 else
214 ActivityPub.fetch_user_activities(user, reading_user, params)
215 end
216
217 conn
218 |> add_link_headers(:user_statuses, activities, params["id"])
219 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
220 end
221 end
222
223 def get_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
224 with %Activity{} = activity <- Repo.get(Activity, id),
225 true <- ActivityPub.visible_for_user?(activity, user) do
226 render(conn, StatusView, "status.json", %{activity: activity, for: user})
227 end
228 end
229
230 def get_context(%{assigns: %{user: user}} = conn, %{"id" => id}) do
231 with %Activity{} = activity <- Repo.get(Activity, id),
232 activities <-
233 ActivityPub.fetch_activities_for_context(activity.data["context"], %{
234 "blocking_user" => user,
235 "user" => user
236 }),
237 activities <-
238 activities |> Enum.filter(fn %{id: aid} -> to_string(aid) != to_string(id) end),
239 activities <-
240 activities |> Enum.filter(fn %{data: %{"type" => type}} -> type == "Create" end),
241 grouped_activities <- Enum.group_by(activities, fn %{id: id} -> id < activity.id end) do
242 result = %{
243 ancestors:
244 StatusView.render(
245 "index.json",
246 for: user,
247 activities: grouped_activities[true] || [],
248 as: :activity
249 )
250 |> Enum.reverse(),
251 descendants:
252 StatusView.render(
253 "index.json",
254 for: user,
255 activities: grouped_activities[false] || [],
256 as: :activity
257 )
258 |> Enum.reverse()
259 }
260
261 json(conn, result)
262 end
263 end
264
265 def post_status(%{assigns: %{user: user}} = conn, %{"status" => _} = params) do
266 params =
267 params
268 |> Map.put("in_reply_to_status_id", params["in_reply_to_id"])
269 |> Map.put("no_attachment_links", true)
270
271 idempotency_key =
272 case get_req_header(conn, "idempotency-key") do
273 [key] -> key
274 _ -> Ecto.UUID.generate()
275 end
276
277 {:ok, activity} =
278 Cachex.get!(
279 :idempotency_cache,
280 idempotency_key,
281 fallback: fn _ -> CommonAPI.post(user, params) end
282 )
283
284 render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
285 end
286
287 def delete_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
288 with {:ok, %Activity{}} <- CommonAPI.delete(id, user) do
289 json(conn, %{})
290 else
291 _e ->
292 conn
293 |> put_status(403)
294 |> json(%{error: "Can't delete this post"})
295 end
296 end
297
298 def reblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
299 with {:ok, announce, _activity} = CommonAPI.repeat(ap_id_or_id, user) do
300 render(conn, StatusView, "status.json", %{activity: announce, for: user, as: :activity})
301 end
302 end
303
304 def unreblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
305 with {:ok, _, _, %{data: %{"id" => id}}} = CommonAPI.unrepeat(ap_id_or_id, user),
306 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
307 render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
308 end
309 end
310
311 def fav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
312 with {:ok, _fav, %{data: %{"id" => id}}} = CommonAPI.favorite(ap_id_or_id, user),
313 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
314 render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
315 end
316 end
317
318 def unfav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
319 with {:ok, _, _, %{data: %{"id" => id}}} = CommonAPI.unfavorite(ap_id_or_id, user),
320 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
321 render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
322 end
323 end
324
325 def notifications(%{assigns: %{user: user}} = conn, params) do
326 notifications = Notification.for_user(user, params)
327
328 result =
329 Enum.map(notifications, fn x ->
330 render_notification(user, x)
331 end)
332 |> Enum.filter(& &1)
333
334 conn
335 |> add_link_headers(:notifications, notifications)
336 |> json(result)
337 end
338
339 def get_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
340 with {:ok, notification} <- Notification.get(user, id) do
341 json(conn, render_notification(user, notification))
342 else
343 {:error, reason} ->
344 conn
345 |> put_resp_content_type("application/json")
346 |> send_resp(403, Jason.encode!(%{"error" => reason}))
347 end
348 end
349
350 def clear_notifications(%{assigns: %{user: user}} = conn, _params) do
351 Notification.clear(user)
352 json(conn, %{})
353 end
354
355 def dismiss_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
356 with {:ok, _notif} <- Notification.dismiss(user, id) do
357 json(conn, %{})
358 else
359 {:error, reason} ->
360 conn
361 |> put_resp_content_type("application/json")
362 |> send_resp(403, Jason.encode!(%{"error" => reason}))
363 end
364 end
365
366 def relationships(%{assigns: %{user: user}} = conn, %{"id" => id}) do
367 id = List.wrap(id)
368 q = from(u in User, where: u.id in ^id)
369 targets = Repo.all(q)
370 render(conn, AccountView, "relationships.json", %{user: user, targets: targets})
371 end
372
373 def upload(%{assigns: %{user: _}} = conn, %{"file" => file}) do
374 with {:ok, object} <- ActivityPub.upload(file) do
375 data =
376 object.data
377 |> Map.put("id", object.id)
378
379 render(conn, StatusView, "attachment.json", %{attachment: data})
380 end
381 end
382
383 def favourited_by(conn, %{"id" => id}) do
384 with %Activity{data: %{"object" => %{"likes" => likes}}} <- Repo.get(Activity, id) do
385 q = from(u in User, where: u.ap_id in ^likes)
386 users = Repo.all(q)
387 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
388 else
389 _ -> json(conn, [])
390 end
391 end
392
393 def reblogged_by(conn, %{"id" => id}) do
394 with %Activity{data: %{"object" => %{"announcements" => announces}}} <- Repo.get(Activity, id) do
395 q = from(u in User, where: u.ap_id in ^announces)
396 users = Repo.all(q)
397 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
398 else
399 _ -> json(conn, [])
400 end
401 end
402
403 def hashtag_timeline(%{assigns: %{user: user}} = conn, params) do
404 params =
405 params
406 |> Map.put("type", "Create")
407 |> Map.put("local_only", !!params["local"])
408 |> Map.put("blocking_user", user)
409
410 activities =
411 ActivityPub.fetch_public_activities(params)
412 |> Enum.reverse()
413
414 conn
415 |> add_link_headers(:hashtag_timeline, activities, params["tag"])
416 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
417 end
418
419 # TODO: Pagination
420 def followers(conn, %{"id" => id}) do
421 with %User{} = user <- Repo.get(User, id),
422 {:ok, followers} <- User.get_followers(user) do
423 render(conn, AccountView, "accounts.json", %{users: followers, as: :user})
424 end
425 end
426
427 def following(conn, %{"id" => id}) do
428 with %User{} = user <- Repo.get(User, id),
429 {:ok, followers} <- User.get_friends(user) do
430 render(conn, AccountView, "accounts.json", %{users: followers, as: :user})
431 end
432 end
433
434 def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
435 with %User{} = followed <- Repo.get(User, id),
436 {:ok, follower} <- User.follow(follower, followed),
437 {:ok, _activity} <- ActivityPub.follow(follower, followed) do
438 render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
439 else
440 {:error, message} ->
441 conn
442 |> put_resp_content_type("application/json")
443 |> send_resp(403, Jason.encode!(%{"error" => message}))
444 end
445 end
446
447 def follow(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do
448 with %User{} = followed <- Repo.get_by(User, nickname: uri),
449 {:ok, follower} <- User.follow(follower, followed),
450 {:ok, _activity} <- ActivityPub.follow(follower, followed) do
451 render(conn, AccountView, "account.json", %{user: followed})
452 else
453 {:error, message} ->
454 conn
455 |> put_resp_content_type("application/json")
456 |> send_resp(403, Jason.encode!(%{"error" => message}))
457 end
458 end
459
460 # TODO: Clean up and unify
461 def unfollow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
462 with %User{} = followed <- Repo.get(User, id),
463 {:ok, follower, follow_activity} <- User.unfollow(follower, followed),
464 {:ok, _activity} <-
465 ActivityPub.insert(%{
466 "type" => "Undo",
467 "actor" => follower.ap_id,
468 # get latest Follow for these users
469 "object" => follow_activity.data["id"]
470 }) do
471 render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
472 end
473 end
474
475 def block(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
476 with %User{} = blocked <- Repo.get(User, id),
477 {:ok, blocker} <- User.block(blocker, blocked) do
478 render(conn, AccountView, "relationship.json", %{user: blocker, target: blocked})
479 else
480 {:error, message} ->
481 conn
482 |> put_resp_content_type("application/json")
483 |> send_resp(403, Jason.encode!(%{"error" => message}))
484 end
485 end
486
487 def unblock(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
488 with %User{} = blocked <- Repo.get(User, id),
489 {:ok, blocker} <- User.unblock(blocker, blocked) do
490 render(conn, AccountView, "relationship.json", %{user: blocker, target: blocked})
491 else
492 {:error, message} ->
493 conn
494 |> put_resp_content_type("application/json")
495 |> send_resp(403, Jason.encode!(%{"error" => message}))
496 end
497 end
498
499 # TODO: Use proper query
500 def blocks(%{assigns: %{user: user}} = conn, _) do
501 with blocked_users <- user.info["blocks"] || [],
502 accounts <- Enum.map(blocked_users, fn ap_id -> User.get_cached_by_ap_id(ap_id) end) do
503 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
504 json(conn, res)
505 end
506 end
507
508 def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
509 accounts = User.search(query, params["resolve"] == "true")
510
511 fetched =
512 if Regex.match?(~r/https?:/, query) do
513 with {:ok, activities} <- OStatus.fetch_activity_from_url(query) do
514 activities
515 |> Enum.filter(fn
516 %{data: %{"type" => "Create"}} -> true
517 _ -> false
518 end)
519 else
520 _e -> []
521 end
522 end || []
523
524 q =
525 from(
526 a in Activity,
527 where: fragment("?->>'type' = 'Create'", a.data),
528 where: "https://www.w3.org/ns/activitystreams#Public" in a.recipients,
529 where:
530 fragment(
531 "to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)",
532 a.data,
533 ^query
534 ),
535 limit: 20,
536 order_by: [desc: :id]
537 )
538
539 statuses = Repo.all(q) ++ fetched
540
541 tags =
542 String.split(query)
543 |> Enum.uniq()
544 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
545 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
546
547 res = %{
548 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
549 "statuses" =>
550 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
551 "hashtags" => tags
552 }
553
554 json(conn, res)
555 end
556
557 def account_search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
558 accounts = User.search(query, params["resolve"] == "true")
559
560 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
561
562 json(conn, res)
563 end
564
565 def favourites(%{assigns: %{user: user}} = conn, _) do
566 params =
567 %{}
568 |> Map.put("type", "Create")
569 |> Map.put("favorited_by", user.ap_id)
570 |> Map.put("blocking_user", user)
571
572 activities =
573 ActivityPub.fetch_public_activities(params)
574 |> Enum.reverse()
575
576 conn
577 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
578 end
579
580 def index(%{assigns: %{user: user}} = conn, _params) do
581 token =
582 conn
583 |> get_session(:oauth_token)
584
585 if user && token do
586 mastodon_emoji = mastodonized_emoji()
587 accounts = Map.put(%{}, user.id, AccountView.render("account.json", %{user: user}))
588
589 initial_state =
590 %{
591 meta: %{
592 streaming_api_base_url:
593 String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws"),
594 access_token: token,
595 locale: "en",
596 domain: Pleroma.Web.Endpoint.host(),
597 admin: "1",
598 me: "#{user.id}",
599 unfollow_modal: false,
600 boost_modal: false,
601 delete_modal: true,
602 auto_play_gif: false,
603 reduce_motion: false
604 },
605 compose: %{
606 me: "#{user.id}",
607 default_privacy: "public",
608 default_sensitive: false
609 },
610 media_attachments: %{
611 accept_content_types: [
612 ".jpg",
613 ".jpeg",
614 ".png",
615 ".gif",
616 ".webm",
617 ".mp4",
618 ".m4v",
619 "image\/jpeg",
620 "image\/png",
621 "image\/gif",
622 "video\/webm",
623 "video\/mp4"
624 ]
625 },
626 settings:
627 Map.get(user.info, "settings") ||
628 %{
629 onboarded: true,
630 home: %{
631 shows: %{
632 reblog: true,
633 reply: true
634 }
635 },
636 notifications: %{
637 alerts: %{
638 follow: true,
639 favourite: true,
640 reblog: true,
641 mention: true
642 },
643 shows: %{
644 follow: true,
645 favourite: true,
646 reblog: true,
647 mention: true
648 },
649 sounds: %{
650 follow: true,
651 favourite: true,
652 reblog: true,
653 mention: true
654 }
655 }
656 },
657 push_subscription: nil,
658 accounts: accounts,
659 custom_emojis: mastodon_emoji,
660 char_limit: Keyword.get(@instance, :limit)
661 }
662 |> Jason.encode!()
663
664 conn
665 |> put_layout(false)
666 |> render(MastodonView, "index.html", %{initial_state: initial_state})
667 else
668 conn
669 |> redirect(to: "/web/login")
670 end
671 end
672
673 def put_settings(%{assigns: %{user: user}} = conn, %{"data" => settings} = _params) do
674 with new_info <- Map.put(user.info, "settings", settings),
675 change <- User.info_changeset(user, %{info: new_info}),
676 {:ok, _user} <- User.update_and_set_cache(change) do
677 conn
678 |> json(%{})
679 else
680 e ->
681 conn
682 |> json(%{error: inspect(e)})
683 end
684 end
685
686 def login(conn, _) do
687 conn
688 |> render(MastodonView, "login.html", %{error: false})
689 end
690
691 defp get_or_make_app() do
692 with %App{} = app <- Repo.get_by(App, client_name: "Mastodon-Local") do
693 {:ok, app}
694 else
695 _e ->
696 cs =
697 App.register_changeset(%App{}, %{
698 client_name: "Mastodon-Local",
699 redirect_uris: ".",
700 scopes: "read,write,follow"
701 })
702
703 Repo.insert(cs)
704 end
705 end
706
707 def login_post(conn, %{"authorization" => %{"name" => name, "password" => password}}) do
708 with %User{} = user <- User.get_by_nickname_or_email(name),
709 true <- Pbkdf2.checkpw(password, user.password_hash),
710 {:ok, app} <- get_or_make_app(),
711 {:ok, auth} <- Authorization.create_authorization(app, user),
712 {:ok, token} <- Token.exchange_token(app, auth) do
713 conn
714 |> put_session(:oauth_token, token.token)
715 |> redirect(to: "/web/getting-started")
716 else
717 _e ->
718 conn
719 |> render(MastodonView, "login.html", %{error: "Wrong username or password"})
720 end
721 end
722
723 def logout(conn, _) do
724 conn
725 |> clear_session
726 |> redirect(to: "/")
727 end
728
729 def relationship_noop(%{assigns: %{user: user}} = conn, %{"id" => id}) do
730 Logger.debug("Unimplemented, returning unmodified relationship")
731
732 with %User{} = target <- Repo.get(User, id) do
733 render(conn, AccountView, "relationship.json", %{user: user, target: target})
734 end
735 end
736
737 def empty_array(conn, _) do
738 Logger.debug("Unimplemented, returning an empty array")
739 json(conn, [])
740 end
741
742 def empty_object(conn, _) do
743 Logger.debug("Unimplemented, returning an empty object")
744 json(conn, %{})
745 end
746
747 def render_notification(user, %{id: id, activity: activity, inserted_at: created_at} = _params) do
748 actor = User.get_cached_by_ap_id(activity.data["actor"])
749
750 created_at =
751 NaiveDateTime.to_iso8601(created_at)
752 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
753
754 case activity.data["type"] do
755 "Create" ->
756 %{
757 id: id,
758 type: "mention",
759 created_at: created_at,
760 account: AccountView.render("account.json", %{user: actor}),
761 status: StatusView.render("status.json", %{activity: activity, for: user})
762 }
763
764 "Like" ->
765 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
766
767 %{
768 id: id,
769 type: "favourite",
770 created_at: created_at,
771 account: AccountView.render("account.json", %{user: actor}),
772 status: StatusView.render("status.json", %{activity: liked_activity, for: user})
773 }
774
775 "Announce" ->
776 announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
777
778 %{
779 id: id,
780 type: "reblog",
781 created_at: created_at,
782 account: AccountView.render("account.json", %{user: actor}),
783 status: StatusView.render("status.json", %{activity: announced_activity, for: user})
784 }
785
786 "Follow" ->
787 %{
788 id: id,
789 type: "follow",
790 created_at: created_at,
791 account: AccountView.render("account.json", %{user: actor})
792 }
793
794 _ ->
795 nil
796 end
797 end
798 end