Skip to content

Commit

Permalink
Merge pull request #534 from photonia-io/rss
Browse files Browse the repository at this point in the history
RSS feeds for photos & albums
  • Loading branch information
janosrusiczki authored Apr 25, 2023
2 parents c16a480 + bf6ca77 commit de12058
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 15 deletions.
3 changes: 3 additions & 0 deletions app/Procfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
backend: bex rails s
frontend: bin/vite dev
sidekiq: bex sidekiq
7 changes: 7 additions & 0 deletions app/controllers/albums_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ def show
@album = Album.includes(:albums_photos).friendly.find(params[:id])
@pagy, @photos = pagy(@album.photos.order(:ordering))
end

def feed
@albums = Album.all.order(created_at: :desc).limit(30)
respond_to do |format|
format.xml
end
end
end
7 changes: 7 additions & 0 deletions app/controllers/photos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ def upload
@photo = Photo.new
end

def feed
@photos = Photo.all.order(imported_at: :desc).limit(30)
respond_to do |format|
format.xml
end
end

def create
@photo = Photo.new(photo_params)
authorize @photo
Expand Down
36 changes: 22 additions & 14 deletions app/javascript/photos/show.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
:id="photo.id"
:title="photoTitle()"
@update-title="updatePhotoTitle"
@enable-keyboard-shortcuts="enableKeyboardShortcuts"
@disable-keyboard-shortcuts="disableKeyboardShortcuts"
@enable-keyboard-shortcuts="enableNavigationShortcuts"
@disable-keyboard-shortcuts="disableNavigationShortcuts"
/>
<h1
v-else
Expand Down Expand Up @@ -46,8 +46,8 @@
:id="photo.id"
:description="photoDescription()"
@update-description="updatePhotoDescription"
@enable-keyboard-shortcuts="enableKeyboardShortcuts"
@disable-keyboard-shortcuts="disableKeyboardShortcuts"
@enable-keyboard-shortcuts="enableNavigationShortcuts"
@disable-keyboard-shortcuts="disableNavigationShortcuts"
/>
<div
v-else
Expand Down Expand Up @@ -276,30 +276,38 @@
const userStore = useUserStore()
onMounted(() => {
enableKeyboardShortcuts()
enableNavigationShortcuts()
})
onBeforeUnmount(() => {
disableKeyboardShortcuts()
disableNavigationShortcuts()
})
const enableKeyboardShortcuts = () => {
const enableNavigationShortcuts = () => {
document.addEventListener('keydown', handleKeyDown)
}
const disableKeyboardShortcuts = () => {
const disableNavigationShortcuts = () => {
document.removeEventListener('keydown', handleKeyDown)
}
const handleKeyDown = (event) => {
if (event.key === 'ArrowLeft') {
if (photo.value.previousPhoto) {
router.push({ name: 'photos-show', params: { id: photo.value.previousPhoto.id } })
}
navigateToPreviousPhoto()
} else if (event.key === 'ArrowRight') {
if (photo.value.nextPhoto) {
router.push({ name: 'photos-show', params: { id: photo.value.nextPhoto.id } })
}
navigateToNextPhoto()
}
}
const navigateToNextPhoto = () => {
if (photo.value.nextPhoto) {
router.push({ name: 'photos-show', params: { id: photo.value.nextPhoto.id } })
}
}
const navigateToPreviousPhoto = () => {
if (photo.value.previousPhoto) {
router.push({ name: 'photos-show', params: { id: photo.value.previousPhoto.id } })
}
}
</script>
21 changes: 21 additions & 0 deletions app/views/albums/feed.xml.builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
xml.instruct! :xml, version: "1.0"
xml.rss version: "2.0" do
xml.channel do
xml.title "Photonia - Albums"
xml.description "Albums by Janos Rusiczki"
xml.link albums_url

@albums.each do |album|
xml.item do
xml.title album.title
xml.description album.description
xml.pubDate album.created_at.to_s(:rfc822)
xml.link album_url(album)
xml.guid album_url(album)
if cover_image = album&.photos&.first&.image(:medium_intelligent).presence || album&.photos&.first&.image(:medium_square)
xml.enclosure url: cover_image.url, type: cover_image.mime_type, length: cover_image.size
end
end
end
end
end
1 change: 1 addition & 0 deletions app/views/albums/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<% content_for(:page_title, 'Albums' + (@pagy.page == 1 ? '' : " - Page #{@pagy.page}")) %>
<% content_for(:autodiscovery_link, auto_discovery_link_tag(:rss, feed_albums_url(format: :xml), title: 'Photonia Albums RSS')) %>
<h1>Albums</h1>
<hr>
<div>
Expand Down
7 changes: 7 additions & 0 deletions app/views/homepage/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
<%
content_for(
:autodiscovery_link,
auto_discovery_link_tag(:rss, feed_photos_url(format: :xml), title: 'Photonia Photos RSS') + "\n " +
auto_discovery_link_tag(:rss, feed_albums_url(format: :xml), title: 'Photonia Albums RSS')
)
%>
<div>
<h1>Latest photo</h1>
<a href="<%= photo_path(@latest_photo) %>">
Expand Down
2 changes: 2 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
<script defer src="https://use.fontawesome.com/releases/v5.15.4/js/all.js"></script>
<%= ENV['PHOTONIA_TRACKING_CODE']&.html_safe %>

<%= yield(:autodiscovery_link) %>

<%= javascript_tag nonce: true do %>
window.configuration_json = <%= @configuration_json.html_safe %>
window.gql_queries = <%= @gql_queries.html_safe %>
Expand Down
25 changes: 25 additions & 0 deletions app/views/photos/feed.xml.builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
xml.instruct! :xml, version: "1.0"
xml.rss version: "2.0", "xmlns:media": "http://search.yahoo.com/mrss/" do
xml.channel do
xml.title "Photonia - Photos"
xml.description "Photos by Janos Rusiczki"
xml.link photos_url

@photos.each do |photo|
next unless medium_image = photo.image(:medium)
thumbnail_image = photo.image(:thumbnail_intelligent).presence || photo.image(:thumbnail_square)
xml.item do
xml.title photo.name
xml.description photo.description
xml.pubDate photo.created_at.to_s(:rfc822)
xml.link photo_url(photo)
xml.guid photo_url(photo)
xml.media :content, url: medium_image.url, type: medium_image.mime_type, width: medium_image.width, height: medium_image.height
xml.media title: photo.name
xml.media description: photo.description
xml.media :thumbnail, url: thumbnail_image.url, width: thumbnail_image.width, height: thumbnail_image.height
xml.media :credit, role: "photographer", content: "Janos Rusiczki"
end
end
end
end
1 change: 1 addition & 0 deletions app/views/photos/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<% content_for(:page_title, 'Photos' + (@pagy.page == 1 ? '' : " - Page #{@pagy.page}")) %>
<% content_for(:autodiscovery_link, auto_discovery_link_tag(:rss, feed_photos_url(format: :xml), title: 'Photonia Photos RSS')) %>
<h1>Photos</h1>
<hr>
<div>
Expand Down
5 changes: 4 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@

resources :photos, except: %i[new] do
get :upload, on: :collection
get :feed, on: :collection, format: :xml
end
resources :tags, only: %i[index show]
resources :albums, only: %i[index show]
resources :albums, only: %i[index show] do
get :feed, on: :collection, format: :xml
end

post '/graphql', to: 'graphql#execute'

Expand Down

0 comments on commit de12058

Please sign in to comment.