1 defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
2 use Pleroma.Web, :controller
3 alias Pleroma.{Repo, 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
23 client_id: app.client_id,
24 client_secret: app.client_secret
31 def update_credentials(%{assigns: %{user: user}} = conn, params) do
35 if bio = params["note"] do
36 Map.put(params, "bio", bio)
42 if name = params["display_name"] do
43 Map.put(params, "name", name)
49 if avatar = params["avatar"] do
50 with %Plug.Upload{} <- avatar,
51 {:ok, object} <- ActivityPub.upload(avatar),
52 change = Ecto.Changeset.change(user, %{avatar: object.data}),
53 {:ok, user} = User.update_and_set_cache(change) do
63 if banner = params["header"] do
64 with %Plug.Upload{} <- banner,
65 {:ok, object} <- ActivityPub.upload(banner),
66 new_info <- Map.put(user.info, "banner", object.data),
67 change <- User.info_changeset(user, %{info: new_info}),
68 {:ok, user} <- User.update_and_set_cache(change) do
78 if locked = params["locked"] do
79 with locked <- locked == "true",
80 new_info <- Map.put(user.info, "locked", locked),
81 change <- User.info_changeset(user, %{info: new_info}),
82 {:ok, user} <- User.update_and_set_cache(change) do
91 with changeset <- User.update_changeset(user, params),
92 {:ok, user} <- User.update_and_set_cache(changeset) do
93 if original_user != user do
94 CommonAPI.update(user)
97 json(conn, AccountView.render("account.json", %{user: user}))
102 |> json(%{error: "Invalid request"})
106 def verify_credentials(%{assigns: %{user: user}} = conn, _) do
107 account = AccountView.render("account.json", %{user: user})
111 def user(conn, %{"id" => id}) do
112 with %User{} = user <- Repo.get(User, id) do
113 account = AccountView.render("account.json", %{user: user})
119 |> json(%{error: "Can't find user"})
123 @instance Application.get_env(:pleroma, :instance)
124 @mastodon_api_level "2.3.3"
126 def masto_instance(conn, _params) do
129 title: Keyword.get(@instance, :name),
130 description: "A Pleroma instance, an alternative fediverse server",
131 version: "#{@mastodon_api_level} (compatible; #{Keyword.get(@instance, :version)})",
132 email: Keyword.get(@instance, :email),
134 streaming_api: String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws")
136 stats: Stats.get_stats(),
137 thumbnail: Web.base_url() <> "/instance/thumbnail.jpeg",
138 max_toot_chars: Keyword.get(@instance, :limit)
144 def peers(conn, _params) do
145 json(conn, Stats.get_peers())
148 defp mastodonized_emoji do
149 Pleroma.Formatter.get_custom_emoji()
150 |> Enum.map(fn {shortcode, relative_url} ->
151 url = to_string(URI.merge(Web.base_url(), relative_url))
154 "shortcode" => shortcode,
156 "visible_in_picker" => true,
162 def custom_emojis(conn, _params) do
163 mastodon_emoji = mastodonized_emoji()
164 json(conn, mastodon_emoji)
167 defp add_link_headers(conn, method, activities, param \\ nil, params \\ %{}) do
168 last = List.last(activities)
169 first = List.first(activities)
175 {next_url, prev_url} =
179 Pleroma.Web.Endpoint,
182 Map.merge(params, %{max_id: min})
185 Pleroma.Web.Endpoint,
188 Map.merge(params, %{since_id: max})
194 Pleroma.Web.Endpoint,
196 Map.merge(params, %{max_id: min})
199 Pleroma.Web.Endpoint,
201 Map.merge(params, %{since_id: max})
207 |> put_resp_header("link", "<#{next_url}>; rel=\"next\", <#{prev_url}>; rel=\"prev\"")
213 def home_timeline(%{assigns: %{user: user}} = conn, params) do
216 |> Map.put("type", ["Create", "Announce"])
217 |> Map.put("blocking_user", user)
218 |> Map.put("user", user)
221 ActivityPub.fetch_activities([user.ap_id | user.following], params)
225 |> add_link_headers(:home_timeline, activities)
226 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
229 def public_timeline(%{assigns: %{user: user}} = conn, params) do
230 local_only = params["local"] in [true, "True", "true", "1"]
234 |> Map.put("type", ["Create", "Announce"])
235 |> Map.put("local_only", local_only)
236 |> Map.put("blocking_user", user)
239 ActivityPub.fetch_public_activities(params)
243 |> add_link_headers(:public_timeline, activities, false, %{"local" => local_only})
244 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
247 def user_statuses(%{assigns: %{user: reading_user}} = conn, params) do
248 with %User{} = user <- Repo.get(User, params["id"]) do
249 # Since Pleroma has no "pinned" posts feature, we'll just set an empty list here
251 if params["pinned"] == "true" do
254 ActivityPub.fetch_user_activities(user, reading_user, params)
258 |> add_link_headers(:user_statuses, activities, params["id"])
259 |> render(StatusView, "index.json", %{
260 activities: activities,
267 def dm_timeline(%{assigns: %{user: user}} = conn, _params) do
269 ActivityPub.fetch_activities_query([user.ap_id], %{"type" => "Create", visibility: "direct"})
271 activities = Repo.all(query)
274 |> add_link_headers(:dm_timeline, activities)
275 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
278 def get_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
279 with %Activity{} = activity <- Repo.get(Activity, id),
280 true <- ActivityPub.visible_for_user?(activity, user) do
281 render(conn, StatusView, "status.json", %{activity: activity, for: user})
285 def get_context(%{assigns: %{user: user}} = conn, %{"id" => id}) do
286 with %Activity{} = activity <- Repo.get(Activity, id),
288 ActivityPub.fetch_activities_for_context(activity.data["context"], %{
289 "blocking_user" => user,
293 activities |> Enum.filter(fn %{id: aid} -> to_string(aid) != to_string(id) end),
295 activities |> Enum.filter(fn %{data: %{"type" => type}} -> type == "Create" end),
296 grouped_activities <- Enum.group_by(activities, fn %{id: id} -> id < activity.id end) do
302 activities: grouped_activities[true] || [],
310 activities: grouped_activities[false] || [],
320 def post_status(conn, %{"status" => "", "media_ids" => media_ids} = params)
321 when length(media_ids) > 0 do
324 |> Map.put("status", ".")
326 post_status(conn, params)
329 def post_status(%{assigns: %{user: user}} = conn, %{"status" => _} = params) do
332 |> Map.put("in_reply_to_status_id", params["in_reply_to_id"])
333 |> Map.put("no_attachment_links", true)
336 case get_req_header(conn, "idempotency-key") do
338 _ -> Ecto.UUID.generate()
342 Cachex.fetch!(:idempotency_cache, idempotency_key, fn _ -> CommonAPI.post(user, params) end)
344 render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
347 def delete_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
348 with {:ok, %Activity{}} <- CommonAPI.delete(id, user) do
354 |> json(%{error: "Can't delete this post"})
358 def reblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
359 with {:ok, announce, _activity} <- CommonAPI.repeat(ap_id_or_id, user) do
360 render(conn, StatusView, "status.json", %{activity: announce, for: user, as: :activity})
364 def unreblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
365 with {:ok, _unannounce, %{data: %{"id" => id}}} <- CommonAPI.unrepeat(ap_id_or_id, user),
366 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
367 render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
371 def fav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
372 with {:ok, _fav, %{data: %{"id" => id}}} <- CommonAPI.favorite(ap_id_or_id, user),
373 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
374 render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
378 def unfav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
379 with {:ok, _, _, %{data: %{"id" => id}}} <- CommonAPI.unfavorite(ap_id_or_id, user),
380 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
381 render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
385 def notifications(%{assigns: %{user: user}} = conn, params) do
386 notifications = Notification.for_user(user, params)
389 Enum.map(notifications, fn x ->
390 render_notification(user, x)
395 |> add_link_headers(:notifications, notifications)
399 def get_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
400 with {:ok, notification} <- Notification.get(user, id) do
401 json(conn, render_notification(user, notification))
405 |> put_resp_content_type("application/json")
406 |> send_resp(403, Jason.encode!(%{"error" => reason}))
410 def clear_notifications(%{assigns: %{user: user}} = conn, _params) do
411 Notification.clear(user)
415 def dismiss_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
416 with {:ok, _notif} <- Notification.dismiss(user, id) do
421 |> put_resp_content_type("application/json")
422 |> send_resp(403, Jason.encode!(%{"error" => reason}))
426 def relationships(%{assigns: %{user: user}} = conn, %{"id" => id}) do
428 q = from(u in User, where: u.id in ^id)
429 targets = Repo.all(q)
430 render(conn, AccountView, "relationships.json", %{user: user, targets: targets})
433 def upload(%{assigns: %{user: _}} = conn, %{"file" => file}) do
434 with {:ok, object} <- ActivityPub.upload(file) do
437 |> Map.put("id", object.id)
439 render(conn, StatusView, "attachment.json", %{attachment: data})
443 def favourited_by(conn, %{"id" => id}) do
444 with %Activity{data: %{"object" => %{"likes" => likes}}} <- Repo.get(Activity, id) do
445 q = from(u in User, where: u.ap_id in ^likes)
447 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
453 def reblogged_by(conn, %{"id" => id}) do
454 with %Activity{data: %{"object" => %{"announcements" => announces}}} <- Repo.get(Activity, id) do
455 q = from(u in User, where: u.ap_id in ^announces)
457 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
463 def hashtag_timeline(%{assigns: %{user: user}} = conn, params) do
464 local_only = params["local"] in [true, "True", "true", "1"]
468 |> Map.put("type", "Create")
469 |> Map.put("local_only", local_only)
470 |> Map.put("blocking_user", user)
473 ActivityPub.fetch_public_activities(params)
477 |> add_link_headers(:hashtag_timeline, activities, params["tag"], %{"local" => local_only})
478 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
482 def followers(conn, %{"id" => id}) do
483 with %User{} = user <- Repo.get(User, id),
484 {:ok, followers} <- User.get_followers(user) do
485 render(conn, AccountView, "accounts.json", %{users: followers, as: :user})
489 def following(conn, %{"id" => id}) do
490 with %User{} = user <- Repo.get(User, id),
491 {:ok, followers} <- User.get_friends(user) do
492 render(conn, AccountView, "accounts.json", %{users: followers, as: :user})
496 def follow_requests(%{assigns: %{user: followed}} = conn, _params) do
497 with {:ok, follow_requests} <- User.get_follow_requests(followed) do
498 render(conn, AccountView, "accounts.json", %{users: follow_requests, as: :user})
502 def authorize_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
503 with %User{} = follower <- Repo.get(User, id),
504 {:ok, follower} <- User.maybe_follow(follower, followed),
505 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
506 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "accept"),
508 ActivityPub.accept(%{
509 to: [follower.ap_id],
510 actor: followed.ap_id,
511 object: follow_activity.data["id"],
514 render(conn, AccountView, "relationship.json", %{user: followed, target: follower})
518 |> put_resp_content_type("application/json")
519 |> send_resp(403, Jason.encode!(%{"error" => message}))
523 def reject_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
524 with %User{} = follower <- Repo.get(User, id),
525 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
526 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "reject"),
528 ActivityPub.reject(%{
529 to: [follower.ap_id],
530 actor: followed.ap_id,
531 object: follow_activity.data["id"],
534 render(conn, AccountView, "relationship.json", %{user: followed, target: follower})
538 |> put_resp_content_type("application/json")
539 |> send_resp(403, Jason.encode!(%{"error" => message}))
543 def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
544 with %User{} = followed <- Repo.get(User, id),
545 {:ok, follower} <- User.maybe_direct_follow(follower, followed),
546 {:ok, _activity} <- ActivityPub.follow(follower, followed) do
547 render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
551 |> put_resp_content_type("application/json")
552 |> send_resp(403, Jason.encode!(%{"error" => message}))
556 def follow(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do
557 with %User{} = followed <- Repo.get_by(User, nickname: uri),
558 {:ok, follower} <- User.maybe_direct_follow(follower, followed),
559 {:ok, _activity} <- ActivityPub.follow(follower, followed) do
560 render(conn, AccountView, "account.json", %{user: followed})
564 |> put_resp_content_type("application/json")
565 |> send_resp(403, Jason.encode!(%{"error" => message}))
569 def unfollow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
570 with %User{} = followed <- Repo.get(User, id),
571 {:ok, _activity} <- ActivityPub.unfollow(follower, followed),
572 {:ok, follower, _} <- User.unfollow(follower, followed) do
573 render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
577 def block(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
578 with %User{} = blocked <- Repo.get(User, id),
579 {:ok, blocker} <- User.block(blocker, blocked),
580 {:ok, _activity} <- ActivityPub.block(blocker, blocked) do
581 render(conn, AccountView, "relationship.json", %{user: blocker, target: blocked})
585 |> put_resp_content_type("application/json")
586 |> send_resp(403, Jason.encode!(%{"error" => message}))
590 def unblock(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
591 with %User{} = blocked <- Repo.get(User, id),
592 {:ok, blocker} <- User.unblock(blocker, blocked),
593 {:ok, _activity} <- ActivityPub.unblock(blocker, blocked) do
594 render(conn, AccountView, "relationship.json", %{user: blocker, target: blocked})
598 |> put_resp_content_type("application/json")
599 |> send_resp(403, Jason.encode!(%{"error" => message}))
603 # TODO: Use proper query
604 def blocks(%{assigns: %{user: user}} = conn, _) do
605 with blocked_users <- user.info["blocks"] || [],
606 accounts <- Enum.map(blocked_users, fn ap_id -> User.get_cached_by_ap_id(ap_id) end) do
607 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
612 def domain_blocks(%{assigns: %{user: %{info: info}}} = conn, _) do
613 json(conn, info["domain_blocks"] || [])
616 def block_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
617 User.block_domain(blocker, domain)
621 def unblock_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
622 User.unblock_domain(blocker, domain)
626 def search2(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
627 accounts = User.search(query, params["resolve"] == "true")
630 if Regex.match?(~r/https?:/, query) do
631 with {:ok, activities} <- OStatus.fetch_activity_from_url(query) do
634 %{data: %{"type" => "Create"}} -> true
645 where: fragment("?->>'type' = 'Create'", a.data),
646 where: "https://www.w3.org/ns/activitystreams#Public" in a.recipients,
649 "to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)",
654 order_by: [desc: :id]
657 statuses = Repo.all(q) ++ fetched
659 tags_path = Web.base_url() <> "/tag/"
664 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
665 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
666 |> Enum.map(fn tag -> %{name: tag, url: tags_path <> tag} end)
669 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
671 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
678 def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
679 accounts = User.search(query, params["resolve"] == "true")
682 if Regex.match?(~r/https?:/, query) do
683 with {:ok, activities} <- OStatus.fetch_activity_from_url(query) do
686 %{data: %{"type" => "Create"}} -> true
697 where: fragment("?->>'type' = 'Create'", a.data),
698 where: "https://www.w3.org/ns/activitystreams#Public" in a.recipients,
701 "to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)",
706 order_by: [desc: :id]
709 statuses = Repo.all(q) ++ fetched
714 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
715 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
718 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
720 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
727 def account_search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
728 accounts = User.search(query, params["resolve"] == "true")
730 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
735 def favourites(%{assigns: %{user: user}} = conn, _) do
738 |> Map.put("type", "Create")
739 |> Map.put("favorited_by", user.ap_id)
740 |> Map.put("blocking_user", user)
743 ActivityPub.fetch_public_activities(params)
747 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
750 def get_lists(%{assigns: %{user: user}} = conn, opts) do
751 lists = Pleroma.List.for_user(user, opts)
752 res = ListView.render("lists.json", lists: lists)
756 def get_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
757 with %Pleroma.List{} = list <- Pleroma.List.get(id, user) do
758 res = ListView.render("list.json", list: list)
761 _e -> json(conn, "error")
765 def delete_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
766 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
767 {:ok, _list} <- Pleroma.List.delete(list) do
775 def create_list(%{assigns: %{user: user}} = conn, %{"title" => title}) do
776 with {:ok, %Pleroma.List{} = list} <- Pleroma.List.create(title, user) do
777 res = ListView.render("list.json", list: list)
782 def add_to_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
784 |> Enum.each(fn account_id ->
785 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
786 %User{} = followed <- Repo.get(User, account_id) do
787 Pleroma.List.follow(list, followed)
794 def remove_from_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
796 |> Enum.each(fn account_id ->
797 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
798 %User{} = followed <- Repo.get(Pleroma.User, account_id) do
799 Pleroma.List.unfollow(list, followed)
806 def list_accounts(%{assigns: %{user: user}} = conn, %{"id" => id}) do
807 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
808 {:ok, users} = Pleroma.List.get_following(list) do
809 render(conn, AccountView, "accounts.json", %{users: users, as: :user})
813 def rename_list(%{assigns: %{user: user}} = conn, %{"id" => id, "title" => title}) do
814 with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
815 {:ok, list} <- Pleroma.List.rename(list, title) do
816 res = ListView.render("list.json", list: list)
824 def list_timeline(%{assigns: %{user: user}} = conn, %{"list_id" => id} = params) do
825 with %Pleroma.List{title: title, following: following} <- Pleroma.List.get(id, user) do
828 |> Map.put("type", "Create")
829 |> Map.put("blocking_user", user)
831 # adding title is a hack to not make empty lists function like a public timeline
833 ActivityPub.fetch_activities([title | following], params)
837 |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
842 |> json(%{error: "Error."})
846 def index(%{assigns: %{user: user}} = conn, _params) do
849 |> get_session(:oauth_token)
852 mastodon_emoji = mastodonized_emoji()
853 accounts = Map.put(%{}, user.id, AccountView.render("account.json", %{user: user}))
858 streaming_api_base_url:
859 String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws"),
862 domain: Pleroma.Web.Endpoint.host(),
865 unfollow_modal: false,
868 auto_play_gif: false,
869 display_sensitive_media: false,
870 reduce_motion: false,
871 max_toot_chars: Keyword.get(@instance, :limit)
874 delete_others_notice: !!user.info["is_moderator"]
878 default_privacy: user.info["default_scope"] || "public",
879 default_sensitive: false
881 media_attachments: %{
882 accept_content_types: [
898 Map.get(user.info, "settings") ||
928 push_subscription: nil,
930 custom_emojis: mastodon_emoji,
931 char_limit: Keyword.get(@instance, :limit)
937 |> render(MastodonView, "index.html", %{initial_state: initial_state})
940 |> redirect(to: "/web/login")
944 def put_settings(%{assigns: %{user: user}} = conn, %{"data" => settings} = _params) do
945 with new_info <- Map.put(user.info, "settings", settings),
946 change <- User.info_changeset(user, %{info: new_info}),
947 {:ok, _user} <- User.update_and_set_cache(change) do
953 |> json(%{error: inspect(e)})
957 def login(conn, _) do
959 |> render(MastodonView, "login.html", %{error: false})
962 defp get_or_make_app() do
963 with %App{} = app <- Repo.get_by(App, client_name: "Mastodon-Local") do
968 App.register_changeset(%App{}, %{
969 client_name: "Mastodon-Local",
971 scopes: "read,write,follow"
978 def login_post(conn, %{"authorization" => %{"name" => name, "password" => password}}) do
979 with %User{} = user <- User.get_by_nickname_or_email(name),
980 true <- Pbkdf2.checkpw(password, user.password_hash),
981 {:ok, app} <- get_or_make_app(),
982 {:ok, auth} <- Authorization.create_authorization(app, user),
983 {:ok, token} <- Token.exchange_token(app, auth) do
985 |> put_session(:oauth_token, token.token)
986 |> redirect(to: "/web/getting-started")
990 |> render(MastodonView, "login.html", %{error: "Wrong username or password"})
994 def logout(conn, _) do
1000 def relationship_noop(%{assigns: %{user: user}} = conn, %{"id" => id}) do
1001 Logger.debug("Unimplemented, returning unmodified relationship")
1003 with %User{} = target <- Repo.get(User, id) do
1004 render(conn, AccountView, "relationship.json", %{user: user, target: target})
1008 def empty_array(conn, _) do
1009 Logger.debug("Unimplemented, returning an empty array")
1013 def empty_object(conn, _) do
1014 Logger.debug("Unimplemented, returning an empty object")
1018 def render_notification(user, %{id: id, activity: activity, inserted_at: created_at} = _params) do
1019 actor = User.get_cached_by_ap_id(activity.data["actor"])
1022 NaiveDateTime.to_iso8601(created_at)
1023 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
1025 case activity.data["type"] do
1030 created_at: created_at,
1031 account: AccountView.render("account.json", %{user: actor}),
1032 status: StatusView.render("status.json", %{activity: activity, for: user})
1036 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
1041 created_at: created_at,
1042 account: AccountView.render("account.json", %{user: actor}),
1043 status: StatusView.render("status.json", %{activity: liked_activity, for: user})
1047 announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
1052 created_at: created_at,
1053 account: AccountView.render("account.json", %{user: actor}),
1054 status: StatusView.render("status.json", %{activity: announced_activity, for: user})
1061 created_at: created_at,
1062 account: AccountView.render("account.json", %{user: actor})
1070 def errors(conn, _) do
1073 |> json("Something went wrong")
1076 @suggestions Application.get_env(:pleroma, :suggestions)
1078 def suggestions(%{assigns: %{user: user}} = conn, _) do
1079 host = String.replace Web.base_url(), "https://", ""
1080 user = user.nickname
1081 api = Keyword.get(@suggestions, :third_party_engine, "")
1082 url = String.replace(api, "{{host}}", host) |> String.replace("{{user}}", user)
1083 with {:ok, %{status_code: 200, body: body}} <-
1084 @httpoison.get(url, [], [timeout: 300000, recv_timeout: 300000]),
1085 {:ok, data} <- Jason.decode(body) do
1086 data2 = Enum.slice(data, 0, 40) |> Enum.map(fn(x) ->
1087 Map.put(x, "id", User.get_or_fetch(x["acct"]).id)
1092 e -> Logger.error("Could not decode user at fetch #{url}, #{inspect(e)}")