mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-12-17 22:47:59 +01:00
rss api
This commit is contained in:
parent
664bd0b0dd
commit
927cc55073
6 changed files with 134 additions and 0 deletions
34
lib/philomena_web/controllers/api/watched_controller.ex
Normal file
34
lib/philomena_web/controllers/api/watched_controller.ex
Normal file
|
@ -0,0 +1,34 @@
|
|||
defmodule PhilomenaWeb.Api.WatchedController do
|
||||
use PhilomenaWeb, :controller
|
||||
|
||||
alias Philomena.Images.{Image, Query}
|
||||
import Ecto.Query
|
||||
|
||||
def index(conn, _params) do
|
||||
user = conn.assigns.current_user
|
||||
filter = conn.assigns.compiled_filter
|
||||
|
||||
{:ok, query} = Query.compile(user, "my:watched")
|
||||
|
||||
images =
|
||||
Image.search_records(
|
||||
%{
|
||||
query: %{
|
||||
bool: %{
|
||||
must: query,
|
||||
must_not: [
|
||||
filter,
|
||||
%{term: %{hidden_from_users: true}}
|
||||
]
|
||||
}
|
||||
},
|
||||
sort: %{created_at: :desc}
|
||||
},
|
||||
conn.assigns.image_pagination,
|
||||
Image |> preload(:tags)
|
||||
)
|
||||
|
||||
conn
|
||||
|> render("index.rss", images: images)
|
||||
end
|
||||
end
|
32
lib/philomena_web/plugs/api_token_plug.ex
Normal file
32
lib/philomena_web/plugs/api_token_plug.ex
Normal file
|
@ -0,0 +1,32 @@
|
|||
defmodule PhilomenaWeb.ApiTokenPlug do
|
||||
|
||||
alias Philomena.Users.User
|
||||
alias Philomena.Repo
|
||||
alias Pow.Plug
|
||||
import Ecto.Query
|
||||
|
||||
def init([]), do: []
|
||||
|
||||
def call(conn, _opts) do
|
||||
conn
|
||||
|> maybe_find_user(conn.params["key"])
|
||||
|> maybe_assign_user()
|
||||
end
|
||||
|
||||
defp maybe_find_user(conn, nil), do: {conn, nil}
|
||||
defp maybe_find_user(conn, key) do
|
||||
user =
|
||||
User
|
||||
|> where(authentication_token: ^key)
|
||||
|> Repo.one()
|
||||
|
||||
{conn, user}
|
||||
end
|
||||
|
||||
defp maybe_assign_user({conn, nil}), do: conn
|
||||
defp maybe_assign_user({conn, user}) do
|
||||
config = Plug.fetch_config(conn)
|
||||
|
||||
Plug.assign_current_user(conn, user, config)
|
||||
end
|
||||
end
|
|
@ -21,6 +21,18 @@ defmodule PhilomenaWeb.Router do
|
|||
plug PhilomenaWeb.ChannelPlug
|
||||
end
|
||||
|
||||
pipeline :rss do
|
||||
plug :accepts, ["rss"]
|
||||
|
||||
plug PhilomenaWeb.ApiTokenPlug
|
||||
plug Pow.Plug.RequireAuthenticated,
|
||||
error_handler: Pow.Phoenix.PlugErrorHandler
|
||||
plug PhilomenaWeb.EnsureUserEnabledPlug
|
||||
plug PhilomenaWeb.CurrentFilterPlug
|
||||
plug PhilomenaWeb.ImageFilterPlug
|
||||
plug PhilomenaWeb.PaginationPlug
|
||||
end
|
||||
|
||||
pipeline :api do
|
||||
plug :accepts, ["json"]
|
||||
end
|
||||
|
@ -54,6 +66,12 @@ defmodule PhilomenaWeb.Router do
|
|||
end
|
||||
end
|
||||
|
||||
scope "/api/rss", PhilomenaWeb.Api, as: :api_rss do
|
||||
pipe_through :rss
|
||||
|
||||
resources "/watched", WatchedController, only: [:index]
|
||||
end
|
||||
|
||||
scope "/", PhilomenaWeb do
|
||||
pipe_through [:browser, :ensure_totp, :protected]
|
||||
|
||||
|
|
26
lib/philomena_web/templates/api/watched/index.rss.eex
Normal file
26
lib/philomena_web/templates/api/watched/index.rss.eex
Normal file
|
@ -0,0 +1,26 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
|
||||
<rss version="2.0">
|
||||
<channel>
|
||||
<title>Derpibooru Watchlist</title>
|
||||
<description>Your watched tags feed from Derpibooru</description>
|
||||
<link><%= Routes.api_rss_watched_url(@conn, :index) %></link>
|
||||
<lastBuildDate><%= last_build_date() %></lastBuildDate>
|
||||
|
||||
<%= for image <- @images do %>
|
||||
<item>
|
||||
<title><%= "##{image.id} - #{image.tag_list_cache}" %></title>
|
||||
<description>
|
||||
<![CDATA[
|
||||
<% mouseovertext = "Size: #{image.image_width}x#{image.image_height} | Tagged: #{image.tag_list_cache}" %>
|
||||
<a href="<%= Routes.image_url(@conn, :show, image) %>">
|
||||
<img src="https:<%= medium_url(image) %>" alt="<%= mouseovertext %>" title="<%= mouseovertext %>"/>
|
||||
</a>
|
||||
]]>
|
||||
</description>
|
||||
<pubDate><%= NaiveDateTime.to_iso8601(image.created_at) %></pubDate>
|
||||
<link><%= Routes.image_url(@conn, :show, image) %></link>
|
||||
<guid><%= Routes.image_url(@conn, :show, image) %></guid>
|
||||
</item>
|
||||
<% end %>
|
||||
</channel>
|
||||
</rss>
|
|
@ -38,6 +38,14 @@ h1 Content Settings
|
|||
=> checkbox f, :no_spoilered_in_watched, class: "checkbox"
|
||||
=> label f, :no_spoilered_in_watched, "Hide images spoilered by filter in watchlist"
|
||||
|
||||
h4 Other
|
||||
p
|
||||
' RSS feed link (for Google Reader, Thunderbird, etc.):
|
||||
br
|
||||
= url_input f, :subscribe_url, value: Routes.api_rss_watched_url(@conn, :index, key: @conn.assigns.current_user.authentication_token), class: "input input--wide"
|
||||
br
|
||||
' Do not share this URL with anyone, it may allow an attacker to compromise your account.
|
||||
|
||||
.block__tab.hidden.flex.flex--maybe-wrap data-tab="display"
|
||||
div
|
||||
.field
|
||||
|
|
16
lib/philomena_web/views/api/watched_view.ex
Normal file
16
lib/philomena_web/views/api/watched_view.ex
Normal file
|
@ -0,0 +1,16 @@
|
|||
defmodule PhilomenaWeb.Api.WatchedView do
|
||||
use PhilomenaWeb, :view
|
||||
|
||||
alias PhilomenaWeb.ImageView
|
||||
|
||||
def last_build_date do
|
||||
DateTime.utc_now()
|
||||
|> DateTime.to_iso8601()
|
||||
end
|
||||
|
||||
def medium_url(image) do
|
||||
image
|
||||
|> ImageView.thumb_urls(false)
|
||||
|> Map.get(:medium)
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue