Merge branch 'bugfix/announce-timeline-flooding' into 'develop'
[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.ObjectView
10 alias Pleroma.Web.ActivityPub.ActivityPubController
11 alias Pleroma.Web.ActivityPub.ActivityPub
12
13 action_fallback(:errors)
14
15 def feed_redirect(conn, %{"nickname" => nickname}) do
16 case get_format(conn) do
17 "html" ->
18 Fallback.RedirectController.redirector(conn, nil)
19
20 "activity+json" ->
21 ActivityPubController.call(conn, :user)
22
23 _ ->
24 with %User{} = user <- User.get_cached_by_nickname(nickname) do
25 redirect(conn, external: OStatus.feed_path(user))
26 else
27 nil -> {:error, :not_found}
28 end
29 end
30 end
31
32 def feed(conn, %{"nickname" => nickname} = params) do
33 with %User{} = user <- User.get_cached_by_nickname(nickname) do
34 query_params =
35 Map.take(params, ["max_id"])
36 |> Map.merge(%{"whole_db" => true, "actor_id" => user.ap_id})
37
38 activities =
39 ActivityPub.fetch_public_activities(query_params)
40 |> Enum.reverse()
41
42 response =
43 user
44 |> FeedRepresenter.to_simple_form(activities, [user])
45 |> :xmerl.export_simple(:xmerl_xml)
46 |> to_string
47
48 conn
49 |> put_resp_content_type("application/atom+xml")
50 |> send_resp(200, response)
51 else
52 nil -> {:error, :not_found}
53 end
54 end
55
56 defp decode_or_retry(body) do
57 with {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
58 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
59 {:ok, doc}
60 else
61 _e ->
62 with [decoded | _] <- Pleroma.Web.Salmon.decode(body),
63 doc <- XML.parse_document(decoded),
64 uri when not is_nil(uri) <- XML.string_from_xpath("/entry/author[1]/uri", doc),
65 {:ok, _} <- Pleroma.Web.OStatus.make_user(uri, true),
66 {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
67 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
68 {:ok, doc}
69 end
70 end
71 end
72
73 def salmon_incoming(conn, _) do
74 {:ok, body, _conn} = read_body(conn)
75 {:ok, doc} = decode_or_retry(body)
76
77 Federator.enqueue(:incoming_doc, doc)
78
79 conn
80 |> send_resp(200, "")
81 end
82
83 def object(conn, %{"uuid" => uuid}) do
84 if get_format(conn) == "activity+json" do
85 ActivityPubController.call(conn, :object)
86 else
87 with id <- o_status_url(conn, :object, uuid),
88 {_, %Activity{} = activity} <-
89 {:activity, Activity.get_create_activity_by_object_ap_id(id)},
90 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
91 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
92 case get_format(conn) do
93 "html" -> redirect(conn, to: "/notice/#{activity.id}")
94 _ -> represent_activity(conn, nil, activity, user)
95 end
96 else
97 {:public?, false} ->
98 {:error, :not_found}
99
100 {:activity, nil} ->
101 {:error, :not_found}
102
103 e ->
104 e
105 end
106 end
107 end
108
109 def activity(conn, %{"uuid" => uuid}) do
110 with id <- o_status_url(conn, :activity, uuid),
111 {_, %Activity{} = activity} <- {:activity, Activity.normalize(id)},
112 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
113 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
114 case format = get_format(conn) do
115 "html" -> redirect(conn, to: "/notice/#{activity.id}")
116 _ -> represent_activity(conn, format, activity, user)
117 end
118 else
119 {:public?, false} ->
120 {:error, :not_found}
121
122 {:activity, nil} ->
123 {:error, :not_found}
124
125 e ->
126 e
127 end
128 end
129
130 def notice(conn, %{"id" => id}) do
131 with {_, %Activity{} = activity} <- {:activity, Repo.get(Activity, id)},
132 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
133 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
134 case format = get_format(conn) do
135 "html" ->
136 conn
137 |> put_resp_content_type("text/html")
138 |> send_file(200, "priv/static/index.html")
139
140 _ ->
141 represent_activity(conn, format, activity, user)
142 end
143 else
144 {:public?, false} ->
145 {:error, :not_found}
146
147 {:activity, nil} ->
148 {:error, :not_found}
149
150 e ->
151 e
152 end
153 end
154
155 defp represent_activity(conn, "activity+json", activity, user) do
156 conn
157 |> put_resp_header("content-type", "application/activity+json")
158 |> json(ObjectView.render("object.json", %{object: activity}))
159 end
160
161 defp represent_activity(conn, _, activity, user) do
162 response =
163 activity
164 |> ActivityRepresenter.to_simple_form(user, true)
165 |> ActivityRepresenter.wrap_with_entry()
166 |> :xmerl.export_simple(:xmerl_xml)
167 |> to_string
168
169 conn
170 |> put_resp_content_type("application/atom+xml")
171 |> send_resp(200, response)
172 end
173
174 def errors(conn, {:error, :not_found}) do
175 conn
176 |> put_status(404)
177 |> text("Not found")
178 end
179
180 def errors(conn, _) do
181 conn
182 |> put_status(500)
183 |> text("Something went wrong")
184 end
185 end