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