1 defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
2 use Pleroma.Web, :controller
3 alias Pleroma.{Repo, Object, Activity, User, Notification, Stats}
4 alias Pleroma.Object.Fetcher
6 alias Pleroma.Web.MastodonAPI.{StatusView, AccountView, MastodonView, ListView, FilterView}
7 alias Pleroma.Web.ActivityPub.ActivityPub
8 alias Pleroma.Web.ActivityPub.Utils
9 alias Pleroma.Web.CommonAPI
10 alias Pleroma.Web.OAuth.{Authorization, Token, App}
11 alias Pleroma.Web.MediaProxy
16 @httpoison Application.get_env(:pleroma, :httpoison)
18 action_fallback(:errors)
20 def create_app(conn, params) do
21 with cs <- App.register_changeset(%App{}, params) |> IO.inspect(),
22 {:ok, app} <- Repo.insert(cs) |> IO.inspect() do
24 id: app.id |> to_string,
25 name: app.client_name,
26 client_id: app.client_id,
27 client_secret: app.client_secret,
28 redirect_uri: app.redirect_uris,
41 value_function \\ fn x -> {:ok, x} end
43 if Map.has_key?(params, params_field) do
44 case value_function.(params[params_field]) do
45 {:ok, new_value} -> Map.put(map, map_field, new_value)
53 def update_credentials(%{assigns: %{user: user}} = conn, params) do
58 |> add_if_present(params, "display_name", :name)
59 |> add_if_present(params, "note", :bio, fn value -> {:ok, User.parse_bio(value)} end)
60 |> add_if_present(params, "avatar", :avatar, fn value ->
61 with %Plug.Upload{} <- value,
62 {:ok, object} <- ActivityPub.upload(value, type: :avatar) do
71 |> add_if_present(params, "locked", :locked, fn value -> {:ok, value == "true"} end)
72 |> add_if_present(params, "header", :banner, fn value ->
73 with %Plug.Upload{} <- value,
74 {:ok, object} <- ActivityPub.upload(value, type: :banner) do
81 info_cng = User.Info.mastodon_profile_update(user.info, info_params)
83 with changeset <- User.update_changeset(user, user_params),
84 changeset <- Ecto.Changeset.put_embed(changeset, :info, info_cng),
85 {:ok, user} <- User.update_and_set_cache(changeset) do
86 if original_user != user do
87 CommonAPI.update(user)
90 json(conn, AccountView.render("account.json", %{user: user, for: user}))
95 |> json(%{error: "Invalid request"})
99 def verify_credentials(%{assigns: %{user: user}} = conn, _) do
100 account = AccountView.render("account.json", %{user: user, for: user})
104 def user(%{assigns: %{user: for_user}} = conn, %{"id" => id}) do
105 with %User{} = user <- Repo.get(User, id) do
106 account = AccountView.render("account.json", %{user: user, for: for_user})
112 |> json(%{error: "Can't find user"})
116 @mastodon_api_level "2.5.0"
118 def masto_instance(conn, _params) do
119 instance = Pleroma.Config.get(:instance)
123 title: Keyword.get(instance, :name),
124 description: Keyword.get(instance, :description),
125 version: "#{@mastodon_api_level} (compatible; #{Pleroma.Application.named_version()})",
126 email: Keyword.get(instance, :email),
128 streaming_api: String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws")
130 stats: Stats.get_stats(),
131 thumbnail: Web.base_url() <> "/instance/thumbnail.jpeg",
132 max_toot_chars: Keyword.get(instance, :limit)
138 def peers(conn, _params) do
139 json(conn, Stats.get_peers())
142 defp mastodonized_emoji do
143 Pleroma.Emoji.get_all()
144 |> Enum.map(fn {shortcode, relative_url} ->
145 url = to_string(URI.merge(Web.base_url(), relative_url))
148 "shortcode" => shortcode,
150 "visible_in_picker" => true,
156 def custom_emojis(conn, _params) do
157 mastodon_emoji = mastodonized_emoji()
158 json(conn, mastodon_emoji)
161 defp add_link_headers(conn, method, activities, param \\ nil, params \\ %{}) do
162 last = List.last(activities)
163 first = List.first(activities)
169 {next_url, prev_url} =
173 Pleroma.Web.Endpoint,
176 Map.merge(params, %{max_id: min})
179 Pleroma.Web.Endpoint,
182 Map.merge(params, %{since_id: max})
188 Pleroma.Web.Endpoint,
190 Map.merge(params, %{max_id: min})
193 Pleroma.Web.Endpoint,
195 Map.merge(params, %{since_id: max})
201 |> put_resp_header("link", "<#{next_url}>; rel=\"next\", <#{prev_url}>; rel=\"prev\"")
207 def home_timeline(%{assigns: %{user: user}} = conn, params) do
210 |> Map.put("type", ["Create", "Announce"])
211 |> Map.put("blocking_user", user)
212 |> Map.put("user", user)
215 ActivityPub.fetch_activities([user.ap_id | user.following], params)
216 |> ActivityPub.contain_timeline(user)
220 |> add_link_headers(:home_timeline, activities)
221 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
224 def public_timeline(%{assigns: %{user: user}} = conn, params) do
225 local_only = params["local"] in [true, "True", "true", "1"]
229 |> Map.put("type", ["Create", "Announce"])
230 |> Map.put("local_only", local_only)
231 |> Map.put("blocking_user", user)
234 ActivityPub.fetch_public_activities(params)
238 |> add_link_headers(:public_timeline, activities, false, %{"local" => local_only})
239 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
242 def user_statuses(%{assigns: %{user: reading_user}} = conn, params) do
243 with %User{} = user <- Repo.get(User, params["id"]) do
244 # Since Pleroma has no "pinned" posts feature, we'll just set an empty list here
246 if params["pinned"] == "true" do
249 ActivityPub.fetch_user_activities(user, reading_user, params)
253 |> add_link_headers(:user_statuses, activities, params["id"])
254 |> render(StatusView, "index.json", %{
255 activities: activities,
262 def dm_timeline(%{assigns: %{user: user}} = conn, params) do
264 ActivityPub.fetch_activities_query(
266 Map.merge(params, %{"type" => "Create", visibility: "direct"})
269 activities = Repo.all(query)
272 |> add_link_headers(:dm_timeline, activities)
273 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
276 def get_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
277 with %Activity{} = activity <- Repo.get(Activity, id),
278 true <- ActivityPub.visible_for_user?(activity, user) do
279 try_render(conn, StatusView, "status.json", %{activity: activity, for: user})
283 def get_context(%{assigns: %{user: user}} = conn, %{"id" => id}) do
284 with %Activity{} = activity <- Repo.get(Activity, id),
286 ActivityPub.fetch_activities_for_context(activity.data["context"], %{
287 "blocking_user" => user,
291 activities |> Enum.filter(fn %{id: aid} -> to_string(aid) != to_string(id) end),
293 activities |> Enum.filter(fn %{data: %{"type" => type}} -> type == "Create" end),
294 grouped_activities <- Enum.group_by(activities, fn %{id: id} -> id < activity.id end) do
300 activities: grouped_activities[true] || [],
308 activities: grouped_activities[false] || [],
318 def post_status(conn, %{"status" => "", "media_ids" => media_ids} = params)
319 when length(media_ids) > 0 do
322 |> Map.put("status", ".")
324 post_status(conn, params)
327 def post_status(%{assigns: %{user: user}} = conn, %{"status" => _} = params) do
330 |> Map.put("in_reply_to_status_id", params["in_reply_to_id"])
331 |> Map.put("no_attachment_links", true)
334 case get_req_header(conn, "idempotency-key") do
336 _ -> Ecto.UUID.generate()
340 Cachex.fetch!(:idempotency_cache, idempotency_key, fn _ -> CommonAPI.post(user, params) end)
342 try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
345 def delete_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
346 with {:ok, %Activity{}} <- CommonAPI.delete(id, user) do
352 |> json(%{error: "Can't delete this post"})
356 def reblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
357 with {:ok, announce, _activity} <- CommonAPI.repeat(ap_id_or_id, user) do
358 try_render(conn, StatusView, "status.json", %{activity: announce, for: user, as: :activity})
362 def unreblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
363 with {:ok, _unannounce, %{data: %{"id" => id}}} <- CommonAPI.unrepeat(ap_id_or_id, user),
364 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
365 try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
369 def fav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
370 with {:ok, _fav, %{data: %{"id" => id}}} <- CommonAPI.favorite(ap_id_or_id, user),
371 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
372 try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
376 def unfav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
377 with {:ok, _, _, %{data: %{"id" => id}}} <- CommonAPI.unfavorite(ap_id_or_id, user),
378 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
379 try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
383 def notifications(%{assigns: %{user: user}} = conn, params) do
384 notifications = Notification.for_user(user, params)
387 Enum.map(notifications, fn x ->
388 render_notification(user, x)
393 |> add_link_headers(:notifications, notifications)
397 def get_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
398 with {:ok, notification} <- Notification.get(user, id) do
399 json(conn, render_notification(user, notification))
403 |> put_resp_content_type("application/json")
404 |> send_resp(403, Jason.encode!(%{"error" => reason}))
408 def clear_notifications(%{assigns: %{user: user}} = conn, _params) do
409 Notification.clear(user)
413 def dismiss_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
414 with {:ok, _notif} <- Notification.dismiss(user, id) do
419 |> put_resp_content_type("application/json")
420 |> send_resp(403, Jason.encode!(%{"error" => reason}))
424 def relationships(%{assigns: %{user: user}} = conn, %{"id" => id}) do
426 q = from(u in User, where: u.id in ^id)
427 targets = Repo.all(q)
428 render(conn, AccountView, "relationships.json", %{user: user, targets: targets})
431 # Instead of returning a 400 when no "id" params is present, Mastodon returns an empty array.
432 def relationships(%{assigns: %{user: user}} = conn, _) do
437 def update_media(%{assigns: %{user: _}} = conn, data) do
438 with %Object{} = object <- Repo.get(Object, data["id"]),
439 true <- is_binary(data["description"]),
440 description <- data["description"] do
441 new_data = %{object.data | "name" => description}
443 change = Object.change(object, %{data: new_data})
444 {:ok, _} = Repo.update(change)
448 |> Map.put("id", object.id)
450 render(conn, StatusView, "attachment.json", %{attachment: data})
454 def upload(%{assigns: %{user: _}} = conn, %{"file" => file} = data) do
455 with {:ok, object} <- ActivityPub.upload(file, description: Map.get(data, "description")) do
456 change = Object.change(object, %{data: object.data})
457 {:ok, object} = Repo.update(change)
461 |> Map.put("id", object.id)
463 render(conn, StatusView, "attachment.json", %{attachment: objdata})
467 def favourited_by(conn, %{"id" => id}) do
468 with %Activity{data: %{"object" => object}} <- Repo.get(Activity, id),
469 %Object{data: %{"likes" => likes}} <- Object.normalize(object) do
470 q = from(u in User, where: u.ap_id in ^likes)
472 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
478 def reblogged_by(conn, %{"id" => id}) do
479 with %Activity{data: %{"object" => object}} <- Repo.get(Activity, id),
480 %Object{data: %{"announcements" => announces}} <- Object.normalize(object) do
481 q = from(u in User, where: u.ap_id in ^announces)
483 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
489 def hashtag_timeline(%{assigns: %{user: user}} = conn, params) do
490 local_only = params["local"] in [true, "True", "true", "1"]
494 |> Map.put("type", "Create")
495 |> Map.put("local_only", local_only)
496 |> Map.put("blocking_user", user)
497 |> Map.put("tag", String.downcase(params["tag"]))
500 ActivityPub.fetch_public_activities(params)
504 |> add_link_headers(:hashtag_timeline, activities, params["tag"], %{"local" => local_only})
505 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
509 def followers(conn, %{"id" => id}) do
510 with %User{} = user <- Repo.get(User, id),
511 {:ok, followers} <- User.get_followers(user) do
512 render(conn, AccountView, "accounts.json", %{users: followers, as: :user})
516 def following(conn, %{"id" => id}) do
517 with %User{} = user <- Repo.get(User, id),
518 {:ok, followers} <- User.get_friends(user) do
519 render(conn, AccountView, "accounts.json", %{users: followers, as: :user})
523 def follow_requests(%{assigns: %{user: followed}} = conn, _params) do
524 with {:ok, follow_requests} <- User.get_follow_requests(followed) do
525 render(conn, AccountView, "accounts.json", %{users: follow_requests, as: :user})
529 def authorize_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
530 with %User{} = follower <- Repo.get(User, id),
531 {:ok, follower} <- User.maybe_follow(follower, followed),
532 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
533 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "accept"),
535 ActivityPub.accept(%{
536 to: [follower.ap_id],
537 actor: followed.ap_id,
538 object: follow_activity.data["id"],
541 render(conn, AccountView, "relationship.json", %{user: followed, target: follower})
545 |> put_resp_content_type("application/json")
546 |> send_resp(403, Jason.encode!(%{"error" => message}))
550 def reject_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
551 with %User{} = follower <- Repo.get(User, id),
552 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
553 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "reject"),
555 ActivityPub.reject(%{
556 to: [follower.ap_id],
557 actor: followed.ap_id,
558 object: follow_activity.data["id"],
561 render(conn, AccountView, "relationship.json", %{user: followed, target: follower})
565 |> put_resp_content_type("application/json")
566 |> send_resp(403, Jason.encode!(%{"error" => message}))
570 def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
571 with %User{} = followed <- Repo.get(User, id),
572 {:ok, follower} <- User.maybe_direct_follow(follower, followed),
573 {:ok, _activity} <- ActivityPub.follow(follower, followed),
574 {:ok, follower, followed} <-
575 User.wait_and_refresh(
576 Pleroma.Config.get([:activitypub, :follow_handshake_timeout]),
580 render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
584 |> put_resp_content_type("application/json")
585 |> send_resp(403, Jason.encode!(%{"error" => message}))
589 def follow(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do
590 with %User{} = followed <- Repo.get_by(User, nickname: uri),
591 {:ok, follower} <- User.maybe_direct_follow(follower, followed),
592 {:ok, _activity} <- ActivityPub.follow(follower, followed) do
593 render(conn, AccountView, "account.json", %{user: followed, for: follower})
597 |> put_resp_content_type("application/json")
598 |> send_resp(403, Jason.encode!(%{"error" => message}))
602 def unfollow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
603 with %User{} = followed <- Repo.get(User, id),
604 {:ok, _activity} <- ActivityPub.unfollow(follower, followed),
605 {:ok, follower, _} <- User.unfollow(follower, followed) do
606 render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
610 def block(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
611 with %User{} = blocked <- Repo.get(User, id),
612 {:ok, blocker} <- User.block(blocker, blocked),
613 {:ok, _activity} <- ActivityPub.block(blocker, blocked) do
614 render(conn, AccountView, "relationship.json", %{user: blocker, target: blocked})
618 |> put_resp_content_type("application/json")
619 |> send_resp(403, Jason.encode!(%{"error" => message}))
623 def unblock(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
624 with %User{} = blocked <- Repo.get(User, id),
625 {:ok, blocker} <- User.unblock(blocker, blocked),
626 {:ok, _activity} <- ActivityPub.unblock(blocker, blocked) do
627 render(conn, AccountView, "relationship.json", %{user: blocker, target: blocked})
631 |> put_resp_content_type("application/json")
632 |> send_resp(403, Jason.encode!(%{"error" => message}))
636 # TODO: Use proper query
637 def blocks(%{assigns: %{user: user}} = conn, _) do
638 with blocked_users <- user.info.blocks || [],
639 accounts <- Enum.map(blocked_users, fn ap_id -> User.get_cached_by_ap_id(ap_id) end) do
640 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
645 def domain_blocks(%{assigns: %{user: %{info: info}}} = conn, _) do
646 json(conn, info.domain_blocks || [])
649 def block_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
650 User.block_domain(blocker, domain)
654 def unblock_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
655 User.unblock_domain(blocker, domain)
659 def status_search(query) do
661 if Regex.match?(~r/https?:/, query) do
662 with {:ok, object} <- Fetcher.fetch_object_from_id(query) do
663 [Activity.get_create_activity_by_object_ap_id(object.data["id"])]
672 where: fragment("?->>'type' = 'Create'", a.data),
673 where: "https://www.w3.org/ns/activitystreams#Public" in a.recipients,
676 "to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)",
681 order_by: [desc: :id]
684 Repo.all(q) ++ fetched
687 def search2(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
688 accounts = User.search(query, params["resolve"] == "true")
690 statuses = status_search(query)
692 tags_path = Web.base_url() <> "/tag/"
697 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
698 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
699 |> Enum.map(fn tag -> %{name: tag, url: tags_path <> tag} end)
702 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
704 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
711 def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
712 accounts = User.search(query, params["resolve"] == "true")
714 statuses = status_search(query)
719 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
720 |> Enum.map(fn tag -> String.slice(tag, 1..-1) 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 account_search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
733 accounts = User.search(query, params["resolve"] == "true")
735 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
740 def favourites(%{assigns: %{user: user}} = conn, _) do
743 |> Map.put("type", "Create")
744 |> Map.put("favorited_by", user.ap_id)
745 |> Map.put("blocking_user", user)
748 ActivityPub.fetch_public_activities(params)
752 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
755 def get_lists(%{assigns: %{user: user}} = conn, opts) do
756 lists = Pleroma.List.for_user(user, opts)
757 res = ListView.render("lists.json", lists: lists)
761 def get_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
762 with %Pleroma.List{} = list <- Pleroma.List.get(id, user) do
763 res = ListView.render("list.json", list: list)
766 _e -> json(conn, "error")
770 def account_lists(%{assigns: %{user: user}} = conn, %{"id" => account_id}) do
771 lists = Pleroma.List.get_lists_account_belongs(user, account_id)
772 res = ListView.render("lists.json", lists: lists)
776 def delete_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
777 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
778 {:ok, _list} <- Pleroma.List.delete(list) do
786 def create_list(%{assigns: %{user: user}} = conn, %{"title" => title}) do
787 with {:ok, %Pleroma.List{} = list} <- Pleroma.List.create(title, user) do
788 res = ListView.render("list.json", list: list)
793 def add_to_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
795 |> Enum.each(fn account_id ->
796 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
797 %User{} = followed <- Repo.get(User, account_id) do
798 Pleroma.List.follow(list, followed)
805 def remove_from_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
807 |> Enum.each(fn account_id ->
808 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
809 %User{} = followed <- Repo.get(Pleroma.User, account_id) do
810 Pleroma.List.unfollow(list, followed)
817 def list_accounts(%{assigns: %{user: user}} = conn, %{"id" => id}) do
818 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
819 {:ok, users} = Pleroma.List.get_following(list) do
820 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
824 def rename_list(%{assigns: %{user: user}} = conn, %{"id" => id, "title" => title}) do
825 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
826 {:ok, list} <- Pleroma.List.rename(list, title) do
827 res = ListView.render("list.json", list: list)
835 def list_timeline(%{assigns: %{user: user}} = conn, %{"list_id" => id} = params) do
836 with %Pleroma.List{title: title, following: following} <- Pleroma.List.get(id, user) do
839 |> Map.put("type", "Create")
840 |> Map.put("blocking_user", user)
842 # we must filter the following list for the user to avoid leaking statuses the user
843 # does not actually have permission to see (for more info, peruse security issue #270).
846 |> Enum.filter(fn x -> x in user.following end)
849 ActivityPub.fetch_activities_bounded(following_to, following, params)
853 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
858 |> json(%{error: "Error."})
862 def index(%{assigns: %{user: user}} = conn, _params) do
865 |> get_session(:oauth_token)
868 mastodon_emoji = mastodonized_emoji()
870 limit = Pleroma.Config.get([:instance, :limit])
873 Map.put(%{}, user.id, AccountView.render("account.json", %{user: user, for: user}))
878 streaming_api_base_url:
879 String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws"),
882 domain: Pleroma.Web.Endpoint.host(),
885 unfollow_modal: false,
888 auto_play_gif: false,
889 display_sensitive_media: false,
890 reduce_motion: false,
891 max_toot_chars: limit
894 delete_others_notice: !!user.info.is_moderator
898 default_privacy: user.info.default_scope,
899 default_sensitive: false
901 media_attachments: %{
902 accept_content_types: [
918 Map.get(user.info, :settings) ||
948 push_subscription: nil,
950 custom_emojis: mastodon_emoji,
957 |> render(MastodonView, "index.html", %{initial_state: initial_state})
960 |> redirect(to: "/web/login")
964 def put_settings(%{assigns: %{user: user}} = conn, %{"data" => settings} = _params) do
965 with new_info <- Map.put(user.info, "settings", settings),
966 change <- User.info_changeset(user, %{info: new_info}),
967 {:ok, _user} <- User.update_and_set_cache(change) do
973 |> json(%{error: inspect(e)})
977 def login(conn, %{"code" => code}) do
978 with {:ok, app} <- get_or_make_app(),
979 %Authorization{} = auth <- Repo.get_by(Authorization, token: code, app_id: app.id),
980 {:ok, token} <- Token.exchange_token(app, auth) do
982 |> put_session(:oauth_token, token.token)
983 |> redirect(to: "/web/getting-started")
987 def login(conn, _) do
988 with {:ok, app} <- get_or_make_app() do
993 response_type: "code",
994 client_id: app.client_id,
1000 |> redirect(to: path)
1004 defp get_or_make_app() do
1005 with %App{} = app <- Repo.get_by(App, client_name: "Mastodon-Local") do
1010 App.register_changeset(%App{}, %{
1011 client_name: "Mastodon-Local",
1013 scopes: "read,write,follow"
1020 def logout(conn, _) do
1023 |> redirect(to: "/")
1026 def relationship_noop(%{assigns: %{user: user}} = conn, %{"id" => id}) do
1027 Logger.debug("Unimplemented, returning unmodified relationship")
1029 with %User{} = target <- Repo.get(User, id) do
1030 render(conn, AccountView, "relationship.json", %{user: user, target: target})
1034 def empty_array(conn, _) do
1035 Logger.debug("Unimplemented, returning an empty array")
1039 def empty_object(conn, _) do
1040 Logger.debug("Unimplemented, returning an empty object")
1044 def render_notification(user, %{id: id, activity: activity, inserted_at: created_at} = _params) do
1045 actor = User.get_cached_by_ap_id(activity.data["actor"])
1048 NaiveDateTime.to_iso8601(created_at)
1049 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
1051 id = id |> to_string
1053 case activity.data["type"] do
1058 created_at: created_at,
1059 account: AccountView.render("account.json", %{user: actor, for: user}),
1060 status: StatusView.render("status.json", %{activity: activity, for: user})
1064 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
1069 created_at: created_at,
1070 account: AccountView.render("account.json", %{user: actor, for: user}),
1071 status: StatusView.render("status.json", %{activity: liked_activity, for: user})
1075 announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
1080 created_at: created_at,
1081 account: AccountView.render("account.json", %{user: actor, for: user}),
1082 status: StatusView.render("status.json", %{activity: announced_activity, for: user})
1089 created_at: created_at,
1090 account: AccountView.render("account.json", %{user: actor, for: user})
1098 def get_filters(%{assigns: %{user: user}} = conn, _) do
1099 filters = Pleroma.Filter.get_filters(user)
1100 res = FilterView.render("filters.json", filters: filters)
1105 %{assigns: %{user: user}} = conn,
1106 %{"phrase" => phrase, "context" => context} = params
1108 query = %Pleroma.Filter{
1112 hide: Map.get(params, "irreversible", nil),
1113 whole_word: Map.get(params, "boolean", true)
1117 {:ok, response} = Pleroma.Filter.create(query)
1118 res = FilterView.render("filter.json", filter: response)
1122 def get_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do
1123 filter = Pleroma.Filter.get(filter_id, user)
1124 res = FilterView.render("filter.json", filter: filter)
1129 %{assigns: %{user: user}} = conn,
1130 %{"phrase" => phrase, "context" => context, "id" => filter_id} = params
1132 query = %Pleroma.Filter{
1134 filter_id: filter_id,
1137 hide: Map.get(params, "irreversible", nil),
1138 whole_word: Map.get(params, "boolean", true)
1142 {:ok, response} = Pleroma.Filter.update(query)
1143 res = FilterView.render("filter.json", filter: response)
1147 def delete_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do
1148 query = %Pleroma.Filter{
1150 filter_id: filter_id
1153 {:ok, _} = Pleroma.Filter.delete(query)
1157 def errors(conn, _) do
1160 |> json("Something went wrong")
1163 def suggestions(%{assigns: %{user: user}} = conn, _) do
1164 suggestions = Pleroma.Config.get(:suggestions)
1166 if Keyword.get(suggestions, :enabled, false) do
1167 api = Keyword.get(suggestions, :third_party_engine, "")
1168 timeout = Keyword.get(suggestions, :timeout, 5000)
1169 limit = Keyword.get(suggestions, :limit, 23)
1171 host = Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host])
1173 user = user.nickname
1174 url = String.replace(api, "{{host}}", host) |> String.replace("{{user}}", user)
1176 with {:ok, %{status_code: 200, body: body}} <-
1177 @httpoison.get(url, [], timeout: timeout, recv_timeout: timeout),
1178 {:ok, data} <- Jason.decode(body) do
1180 Enum.slice(data, 0, limit)
1185 case User.get_or_fetch(x["acct"]) do
1192 Map.put(x, "avatar", MediaProxy.url(x["avatar"]))
1195 Map.put(x, "avatar_static", MediaProxy.url(x["avatar_static"]))
1201 e -> Logger.error("Could not retrieve suggestions at fetch #{url}, #{inspect(e)}")
1208 def try_render(conn, renderer, target, params)
1209 when is_binary(target) do
1210 res = render(conn, renderer, target, params)
1215 |> json(%{error: "Can't display this activity"})
1221 def try_render(conn, _, _, _) do
1224 |> json(%{error: "Can't display this activity"})