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, OStatus}
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, activities} <- OStatus.fetch_activity_from_url(query) do
664 %{data: %{"type" => "Create"}} -> true
675 where: fragment("?->>'type' = 'Create'", a.data),
676 where: "https://www.w3.org/ns/activitystreams#Public" in a.recipients,
679 "to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)",
684 order_by: [desc: :id]
687 statuses = Repo.all(q) ++ fetched
689 tags_path = Web.base_url() <> "/tag/"
694 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
695 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
696 |> Enum.map(fn tag -> %{name: tag, url: tags_path <> tag} end)
699 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
701 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
708 def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
709 accounts = User.search(query, params["resolve"] == "true")
712 if Regex.match?(~r/https?:/, query) do
713 with {:ok, activities} <- OStatus.fetch_activity_from_url(query) do
716 %{data: %{"type" => "Create"}} -> true
727 where: fragment("?->>'type' = 'Create'", a.data),
728 where: "https://www.w3.org/ns/activitystreams#Public" in a.recipients,
731 "to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)",
736 order_by: [desc: :id]
739 statuses = Repo.all(q) ++ fetched
744 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
745 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
748 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
750 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
757 def account_search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
758 accounts = User.search(query, params["resolve"] == "true")
760 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
765 def favourites(%{assigns: %{user: user}} = conn, _) do
768 |> Map.put("type", "Create")
769 |> Map.put("favorited_by", user.ap_id)
770 |> Map.put("blocking_user", user)
773 ActivityPub.fetch_public_activities(params)
777 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
780 def get_lists(%{assigns: %{user: user}} = conn, opts) do
781 lists = Pleroma.List.for_user(user, opts)
782 res = ListView.render("lists.json", lists: lists)
786 def get_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
787 with %Pleroma.List{} = list <- Pleroma.List.get(id, user) do
788 res = ListView.render("list.json", list: list)
791 _e -> json(conn, "error")
795 def delete_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
796 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
797 {:ok, _list} <- Pleroma.List.delete(list) do
805 def create_list(%{assigns: %{user: user}} = conn, %{"title" => title}) do
806 with {:ok, %Pleroma.List{} = list} <- Pleroma.List.create(title, user) do
807 res = ListView.render("list.json", list: list)
812 def add_to_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
814 |> Enum.each(fn account_id ->
815 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
816 %User{} = followed <- Repo.get(User, account_id) do
817 Pleroma.List.follow(list, followed)
824 def remove_from_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
826 |> Enum.each(fn account_id ->
827 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
828 %User{} = followed <- Repo.get(Pleroma.User, account_id) do
829 Pleroma.List.unfollow(list, followed)
836 def list_accounts(%{assigns: %{user: user}} = conn, %{"id" => id}) do
837 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
838 {:ok, users} = Pleroma.List.get_following(list) do
839 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
843 def rename_list(%{assigns: %{user: user}} = conn, %{"id" => id, "title" => title}) do
844 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
845 {:ok, list} <- Pleroma.List.rename(list, title) do
846 res = ListView.render("list.json", list: list)
854 def list_timeline(%{assigns: %{user: user}} = conn, %{"list_id" => id} = params) do
855 with %Pleroma.List{title: title, following: following} <- Pleroma.List.get(id, user) do
858 |> Map.put("type", "Create")
859 |> Map.put("blocking_user", user)
861 # adding title is a hack to not make empty lists function like a public timeline
863 ActivityPub.fetch_activities([title | following], params)
867 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
872 |> json(%{error: "Error."})
876 def index(%{assigns: %{user: user}} = conn, _params) do
879 |> get_session(:oauth_token)
882 mastodon_emoji = mastodonized_emoji()
883 accounts = Map.put(%{}, user.id, AccountView.render("account.json", %{user: user}))
888 streaming_api_base_url:
889 String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws"),
892 domain: Pleroma.Web.Endpoint.host(),
895 unfollow_modal: false,
898 auto_play_gif: false,
899 display_sensitive_media: false,
900 reduce_motion: false,
901 max_toot_chars: Keyword.get(@instance, :limit)
904 delete_others_notice: !!user.info["is_moderator"]
908 default_privacy: user.info["default_scope"] || "public",
909 default_sensitive: false
911 media_attachments: %{
912 accept_content_types: [
928 Map.get(user.info, "settings") ||
958 push_subscription: nil,
960 custom_emojis: mastodon_emoji,
961 char_limit: Keyword.get(@instance, :limit)
967 |> render(MastodonView, "index.html", %{initial_state: initial_state})
970 |> redirect(to: "/web/login")
974 def put_settings(%{assigns: %{user: user}} = conn, %{"data" => settings} = _params) do
975 with new_info <- Map.put(user.info, "settings", settings),
976 change <- User.info_changeset(user, %{info: new_info}),
977 {:ok, _user} <- User.update_and_set_cache(change) do
983 |> json(%{error: inspect(e)})
987 def login(conn, _) do
989 |> render(MastodonView, "login.html", %{error: false})
992 defp get_or_make_app() do
993 with %App{} = app <- Repo.get_by(App, client_name: "Mastodon-Local") do
998 App.register_changeset(%App{}, %{
999 client_name: "Mastodon-Local",
1001 scopes: "read,write,follow"
1008 def login_post(conn, %{"authorization" => %{"name" => name, "password" => password}}) do
1009 with %User{} = user <- User.get_by_nickname_or_email(name),
1010 true <- Pbkdf2.checkpw(password, user.password_hash),
1011 {:ok, app} <- get_or_make_app(),
1012 {:ok, auth} <- Authorization.create_authorization(app, user),
1013 {:ok, token} <- Token.exchange_token(app, auth) do
1015 |> put_session(:oauth_token, token.token)
1016 |> redirect(to: "/web/getting-started")
1020 |> render(MastodonView, "login.html", %{error: "Wrong username or password"})
1024 def logout(conn, _) do
1027 |> redirect(to: "/")
1030 def relationship_noop(%{assigns: %{user: user}} = conn, %{"id" => id}) do
1031 Logger.debug("Unimplemented, returning unmodified relationship")
1033 with %User{} = target <- Repo.get(User, id) do
1034 render(conn, AccountView, "relationship.json", %{user: user, target: target})
1038 def empty_array(conn, _) do
1039 Logger.debug("Unimplemented, returning an empty array")
1043 def empty_object(conn, _) do
1044 Logger.debug("Unimplemented, returning an empty object")
1048 def render_notification(user, %{id: id, activity: activity, inserted_at: created_at} = _params) do
1049 actor = User.get_cached_by_ap_id(activity.data["actor"])
1052 NaiveDateTime.to_iso8601(created_at)
1053 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
1055 case activity.data["type"] do
1060 created_at: created_at,
1061 account: AccountView.render("account.json", %{user: actor}),
1062 status: StatusView.render("status.json", %{activity: activity, for: user})
1066 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
1071 created_at: created_at,
1072 account: AccountView.render("account.json", %{user: actor}),
1073 status: StatusView.render("status.json", %{activity: liked_activity, for: user})
1077 announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
1082 created_at: created_at,
1083 account: AccountView.render("account.json", %{user: actor}),
1084 status: StatusView.render("status.json", %{activity: announced_activity, for: user})
1091 created_at: created_at,
1092 account: AccountView.render("account.json", %{user: actor})
1100 def errors(conn, _) do
1103 |> json("Something went wrong")
1106 @suggestions Application.get_env(:pleroma, :suggestions)
1108 def suggestions(%{assigns: %{user: user}} = conn, _) do
1109 if Keyword.get(@suggestions, :enabled, false) do
1110 api = Keyword.get(@suggestions, :third_party_engine, "")
1111 timeout = Keyword.get(@suggestions, :timeout, 5000)
1114 Application.get_env(:pleroma, Pleroma.Web.Endpoint)
1115 |> Keyword.get(:url)
1116 |> Keyword.get(:host)
1118 user = user.nickname
1119 url = String.replace(api, "{{host}}", host) |> String.replace("{{user}}", user)
1121 with {:ok, %{status_code: 200, body: body}} <-
1122 @httpoison.get(url, [], timeout: timeout, recv_timeout: timeout),
1123 {:ok, data} <- Jason.decode(body) do
1125 Enum.slice(data, 0, 40)
1127 Map.put(x, "id", User.get_or_fetch(x["acct"]).id)
1133 e -> Logger.error("Could not retrieve suggestions at fetch #{url}, #{inspect(e)}")