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