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,
40 value_function \\ fn x -> {:ok, x} end
42 if Map.has_key?(params, params_field) do
43 case value_function.(params[params_field]) do
44 {:ok, new_value} -> Map.put(map, map_field, new_value)
52 def update_credentials(%{assigns: %{user: user}} = conn, params) do
57 |> add_if_present(params, "display_name", :name)
58 |> add_if_present(params, "note", :bio, fn value -> {:ok, User.parse_bio(value)} end)
59 |> add_if_present(params, "avatar", :avatar, fn value ->
60 with %Plug.Upload{} <- value,
61 {:ok, object} <- ActivityPub.upload(value, type: :avatar) do
70 |> add_if_present(params, "locked", :locked, fn value -> {:ok, value == "true"} end)
71 |> add_if_present(params, "header", :banner, fn value ->
72 with %Plug.Upload{} <- value,
73 {:ok, object} <- ActivityPub.upload(value, type: :banner) do
80 info_cng = User.Info.mastodon_profile_update(user.info, info_params)
82 with changeset <- User.update_changeset(user, user_params),
83 changeset <- Ecto.Changeset.put_embed(changeset, :info, info_cng),
84 {:ok, user} <- User.update_and_set_cache(changeset) do
85 if original_user != user do
86 CommonAPI.update(user)
89 json(conn, AccountView.render("account.json", %{user: user, for: user}))
94 |> json(%{error: "Invalid request"})
98 def verify_credentials(%{assigns: %{user: user}} = conn, _) do
99 account = AccountView.render("account.json", %{user: user, for: user})
103 def user(%{assigns: %{user: for_user}} = conn, %{"id" => id}) do
104 with %User{} = user <- Repo.get(User, id) do
105 account = AccountView.render("account.json", %{user: user, for: for_user})
111 |> json(%{error: "Can't find user"})
115 @mastodon_api_level "2.5.0"
117 def masto_instance(conn, _params) do
118 instance = Pleroma.Config.get(:instance)
122 title: Keyword.get(instance, :name),
123 description: Keyword.get(instance, :description),
124 version: "#{@mastodon_api_level} (compatible; #{Pleroma.Application.named_version()})",
125 email: Keyword.get(instance, :email),
127 streaming_api: String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws")
129 stats: Stats.get_stats(),
130 thumbnail: Web.base_url() <> "/instance/thumbnail.jpeg",
131 max_toot_chars: Keyword.get(instance, :limit)
137 def peers(conn, _params) do
138 json(conn, Stats.get_peers())
141 defp mastodonized_emoji do
142 Pleroma.Emoji.get_all()
143 |> Enum.map(fn {shortcode, relative_url} ->
144 url = to_string(URI.merge(Web.base_url(), relative_url))
147 "shortcode" => shortcode,
149 "visible_in_picker" => true,
155 def custom_emojis(conn, _params) do
156 mastodon_emoji = mastodonized_emoji()
157 json(conn, mastodon_emoji)
160 defp add_link_headers(conn, method, activities, param \\ nil, params \\ %{}) do
161 last = List.last(activities)
162 first = List.first(activities)
168 {next_url, prev_url} =
172 Pleroma.Web.Endpoint,
175 Map.merge(params, %{max_id: min})
178 Pleroma.Web.Endpoint,
181 Map.merge(params, %{since_id: max})
187 Pleroma.Web.Endpoint,
189 Map.merge(params, %{max_id: min})
192 Pleroma.Web.Endpoint,
194 Map.merge(params, %{since_id: max})
200 |> put_resp_header("link", "<#{next_url}>; rel=\"next\", <#{prev_url}>; rel=\"prev\"")
206 def home_timeline(%{assigns: %{user: user}} = conn, params) do
209 |> Map.put("type", ["Create", "Announce"])
210 |> Map.put("blocking_user", user)
211 |> Map.put("user", user)
214 ActivityPub.fetch_activities([user.ap_id | user.following], params)
215 |> ActivityPub.contain_timeline(user)
219 |> add_link_headers(:home_timeline, activities)
220 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
223 def public_timeline(%{assigns: %{user: user}} = conn, params) do
224 local_only = params["local"] in [true, "True", "true", "1"]
228 |> Map.put("type", ["Create", "Announce"])
229 |> Map.put("local_only", local_only)
230 |> Map.put("blocking_user", user)
233 ActivityPub.fetch_public_activities(params)
237 |> add_link_headers(:public_timeline, activities, false, %{"local" => local_only})
238 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
241 def user_statuses(%{assigns: %{user: reading_user}} = conn, params) do
242 with %User{} = user <- Repo.get(User, params["id"]) do
243 # Since Pleroma has no "pinned" posts feature, we'll just set an empty list here
245 if params["pinned"] == "true" do
248 ActivityPub.fetch_user_activities(user, reading_user, params)
252 |> add_link_headers(:user_statuses, activities, params["id"])
253 |> render(StatusView, "index.json", %{
254 activities: activities,
261 def dm_timeline(%{assigns: %{user: user}} = conn, params) do
263 ActivityPub.fetch_activities_query(
265 Map.merge(params, %{"type" => "Create", visibility: "direct"})
268 activities = Repo.all(query)
271 |> add_link_headers(:dm_timeline, activities)
272 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
275 def get_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
276 with %Activity{} = activity <- Repo.get(Activity, id),
277 true <- ActivityPub.visible_for_user?(activity, user) do
278 try_render(conn, StatusView, "status.json", %{activity: activity, for: user})
282 def get_context(%{assigns: %{user: user}} = conn, %{"id" => id}) do
283 with %Activity{} = activity <- Repo.get(Activity, id),
285 ActivityPub.fetch_activities_for_context(activity.data["context"], %{
286 "blocking_user" => user,
290 activities |> Enum.filter(fn %{id: aid} -> to_string(aid) != to_string(id) end),
292 activities |> Enum.filter(fn %{data: %{"type" => type}} -> type == "Create" end),
293 grouped_activities <- Enum.group_by(activities, fn %{id: id} -> id < activity.id end) do
299 activities: grouped_activities[true] || [],
307 activities: grouped_activities[false] || [],
317 def post_status(conn, %{"status" => "", "media_ids" => media_ids} = params)
318 when length(media_ids) > 0 do
321 |> Map.put("status", ".")
323 post_status(conn, params)
326 def post_status(%{assigns: %{user: user}} = conn, %{"status" => _} = params) do
329 |> Map.put("in_reply_to_status_id", params["in_reply_to_id"])
330 |> Map.put("no_attachment_links", true)
333 case get_req_header(conn, "idempotency-key") do
335 _ -> Ecto.UUID.generate()
339 Cachex.fetch!(:idempotency_cache, idempotency_key, fn _ -> CommonAPI.post(user, params) end)
341 try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
344 def delete_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
345 with {:ok, %Activity{}} <- CommonAPI.delete(id, user) do
351 |> json(%{error: "Can't delete this post"})
355 def reblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
356 with {:ok, announce, _activity} <- CommonAPI.repeat(ap_id_or_id, user) do
357 try_render(conn, StatusView, "status.json", %{activity: announce, for: user, as: :activity})
361 def unreblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
362 with {:ok, _unannounce, %{data: %{"id" => id}}} <- CommonAPI.unrepeat(ap_id_or_id, user),
363 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
364 try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
368 def fav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
369 with {:ok, _fav, %{data: %{"id" => id}}} <- CommonAPI.favorite(ap_id_or_id, user),
370 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
371 try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
375 def unfav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
376 with {:ok, _, _, %{data: %{"id" => id}}} <- CommonAPI.unfavorite(ap_id_or_id, user),
377 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
378 try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
382 def notifications(%{assigns: %{user: user}} = conn, params) do
383 notifications = Notification.for_user(user, params)
386 Enum.map(notifications, fn x ->
387 render_notification(user, x)
392 |> add_link_headers(:notifications, notifications)
396 def get_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
397 with {:ok, notification} <- Notification.get(user, id) do
398 json(conn, render_notification(user, notification))
402 |> put_resp_content_type("application/json")
403 |> send_resp(403, Jason.encode!(%{"error" => reason}))
407 def clear_notifications(%{assigns: %{user: user}} = conn, _params) do
408 Notification.clear(user)
412 def dismiss_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
413 with {:ok, _notif} <- Notification.dismiss(user, id) do
418 |> put_resp_content_type("application/json")
419 |> send_resp(403, Jason.encode!(%{"error" => reason}))
423 def relationships(%{assigns: %{user: user}} = conn, %{"id" => id}) do
425 q = from(u in User, where: u.id in ^id)
426 targets = Repo.all(q)
427 render(conn, AccountView, "relationships.json", %{user: user, targets: targets})
430 # Instead of returning a 400 when no "id" params is present, Mastodon returns an empty array.
431 def relationships(%{assigns: %{user: user}} = conn, _) do
436 def update_media(%{assigns: %{user: user}} = conn, data) do
437 with %Object{} = object <- Repo.get(Object, data["id"]),
438 true <- Object.authorize_mutation(object, user),
439 true <- is_binary(data["description"]),
440 description <- data["description"] do
441 new_data = %{object.data | "name" => description}
445 |> Object.change(%{data: new_data})
448 attachment_data = Map.put(new_data, "id", object.id)
449 render(conn, StatusView, "attachment.json", %{attachment: attachment_data})
453 def upload(%{assigns: %{user: user}} = conn, %{"file" => file} = data) do
454 with {:ok, object} <-
455 ActivityPub.upload(file,
456 actor: User.ap_id(user),
457 description: Map.get(data, "description")
459 attachment_data = Map.put(object.data, "id", object.id)
460 render(conn, StatusView, "attachment.json", %{attachment: attachment_data})
464 def favourited_by(conn, %{"id" => id}) do
465 with %Activity{data: %{"object" => %{"likes" => likes}}} <- Repo.get(Activity, id) do
466 q = from(u in User, where: u.ap_id in ^likes)
468 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
474 def reblogged_by(conn, %{"id" => id}) do
475 with %Activity{data: %{"object" => %{"announcements" => announces}}} <- Repo.get(Activity, id) do
476 q = from(u in User, where: u.ap_id in ^announces)
478 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
484 def hashtag_timeline(%{assigns: %{user: user}} = conn, params) do
485 local_only = params["local"] in [true, "True", "true", "1"]
489 |> Map.put("type", "Create")
490 |> Map.put("local_only", local_only)
491 |> Map.put("blocking_user", user)
492 |> Map.put("tag", String.downcase(params["tag"]))
495 ActivityPub.fetch_public_activities(params)
499 |> add_link_headers(:hashtag_timeline, activities, params["tag"], %{"local" => local_only})
500 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
503 def followers(%{assigns: %{user: for_user}} = conn, %{"id" => id}) do
504 with %User{} = user <- Repo.get(User, id),
505 {:ok, followers} <- User.get_followers(user) do
508 for_user && user.id == for_user.id -> followers
509 user.info.hide_network -> []
513 render(conn, AccountView, "accounts.json", %{users: followers, as: :user})
517 def following(%{assigns: %{user: for_user}} = conn, %{"id" => id}) do
518 with %User{} = user <- Repo.get(User, id),
519 {:ok, followers} <- User.get_friends(user) do
522 for_user && user.id == for_user.id -> followers
523 user.info.hide_network -> []
527 render(conn, AccountView, "accounts.json", %{users: followers, as: :user})
531 def follow_requests(%{assigns: %{user: followed}} = conn, _params) do
532 with {:ok, follow_requests} <- User.get_follow_requests(followed) do
533 render(conn, AccountView, "accounts.json", %{users: follow_requests, as: :user})
537 def authorize_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
538 with %User{} = follower <- Repo.get(User, id),
539 {:ok, follower} <- User.maybe_follow(follower, followed),
540 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
541 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "accept"),
543 ActivityPub.accept(%{
544 to: [follower.ap_id],
545 actor: followed.ap_id,
546 object: follow_activity.data["id"],
549 render(conn, AccountView, "relationship.json", %{user: followed, target: follower})
553 |> put_resp_content_type("application/json")
554 |> send_resp(403, Jason.encode!(%{"error" => message}))
558 def reject_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
559 with %User{} = follower <- Repo.get(User, id),
560 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
561 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "reject"),
563 ActivityPub.reject(%{
564 to: [follower.ap_id],
565 actor: followed.ap_id,
566 object: follow_activity.data["id"],
569 render(conn, AccountView, "relationship.json", %{user: followed, target: follower})
573 |> put_resp_content_type("application/json")
574 |> send_resp(403, Jason.encode!(%{"error" => message}))
578 def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
579 with %User{} = followed <- Repo.get(User, id),
580 {:ok, follower} <- User.maybe_direct_follow(follower, followed),
581 {:ok, _activity} <- ActivityPub.follow(follower, followed),
582 {:ok, follower, followed} <-
583 User.wait_and_refresh(
584 Pleroma.Config.get([:activitypub, :follow_handshake_timeout]),
588 render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
592 |> put_resp_content_type("application/json")
593 |> send_resp(403, Jason.encode!(%{"error" => message}))
597 def follow(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do
598 with %User{} = followed <- Repo.get_by(User, nickname: uri),
599 {:ok, follower} <- User.maybe_direct_follow(follower, followed),
600 {:ok, _activity} <- ActivityPub.follow(follower, followed) do
601 render(conn, AccountView, "account.json", %{user: followed, for: follower})
605 |> put_resp_content_type("application/json")
606 |> send_resp(403, Jason.encode!(%{"error" => message}))
610 def unfollow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
611 with %User{} = followed <- Repo.get(User, id),
612 {:ok, _activity} <- ActivityPub.unfollow(follower, followed),
613 {:ok, follower, _} <- User.unfollow(follower, followed) do
614 render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
618 def block(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
619 with %User{} = blocked <- Repo.get(User, id),
620 {:ok, blocker} <- User.block(blocker, blocked),
621 {:ok, _activity} <- ActivityPub.block(blocker, blocked) do
622 render(conn, AccountView, "relationship.json", %{user: blocker, target: blocked})
626 |> put_resp_content_type("application/json")
627 |> send_resp(403, Jason.encode!(%{"error" => message}))
631 def unblock(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
632 with %User{} = blocked <- Repo.get(User, id),
633 {:ok, blocker} <- User.unblock(blocker, blocked),
634 {:ok, _activity} <- ActivityPub.unblock(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 # TODO: Use proper query
645 def blocks(%{assigns: %{user: user}} = conn, _) do
646 with blocked_users <- user.info.blocks || [],
647 accounts <- Enum.map(blocked_users, fn ap_id -> User.get_cached_by_ap_id(ap_id) end) do
648 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
653 def domain_blocks(%{assigns: %{user: %{info: info}}} = conn, _) do
654 json(conn, info.domain_blocks || [])
657 def block_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
658 User.block_domain(blocker, domain)
662 def unblock_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
663 User.unblock_domain(blocker, domain)
667 def status_search(query) do
669 if Regex.match?(~r/https?:/, query) do
670 with {:ok, object} <- ActivityPub.fetch_object_from_id(query) do
671 [Activity.get_create_activity_by_object_ap_id(object.data["id"])]
680 where: fragment("?->>'type' = 'Create'", a.data),
681 where: "https://www.w3.org/ns/activitystreams#Public" in a.recipients,
684 "to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)",
689 order_by: [desc: :id]
692 Repo.all(q) ++ fetched
695 def search2(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
696 accounts = User.search(query, params["resolve"] == "true")
698 statuses = status_search(query)
700 tags_path = Web.base_url() <> "/tag/"
705 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
706 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
707 |> Enum.map(fn tag -> %{name: tag, url: tags_path <> tag} end)
710 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
712 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
719 def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
720 accounts = User.search(query, params["resolve"] == "true")
722 statuses = status_search(query)
727 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
728 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
731 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
733 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
740 def account_search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
741 accounts = User.search(query, params["resolve"] == "true")
743 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
748 def favourites(%{assigns: %{user: user}} = conn, _) do
751 |> Map.put("type", "Create")
752 |> Map.put("favorited_by", user.ap_id)
753 |> Map.put("blocking_user", user)
756 ActivityPub.fetch_public_activities(params)
760 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
763 def get_lists(%{assigns: %{user: user}} = conn, opts) do
764 lists = Pleroma.List.for_user(user, opts)
765 res = ListView.render("lists.json", lists: lists)
769 def get_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
770 with %Pleroma.List{} = list <- Pleroma.List.get(id, user) do
771 res = ListView.render("list.json", list: list)
774 _e -> json(conn, "error")
778 def account_lists(%{assigns: %{user: user}} = conn, %{"id" => account_id}) do
779 lists = Pleroma.List.get_lists_account_belongs(user, account_id)
780 res = ListView.render("lists.json", lists: lists)
784 def delete_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
785 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
786 {:ok, _list} <- Pleroma.List.delete(list) do
794 def create_list(%{assigns: %{user: user}} = conn, %{"title" => title}) do
795 with {:ok, %Pleroma.List{} = list} <- Pleroma.List.create(title, user) do
796 res = ListView.render("list.json", list: list)
801 def add_to_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
803 |> Enum.each(fn account_id ->
804 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
805 %User{} = followed <- Repo.get(User, account_id) do
806 Pleroma.List.follow(list, followed)
813 def remove_from_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
815 |> Enum.each(fn account_id ->
816 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
817 %User{} = followed <- Repo.get(Pleroma.User, account_id) do
818 Pleroma.List.unfollow(list, followed)
825 def list_accounts(%{assigns: %{user: user}} = conn, %{"id" => id}) do
826 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
827 {:ok, users} = Pleroma.List.get_following(list) do
828 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
832 def rename_list(%{assigns: %{user: user}} = conn, %{"id" => id, "title" => title}) do
833 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
834 {:ok, list} <- Pleroma.List.rename(list, title) do
835 res = ListView.render("list.json", list: list)
843 def list_timeline(%{assigns: %{user: user}} = conn, %{"list_id" => id} = params) do
844 with %Pleroma.List{title: title, following: following} <- Pleroma.List.get(id, user) do
847 |> Map.put("type", "Create")
848 |> Map.put("blocking_user", user)
850 # we must filter the following list for the user to avoid leaking statuses the user
851 # does not actually have permission to see (for more info, peruse security issue #270).
854 |> Enum.filter(fn x -> x in user.following end)
857 ActivityPub.fetch_activities_bounded(following_to, following, params)
861 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
866 |> json(%{error: "Error."})
870 def index(%{assigns: %{user: user}} = conn, _params) do
873 |> get_session(:oauth_token)
876 mastodon_emoji = mastodonized_emoji()
878 limit = Pleroma.Config.get([:instance, :limit])
881 Map.put(%{}, user.id, AccountView.render("account.json", %{user: user, for: user}))
886 streaming_api_base_url:
887 String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws"),
890 domain: Pleroma.Web.Endpoint.host(),
893 unfollow_modal: false,
896 auto_play_gif: false,
897 display_sensitive_media: false,
898 reduce_motion: false,
899 max_toot_chars: limit
902 delete_others_notice: !!user.info.is_moderator
906 default_privacy: user.info.default_scope,
907 default_sensitive: false
909 media_attachments: %{
910 accept_content_types: [
926 Map.get(user.info, :settings) ||
956 push_subscription: nil,
958 custom_emojis: mastodon_emoji,
965 |> render(MastodonView, "index.html", %{initial_state: initial_state})
968 |> redirect(to: "/web/login")
972 def put_settings(%{assigns: %{user: user}} = conn, %{"data" => settings} = _params) do
973 info_cng = User.Info.mastodon_settings_update(user.info, settings)
975 with changeset <- User.update_changeset(user),
976 changeset <- Ecto.Changeset.put_embed(changeset, :info, info_cng),
977 {:ok, user} <- User.update_and_set_cache(changeset) do
983 |> json(%{error: inspect(e)})
987 def login(conn, %{"code" => code}) do
988 with {:ok, app} <- get_or_make_app(),
989 %Authorization{} = auth <- Repo.get_by(Authorization, token: code, app_id: app.id),
990 {:ok, token} <- Token.exchange_token(app, auth) do
992 |> put_session(:oauth_token, token.token)
993 |> redirect(to: "/web/getting-started")
997 def login(conn, _) do
998 with {:ok, app} <- get_or_make_app() do
1000 o_auth_path(conn, :authorize,
1001 response_type: "code",
1002 client_id: app.client_id,
1008 |> redirect(to: path)
1012 defp get_or_make_app() do
1013 with %App{} = app <- Repo.get_by(App, client_name: "Mastodon-Local") do
1018 App.register_changeset(%App{}, %{
1019 client_name: "Mastodon-Local",
1021 scopes: "read,write,follow"
1028 def logout(conn, _) do
1031 |> redirect(to: "/")
1034 def relationship_noop(%{assigns: %{user: user}} = conn, %{"id" => id}) do
1035 Logger.debug("Unimplemented, returning unmodified relationship")
1037 with %User{} = target <- Repo.get(User, id) do
1038 render(conn, AccountView, "relationship.json", %{user: user, target: target})
1042 def empty_array(conn, _) do
1043 Logger.debug("Unimplemented, returning an empty array")
1047 def empty_object(conn, _) do
1048 Logger.debug("Unimplemented, returning an empty object")
1052 def render_notification(user, %{id: id, activity: activity, inserted_at: created_at} = _params) do
1053 actor = User.get_cached_by_ap_id(activity.data["actor"])
1056 NaiveDateTime.to_iso8601(created_at)
1057 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
1059 id = id |> to_string
1061 case activity.data["type"] do
1066 created_at: created_at,
1067 account: AccountView.render("account.json", %{user: actor, for: user}),
1068 status: StatusView.render("status.json", %{activity: activity, for: user})
1072 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
1077 created_at: created_at,
1078 account: AccountView.render("account.json", %{user: actor, for: user}),
1079 status: StatusView.render("status.json", %{activity: liked_activity, for: user})
1083 announced_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: announced_activity, for: user})
1097 created_at: created_at,
1098 account: AccountView.render("account.json", %{user: actor, for: user})
1106 def get_filters(%{assigns: %{user: user}} = conn, _) do
1107 filters = Pleroma.Filter.get_filters(user)
1108 res = FilterView.render("filters.json", filters: filters)
1113 %{assigns: %{user: user}} = conn,
1114 %{"phrase" => phrase, "context" => context} = params
1116 query = %Pleroma.Filter{
1120 hide: Map.get(params, "irreversible", nil),
1121 whole_word: Map.get(params, "boolean", true)
1125 {:ok, response} = Pleroma.Filter.create(query)
1126 res = FilterView.render("filter.json", filter: response)
1130 def get_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do
1131 filter = Pleroma.Filter.get(filter_id, user)
1132 res = FilterView.render("filter.json", filter: filter)
1137 %{assigns: %{user: user}} = conn,
1138 %{"phrase" => phrase, "context" => context, "id" => filter_id} = params
1140 query = %Pleroma.Filter{
1142 filter_id: filter_id,
1145 hide: Map.get(params, "irreversible", nil),
1146 whole_word: Map.get(params, "boolean", true)
1150 {:ok, response} = Pleroma.Filter.update(query)
1151 res = FilterView.render("filter.json", filter: response)
1155 def delete_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do
1156 query = %Pleroma.Filter{
1158 filter_id: filter_id
1161 {:ok, _} = Pleroma.Filter.delete(query)
1165 def errors(conn, _) do
1168 |> json("Something went wrong")
1171 def suggestions(%{assigns: %{user: user}} = conn, _) do
1172 suggestions = Pleroma.Config.get(:suggestions)
1174 if Keyword.get(suggestions, :enabled, false) do
1175 api = Keyword.get(suggestions, :third_party_engine, "")
1176 timeout = Keyword.get(suggestions, :timeout, 5000)
1177 limit = Keyword.get(suggestions, :limit, 23)
1179 host = Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host])
1181 user = user.nickname
1182 url = String.replace(api, "{{host}}", host) |> String.replace("{{user}}", user)
1184 with {:ok, %{status: 200, body: body}} <-
1185 @httpoison.get(url, [], timeout: timeout, recv_timeout: timeout),
1186 {:ok, data} <- Jason.decode(body) do
1188 Enum.slice(data, 0, limit)
1193 case User.get_or_fetch(x["acct"]) do
1200 Map.put(x, "avatar", MediaProxy.url(x["avatar"]))
1203 Map.put(x, "avatar_static", MediaProxy.url(x["avatar_static"]))
1209 e -> Logger.error("Could not retrieve suggestions at fetch #{url}, #{inspect(e)}")
1216 def try_render(conn, renderer, target, params)
1217 when is_binary(target) do
1218 res = render(conn, renderer, target, params)
1223 |> json(%{error: "Can't display this activity"})
1229 def try_render(conn, _, _, _) do
1232 |> json(%{error: "Can't display this activity"})