1 defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
2 use Pleroma.Web, :controller
3 alias Pleroma.{Repo, Object, Activity, User, Notification, Stats}
5 alias Pleroma.Web.MastodonAPI.{StatusView, AccountView, MastodonView, ListView, FilterView}
6 alias Pleroma.Web.ActivityPub.ActivityPub
7 alias Pleroma.Web.ActivityPub.Utils
8 alias Pleroma.Web.CommonAPI
9 alias Pleroma.Web.OAuth.{Authorization, Token, App}
10 alias Pleroma.Web.MediaProxy
15 @httpoison Application.get_env(:pleroma, :httpoison)
17 action_fallback(:errors)
19 def create_app(conn, params) do
20 with cs <- App.register_changeset(%App{}, params) |> IO.inspect(),
21 {:ok, app} <- Repo.insert(cs) |> IO.inspect() do
23 id: app.id |> to_string,
24 name: app.client_name,
25 client_id: app.client_id,
26 client_secret: app.client_secret,
27 redirect_uri: app.redirect_uris,
35 def update_credentials(%{assigns: %{user: user}} = conn, params) do
39 if bio = params["note"] do
40 Map.put(params, "bio", bio)
46 if name = params["display_name"] do
47 Map.put(params, "name", name)
53 if avatar = params["avatar"] do
54 with %Plug.Upload{} <- avatar,
55 {:ok, object} <- ActivityPub.upload(avatar),
56 change = Ecto.Changeset.change(user, %{avatar: object.data}),
57 {:ok, user} = User.update_and_set_cache(change) do
67 if banner = params["header"] do
68 with %Plug.Upload{} <- banner,
69 {:ok, object} <- ActivityPub.upload(banner),
70 new_info <- Map.put(user.info, "banner", object.data),
71 change <- User.info_changeset(user, %{info: new_info}),
72 {:ok, user} <- User.update_and_set_cache(change) do
82 if locked = params["locked"] do
83 with locked <- locked == "true",
84 new_info <- Map.put(user.info, "locked", locked),
85 change <- User.info_changeset(user, %{info: new_info}),
86 {:ok, user} <- User.update_and_set_cache(change) do
95 with changeset <- User.update_changeset(user, params),
96 {:ok, user} <- User.update_and_set_cache(changeset) do
97 if original_user != user do
98 CommonAPI.update(user)
101 json(conn, AccountView.render("account.json", %{user: user, for: user}))
106 |> json(%{error: "Invalid request"})
110 def verify_credentials(%{assigns: %{user: user}} = conn, _) do
111 account = AccountView.render("account.json", %{user: user, for: user})
115 def user(%{assigns: %{user: for_user}} = conn, %{"id" => id}) do
116 with %User{} = user <- Repo.get(User, id) do
117 account = AccountView.render("account.json", %{user: user, for: for_user})
123 |> json(%{error: "Can't find user"})
127 @instance Application.get_env(:pleroma, :instance)
128 @mastodon_api_level "2.5.0"
130 def masto_instance(conn, _params) do
133 title: Keyword.get(@instance, :name),
134 description: Keyword.get(@instance, :description),
135 version: "#{@mastodon_api_level} (compatible; #{Keyword.get(@instance, :version)})",
136 email: Keyword.get(@instance, :email),
138 streaming_api: String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws")
140 stats: Stats.get_stats(),
141 thumbnail: Web.base_url() <> "/instance/thumbnail.jpeg",
142 max_toot_chars: Keyword.get(@instance, :limit)
148 def peers(conn, _params) do
149 json(conn, Stats.get_peers())
152 defp mastodonized_emoji do
153 Pleroma.Formatter.get_custom_emoji()
154 |> Enum.map(fn {shortcode, relative_url} ->
155 url = to_string(URI.merge(Web.base_url(), relative_url))
158 "shortcode" => shortcode,
160 "visible_in_picker" => true,
166 def custom_emojis(conn, _params) do
167 mastodon_emoji = mastodonized_emoji()
168 json(conn, mastodon_emoji)
171 defp add_link_headers(conn, method, activities, param \\ nil, params \\ %{}) do
172 last = List.last(activities)
173 first = List.first(activities)
179 {next_url, prev_url} =
183 Pleroma.Web.Endpoint,
186 Map.merge(params, %{max_id: min})
189 Pleroma.Web.Endpoint,
192 Map.merge(params, %{since_id: max})
198 Pleroma.Web.Endpoint,
200 Map.merge(params, %{max_id: min})
203 Pleroma.Web.Endpoint,
205 Map.merge(params, %{since_id: max})
211 |> put_resp_header("link", "<#{next_url}>; rel=\"next\", <#{prev_url}>; rel=\"prev\"")
217 def home_timeline(%{assigns: %{user: user}} = conn, params) do
220 |> Map.put("type", ["Create", "Announce"])
221 |> Map.put("blocking_user", user)
222 |> Map.put("user", user)
225 ActivityPub.fetch_activities([user.ap_id | user.following], params)
229 |> add_link_headers(:home_timeline, activities)
230 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
233 def public_timeline(%{assigns: %{user: user}} = conn, params) do
234 local_only = params["local"] in [true, "True", "true", "1"]
238 |> Map.put("type", ["Create", "Announce"])
239 |> Map.put("local_only", local_only)
240 |> Map.put("blocking_user", user)
243 ActivityPub.fetch_public_activities(params)
247 |> add_link_headers(:public_timeline, activities, false, %{"local" => local_only})
248 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
251 def user_statuses(%{assigns: %{user: reading_user}} = conn, params) do
252 with %User{} = user <- Repo.get(User, params["id"]) do
253 # Since Pleroma has no "pinned" posts feature, we'll just set an empty list here
255 if params["pinned"] == "true" do
258 ActivityPub.fetch_user_activities(user, reading_user, params)
262 |> add_link_headers(:user_statuses, activities, params["id"])
263 |> render(StatusView, "index.json", %{
264 activities: activities,
271 def dm_timeline(%{assigns: %{user: user}} = conn, _params) do
273 ActivityPub.fetch_activities_query([user.ap_id], %{"type" => "Create", visibility: "direct"})
275 activities = Repo.all(query)
278 |> add_link_headers(:dm_timeline, activities)
279 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
282 def get_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
283 with %Activity{} = activity <- Repo.get(Activity, id),
284 true <- ActivityPub.visible_for_user?(activity, user) do
285 render(conn, StatusView, "status.json", %{activity: activity, for: user})
289 def get_context(%{assigns: %{user: user}} = conn, %{"id" => id}) do
290 with %Activity{} = activity <- Repo.get(Activity, id),
292 ActivityPub.fetch_activities_for_context(activity.data["context"], %{
293 "blocking_user" => user,
297 activities |> Enum.filter(fn %{id: aid} -> to_string(aid) != to_string(id) end),
299 activities |> Enum.filter(fn %{data: %{"type" => type}} -> type == "Create" end),
300 grouped_activities <- Enum.group_by(activities, fn %{id: id} -> id < activity.id end) do
306 activities: grouped_activities[true] || [],
314 activities: grouped_activities[false] || [],
324 def post_status(conn, %{"status" => "", "media_ids" => media_ids} = params)
325 when length(media_ids) > 0 do
328 |> Map.put("status", ".")
330 post_status(conn, params)
333 def post_status(%{assigns: %{user: user}} = conn, %{"status" => _} = params) do
336 |> Map.put("in_reply_to_status_id", params["in_reply_to_id"])
337 |> Map.put("no_attachment_links", true)
340 case get_req_header(conn, "idempotency-key") do
342 _ -> Ecto.UUID.generate()
346 Cachex.fetch!(:idempotency_cache, idempotency_key, fn _ -> CommonAPI.post(user, params) end)
348 render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
351 def delete_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
352 with {:ok, %Activity{}} <- CommonAPI.delete(id, user) do
358 |> json(%{error: "Can't delete this post"})
362 def reblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
363 with {:ok, announce, _activity} <- CommonAPI.repeat(ap_id_or_id, user) do
364 render(conn, StatusView, "status.json", %{activity: announce, for: user, as: :activity})
368 def unreblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
369 with {:ok, _unannounce, %{data: %{"id" => id}}} <- CommonAPI.unrepeat(ap_id_or_id, user),
370 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
371 render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
375 def fav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
376 with {:ok, _fav, %{data: %{"id" => id}}} <- CommonAPI.favorite(ap_id_or_id, user),
377 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
378 render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
382 def unfav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
383 with {:ok, _, _, %{data: %{"id" => id}}} <- CommonAPI.unfavorite(ap_id_or_id, user),
384 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
385 render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
389 def notifications(%{assigns: %{user: user}} = conn, params) do
390 notifications = Notification.for_user(user, params)
393 Enum.map(notifications, fn x ->
394 render_notification(user, x)
399 |> add_link_headers(:notifications, notifications)
403 def get_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
404 with {:ok, notification} <- Notification.get(user, id) do
405 json(conn, render_notification(user, notification))
409 |> put_resp_content_type("application/json")
410 |> send_resp(403, Jason.encode!(%{"error" => reason}))
414 def clear_notifications(%{assigns: %{user: user}} = conn, _params) do
415 Notification.clear(user)
419 def dismiss_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
420 with {:ok, _notif} <- Notification.dismiss(user, id) do
425 |> put_resp_content_type("application/json")
426 |> send_resp(403, Jason.encode!(%{"error" => reason}))
430 def relationships(%{assigns: %{user: user}} = conn, %{"id" => id}) do
432 q = from(u in User, where: u.id in ^id)
433 targets = Repo.all(q)
434 render(conn, AccountView, "relationships.json", %{user: user, targets: targets})
437 def update_media(%{assigns: %{user: _}} = conn, data) do
438 with %Object{} = object <- Repo.get(Object, data["id"]),
439 true <- is_binary(data["description"]),
440 description <- data["description"] do
441 new_data = %{object.data | "name" => description}
443 change = Object.change(object, %{data: new_data})
444 {:ok, _} = Repo.update(change)
448 |> Map.put("id", object.id)
450 render(conn, StatusView, "attachment.json", %{attachment: data})
454 def upload(%{assigns: %{user: _}} = conn, %{"file" => file} = data) do
455 with {:ok, object} <- ActivityPub.upload(file) do
457 if Map.has_key?(data, "description") do
458 Map.put(object.data, "name", data["description"])
463 change = Object.change(object, %{data: objdata})
464 {:ok, object} = Repo.update(change)
468 |> Map.put("id", object.id)
470 render(conn, StatusView, "attachment.json", %{attachment: objdata})
474 def favourited_by(conn, %{"id" => id}) do
475 with %Activity{data: %{"object" => %{"likes" => likes}}} <- Repo.get(Activity, id) do
476 q = from(u in User, where: u.ap_id in ^likes)
478 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
484 def reblogged_by(conn, %{"id" => id}) do
485 with %Activity{data: %{"object" => %{"announcements" => announces}}} <- Repo.get(Activity, id) do
486 q = from(u in User, where: u.ap_id in ^announces)
488 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
494 def hashtag_timeline(%{assigns: %{user: user}} = conn, params) do
495 local_only = params["local"] in [true, "True", "true", "1"]
499 |> Map.put("type", "Create")
500 |> Map.put("local_only", local_only)
501 |> Map.put("blocking_user", user)
504 ActivityPub.fetch_public_activities(params)
508 |> add_link_headers(:hashtag_timeline, activities, params["tag"], %{"local" => local_only})
509 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
513 def followers(conn, %{"id" => id}) do
514 with %User{} = user <- Repo.get(User, id),
515 {:ok, followers} <- User.get_followers(user) do
516 render(conn, AccountView, "accounts.json", %{users: followers, as: :user})
520 def following(conn, %{"id" => id}) do
521 with %User{} = user <- Repo.get(User, id),
522 {:ok, followers} <- User.get_friends(user) do
523 render(conn, AccountView, "accounts.json", %{users: followers, as: :user})
527 def follow_requests(%{assigns: %{user: followed}} = conn, _params) do
528 with {:ok, follow_requests} <- User.get_follow_requests(followed) do
529 render(conn, AccountView, "accounts.json", %{users: follow_requests, as: :user})
533 def authorize_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
534 with %User{} = follower <- Repo.get(User, id),
535 {:ok, follower} <- User.maybe_follow(follower, followed),
536 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
537 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "accept"),
539 ActivityPub.accept(%{
540 to: [follower.ap_id],
541 actor: followed.ap_id,
542 object: follow_activity.data["id"],
545 render(conn, AccountView, "relationship.json", %{user: followed, target: follower})
549 |> put_resp_content_type("application/json")
550 |> send_resp(403, Jason.encode!(%{"error" => message}))
554 def reject_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
555 with %User{} = follower <- Repo.get(User, id),
556 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
557 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "reject"),
559 ActivityPub.reject(%{
560 to: [follower.ap_id],
561 actor: followed.ap_id,
562 object: follow_activity.data["id"],
565 render(conn, AccountView, "relationship.json", %{user: followed, target: follower})
569 |> put_resp_content_type("application/json")
570 |> send_resp(403, Jason.encode!(%{"error" => message}))
574 def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
575 with %User{} = followed <- Repo.get(User, id),
576 {:ok, follower} <- User.maybe_direct_follow(follower, followed),
577 {:ok, _activity} <- ActivityPub.follow(follower, followed) do
578 render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
582 |> put_resp_content_type("application/json")
583 |> send_resp(403, Jason.encode!(%{"error" => message}))
587 def follow(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do
588 with %User{} = followed <- Repo.get_by(User, nickname: uri),
589 {:ok, follower} <- User.maybe_direct_follow(follower, followed),
590 {:ok, _activity} <- ActivityPub.follow(follower, followed) do
591 render(conn, AccountView, "account.json", %{user: followed, for: follower})
595 |> put_resp_content_type("application/json")
596 |> send_resp(403, Jason.encode!(%{"error" => message}))
600 def unfollow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
601 with %User{} = followed <- Repo.get(User, id),
602 {:ok, _activity} <- ActivityPub.unfollow(follower, followed),
603 {:ok, follower, _} <- User.unfollow(follower, followed) do
604 render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
608 def block(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
609 with %User{} = blocked <- Repo.get(User, id),
610 {:ok, blocker} <- User.block(blocker, blocked),
611 {:ok, _activity} <- ActivityPub.block(blocker, blocked) do
612 render(conn, AccountView, "relationship.json", %{user: blocker, target: blocked})
616 |> put_resp_content_type("application/json")
617 |> send_resp(403, Jason.encode!(%{"error" => message}))
621 def unblock(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
622 with %User{} = blocked <- Repo.get(User, id),
623 {:ok, blocker} <- User.unblock(blocker, blocked),
624 {:ok, _activity} <- ActivityPub.unblock(blocker, blocked) do
625 render(conn, AccountView, "relationship.json", %{user: blocker, target: blocked})
629 |> put_resp_content_type("application/json")
630 |> send_resp(403, Jason.encode!(%{"error" => message}))
634 # TODO: Use proper query
635 def blocks(%{assigns: %{user: user}} = conn, _) do
636 with blocked_users <- user.info["blocks"] || [],
637 accounts <- Enum.map(blocked_users, fn ap_id -> User.get_cached_by_ap_id(ap_id) end) do
638 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
643 def domain_blocks(%{assigns: %{user: %{info: info}}} = conn, _) do
644 json(conn, info["domain_blocks"] || [])
647 def block_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
648 User.block_domain(blocker, domain)
652 def unblock_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
653 User.unblock_domain(blocker, domain)
657 def status_search(query) do
659 if Regex.match?(~r/https?:/, query) do
660 with {:ok, object} <- ActivityPub.fetch_object_from_id(query) do
661 [Activity.get_create_activity_by_object_ap_id(object.data["id"])]
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 Repo.all(q) ++ fetched
685 def search2(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
686 accounts = User.search(query, params["resolve"] == "true")
688 statuses = status_search(query)
690 tags_path = Web.base_url() <> "/tag/"
695 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
696 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
697 |> Enum.map(fn tag -> %{name: tag, url: tags_path <> tag} end)
700 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
702 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
709 def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
710 accounts = User.search(query, params["resolve"] == "true")
712 statuses = status_search(query)
717 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
718 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
721 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
723 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
730 def account_search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
731 accounts = User.search(query, params["resolve"] == "true")
733 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
738 def favourites(%{assigns: %{user: user}} = conn, _) do
741 |> Map.put("type", "Create")
742 |> Map.put("favorited_by", user.ap_id)
743 |> Map.put("blocking_user", user)
746 ActivityPub.fetch_public_activities(params)
750 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
753 def get_lists(%{assigns: %{user: user}} = conn, opts) do
754 lists = Pleroma.List.for_user(user, opts)
755 res = ListView.render("lists.json", lists: lists)
759 def get_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
760 with %Pleroma.List{} = list <- Pleroma.List.get(id, user) do
761 res = ListView.render("list.json", list: list)
764 _e -> json(conn, "error")
768 def delete_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
769 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
770 {:ok, _list} <- Pleroma.List.delete(list) do
778 def create_list(%{assigns: %{user: user}} = conn, %{"title" => title}) do
779 with {:ok, %Pleroma.List{} = list} <- Pleroma.List.create(title, user) do
780 res = ListView.render("list.json", list: list)
785 def add_to_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
787 |> Enum.each(fn account_id ->
788 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
789 %User{} = followed <- Repo.get(User, account_id) do
790 Pleroma.List.follow(list, followed)
797 def remove_from_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
799 |> Enum.each(fn account_id ->
800 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
801 %User{} = followed <- Repo.get(Pleroma.User, account_id) do
802 Pleroma.List.unfollow(list, followed)
809 def list_accounts(%{assigns: %{user: user}} = conn, %{"id" => id}) do
810 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
811 {:ok, users} = Pleroma.List.get_following(list) do
812 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
816 def rename_list(%{assigns: %{user: user}} = conn, %{"id" => id, "title" => title}) do
817 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
818 {:ok, list} <- Pleroma.List.rename(list, title) do
819 res = ListView.render("list.json", list: list)
827 def list_timeline(%{assigns: %{user: user}} = conn, %{"list_id" => id} = params) do
828 with %Pleroma.List{title: title, following: following} <- Pleroma.List.get(id, user) do
831 |> Map.put("type", "Create")
832 |> Map.put("blocking_user", user)
834 # we must filter the following list for the user to avoid leaking statuses the user
835 # does not actually have permission to see (for more info, peruse security issue #270).
838 |> Enum.filter(fn x -> x in user.following end)
841 ActivityPub.fetch_activities_bounded(following_to, following, params)
845 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
850 |> json(%{error: "Error."})
854 def index(%{assigns: %{user: user}} = conn, _params) do
857 |> get_session(:oauth_token)
860 mastodon_emoji = mastodonized_emoji()
863 Map.put(%{}, user.id, AccountView.render("account.json", %{user: user, for: user}))
868 streaming_api_base_url:
869 String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws"),
872 domain: Pleroma.Web.Endpoint.host(),
875 unfollow_modal: false,
878 auto_play_gif: false,
879 display_sensitive_media: false,
880 reduce_motion: false,
881 max_toot_chars: Keyword.get(@instance, :limit)
884 delete_others_notice: !!user.info["is_moderator"]
888 default_privacy: user.info["default_scope"] || "public",
889 default_sensitive: false
891 media_attachments: %{
892 accept_content_types: [
908 Map.get(user.info, "settings") ||
938 push_subscription: nil,
940 custom_emojis: mastodon_emoji,
941 char_limit: Keyword.get(@instance, :limit)
947 |> render(MastodonView, "index.html", %{initial_state: initial_state})
950 |> redirect(to: "/web/login")
954 def put_settings(%{assigns: %{user: user}} = conn, %{"data" => settings} = _params) do
955 with new_info <- Map.put(user.info, "settings", settings),
956 change <- User.info_changeset(user, %{info: new_info}),
957 {:ok, _user} <- User.update_and_set_cache(change) do
963 |> json(%{error: inspect(e)})
967 def login(conn, _) do
969 |> render(MastodonView, "login.html", %{error: false})
972 defp get_or_make_app() do
973 with %App{} = app <- Repo.get_by(App, client_name: "Mastodon-Local") do
978 App.register_changeset(%App{}, %{
979 client_name: "Mastodon-Local",
981 scopes: "read,write,follow"
988 def login_post(conn, %{"authorization" => %{"name" => name, "password" => password}}) do
989 with %User{} = user <- User.get_by_nickname_or_email(name),
990 true <- Pbkdf2.checkpw(password, user.password_hash),
991 {:ok, app} <- get_or_make_app(),
992 {:ok, auth} <- Authorization.create_authorization(app, user),
993 {:ok, token} <- Token.exchange_token(app, auth) do
995 |> put_session(:oauth_token, token.token)
996 |> redirect(to: "/web/getting-started")
1000 |> render(MastodonView, "login.html", %{error: "Wrong username or password"})
1004 def logout(conn, _) do
1007 |> redirect(to: "/")
1010 def relationship_noop(%{assigns: %{user: user}} = conn, %{"id" => id}) do
1011 Logger.debug("Unimplemented, returning unmodified relationship")
1013 with %User{} = target <- Repo.get(User, id) do
1014 render(conn, AccountView, "relationship.json", %{user: user, target: target})
1018 def empty_array(conn, _) do
1019 Logger.debug("Unimplemented, returning an empty array")
1023 def empty_object(conn, _) do
1024 Logger.debug("Unimplemented, returning an empty object")
1028 def render_notification(user, %{id: id, activity: activity, inserted_at: created_at} = _params) do
1029 actor = User.get_cached_by_ap_id(activity.data["actor"])
1032 NaiveDateTime.to_iso8601(created_at)
1033 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
1035 id = id |> to_string
1037 case activity.data["type"] do
1042 created_at: created_at,
1043 account: AccountView.render("account.json", %{user: actor, for: user}),
1044 status: StatusView.render("status.json", %{activity: activity, for: user})
1048 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
1053 created_at: created_at,
1054 account: AccountView.render("account.json", %{user: actor, for: user}),
1055 status: StatusView.render("status.json", %{activity: liked_activity, for: user})
1059 announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
1064 created_at: created_at,
1065 account: AccountView.render("account.json", %{user: actor, for: user}),
1066 status: StatusView.render("status.json", %{activity: announced_activity, for: user})
1073 created_at: created_at,
1074 account: AccountView.render("account.json", %{user: actor, for: user})
1082 def get_filters(%{assigns: %{user: user}} = conn, _) do
1083 filters = Pleroma.Filter.get_filters(user)
1084 res = FilterView.render("filters.json", filters: filters)
1089 %{assigns: %{user: user}} = conn,
1090 %{"phrase" => phrase, "context" => context} = params
1092 query = %Pleroma.Filter{
1096 hide: Map.get(params, "irreversible", nil),
1097 whole_word: Map.get(params, "boolean", true)
1101 {:ok, response} = Pleroma.Filter.create(query)
1102 res = FilterView.render("filter.json", filter: response)
1106 def get_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do
1107 filter = Pleroma.Filter.get(filter_id, user)
1108 res = FilterView.render("filter.json", filter: filter)
1113 %{assigns: %{user: user}} = conn,
1114 %{"phrase" => phrase, "context" => context, "id" => filter_id} = params
1116 query = %Pleroma.Filter{
1118 filter_id: filter_id,
1121 hide: Map.get(params, "irreversible", nil),
1122 whole_word: Map.get(params, "boolean", true)
1126 {:ok, response} = Pleroma.Filter.update(query)
1127 res = FilterView.render("filter.json", filter: response)
1131 def delete_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do
1132 query = %Pleroma.Filter{
1134 filter_id: filter_id
1137 {:ok, _} = Pleroma.Filter.delete(query)
1141 def errors(conn, _) do
1144 |> json("Something went wrong")
1147 @suggestions Application.get_env(:pleroma, :suggestions)
1149 def suggestions(%{assigns: %{user: user}} = conn, _) do
1150 if Keyword.get(@suggestions, :enabled, false) do
1151 api = Keyword.get(@suggestions, :third_party_engine, "")
1152 timeout = Keyword.get(@suggestions, :timeout, 5000)
1153 limit = Keyword.get(@suggestions, :limit, 23)
1156 Application.get_env(:pleroma, Pleroma.Web.Endpoint)
1157 |> Keyword.get(:url)
1158 |> Keyword.get(:host)
1160 user = user.nickname
1161 url = String.replace(api, "{{host}}", host) |> String.replace("{{user}}", user)
1163 with {:ok, %{status_code: 200, body: body}} <-
1164 @httpoison.get(url, [], timeout: timeout, recv_timeout: timeout),
1165 {:ok, data} <- Jason.decode(body) do
1167 Enum.slice(data, 0, limit)
1172 case User.get_or_fetch(x["acct"]) do
1179 Map.put(x, "avatar", MediaProxy.url(x["avatar"]))
1182 Map.put(x, "avatar_static", MediaProxy.url(x["avatar_static"]))
1188 e -> Logger.error("Could not retrieve suggestions at fetch #{url}, #{inspect(e)}")