1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
6 use Pleroma.Web, :controller
7 alias Pleroma.{Repo, Object, Activity, User, Notification, Stats}
10 alias Pleroma.Web.MastodonAPI.{
19 alias Pleroma.Web.ActivityPub.ActivityPub
20 alias Pleroma.Web.ActivityPub.Utils
21 alias Pleroma.Web.CommonAPI
22 alias Pleroma.Web.OAuth.{Authorization, Token, App}
23 alias Pleroma.Web.MediaProxy
28 @httpoison Application.get_env(:pleroma, :httpoison)
30 action_fallback(:errors)
32 def create_app(conn, params) do
33 with cs <- App.register_changeset(%App{}, params) |> IO.inspect(),
34 {:ok, app} <- Repo.insert(cs) |> IO.inspect() do
36 id: app.id |> to_string,
37 name: app.client_name,
38 client_id: app.client_id,
39 client_secret: app.client_secret,
40 redirect_uri: app.redirect_uris,
53 value_function \\ fn x -> {:ok, x} end
55 if Map.has_key?(params, params_field) do
56 case value_function.(params[params_field]) do
57 {:ok, new_value} -> Map.put(map, map_field, new_value)
65 def update_credentials(%{assigns: %{user: user}} = conn, params) do
70 |> add_if_present(params, "display_name", :name)
71 |> add_if_present(params, "note", :bio, fn value -> {:ok, User.parse_bio(value)} end)
72 |> add_if_present(params, "avatar", :avatar, fn value ->
73 with %Plug.Upload{} <- value,
74 {:ok, object} <- ActivityPub.upload(value, type: :avatar) do
83 |> add_if_present(params, "locked", :locked, fn value -> {:ok, value == "true"} end)
84 |> add_if_present(params, "header", :banner, fn value ->
85 with %Plug.Upload{} <- value,
86 {:ok, object} <- ActivityPub.upload(value, type: :banner) do
93 info_cng = User.Info.mastodon_profile_update(user.info, info_params)
95 with changeset <- User.update_changeset(user, user_params),
96 changeset <- Ecto.Changeset.put_embed(changeset, :info, info_cng),
97 {:ok, user} <- User.update_and_set_cache(changeset) do
98 if original_user != user do
99 CommonAPI.update(user)
102 json(conn, AccountView.render("account.json", %{user: user, for: user}))
107 |> json(%{error: "Invalid request"})
111 def verify_credentials(%{assigns: %{user: user}} = conn, _) do
112 account = AccountView.render("account.json", %{user: user, for: user})
116 def user(%{assigns: %{user: for_user}} = conn, %{"id" => id}) do
117 with %User{} = user <- Repo.get(User, id),
118 true <- User.auth_active?(user) || user.id == for_user.id || User.superuser?(for_user) do
119 account = AccountView.render("account.json", %{user: user, for: for_user})
125 |> json(%{error: "Can't find user"})
129 @mastodon_api_level "2.5.0"
131 def masto_instance(conn, _params) do
132 instance = Pleroma.Config.get(:instance)
136 title: Keyword.get(instance, :name),
137 description: Keyword.get(instance, :description),
138 version: "#{@mastodon_api_level} (compatible; #{Pleroma.Application.named_version()})",
139 email: Keyword.get(instance, :email),
141 streaming_api: String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws")
143 stats: Stats.get_stats(),
144 thumbnail: Web.base_url() <> "/instance/thumbnail.jpeg",
145 max_toot_chars: Keyword.get(instance, :limit)
151 def peers(conn, _params) do
152 json(conn, Stats.get_peers())
155 defp mastodonized_emoji do
156 Pleroma.Emoji.get_all()
157 |> Enum.map(fn {shortcode, relative_url} ->
158 url = to_string(URI.merge(Web.base_url(), relative_url))
161 "shortcode" => shortcode,
163 "visible_in_picker" => true,
169 def custom_emojis(conn, _params) do
170 mastodon_emoji = mastodonized_emoji()
171 json(conn, mastodon_emoji)
174 defp add_link_headers(conn, method, activities, param \\ nil, params \\ %{}) do
175 last = List.last(activities)
176 first = List.first(activities)
182 {next_url, prev_url} =
186 Pleroma.Web.Endpoint,
189 Map.merge(params, %{max_id: min})
192 Pleroma.Web.Endpoint,
195 Map.merge(params, %{since_id: max})
201 Pleroma.Web.Endpoint,
203 Map.merge(params, %{max_id: min})
206 Pleroma.Web.Endpoint,
208 Map.merge(params, %{since_id: max})
214 |> put_resp_header("link", "<#{next_url}>; rel=\"next\", <#{prev_url}>; rel=\"prev\"")
220 def home_timeline(%{assigns: %{user: user}} = conn, params) do
223 |> Map.put("type", ["Create", "Announce"])
224 |> Map.put("blocking_user", user)
225 |> Map.put("user", user)
228 ActivityPub.fetch_activities([user.ap_id | user.following], params)
229 |> ActivityPub.contain_timeline(user)
233 |> add_link_headers(:home_timeline, activities)
234 |> put_view(StatusView)
235 |> render("index.json", %{activities: activities, for: user, as: :activity})
238 def public_timeline(%{assigns: %{user: user}} = conn, params) do
239 local_only = params["local"] in [true, "True", "true", "1"]
243 |> Map.put("type", ["Create", "Announce"])
244 |> Map.put("local_only", local_only)
245 |> Map.put("blocking_user", user)
248 ActivityPub.fetch_public_activities(params)
252 |> add_link_headers(:public_timeline, activities, false, %{"local" => local_only})
253 |> put_view(StatusView)
254 |> render("index.json", %{activities: activities, for: user, as: :activity})
257 def user_statuses(%{assigns: %{user: reading_user}} = conn, params) do
258 with %User{} = user <- Repo.get(User, params["id"]) do
259 activities = ActivityPub.fetch_user_activities(user, reading_user, params)
262 |> add_link_headers(:user_statuses, activities, params["id"])
263 |> put_view(StatusView)
264 |> render("index.json", %{
265 activities: activities,
272 def dm_timeline(%{assigns: %{user: user}} = conn, params) do
274 ActivityPub.fetch_activities_query(
276 Map.merge(params, %{"type" => "Create", visibility: "direct"})
279 activities = Repo.all(query)
282 |> add_link_headers(:dm_timeline, activities)
283 |> put_view(StatusView)
284 |> render("index.json", %{activities: activities, for: user, as: :activity})
287 def get_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
288 with %Activity{} = activity <- Repo.get(Activity, id),
289 true <- ActivityPub.visible_for_user?(activity, user) do
291 |> put_view(StatusView)
292 |> try_render("status.json", %{activity: activity, for: user})
296 def get_context(%{assigns: %{user: user}} = conn, %{"id" => id}) do
297 with %Activity{} = activity <- Repo.get(Activity, id),
299 ActivityPub.fetch_activities_for_context(activity.data["context"], %{
300 "blocking_user" => user,
304 activities |> Enum.filter(fn %{id: aid} -> to_string(aid) != to_string(id) end),
306 activities |> Enum.filter(fn %{data: %{"type" => type}} -> type == "Create" end),
307 grouped_activities <- Enum.group_by(activities, fn %{id: id} -> id < activity.id end) do
313 activities: grouped_activities[true] || [],
321 activities: grouped_activities[false] || [],
331 def post_status(conn, %{"status" => "", "media_ids" => media_ids} = params)
332 when length(media_ids) > 0 do
335 |> Map.put("status", ".")
337 post_status(conn, params)
340 def post_status(%{assigns: %{user: user}} = conn, %{"status" => _} = params) do
343 |> Map.put("in_reply_to_status_id", params["in_reply_to_id"])
344 |> Map.put("no_attachment_links", true)
347 case get_req_header(conn, "idempotency-key") do
349 _ -> Ecto.UUID.generate()
353 Cachex.fetch!(:idempotency_cache, idempotency_key, fn _ -> CommonAPI.post(user, params) end)
356 |> put_view(StatusView)
357 |> try_render("status.json", %{activity: activity, for: user, as: :activity})
360 def delete_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
361 with {:ok, %Activity{}} <- CommonAPI.delete(id, user) do
367 |> json(%{error: "Can't delete this post"})
371 def reblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
372 with {:ok, announce, _activity} <- CommonAPI.repeat(ap_id_or_id, user) do
374 |> put_view(StatusView)
375 |> try_render("status.json", %{activity: announce, for: user, as: :activity})
379 def unreblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
380 with {:ok, _unannounce, %{data: %{"id" => id}}} <- CommonAPI.unrepeat(ap_id_or_id, user),
381 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
383 |> put_view(StatusView)
384 |> try_render("status.json", %{activity: activity, for: user, as: :activity})
388 def fav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
389 with {:ok, _fav, %{data: %{"id" => id}}} <- CommonAPI.favorite(ap_id_or_id, user),
390 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
392 |> put_view(StatusView)
393 |> try_render("status.json", %{activity: activity, for: user, as: :activity})
397 def unfav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
398 with {:ok, _, _, %{data: %{"id" => id}}} <- CommonAPI.unfavorite(ap_id_or_id, user),
399 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
401 |> put_view(StatusView)
402 |> try_render("status.json", %{activity: activity, for: user, as: :activity})
406 def pin_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
407 with {:ok, activity} <- CommonAPI.pin(ap_id_or_id, user) do
409 |> put_view(StatusView)
410 |> try_render("status.json", %{activity: activity, for: user, as: :activity})
414 |> put_resp_content_type("application/json")
415 |> send_resp(:bad_request, Jason.encode!(%{"error" => reason}))
419 def unpin_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
420 with {:ok, activity} <- CommonAPI.unpin(ap_id_or_id, user) do
422 |> put_view(StatusView)
423 |> try_render("status.json", %{activity: activity, for: user, as: :activity})
427 def notifications(%{assigns: %{user: user}} = conn, params) do
428 notifications = Notification.for_user(user, params)
431 Enum.map(notifications, fn x ->
432 render_notification(user, x)
437 |> add_link_headers(:notifications, notifications)
441 def get_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
442 with {:ok, notification} <- Notification.get(user, id) do
443 json(conn, render_notification(user, notification))
447 |> put_resp_content_type("application/json")
448 |> send_resp(403, Jason.encode!(%{"error" => reason}))
452 def clear_notifications(%{assigns: %{user: user}} = conn, _params) do
453 Notification.clear(user)
457 def dismiss_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
458 with {:ok, _notif} <- Notification.dismiss(user, id) do
463 |> put_resp_content_type("application/json")
464 |> send_resp(403, Jason.encode!(%{"error" => reason}))
468 def relationships(%{assigns: %{user: user}} = conn, %{"id" => id}) do
470 q = from(u in User, where: u.id in ^id)
471 targets = Repo.all(q)
474 |> put_view(AccountView)
475 |> render("relationships.json", %{user: user, targets: targets})
478 # Instead of returning a 400 when no "id" params is present, Mastodon returns an empty array.
479 def relationships(%{assigns: %{user: _user}} = conn, _), do: json(conn, [])
481 def update_media(%{assigns: %{user: user}} = conn, data) do
482 with %Object{} = object <- Repo.get(Object, data["id"]),
483 true <- Object.authorize_mutation(object, user),
484 true <- is_binary(data["description"]),
485 description <- data["description"] do
486 new_data = %{object.data | "name" => description}
490 |> Object.change(%{data: new_data})
493 attachment_data = Map.put(new_data, "id", object.id)
496 |> put_view(StatusView)
497 |> render("attachment.json", %{attachment: attachment_data})
501 def upload(%{assigns: %{user: user}} = conn, %{"file" => file} = data) do
502 with {:ok, object} <-
503 ActivityPub.upload(file,
504 actor: User.ap_id(user),
505 description: Map.get(data, "description")
507 attachment_data = Map.put(object.data, "id", object.id)
510 |> put_view(StatusView)
511 |> render("attachment.json", %{attachment: attachment_data})
515 def favourited_by(conn, %{"id" => id}) do
516 with %Activity{data: %{"object" => %{"likes" => likes}}} <- Repo.get(Activity, id) do
517 q = from(u in User, where: u.ap_id in ^likes)
521 |> put_view(AccountView)
522 |> render(AccountView, "accounts.json", %{users: users, as: :user})
528 def reblogged_by(conn, %{"id" => id}) do
529 with %Activity{data: %{"object" => %{"announcements" => announces}}} <- Repo.get(Activity, id) do
530 q = from(u in User, where: u.ap_id in ^announces)
534 |> put_view(AccountView)
535 |> render("accounts.json", %{users: users, as: :user})
541 def hashtag_timeline(%{assigns: %{user: user}} = conn, params) do
542 local_only = params["local"] in [true, "True", "true", "1"]
546 |> Map.put("type", "Create")
547 |> Map.put("local_only", local_only)
548 |> Map.put("blocking_user", user)
549 |> Map.put("tag", String.downcase(params["tag"]))
552 ActivityPub.fetch_public_activities(params)
556 |> add_link_headers(:hashtag_timeline, activities, params["tag"], %{"local" => local_only})
557 |> put_view(StatusView)
558 |> render("index.json", %{activities: activities, for: user, as: :activity})
561 def followers(%{assigns: %{user: for_user}} = conn, %{"id" => id}) do
562 with %User{} = user <- Repo.get(User, id),
563 {:ok, followers} <- User.get_followers(user) do
566 for_user && user.id == for_user.id -> followers
567 user.info.hide_network -> []
572 |> put_view(AccountView)
573 |> render("accounts.json", %{users: followers, as: :user})
577 def following(%{assigns: %{user: for_user}} = conn, %{"id" => id}) do
578 with %User{} = user <- Repo.get(User, id),
579 {:ok, followers} <- User.get_friends(user) do
582 for_user && user.id == for_user.id -> followers
583 user.info.hide_network -> []
588 |> put_view(AccountView)
589 |> render("accounts.json", %{users: followers, as: :user})
593 def follow_requests(%{assigns: %{user: followed}} = conn, _params) do
594 with {:ok, follow_requests} <- User.get_follow_requests(followed) do
596 |> put_view(AccountView)
597 |> render("accounts.json", %{users: follow_requests, as: :user})
601 def authorize_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
602 with %User{} = follower <- Repo.get(User, id),
603 {:ok, follower} <- User.maybe_follow(follower, followed),
604 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
605 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "accept"),
607 ActivityPub.accept(%{
608 to: [follower.ap_id],
609 actor: followed.ap_id,
610 object: follow_activity.data["id"],
614 |> put_view(AccountView)
615 |> render("relationship.json", %{user: followed, target: follower})
619 |> put_resp_content_type("application/json")
620 |> send_resp(403, Jason.encode!(%{"error" => message}))
624 def reject_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
625 with %User{} = follower <- Repo.get(User, id),
626 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
627 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "reject"),
629 ActivityPub.reject(%{
630 to: [follower.ap_id],
631 actor: followed.ap_id,
632 object: follow_activity.data["id"],
636 |> put_view(AccountView)
637 |> render("relationship.json", %{user: followed, target: follower})
641 |> put_resp_content_type("application/json")
642 |> send_resp(403, Jason.encode!(%{"error" => message}))
646 def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
647 with %User{} = followed <- Repo.get(User, id),
648 {:ok, follower} <- User.maybe_direct_follow(follower, followed),
649 {:ok, _activity} <- ActivityPub.follow(follower, followed),
650 {:ok, follower, followed} <-
651 User.wait_and_refresh(
652 Pleroma.Config.get([:activitypub, :follow_handshake_timeout]),
657 |> put_view(AccountView)
658 |> render("relationship.json", %{user: follower, target: followed})
662 |> put_resp_content_type("application/json")
663 |> send_resp(403, Jason.encode!(%{"error" => message}))
667 def follow(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do
668 with %User{} = followed <- Repo.get_by(User, nickname: uri),
669 {:ok, follower} <- User.maybe_direct_follow(follower, followed),
670 {:ok, _activity} <- ActivityPub.follow(follower, followed) do
672 |> put_view(AccountView)
673 |> render("account.json", %{user: followed, for: follower})
677 |> put_resp_content_type("application/json")
678 |> send_resp(403, Jason.encode!(%{"error" => message}))
682 def unfollow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
683 with %User{} = followed <- Repo.get(User, id),
684 {:ok, _activity} <- ActivityPub.unfollow(follower, followed),
685 {:ok, follower, _} <- User.unfollow(follower, followed) do
687 |> put_view(AccountView)
688 |> render("relationship.json", %{user: follower, target: followed})
692 def block(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
693 with %User{} = blocked <- Repo.get(User, id),
694 {:ok, blocker} <- User.block(blocker, blocked),
695 {:ok, _activity} <- ActivityPub.block(blocker, blocked) do
697 |> put_view(AccountView)
698 |> render("relationship.json", %{user: blocker, target: blocked})
702 |> put_resp_content_type("application/json")
703 |> send_resp(403, Jason.encode!(%{"error" => message}))
707 def unblock(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
708 with %User{} = blocked <- Repo.get(User, id),
709 {:ok, blocker} <- User.unblock(blocker, blocked),
710 {:ok, _activity} <- ActivityPub.unblock(blocker, blocked) do
712 |> put_view(AccountView)
713 |> render("relationship.json", %{user: blocker, target: blocked})
717 |> put_resp_content_type("application/json")
718 |> send_resp(403, Jason.encode!(%{"error" => message}))
722 def blocks(%{assigns: %{user: user}} = conn, _) do
723 with blocked_accounts <- User.blocked_users(user) do
724 res = AccountView.render("accounts.json", users: blocked_accounts, for: user, as: :user)
729 def domain_blocks(%{assigns: %{user: %{info: info}}} = conn, _) do
730 json(conn, info.domain_blocks || [])
733 def block_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
734 User.block_domain(blocker, domain)
738 def unblock_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
739 User.unblock_domain(blocker, domain)
743 def status_search(user, query) do
745 if Regex.match?(~r/https?:/, query) do
746 with {:ok, object} <- ActivityPub.fetch_object_from_id(query),
747 %Activity{} = activity <-
748 Activity.get_create_activity_by_object_ap_id(object.data["id"]),
749 true <- ActivityPub.visible_for_user?(activity, user) do
759 where: fragment("?->>'type' = 'Create'", a.data),
760 where: "https://www.w3.org/ns/activitystreams#Public" in a.recipients,
763 "to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)",
768 order_by: [desc: :id]
771 Repo.all(q) ++ fetched
774 def search2(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
775 accounts = User.search(query, params["resolve"] == "true")
777 statuses = status_search(user, query)
779 tags_path = Web.base_url() <> "/tag/"
784 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
785 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
786 |> Enum.map(fn tag -> %{name: tag, url: tags_path <> tag} end)
789 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
791 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
798 def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
799 accounts = User.search(query, params["resolve"] == "true")
801 statuses = status_search(user, query)
806 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
807 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
810 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
812 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
819 def account_search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
820 accounts = User.search(query, params["resolve"] == "true")
822 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
827 def favourites(%{assigns: %{user: user}} = conn, _) do
830 |> Map.put("type", "Create")
831 |> Map.put("favorited_by", user.ap_id)
832 |> Map.put("blocking_user", user)
835 ActivityPub.fetch_public_activities(params)
839 |> put_view(StatusView)
840 |> render("index.json", %{activities: activities, for: user, as: :activity})
843 def get_lists(%{assigns: %{user: user}} = conn, opts) do
844 lists = Pleroma.List.for_user(user, opts)
845 res = ListView.render("lists.json", lists: lists)
849 def get_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
850 with %Pleroma.List{} = list <- Pleroma.List.get(id, user) do
851 res = ListView.render("list.json", list: list)
854 _e -> json(conn, "error")
858 def account_lists(%{assigns: %{user: user}} = conn, %{"id" => account_id}) do
859 lists = Pleroma.List.get_lists_account_belongs(user, account_id)
860 res = ListView.render("lists.json", lists: lists)
864 def delete_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
865 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
866 {:ok, _list} <- Pleroma.List.delete(list) do
874 def create_list(%{assigns: %{user: user}} = conn, %{"title" => title}) do
875 with {:ok, %Pleroma.List{} = list} <- Pleroma.List.create(title, user) do
876 res = ListView.render("list.json", list: list)
881 def add_to_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
883 |> Enum.each(fn account_id ->
884 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
885 %User{} = followed <- Repo.get(User, account_id) do
886 Pleroma.List.follow(list, followed)
893 def remove_from_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
895 |> Enum.each(fn account_id ->
896 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
897 %User{} = followed <- Repo.get(Pleroma.User, account_id) do
898 Pleroma.List.unfollow(list, followed)
905 def list_accounts(%{assigns: %{user: user}} = conn, %{"id" => id}) do
906 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
907 {:ok, users} = Pleroma.List.get_following(list) do
909 |> put_view(AccountView)
910 |> render("accounts.json", %{users: users, as: :user})
914 def rename_list(%{assigns: %{user: user}} = conn, %{"id" => id, "title" => title}) do
915 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
916 {:ok, list} <- Pleroma.List.rename(list, title) do
917 res = ListView.render("list.json", list: list)
925 def list_timeline(%{assigns: %{user: user}} = conn, %{"list_id" => id} = params) do
926 with %Pleroma.List{title: _title, following: following} <- Pleroma.List.get(id, user) do
929 |> Map.put("type", "Create")
930 |> Map.put("blocking_user", user)
932 # we must filter the following list for the user to avoid leaking statuses the user
933 # does not actually have permission to see (for more info, peruse security issue #270).
936 |> Enum.filter(fn x -> x in user.following end)
939 ActivityPub.fetch_activities_bounded(following_to, following, params)
943 |> put_view(StatusView)
944 |> render("index.json", %{activities: activities, for: user, as: :activity})
949 |> json(%{error: "Error."})
953 def index(%{assigns: %{user: user}} = conn, _params) do
956 |> get_session(:oauth_token)
959 mastodon_emoji = mastodonized_emoji()
961 limit = Pleroma.Config.get([:instance, :limit])
964 Map.put(%{}, user.id, AccountView.render("account.json", %{user: user, for: user}))
969 streaming_api_base_url:
970 String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws"),
973 domain: Pleroma.Web.Endpoint.host(),
976 unfollow_modal: false,
979 auto_play_gif: false,
980 display_sensitive_media: false,
981 reduce_motion: false,
982 max_toot_chars: limit
985 delete_others_notice: !!user.info.is_moderator,
986 admin: !!user.info.is_admin
990 default_privacy: user.info.default_scope,
991 default_sensitive: false
993 media_attachments: %{
994 accept_content_types: [
1010 user.info.settings ||
1040 push_subscription: nil,
1042 custom_emojis: mastodon_emoji,
1048 |> put_layout(false)
1049 |> put_view(MastodonView)
1050 |> render("index.html", %{initial_state: initial_state})
1053 |> redirect(to: "/web/login")
1057 def put_settings(%{assigns: %{user: user}} = conn, %{"data" => settings} = _params) do
1058 info_cng = User.Info.mastodon_settings_update(user.info, settings)
1060 with changeset <- Ecto.Changeset.change(user),
1061 changeset <- Ecto.Changeset.put_embed(changeset, :info, info_cng),
1062 {:ok, _user} <- User.update_and_set_cache(changeset) do
1067 |> put_resp_content_type("application/json")
1068 |> send_resp(500, Jason.encode!(%{"error" => inspect(e)}))
1072 def login(conn, %{"code" => code}) do
1073 with {:ok, app} <- get_or_make_app(),
1074 %Authorization{} = auth <- Repo.get_by(Authorization, token: code, app_id: app.id),
1075 {:ok, token} <- Token.exchange_token(app, auth) do
1077 |> put_session(:oauth_token, token.token)
1078 |> redirect(to: "/web/getting-started")
1082 def login(conn, _) do
1083 with {:ok, app} <- get_or_make_app() do
1085 o_auth_path(conn, :authorize,
1086 response_type: "code",
1087 client_id: app.client_id,
1093 |> redirect(to: path)
1097 defp get_or_make_app() do
1098 with %App{} = app <- Repo.get_by(App, client_name: "Mastodon-Local") do
1103 App.register_changeset(%App{}, %{
1104 client_name: "Mastodon-Local",
1106 scopes: "read,write,follow"
1113 def logout(conn, _) do
1116 |> redirect(to: "/")
1119 def relationship_noop(%{assigns: %{user: user}} = conn, %{"id" => id}) do
1120 Logger.debug("Unimplemented, returning unmodified relationship")
1122 with %User{} = target <- Repo.get(User, id) do
1124 |> put_view(AccountView)
1125 |> render("relationship.json", %{user: user, target: target})
1129 def empty_array(conn, _) do
1130 Logger.debug("Unimplemented, returning an empty array")
1134 def empty_object(conn, _) do
1135 Logger.debug("Unimplemented, returning an empty object")
1139 def render_notification(user, %{id: id, activity: activity, inserted_at: created_at} = _params) do
1140 actor = User.get_cached_by_ap_id(activity.data["actor"])
1141 parent_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
1142 mastodon_type = Activity.mastodon_notification_type(activity)
1146 type: mastodon_type,
1147 created_at: CommonAPI.Utils.to_masto_date(created_at),
1148 account: AccountView.render("account.json", %{user: actor, for: user})
1151 case mastodon_type do
1155 status: StatusView.render("status.json", %{activity: activity, for: user})
1161 status: StatusView.render("status.json", %{activity: parent_activity, for: user})
1167 status: StatusView.render("status.json", %{activity: parent_activity, for: user})
1178 def get_filters(%{assigns: %{user: user}} = conn, _) do
1179 filters = Pleroma.Filter.get_filters(user)
1180 res = FilterView.render("filters.json", filters: filters)
1185 %{assigns: %{user: user}} = conn,
1186 %{"phrase" => phrase, "context" => context} = params
1188 query = %Pleroma.Filter{
1192 hide: Map.get(params, "irreversible", nil),
1193 whole_word: Map.get(params, "boolean", true)
1197 {:ok, response} = Pleroma.Filter.create(query)
1198 res = FilterView.render("filter.json", filter: response)
1202 def get_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do
1203 filter = Pleroma.Filter.get(filter_id, user)
1204 res = FilterView.render("filter.json", filter: filter)
1209 %{assigns: %{user: user}} = conn,
1210 %{"phrase" => phrase, "context" => context, "id" => filter_id} = params
1212 query = %Pleroma.Filter{
1214 filter_id: filter_id,
1217 hide: Map.get(params, "irreversible", nil),
1218 whole_word: Map.get(params, "boolean", true)
1222 {:ok, response} = Pleroma.Filter.update(query)
1223 res = FilterView.render("filter.json", filter: response)
1227 def delete_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do
1228 query = %Pleroma.Filter{
1230 filter_id: filter_id
1233 {:ok, _} = Pleroma.Filter.delete(query)
1237 def create_push_subscription(%{assigns: %{user: user, token: token}} = conn, params) do
1238 true = Pleroma.Web.Push.enabled()
1239 Pleroma.Web.Push.Subscription.delete_if_exists(user, token)
1240 {:ok, subscription} = Pleroma.Web.Push.Subscription.create(user, token, params)
1241 view = PushSubscriptionView.render("push_subscription.json", subscription: subscription)
1245 def get_push_subscription(%{assigns: %{user: user, token: token}} = conn, _params) do
1246 true = Pleroma.Web.Push.enabled()
1247 subscription = Pleroma.Web.Push.Subscription.get(user, token)
1248 view = PushSubscriptionView.render("push_subscription.json", subscription: subscription)
1252 def update_push_subscription(
1253 %{assigns: %{user: user, token: token}} = conn,
1256 true = Pleroma.Web.Push.enabled()
1257 {:ok, subscription} = Pleroma.Web.Push.Subscription.update(user, token, params)
1258 view = PushSubscriptionView.render("push_subscription.json", subscription: subscription)
1262 def delete_push_subscription(%{assigns: %{user: user, token: token}} = conn, _params) do
1263 true = Pleroma.Web.Push.enabled()
1264 {:ok, _response} = Pleroma.Web.Push.Subscription.delete(user, token)
1268 def errors(conn, _) do
1271 |> json("Something went wrong")
1274 def suggestions(%{assigns: %{user: user}} = conn, _) do
1275 suggestions = Pleroma.Config.get(:suggestions)
1277 if Keyword.get(suggestions, :enabled, false) do
1278 api = Keyword.get(suggestions, :third_party_engine, "")
1279 timeout = Keyword.get(suggestions, :timeout, 5000)
1280 limit = Keyword.get(suggestions, :limit, 23)
1282 host = Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host])
1284 user = user.nickname
1285 url = String.replace(api, "{{host}}", host) |> String.replace("{{user}}", user)
1287 with {:ok, %{status: 200, body: body}} <-
1293 recv_timeout: timeout
1296 {:ok, data} <- Jason.decode(body) do
1298 Enum.slice(data, 0, limit)
1303 case User.get_or_fetch(x["acct"]) do
1310 Map.put(x, "avatar", MediaProxy.url(x["avatar"]))
1313 Map.put(x, "avatar_static", MediaProxy.url(x["avatar_static"]))
1319 e -> Logger.error("Could not retrieve suggestions at fetch #{url}, #{inspect(e)}")
1326 def try_render(conn, target, params)
1327 when is_binary(target) do
1328 res = render(conn, target, params)
1333 |> json(%{error: "Can't display this activity"})
1339 def try_render(conn, _, _) do
1342 |> json(%{error: "Can't display this activity"})