|
@@ -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']]
|