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