Use desc id to sort instead of desc inserted_at
[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 import Ecto.Query
9
10 def feed_redirect(conn, %{"nickname" => nickname}) do
11 user = User.get_cached_by_nickname(nickname)
12
13 case get_format(conn) do
14 "html" -> Fallback.RedirectController.redirector(conn, nil)
15 _ -> redirect conn, external: OStatus.feed_path(user)
16 end
17 end
18
19 def feed(conn, %{"nickname" => nickname}) do
20 user = User.get_cached_by_nickname(nickname)
21 query = from activity in Activity,
22 where: fragment("?->>'actor' = ?", activity.data, ^user.ap_id),
23 limit: 20,
24 order_by: [desc: :id]
25
26 activities = query
27 |> Repo.all
28
29 response = user
30 |> FeedRepresenter.to_simple_form(activities, [user])
31 |> :xmerl.export_simple(:xmerl_xml)
32 |> to_string
33
34 conn
35 |> put_resp_content_type("application/atom+xml")
36 |> send_resp(200, response)
37 end
38
39 def salmon_incoming(conn, params) do
40 {:ok, body, _conn} = read_body(conn)
41 {:ok, magic_key} = Pleroma.Web.Salmon.fetch_magic_key(body)
42 {:ok, doc} = Pleroma.Web.Salmon.decode_and_validate(magic_key, body)
43
44 Federator.enqueue(:incoming_doc, doc)
45
46 conn
47 |> send_resp(200, "")
48 end
49
50 def object(conn, %{"uuid" => uuid}) do
51 with id <- o_status_url(conn, :object, uuid),
52 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id),
53 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
54 case get_format(conn) do
55 "html" -> redirect(conn, to: "/notice/#{activity.id}")
56 _ -> represent_activity(conn, activity, user)
57 end
58 end
59 end
60
61 def activity(conn, %{"uuid" => uuid}) do
62 with id <- o_status_url(conn, :activity, uuid),
63 %Activity{} = activity <- Activity.get_by_ap_id(id),
64 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
65 case get_format(conn) do
66 "html" -> redirect(conn, to: "/notice/#{activity.id}")
67 _ -> represent_activity(conn, activity, user)
68 end
69 end
70 end
71
72 defp represent_activity(conn, activity, user) do
73 response = activity
74 |> ActivityRepresenter.to_simple_form(user, true)
75 |> ActivityRepresenter.wrap_with_entry
76 |> :xmerl.export_simple(:xmerl_xml)
77 |> to_string
78
79 conn
80 |> put_resp_content_type("application/atom+xml")
81 |> send_resp(200, response)
82 end
83 end