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(
285 Map.merge(params, %{"type" => "Create", visibility: "direct"})
288 activities = Repo.all(query)
291 |> add_link_headers(:dm_timeline, activities)
292 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
295 def get_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
296 with %Activity{} = activity <- Repo.get(Activity, id),
297 true <- ActivityPub.visible_for_user?(activity, user) do
298 try_render(conn, StatusView, "status.json", %{activity: activity, for: user})
302 def get_context(%{assigns: %{user: user}} = conn, %{"id" => id}) do
303 with %Activity{} = activity <- Repo.get(Activity, id),
305 ActivityPub.fetch_activities_for_context(activity.data["context"], %{
306 "blocking_user" => user,
310 activities |> Enum.filter(fn %{id: aid} -> to_string(aid) != to_string(id) end),
312 activities |> Enum.filter(fn %{data: %{"type" => type}} -> type == "Create" end),
313 grouped_activities <- Enum.group_by(activities, fn %{id: id} -> id < activity.id end) do
319 activities: grouped_activities[true] || [],
327 activities: grouped_activities[false] || [],
337 def post_status(conn, %{"status" => "", "media_ids" => media_ids} = params)
338 when length(media_ids) > 0 do
341 |> Map.put("status", ".")
343 post_status(conn, params)
346 def post_status(%{assigns: %{user: user}} = conn, %{"status" => _} = params) do
349 |> Map.put("in_reply_to_status_id", params["in_reply_to_id"])
350 |> Map.put("no_attachment_links", true)
353 case get_req_header(conn, "idempotency-key") do
355 _ -> Ecto.UUID.generate()
359 Cachex.fetch!(:idempotency_cache, idempotency_key, fn _ -> CommonAPI.post(user, params) end)
361 try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
364 def delete_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
365 with {:ok, %Activity{}} <- CommonAPI.delete(id, user) do
371 |> json(%{error: "Can't delete this post"})
375 def reblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
376 with {:ok, announce, _activity} <- CommonAPI.repeat(ap_id_or_id, user) do
377 try_render(conn, StatusView, "status.json", %{activity: announce, for: user, as: :activity})
381 def unreblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
382 with {:ok, _unannounce, %{data: %{"id" => id}}} <- CommonAPI.unrepeat(ap_id_or_id, user),
383 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
384 try_render(conn, StatusView, "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
391 try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
395 def unfav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
396 with {:ok, _, _, %{data: %{"id" => id}}} <- CommonAPI.unfavorite(ap_id_or_id, user),
397 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
398 try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
402 def notifications(%{assigns: %{user: user}} = conn, params) do
403 notifications = Notification.for_user(user, params)
406 Enum.map(notifications, fn x ->
407 render_notification(user, x)
412 |> add_link_headers(:notifications, notifications)
416 def get_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
417 with {:ok, notification} <- Notification.get(user, id) do
418 json(conn, render_notification(user, notification))
422 |> put_resp_content_type("application/json")
423 |> send_resp(403, Jason.encode!(%{"error" => reason}))
427 def clear_notifications(%{assigns: %{user: user}} = conn, _params) do
428 Notification.clear(user)
432 def dismiss_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
433 with {:ok, _notif} <- Notification.dismiss(user, id) do
438 |> put_resp_content_type("application/json")
439 |> send_resp(403, Jason.encode!(%{"error" => reason}))
443 def relationships(%{assigns: %{user: user}} = conn, %{"id" => id}) do
445 q = from(u in User, where: u.id in ^id)
446 targets = Repo.all(q)
447 render(conn, AccountView, "relationships.json", %{user: user, targets: targets})
450 # Instead of returning a 400 when no "id" params is present, Mastodon returns an empty array.
451 def relationships(%{assigns: %{user: user}} = conn, _) do
456 def update_media(%{assigns: %{user: _}} = conn, data) do
457 with %Object{} = object <- Repo.get(Object, data["id"]),
458 true <- is_binary(data["description"]),
459 description <- data["description"] do
460 new_data = %{object.data | "name" => description}
462 change = Object.change(object, %{data: new_data})
463 {:ok, _} = Repo.update(change)
467 |> Map.put("id", object.id)
469 render(conn, StatusView, "attachment.json", %{attachment: data})
473 def upload(%{assigns: %{user: _}} = conn, %{"file" => file} = data) do
474 with {:ok, object} <- ActivityPub.upload(file) do
476 if Map.has_key?(data, "description") do
477 Map.put(object.data, "name", data["description"])
482 change = Object.change(object, %{data: objdata})
483 {:ok, object} = Repo.update(change)
487 |> Map.put("id", object.id)
489 render(conn, StatusView, "attachment.json", %{attachment: objdata})
493 def favourited_by(conn, %{"id" => id}) do
494 with %Activity{data: %{"object" => %{"likes" => likes}}} <- Repo.get(Activity, id) do
495 q = from(u in User, where: u.ap_id in ^likes)
497 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
503 def reblogged_by(conn, %{"id" => id}) do
504 with %Activity{data: %{"object" => %{"announcements" => announces}}} <- Repo.get(Activity, id) do
505 q = from(u in User, where: u.ap_id in ^announces)
507 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
513 def hashtag_timeline(%{assigns: %{user: user}} = conn, params) do
514 local_only = params["local"] in [true, "True", "true", "1"]
518 |> Map.put("type", "Create")
519 |> Map.put("local_only", local_only)
520 |> Map.put("blocking_user", user)
521 |> Map.put("tag", String.downcase(params["tag"]))
524 ActivityPub.fetch_public_activities(params)
528 |> add_link_headers(:hashtag_timeline, activities, params["tag"], %{"local" => local_only})
529 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
533 def followers(conn, %{"id" => id}) do
534 with %User{} = user <- Repo.get(User, id),
535 {:ok, followers} <- User.get_followers(user) do
536 render(conn, AccountView, "accounts.json", %{users: followers, as: :user})
540 def following(conn, %{"id" => id}) do
541 with %User{} = user <- Repo.get(User, id),
542 {:ok, followers} <- User.get_friends(user) do
543 render(conn, AccountView, "accounts.json", %{users: followers, as: :user})
547 def follow_requests(%{assigns: %{user: followed}} = conn, _params) do
548 with {:ok, follow_requests} <- User.get_follow_requests(followed) do
549 render(conn, AccountView, "accounts.json", %{users: follow_requests, as: :user})
553 def authorize_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
554 with %User{} = follower <- Repo.get(User, id),
555 {:ok, follower} <- User.maybe_follow(follower, followed),
556 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
557 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "accept"),
559 ActivityPub.accept(%{
560 to: [follower.ap_id],
561 actor: followed.ap_id,
562 object: follow_activity.data["id"],
565 render(conn, AccountView, "relationship.json", %{user: followed, target: follower})
569 |> put_resp_content_type("application/json")
570 |> send_resp(403, Jason.encode!(%{"error" => message}))
574 def reject_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
575 with %User{} = follower <- Repo.get(User, id),
576 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
577 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "reject"),
579 ActivityPub.reject(%{
580 to: [follower.ap_id],
581 actor: followed.ap_id,
582 object: follow_activity.data["id"],
585 render(conn, AccountView, "relationship.json", %{user: followed, target: follower})
589 |> put_resp_content_type("application/json")
590 |> send_resp(403, Jason.encode!(%{"error" => message}))
594 def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
595 with %User{} = followed <- Repo.get(User, id),
596 {:ok, follower} <- User.maybe_direct_follow(follower, followed),
597 {:ok, _activity} <- ActivityPub.follow(follower, followed),
598 {:ok, follower, followed} <-
599 User.wait_and_refresh(
600 Pleroma.Config.get([:activitypub, :follow_handshake_timeout]),
604 render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
608 |> put_resp_content_type("application/json")
609 |> send_resp(403, Jason.encode!(%{"error" => message}))
613 def follow(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do
614 with %User{} = followed <- Repo.get_by(User, nickname: uri),
615 {:ok, follower} <- User.maybe_direct_follow(follower, followed),
616 {:ok, _activity} <- ActivityPub.follow(follower, followed) do
617 render(conn, AccountView, "account.json", %{user: followed, for: follower})
621 |> put_resp_content_type("application/json")
622 |> send_resp(403, Jason.encode!(%{"error" => message}))
626 def unfollow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
627 with %User{} = followed <- Repo.get(User, id),
628 {:ok, _activity} <- ActivityPub.unfollow(follower, followed),
629 {:ok, follower, _} <- User.unfollow(follower, followed) do
630 render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
634 def block(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
635 with %User{} = blocked <- Repo.get(User, id),
636 {:ok, blocker} <- User.block(blocker, blocked),
637 {:ok, _activity} <- ActivityPub.block(blocker, blocked) do
638 render(conn, AccountView, "relationship.json", %{user: blocker, target: blocked})
642 |> put_resp_content_type("application/json")
643 |> send_resp(403, Jason.encode!(%{"error" => message}))
647 def unblock(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
648 with %User{} = blocked <- Repo.get(User, id),
649 {:ok, blocker} <- User.unblock(blocker, blocked),
650 {:ok, _activity} <- ActivityPub.unblock(blocker, blocked) do
651 render(conn, AccountView, "relationship.json", %{user: blocker, target: blocked})
655 |> put_resp_content_type("application/json")
656 |> send_resp(403, Jason.encode!(%{"error" => message}))
660 # TODO: Use proper query
661 def blocks(%{assigns: %{user: user}} = conn, _) do
662 with blocked_users <- user.info.blocks || [],
663 accounts <- Enum.map(blocked_users, fn ap_id -> User.get_cached_by_ap_id(ap_id) end) do
664 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
669 def domain_blocks(%{assigns: %{user: %{info: info}}} = conn, _) do
670 json(conn, info.domain_blocks || [])
673 def block_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
674 User.block_domain(blocker, domain)
678 def unblock_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
679 User.unblock_domain(blocker, domain)
683 def status_search(query) do
685 if Regex.match?(~r/https?:/, query) do
686 with {:ok, object} <- ActivityPub.fetch_object_from_id(query) do
687 [Activity.get_create_activity_by_object_ap_id(object.data["id"])]
696 where: fragment("?->>'type' = 'Create'", a.data),
697 where: "https://www.w3.org/ns/activitystreams#Public" in a.recipients,
700 "to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)",
705 order_by: [desc: :id]
708 Repo.all(q) ++ fetched
711 def search2(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
712 accounts = User.search(query, params["resolve"] == "true")
714 statuses = status_search(query)
716 tags_path = Web.base_url() <> "/tag/"
721 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
722 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
723 |> Enum.map(fn tag -> %{name: tag, url: tags_path <> tag} end)
726 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
728 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
735 def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
736 accounts = User.search(query, params["resolve"] == "true")
738 statuses = status_search(query)
743 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
744 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
747 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
749 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
756 def account_search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
757 accounts = User.search(query, params["resolve"] == "true")
759 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
764 def favourites(%{assigns: %{user: user}} = conn, _) do
767 |> Map.put("type", "Create")
768 |> Map.put("favorited_by", user.ap_id)
769 |> Map.put("blocking_user", user)
772 ActivityPub.fetch_public_activities(params)
776 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
779 def get_lists(%{assigns: %{user: user}} = conn, opts) do
780 lists = Pleroma.List.for_user(user, opts)
781 res = ListView.render("lists.json", lists: lists)
785 def get_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
786 with %Pleroma.List{} = list <- Pleroma.List.get(id, user) do
787 res = ListView.render("list.json", list: list)
790 _e -> json(conn, "error")
794 def account_lists(%{assigns: %{user: user}} = conn, %{"id" => account_id}) do
795 lists = Pleroma.List.get_lists_account_belongs(user, account_id)
796 res = ListView.render("lists.json", lists: lists)
800 def delete_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
801 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
802 {:ok, _list} <- Pleroma.List.delete(list) do
810 def create_list(%{assigns: %{user: user}} = conn, %{"title" => title}) do
811 with {:ok, %Pleroma.List{} = list} <- Pleroma.List.create(title, user) do
812 res = ListView.render("list.json", list: list)
817 def add_to_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
819 |> Enum.each(fn account_id ->
820 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
821 %User{} = followed <- Repo.get(User, account_id) do
822 Pleroma.List.follow(list, followed)
829 def remove_from_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
831 |> Enum.each(fn account_id ->
832 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
833 %User{} = followed <- Repo.get(Pleroma.User, account_id) do
834 Pleroma.List.unfollow(list, followed)
841 def list_accounts(%{assigns: %{user: user}} = conn, %{"id" => id}) do
842 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
843 {:ok, users} = Pleroma.List.get_following(list) do
844 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
848 def rename_list(%{assigns: %{user: user}} = conn, %{"id" => id, "title" => title}) do
849 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
850 {:ok, list} <- Pleroma.List.rename(list, title) do
851 res = ListView.render("list.json", list: list)
859 def list_timeline(%{assigns: %{user: user}} = conn, %{"list_id" => id} = params) do
860 with %Pleroma.List{title: title, following: following} <- Pleroma.List.get(id, user) do
863 |> Map.put("type", "Create")
864 |> Map.put("blocking_user", user)
866 # we must filter the following list for the user to avoid leaking statuses the user
867 # does not actually have permission to see (for more info, peruse security issue #270).
870 |> Enum.filter(fn x -> x in user.following end)
873 ActivityPub.fetch_activities_bounded(following_to, following, params)
877 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
882 |> json(%{error: "Error."})
886 def index(%{assigns: %{user: user}} = conn, _params) do
889 |> get_session(:oauth_token)
892 mastodon_emoji = mastodonized_emoji()
894 limit = Pleroma.Config.get([:instance, :limit])
897 Map.put(%{}, user.id, AccountView.render("account.json", %{user: user, for: user}))
902 streaming_api_base_url:
903 String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws"),
906 domain: Pleroma.Web.Endpoint.host(),
909 unfollow_modal: false,
912 auto_play_gif: false,
913 display_sensitive_media: false,
914 reduce_motion: false,
915 max_toot_chars: limit
918 delete_others_notice: !!user.info.is_moderator
922 default_privacy: user.info.default_scope,
923 default_sensitive: false
925 media_attachments: %{
926 accept_content_types: [
942 Map.get(user.info, :settings) ||
972 push_subscription: nil,
974 custom_emojis: mastodon_emoji,
981 |> render(MastodonView, "index.html", %{initial_state: initial_state})
984 |> redirect(to: "/web/login")
988 def put_settings(%{assigns: %{user: user}} = conn, %{"data" => settings} = _params) do
989 with new_info <- Map.put(user.info, "settings", settings),
990 change <- User.info_changeset(user, %{info: new_info}),
991 {:ok, _user} <- User.update_and_set_cache(change) do
997 |> json(%{error: inspect(e)})
1001 def login(conn, %{"code" => code}) do
1002 with {:ok, app} <- get_or_make_app(),
1003 %Authorization{} = auth <- Repo.get_by(Authorization, token: code, app_id: app.id),
1004 {:ok, token} <- Token.exchange_token(app, auth) do
1006 |> put_session(:oauth_token, token.token)
1007 |> redirect(to: "/web/getting-started")
1011 def login(conn, _) do
1012 with {:ok, app} <- get_or_make_app() do
1014 o_auth_path(conn, :authorize,
1015 response_type: "code",
1016 client_id: app.client_id,
1022 |> redirect(to: path)
1026 defp get_or_make_app() do
1027 with %App{} = app <- Repo.get_by(App, client_name: "Mastodon-Local") do
1032 App.register_changeset(%App{}, %{
1033 client_name: "Mastodon-Local",
1035 scopes: "read,write,follow"
1042 def logout(conn, _) do
1045 |> redirect(to: "/")
1048 def relationship_noop(%{assigns: %{user: user}} = conn, %{"id" => id}) do
1049 Logger.debug("Unimplemented, returning unmodified relationship")
1051 with %User{} = target <- Repo.get(User, id) do
1052 render(conn, AccountView, "relationship.json", %{user: user, target: target})
1056 def empty_array(conn, _) do
1057 Logger.debug("Unimplemented, returning an empty array")
1061 def empty_object(conn, _) do
1062 Logger.debug("Unimplemented, returning an empty object")
1066 def render_notification(user, %{id: id, activity: activity, inserted_at: created_at} = _params) do
1067 actor = User.get_cached_by_ap_id(activity.data["actor"])
1070 NaiveDateTime.to_iso8601(created_at)
1071 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
1073 id = id |> to_string
1075 case activity.data["type"] do
1080 created_at: created_at,
1081 account: AccountView.render("account.json", %{user: actor, for: user}),
1082 status: StatusView.render("status.json", %{activity: activity, for: user})
1086 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
1091 created_at: created_at,
1092 account: AccountView.render("account.json", %{user: actor, for: user}),
1093 status: StatusView.render("status.json", %{activity: liked_activity, for: user})
1097 announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
1102 created_at: created_at,
1103 account: AccountView.render("account.json", %{user: actor, for: user}),
1104 status: StatusView.render("status.json", %{activity: announced_activity, for: user})
1111 created_at: created_at,
1112 account: AccountView.render("account.json", %{user: actor, for: user})
1120 def get_filters(%{assigns: %{user: user}} = conn, _) do
1121 filters = Pleroma.Filter.get_filters(user)
1122 res = FilterView.render("filters.json", filters: filters)
1127 %{assigns: %{user: user}} = conn,
1128 %{"phrase" => phrase, "context" => context} = params
1130 query = %Pleroma.Filter{
1134 hide: Map.get(params, "irreversible", nil),
1135 whole_word: Map.get(params, "boolean", true)
1139 {:ok, response} = Pleroma.Filter.create(query)
1140 res = FilterView.render("filter.json", filter: response)
1144 def get_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do
1145 filter = Pleroma.Filter.get(filter_id, user)
1146 res = FilterView.render("filter.json", filter: filter)
1151 %{assigns: %{user: user}} = conn,
1152 %{"phrase" => phrase, "context" => context, "id" => filter_id} = params
1154 query = %Pleroma.Filter{
1156 filter_id: filter_id,
1159 hide: Map.get(params, "irreversible", nil),
1160 whole_word: Map.get(params, "boolean", true)
1164 {:ok, response} = Pleroma.Filter.update(query)
1165 res = FilterView.render("filter.json", filter: response)
1169 def delete_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do
1170 query = %Pleroma.Filter{
1172 filter_id: filter_id
1175 {:ok, _} = Pleroma.Filter.delete(query)
1179 def errors(conn, _) do
1182 |> json("Something went wrong")
1185 def suggestions(%{assigns: %{user: user}} = conn, _) do
1186 suggestions = Pleroma.Config.get(:suggestions)
1188 if Keyword.get(suggestions, :enabled, false) do
1189 api = Keyword.get(suggestions, :third_party_engine, "")
1190 timeout = Keyword.get(suggestions, :timeout, 5000)
1191 limit = Keyword.get(suggestions, :limit, 23)
1193 host = Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host])
1195 user = user.nickname
1196 url = String.replace(api, "{{host}}", host) |> String.replace("{{user}}", user)
1198 with {:ok, %{status_code: 200, body: body}} <-
1199 @httpoison.get(url, [], timeout: timeout, recv_timeout: timeout),
1200 {:ok, data} <- Jason.decode(body) do
1202 Enum.slice(data, 0, limit)
1207 case User.get_or_fetch(x["acct"]) do
1214 Map.put(x, "avatar", MediaProxy.url(x["avatar"]))
1217 Map.put(x, "avatar_static", MediaProxy.url(x["avatar_static"]))
1223 e -> Logger.error("Could not retrieve suggestions at fetch #{url}, #{inspect(e)}")
1230 def try_render(conn, renderer, target, params)
1231 when is_binary(target) do
1232 res = render(conn, renderer, target, params)
1237 |> json(%{error: "Can't display this activity"})
1243 def try_render(conn, _, _, _) do
1246 |> json(%{error: "Can't display this activity"})