ostatus_controller.ex 5.57 KB
Newer Older
lain's avatar
lain committed
1
2
3
defmodule Pleroma.Web.OStatus.OStatusController do
  use Pleroma.Web, :controller

4
  alias Pleroma.{User, Activity, Object}
5
  alias Pleroma.Web.OStatus.{FeedRepresenter, ActivityRepresenter}
lain's avatar
lain committed
6
  alias Pleroma.Repo
7
  alias Pleroma.Web.{OStatus, Federator}
8
  alias Pleroma.Web.XML
9
  alias Pleroma.Web.ActivityPub.ObjectView
lain's avatar
lain committed
10
  alias Pleroma.Web.ActivityPub.ActivityPubController
lain's avatar
lain committed
11
  alias Pleroma.Web.ActivityPub.ActivityPub
lain's avatar
lain committed
12

href's avatar
href committed
13
  plug(Pleroma.Web.FederatingPlug when action in [:salmon_incoming])
14
  action_fallback(:errors)
15

16
  def feed_redirect(conn, %{"nickname" => nickname}) do
Rachel H's avatar
Rachel H committed
17
18
19
20
21
22
23
24
25
26
27
28
29
    case get_format(conn) do
      "html" ->
        Fallback.RedirectController.redirector(conn, nil)

      "activity+json" ->
        ActivityPubController.call(conn, :user)

      _ ->
        with %User{} = user <- User.get_cached_by_nickname(nickname) do
          redirect(conn, external: OStatus.feed_path(user))
        else
          nil -> {:error, :not_found}
        end
lain's avatar
lain committed
30
    end
31
32
  end

33
  def feed(conn, %{"nickname" => nickname} = params) do
Rachel H's avatar
Rachel H committed
34
    with %User{} = user <- User.get_cached_by_nickname(nickname) do
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
      query_params =
        Map.take(params, ["max_id"])
        |> Map.merge(%{"whole_db" => true, "actor_id" => user.ap_id})

      activities =
        ActivityPub.fetch_public_activities(query_params)
        |> Enum.reverse()

      response =
        user
        |> FeedRepresenter.to_simple_form(activities, [user])
        |> :xmerl.export_simple(:xmerl_xml)
        |> to_string

      conn
      |> put_resp_content_type("application/atom+xml")
      |> send_resp(200, response)
    else
Rachel H's avatar
Rachel H committed
53
      nil -> {:error, :not_found}
54
    end
lain's avatar
lain committed
55
  end
lain's avatar
lain committed
56

57
58
59
60
61
62
63
64
65
  defp decode_or_retry(body) do
    with {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
         {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
      {:ok, doc}
    else
      _e ->
        with [decoded | _] <- Pleroma.Web.Salmon.decode(body),
             doc <- XML.parse_document(decoded),
             uri when not is_nil(uri) <- XML.string_from_xpath("/entry/author[1]/uri", doc),
Thog's avatar
Thog committed
66
             {:ok, _} <- Pleroma.Web.OStatus.make_user(uri, true),
67
68
69
70
71
72
73
             {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
             {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
          {:ok, doc}
        end
    end
  end

Thog's avatar
Thog committed
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
81

    conn
    |> send_resp(200, "")
lain's avatar
lain committed
82
  end
83

84
  def object(conn, %{"uuid" => uuid}) do
85
    if get_format(conn) == "activity+json" do
86
      ActivityPubController.call(conn, :object)
87
88
    else
      with id <- o_status_url(conn, :object, uuid),
89
90
           {_, %Activity{} = activity} <-
             {:activity, Activity.get_create_activity_by_object_ap_id(id)},
lain's avatar
lain committed
91
           {_, true} <- {:public?, ActivityPub.is_public?(activity)},
lain's avatar
lain committed
92
           %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
93
94
        case get_format(conn) do
          "html" -> redirect(conn, to: "/notice/#{activity.id}")
95
          _ -> represent_activity(conn, nil, activity, user)
96
        end
lain's avatar
lain committed
97
98
      else
        {:public?, false} ->
99
100
101
102
103
104
105
          {:error, :not_found}

        {:activity, nil} ->
          {:error, :not_found}

        e ->
          e
106
      end
lain's avatar
lain committed
107
108
109
110
111
    end
  end

  def activity(conn, %{"uuid" => uuid}) do
    with id <- o_status_url(conn, :activity, uuid),
112
         {_, %Activity{} = activity} <- {:activity, Activity.normalize(id)},
lain's avatar
lain committed
113
         {_, true} <- {:public?, ActivityPub.is_public?(activity)},
lain's avatar
lain committed
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
lain's avatar
lain committed
119
120
    else
      {:public?, false} ->
121
122
123
124
125
126
127
        {:error, :not_found}

      {:activity, nil} ->
        {:error, :not_found}

      e ->
        e
lain's avatar
lain committed
128
129
    end
  end
130

lain's avatar
lain committed
131
  def notice(conn, %{"id" => id}) do
132
    with {_, %Activity{} = activity} <- {:activity, Repo.get(Activity, id)},
lain's avatar
lain committed
133
         {_, true} <- {:public?, ActivityPub.is_public?(activity)},
lain's avatar
lain committed
134
         %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
135
      case format = get_format(conn) do
lain's avatar
lain committed
136
137
138
139
        "html" ->
          conn
          |> put_resp_content_type("text/html")
          |> send_file(200, "priv/static/index.html")
lain's avatar
lain committed
140
141

        _ ->
142
          represent_activity(conn, format, activity, user)
lain's avatar
lain committed
143
      end
lain's avatar
lain committed
144
145
    else
      {:public?, false} ->
146
147
148
149
150
151
152
        {:error, :not_found}

      {:activity, nil} ->
        {:error, :not_found}

      e ->
        e
lain's avatar
lain committed
153
154
155
    end
  end

156
157
158
159
160
161
162
163
  defp represent_activity(
         conn,
         "activity+json",
         %Activity{data: %{"type" => "Create"}} = activity,
         user
       ) do
    object = Object.normalize(activity.data["object"])

164
165
    conn
    |> put_resp_header("content-type", "application/activity+json")
166
167
168
169
170
    |> json(ObjectView.render("object.json", %{object: object}))
  end

  defp represent_activity(conn, "activity+json", _, _) do
    {:error, :not_found}
171
172
173
  end

  defp represent_activity(conn, _, activity, user) do
lain's avatar
lain committed
174
175
176
177
178
179
    response =
      activity
      |> ActivityRepresenter.to_simple_form(user, true)
      |> ActivityRepresenter.wrap_with_entry()
      |> :xmerl.export_simple(:xmerl_xml)
      |> to_string
180
181
182
183

    conn
    |> put_resp_content_type("application/atom+xml")
    |> send_resp(200, response)
lain's avatar
lain committed
184
  end
185
186
187
188
189
190
191
192
193
194
195
196

  def errors(conn, {:error, :not_found}) do
    conn
    |> put_status(404)
    |> text("Not found")
  end

  def errors(conn, _) do
    conn
    |> put_status(500)
    |> text("Something went wrong")
  end
lain's avatar
lain committed
197
end