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