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