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 action_fallback(:errors)
16 def create_app(conn, params) do
17 with cs <- App.register_changeset(%App{}, params) |> IO.inspect(),
18 {:ok, app} <- Repo.insert(cs) |> IO.inspect() do
21 client_id: app.client_id,
22 client_secret: app.client_secret
29 def update_credentials(%{assigns: %{user: user}} = conn, params) do
33 if bio = params["note"] do
34 Map.put(params, "bio", bio)
40 if name = params["display_name"] do
41 Map.put(params, "name", name)
47 if avatar = params["avatar"] do
48 with %Plug.Upload{} <- avatar,
49 {:ok, object} <- ActivityPub.upload(avatar),
50 change = Ecto.Changeset.change(user, %{avatar: object.data}),
51 {:ok, user} = User.update_and_set_cache(change) do
61 if banner = params["header"] do
62 with %Plug.Upload{} <- banner,
63 {:ok, object} <- ActivityPub.upload(banner),
64 new_info <- Map.put(user.info, "banner", object.data),
65 change <- User.info_changeset(user, %{info: new_info}),
66 {:ok, user} <- User.update_and_set_cache(change) do
76 if locked = params["locked"] do
77 with locked <- locked == "true",
78 new_info <- Map.put(user.info, "locked", locked),
79 change <- User.info_changeset(user, %{info: new_info}),
80 {:ok, user} <- User.update_and_set_cache(change) do
89 with changeset <- User.update_changeset(user, params),
90 {:ok, user} <- User.update_and_set_cache(changeset) do
91 if original_user != user do
92 CommonAPI.update(user)
95 json(conn, AccountView.render("account.json", %{user: user}))
100 |> json(%{error: "Invalid request"})
104 def verify_credentials(%{assigns: %{user: user}} = conn, _) do
105 account = AccountView.render("account.json", %{user: user})
109 def user(conn, %{"id" => id}) do
110 with %User{} = user <- Repo.get(User, id) do
111 account = AccountView.render("account.json", %{user: user})
117 |> json(%{error: "Can't find user"})
121 @instance Application.get_env(:pleroma, :instance)
122 @mastodon_api_level "2.3.3"
124 def masto_instance(conn, _params) do
127 title: Keyword.get(@instance, :name),
128 description: "A Pleroma instance, an alternative fediverse server",
129 version: "#{@mastodon_api_level} (compatible; #{Keyword.get(@instance, :version)})",
130 email: Keyword.get(@instance, :email),
132 streaming_api: String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws")
134 stats: Stats.get_stats(),
135 thumbnail: Web.base_url() <> "/instance/thumbnail.jpeg",
136 max_toot_chars: Keyword.get(@instance, :limit)
142 def peers(conn, _params) do
143 json(conn, Stats.get_peers())
146 defp mastodonized_emoji do
147 Pleroma.Formatter.get_custom_emoji()
148 |> Enum.map(fn {shortcode, relative_url} ->
149 url = to_string(URI.merge(Web.base_url(), relative_url))
152 "shortcode" => shortcode,
154 "visible_in_picker" => true,
160 def custom_emojis(conn, _params) do
161 mastodon_emoji = mastodonized_emoji()
162 json(conn, mastodon_emoji)
165 defp add_link_headers(conn, method, activities, param \\ nil, params \\ %{}) do
166 last = List.last(activities)
167 first = List.first(activities)
173 {next_url, prev_url} =
177 Pleroma.Web.Endpoint,
180 Map.merge(params, %{max_id: min})
183 Pleroma.Web.Endpoint,
186 Map.merge(params, %{since_id: max})
192 Pleroma.Web.Endpoint,
194 Map.merge(params, %{max_id: min})
197 Pleroma.Web.Endpoint,
199 Map.merge(params, %{since_id: max})
205 |> put_resp_header("link", "<#{next_url}>; rel=\"next\", <#{prev_url}>; rel=\"prev\"")
211 def home_timeline(%{assigns: %{user: user}} = conn, params) do
214 |> Map.put("type", ["Create", "Announce"])
215 |> Map.put("blocking_user", user)
216 |> Map.put("user", user)
219 ActivityPub.fetch_activities([user.ap_id | user.following], params)
223 |> add_link_headers(:home_timeline, activities)
224 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
227 def public_timeline(%{assigns: %{user: user}} = conn, params) do
228 local_only = params["local"] in [true, "True", "true", "1"]
232 |> Map.put("type", ["Create", "Announce"])
233 |> Map.put("local_only", local_only)
234 |> Map.put("blocking_user", user)
237 ActivityPub.fetch_public_activities(params)
241 |> add_link_headers(:public_timeline, activities, false, %{"local" => local_only})
242 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
245 def user_statuses(%{assigns: %{user: reading_user}} = conn, params) do
246 with %User{} = user <- Repo.get(User, params["id"]) do
247 # Since Pleroma has no "pinned" posts feature, we'll just set an empty list here
249 if params["pinned"] == "true" do
252 ActivityPub.fetch_user_activities(user, reading_user, params)
256 |> add_link_headers(:user_statuses, activities, params["id"])
257 |> render(StatusView, "index.json", %{
258 activities: activities,
265 def dm_timeline(%{assigns: %{user: user}} = conn, _params) do
267 ActivityPub.fetch_activities_query([user.ap_id], %{"type" => "Create", visibility: "direct"})
269 activities = Repo.all(query)
272 |> add_link_headers(:dm_timeline, activities)
273 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
276 def get_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
277 with %Activity{} = activity <- Repo.get(Activity, id),
278 true <- ActivityPub.visible_for_user?(activity, user) do
279 render(conn, StatusView, "status.json", %{activity: activity, for: user})
283 def get_context(%{assigns: %{user: user}} = conn, %{"id" => id}) do
284 with %Activity{} = activity <- Repo.get(Activity, id),
286 ActivityPub.fetch_activities_for_context(activity.data["context"], %{
287 "blocking_user" => user,
291 activities |> Enum.filter(fn %{id: aid} -> to_string(aid) != to_string(id) end),
293 activities |> Enum.filter(fn %{data: %{"type" => type}} -> type == "Create" end),
294 grouped_activities <- Enum.group_by(activities, fn %{id: id} -> id < activity.id end) do
300 activities: grouped_activities[true] || [],
308 activities: grouped_activities[false] || [],
318 def post_status(conn, %{"status" => "", "media_ids" => media_ids} = params)
319 when length(media_ids) > 0 do
322 |> Map.put("status", ".")
324 post_status(conn, params)
327 def post_status(%{assigns: %{user: user}} = conn, %{"status" => _} = params) do
330 |> Map.put("in_reply_to_status_id", params["in_reply_to_id"])
331 |> Map.put("no_attachment_links", true)
334 case get_req_header(conn, "idempotency-key") do
336 _ -> Ecto.UUID.generate()
340 Cachex.fetch!(:idempotency_cache, idempotency_key, fn _ -> CommonAPI.post(user, params) end)
342 render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
345 def delete_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
346 with {:ok, %Activity{}} <- CommonAPI.delete(id, user) do
352 |> json(%{error: "Can't delete this post"})
356 def reblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
357 with {:ok, announce, _activity} <- CommonAPI.repeat(ap_id_or_id, user) do
358 render(conn, StatusView, "status.json", %{activity: announce, for: user, as: :activity})
362 def unreblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
363 with {:ok, _unannounce, %{data: %{"id" => id}}} <- CommonAPI.unrepeat(ap_id_or_id, user),
364 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
365 render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
369 def fav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
370 with {:ok, _fav, %{data: %{"id" => id}}} <- CommonAPI.favorite(ap_id_or_id, user),
371 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
372 render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
376 def unfav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
377 with {:ok, _, _, %{data: %{"id" => id}}} <- CommonAPI.unfavorite(ap_id_or_id, user),
378 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
379 render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
383 def notifications(%{assigns: %{user: user}} = conn, params) do
384 notifications = Notification.for_user(user, params)
387 Enum.map(notifications, fn x ->
388 render_notification(user, x)
393 |> add_link_headers(:notifications, notifications)
397 def get_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
398 with {:ok, notification} <- Notification.get(user, id) do
399 json(conn, render_notification(user, notification))
403 |> put_resp_content_type("application/json")
404 |> send_resp(403, Jason.encode!(%{"error" => reason}))
408 def clear_notifications(%{assigns: %{user: user}} = conn, _params) do
409 Notification.clear(user)
413 def dismiss_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
414 with {:ok, _notif} <- Notification.dismiss(user, id) do
419 |> put_resp_content_type("application/json")
420 |> send_resp(403, Jason.encode!(%{"error" => reason}))
424 def relationships(%{assigns: %{user: user}} = conn, %{"id" => id}) do
426 q = from(u in User, where: u.id in ^id)
427 targets = Repo.all(q)
428 render(conn, AccountView, "relationships.json", %{user: user, targets: targets})
431 def update_media(%{assigns: %{user: _}} = conn, data) do
432 with %Object{} = object <- Repo.get(Object, data["id"]),
433 true <- is_binary(data["description"]),
434 description <- data["description"] do
435 new_data = %{object.data | "name" => description}
437 change = Object.change(object, %{data: new_data})
438 {:ok, media_obj} = Repo.update(change)
442 |> Map.put("id", object.id)
444 render(conn, StatusView, "attachment.json", %{attachment: data})
448 def upload(%{assigns: %{user: _}} = conn, %{"file" => file} = data) do
449 with {:ok, object} <- ActivityPub.upload(file) do
451 if Map.has_key?(data, "description") do
452 Map.put(object.data, "name", data["description"])
457 change = Object.change(object, %{data: objdata})
458 {:ok, object} = Repo.update(change)
462 |> Map.put("id", object.id)
464 render(conn, StatusView, "attachment.json", %{attachment: objdata})
468 def favourited_by(conn, %{"id" => id}) do
469 with %Activity{data: %{"object" => %{"likes" => likes}}} <- Repo.get(Activity, id) do
470 q = from(u in User, where: u.ap_id in ^likes)
472 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
478 def reblogged_by(conn, %{"id" => id}) do
479 with %Activity{data: %{"object" => %{"announcements" => announces}}} <- Repo.get(Activity, id) do
480 q = from(u in User, where: u.ap_id in ^announces)
482 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
488 def hashtag_timeline(%{assigns: %{user: user}} = conn, params) do
489 local_only = params["local"] in [true, "True", "true", "1"]
493 |> Map.put("type", "Create")
494 |> Map.put("local_only", local_only)
495 |> Map.put("blocking_user", user)
498 ActivityPub.fetch_public_activities(params)
502 |> add_link_headers(:hashtag_timeline, activities, params["tag"], %{"local" => local_only})
503 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
507 def followers(conn, %{"id" => id}) do
508 with %User{} = user <- Repo.get(User, id),
509 {:ok, followers} <- User.get_followers(user) do
510 render(conn, AccountView, "accounts.json", %{users: followers, as: :user})
514 def following(conn, %{"id" => id}) do
515 with %User{} = user <- Repo.get(User, id),
516 {:ok, followers} <- User.get_friends(user) do
517 render(conn, AccountView, "accounts.json", %{users: followers, as: :user})
521 def follow_requests(%{assigns: %{user: followed}} = conn, _params) do
522 with {:ok, follow_requests} <- User.get_follow_requests(followed) do
523 render(conn, AccountView, "accounts.json", %{users: follow_requests, as: :user})
527 def authorize_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
528 with %User{} = follower <- Repo.get(User, id),
529 {:ok, follower} <- User.maybe_follow(follower, followed),
530 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
531 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "accept"),
533 ActivityPub.accept(%{
534 to: [follower.ap_id],
535 actor: followed.ap_id,
536 object: follow_activity.data["id"],
539 render(conn, AccountView, "relationship.json", %{user: followed, target: follower})
543 |> put_resp_content_type("application/json")
544 |> send_resp(403, Jason.encode!(%{"error" => message}))
548 def reject_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
549 with %User{} = follower <- Repo.get(User, id),
550 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
551 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "reject"),
553 ActivityPub.reject(%{
554 to: [follower.ap_id],
555 actor: followed.ap_id,
556 object: follow_activity.data["id"],
559 render(conn, AccountView, "relationship.json", %{user: followed, target: follower})
563 |> put_resp_content_type("application/json")
564 |> send_resp(403, Jason.encode!(%{"error" => message}))
568 def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
569 with %User{} = followed <- Repo.get(User, id),
570 {:ok, follower} <- User.maybe_direct_follow(follower, followed),
571 {:ok, _activity} <- ActivityPub.follow(follower, followed) do
572 render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
576 |> put_resp_content_type("application/json")
577 |> send_resp(403, Jason.encode!(%{"error" => message}))
581 def follow(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do
582 with %User{} = followed <- Repo.get_by(User, nickname: uri),
583 {:ok, follower} <- User.maybe_direct_follow(follower, followed),
584 {:ok, _activity} <- ActivityPub.follow(follower, followed) do
585 render(conn, AccountView, "account.json", %{user: followed})
589 |> put_resp_content_type("application/json")
590 |> send_resp(403, Jason.encode!(%{"error" => message}))
594 def unfollow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
595 with %User{} = followed <- Repo.get(User, id),
596 {:ok, _activity} <- ActivityPub.unfollow(follower, followed),
597 {:ok, follower, _} <- User.unfollow(follower, followed) do
598 render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
602 def block(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
603 with %User{} = blocked <- Repo.get(User, id),
604 {:ok, blocker} <- User.block(blocker, blocked),
605 {:ok, _activity} <- ActivityPub.block(blocker, blocked) do
606 render(conn, AccountView, "relationship.json", %{user: blocker, target: blocked})
610 |> put_resp_content_type("application/json")
611 |> send_resp(403, Jason.encode!(%{"error" => message}))
615 def unblock(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
616 with %User{} = blocked <- Repo.get(User, id),
617 {:ok, blocker} <- User.unblock(blocker, blocked),
618 {:ok, _activity} <- ActivityPub.unblock(blocker, blocked) do
619 render(conn, AccountView, "relationship.json", %{user: blocker, target: blocked})
623 |> put_resp_content_type("application/json")
624 |> send_resp(403, Jason.encode!(%{"error" => message}))
628 # TODO: Use proper query
629 def blocks(%{assigns: %{user: user}} = conn, _) do
630 with blocked_users <- user.info["blocks"] || [],
631 accounts <- Enum.map(blocked_users, fn ap_id -> User.get_cached_by_ap_id(ap_id) end) do
632 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
637 def domain_blocks(%{assigns: %{user: %{info: info}}} = conn, _) do
638 json(conn, info["domain_blocks"] || [])
641 def block_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
642 User.block_domain(blocker, domain)
646 def unblock_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
647 User.unblock_domain(blocker, domain)
651 def search2(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
652 accounts = User.search(query, params["resolve"] == "true")
655 if Regex.match?(~r/https?:/, query) do
656 with {:ok, activities} <- OStatus.fetch_activity_from_url(query) do
659 %{data: %{"type" => "Create"}} -> true
670 where: fragment("?->>'type' = 'Create'", a.data),
671 where: "https://www.w3.org/ns/activitystreams#Public" in a.recipients,
674 "to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)",
679 order_by: [desc: :id]
682 statuses = Repo.all(q) ++ fetched
684 tags_path = Web.base_url() <> "/tag/"
689 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
690 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
691 |> Enum.map(fn tag -> %{name: tag, url: tags_path <> tag} end)
694 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
696 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
703 def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
704 accounts = User.search(query, params["resolve"] == "true")
707 if Regex.match?(~r/https?:/, query) do
708 with {:ok, activities} <- OStatus.fetch_activity_from_url(query) do
711 %{data: %{"type" => "Create"}} -> true
722 where: fragment("?->>'type' = 'Create'", a.data),
723 where: "https://www.w3.org/ns/activitystreams#Public" in a.recipients,
726 "to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)",
731 order_by: [desc: :id]
734 statuses = Repo.all(q) ++ fetched
739 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
740 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
743 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
745 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
752 def account_search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
753 accounts = User.search(query, params["resolve"] == "true")
755 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
760 def favourites(%{assigns: %{user: user}} = conn, _) do
763 |> Map.put("type", "Create")
764 |> Map.put("favorited_by", user.ap_id)
765 |> Map.put("blocking_user", user)
768 ActivityPub.fetch_public_activities(params)
772 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
775 def get_lists(%{assigns: %{user: user}} = conn, opts) do
776 lists = Pleroma.List.for_user(user, opts)
777 res = ListView.render("lists.json", lists: lists)
781 def get_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
782 with %Pleroma.List{} = list <- Pleroma.List.get(id, user) do
783 res = ListView.render("list.json", list: list)
786 _e -> json(conn, "error")
790 def delete_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
791 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
792 {:ok, _list} <- Pleroma.List.delete(list) do
800 def create_list(%{assigns: %{user: user}} = conn, %{"title" => title}) do
801 with {:ok, %Pleroma.List{} = list} <- Pleroma.List.create(title, user) do
802 res = ListView.render("list.json", list: list)
807 def add_to_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
809 |> Enum.each(fn account_id ->
810 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
811 %User{} = followed <- Repo.get(User, account_id) do
812 Pleroma.List.follow(list, followed)
819 def remove_from_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
821 |> Enum.each(fn account_id ->
822 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
823 %User{} = followed <- Repo.get(Pleroma.User, account_id) do
824 Pleroma.List.unfollow(list, followed)
831 def list_accounts(%{assigns: %{user: user}} = conn, %{"id" => id}) do
832 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
833 {:ok, users} = Pleroma.List.get_following(list) do
834 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
838 def rename_list(%{assigns: %{user: user}} = conn, %{"id" => id, "title" => title}) do
839 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
840 {:ok, list} <- Pleroma.List.rename(list, title) do
841 res = ListView.render("list.json", list: list)
849 def list_timeline(%{assigns: %{user: user}} = conn, %{"list_id" => id} = params) do
850 with %Pleroma.List{title: title, following: following} <- Pleroma.List.get(id, user) do
853 |> Map.put("type", "Create")
854 |> Map.put("blocking_user", user)
856 # adding title is a hack to not make empty lists function like a public timeline
858 ActivityPub.fetch_activities([title | 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()
878 accounts = Map.put(%{}, user.id, AccountView.render("account.json", %{user: user}))
883 streaming_api_base_url:
884 String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws"),
887 domain: Pleroma.Web.Endpoint.host(),
890 unfollow_modal: false,
893 auto_play_gif: false,
894 display_sensitive_media: false,
895 reduce_motion: false,
896 max_toot_chars: Keyword.get(@instance, :limit)
899 delete_others_notice: !!user.info["is_moderator"]
903 default_privacy: user.info["default_scope"] || "public",
904 default_sensitive: false
906 media_attachments: %{
907 accept_content_types: [
923 Map.get(user.info, "settings") ||
953 push_subscription: nil,
955 custom_emojis: mastodon_emoji,
956 char_limit: Keyword.get(@instance, :limit)
962 |> render(MastodonView, "index.html", %{initial_state: initial_state})
965 |> redirect(to: "/web/login")
969 def put_settings(%{assigns: %{user: user}} = conn, %{"data" => settings} = _params) do
970 with new_info <- Map.put(user.info, "settings", settings),
971 change <- User.info_changeset(user, %{info: new_info}),
972 {:ok, _user} <- User.update_and_set_cache(change) do
978 |> json(%{error: inspect(e)})
982 def login(conn, _) do
984 |> render(MastodonView, "login.html", %{error: false})
987 defp get_or_make_app() do
988 with %App{} = app <- Repo.get_by(App, client_name: "Mastodon-Local") do
993 App.register_changeset(%App{}, %{
994 client_name: "Mastodon-Local",
996 scopes: "read,write,follow"
1003 def login_post(conn, %{"authorization" => %{"name" => name, "password" => password}}) do
1004 with %User{} = user <- User.get_by_nickname_or_email(name),
1005 true <- Pbkdf2.checkpw(password, user.password_hash),
1006 {:ok, app} <- get_or_make_app(),
1007 {:ok, auth} <- Authorization.create_authorization(app, user),
1008 {:ok, token} <- Token.exchange_token(app, auth) do
1010 |> put_session(:oauth_token, token.token)
1011 |> redirect(to: "/web/getting-started")
1015 |> render(MastodonView, "login.html", %{error: "Wrong username or password"})
1019 def logout(conn, _) do
1022 |> redirect(to: "/")
1025 def relationship_noop(%{assigns: %{user: user}} = conn, %{"id" => id}) do
1026 Logger.debug("Unimplemented, returning unmodified relationship")
1028 with %User{} = target <- Repo.get(User, id) do
1029 render(conn, AccountView, "relationship.json", %{user: user, target: target})
1033 def empty_array(conn, _) do
1034 Logger.debug("Unimplemented, returning an empty array")
1038 def empty_object(conn, _) do
1039 Logger.debug("Unimplemented, returning an empty object")
1043 def render_notification(user, %{id: id, activity: activity, inserted_at: created_at} = _params) do
1044 actor = User.get_cached_by_ap_id(activity.data["actor"])
1047 NaiveDateTime.to_iso8601(created_at)
1048 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
1050 case activity.data["type"] do
1055 created_at: created_at,
1056 account: AccountView.render("account.json", %{user: actor}),
1057 status: StatusView.render("status.json", %{activity: activity, for: user})
1061 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
1066 created_at: created_at,
1067 account: AccountView.render("account.json", %{user: actor}),
1068 status: StatusView.render("status.json", %{activity: liked_activity, for: user})
1072 announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
1077 created_at: created_at,
1078 account: AccountView.render("account.json", %{user: actor}),
1079 status: StatusView.render("status.json", %{activity: announced_activity, for: user})
1086 created_at: created_at,
1087 account: AccountView.render("account.json", %{user: actor})
1095 def errors(conn, _) do
1098 |> json("Something went wrong")