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 if bio = params["note"] do
40 Map.put(params, "bio", bio)
46 if name = params["display_name"] do
47 Map.put(params, "name", name)
53 if avatar = params["avatar"] do
54 with %Plug.Upload{} <- avatar,
55 {:ok, object} <- ActivityPub.upload(avatar, type: :avatar),
56 change = Ecto.Changeset.change(user, %{avatar: object.data}),
57 {:ok, user} = User.update_and_set_cache(change) do
67 if banner = params["header"] do
68 with %Plug.Upload{} <- banner,
69 {:ok, object} <- ActivityPub.upload(banner, type: :banner),
70 new_info <- Map.put(user.info, "banner", object.data),
71 change <- User.info_changeset(user, %{info: new_info}),
72 {:ok, user} <- User.update_and_set_cache(change) do
82 if locked = params["locked"] do
83 with locked <- locked == "true",
84 new_info <- Map.put(user.info, "locked", locked),
85 change <- User.info_changeset(user, %{info: new_info}),
86 {:ok, user} <- User.update_and_set_cache(change) do
95 with changeset <- User.update_changeset(user, params),
96 {:ok, user} <- User.update_and_set_cache(changeset) do
97 if original_user != user do
98 CommonAPI.update(user)
101 json(conn, AccountView.render("account.json", %{user: user, for: user}))
106 |> json(%{error: "Invalid request"})
110 def verify_credentials(%{assigns: %{user: user}} = conn, _) do
111 account = AccountView.render("account.json", %{user: user, for: user})
115 def user(%{assigns: %{user: for_user}} = conn, %{"id" => id}) do
116 with %User{} = user <- Repo.get(User, id) do
117 account = AccountView.render("account.json", %{user: user, for: for_user})
123 |> json(%{error: "Can't find user"})
127 @mastodon_api_level "2.5.0"
129 def masto_instance(conn, _params) do
130 instance = Pleroma.Config.get(:instance)
134 title: Keyword.get(instance, :name),
135 description: Keyword.get(instance, :description),
136 version: "#{@mastodon_api_level} (compatible; #{Pleroma.Application.named_version()})",
137 email: Keyword.get(instance, :email),
139 streaming_api: String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws")
141 stats: Stats.get_stats(),
142 thumbnail: Web.base_url() <> "/instance/thumbnail.jpeg",
143 max_toot_chars: Keyword.get(instance, :limit)
149 def peers(conn, _params) do
150 json(conn, Stats.get_peers())
153 defp mastodonized_emoji do
154 Pleroma.Emoji.get_all()
155 |> Enum.map(fn {shortcode, relative_url} ->
156 url = to_string(URI.merge(Web.base_url(), relative_url))
159 "shortcode" => shortcode,
161 "visible_in_picker" => true,
167 def custom_emojis(conn, _params) do
168 mastodon_emoji = mastodonized_emoji()
169 json(conn, mastodon_emoji)
172 defp add_link_headers(conn, method, activities, param \\ nil, params \\ %{}) do
173 last = List.last(activities)
174 first = List.first(activities)
180 {next_url, prev_url} =
184 Pleroma.Web.Endpoint,
187 Map.merge(params, %{max_id: min})
190 Pleroma.Web.Endpoint,
193 Map.merge(params, %{since_id: max})
199 Pleroma.Web.Endpoint,
201 Map.merge(params, %{max_id: min})
204 Pleroma.Web.Endpoint,
206 Map.merge(params, %{since_id: max})
212 |> put_resp_header("link", "<#{next_url}>; rel=\"next\", <#{prev_url}>; rel=\"prev\"")
218 def home_timeline(%{assigns: %{user: user}} = conn, params) do
221 |> Map.put("type", ["Create", "Announce"])
222 |> Map.put("blocking_user", user)
223 |> Map.put("user", user)
226 ActivityPub.fetch_activities([user.ap_id | user.following], params)
227 |> ActivityPub.contain_timeline(user)
231 |> add_link_headers(:home_timeline, activities)
232 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
235 def public_timeline(%{assigns: %{user: user}} = conn, params) do
236 local_only = params["local"] in [true, "True", "true", "1"]
240 |> Map.put("type", ["Create", "Announce"])
241 |> Map.put("local_only", local_only)
242 |> Map.put("blocking_user", user)
245 ActivityPub.fetch_public_activities(params)
249 |> add_link_headers(:public_timeline, activities, false, %{"local" => local_only})
250 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
253 def user_statuses(%{assigns: %{user: reading_user}} = conn, params) do
254 with %User{} = user <- Repo.get(User, params["id"]) do
255 # Since Pleroma has no "pinned" posts feature, we'll just set an empty list here
257 if params["pinned"] == "true" do
260 ActivityPub.fetch_user_activities(user, reading_user, params)
264 |> add_link_headers(:user_statuses, activities, params["id"])
265 |> render(StatusView, "index.json", %{
266 activities: activities,
273 def dm_timeline(%{assigns: %{user: user}} = conn, params) do
275 ActivityPub.fetch_activities_query(
277 Map.merge(params, %{"type" => "Create", visibility: "direct"})
280 activities = Repo.all(query)
283 |> add_link_headers(:dm_timeline, activities)
284 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
287 def get_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
288 with %Activity{} = activity <- Repo.get(Activity, id),
289 true <- ActivityPub.visible_for_user?(activity, user) do
290 try_render(conn, StatusView, "status.json", %{activity: activity, for: user})
294 def get_context(%{assigns: %{user: user}} = conn, %{"id" => id}) do
295 with %Activity{} = activity <- Repo.get(Activity, id),
297 ActivityPub.fetch_activities_for_context(activity.data["context"], %{
298 "blocking_user" => user,
302 activities |> Enum.filter(fn %{id: aid} -> to_string(aid) != to_string(id) end),
304 activities |> Enum.filter(fn %{data: %{"type" => type}} -> type == "Create" end),
305 grouped_activities <- Enum.group_by(activities, fn %{id: id} -> id < activity.id end) do
311 activities: grouped_activities[true] || [],
319 activities: grouped_activities[false] || [],
329 def post_status(conn, %{"status" => "", "media_ids" => media_ids} = params)
330 when length(media_ids) > 0 do
333 |> Map.put("status", ".")
335 post_status(conn, params)
338 def post_status(%{assigns: %{user: user}} = conn, %{"status" => _} = params) do
341 |> Map.put("in_reply_to_status_id", params["in_reply_to_id"])
342 |> Map.put("no_attachment_links", true)
345 case get_req_header(conn, "idempotency-key") do
347 _ -> Ecto.UUID.generate()
351 Cachex.fetch!(:idempotency_cache, idempotency_key, fn _ -> CommonAPI.post(user, params) end)
353 try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
356 def delete_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
357 with {:ok, %Activity{}} <- CommonAPI.delete(id, user) do
363 |> json(%{error: "Can't delete this post"})
367 def reblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
368 with {:ok, announce, _activity} <- CommonAPI.repeat(ap_id_or_id, user) do
369 try_render(conn, StatusView, "status.json", %{activity: announce, for: user, as: :activity})
373 def unreblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
374 with {:ok, _unannounce, %{data: %{"id" => id}}} <- CommonAPI.unrepeat(ap_id_or_id, user),
375 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
376 try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
380 def fav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
381 with {:ok, _fav, %{data: %{"id" => id}}} <- CommonAPI.favorite(ap_id_or_id, user),
382 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
383 try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
387 def unfav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
388 with {:ok, _, _, %{data: %{"id" => id}}} <- CommonAPI.unfavorite(ap_id_or_id, user),
389 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
390 try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
394 def notifications(%{assigns: %{user: user}} = conn, params) do
395 notifications = Notification.for_user(user, params)
398 Enum.map(notifications, fn x ->
399 render_notification(user, x)
404 |> add_link_headers(:notifications, notifications)
408 def get_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
409 with {:ok, notification} <- Notification.get(user, id) do
410 json(conn, render_notification(user, notification))
414 |> put_resp_content_type("application/json")
415 |> send_resp(403, Jason.encode!(%{"error" => reason}))
419 def clear_notifications(%{assigns: %{user: user}} = conn, _params) do
420 Notification.clear(user)
424 def dismiss_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
425 with {:ok, _notif} <- Notification.dismiss(user, id) do
430 |> put_resp_content_type("application/json")
431 |> send_resp(403, Jason.encode!(%{"error" => reason}))
435 def relationships(%{assigns: %{user: user}} = conn, %{"id" => id}) do
437 q = from(u in User, where: u.id in ^id)
438 targets = Repo.all(q)
439 render(conn, AccountView, "relationships.json", %{user: user, targets: targets})
442 # Instead of returning a 400 when no "id" params is present, Mastodon returns an empty array.
443 def relationships(%{assigns: %{user: user}} = conn, _) do
448 def update_media(%{assigns: %{user: _}} = conn, data) do
449 with %Object{} = object <- Repo.get(Object, data["id"]),
450 true <- is_binary(data["description"]),
451 description <- data["description"] do
452 new_data = %{object.data | "name" => description}
454 change = Object.change(object, %{data: new_data})
455 {:ok, _} = Repo.update(change)
459 |> Map.put("id", object.id)
461 render(conn, StatusView, "attachment.json", %{attachment: data})
465 def upload(%{assigns: %{user: _}} = conn, %{"file" => file} = data) do
466 with {:ok, object} <- ActivityPub.upload(file, description: Map.get(data, "description")) do
467 change = Object.change(object, %{data: object.data})
468 {:ok, object} = Repo.update(change)
472 |> Map.put("id", object.id)
474 render(conn, StatusView, "attachment.json", %{attachment: objdata})
478 def favourited_by(conn, %{"id" => id}) do
479 with %Activity{data: %{"object" => %{"likes" => likes}}} <- Repo.get(Activity, id) do
480 q = from(u in User, where: u.ap_id in ^likes)
482 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
488 def reblogged_by(conn, %{"id" => id}) do
489 with %Activity{data: %{"object" => %{"announcements" => announces}}} <- Repo.get(Activity, id) do
490 q = from(u in User, where: u.ap_id in ^announces)
492 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
498 def hashtag_timeline(%{assigns: %{user: user}} = conn, params) do
499 local_only = params["local"] in [true, "True", "true", "1"]
503 |> Map.put("type", "Create")
504 |> Map.put("local_only", local_only)
505 |> Map.put("blocking_user", user)
506 |> Map.put("tag", String.downcase(params["tag"]))
509 ActivityPub.fetch_public_activities(params)
513 |> add_link_headers(:hashtag_timeline, activities, params["tag"], %{"local" => local_only})
514 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
518 def followers(conn, %{"id" => id}) do
519 with %User{} = user <- Repo.get(User, id),
520 {:ok, followers} <- User.get_followers(user) do
521 render(conn, AccountView, "accounts.json", %{users: followers, as: :user})
525 def following(conn, %{"id" => id}) do
526 with %User{} = user <- Repo.get(User, id),
527 {:ok, followers} <- User.get_friends(user) do
528 render(conn, AccountView, "accounts.json", %{users: followers, as: :user})
532 def follow_requests(%{assigns: %{user: followed}} = conn, _params) do
533 with {:ok, follow_requests} <- User.get_follow_requests(followed) do
534 render(conn, AccountView, "accounts.json", %{users: follow_requests, as: :user})
538 def authorize_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
539 with %User{} = follower <- Repo.get(User, id),
540 {:ok, follower} <- User.maybe_follow(follower, followed),
541 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
542 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "accept"),
544 ActivityPub.accept(%{
545 to: [follower.ap_id],
546 actor: followed.ap_id,
547 object: follow_activity.data["id"],
550 render(conn, AccountView, "relationship.json", %{user: followed, target: follower})
554 |> put_resp_content_type("application/json")
555 |> send_resp(403, Jason.encode!(%{"error" => message}))
559 def reject_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
560 with %User{} = follower <- Repo.get(User, id),
561 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
562 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "reject"),
564 ActivityPub.reject(%{
565 to: [follower.ap_id],
566 actor: followed.ap_id,
567 object: follow_activity.data["id"],
570 render(conn, AccountView, "relationship.json", %{user: followed, target: follower})
574 |> put_resp_content_type("application/json")
575 |> send_resp(403, Jason.encode!(%{"error" => message}))
579 def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
580 with %User{} = followed <- Repo.get(User, id),
581 {:ok, follower} <- User.maybe_direct_follow(follower, followed),
582 {:ok, _activity} <- ActivityPub.follow(follower, followed),
583 {:ok, follower, followed} <-
584 User.wait_and_refresh(
585 Pleroma.Config.get([:activitypub, :follow_handshake_timeout]),
589 render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
593 |> put_resp_content_type("application/json")
594 |> send_resp(403, Jason.encode!(%{"error" => message}))
598 def follow(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do
599 with %User{} = followed <- Repo.get_by(User, nickname: uri),
600 {:ok, follower} <- User.maybe_direct_follow(follower, followed),
601 {:ok, _activity} <- ActivityPub.follow(follower, followed) do
602 render(conn, AccountView, "account.json", %{user: followed, for: follower})
606 |> put_resp_content_type("application/json")
607 |> send_resp(403, Jason.encode!(%{"error" => message}))
611 def unfollow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
612 with %User{} = followed <- Repo.get(User, id),
613 {:ok, _activity} <- ActivityPub.unfollow(follower, followed),
614 {:ok, follower, _} <- User.unfollow(follower, followed) do
615 render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
619 def block(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
620 with %User{} = blocked <- Repo.get(User, id),
621 {:ok, blocker} <- User.block(blocker, blocked),
622 {:ok, _activity} <- ActivityPub.block(blocker, blocked) do
623 render(conn, AccountView, "relationship.json", %{user: blocker, target: blocked})
627 |> put_resp_content_type("application/json")
628 |> send_resp(403, Jason.encode!(%{"error" => message}))
632 def unblock(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
633 with %User{} = blocked <- Repo.get(User, id),
634 {:ok, blocker} <- User.unblock(blocker, blocked),
635 {:ok, _activity} <- ActivityPub.unblock(blocker, blocked) do
636 render(conn, AccountView, "relationship.json", %{user: blocker, target: blocked})
640 |> put_resp_content_type("application/json")
641 |> send_resp(403, Jason.encode!(%{"error" => message}))
645 # TODO: Use proper query
646 def blocks(%{assigns: %{user: user}} = conn, _) do
647 with blocked_users <- user.info["blocks"] || [],
648 accounts <- Enum.map(blocked_users, fn ap_id -> User.get_cached_by_ap_id(ap_id) end) do
649 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
654 def domain_blocks(%{assigns: %{user: %{info: info}}} = conn, _) do
655 json(conn, info["domain_blocks"] || [])
658 def block_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
659 User.block_domain(blocker, domain)
663 def unblock_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
664 User.unblock_domain(blocker, domain)
668 def status_search(query) do
670 if Regex.match?(~r/https?:/, query) do
671 with {:ok, object} <- ActivityPub.fetch_object_from_id(query) do
672 [Activity.get_create_activity_by_object_ap_id(object.data["id"])]
681 where: fragment("?->>'type' = 'Create'", a.data),
682 where: "https://www.w3.org/ns/activitystreams#Public" in a.recipients,
685 "to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)",
690 order_by: [desc: :id]
693 Repo.all(q) ++ fetched
696 def search2(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
697 accounts = User.search(query, params["resolve"] == "true")
699 statuses = status_search(query)
701 tags_path = Web.base_url() <> "/tag/"
706 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
707 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
708 |> Enum.map(fn tag -> %{name: tag, url: tags_path <> tag} end)
711 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
713 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
720 def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
721 accounts = User.search(query, params["resolve"] == "true")
723 statuses = status_search(query)
728 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
729 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
732 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
734 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
741 def account_search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
742 accounts = User.search(query, params["resolve"] == "true")
744 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
749 def favourites(%{assigns: %{user: user}} = conn, _) do
752 |> Map.put("type", "Create")
753 |> Map.put("favorited_by", user.ap_id)
754 |> Map.put("blocking_user", user)
757 ActivityPub.fetch_public_activities(params)
761 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
764 def get_lists(%{assigns: %{user: user}} = conn, opts) do
765 lists = Pleroma.List.for_user(user, opts)
766 res = ListView.render("lists.json", lists: lists)
770 def get_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
771 with %Pleroma.List{} = list <- Pleroma.List.get(id, user) do
772 res = ListView.render("list.json", list: list)
775 _e -> json(conn, "error")
779 def account_lists(%{assigns: %{user: user}} = conn, %{"id" => account_id}) do
780 lists = Pleroma.List.get_lists_account_belongs(user, account_id)
781 res = ListView.render("lists.json", lists: lists)
785 def delete_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
786 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
787 {:ok, _list} <- Pleroma.List.delete(list) do
795 def create_list(%{assigns: %{user: user}} = conn, %{"title" => title}) do
796 with {:ok, %Pleroma.List{} = list} <- Pleroma.List.create(title, user) do
797 res = ListView.render("list.json", list: list)
802 def add_to_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
804 |> Enum.each(fn account_id ->
805 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
806 %User{} = followed <- Repo.get(User, account_id) do
807 Pleroma.List.follow(list, followed)
814 def remove_from_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(Pleroma.User, account_id) do
819 Pleroma.List.unfollow(list, followed)
826 def list_accounts(%{assigns: %{user: user}} = conn, %{"id" => id}) do
827 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
828 {:ok, users} = Pleroma.List.get_following(list) do
829 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
833 def rename_list(%{assigns: %{user: user}} = conn, %{"id" => id, "title" => title}) do
834 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
835 {:ok, list} <- Pleroma.List.rename(list, title) do
836 res = ListView.render("list.json", list: list)
844 def list_timeline(%{assigns: %{user: user}} = conn, %{"list_id" => id} = params) do
845 with %Pleroma.List{title: title, following: following} <- Pleroma.List.get(id, user) do
848 |> Map.put("type", "Create")
849 |> Map.put("blocking_user", user)
851 # we must filter the following list for the user to avoid leaking statuses the user
852 # does not actually have permission to see (for more info, peruse security issue #270).
855 |> Enum.filter(fn x -> x in user.following end)
858 ActivityPub.fetch_activities_bounded(following_to, following, params)
862 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
867 |> json(%{error: "Error."})
871 def index(%{assigns: %{user: user}} = conn, _params) do
874 |> get_session(:oauth_token)
877 mastodon_emoji = mastodonized_emoji()
879 limit = Pleroma.Config.get([:instance, :limit])
882 Map.put(%{}, user.id, AccountView.render("account.json", %{user: user, for: user}))
887 streaming_api_base_url:
888 String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws"),
891 domain: Pleroma.Web.Endpoint.host(),
894 unfollow_modal: false,
897 auto_play_gif: false,
898 display_sensitive_media: false,
899 reduce_motion: false,
900 max_toot_chars: limit
903 delete_others_notice: !!user.info["is_moderator"]
907 default_privacy: user.info["default_scope"] || "public",
908 default_sensitive: false
910 media_attachments: %{
911 accept_content_types: [
927 Map.get(user.info, "settings") ||
957 push_subscription: nil,
959 custom_emojis: mastodon_emoji,
966 |> render(MastodonView, "index.html", %{initial_state: initial_state})
969 |> redirect(to: "/web/login")
973 def put_settings(%{assigns: %{user: user}} = conn, %{"data" => settings} = _params) do
974 with new_info <- Map.put(user.info, "settings", settings),
975 change <- User.info_changeset(user, %{info: new_info}),
976 {:ok, _user} <- User.update_and_set_cache(change) do
982 |> json(%{error: inspect(e)})
986 def login(conn, %{"code" => code}) do
987 with {:ok, app} <- get_or_make_app(),
988 %Authorization{} = auth <- Repo.get_by(Authorization, token: code, app_id: app.id),
989 {:ok, token} <- Token.exchange_token(app, auth) do
991 |> put_session(:oauth_token, token.token)
992 |> redirect(to: "/web/getting-started")
996 def login(conn, _) do
997 with {:ok, app} <- get_or_make_app() do
999 o_auth_path(conn, :authorize,
1000 response_type: "code",
1001 client_id: app.client_id,
1007 |> redirect(to: path)
1011 defp get_or_make_app() do
1012 with %App{} = app <- Repo.get_by(App, client_name: "Mastodon-Local") do
1017 App.register_changeset(%App{}, %{
1018 client_name: "Mastodon-Local",
1020 scopes: "read,write,follow"
1027 def logout(conn, _) do
1030 |> redirect(to: "/")
1033 def relationship_noop(%{assigns: %{user: user}} = conn, %{"id" => id}) do
1034 Logger.debug("Unimplemented, returning unmodified relationship")
1036 with %User{} = target <- Repo.get(User, id) do
1037 render(conn, AccountView, "relationship.json", %{user: user, target: target})
1041 def empty_array(conn, _) do
1042 Logger.debug("Unimplemented, returning an empty array")
1046 def empty_object(conn, _) do
1047 Logger.debug("Unimplemented, returning an empty object")
1051 def render_notification(user, %{id: id, activity: activity, inserted_at: created_at} = _params) do
1052 actor = User.get_cached_by_ap_id(activity.data["actor"])
1055 NaiveDateTime.to_iso8601(created_at)
1056 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
1058 id = id |> to_string
1060 case activity.data["type"] do
1065 created_at: created_at,
1066 account: AccountView.render("account.json", %{user: actor, for: user}),
1067 status: StatusView.render("status.json", %{activity: activity, for: user})
1071 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
1076 created_at: created_at,
1077 account: AccountView.render("account.json", %{user: actor, for: user}),
1078 status: StatusView.render("status.json", %{activity: liked_activity, for: user})
1082 announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
1087 created_at: created_at,
1088 account: AccountView.render("account.json", %{user: actor, for: user}),
1089 status: StatusView.render("status.json", %{activity: announced_activity, for: user})
1096 created_at: created_at,
1097 account: AccountView.render("account.json", %{user: actor, for: user})
1105 def get_filters(%{assigns: %{user: user}} = conn, _) do
1106 filters = Pleroma.Filter.get_filters(user)
1107 res = FilterView.render("filters.json", filters: filters)
1112 %{assigns: %{user: user}} = conn,
1113 %{"phrase" => phrase, "context" => context} = params
1115 query = %Pleroma.Filter{
1119 hide: Map.get(params, "irreversible", nil),
1120 whole_word: Map.get(params, "boolean", true)
1124 {:ok, response} = Pleroma.Filter.create(query)
1125 res = FilterView.render("filter.json", filter: response)
1129 def get_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do
1130 filter = Pleroma.Filter.get(filter_id, user)
1131 res = FilterView.render("filter.json", filter: filter)
1136 %{assigns: %{user: user}} = conn,
1137 %{"phrase" => phrase, "context" => context, "id" => filter_id} = params
1139 query = %Pleroma.Filter{
1141 filter_id: filter_id,
1144 hide: Map.get(params, "irreversible", nil),
1145 whole_word: Map.get(params, "boolean", true)
1149 {:ok, response} = Pleroma.Filter.update(query)
1150 res = FilterView.render("filter.json", filter: response)
1154 def delete_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do
1155 query = %Pleroma.Filter{
1157 filter_id: filter_id
1160 {:ok, _} = Pleroma.Filter.delete(query)
1164 def errors(conn, _) do
1167 |> json("Something went wrong")
1170 def suggestions(%{assigns: %{user: user}} = conn, _) do
1171 suggestions = Pleroma.Config.get(:suggestions)
1173 if Keyword.get(suggestions, :enabled, false) do
1174 api = Keyword.get(suggestions, :third_party_engine, "")
1175 timeout = Keyword.get(suggestions, :timeout, 5000)
1176 limit = Keyword.get(suggestions, :limit, 23)
1178 host = Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host])
1180 user = user.nickname
1181 url = String.replace(api, "{{host}}", host) |> String.replace("{{user}}", user)
1183 with {:ok, %{status_code: 200, body: body}} <-
1184 @httpoison.get(url, [], timeout: timeout, recv_timeout: timeout),
1185 {:ok, data} <- Jason.decode(body) do
1187 Enum.slice(data, 0, limit)
1192 case User.get_or_fetch(x["acct"]) do
1199 Map.put(x, "avatar", MediaProxy.url(x["avatar"]))
1202 Map.put(x, "avatar_static", MediaProxy.url(x["avatar_static"]))
1208 e -> Logger.error("Could not retrieve suggestions at fetch #{url}, #{inspect(e)}")
1215 def try_render(conn, renderer, target, params)
1216 when is_binary(target) do
1217 res = render(conn, renderer, target, params)
1222 |> json(%{error: "Can't display this activity"})
1228 def try_render(conn, _, _, _) do
1231 |> json(%{error: "Can't display this activity"})