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"])
346 case get_req_header(conn, "idempotency-key") do
348 _ -> Ecto.UUID.generate()
352 Cachex.fetch!(:idempotency_cache, idempotency_key, fn _ -> CommonAPI.post(user, params) end)
355 |> put_view(StatusView)
356 |> try_render("status.json", %{activity: activity, for: user, as: :activity})
359 def delete_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
360 with {:ok, %Activity{}} <- CommonAPI.delete(id, user) do
366 |> json(%{error: "Can't delete this post"})
370 def reblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
371 with {:ok, announce, _activity} <- CommonAPI.repeat(ap_id_or_id, user) do
373 |> put_view(StatusView)
374 |> try_render("status.json", %{activity: announce, for: user, as: :activity})
378 def unreblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
379 with {:ok, _unannounce, %{data: %{"id" => id}}} <- CommonAPI.unrepeat(ap_id_or_id, user),
380 %Activity{} = activity <- Activity.get_create_by_object_ap_id(id) do
382 |> put_view(StatusView)
383 |> try_render("status.json", %{activity: activity, for: user, as: :activity})
387 def fav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
388 with {:ok, _fav, %{data: %{"id" => id}}} <- CommonAPI.favorite(ap_id_or_id, user),
389 %Activity{} = activity <- Activity.get_create_by_object_ap_id(id) do
391 |> put_view(StatusView)
392 |> try_render("status.json", %{activity: activity, for: user, as: :activity})
396 def unfav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
397 with {:ok, _, _, %{data: %{"id" => id}}} <- CommonAPI.unfavorite(ap_id_or_id, user),
398 %Activity{} = activity <- Activity.get_create_by_object_ap_id(id) do
400 |> put_view(StatusView)
401 |> try_render("status.json", %{activity: activity, for: user, as: :activity})
405 def pin_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
406 with {:ok, activity} <- CommonAPI.pin(ap_id_or_id, user) do
408 |> put_view(StatusView)
409 |> try_render("status.json", %{activity: activity, for: user, as: :activity})
413 |> put_resp_content_type("application/json")
414 |> send_resp(:bad_request, Jason.encode!(%{"error" => reason}))
418 def unpin_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
419 with {:ok, activity} <- CommonAPI.unpin(ap_id_or_id, user) do
421 |> put_view(StatusView)
422 |> try_render("status.json", %{activity: activity, for: user, as: :activity})
426 def bookmark_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
427 with %Activity{} = activity <- Repo.get(Activity, id),
428 %User{} = user <- User.get_by_nickname(user.nickname),
429 true <- ActivityPub.visible_for_user?(activity, user),
430 {:ok, user} <- User.bookmark(user, activity.data["object"]["id"]) do
432 |> put_view(StatusView)
433 |> try_render("status.json", %{activity: activity, for: user, as: :activity})
437 def unbookmark_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
438 with %Activity{} = activity <- Repo.get(Activity, id),
439 %User{} = user <- User.get_by_nickname(user.nickname),
440 true <- ActivityPub.visible_for_user?(activity, user),
441 {:ok, user} <- User.unbookmark(user, activity.data["object"]["id"]) do
443 |> put_view(StatusView)
444 |> try_render("status.json", %{activity: activity, for: user, as: :activity})
448 def notifications(%{assigns: %{user: user}} = conn, params) do
449 notifications = Notification.for_user(user, params)
452 Enum.map(notifications, fn x ->
453 render_notification(user, x)
458 |> add_link_headers(:notifications, notifications)
462 def get_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
463 with {:ok, notification} <- Notification.get(user, id) do
464 json(conn, render_notification(user, notification))
468 |> put_resp_content_type("application/json")
469 |> send_resp(403, Jason.encode!(%{"error" => reason}))
473 def clear_notifications(%{assigns: %{user: user}} = conn, _params) do
474 Notification.clear(user)
478 def dismiss_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
479 with {:ok, _notif} <- Notification.dismiss(user, id) do
484 |> put_resp_content_type("application/json")
485 |> send_resp(403, Jason.encode!(%{"error" => reason}))
489 def relationships(%{assigns: %{user: user}} = conn, %{"id" => id}) do
491 q = from(u in User, where: u.id in ^id)
492 targets = Repo.all(q)
495 |> put_view(AccountView)
496 |> render("relationships.json", %{user: user, targets: targets})
499 # Instead of returning a 400 when no "id" params is present, Mastodon returns an empty array.
500 def relationships(%{assigns: %{user: _user}} = conn, _), do: json(conn, [])
502 def update_media(%{assigns: %{user: user}} = conn, data) do
503 with %Object{} = object <- Repo.get(Object, data["id"]),
504 true <- Object.authorize_mutation(object, user),
505 true <- is_binary(data["description"]),
506 description <- data["description"] do
507 new_data = %{object.data | "name" => description}
511 |> Object.change(%{data: new_data})
514 attachment_data = Map.put(new_data, "id", object.id)
517 |> put_view(StatusView)
518 |> render("attachment.json", %{attachment: attachment_data})
522 def upload(%{assigns: %{user: user}} = conn, %{"file" => file} = data) do
523 with {:ok, object} <-
526 actor: User.ap_id(user),
527 description: Map.get(data, "description")
529 attachment_data = Map.put(object.data, "id", object.id)
532 |> put_view(StatusView)
533 |> render("attachment.json", %{attachment: attachment_data})
537 def favourited_by(conn, %{"id" => id}) do
538 with %Activity{data: %{"object" => %{"likes" => likes}}} <- Repo.get(Activity, id) do
539 q = from(u in User, where: u.ap_id in ^likes)
543 |> put_view(AccountView)
544 |> render(AccountView, "accounts.json", %{users: users, as: :user})
550 def reblogged_by(conn, %{"id" => id}) do
551 with %Activity{data: %{"object" => %{"announcements" => announces}}} <- Repo.get(Activity, id) do
552 q = from(u in User, where: u.ap_id in ^announces)
556 |> put_view(AccountView)
557 |> render("accounts.json", %{users: users, as: :user})
563 def hashtag_timeline(%{assigns: %{user: user}} = conn, params) do
564 local_only = params["local"] in [true, "True", "true", "1"]
567 [params["tag"], params["any"]]
571 |> Enum.map(&String.downcase(&1))
576 |> Enum.map(&String.downcase(&1))
581 |> Enum.map(&String.downcase(&1))
585 |> Map.put("type", "Create")
586 |> Map.put("local_only", local_only)
587 |> Map.put("blocking_user", user)
588 |> Map.put("tag", tags)
589 |> Map.put("tag_all", tag_all)
590 |> Map.put("tag_reject", tag_reject)
593 ActivityPub.fetch_public_activities(query_params)
597 |> add_link_headers(:hashtag_timeline, activities, params["tag"], %{"local" => local_only})
598 |> put_view(StatusView)
599 |> render("index.json", %{activities: activities, for: user, as: :activity})
602 def followers(%{assigns: %{user: for_user}} = conn, %{"id" => id}) do
603 with %User{} = user <- Repo.get(User, id),
604 {:ok, followers} <- User.get_followers(user) do
607 for_user && user.id == for_user.id -> followers
608 user.info.hide_network -> []
613 |> put_view(AccountView)
614 |> render("accounts.json", %{users: followers, as: :user})
618 def following(%{assigns: %{user: for_user}} = conn, %{"id" => id}) do
619 with %User{} = user <- Repo.get(User, id),
620 {:ok, followers} <- User.get_friends(user) do
623 for_user && user.id == for_user.id -> followers
624 user.info.hide_network -> []
629 |> put_view(AccountView)
630 |> render("accounts.json", %{users: followers, as: :user})
634 def follow_requests(%{assigns: %{user: followed}} = conn, _params) do
635 with {:ok, follow_requests} <- User.get_follow_requests(followed) do
637 |> put_view(AccountView)
638 |> render("accounts.json", %{users: follow_requests, as: :user})
642 def authorize_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
643 with %User{} = follower <- Repo.get(User, id),
644 {:ok, follower} <- User.maybe_follow(follower, followed),
645 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
646 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "accept"),
648 ActivityPub.accept(%{
649 to: [follower.ap_id],
650 actor: followed.ap_id,
651 object: follow_activity.data["id"],
655 |> put_view(AccountView)
656 |> render("relationship.json", %{user: followed, target: follower})
660 |> put_resp_content_type("application/json")
661 |> send_resp(403, Jason.encode!(%{"error" => message}))
665 def reject_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
666 with %User{} = follower <- Repo.get(User, id),
667 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
668 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "reject"),
670 ActivityPub.reject(%{
671 to: [follower.ap_id],
672 actor: followed.ap_id,
673 object: follow_activity.data["id"],
677 |> put_view(AccountView)
678 |> render("relationship.json", %{user: followed, target: follower})
682 |> put_resp_content_type("application/json")
683 |> send_resp(403, Jason.encode!(%{"error" => message}))
687 def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
688 with %User{} = followed <- Repo.get(User, id),
689 {:ok, follower} <- User.maybe_direct_follow(follower, followed),
690 {:ok, _activity} <- ActivityPub.follow(follower, followed),
691 {:ok, follower, followed} <-
692 User.wait_and_refresh(
693 Pleroma.Config.get([:activitypub, :follow_handshake_timeout]),
698 |> put_view(AccountView)
699 |> render("relationship.json", %{user: follower, target: followed})
703 |> put_resp_content_type("application/json")
704 |> send_resp(403, Jason.encode!(%{"error" => message}))
708 def follow(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do
709 with %User{} = followed <- Repo.get_by(User, nickname: uri),
710 {:ok, follower} <- User.maybe_direct_follow(follower, followed),
711 {:ok, _activity} <- ActivityPub.follow(follower, followed) do
713 |> put_view(AccountView)
714 |> render("account.json", %{user: followed, for: follower})
718 |> put_resp_content_type("application/json")
719 |> send_resp(403, Jason.encode!(%{"error" => message}))
723 def unfollow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
724 with %User{} = followed <- Repo.get(User, id),
725 {:ok, _activity} <- ActivityPub.unfollow(follower, followed),
726 {:ok, follower, _} <- User.unfollow(follower, followed) do
728 |> put_view(AccountView)
729 |> render("relationship.json", %{user: follower, target: followed})
733 def block(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
734 with %User{} = blocked <- Repo.get(User, id),
735 {:ok, blocker} <- User.block(blocker, blocked),
736 {:ok, _activity} <- ActivityPub.block(blocker, blocked) do
738 |> put_view(AccountView)
739 |> render("relationship.json", %{user: blocker, target: blocked})
743 |> put_resp_content_type("application/json")
744 |> send_resp(403, Jason.encode!(%{"error" => message}))
748 def unblock(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
749 with %User{} = blocked <- Repo.get(User, id),
750 {:ok, blocker} <- User.unblock(blocker, blocked),
751 {:ok, _activity} <- ActivityPub.unblock(blocker, blocked) do
753 |> put_view(AccountView)
754 |> render("relationship.json", %{user: blocker, target: blocked})
758 |> put_resp_content_type("application/json")
759 |> send_resp(403, Jason.encode!(%{"error" => message}))
763 def blocks(%{assigns: %{user: user}} = conn, _) do
764 with blocked_accounts <- User.blocked_users(user) do
765 res = AccountView.render("accounts.json", users: blocked_accounts, for: user, as: :user)
770 def domain_blocks(%{assigns: %{user: %{info: info}}} = conn, _) do
771 json(conn, info.domain_blocks || [])
774 def block_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
775 User.block_domain(blocker, domain)
779 def unblock_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
780 User.unblock_domain(blocker, domain)
784 def status_search(user, query) do
786 if Regex.match?(~r/https?:/, query) do
787 with {:ok, object} <- ActivityPub.fetch_object_from_id(query),
788 %Activity{} = activity <- Activity.get_create_by_object_ap_id(object.data["id"]),
789 true <- ActivityPub.visible_for_user?(activity, user) do
799 where: fragment("?->>'type' = 'Create'", a.data),
800 where: "https://www.w3.org/ns/activitystreams#Public" in a.recipients,
803 "to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)",
808 order_by: [desc: :id]
811 Repo.all(q) ++ fetched
814 def search2(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
815 accounts = User.search(query, params["resolve"] == "true", user)
817 statuses = status_search(user, query)
819 tags_path = Web.base_url() <> "/tag/"
824 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
825 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
826 |> Enum.map(fn tag -> %{name: tag, url: tags_path <> tag} end)
829 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
831 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
838 def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
839 accounts = User.search(query, params["resolve"] == "true", user)
841 statuses = status_search(user, query)
846 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
847 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
850 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
852 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
859 def account_search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
860 accounts = User.search(query, params["resolve"] == "true", user)
862 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
867 def favourites(%{assigns: %{user: user}} = conn, params) do
870 |> Map.put("type", "Create")
871 |> Map.put("favorited_by", user.ap_id)
872 |> Map.put("blocking_user", user)
875 ActivityPub.fetch_public_activities(params)
879 |> add_link_headers(:favourites, activities)
880 |> put_view(StatusView)
881 |> render("index.json", %{activities: activities, for: user, as: :activity})
884 def bookmarks(%{assigns: %{user: user}} = conn, _) do
885 user = Repo.get(User, user.id)
889 |> Enum.map(fn id -> Activity.get_create_by_object_ap_id(id) end)
893 |> put_view(StatusView)
894 |> render("index.json", %{activities: activities, for: user, as: :activity})
897 def get_lists(%{assigns: %{user: user}} = conn, opts) do
898 lists = Pleroma.List.for_user(user, opts)
899 res = ListView.render("lists.json", lists: lists)
903 def get_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
904 with %Pleroma.List{} = list <- Pleroma.List.get(id, user) do
905 res = ListView.render("list.json", list: list)
908 _e -> json(conn, "error")
912 def account_lists(%{assigns: %{user: user}} = conn, %{"id" => account_id}) do
913 lists = Pleroma.List.get_lists_account_belongs(user, account_id)
914 res = ListView.render("lists.json", lists: lists)
918 def delete_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
919 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
920 {:ok, _list} <- Pleroma.List.delete(list) do
928 def create_list(%{assigns: %{user: user}} = conn, %{"title" => title}) do
929 with {:ok, %Pleroma.List{} = list} <- Pleroma.List.create(title, user) do
930 res = ListView.render("list.json", list: list)
935 def add_to_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
937 |> Enum.each(fn account_id ->
938 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
939 %User{} = followed <- Repo.get(User, account_id) do
940 Pleroma.List.follow(list, followed)
947 def remove_from_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
949 |> Enum.each(fn account_id ->
950 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
951 %User{} = followed <- Repo.get(Pleroma.User, account_id) do
952 Pleroma.List.unfollow(list, followed)
959 def list_accounts(%{assigns: %{user: user}} = conn, %{"id" => id}) do
960 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
961 {:ok, users} = Pleroma.List.get_following(list) do
963 |> put_view(AccountView)
964 |> render("accounts.json", %{users: users, as: :user})
968 def rename_list(%{assigns: %{user: user}} = conn, %{"id" => id, "title" => title}) do
969 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
970 {:ok, list} <- Pleroma.List.rename(list, title) do
971 res = ListView.render("list.json", list: list)
979 def list_timeline(%{assigns: %{user: user}} = conn, %{"list_id" => id} = params) do
980 with %Pleroma.List{title: _title, following: following} <- Pleroma.List.get(id, user) do
983 |> Map.put("type", "Create")
984 |> Map.put("blocking_user", user)
986 # we must filter the following list for the user to avoid leaking statuses the user
987 # does not actually have permission to see (for more info, peruse security issue #270).
990 |> Enum.filter(fn x -> x in user.following end)
993 ActivityPub.fetch_activities_bounded(following_to, following, params)
997 |> put_view(StatusView)
998 |> render("index.json", %{activities: activities, for: user, as: :activity})
1003 |> json(%{error: "Error."})
1007 def index(%{assigns: %{user: user}} = conn, _params) do
1010 |> get_session(:oauth_token)
1013 mastodon_emoji = mastodonized_emoji()
1015 limit = Pleroma.Config.get([:instance, :limit])
1018 Map.put(%{}, user.id, AccountView.render("account.json", %{user: user, for: user}))
1023 streaming_api_base_url:
1024 String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws"),
1025 access_token: token,
1027 domain: Pleroma.Web.Endpoint.host(),
1030 unfollow_modal: false,
1033 auto_play_gif: false,
1034 display_sensitive_media: false,
1035 reduce_motion: false,
1036 max_toot_chars: limit
1039 delete_others_notice: !!user.info.is_moderator,
1040 admin: !!user.info.is_admin
1044 default_privacy: user.info.default_scope,
1045 default_sensitive: false
1047 media_attachments: %{
1048 accept_content_types: [
1064 user.info.settings ||
1094 push_subscription: nil,
1096 custom_emojis: mastodon_emoji,
1102 |> put_layout(false)
1103 |> put_view(MastodonView)
1104 |> render("index.html", %{initial_state: initial_state})
1107 |> redirect(to: "/web/login")
1111 def put_settings(%{assigns: %{user: user}} = conn, %{"data" => settings} = _params) do
1112 info_cng = User.Info.mastodon_settings_update(user.info, settings)
1114 with changeset <- Ecto.Changeset.change(user),
1115 changeset <- Ecto.Changeset.put_embed(changeset, :info, info_cng),
1116 {:ok, _user} <- User.update_and_set_cache(changeset) do
1121 |> put_resp_content_type("application/json")
1122 |> send_resp(500, Jason.encode!(%{"error" => inspect(e)}))
1126 def login(conn, %{"code" => code}) do
1127 with {:ok, app} <- get_or_make_app(),
1128 %Authorization{} = auth <- Repo.get_by(Authorization, token: code, app_id: app.id),
1129 {:ok, token} <- Token.exchange_token(app, auth) do
1131 |> put_session(:oauth_token, token.token)
1132 |> redirect(to: "/web/getting-started")
1136 def login(conn, _) do
1137 with {:ok, app} <- get_or_make_app() do
1142 response_type: "code",
1143 client_id: app.client_id,
1149 |> redirect(to: path)
1153 defp get_or_make_app() do
1154 with %App{} = app <- Repo.get_by(App, client_name: "Mastodon-Local") do
1159 App.register_changeset(%App{}, %{
1160 client_name: "Mastodon-Local",
1162 scopes: "read,write,follow"
1169 def logout(conn, _) do
1172 |> redirect(to: "/")
1175 def relationship_noop(%{assigns: %{user: user}} = conn, %{"id" => id}) do
1176 Logger.debug("Unimplemented, returning unmodified relationship")
1178 with %User{} = target <- Repo.get(User, id) do
1180 |> put_view(AccountView)
1181 |> render("relationship.json", %{user: user, target: target})
1185 def empty_array(conn, _) do
1186 Logger.debug("Unimplemented, returning an empty array")
1190 def empty_object(conn, _) do
1191 Logger.debug("Unimplemented, returning an empty object")
1195 def render_notification(user, %{id: id, activity: activity, inserted_at: created_at} = _params) do
1196 actor = User.get_cached_by_ap_id(activity.data["actor"])
1197 parent_activity = Activity.get_create_by_object_ap_id(activity.data["object"])
1198 mastodon_type = Activity.mastodon_notification_type(activity)
1202 type: mastodon_type,
1203 created_at: CommonAPI.Utils.to_masto_date(created_at),
1204 account: AccountView.render("account.json", %{user: actor, for: user})
1207 case mastodon_type do
1211 status: StatusView.render("status.json", %{activity: activity, for: user})
1217 status: StatusView.render("status.json", %{activity: parent_activity, for: user})
1223 status: StatusView.render("status.json", %{activity: parent_activity, for: user})
1234 def get_filters(%{assigns: %{user: user}} = conn, _) do
1235 filters = Pleroma.Filter.get_filters(user)
1236 res = FilterView.render("filters.json", filters: filters)
1241 %{assigns: %{user: user}} = conn,
1242 %{"phrase" => phrase, "context" => context} = params
1244 query = %Pleroma.Filter{
1248 hide: Map.get(params, "irreversible", nil),
1249 whole_word: Map.get(params, "boolean", true)
1253 {:ok, response} = Pleroma.Filter.create(query)
1254 res = FilterView.render("filter.json", filter: response)
1258 def get_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do
1259 filter = Pleroma.Filter.get(filter_id, user)
1260 res = FilterView.render("filter.json", filter: filter)
1265 %{assigns: %{user: user}} = conn,
1266 %{"phrase" => phrase, "context" => context, "id" => filter_id} = params
1268 query = %Pleroma.Filter{
1270 filter_id: filter_id,
1273 hide: Map.get(params, "irreversible", nil),
1274 whole_word: Map.get(params, "boolean", true)
1278 {:ok, response} = Pleroma.Filter.update(query)
1279 res = FilterView.render("filter.json", filter: response)
1283 def delete_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do
1284 query = %Pleroma.Filter{
1286 filter_id: filter_id
1289 {:ok, _} = Pleroma.Filter.delete(query)
1293 def create_push_subscription(%{assigns: %{user: user, token: token}} = conn, params) do
1294 true = Pleroma.Web.Push.enabled()
1295 Pleroma.Web.Push.Subscription.delete_if_exists(user, token)
1296 {:ok, subscription} = Pleroma.Web.Push.Subscription.create(user, token, params)
1297 view = PushSubscriptionView.render("push_subscription.json", subscription: subscription)
1301 def get_push_subscription(%{assigns: %{user: user, token: token}} = conn, _params) do
1302 true = Pleroma.Web.Push.enabled()
1303 subscription = Pleroma.Web.Push.Subscription.get(user, token)
1304 view = PushSubscriptionView.render("push_subscription.json", subscription: subscription)
1308 def update_push_subscription(
1309 %{assigns: %{user: user, token: token}} = conn,
1312 true = Pleroma.Web.Push.enabled()
1313 {:ok, subscription} = Pleroma.Web.Push.Subscription.update(user, token, params)
1314 view = PushSubscriptionView.render("push_subscription.json", subscription: subscription)
1318 def delete_push_subscription(%{assigns: %{user: user, token: token}} = conn, _params) do
1319 true = Pleroma.Web.Push.enabled()
1320 {:ok, _response} = Pleroma.Web.Push.Subscription.delete(user, token)
1324 def errors(conn, _) do
1327 |> json("Something went wrong")
1330 def suggestions(%{assigns: %{user: user}} = conn, _) do
1331 suggestions = Pleroma.Config.get(:suggestions)
1333 if Keyword.get(suggestions, :enabled, false) do
1334 api = Keyword.get(suggestions, :third_party_engine, "")
1335 timeout = Keyword.get(suggestions, :timeout, 5000)
1336 limit = Keyword.get(suggestions, :limit, 23)
1338 host = Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host])
1340 user = user.nickname
1341 url = String.replace(api, "{{host}}", host) |> String.replace("{{user}}", user)
1343 with {:ok, %{status: 200, body: body}} <-
1349 recv_timeout: timeout,
1353 {:ok, data} <- Jason.decode(body) do
1355 Enum.slice(data, 0, limit)
1360 case User.get_or_fetch(x["acct"]) do
1367 Map.put(x, "avatar", MediaProxy.url(x["avatar"]))
1370 Map.put(x, "avatar_static", MediaProxy.url(x["avatar_static"]))
1376 e -> Logger.error("Could not retrieve suggestions at fetch #{url}, #{inspect(e)}")
1383 def status_card(conn, %{"id" => status_id}) do
1384 with %Activity{} = activity <- Repo.get(Activity, status_id),
1385 true <- ActivityPub.is_public?(activity) do
1389 Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
1399 def try_render(conn, target, params)
1400 when is_binary(target) do
1401 res = render(conn, target, params)
1406 |> json(%{error: "Can't display this activity"})
1412 def try_render(conn, _, _) do
1415 |> json(%{error: "Can't display this activity"})