Fix atom feed pagination.
[akkoma] / lib / pleroma / web / ostatus / ostatus_controller.ex
1 defmodule Pleroma.Web.OStatus.OStatusController do
2 use Pleroma.Web, :controller
3
4 alias Pleroma.{User, Activity}
5 alias Pleroma.Web.OStatus.{FeedRepresenter, ActivityRepresenter}
6 alias Pleroma.Repo
7 alias Pleroma.Web.{OStatus, Federator}
8 alias Pleroma.Web.XML
9 alias Pleroma.Web.ActivityPub.ActivityPubController
10 alias Pleroma.Web.ActivityPub.ActivityPub
11 import Ecto.Query
12
13 def feed_redirect(conn, %{"nickname" => nickname} = params) do
14 user = User.get_cached_by_nickname(nickname)
15
16 case get_format(conn) do
17 "html" -> Fallback.RedirectController.redirector(conn, nil)
18 "activity+json" -> ActivityPubController.user(conn, params)
19 _ -> redirect conn, external: OStatus.feed_path(user)
20 end
21 end
22
23 def feed(conn, %{"nickname" => nickname} = params) do
24 user = User.get_cached_by_nickname(nickname)
25
26 query_params = Map.take(params, ["max_id"])
27 |> Map.merge(%{"whole_db" => true, "actor_id" => user.ap_id})
28
29 activities = ActivityPub.fetch_public_activities(query_params)
30 |> Enum.reverse
31
32 response = user
33 |> FeedRepresenter.to_simple_form(activities, [user])
34 |> :xmerl.export_simple(:xmerl_xml)
35 |> to_string
36
37 conn
38 |> put_resp_content_type("application/atom+xml")
39 |> send_resp(200, response)
40 end
41
42 defp decode_or_retry(body) do
43 with {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
44 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
45 {:ok, doc}
46 else
47 _e ->
48 with [decoded | _] <- Pleroma.Web.Salmon.decode(body),
49 doc <- XML.parse_document(decoded),
50 uri when not is_nil(uri) <- XML.string_from_xpath("/entry/author[1]/uri", doc),
51 {:ok, _} <- Pleroma.Web.OStatus.make_user(uri, true),
52 {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
53 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
54 {:ok, doc}
55 end
56 end
57 end
58
59 def salmon_incoming(conn, _) do
60 {:ok, body, _conn} = read_body(conn)
61 {:ok, doc} = decode_or_retry(body)
62
63 Federator.enqueue(:incoming_doc, doc)
64
65 conn
66 |> send_resp(200, "")
67 end
68
69 # TODO: Data leak
70 def object(conn, %{"uuid" => uuid} = params) do
71 if get_format(conn) == "activity+json" do
72 ActivityPubController.object(conn, params)
73 else
74 with id <- o_status_url(conn, :object, uuid),
75 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id),
76 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
77 case get_format(conn) do
78 "html" -> redirect(conn, to: "/notice/#{activity.id}")
79 _ -> represent_activity(conn, activity, user)
80 end
81 end
82 end
83 end
84
85 # TODO: Data leak
86 def activity(conn, %{"uuid" => uuid}) do
87 with id <- o_status_url(conn, :activity, uuid),
88 %Activity{} = activity <- Activity.get_by_ap_id(id),
89 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
90 case get_format(conn) do
91 "html" -> redirect(conn, to: "/notice/#{activity.id}")
92 _ -> represent_activity(conn, activity, user)
93 end
94 end
95 end
96
97 # TODO: Data leak
98 def notice(conn, %{"id" => id}) do
99 with %Activity{} = activity <- Repo.get(Activity, id),
100 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
101 case get_format(conn) do
102 "html" ->
103 conn
104 |> put_resp_content_type("text/html")
105 |> send_file(200, "priv/static/index.html")
106 _ -> represent_activity(conn, activity, user)
107 end
108 end
109 end
110
111 defp represent_activity(conn, activity, user) do
112 response = activity
113 |> ActivityRepresenter.to_simple_form(user, true)
114 |> ActivityRepresenter.wrap_with_entry
115 |> :xmerl.export_simple(:xmerl_xml)
116 |> to_string
117
118 conn
119 |> put_resp_content_type("application/atom+xml")
120 |> send_resp(200, response)
121 end
122 end