1 defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
2 use Pleroma.Web, :controller
3 alias Pleroma.{Repo, Object, Activity, User, Notification, Stats}
6 alias Pleroma.Web.MastodonAPI.{
15 alias Pleroma.Web.ActivityPub.ActivityPub
16 alias Pleroma.Web.ActivityPub.Utils
17 alias Pleroma.Web.CommonAPI
18 alias Pleroma.Web.OAuth.{Authorization, Token, App}
19 alias Pleroma.Web.MediaProxy
24 @httpoison Application.get_env(:pleroma, :httpoison)
26 action_fallback(:errors)
28 def create_app(conn, params) do
29 with cs <- App.register_changeset(%App{}, params) |> IO.inspect(),
30 {:ok, app} <- Repo.insert(cs) |> IO.inspect() do
32 id: app.id |> to_string,
33 name: app.client_name,
34 client_id: app.client_id,
35 client_secret: app.client_secret,
36 redirect_uri: app.redirect_uris,
49 value_function \\ fn x -> {:ok, x} end
51 if Map.has_key?(params, params_field) do
52 case value_function.(params[params_field]) do
53 {:ok, new_value} -> Map.put(map, map_field, new_value)
61 def update_credentials(%{assigns: %{user: user}} = conn, params) do
66 |> add_if_present(params, "display_name", :name)
67 |> add_if_present(params, "note", :bio, fn value -> {:ok, User.parse_bio(value)} end)
68 |> add_if_present(params, "avatar", :avatar, fn value ->
69 with %Plug.Upload{} <- value,
70 {:ok, object} <- ActivityPub.upload(value, type: :avatar) do
79 |> add_if_present(params, "locked", :locked, fn value -> {:ok, value == "true"} end)
80 |> add_if_present(params, "header", :banner, fn value ->
81 with %Plug.Upload{} <- value,
82 {:ok, object} <- ActivityPub.upload(value, type: :banner) do
89 info_cng = User.Info.mastodon_profile_update(user.info, info_params)
91 with changeset <- User.update_changeset(user, user_params),
92 changeset <- Ecto.Changeset.put_embed(changeset, :info, info_cng),
93 {:ok, user} <- User.update_and_set_cache(changeset) do
94 if original_user != user do
95 CommonAPI.update(user)
98 json(conn, AccountView.render("account.json", %{user: user, for: user}))
103 |> json(%{error: "Invalid request"})
107 def verify_credentials(%{assigns: %{user: user}} = conn, _) do
108 account = AccountView.render("account.json", %{user: user, for: user})
112 def user(%{assigns: %{user: for_user}} = conn, %{"id" => id}) do
113 with %User{} = user <- Repo.get(User, id) do
114 account = AccountView.render("account.json", %{user: user, for: for_user})
120 |> json(%{error: "Can't find user"})
124 @mastodon_api_level "2.5.0"
126 def masto_instance(conn, _params) do
127 instance = Pleroma.Config.get(:instance)
131 title: Keyword.get(instance, :name),
132 description: Keyword.get(instance, :description),
133 version: "#{@mastodon_api_level} (compatible; #{Pleroma.Application.named_version()})",
134 email: Keyword.get(instance, :email),
136 streaming_api: String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws")
138 stats: Stats.get_stats(),
139 thumbnail: Web.base_url() <> "/instance/thumbnail.jpeg",
140 max_toot_chars: Keyword.get(instance, :limit)
146 def peers(conn, _params) do
147 json(conn, Stats.get_peers())
150 defp mastodonized_emoji do
151 Pleroma.Emoji.get_all()
152 |> Enum.map(fn {shortcode, relative_url} ->
153 url = to_string(URI.merge(Web.base_url(), relative_url))
156 "shortcode" => shortcode,
158 "visible_in_picker" => true,
164 def custom_emojis(conn, _params) do
165 mastodon_emoji = mastodonized_emoji()
166 json(conn, mastodon_emoji)
169 defp add_link_headers(conn, method, activities, param \\ nil, params \\ %{}) do
170 last = List.last(activities)
171 first = List.first(activities)
177 {next_url, prev_url} =
181 Pleroma.Web.Endpoint,
184 Map.merge(params, %{max_id: min})
187 Pleroma.Web.Endpoint,
190 Map.merge(params, %{since_id: max})
196 Pleroma.Web.Endpoint,
198 Map.merge(params, %{max_id: min})
201 Pleroma.Web.Endpoint,
203 Map.merge(params, %{since_id: max})
209 |> put_resp_header("link", "<#{next_url}>; rel=\"next\", <#{prev_url}>; rel=\"prev\"")
215 def home_timeline(%{assigns: %{user: user}} = conn, params) do
218 |> Map.put("type", ["Create", "Announce"])
219 |> Map.put("blocking_user", user)
220 |> Map.put("user", user)
223 ActivityPub.fetch_activities([user.ap_id | user.following], params)
224 |> ActivityPub.contain_timeline(user)
228 |> add_link_headers(:home_timeline, activities)
229 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
232 def public_timeline(%{assigns: %{user: user}} = conn, params) do
233 local_only = params["local"] in [true, "True", "true", "1"]
237 |> Map.put("type", ["Create", "Announce"])
238 |> Map.put("local_only", local_only)
239 |> Map.put("blocking_user", user)
242 ActivityPub.fetch_public_activities(params)
246 |> add_link_headers(:public_timeline, activities, false, %{"local" => local_only})
247 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
250 def user_statuses(%{assigns: %{user: reading_user}} = conn, params) do
251 with %User{} = user <- Repo.get(User, params["id"]) do
252 # Since Pleroma has no "pinned" posts feature, we'll just set an empty list here
254 if params["pinned"] == "true" do
257 ActivityPub.fetch_user_activities(user, reading_user, params)
261 |> add_link_headers(:user_statuses, activities, params["id"])
262 |> render(StatusView, "index.json", %{
263 activities: activities,
270 def dm_timeline(%{assigns: %{user: user}} = conn, params) do
272 ActivityPub.fetch_activities_query(
274 Map.merge(params, %{"type" => "Create", visibility: "direct"})
277 activities = Repo.all(query)
280 |> add_link_headers(:dm_timeline, activities)
281 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
284 def get_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
285 with %Activity{} = activity <- Repo.get(Activity, id),
286 true <- ActivityPub.visible_for_user?(activity, user) do
287 try_render(conn, StatusView, "status.json", %{activity: activity, for: user})
291 def get_context(%{assigns: %{user: user}} = conn, %{"id" => id}) do
292 with %Activity{} = activity <- Repo.get(Activity, id),
294 ActivityPub.fetch_activities_for_context(activity.data["context"], %{
295 "blocking_user" => user,
299 activities |> Enum.filter(fn %{id: aid} -> to_string(aid) != to_string(id) end),
301 activities |> Enum.filter(fn %{data: %{"type" => type}} -> type == "Create" end),
302 grouped_activities <- Enum.group_by(activities, fn %{id: id} -> id < activity.id end) do
308 activities: grouped_activities[true] || [],
316 activities: grouped_activities[false] || [],
326 def post_status(conn, %{"status" => "", "media_ids" => media_ids} = params)
327 when length(media_ids) > 0 do
330 |> Map.put("status", ".")
332 post_status(conn, params)
335 def post_status(%{assigns: %{user: user}} = conn, %{"status" => _} = params) do
338 |> Map.put("in_reply_to_status_id", params["in_reply_to_id"])
339 |> Map.put("no_attachment_links", true)
342 case get_req_header(conn, "idempotency-key") do
344 _ -> Ecto.UUID.generate()
348 Cachex.fetch!(:idempotency_cache, idempotency_key, fn _ -> CommonAPI.post(user, params) end)
350 try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
353 def delete_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
354 with {:ok, %Activity{}} <- CommonAPI.delete(id, user) do
360 |> json(%{error: "Can't delete this post"})
364 def reblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
365 with {:ok, announce, _activity} <- CommonAPI.repeat(ap_id_or_id, user) do
366 try_render(conn, StatusView, "status.json", %{activity: announce, for: user, as: :activity})
370 def unreblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
371 with {:ok, _unannounce, %{data: %{"id" => id}}} <- CommonAPI.unrepeat(ap_id_or_id, user),
372 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
373 try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
377 def fav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
378 with {:ok, _fav, %{data: %{"id" => id}}} <- CommonAPI.favorite(ap_id_or_id, user),
379 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
380 try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
384 def unfav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
385 with {:ok, _, _, %{data: %{"id" => id}}} <- CommonAPI.unfavorite(ap_id_or_id, user),
386 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
387 try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
391 def notifications(%{assigns: %{user: user}} = conn, params) do
392 notifications = Notification.for_user(user, params)
395 Enum.map(notifications, fn x ->
396 render_notification(user, x)
401 |> add_link_headers(:notifications, notifications)
405 def get_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
406 with {:ok, notification} <- Notification.get(user, id) do
407 json(conn, render_notification(user, notification))
411 |> put_resp_content_type("application/json")
412 |> send_resp(403, Jason.encode!(%{"error" => reason}))
416 def clear_notifications(%{assigns: %{user: user}} = conn, _params) do
417 Notification.clear(user)
421 def dismiss_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
422 with {:ok, _notif} <- Notification.dismiss(user, id) do
427 |> put_resp_content_type("application/json")
428 |> send_resp(403, Jason.encode!(%{"error" => reason}))
432 def relationships(%{assigns: %{user: user}} = conn, %{"id" => id}) do
434 q = from(u in User, where: u.id in ^id)
435 targets = Repo.all(q)
436 render(conn, AccountView, "relationships.json", %{user: user, targets: targets})
439 # Instead of returning a 400 when no "id" params is present, Mastodon returns an empty array.
440 def relationships(%{assigns: %{user: _user}} = conn, _), do: json(conn, [])
442 def update_media(%{assigns: %{user: user}} = conn, data) do
443 with %Object{} = object <- Repo.get(Object, data["id"]),
444 true <- Object.authorize_mutation(object, user),
445 true <- is_binary(data["description"]),
446 description <- data["description"] do
447 new_data = %{object.data | "name" => description}
451 |> Object.change(%{data: new_data})
454 attachment_data = Map.put(new_data, "id", object.id)
455 render(conn, StatusView, "attachment.json", %{attachment: attachment_data})
459 def upload(%{assigns: %{user: user}} = conn, %{"file" => file} = data) do
460 with {:ok, object} <-
461 ActivityPub.upload(file,
462 actor: User.ap_id(user),
463 description: Map.get(data, "description")
465 attachment_data = Map.put(object.data, "id", object.id)
466 render(conn, StatusView, "attachment.json", %{attachment: attachment_data})
470 def favourited_by(conn, %{"id" => id}) do
471 with %Activity{data: %{"object" => %{"likes" => likes}}} <- Repo.get(Activity, id) do
472 q = from(u in User, where: u.ap_id in ^likes)
474 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
480 def reblogged_by(conn, %{"id" => id}) do
481 with %Activity{data: %{"object" => %{"announcements" => announces}}} <- Repo.get(Activity, id) do
482 q = from(u in User, where: u.ap_id in ^announces)
484 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
490 def hashtag_timeline(%{assigns: %{user: user}} = conn, params) do
491 local_only = params["local"] in [true, "True", "true", "1"]
495 |> Map.put("type", "Create")
496 |> Map.put("local_only", local_only)
497 |> Map.put("blocking_user", user)
498 |> Map.put("tag", String.downcase(params["tag"]))
501 ActivityPub.fetch_public_activities(params)
505 |> add_link_headers(:hashtag_timeline, activities, params["tag"], %{"local" => local_only})
506 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
509 def followers(%{assigns: %{user: for_user}} = conn, %{"id" => id}) do
510 with %User{} = user <- Repo.get(User, id),
511 {:ok, followers} <- User.get_followers(user) do
514 for_user && user.id == for_user.id -> followers
515 user.info.hide_network -> []
519 render(conn, AccountView, "accounts.json", %{users: followers, as: :user})
523 def following(%{assigns: %{user: for_user}} = conn, %{"id" => id}) do
524 with %User{} = user <- Repo.get(User, id),
525 {:ok, followers} <- User.get_friends(user) do
528 for_user && user.id == for_user.id -> followers
529 user.info.hide_network -> []
533 render(conn, AccountView, "accounts.json", %{users: followers, as: :user})
537 def follow_requests(%{assigns: %{user: followed}} = conn, _params) do
538 with {:ok, follow_requests} <- User.get_follow_requests(followed) do
539 render(conn, AccountView, "accounts.json", %{users: follow_requests, as: :user})
543 def authorize_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
544 with %User{} = follower <- Repo.get(User, id),
545 {:ok, follower} <- User.maybe_follow(follower, followed),
546 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
547 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "accept"),
549 ActivityPub.accept(%{
550 to: [follower.ap_id],
551 actor: followed.ap_id,
552 object: follow_activity.data["id"],
555 render(conn, AccountView, "relationship.json", %{user: followed, target: follower})
559 |> put_resp_content_type("application/json")
560 |> send_resp(403, Jason.encode!(%{"error" => message}))
564 def reject_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
565 with %User{} = follower <- Repo.get(User, id),
566 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
567 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "reject"),
569 ActivityPub.reject(%{
570 to: [follower.ap_id],
571 actor: followed.ap_id,
572 object: follow_activity.data["id"],
575 render(conn, AccountView, "relationship.json", %{user: followed, target: follower})
579 |> put_resp_content_type("application/json")
580 |> send_resp(403, Jason.encode!(%{"error" => message}))
584 def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
585 with %User{} = followed <- Repo.get(User, id),
586 {:ok, follower} <- User.maybe_direct_follow(follower, followed),
587 {:ok, _activity} <- ActivityPub.follow(follower, followed),
588 {:ok, follower, followed} <-
589 User.wait_and_refresh(
590 Pleroma.Config.get([:activitypub, :follow_handshake_timeout]),
594 render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
598 |> put_resp_content_type("application/json")
599 |> send_resp(403, Jason.encode!(%{"error" => message}))
603 def follow(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do
604 with %User{} = followed <- Repo.get_by(User, nickname: uri),
605 {:ok, follower} <- User.maybe_direct_follow(follower, followed),
606 {:ok, _activity} <- ActivityPub.follow(follower, followed) do
607 render(conn, AccountView, "account.json", %{user: followed, for: follower})
611 |> put_resp_content_type("application/json")
612 |> send_resp(403, Jason.encode!(%{"error" => message}))
616 def unfollow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
617 with %User{} = followed <- Repo.get(User, id),
618 {:ok, _activity} <- ActivityPub.unfollow(follower, followed),
619 {:ok, follower, _} <- User.unfollow(follower, followed) do
620 render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
624 def block(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
625 with %User{} = blocked <- Repo.get(User, id),
626 {:ok, blocker} <- User.block(blocker, blocked),
627 {:ok, _activity} <- ActivityPub.block(blocker, blocked) do
628 render(conn, AccountView, "relationship.json", %{user: blocker, target: blocked})
632 |> put_resp_content_type("application/json")
633 |> send_resp(403, Jason.encode!(%{"error" => message}))
637 def unblock(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
638 with %User{} = blocked <- Repo.get(User, id),
639 {:ok, blocker} <- User.unblock(blocker, blocked),
640 {:ok, _activity} <- ActivityPub.unblock(blocker, blocked) do
641 render(conn, AccountView, "relationship.json", %{user: blocker, target: blocked})
645 |> put_resp_content_type("application/json")
646 |> send_resp(403, Jason.encode!(%{"error" => message}))
650 # TODO: Use proper query
651 def blocks(%{assigns: %{user: user}} = conn, _) do
652 with blocked_users <- user.info.blocks || [],
653 accounts <- Enum.map(blocked_users, fn ap_id -> User.get_cached_by_ap_id(ap_id) end) do
654 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
659 def domain_blocks(%{assigns: %{user: %{info: info}}} = conn, _) do
660 json(conn, info.domain_blocks || [])
663 def block_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
664 User.block_domain(blocker, domain)
668 def unblock_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
669 User.unblock_domain(blocker, domain)
673 def status_search(query) do
675 if Regex.match?(~r/https?:/, query) do
676 with {:ok, object} <- ActivityPub.fetch_object_from_id(query) do
677 [Activity.get_create_activity_by_object_ap_id(object.data["id"])]
686 where: fragment("?->>'type' = 'Create'", a.data),
687 where: "https://www.w3.org/ns/activitystreams#Public" in a.recipients,
690 "to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)",
695 order_by: [desc: :id]
698 Repo.all(q) ++ fetched
701 def search2(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
702 accounts = User.search(query, params["resolve"] == "true")
704 statuses = status_search(query)
706 tags_path = Web.base_url() <> "/tag/"
711 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
712 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
713 |> Enum.map(fn tag -> %{name: tag, url: tags_path <> tag} end)
716 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
718 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
725 def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
726 accounts = User.search(query, params["resolve"] == "true")
728 statuses = status_search(query)
733 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
734 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
737 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
739 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
746 def account_search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
747 accounts = User.search(query, params["resolve"] == "true")
749 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
754 def favourites(%{assigns: %{user: user}} = conn, _) do
757 |> Map.put("type", "Create")
758 |> Map.put("favorited_by", user.ap_id)
759 |> Map.put("blocking_user", user)
762 ActivityPub.fetch_public_activities(params)
766 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
769 def get_lists(%{assigns: %{user: user}} = conn, opts) do
770 lists = Pleroma.List.for_user(user, opts)
771 res = ListView.render("lists.json", lists: lists)
775 def get_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
776 with %Pleroma.List{} = list <- Pleroma.List.get(id, user) do
777 res = ListView.render("list.json", list: list)
780 _e -> json(conn, "error")
784 def account_lists(%{assigns: %{user: user}} = conn, %{"id" => account_id}) do
785 lists = Pleroma.List.get_lists_account_belongs(user, account_id)
786 res = ListView.render("lists.json", lists: lists)
790 def delete_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
791 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
792 {:ok, _list} <- Pleroma.List.delete(list) do
800 def create_list(%{assigns: %{user: user}} = conn, %{"title" => title}) do
801 with {:ok, %Pleroma.List{} = list} <- Pleroma.List.create(title, user) do
802 res = ListView.render("list.json", list: list)
807 def add_to_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
809 |> Enum.each(fn account_id ->
810 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
811 %User{} = followed <- Repo.get(User, account_id) do
812 Pleroma.List.follow(list, followed)
819 def remove_from_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
821 |> Enum.each(fn account_id ->
822 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
823 %User{} = followed <- Repo.get(Pleroma.User, account_id) do
824 Pleroma.List.unfollow(list, followed)
831 def list_accounts(%{assigns: %{user: user}} = conn, %{"id" => id}) do
832 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
833 {:ok, users} = Pleroma.List.get_following(list) do
834 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
838 def rename_list(%{assigns: %{user: user}} = conn, %{"id" => id, "title" => title}) do
839 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
840 {:ok, list} <- Pleroma.List.rename(list, title) do
841 res = ListView.render("list.json", list: list)
849 def list_timeline(%{assigns: %{user: user}} = conn, %{"list_id" => id} = params) do
850 with %Pleroma.List{title: _title, following: following} <- Pleroma.List.get(id, user) do
853 |> Map.put("type", "Create")
854 |> Map.put("blocking_user", user)
856 # we must filter the following list for the user to avoid leaking statuses the user
857 # does not actually have permission to see (for more info, peruse security issue #270).
860 |> Enum.filter(fn x -> x in user.following end)
863 ActivityPub.fetch_activities_bounded(following_to, following, params)
867 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
872 |> json(%{error: "Error."})
876 def index(%{assigns: %{user: user}} = conn, _params) do
879 |> get_session(:oauth_token)
882 mastodon_emoji = mastodonized_emoji()
884 limit = Pleroma.Config.get([:instance, :limit])
887 Map.put(%{}, user.id, AccountView.render("account.json", %{user: user, for: user}))
892 streaming_api_base_url:
893 String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws"),
896 domain: Pleroma.Web.Endpoint.host(),
899 unfollow_modal: false,
902 auto_play_gif: false,
903 display_sensitive_media: false,
904 reduce_motion: false,
905 max_toot_chars: limit
908 delete_others_notice: !!user.info.is_moderator
912 default_privacy: user.info.default_scope,
913 default_sensitive: false
915 media_attachments: %{
916 accept_content_types: [
932 Map.get(user.info, :settings) ||
962 push_subscription: nil,
964 custom_emojis: mastodon_emoji,
971 |> render(MastodonView, "index.html", %{initial_state: initial_state})
974 |> redirect(to: "/web/login")
978 def put_settings(%{assigns: %{user: user}} = conn, %{"data" => settings} = _params) do
979 info_cng = User.Info.mastodon_settings_update(user.info, settings)
981 with changeset <- User.update_changeset(user),
982 changeset <- Ecto.Changeset.put_embed(changeset, :info, info_cng),
983 {:ok, _user} <- User.update_and_set_cache(changeset) do
987 json(conn, %{error: inspect(e)})
991 def login(conn, %{"code" => code}) do
992 with {:ok, app} <- get_or_make_app(),
993 %Authorization{} = auth <- Repo.get_by(Authorization, token: code, app_id: app.id),
994 {:ok, token} <- Token.exchange_token(app, auth) do
996 |> put_session(:oauth_token, token.token)
997 |> redirect(to: "/web/getting-started")
1001 def login(conn, _) do
1002 with {:ok, app} <- get_or_make_app() do
1004 o_auth_path(conn, :authorize,
1005 response_type: "code",
1006 client_id: app.client_id,
1012 |> redirect(to: path)
1016 defp get_or_make_app() do
1017 with %App{} = app <- Repo.get_by(App, client_name: "Mastodon-Local") do
1022 App.register_changeset(%App{}, %{
1023 client_name: "Mastodon-Local",
1025 scopes: "read,write,follow"
1032 def logout(conn, _) do
1035 |> redirect(to: "/")
1038 def relationship_noop(%{assigns: %{user: user}} = conn, %{"id" => id}) do
1039 Logger.debug("Unimplemented, returning unmodified relationship")
1041 with %User{} = target <- Repo.get(User, id) do
1042 render(conn, AccountView, "relationship.json", %{user: user, target: target})
1046 def empty_array(conn, _) do
1047 Logger.debug("Unimplemented, returning an empty array")
1051 def empty_object(conn, _) do
1052 Logger.debug("Unimplemented, returning an empty object")
1056 def render_notification(user, %{id: id, activity: activity, inserted_at: created_at} = _params) do
1057 actor = User.get_cached_by_ap_id(activity.data["actor"])
1058 parent_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
1059 mastodon_type = Activity.mastodon_notification_type(activity)
1063 type: mastodon_type,
1064 created_at: CommonAPI.Utils.to_masto_date(created_at),
1065 account: AccountView.render("account.json", %{user: actor, for: user})
1068 case mastodon_type do
1072 status: StatusView.render("status.json", %{activity: activity, for: user})
1078 status: StatusView.render("status.json", %{activity: parent_activity, for: user})
1084 status: StatusView.render("status.json", %{activity: parent_activity, for: user})
1095 def get_filters(%{assigns: %{user: user}} = conn, _) do
1096 filters = Pleroma.Filter.get_filters(user)
1097 res = FilterView.render("filters.json", filters: filters)
1102 %{assigns: %{user: user}} = conn,
1103 %{"phrase" => phrase, "context" => context} = params
1105 query = %Pleroma.Filter{
1109 hide: Map.get(params, "irreversible", nil),
1110 whole_word: Map.get(params, "boolean", true)
1114 {:ok, response} = Pleroma.Filter.create(query)
1115 res = FilterView.render("filter.json", filter: response)
1119 def get_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do
1120 filter = Pleroma.Filter.get(filter_id, user)
1121 res = FilterView.render("filter.json", filter: filter)
1126 %{assigns: %{user: user}} = conn,
1127 %{"phrase" => phrase, "context" => context, "id" => filter_id} = params
1129 query = %Pleroma.Filter{
1131 filter_id: filter_id,
1134 hide: Map.get(params, "irreversible", nil),
1135 whole_word: Map.get(params, "boolean", true)
1139 {:ok, response} = Pleroma.Filter.update(query)
1140 res = FilterView.render("filter.json", filter: response)
1144 def delete_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do
1145 query = %Pleroma.Filter{
1147 filter_id: filter_id
1150 {:ok, _} = Pleroma.Filter.delete(query)
1154 def create_push_subscription(%{assigns: %{user: user, token: token}} = conn, params) do
1155 true = Pleroma.Web.Push.enabled()
1156 Pleroma.Web.Push.Subscription.delete_if_exists(user, token)
1157 {:ok, subscription} = Pleroma.Web.Push.Subscription.create(user, token, params)
1158 view = PushSubscriptionView.render("push_subscription.json", subscription: subscription)
1162 def get_push_subscription(%{assigns: %{user: user, token: token}} = conn, _params) do
1163 true = Pleroma.Web.Push.enabled()
1164 subscription = Pleroma.Web.Push.Subscription.get(user, token)
1165 view = PushSubscriptionView.render("push_subscription.json", subscription: subscription)
1169 def update_push_subscription(
1170 %{assigns: %{user: user, token: token}} = conn,
1173 true = Pleroma.Web.Push.enabled()
1174 {:ok, subscription} = Pleroma.Web.Push.Subscription.update(user, token, params)
1175 view = PushSubscriptionView.render("push_subscription.json", subscription: subscription)
1179 def delete_push_subscription(%{assigns: %{user: user, token: token}} = conn, _params) do
1180 true = Pleroma.Web.Push.enabled()
1181 {:ok, _response} = Pleroma.Web.Push.Subscription.delete(user, token)
1185 def errors(conn, _) do
1188 |> json("Something went wrong")
1191 def suggestions(%{assigns: %{user: user}} = conn, _) do
1192 suggestions = Pleroma.Config.get(:suggestions)
1194 if Keyword.get(suggestions, :enabled, false) do
1195 api = Keyword.get(suggestions, :third_party_engine, "")
1196 timeout = Keyword.get(suggestions, :timeout, 5000)
1197 limit = Keyword.get(suggestions, :limit, 23)
1199 host = Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host])
1201 user = user.nickname
1202 url = String.replace(api, "{{host}}", host) |> String.replace("{{user}}", user)
1204 with {:ok, %{status: 200, body: body}} <-
1210 recv_timeout: timeout
1213 {:ok, data} <- Jason.decode(body) do
1215 Enum.slice(data, 0, limit)
1220 case User.get_or_fetch(x["acct"]) do
1227 Map.put(x, "avatar", MediaProxy.url(x["avatar"]))
1230 Map.put(x, "avatar_static", MediaProxy.url(x["avatar_static"]))
1236 e -> Logger.error("Could not retrieve suggestions at fetch #{url}, #{inspect(e)}")
1243 def try_render(conn, renderer, target, params)
1244 when is_binary(target) do
1245 res = render(conn, renderer, target, params)
1250 |> json(%{error: "Can't display this activity"})
1256 def try_render(conn, _, _, _) do
1259 |> json(%{error: "Can't display this activity"})