1 defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
2 use Pleroma.Web, :controller
3 alias Pleroma.{Repo, Object, Activity, User, Notification, Stats}
5 alias Pleroma.Web.MastodonAPI.{StatusView, AccountView, MastodonView, ListView, FilterView}
6 alias Pleroma.Web.ActivityPub.ActivityPub
7 alias Pleroma.Web.ActivityPub.Utils
8 alias Pleroma.Web.CommonAPI
9 alias Pleroma.Web.OAuth.{Authorization, Token, App}
10 alias Pleroma.Web.MediaProxy
15 @httpoison Application.get_env(:pleroma, :httpoison)
17 action_fallback(:errors)
19 def create_app(conn, params) do
20 with cs <- App.register_changeset(%App{}, params) |> IO.inspect(),
21 {:ok, app} <- Repo.insert(cs) |> IO.inspect() do
23 id: app.id |> to_string,
24 name: app.client_name,
25 client_id: app.client_id,
26 client_secret: app.client_secret,
27 redirect_uri: app.redirect_uris,
35 def update_credentials(%{assigns: %{user: user}} = conn, params) do
39 Application.get_env(:pleroma, :instance)
40 |> Keyword.fetch(:avatar_upload_limit)
43 Application.get_env(:pleroma, :instance)
44 |> Keyword.fetch(:banner_upload_limit)
47 if bio = params["note"] do
48 Map.put(params, "bio", bio)
54 if name = params["display_name"] do
55 Map.put(params, "name", name)
61 if avatar = params["avatar"] do
62 with %Plug.Upload{} <- avatar,
63 {:ok, object} <- ActivityPub.upload(avatar, avatar_upload_limit),
64 change = Ecto.Changeset.change(user, %{avatar: object.data}),
65 {:ok, user} = User.update_and_set_cache(change) do
75 if banner = params["header"] do
76 with %Plug.Upload{} <- banner,
77 {:ok, object} <- ActivityPub.upload(banner, banner_upload_limit),
78 new_info <- Map.put(user.info, "banner", object.data),
79 change <- User.info_changeset(user, %{info: new_info}),
80 {:ok, user} <- User.update_and_set_cache(change) do
90 if locked = params["locked"] do
91 with locked <- locked == "true",
92 new_info <- Map.put(user.info, "locked", locked),
93 change <- User.info_changeset(user, %{info: new_info}),
94 {:ok, user} <- User.update_and_set_cache(change) do
103 with changeset <- User.update_changeset(user, params),
104 {:ok, user} <- User.update_and_set_cache(changeset) do
105 if original_user != user do
106 CommonAPI.update(user)
109 json(conn, AccountView.render("account.json", %{user: user, for: user}))
114 |> json(%{error: "Invalid request"})
118 def verify_credentials(%{assigns: %{user: user}} = conn, _) do
119 account = AccountView.render("account.json", %{user: user, for: user})
123 def user(%{assigns: %{user: for_user}} = conn, %{"id" => id}) do
124 with %User{} = user <- Repo.get(User, id) do
125 account = AccountView.render("account.json", %{user: user, for: for_user})
131 |> json(%{error: "Can't find user"})
135 @mastodon_api_level "2.5.0"
137 def masto_instance(conn, _params) do
138 instance = Pleroma.Config.get(:instance)
142 title: Keyword.get(instance, :name),
143 description: Keyword.get(instance, :description),
144 version: "#{@mastodon_api_level} (compatible; #{Keyword.get(instance, :version)})",
145 email: Keyword.get(instance, :email),
147 streaming_api: String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws")
149 stats: Stats.get_stats(),
150 thumbnail: Web.base_url() <> "/instance/thumbnail.jpeg",
151 max_toot_chars: Keyword.get(instance, :limit)
157 def peers(conn, _params) do
158 json(conn, Stats.get_peers())
161 defp mastodonized_emoji do
162 Pleroma.Emoji.get_all()
163 |> Enum.map(fn {shortcode, relative_url} ->
164 url = to_string(URI.merge(Web.base_url(), relative_url))
167 "shortcode" => shortcode,
169 "visible_in_picker" => true,
175 def custom_emojis(conn, _params) do
176 mastodon_emoji = mastodonized_emoji()
177 json(conn, mastodon_emoji)
180 defp add_link_headers(conn, method, activities, param \\ nil, params \\ %{}) do
181 last = List.last(activities)
182 first = List.first(activities)
188 {next_url, prev_url} =
192 Pleroma.Web.Endpoint,
195 Map.merge(params, %{max_id: min})
198 Pleroma.Web.Endpoint,
201 Map.merge(params, %{since_id: max})
207 Pleroma.Web.Endpoint,
209 Map.merge(params, %{max_id: min})
212 Pleroma.Web.Endpoint,
214 Map.merge(params, %{since_id: max})
220 |> put_resp_header("link", "<#{next_url}>; rel=\"next\", <#{prev_url}>; rel=\"prev\"")
226 def home_timeline(%{assigns: %{user: user}} = conn, params) do
229 |> Map.put("type", ["Create", "Announce"])
230 |> Map.put("blocking_user", user)
231 |> Map.put("user", user)
234 ActivityPub.fetch_activities([user.ap_id | user.following], params)
235 |> ActivityPub.contain_timeline(user)
239 |> add_link_headers(:home_timeline, activities)
240 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
243 def public_timeline(%{assigns: %{user: user}} = conn, params) do
244 local_only = params["local"] in [true, "True", "true", "1"]
248 |> Map.put("type", ["Create", "Announce"])
249 |> Map.put("local_only", local_only)
250 |> Map.put("blocking_user", user)
253 ActivityPub.fetch_public_activities(params)
257 |> add_link_headers(:public_timeline, activities, false, %{"local" => local_only})
258 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
261 def user_statuses(%{assigns: %{user: reading_user}} = conn, params) do
262 with %User{} = user <- Repo.get(User, params["id"]) do
263 # Since Pleroma has no "pinned" posts feature, we'll just set an empty list here
265 if params["pinned"] == "true" do
268 ActivityPub.fetch_user_activities(user, reading_user, params)
272 |> add_link_headers(:user_statuses, activities, params["id"])
273 |> render(StatusView, "index.json", %{
274 activities: activities,
281 def dm_timeline(%{assigns: %{user: user}} = conn, _params) do
283 ActivityPub.fetch_activities_query([user.ap_id], %{"type" => "Create", visibility: "direct"})
285 activities = Repo.all(query)
288 |> add_link_headers(:dm_timeline, activities)
289 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
292 def get_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
293 with %Activity{} = activity <- Repo.get(Activity, id),
294 true <- ActivityPub.visible_for_user?(activity, user) do
295 try_render(conn, StatusView, "status.json", %{activity: activity, for: user})
299 def get_context(%{assigns: %{user: user}} = conn, %{"id" => id}) do
300 with %Activity{} = activity <- Repo.get(Activity, id),
302 ActivityPub.fetch_activities_for_context(activity.data["context"], %{
303 "blocking_user" => user,
307 activities |> Enum.filter(fn %{id: aid} -> to_string(aid) != to_string(id) end),
309 activities |> Enum.filter(fn %{data: %{"type" => type}} -> type == "Create" end),
310 grouped_activities <- Enum.group_by(activities, fn %{id: id} -> id < activity.id end) do
316 activities: grouped_activities[true] || [],
324 activities: grouped_activities[false] || [],
334 def post_status(conn, %{"status" => "", "media_ids" => media_ids} = params)
335 when length(media_ids) > 0 do
338 |> Map.put("status", ".")
340 post_status(conn, params)
343 def post_status(%{assigns: %{user: user}} = conn, %{"status" => _} = params) do
346 |> Map.put("in_reply_to_status_id", params["in_reply_to_id"])
347 |> Map.put("no_attachment_links", true)
350 case get_req_header(conn, "idempotency-key") do
352 _ -> Ecto.UUID.generate()
356 Cachex.fetch!(:idempotency_cache, idempotency_key, fn _ -> CommonAPI.post(user, params) end)
358 try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
361 def delete_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
362 with {:ok, %Activity{}} <- CommonAPI.delete(id, user) do
368 |> json(%{error: "Can't delete this post"})
372 def reblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
373 with {:ok, announce, _activity} <- CommonAPI.repeat(ap_id_or_id, user) do
374 try_render(conn, StatusView, "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_activity_by_object_ap_id(id) do
381 try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
385 def fav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
386 with {:ok, _fav, %{data: %{"id" => id}}} <- CommonAPI.favorite(ap_id_or_id, user),
387 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
388 try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
392 def unfav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
393 with {:ok, _, _, %{data: %{"id" => id}}} <- CommonAPI.unfavorite(ap_id_or_id, user),
394 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
395 try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
399 def notifications(%{assigns: %{user: user}} = conn, params) do
400 notifications = Notification.for_user(user, params)
403 Enum.map(notifications, fn x ->
404 render_notification(user, x)
409 |> add_link_headers(:notifications, notifications)
413 def get_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
414 with {:ok, notification} <- Notification.get(user, id) do
415 json(conn, render_notification(user, notification))
419 |> put_resp_content_type("application/json")
420 |> send_resp(403, Jason.encode!(%{"error" => reason}))
424 def clear_notifications(%{assigns: %{user: user}} = conn, _params) do
425 Notification.clear(user)
429 def dismiss_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
430 with {:ok, _notif} <- Notification.dismiss(user, id) do
435 |> put_resp_content_type("application/json")
436 |> send_resp(403, Jason.encode!(%{"error" => reason}))
440 def relationships(%{assigns: %{user: user}} = conn, %{"id" => id}) do
442 q = from(u in User, where: u.id in ^id)
443 targets = Repo.all(q)
444 render(conn, AccountView, "relationships.json", %{user: user, targets: targets})
447 # Instead of returning a 400 when no "id" params is present, Mastodon returns an empty array.
448 def relationships(%{assigns: %{user: user}} = conn, _) do
453 def update_media(%{assigns: %{user: _}} = conn, data) do
454 with %Object{} = object <- Repo.get(Object, data["id"]),
455 true <- is_binary(data["description"]),
456 description <- data["description"] do
457 new_data = %{object.data | "name" => description}
459 change = Object.change(object, %{data: new_data})
460 {:ok, _} = Repo.update(change)
464 |> Map.put("id", object.id)
466 render(conn, StatusView, "attachment.json", %{attachment: data})
470 def upload(%{assigns: %{user: _}} = conn, %{"file" => file} = data) do
471 with {:ok, object} <- ActivityPub.upload(file) do
473 if Map.has_key?(data, "description") do
474 Map.put(object.data, "name", data["description"])
479 change = Object.change(object, %{data: objdata})
480 {:ok, object} = Repo.update(change)
484 |> Map.put("id", object.id)
486 render(conn, StatusView, "attachment.json", %{attachment: objdata})
490 def favourited_by(conn, %{"id" => id}) do
491 with %Activity{data: %{"object" => %{"likes" => likes}}} <- Repo.get(Activity, id) do
492 q = from(u in User, where: u.ap_id in ^likes)
494 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
500 def reblogged_by(conn, %{"id" => id}) do
501 with %Activity{data: %{"object" => %{"announcements" => announces}}} <- Repo.get(Activity, id) do
502 q = from(u in User, where: u.ap_id in ^announces)
504 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
510 def hashtag_timeline(%{assigns: %{user: user}} = conn, params) do
511 local_only = params["local"] in [true, "True", "true", "1"]
515 |> Map.put("type", "Create")
516 |> Map.put("local_only", local_only)
517 |> Map.put("blocking_user", user)
518 |> Map.put("tag", String.downcase(params["tag"]))
521 ActivityPub.fetch_public_activities(params)
525 |> add_link_headers(:hashtag_timeline, activities, params["tag"], %{"local" => local_only})
526 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
530 def followers(conn, %{"id" => id}) do
531 with %User{} = user <- Repo.get(User, id),
532 {:ok, followers} <- User.get_followers(user) do
533 render(conn, AccountView, "accounts.json", %{users: followers, as: :user})
537 def following(conn, %{"id" => id}) do
538 with %User{} = user <- Repo.get(User, id),
539 {:ok, followers} <- User.get_friends(user) do
540 render(conn, AccountView, "accounts.json", %{users: followers, as: :user})
544 def follow_requests(%{assigns: %{user: followed}} = conn, _params) do
545 with {:ok, follow_requests} <- User.get_follow_requests(followed) do
546 render(conn, AccountView, "accounts.json", %{users: follow_requests, as: :user})
550 def authorize_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
551 with %User{} = follower <- Repo.get(User, id),
552 {:ok, follower} <- User.maybe_follow(follower, followed),
553 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
554 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "accept"),
556 ActivityPub.accept(%{
557 to: [follower.ap_id],
558 actor: followed.ap_id,
559 object: follow_activity.data["id"],
562 render(conn, AccountView, "relationship.json", %{user: followed, target: follower})
566 |> put_resp_content_type("application/json")
567 |> send_resp(403, Jason.encode!(%{"error" => message}))
571 def reject_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
572 with %User{} = follower <- Repo.get(User, id),
573 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
574 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "reject"),
576 ActivityPub.reject(%{
577 to: [follower.ap_id],
578 actor: followed.ap_id,
579 object: follow_activity.data["id"],
582 render(conn, AccountView, "relationship.json", %{user: followed, target: follower})
586 |> put_resp_content_type("application/json")
587 |> send_resp(403, Jason.encode!(%{"error" => message}))
591 def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
592 with %User{} = followed <- Repo.get(User, id),
593 {:ok, follower} <- User.maybe_direct_follow(follower, followed),
594 {:ok, _activity} <- ActivityPub.follow(follower, followed),
595 {:ok, follower, followed} <-
596 User.wait_and_refresh(
597 Pleroma.Config.get([:activitypub, :follow_handshake_timeout]),
601 render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
605 |> put_resp_content_type("application/json")
606 |> send_resp(403, Jason.encode!(%{"error" => message}))
610 def follow(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do
611 with %User{} = followed <- Repo.get_by(User, nickname: uri),
612 {:ok, follower} <- User.maybe_direct_follow(follower, followed),
613 {:ok, _activity} <- ActivityPub.follow(follower, followed) do
614 render(conn, AccountView, "account.json", %{user: followed, for: follower})
618 |> put_resp_content_type("application/json")
619 |> send_resp(403, Jason.encode!(%{"error" => message}))
623 def unfollow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
624 with %User{} = followed <- Repo.get(User, id),
625 {:ok, _activity} <- ActivityPub.unfollow(follower, followed),
626 {:ok, follower, _} <- User.unfollow(follower, followed) do
627 render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
631 def block(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
632 with %User{} = blocked <- Repo.get(User, id),
633 {:ok, blocker} <- User.block(blocker, blocked),
634 {:ok, _activity} <- ActivityPub.block(blocker, blocked) do
635 render(conn, AccountView, "relationship.json", %{user: blocker, target: blocked})
639 |> put_resp_content_type("application/json")
640 |> send_resp(403, Jason.encode!(%{"error" => message}))
644 def unblock(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
645 with %User{} = blocked <- Repo.get(User, id),
646 {:ok, blocker} <- User.unblock(blocker, blocked),
647 {:ok, _activity} <- ActivityPub.unblock(blocker, blocked) do
648 render(conn, AccountView, "relationship.json", %{user: blocker, target: blocked})
652 |> put_resp_content_type("application/json")
653 |> send_resp(403, Jason.encode!(%{"error" => message}))
657 # TODO: Use proper query
658 def blocks(%{assigns: %{user: user}} = conn, _) do
659 with blocked_users <- user.info["blocks"] || [],
660 accounts <- Enum.map(blocked_users, fn ap_id -> User.get_cached_by_ap_id(ap_id) end) do
661 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
666 def domain_blocks(%{assigns: %{user: %{info: info}}} = conn, _) do
667 json(conn, info["domain_blocks"] || [])
670 def block_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
671 User.block_domain(blocker, domain)
675 def unblock_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
676 User.unblock_domain(blocker, domain)
680 def status_search(query) do
682 if Regex.match?(~r/https?:/, query) do
683 with {:ok, object} <- ActivityPub.fetch_object_from_id(query) do
684 [Activity.get_create_activity_by_object_ap_id(object.data["id"])]
693 where: fragment("?->>'type' = 'Create'", a.data),
694 where: "https://www.w3.org/ns/activitystreams#Public" in a.recipients,
697 "to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)",
702 order_by: [desc: :id]
705 Repo.all(q) ++ fetched
708 def search2(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
709 accounts = User.search(query, params["resolve"] == "true")
711 statuses = status_search(query)
713 tags_path = Web.base_url() <> "/tag/"
718 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
719 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
720 |> Enum.map(fn tag -> %{name: tag, url: tags_path <> tag} end)
723 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
725 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
732 def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
733 accounts = User.search(query, params["resolve"] == "true")
735 statuses = status_search(query)
740 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
741 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
744 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
746 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
753 def account_search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
754 accounts = User.search(query, params["resolve"] == "true")
756 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
761 def favourites(%{assigns: %{user: user}} = conn, _) do
764 |> Map.put("type", "Create")
765 |> Map.put("favorited_by", user.ap_id)
766 |> Map.put("blocking_user", user)
769 ActivityPub.fetch_public_activities(params)
773 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
776 def get_lists(%{assigns: %{user: user}} = conn, opts) do
777 lists = Pleroma.List.for_user(user, opts)
778 res = ListView.render("lists.json", lists: lists)
782 def get_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
783 with %Pleroma.List{} = list <- Pleroma.List.get(id, user) do
784 res = ListView.render("list.json", list: list)
787 _e -> json(conn, "error")
791 def account_lists(%{assigns: %{user: user}} = conn, %{"id" => account_id}) do
792 lists = Pleroma.List.get_lists_account_belongs(user, account_id)
793 res = ListView.render("lists.json", lists: lists)
797 def delete_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
798 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
799 {:ok, _list} <- Pleroma.List.delete(list) do
807 def create_list(%{assigns: %{user: user}} = conn, %{"title" => title}) do
808 with {:ok, %Pleroma.List{} = list} <- Pleroma.List.create(title, user) do
809 res = ListView.render("list.json", list: list)
814 def add_to_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
816 |> Enum.each(fn account_id ->
817 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
818 %User{} = followed <- Repo.get(User, account_id) do
819 Pleroma.List.follow(list, followed)
826 def remove_from_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
828 |> Enum.each(fn account_id ->
829 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
830 %User{} = followed <- Repo.get(Pleroma.User, account_id) do
831 Pleroma.List.unfollow(list, followed)
838 def list_accounts(%{assigns: %{user: user}} = conn, %{"id" => id}) do
839 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
840 {:ok, users} = Pleroma.List.get_following(list) do
841 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
845 def rename_list(%{assigns: %{user: user}} = conn, %{"id" => id, "title" => title}) do
846 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
847 {:ok, list} <- Pleroma.List.rename(list, title) do
848 res = ListView.render("list.json", list: list)
856 def list_timeline(%{assigns: %{user: user}} = conn, %{"list_id" => id} = params) do
857 with %Pleroma.List{title: title, following: following} <- Pleroma.List.get(id, user) do
860 |> Map.put("type", "Create")
861 |> Map.put("blocking_user", user)
863 # we must filter the following list for the user to avoid leaking statuses the user
864 # does not actually have permission to see (for more info, peruse security issue #270).
867 |> Enum.filter(fn x -> x in user.following end)
870 ActivityPub.fetch_activities_bounded(following_to, following, params)
874 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
879 |> json(%{error: "Error."})
883 def index(%{assigns: %{user: user}} = conn, _params) do
886 |> get_session(:oauth_token)
889 mastodon_emoji = mastodonized_emoji()
891 limit = Pleroma.Config.get([:instance, :limit])
894 Map.put(%{}, user.id, AccountView.render("account.json", %{user: user, for: user}))
899 streaming_api_base_url:
900 String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws"),
903 domain: Pleroma.Web.Endpoint.host(),
906 unfollow_modal: false,
909 auto_play_gif: false,
910 display_sensitive_media: false,
911 reduce_motion: false,
912 max_toot_chars: limit
915 delete_others_notice: !!user.info["is_moderator"]
919 default_privacy: user.info["default_scope"] || "public",
920 default_sensitive: false
922 media_attachments: %{
923 accept_content_types: [
939 Map.get(user.info, "settings") ||
969 push_subscription: nil,
971 custom_emojis: mastodon_emoji,
978 |> render(MastodonView, "index.html", %{initial_state: initial_state})
981 |> redirect(to: "/web/login")
985 def put_settings(%{assigns: %{user: user}} = conn, %{"data" => settings} = _params) do
986 with new_info <- Map.put(user.info, "settings", settings),
987 change <- User.info_changeset(user, %{info: new_info}),
988 {:ok, _user} <- User.update_and_set_cache(change) do
994 |> json(%{error: inspect(e)})
998 def login(conn, %{"code" => code}) do
999 with {:ok, app} <- get_or_make_app(),
1000 %Authorization{} = auth <- Repo.get_by(Authorization, token: code, app_id: app.id),
1001 {:ok, token} <- Token.exchange_token(app, auth) do
1003 |> put_session(:oauth_token, token.token)
1004 |> redirect(to: "/web/getting-started")
1008 def login(conn, _) do
1009 with {:ok, app} <- get_or_make_app() do
1011 o_auth_path(conn, :authorize,
1012 response_type: "code",
1013 client_id: app.client_id,
1019 |> redirect(to: path)
1023 defp get_or_make_app() do
1024 with %App{} = app <- Repo.get_by(App, client_name: "Mastodon-Local") do
1029 App.register_changeset(%App{}, %{
1030 client_name: "Mastodon-Local",
1032 scopes: "read,write,follow"
1039 def logout(conn, _) do
1042 |> redirect(to: "/")
1045 def relationship_noop(%{assigns: %{user: user}} = conn, %{"id" => id}) do
1046 Logger.debug("Unimplemented, returning unmodified relationship")
1048 with %User{} = target <- Repo.get(User, id) do
1049 render(conn, AccountView, "relationship.json", %{user: user, target: target})
1053 def empty_array(conn, _) do
1054 Logger.debug("Unimplemented, returning an empty array")
1058 def empty_object(conn, _) do
1059 Logger.debug("Unimplemented, returning an empty object")
1063 def render_notification(user, %{id: id, activity: activity, inserted_at: created_at} = _params) do
1064 actor = User.get_cached_by_ap_id(activity.data["actor"])
1067 NaiveDateTime.to_iso8601(created_at)
1068 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
1070 id = id |> to_string
1072 case activity.data["type"] do
1077 created_at: created_at,
1078 account: AccountView.render("account.json", %{user: actor, for: user}),
1079 status: StatusView.render("status.json", %{activity: activity, for: user})
1083 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
1088 created_at: created_at,
1089 account: AccountView.render("account.json", %{user: actor, for: user}),
1090 status: StatusView.render("status.json", %{activity: liked_activity, for: user})
1094 announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
1099 created_at: created_at,
1100 account: AccountView.render("account.json", %{user: actor, for: user}),
1101 status: StatusView.render("status.json", %{activity: announced_activity, for: user})
1108 created_at: created_at,
1109 account: AccountView.render("account.json", %{user: actor, for: user})
1117 def get_filters(%{assigns: %{user: user}} = conn, _) do
1118 filters = Pleroma.Filter.get_filters(user)
1119 res = FilterView.render("filters.json", filters: filters)
1124 %{assigns: %{user: user}} = conn,
1125 %{"phrase" => phrase, "context" => context} = params
1127 query = %Pleroma.Filter{
1131 hide: Map.get(params, "irreversible", nil),
1132 whole_word: Map.get(params, "boolean", true)
1136 {:ok, response} = Pleroma.Filter.create(query)
1137 res = FilterView.render("filter.json", filter: response)
1141 def get_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do
1142 filter = Pleroma.Filter.get(filter_id, user)
1143 res = FilterView.render("filter.json", filter: filter)
1148 %{assigns: %{user: user}} = conn,
1149 %{"phrase" => phrase, "context" => context, "id" => filter_id} = params
1151 query = %Pleroma.Filter{
1153 filter_id: filter_id,
1156 hide: Map.get(params, "irreversible", nil),
1157 whole_word: Map.get(params, "boolean", true)
1161 {:ok, response} = Pleroma.Filter.update(query)
1162 res = FilterView.render("filter.json", filter: response)
1166 def delete_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do
1167 query = %Pleroma.Filter{
1169 filter_id: filter_id
1172 {:ok, _} = Pleroma.Filter.delete(query)
1176 def errors(conn, _) do
1179 |> json("Something went wrong")
1182 def suggestions(%{assigns: %{user: user}} = conn, _) do
1183 suggestions = Pleroma.Config.get(:suggestions)
1185 if Keyword.get(suggestions, :enabled, false) do
1186 api = Keyword.get(suggestions, :third_party_engine, "")
1187 timeout = Keyword.get(suggestions, :timeout, 5000)
1188 limit = Keyword.get(suggestions, :limit, 23)
1190 host = Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host])
1192 user = user.nickname
1193 url = String.replace(api, "{{host}}", host) |> String.replace("{{user}}", user)
1195 with {:ok, %{status_code: 200, body: body}} <-
1196 @httpoison.get(url, [], timeout: timeout, recv_timeout: timeout),
1197 {:ok, data} <- Jason.decode(body) do
1199 Enum.slice(data, 0, limit)
1204 case User.get_or_fetch(x["acct"]) do
1211 Map.put(x, "avatar", MediaProxy.url(x["avatar"]))
1214 Map.put(x, "avatar_static", MediaProxy.url(x["avatar_static"]))
1220 e -> Logger.error("Could not retrieve suggestions at fetch #{url}, #{inspect(e)}")
1227 def try_render(conn, renderer, target, params)
1228 when is_binary(target) do
1229 res = render(conn, renderer, target, params)
1234 |> json(%{error: "Can't display this activity"})
1240 def try_render(conn, _, _, _) do
1243 |> json(%{error: "Can't display this activity"})