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 @instance Application.get_env(:pleroma, :instance)
136 @mastodon_api_level "2.5.0"
138 def masto_instance(conn, _params) do
141 title: Keyword.get(@instance, :name),
142 description: Keyword.get(@instance, :description),
143 version: "#{@mastodon_api_level} (compatible; #{Keyword.get(@instance, :version)})",
144 email: Keyword.get(@instance, :email),
146 streaming_api: String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws")
148 stats: Stats.get_stats(),
149 thumbnail: Web.base_url() <> "/instance/thumbnail.jpeg",
150 max_toot_chars: Keyword.get(@instance, :limit)
156 def peers(conn, _params) do
157 json(conn, Stats.get_peers())
160 defp mastodonized_emoji do
161 Pleroma.Emoji.get_all()
162 |> Enum.map(fn {shortcode, relative_url} ->
163 url = to_string(URI.merge(Web.base_url(), relative_url))
166 "shortcode" => shortcode,
168 "visible_in_picker" => true,
174 def custom_emojis(conn, _params) do
175 mastodon_emoji = mastodonized_emoji()
176 json(conn, mastodon_emoji)
179 defp add_link_headers(conn, method, activities, param \\ nil, params \\ %{}) do
180 last = List.last(activities)
181 first = List.first(activities)
187 {next_url, prev_url} =
191 Pleroma.Web.Endpoint,
194 Map.merge(params, %{max_id: min})
197 Pleroma.Web.Endpoint,
200 Map.merge(params, %{since_id: max})
206 Pleroma.Web.Endpoint,
208 Map.merge(params, %{max_id: min})
211 Pleroma.Web.Endpoint,
213 Map.merge(params, %{since_id: max})
219 |> put_resp_header("link", "<#{next_url}>; rel=\"next\", <#{prev_url}>; rel=\"prev\"")
225 def home_timeline(%{assigns: %{user: user}} = conn, params) do
228 |> Map.put("type", ["Create", "Announce"])
229 |> Map.put("blocking_user", user)
230 |> Map.put("user", user)
233 ActivityPub.fetch_activities([user.ap_id | user.following], params)
234 |> ActivityPub.contain_timeline(user)
238 |> add_link_headers(:home_timeline, activities)
239 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
242 def public_timeline(%{assigns: %{user: user}} = conn, params) do
243 local_only = params["local"] in [true, "True", "true", "1"]
247 |> Map.put("type", ["Create", "Announce"])
248 |> Map.put("local_only", local_only)
249 |> Map.put("blocking_user", user)
252 ActivityPub.fetch_public_activities(params)
256 |> add_link_headers(:public_timeline, activities, false, %{"local" => local_only})
257 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
260 def user_statuses(%{assigns: %{user: reading_user}} = conn, params) do
261 with %User{} = user <- Repo.get(User, params["id"]) do
262 # Since Pleroma has no "pinned" posts feature, we'll just set an empty list here
264 if params["pinned"] == "true" do
267 ActivityPub.fetch_user_activities(user, reading_user, params)
271 |> add_link_headers(:user_statuses, activities, params["id"])
272 |> render(StatusView, "index.json", %{
273 activities: activities,
280 def dm_timeline(%{assigns: %{user: user}} = conn, _params) do
282 ActivityPub.fetch_activities_query([user.ap_id], %{"type" => "Create", visibility: "direct"})
284 activities = Repo.all(query)
287 |> add_link_headers(:dm_timeline, activities)
288 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
291 def get_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
292 with %Activity{} = activity <- Repo.get(Activity, id),
293 true <- ActivityPub.visible_for_user?(activity, user) do
294 try_render(conn, StatusView, "status.json", %{activity: activity, for: user})
298 def get_context(%{assigns: %{user: user}} = conn, %{"id" => id}) do
299 with %Activity{} = activity <- Repo.get(Activity, id),
301 ActivityPub.fetch_activities_for_context(activity.data["context"], %{
302 "blocking_user" => user,
306 activities |> Enum.filter(fn %{id: aid} -> to_string(aid) != to_string(id) end),
308 activities |> Enum.filter(fn %{data: %{"type" => type}} -> type == "Create" end),
309 grouped_activities <- Enum.group_by(activities, fn %{id: id} -> id < activity.id end) do
315 activities: grouped_activities[true] || [],
323 activities: grouped_activities[false] || [],
333 def post_status(conn, %{"status" => "", "media_ids" => media_ids} = params)
334 when length(media_ids) > 0 do
337 |> Map.put("status", ".")
339 post_status(conn, params)
342 def post_status(%{assigns: %{user: user}} = conn, %{"status" => _} = params) do
345 |> Map.put("in_reply_to_status_id", params["in_reply_to_id"])
346 |> Map.put("no_attachment_links", true)
349 case get_req_header(conn, "idempotency-key") do
351 _ -> Ecto.UUID.generate()
355 Cachex.fetch!(:idempotency_cache, idempotency_key, fn _ -> CommonAPI.post(user, params) end)
357 try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
360 def delete_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
361 with {:ok, %Activity{}} <- CommonAPI.delete(id, user) do
367 |> json(%{error: "Can't delete this post"})
371 def reblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
372 with {:ok, announce, _activity} <- CommonAPI.repeat(ap_id_or_id, user) do
373 try_render(conn, StatusView, "status.json", %{activity: announce, for: user, as: :activity})
377 def unreblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
378 with {:ok, _unannounce, %{data: %{"id" => id}}} <- CommonAPI.unrepeat(ap_id_or_id, user),
379 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
380 try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
384 def fav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
385 with {:ok, _fav, %{data: %{"id" => id}}} <- CommonAPI.favorite(ap_id_or_id, user),
386 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
387 try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
391 def unfav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
392 with {:ok, _, _, %{data: %{"id" => id}}} <- CommonAPI.unfavorite(ap_id_or_id, user),
393 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
394 try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
398 def notifications(%{assigns: %{user: user}} = conn, params) do
399 notifications = Notification.for_user(user, params)
402 Enum.map(notifications, fn x ->
403 render_notification(user, x)
408 |> add_link_headers(:notifications, notifications)
412 def get_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
413 with {:ok, notification} <- Notification.get(user, id) do
414 json(conn, render_notification(user, notification))
418 |> put_resp_content_type("application/json")
419 |> send_resp(403, Jason.encode!(%{"error" => reason}))
423 def clear_notifications(%{assigns: %{user: user}} = conn, _params) do
424 Notification.clear(user)
428 def dismiss_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
429 with {:ok, _notif} <- Notification.dismiss(user, id) do
434 |> put_resp_content_type("application/json")
435 |> send_resp(403, Jason.encode!(%{"error" => reason}))
439 def relationships(%{assigns: %{user: user}} = conn, %{"id" => id}) do
441 q = from(u in User, where: u.id in ^id)
442 targets = Repo.all(q)
443 render(conn, AccountView, "relationships.json", %{user: user, targets: targets})
446 def update_media(%{assigns: %{user: _}} = conn, data) do
447 with %Object{} = object <- Repo.get(Object, data["id"]),
448 true <- is_binary(data["description"]),
449 description <- data["description"] do
450 new_data = %{object.data | "name" => description}
452 change = Object.change(object, %{data: new_data})
453 {:ok, _} = Repo.update(change)
457 |> Map.put("id", object.id)
459 render(conn, StatusView, "attachment.json", %{attachment: data})
463 def upload(%{assigns: %{user: _}} = conn, %{"file" => file} = data) do
464 with {:ok, object} <- ActivityPub.upload(file) do
466 if Map.has_key?(data, "description") do
467 Map.put(object.data, "name", data["description"])
472 change = Object.change(object, %{data: objdata})
473 {:ok, object} = Repo.update(change)
477 |> Map.put("id", object.id)
479 render(conn, StatusView, "attachment.json", %{attachment: objdata})
483 def favourited_by(conn, %{"id" => id}) do
484 with %Activity{data: %{"object" => %{"likes" => likes}}} <- Repo.get(Activity, id) do
485 q = from(u in User, where: u.ap_id in ^likes)
487 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
493 def reblogged_by(conn, %{"id" => id}) do
494 with %Activity{data: %{"object" => %{"announcements" => announces}}} <- Repo.get(Activity, id) do
495 q = from(u in User, where: u.ap_id in ^announces)
497 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
503 def hashtag_timeline(%{assigns: %{user: user}} = conn, params) do
504 local_only = params["local"] in [true, "True", "true", "1"]
508 |> Map.put("type", "Create")
509 |> Map.put("local_only", local_only)
510 |> Map.put("blocking_user", user)
511 |> Map.put("tag", String.downcase(params["tag"]))
514 ActivityPub.fetch_public_activities(params)
518 |> add_link_headers(:hashtag_timeline, activities, params["tag"], %{"local" => local_only})
519 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
523 def followers(conn, %{"id" => id}) do
524 with %User{} = user <- Repo.get(User, id),
525 {:ok, followers} <- User.get_followers(user) do
526 render(conn, AccountView, "accounts.json", %{users: followers, as: :user})
530 def following(conn, %{"id" => id}) do
531 with %User{} = user <- Repo.get(User, id),
532 {:ok, followers} <- User.get_friends(user) do
533 render(conn, AccountView, "accounts.json", %{users: followers, as: :user})
537 def follow_requests(%{assigns: %{user: followed}} = conn, _params) do
538 with {:ok, follow_requests} <- User.get_follow_requests(followed) do
539 render(conn, AccountView, "accounts.json", %{users: follow_requests, as: :user})
543 def authorize_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
544 with %User{} = follower <- Repo.get(User, id),
545 {:ok, follower} <- User.maybe_follow(follower, followed),
546 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
547 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "accept"),
549 ActivityPub.accept(%{
550 to: [follower.ap_id],
551 actor: followed.ap_id,
552 object: follow_activity.data["id"],
555 render(conn, AccountView, "relationship.json", %{user: followed, target: follower})
559 |> put_resp_content_type("application/json")
560 |> send_resp(403, Jason.encode!(%{"error" => message}))
564 def reject_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
565 with %User{} = follower <- Repo.get(User, id),
566 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
567 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "reject"),
569 ActivityPub.reject(%{
570 to: [follower.ap_id],
571 actor: followed.ap_id,
572 object: follow_activity.data["id"],
575 render(conn, AccountView, "relationship.json", %{user: followed, target: follower})
579 |> put_resp_content_type("application/json")
580 |> send_resp(403, Jason.encode!(%{"error" => message}))
584 @activitypub Application.get_env(:pleroma, :activitypub)
585 @follow_handshake_timeout Keyword.get(@activitypub, :follow_handshake_timeout)
587 def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
588 with %User{} = followed <- Repo.get(User, id),
589 {:ok, follower} <- User.maybe_direct_follow(follower, followed),
590 {:ok, _activity} <- ActivityPub.follow(follower, followed),
591 {:ok, follower, followed} <-
592 User.wait_and_refresh(@follow_handshake_timeout, follower, followed) do
593 render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
597 |> put_resp_content_type("application/json")
598 |> send_resp(403, Jason.encode!(%{"error" => message}))
602 def follow(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do
603 with %User{} = followed <- Repo.get_by(User, nickname: uri),
604 {:ok, follower} <- User.maybe_direct_follow(follower, followed),
605 {:ok, _activity} <- ActivityPub.follow(follower, followed) do
606 render(conn, AccountView, "account.json", %{user: followed, for: follower})
610 |> put_resp_content_type("application/json")
611 |> send_resp(403, Jason.encode!(%{"error" => message}))
615 def unfollow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
616 with %User{} = followed <- Repo.get(User, id),
617 {:ok, _activity} <- ActivityPub.unfollow(follower, followed),
618 {:ok, follower, _} <- User.unfollow(follower, followed) do
619 render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
623 def block(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
624 with %User{} = blocked <- Repo.get(User, id),
625 {:ok, blocker} <- User.block(blocker, blocked),
626 {:ok, _activity} <- ActivityPub.block(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 def unblock(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
637 with %User{} = blocked <- Repo.get(User, id),
638 {:ok, blocker} <- User.unblock(blocker, blocked),
639 {:ok, _activity} <- ActivityPub.unblock(blocker, blocked) do
640 render(conn, AccountView, "relationship.json", %{user: blocker, target: blocked})
644 |> put_resp_content_type("application/json")
645 |> send_resp(403, Jason.encode!(%{"error" => message}))
649 # TODO: Use proper query
650 def blocks(%{assigns: %{user: user}} = conn, _) do
651 with blocked_users <- user.info["blocks"] || [],
652 accounts <- Enum.map(blocked_users, fn ap_id -> User.get_cached_by_ap_id(ap_id) end) do
653 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
658 def domain_blocks(%{assigns: %{user: %{info: info}}} = conn, _) do
659 json(conn, info["domain_blocks"] || [])
662 def block_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
663 User.block_domain(blocker, domain)
667 def unblock_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
668 User.unblock_domain(blocker, domain)
672 def status_search(query) do
674 if Regex.match?(~r/https?:/, query) do
675 with {:ok, object} <- ActivityPub.fetch_object_from_id(query) do
676 [Activity.get_create_activity_by_object_ap_id(object.data["id"])]
685 where: fragment("?->>'type' = 'Create'", a.data),
686 where: "https://www.w3.org/ns/activitystreams#Public" in a.recipients,
689 "to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)",
694 order_by: [desc: :id]
697 Repo.all(q) ++ fetched
700 def search2(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
701 accounts = User.search(query, params["resolve"] == "true")
703 statuses = status_search(query)
705 tags_path = Web.base_url() <> "/tag/"
710 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
711 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
712 |> Enum.map(fn tag -> %{name: tag, url: tags_path <> tag} end)
715 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
717 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
724 def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
725 accounts = User.search(query, params["resolve"] == "true")
727 statuses = status_search(query)
732 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
733 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
736 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
738 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
745 def account_search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
746 accounts = User.search(query, params["resolve"] == "true")
748 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
753 def favourites(%{assigns: %{user: user}} = conn, _) do
756 |> Map.put("type", "Create")
757 |> Map.put("favorited_by", user.ap_id)
758 |> Map.put("blocking_user", user)
761 ActivityPub.fetch_public_activities(params)
765 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
768 def get_lists(%{assigns: %{user: user}} = conn, opts) do
769 lists = Pleroma.List.for_user(user, opts)
770 res = ListView.render("lists.json", lists: lists)
774 def get_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
775 with %Pleroma.List{} = list <- Pleroma.List.get(id, user) do
776 res = ListView.render("list.json", list: list)
779 _e -> json(conn, "error")
783 def account_lists(%{assigns: %{user: user}} = conn, %{"id" => account_id}) do
784 lists = Pleroma.List.get_lists_account_belongs(user, account_id)
785 res = ListView.render("lists.json", lists: lists)
789 def delete_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
790 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
791 {:ok, _list} <- Pleroma.List.delete(list) do
799 def create_list(%{assigns: %{user: user}} = conn, %{"title" => title}) do
800 with {:ok, %Pleroma.List{} = list} <- Pleroma.List.create(title, user) do
801 res = ListView.render("list.json", list: list)
806 def add_to_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
808 |> Enum.each(fn account_id ->
809 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
810 %User{} = followed <- Repo.get(User, account_id) do
811 Pleroma.List.follow(list, followed)
818 def remove_from_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
820 |> Enum.each(fn account_id ->
821 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
822 %User{} = followed <- Repo.get(Pleroma.User, account_id) do
823 Pleroma.List.unfollow(list, followed)
830 def list_accounts(%{assigns: %{user: user}} = conn, %{"id" => id}) do
831 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
832 {:ok, users} = Pleroma.List.get_following(list) do
833 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
837 def rename_list(%{assigns: %{user: user}} = conn, %{"id" => id, "title" => title}) do
838 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
839 {:ok, list} <- Pleroma.List.rename(list, title) do
840 res = ListView.render("list.json", list: list)
848 def list_timeline(%{assigns: %{user: user}} = conn, %{"list_id" => id} = params) do
849 with %Pleroma.List{title: title, following: following} <- Pleroma.List.get(id, user) do
852 |> Map.put("type", "Create")
853 |> Map.put("blocking_user", user)
855 # we must filter the following list for the user to avoid leaking statuses the user
856 # does not actually have permission to see (for more info, peruse security issue #270).
859 |> Enum.filter(fn x -> x in user.following end)
862 ActivityPub.fetch_activities_bounded(following_to, following, params)
866 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
871 |> json(%{error: "Error."})
875 def index(%{assigns: %{user: user}} = conn, _params) do
878 |> get_session(:oauth_token)
881 mastodon_emoji = mastodonized_emoji()
884 Map.put(%{}, user.id, AccountView.render("account.json", %{user: user, for: user}))
889 streaming_api_base_url:
890 String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws"),
893 domain: Pleroma.Web.Endpoint.host(),
896 unfollow_modal: false,
899 auto_play_gif: false,
900 display_sensitive_media: false,
901 reduce_motion: false,
902 max_toot_chars: Keyword.get(@instance, :limit)
905 delete_others_notice: !!user.info["is_moderator"]
909 default_privacy: user.info["default_scope"] || "public",
910 default_sensitive: false
912 media_attachments: %{
913 accept_content_types: [
929 Map.get(user.info, "settings") ||
959 push_subscription: nil,
961 custom_emojis: mastodon_emoji,
962 char_limit: Keyword.get(@instance, :limit)
968 |> render(MastodonView, "index.html", %{initial_state: initial_state})
971 |> redirect(to: "/web/login")
975 def put_settings(%{assigns: %{user: user}} = conn, %{"data" => settings} = _params) do
976 with new_info <- Map.put(user.info, "settings", settings),
977 change <- User.info_changeset(user, %{info: new_info}),
978 {:ok, _user} <- User.update_and_set_cache(change) do
984 |> json(%{error: inspect(e)})
988 def login(conn, _) do
990 |> render(MastodonView, "login.html", %{error: false})
993 defp get_or_make_app() do
994 with %App{} = app <- Repo.get_by(App, client_name: "Mastodon-Local") do
999 App.register_changeset(%App{}, %{
1000 client_name: "Mastodon-Local",
1002 scopes: "read,write,follow"
1009 def login_post(conn, %{"authorization" => %{"name" => name, "password" => password}}) do
1010 with %User{} = user <- User.get_by_nickname_or_email(name),
1011 true <- Pbkdf2.checkpw(password, user.password_hash),
1012 {:ok, app} <- get_or_make_app(),
1013 {:ok, auth} <- Authorization.create_authorization(app, user),
1014 {:ok, token} <- Token.exchange_token(app, auth) do
1016 |> put_session(:oauth_token, token.token)
1017 |> redirect(to: "/web/getting-started")
1021 |> render(MastodonView, "login.html", %{error: "Wrong username or password"})
1025 def logout(conn, _) do
1028 |> redirect(to: "/")
1031 def relationship_noop(%{assigns: %{user: user}} = conn, %{"id" => id}) do
1032 Logger.debug("Unimplemented, returning unmodified relationship")
1034 with %User{} = target <- Repo.get(User, id) do
1035 render(conn, AccountView, "relationship.json", %{user: user, target: target})
1039 def empty_array(conn, _) do
1040 Logger.debug("Unimplemented, returning an empty array")
1044 def empty_object(conn, _) do
1045 Logger.debug("Unimplemented, returning an empty object")
1049 def render_notification(user, %{id: id, activity: activity, inserted_at: created_at} = _params) do
1050 actor = User.get_cached_by_ap_id(activity.data["actor"])
1053 NaiveDateTime.to_iso8601(created_at)
1054 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
1056 id = id |> to_string
1058 case activity.data["type"] do
1063 created_at: created_at,
1064 account: AccountView.render("account.json", %{user: actor, for: user}),
1065 status: StatusView.render("status.json", %{activity: activity, for: user})
1069 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
1074 created_at: created_at,
1075 account: AccountView.render("account.json", %{user: actor, for: user}),
1076 status: StatusView.render("status.json", %{activity: liked_activity, for: user})
1080 announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
1085 created_at: created_at,
1086 account: AccountView.render("account.json", %{user: actor, for: user}),
1087 status: StatusView.render("status.json", %{activity: announced_activity, for: user})
1094 created_at: created_at,
1095 account: AccountView.render("account.json", %{user: actor, for: user})
1103 def get_filters(%{assigns: %{user: user}} = conn, _) do
1104 filters = Pleroma.Filter.get_filters(user)
1105 res = FilterView.render("filters.json", filters: filters)
1110 %{assigns: %{user: user}} = conn,
1111 %{"phrase" => phrase, "context" => context} = params
1113 query = %Pleroma.Filter{
1117 hide: Map.get(params, "irreversible", nil),
1118 whole_word: Map.get(params, "boolean", true)
1122 {:ok, response} = Pleroma.Filter.create(query)
1123 res = FilterView.render("filter.json", filter: response)
1127 def get_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do
1128 filter = Pleroma.Filter.get(filter_id, user)
1129 res = FilterView.render("filter.json", filter: filter)
1134 %{assigns: %{user: user}} = conn,
1135 %{"phrase" => phrase, "context" => context, "id" => filter_id} = params
1137 query = %Pleroma.Filter{
1139 filter_id: filter_id,
1142 hide: Map.get(params, "irreversible", nil),
1143 whole_word: Map.get(params, "boolean", true)
1147 {:ok, response} = Pleroma.Filter.update(query)
1148 res = FilterView.render("filter.json", filter: response)
1152 def delete_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do
1153 query = %Pleroma.Filter{
1155 filter_id: filter_id
1158 {:ok, _} = Pleroma.Filter.delete(query)
1162 def errors(conn, _) do
1165 |> json("Something went wrong")
1168 @suggestions Application.get_env(:pleroma, :suggestions)
1170 def suggestions(%{assigns: %{user: user}} = conn, _) do
1171 if Keyword.get(@suggestions, :enabled, false) do
1172 api = Keyword.get(@suggestions, :third_party_engine, "")
1173 timeout = Keyword.get(@suggestions, :timeout, 5000)
1174 limit = Keyword.get(@suggestions, :limit, 23)
1177 Application.get_env(:pleroma, Pleroma.Web.Endpoint)
1178 |> Keyword.get(:url)
1179 |> Keyword.get(:host)
1181 user = user.nickname
1182 url = String.replace(api, "{{host}}", host) |> String.replace("{{user}}", user)
1184 with {:ok, %{status_code: 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"})