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}
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}
14 @httpoison Application.get_env(:pleroma, :httpoison)
16 action_fallback(:errors)
18 def create_app(conn, params) do
19 with cs <- App.register_changeset(%App{}, params) |> IO.inspect(),
20 {:ok, app} <- Repo.insert(cs) |> IO.inspect() do
22 id: app.id |> to_string,
23 name: app.client_name,
24 client_id: app.client_id,
25 client_secret: app.client_secret,
26 redirect_uri: app.redirect_uris,
34 def update_credentials(%{assigns: %{user: user}} = conn, params) do
38 if bio = params["note"] do
39 Map.put(params, "bio", bio)
45 if name = params["display_name"] do
46 Map.put(params, "name", name)
52 if avatar = params["avatar"] do
53 with %Plug.Upload{} <- avatar,
54 {:ok, object} <- ActivityPub.upload(avatar),
55 change = Ecto.Changeset.change(user, %{avatar: object.data}),
56 {:ok, user} = User.update_and_set_cache(change) do
66 if banner = params["header"] do
67 with %Plug.Upload{} <- banner,
68 {:ok, object} <- ActivityPub.upload(banner),
69 new_info <- Map.put(user.info, "banner", object.data),
70 change <- User.info_changeset(user, %{info: new_info}),
71 {:ok, user} <- User.update_and_set_cache(change) do
81 if locked = params["locked"] do
82 with locked <- locked == "true",
83 new_info <- Map.put(user.info, "locked", locked),
84 change <- User.info_changeset(user, %{info: new_info}),
85 {:ok, user} <- User.update_and_set_cache(change) do
94 with changeset <- User.update_changeset(user, params),
95 {:ok, user} <- User.update_and_set_cache(changeset) do
96 if original_user != user do
97 CommonAPI.update(user)
100 json(conn, AccountView.render("account.json", %{user: user}))
105 |> json(%{error: "Invalid request"})
109 def verify_credentials(%{assigns: %{user: user}} = conn, _) do
110 account = AccountView.render("account.json", %{user: user})
114 def user(conn, %{"id" => id}) do
115 with %User{} = user <- Repo.get(User, id) do
116 account = AccountView.render("account.json", %{user: user})
122 |> json(%{error: "Can't find user"})
126 @instance Application.get_env(:pleroma, :instance)
127 @mastodon_api_level "2.3.3"
129 def masto_instance(conn, _params) do
132 title: Keyword.get(@instance, :name),
133 description: Keyword.get(@instance, :description),
134 version: "#{@mastodon_api_level} (compatible; #{Keyword.get(@instance, :version)})",
135 email: Keyword.get(@instance, :email),
137 streaming_api: String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws")
139 stats: Stats.get_stats(),
140 thumbnail: Web.base_url() <> "/instance/thumbnail.jpeg",
141 max_toot_chars: Keyword.get(@instance, :limit)
147 def peers(conn, _params) do
148 json(conn, Stats.get_peers())
151 defp mastodonized_emoji do
152 Pleroma.Formatter.get_custom_emoji()
153 |> Enum.map(fn {shortcode, relative_url} ->
154 url = to_string(URI.merge(Web.base_url(), relative_url))
157 "shortcode" => shortcode,
159 "visible_in_picker" => true,
165 def custom_emojis(conn, _params) do
166 mastodon_emoji = mastodonized_emoji()
167 json(conn, mastodon_emoji)
170 defp add_link_headers(conn, method, activities, param \\ nil, params \\ %{}) do
171 last = List.last(activities)
172 first = List.first(activities)
178 {next_url, prev_url} =
182 Pleroma.Web.Endpoint,
185 Map.merge(params, %{max_id: min})
188 Pleroma.Web.Endpoint,
191 Map.merge(params, %{since_id: max})
197 Pleroma.Web.Endpoint,
199 Map.merge(params, %{max_id: min})
202 Pleroma.Web.Endpoint,
204 Map.merge(params, %{since_id: max})
210 |> put_resp_header("link", "<#{next_url}>; rel=\"next\", <#{prev_url}>; rel=\"prev\"")
216 def home_timeline(%{assigns: %{user: user}} = conn, params) do
219 |> Map.put("type", ["Create", "Announce"])
220 |> Map.put("blocking_user", user)
221 |> Map.put("user", user)
224 ActivityPub.fetch_activities([user.ap_id | user.following], params)
228 |> add_link_headers(:home_timeline, activities)
229 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
232 def public_timeline(%{assigns: %{user: user}} = conn, params) do
233 local_only = params["local"] in [true, "True", "true", "1"]
237 |> Map.put("type", ["Create", "Announce"])
238 |> Map.put("local_only", local_only)
239 |> Map.put("blocking_user", user)
242 ActivityPub.fetch_public_activities(params)
246 |> add_link_headers(:public_timeline, activities, false, %{"local" => local_only})
247 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
250 def user_statuses(%{assigns: %{user: reading_user}} = conn, params) do
251 with %User{} = user <- Repo.get(User, params["id"]) do
252 # Since Pleroma has no "pinned" posts feature, we'll just set an empty list here
254 if params["pinned"] == "true" do
257 ActivityPub.fetch_user_activities(user, reading_user, params)
261 |> add_link_headers(:user_statuses, activities, params["id"])
262 |> render(StatusView, "index.json", %{
263 activities: activities,
270 def dm_timeline(%{assigns: %{user: user}} = conn, _params) do
272 ActivityPub.fetch_activities_query([user.ap_id], %{"type" => "Create", visibility: "direct"})
274 activities = Repo.all(query)
277 |> add_link_headers(:dm_timeline, activities)
278 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
281 def get_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
282 with %Activity{} = activity <- Repo.get(Activity, id),
283 true <- ActivityPub.visible_for_user?(activity, user) do
284 render(conn, StatusView, "status.json", %{activity: activity, for: user})
288 def get_context(%{assigns: %{user: user}} = conn, %{"id" => id}) do
289 with %Activity{} = activity <- Repo.get(Activity, id),
291 ActivityPub.fetch_activities_for_context(activity.data["context"], %{
292 "blocking_user" => user,
296 activities |> Enum.filter(fn %{id: aid} -> to_string(aid) != to_string(id) end),
298 activities |> Enum.filter(fn %{data: %{"type" => type}} -> type == "Create" end),
299 grouped_activities <- Enum.group_by(activities, fn %{id: id} -> id < activity.id end) do
305 activities: grouped_activities[true] || [],
313 activities: grouped_activities[false] || [],
323 def post_status(conn, %{"status" => "", "media_ids" => media_ids} = params)
324 when length(media_ids) > 0 do
327 |> Map.put("status", ".")
329 post_status(conn, params)
332 def post_status(%{assigns: %{user: user}} = conn, %{"status" => _} = params) do
335 |> Map.put("in_reply_to_status_id", params["in_reply_to_id"])
336 |> Map.put("no_attachment_links", true)
339 case get_req_header(conn, "idempotency-key") do
341 _ -> Ecto.UUID.generate()
345 Cachex.fetch!(:idempotency_cache, idempotency_key, fn _ -> CommonAPI.post(user, params) end)
347 render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
350 def delete_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
351 with {:ok, %Activity{}} <- CommonAPI.delete(id, user) do
357 |> json(%{error: "Can't delete this post"})
361 def reblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
362 with {:ok, announce, _activity} <- CommonAPI.repeat(ap_id_or_id, user) do
363 render(conn, StatusView, "status.json", %{activity: announce, for: user, as: :activity})
367 def unreblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
368 with {:ok, _unannounce, %{data: %{"id" => id}}} <- CommonAPI.unrepeat(ap_id_or_id, user),
369 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
370 render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
374 def fav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
375 with {:ok, _fav, %{data: %{"id" => id}}} <- CommonAPI.favorite(ap_id_or_id, user),
376 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
377 render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
381 def unfav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
382 with {:ok, _, _, %{data: %{"id" => id}}} <- CommonAPI.unfavorite(ap_id_or_id, user),
383 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
384 render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
388 def notifications(%{assigns: %{user: user}} = conn, params) do
389 notifications = Notification.for_user(user, params)
392 Enum.map(notifications, fn x ->
393 render_notification(user, x)
398 |> add_link_headers(:notifications, notifications)
402 def get_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
403 with {:ok, notification} <- Notification.get(user, id) do
404 json(conn, render_notification(user, notification))
408 |> put_resp_content_type("application/json")
409 |> send_resp(403, Jason.encode!(%{"error" => reason}))
413 def clear_notifications(%{assigns: %{user: user}} = conn, _params) do
414 Notification.clear(user)
418 def dismiss_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
419 with {:ok, _notif} <- Notification.dismiss(user, id) do
424 |> put_resp_content_type("application/json")
425 |> send_resp(403, Jason.encode!(%{"error" => reason}))
429 def relationships(%{assigns: %{user: user}} = conn, %{"id" => id}) do
431 q = from(u in User, where: u.id in ^id)
432 targets = Repo.all(q)
433 render(conn, AccountView, "relationships.json", %{user: user, targets: targets})
436 def update_media(%{assigns: %{user: _}} = conn, data) do
437 with %Object{} = object <- Repo.get(Object, data["id"]),
438 true <- is_binary(data["description"]),
439 description <- data["description"] do
440 new_data = %{object.data | "name" => description}
442 change = Object.change(object, %{data: new_data})
443 {:ok, media_obj} = Repo.update(change)
447 |> Map.put("id", object.id)
449 render(conn, StatusView, "attachment.json", %{attachment: data})
453 def upload(%{assigns: %{user: _}} = conn, %{"file" => file} = data) do
454 with {:ok, object} <- ActivityPub.upload(file) do
456 if Map.has_key?(data, "description") do
457 Map.put(object.data, "name", data["description"])
462 change = Object.change(object, %{data: objdata})
463 {:ok, object} = Repo.update(change)
467 |> Map.put("id", object.id)
469 render(conn, StatusView, "attachment.json", %{attachment: objdata})
473 def favourited_by(conn, %{"id" => id}) do
474 with %Activity{data: %{"object" => %{"likes" => likes}}} <- Repo.get(Activity, id) do
475 q = from(u in User, where: u.ap_id in ^likes)
477 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
483 def reblogged_by(conn, %{"id" => id}) do
484 with %Activity{data: %{"object" => %{"announcements" => announces}}} <- Repo.get(Activity, id) do
485 q = from(u in User, where: u.ap_id in ^announces)
487 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
493 def hashtag_timeline(%{assigns: %{user: user}} = conn, params) do
494 local_only = params["local"] in [true, "True", "true", "1"]
498 |> Map.put("type", "Create")
499 |> Map.put("local_only", local_only)
500 |> Map.put("blocking_user", user)
503 ActivityPub.fetch_public_activities(params)
507 |> add_link_headers(:hashtag_timeline, activities, params["tag"], %{"local" => local_only})
508 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
512 def followers(conn, %{"id" => id}) do
513 with %User{} = user <- Repo.get(User, id),
514 {:ok, followers} <- User.get_followers(user) do
515 render(conn, AccountView, "accounts.json", %{users: followers, as: :user})
519 def following(conn, %{"id" => id}) do
520 with %User{} = user <- Repo.get(User, id),
521 {:ok, followers} <- User.get_friends(user) do
522 render(conn, AccountView, "accounts.json", %{users: followers, as: :user})
526 def follow_requests(%{assigns: %{user: followed}} = conn, _params) do
527 with {:ok, follow_requests} <- User.get_follow_requests(followed) do
528 render(conn, AccountView, "accounts.json", %{users: follow_requests, as: :user})
532 def authorize_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
533 with %User{} = follower <- Repo.get(User, id),
534 {:ok, follower} <- User.maybe_follow(follower, followed),
535 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
536 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "accept"),
538 ActivityPub.accept(%{
539 to: [follower.ap_id],
540 actor: followed.ap_id,
541 object: follow_activity.data["id"],
544 render(conn, AccountView, "relationship.json", %{user: followed, target: follower})
548 |> put_resp_content_type("application/json")
549 |> send_resp(403, Jason.encode!(%{"error" => message}))
553 def reject_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
554 with %User{} = follower <- Repo.get(User, id),
555 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
556 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "reject"),
558 ActivityPub.reject(%{
559 to: [follower.ap_id],
560 actor: followed.ap_id,
561 object: follow_activity.data["id"],
564 render(conn, AccountView, "relationship.json", %{user: followed, target: follower})
568 |> put_resp_content_type("application/json")
569 |> send_resp(403, Jason.encode!(%{"error" => message}))
573 def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
574 with %User{} = followed <- Repo.get(User, id),
575 {:ok, follower} <- User.maybe_direct_follow(follower, followed),
576 {:ok, _activity} <- ActivityPub.follow(follower, followed) do
577 render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
581 |> put_resp_content_type("application/json")
582 |> send_resp(403, Jason.encode!(%{"error" => message}))
586 def follow(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do
587 with %User{} = followed <- Repo.get_by(User, nickname: uri),
588 {:ok, follower} <- User.maybe_direct_follow(follower, followed),
589 {:ok, _activity} <- ActivityPub.follow(follower, followed) do
590 render(conn, AccountView, "account.json", %{user: followed})
594 |> put_resp_content_type("application/json")
595 |> send_resp(403, Jason.encode!(%{"error" => message}))
599 def unfollow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
600 with %User{} = followed <- Repo.get(User, id),
601 {:ok, _activity} <- ActivityPub.unfollow(follower, followed),
602 {:ok, follower, _} <- User.unfollow(follower, followed) do
603 render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
607 def block(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
608 with %User{} = blocked <- Repo.get(User, id),
609 {:ok, blocker} <- User.block(blocker, blocked),
610 {:ok, _activity} <- ActivityPub.block(blocker, blocked) do
611 render(conn, AccountView, "relationship.json", %{user: blocker, target: blocked})
615 |> put_resp_content_type("application/json")
616 |> send_resp(403, Jason.encode!(%{"error" => message}))
620 def unblock(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
621 with %User{} = blocked <- Repo.get(User, id),
622 {:ok, blocker} <- User.unblock(blocker, blocked),
623 {:ok, _activity} <- ActivityPub.unblock(blocker, blocked) do
624 render(conn, AccountView, "relationship.json", %{user: blocker, target: blocked})
628 |> put_resp_content_type("application/json")
629 |> send_resp(403, Jason.encode!(%{"error" => message}))
633 # TODO: Use proper query
634 def blocks(%{assigns: %{user: user}} = conn, _) do
635 with blocked_users <- user.info["blocks"] || [],
636 accounts <- Enum.map(blocked_users, fn ap_id -> User.get_cached_by_ap_id(ap_id) end) do
637 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
642 def domain_blocks(%{assigns: %{user: %{info: info}}} = conn, _) do
643 json(conn, info["domain_blocks"] || [])
646 def block_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
647 User.block_domain(blocker, domain)
651 def unblock_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
652 User.unblock_domain(blocker, domain)
656 def search2(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
657 accounts = User.search(query, params["resolve"] == "true")
660 if Regex.match?(~r/https?:/, query) do
661 with {:ok, object} <- ActivityPub.fetch_object_from_id(query) do
662 [Activity.get_create_activity_by_object_ap_id(object.data["id"])]
671 where: fragment("?->>'type' = 'Create'", a.data),
672 where: "https://www.w3.org/ns/activitystreams#Public" in a.recipients,
675 "to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)",
680 order_by: [desc: :id]
683 statuses = Repo.all(q) ++ fetched
685 tags_path = Web.base_url() <> "/tag/"
690 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
691 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
692 |> Enum.map(fn tag -> %{name: tag, url: tags_path <> tag} end)
695 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
697 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
704 def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
705 accounts = User.search(query, params["resolve"] == "true")
708 if Regex.match?(~r/https?:/, query) do
709 with {:ok, object} <- ActivityPub.fetch_object_from_id(query) do
710 [Activity.get_create_activity_by_object_ap_id(object.data["id"])]
719 where: fragment("?->>'type' = 'Create'", a.data),
720 where: "https://www.w3.org/ns/activitystreams#Public" in a.recipients,
723 "to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)",
728 order_by: [desc: :id]
731 statuses = Repo.all(q) ++ fetched
736 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
737 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
740 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
742 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
749 def account_search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
750 accounts = User.search(query, params["resolve"] == "true")
752 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
757 def favourites(%{assigns: %{user: user}} = conn, _) do
760 |> Map.put("type", "Create")
761 |> Map.put("favorited_by", user.ap_id)
762 |> Map.put("blocking_user", user)
765 ActivityPub.fetch_public_activities(params)
769 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
772 def get_lists(%{assigns: %{user: user}} = conn, opts) do
773 lists = Pleroma.List.for_user(user, opts)
774 res = ListView.render("lists.json", lists: lists)
778 def get_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
779 with %Pleroma.List{} = list <- Pleroma.List.get(id, user) do
780 res = ListView.render("list.json", list: list)
783 _e -> json(conn, "error")
787 def delete_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
788 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
789 {:ok, _list} <- Pleroma.List.delete(list) do
797 def create_list(%{assigns: %{user: user}} = conn, %{"title" => title}) do
798 with {:ok, %Pleroma.List{} = list} <- Pleroma.List.create(title, user) do
799 res = ListView.render("list.json", list: list)
804 def add_to_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
806 |> Enum.each(fn account_id ->
807 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
808 %User{} = followed <- Repo.get(User, account_id) do
809 Pleroma.List.follow(list, followed)
816 def remove_from_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
818 |> Enum.each(fn account_id ->
819 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
820 %User{} = followed <- Repo.get(Pleroma.User, account_id) do
821 Pleroma.List.unfollow(list, followed)
828 def list_accounts(%{assigns: %{user: user}} = conn, %{"id" => id}) do
829 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
830 {:ok, users} = Pleroma.List.get_following(list) do
831 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
835 def rename_list(%{assigns: %{user: user}} = conn, %{"id" => id, "title" => title}) do
836 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
837 {:ok, list} <- Pleroma.List.rename(list, title) do
838 res = ListView.render("list.json", list: list)
846 def list_timeline(%{assigns: %{user: user}} = conn, %{"list_id" => id} = params) do
847 with %Pleroma.List{title: title, following: following} <- Pleroma.List.get(id, user) do
850 |> Map.put("type", "Create")
851 |> Map.put("blocking_user", user)
853 # adding title is a hack to not make empty lists function like a public timeline
855 ActivityPub.fetch_activities([title | following], params)
859 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
864 |> json(%{error: "Error."})
868 def index(%{assigns: %{user: user}} = conn, _params) do
871 |> get_session(:oauth_token)
874 mastodon_emoji = mastodonized_emoji()
875 accounts = Map.put(%{}, user.id, AccountView.render("account.json", %{user: user}))
880 streaming_api_base_url:
881 String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws"),
884 domain: Pleroma.Web.Endpoint.host(),
887 unfollow_modal: false,
890 auto_play_gif: false,
891 display_sensitive_media: false,
892 reduce_motion: false,
893 max_toot_chars: Keyword.get(@instance, :limit)
896 delete_others_notice: !!user.info["is_moderator"]
900 default_privacy: user.info["default_scope"] || "public",
901 default_sensitive: false
903 media_attachments: %{
904 accept_content_types: [
920 Map.get(user.info, "settings") ||
950 push_subscription: nil,
952 custom_emojis: mastodon_emoji,
953 char_limit: Keyword.get(@instance, :limit)
959 |> render(MastodonView, "index.html", %{initial_state: initial_state})
962 |> redirect(to: "/web/login")
966 def put_settings(%{assigns: %{user: user}} = conn, %{"data" => settings} = _params) do
967 with new_info <- Map.put(user.info, "settings", settings),
968 change <- User.info_changeset(user, %{info: new_info}),
969 {:ok, _user} <- User.update_and_set_cache(change) do
975 |> json(%{error: inspect(e)})
979 def login(conn, _) do
981 |> render(MastodonView, "login.html", %{error: false})
984 defp get_or_make_app() do
985 with %App{} = app <- Repo.get_by(App, client_name: "Mastodon-Local") do
990 App.register_changeset(%App{}, %{
991 client_name: "Mastodon-Local",
993 scopes: "read,write,follow"
1000 def login_post(conn, %{"authorization" => %{"name" => name, "password" => password}}) do
1001 with %User{} = user <- User.get_by_nickname_or_email(name),
1002 true <- Pbkdf2.checkpw(password, user.password_hash),
1003 {:ok, app} <- get_or_make_app(),
1004 {:ok, auth} <- Authorization.create_authorization(app, user),
1005 {:ok, token} <- Token.exchange_token(app, auth) do
1007 |> put_session(:oauth_token, token.token)
1008 |> redirect(to: "/web/getting-started")
1012 |> render(MastodonView, "login.html", %{error: "Wrong username or password"})
1016 def logout(conn, _) do
1019 |> redirect(to: "/")
1022 def relationship_noop(%{assigns: %{user: user}} = conn, %{"id" => id}) do
1023 Logger.debug("Unimplemented, returning unmodified relationship")
1025 with %User{} = target <- Repo.get(User, id) do
1026 render(conn, AccountView, "relationship.json", %{user: user, target: target})
1030 def empty_array(conn, _) do
1031 Logger.debug("Unimplemented, returning an empty array")
1035 def empty_object(conn, _) do
1036 Logger.debug("Unimplemented, returning an empty object")
1040 def render_notification(user, %{id: id, activity: activity, inserted_at: created_at} = _params) do
1041 actor = User.get_cached_by_ap_id(activity.data["actor"])
1044 NaiveDateTime.to_iso8601(created_at)
1045 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
1047 case activity.data["type"] do
1052 created_at: created_at,
1053 account: AccountView.render("account.json", %{user: actor}),
1054 status: StatusView.render("status.json", %{activity: activity, for: user})
1058 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
1063 created_at: created_at,
1064 account: AccountView.render("account.json", %{user: actor}),
1065 status: StatusView.render("status.json", %{activity: liked_activity, for: user})
1069 announced_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}),
1076 status: StatusView.render("status.json", %{activity: announced_activity, for: user})
1083 created_at: created_at,
1084 account: AccountView.render("account.json", %{user: actor})
1092 def errors(conn, _) do
1095 |> json("Something went wrong")
1098 @suggestions Application.get_env(:pleroma, :suggestions)
1100 def suggestions(%{assigns: %{user: user}} = conn, _) do
1101 if Keyword.get(@suggestions, :enabled, false) do
1102 api = Keyword.get(@suggestions, :third_party_engine, "")
1103 timeout = Keyword.get(@suggestions, :timeout, 5000)
1106 Application.get_env(:pleroma, Pleroma.Web.Endpoint)
1107 |> Keyword.get(:url)
1108 |> Keyword.get(:host)
1110 user = user.nickname
1111 url = String.replace(api, "{{host}}", host) |> String.replace("{{user}}", user)
1113 with {:ok, %{status_code: 200, body: body}} <-
1114 @httpoison.get(url, [], timeout: timeout, recv_timeout: timeout),
1115 {:ok, data} <- Jason.decode(body) do
1117 Enum.slice(data, 0, 40)
1119 Map.put(x, "id", User.get_or_fetch(x["acct"]).id)
1125 e -> Logger.error("Could not retrieve suggestions at fetch #{url}, #{inspect(e)}")