Browse Source

Store show license/privacy/ignored in database

Douglas Thrift 6 years ago
parent
commit
fe2d8bf8fc
5 changed files with 80 additions and 12 deletions
  1. 1 0
      Gemfile
  2. 3 0
      Gemfile.lock
  3. 64 0
      app.rb
  4. 8 11
      views/index.erb
  5. 4 1
      views/layout.erb

+ 1 - 0
Gemfile

@@ -5,6 +5,7 @@ gem 'flickr-login'
 gem 'flickraw'
 gem 'pry'
 gem 'pry-doc'
+gem 'sequel_enum'
 gem 'sinatra'
 gem 'sinatra-contrib'
 gem 'sinatra-sequel'

+ 3 - 0
Gemfile.lock

@@ -21,6 +21,8 @@ GEM
     rack-protection (2.0.0)
       rack
     sequel (5.2.0)
+    sequel_enum (0.1.1)
+      sequel
     sinatra (2.0.0)
       mustermann (~> 1.0)
       rack (~> 2.0)
@@ -49,6 +51,7 @@ DEPENDENCIES
   flickraw
   pry
   pry-doc
+  sequel_enum
   sinatra
   sinatra-contrib
   sinatra-sequel

+ 64 - 0
app.rb

@@ -1,6 +1,7 @@
 require 'flickr/login'
 require 'flickraw'
 require 'pry'
+require 'sequel_enum'
 require 'sinatra'
 require 'sinatra/config_file'
 require 'sinatra/json'
@@ -22,6 +23,9 @@ migration 'create users, licenses, and photos tables' do
     column :username, String
     column :fullname, String
     # column :json, 'text'
+    # foreign_key :show_license_id, :licenses, null: true, on_delete: :set_null, on_update: :restrict
+    # column :show_privacy, Integer, default: 0, null: false
+    # column :show_ignored, 'boolean', default: true, null: false
   end
 
   database.create_table :licenses do
@@ -80,8 +84,19 @@ migration 'add json field to user' do
   end
 end
 
+migration 'add show license/privacy/ignored fields to user' do
+  database.alter_table :users do
+    add_foreign_key :show_license_id, :licenses, null: true, on_delete: :set_null, on_update: :restrict
+    add_column :show_privacy, Integer, default: 0, null: false
+    add_column :show_ignored, 'boolean', default: true, null: false
+  end
+end
+
 class User < Sequel::Model
+  plugin :enum
   one_to_many :photos
+  many_to_one :show_license, class: :License
+  enum :show_privacy, [:all, :public, :friends_family, :friends, :family, :private]
   unrestrict_primary_key
 
   def flickraw
@@ -166,6 +181,9 @@ before do
     info.timezone = info.timezone.to_hash
     info.photos = info.photos.to_hash
     user.json = info.to_h.to_json
+    user.show_license = nil
+    user.show_privacy = :all
+    user.show_ignored = true
   end
 end
 
@@ -178,6 +196,21 @@ get '/' do
       license.url = flickr_license.url
     end
   end if @licenses.count == 0
+  @show_licenses = [
+    OpenStruct.new(id: nil, name: 'show photos with any license'),
+  ] + @licenses
+  @show_privacies = {
+    all: 'show public and private photos',
+    public: 'show only public photos',
+    friends_family: 'show photos visible to friends and family',
+    friends: 'show photos visible to only friends',
+    family: 'show photos visible to only family',
+    private: 'show completely private photos',
+  }
+  @show_ignoreds = {
+    true => 'show ignored photos',
+    false => 'hide ignored photos',
+  }
   erb :index
 end
 
@@ -209,6 +242,37 @@ get %r{/photos/([1-8])} do |page|
   json photos: photos
 end
 
+post '/user' do
+  halt 422, json(error: 'Missing required parameter(s)') unless %w(show_license show_privacy show_ignored).any? {|param| params[param]}
+  show_license_id = params['show_license']
+  if show_license_id
+    if show_license_id.empty?
+      show_license = nil
+    else
+      show_license_id = show_license_id.to_i
+      show_license = License[show_license_id]
+      halt 422, json(error: "Could not find license with ID: #{show_license_id.inspect}") unless show_license
+    end
+    @user.show_license = show_license
+    @user.save
+  end
+  show_privacy = params['show_privacy']
+  if show_privacy
+    @user.show_privacy = show_privacy
+    begin
+      @user.save
+    rescue Sequel::NotNullConstraintViolation
+      halt 422, json(error: "Invalid privacy value: #{show_privacy.inspect}")
+    end
+  end
+  show_ignored = params['show_ignored']
+  if show_ignored
+    @user.show_ignored = show_ignored == 'true'
+    @user.save
+  end
+  status 204
+end
+
 post '/photos' do
   ignore = params['ignore'] == 'true'
   ids = params['photos'] && params['photos'].is_a?(Array) ? params['photos'] : [params['photo']]

+ 8 - 11
views/index.erb

@@ -1,22 +1,19 @@
 <div>
     <button id="reload_photos" type="button">reload photos</button>
     <select id="show_license">
-        <option value="all" selected>show photos with any license</option>
-        <% @licenses.each do |license| %>
-            <option value="<%= license.id %>"><%= license.name %></option>
+        <% @show_licenses.each do |show_license| %>
+            <option <% if show_license == @user.show_license %>selected <% end %>value="<%= show_license.id %>"><%= show_license.name %></option>
         <% end %>
     </select>
     <select id="show_privacy">
-        <option value="all" selected>show public and private photos</option>
-        <option value="public">show only public photos</option>
-        <option value="friends/family">show photos visible to friends and family</option>
-        <option value="friends">show photos visible to only friends</option>
-        <option value="family">show photos visible to only family</option>
-        <option value="private">show completely private photos</option>
+        <% @show_privacies.each do |show_privacy, name| %>
+            <option <% if show_privacy == @user.show_privacy %>selected <% end %>value="<%= show_privacy %>"><%= name %></option>
+        <% end %>
     </select>
     <select id="show_ignored">
-        <option value="true" selected>show ignored photos</option>
-        <option value="false">hide ignored photos</option>
+        <% @show_ignoreds.each do |show_ignored, name| %>
+            <option <% if show_ignored == @user.show_ignored %>selected <% end %>value="<%= show_ignored %>"><%= name %></option>
+        <% end %>
     </select>
 </div>
 

+ 4 - 1
views/layout.erb

@@ -13,7 +13,10 @@
     </head>
     <body>
         <header container>
-            <div><span style="color: #0063dc;">flick</span><span style="color: #ff0084;">r</span> license <a href="<%= @user.photosurl %>"><img src="<%= @user.buddyicon %>"> <%= @user.username %></a> <% if @user.fullname && !@user.fullname.empty? %>(<%= @user.fullname %>)<% end %> | <a href="/logout">logout</a></div>
+            <div grid="center">
+                <div column><span style="color: #0063dc;">flick</span><span style="color: #ff0084;">r</span> license</div>
+                <div column style="text-align: right;"><a href="<%= @user.photosurl %>"><img src="<%= @user.buddyicon %>"> <%= @user.username %></a> <% if @user.fullname && !@user.fullname.empty? %>(<%= @user.fullname %>)<% end %> | <a href="/logout">logout</a></div>
+            </div>
         </header>
         <main container>
             <%= yield %>