introduce max_id parameter in feed.atom
[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 import Ecto.Query
10
11 def feed_redirect(conn, %{"nickname" => nickname}) do
12 user = User.get_cached_by_nickname(nickname)
13
14 case get_format(conn) do
15 "html" -> Fallback.RedirectController.redirector(conn, nil)
16 _ -> redirect conn, external: OStatus.feed_path(user)
17 end
18 end
19
20 def feed(conn, %{"nickname" => nickname}) do
21 user = User.get_cached_by_nickname(nickname)
22 query = from activity in Activity,
23 where: fragment("?->>'actor' = ?", activity.data, ^user.ap_id),
24 limit: 20,
25 order_by: [desc: :id]
26
27 activities = query
28 |> restrict_max(opts)
29 |> Repo.all
30
31 response = user
32 |> FeedRepresenter.to_simple_form(activities, [user])
33 |> :xmerl.export_simple(:xmerl_xml)
34 |> to_string
35
36 conn
37 |> put_resp_content_type("application/atom+xml")
38 |> send_resp(200, response)
39 end
40
41 defp decode_or_retry(body) do
42 with {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
43 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
44 {:ok, doc}
45 else
46 _e ->
47 with [decoded | _] <- Pleroma.Web.Salmon.decode(body),
48 doc <- XML.parse_document(decoded),
49 uri when not is_nil(uri) <- XML.string_from_xpath("/entry/author[1]/uri", doc),
50 {:ok, _} <- Pleroma.Web.OStatus.make_user(uri, true),
51 {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
52 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
53 {:ok, doc}
54 end
55 end
56 end
57
58 defp restrict_max(query, %{"max_id" => max_id}) do
59 from activity in query, where: activity.id < ^max_id
60 end
61 defp restrict_max(query, _), do: query
62
63 def salmon_incoming(conn, _) do
64 {:ok, body, _conn} = read_body(conn)
65 {:ok, doc} = decode_or_retry(body)
66
67 Federator.enqueue(:incoming_doc, doc)
68
69 conn
70 |> send_resp(200, "")
71 end
72
73 def object(conn, %{"uuid" => uuid}) do
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
84 def activity(conn, %{"uuid" => uuid}) do
85 with id <- o_status_url(conn, :activity, uuid),
86 %Activity{} = activity <- Activity.get_by_ap_id(id),
87 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
88 case get_format(conn) do
89 "html" -> redirect(conn, to: "/notice/#{activity.id}")
90 _ -> represent_activity(conn, activity, user)
91 end
92 end
93 end
94
95 def notice(conn, %{"id" => id}) do
96 with %Activity{} = activity <- Repo.get(Activity, id),
97 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
98 case get_format(conn) do
99 "html" ->
100 conn
101 |> put_resp_content_type("text/html")
102 |> send_file(200, "priv/static/index.html")
103 _ -> represent_activity(conn, activity, user)
104 end
105 end
106 end
107
108 defp represent_activity(conn, activity, user) do
109 response = activity
110 |> ActivityRepresenter.to_simple_form(user, true)
111 |> ActivityRepresenter.wrap_with_entry
112 |> :xmerl.export_simple(:xmerl_xml)
113 |> to_string
114
115 conn
116 |> put_resp_content_type("application/atom+xml")
117 |> send_resp(200, response)
118 end
119 end