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