From 704d40caddc5e5506acc0ff214004f37bc7d28cf Mon Sep 17 00:00:00 2001 From: nilsding Date: Fri, 12 Dec 2014 19:59:53 +0100 Subject: [PATCH 01/16] added OmniAuth gems --- Gemfile | 4 ++++ Gemfile.lock | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/Gemfile b/Gemfile index 4322d230..6e609c5d 100644 --- a/Gemfile +++ b/Gemfile @@ -40,6 +40,10 @@ gem 'sinatra', require: false gem 'questiongenerator', git: 'https://github.com/justask/questiongenerator.git' +# OmniAuth and providers +gem 'omniauth' +gem 'omniauth-twitter' + group :development do gem 'spring' end diff --git a/Gemfile.lock b/Gemfile.lock index c68728f3..62216894 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -91,6 +91,7 @@ GEM rails haml (4.0.6) tilt + hashie (3.3.2) hike (1.2.3) hitimes (1.2.2) http (0.6.3) @@ -129,6 +130,16 @@ GEM nokogiri (1.6.5) mini_portile (~> 0.6.0) nprogress-rails (0.1.6.3) + oauth (0.4.7) + omniauth (1.2.2) + hashie (>= 1.2, < 4) + rack (~> 1.0) + omniauth-oauth (1.0.1) + oauth + omniauth (~> 1.0) + omniauth-twitter (1.1.0) + multi_json (~> 1.3) + omniauth-oauth (~> 1.0) orm_adapter (0.5.0) pg (0.17.1) poltergeist (1.5.1) @@ -302,6 +313,8 @@ DEPENDENCIES jquery-turbolinks mysql2 nprogress-rails + omniauth + omniauth-twitter pg poltergeist questiongenerator! From 6d23ed5dcd29de670a2cdb54f3e205d119bd4067 Mon Sep 17 00:00:00 2001 From: nilsding Date: Fri, 12 Dec 2014 21:42:34 +0100 Subject: [PATCH 02/16] added service model --- app/models/service.rb | 38 +++++++++++++++++++ app/models/services/twitter.rb | 40 ++++++++++++++++++++ app/models/user.rb | 1 + db/migrate/20141212193625_create_services.rb | 14 +++++++ db/schema.rb | 13 ++++++- 5 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 app/models/service.rb create mode 100644 app/models/services/twitter.rb create mode 100644 db/migrate/20141212193625_create_services.rb diff --git a/app/models/service.rb b/app/models/service.rb new file mode 100644 index 00000000..5457d168 --- /dev/null +++ b/app/models/service.rb @@ -0,0 +1,38 @@ +class Service < ActiveRecord::Base + belongs_to :user + validates_uniqueness_of :uid, scope: :type + + class << self + + def first_from_omniauth(auth_hash) + @@auth = auth_hash + where(type: service_type, uid: options[:uid]).first + end + + def initialize_from_omniauth(auth_hash) + @@auth = auth_hash + service_type.constantize.new(options) + end + + private + + def auth + @@auth + end + + def service_type + "Services::#{options[:provider].camelize}" + end + + def options + { + nickname: auth['info']['nickname'], + access_token: auth['credentials']['token'], + access_secret: auth['credentials']['secret'], + uid: auth['uid'], + provider: auth['provider'], + info: auth['info'] + } + end + end +end diff --git a/app/models/services/twitter.rb b/app/models/services/twitter.rb new file mode 100644 index 00000000..bb47815c --- /dev/null +++ b/app/models/services/twitter.rb @@ -0,0 +1,40 @@ +class Services::Twitter < Service + include Rails.application.routes.url_helpers + + def provider + "twitter" + end + + def post(answer) + Rails.logger.debug "posting to Twitter {'answer' => #{answer.id}, 'user' => #{self.user_id}}" + post_tweet answer + end + + private + + def client + @client ||= Twitter::REST::Client.new( + consumer_key: APP_CONFIG['sharing']['twitter']['consumer_key'], + consumer_secret: APP_CONFIG['sharing']['twitter']['consumer_secret'], + access_token: self.access_token, + access_token_secret: self.access_secret + ) + end + + def post_tweet(answer) + client.update prepare_tweet(answer) + end + + def prepare_tweet(answer) + # TODO: improve this. + question_content = answer.question.content + answer_content = answer.content + answer_url = show_user_answer_url( + id: answer.id, + username: answer.user.screen_name + ) + Rails.logger.debug "answer_url => #{answer_url}" + "#{question_content[0..55]}#{'…' if question_content.length > 56}" \ + " — #{answer_content[0..55]}#{'…' if answercontent.length > 56} #{answer_url}" + end +end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index feb6240d..a34029fe 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -20,6 +20,7 @@ class User < ActiveRecord::Base has_many :friends, through: :active_relationships, source: :target has_many :followers, through: :passive_relationships, source: :source has_many :smiles + has_many :services SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/ WEBSITE_REGEX = /https?:\/\/([A-Za-z.\-]+)\/?(?:.*)/i diff --git a/db/migrate/20141212193625_create_services.rb b/db/migrate/20141212193625_create_services.rb new file mode 100644 index 00000000..0ee0db34 --- /dev/null +++ b/db/migrate/20141212193625_create_services.rb @@ -0,0 +1,14 @@ +class CreateServices < ActiveRecord::Migration + def change + create_table :services do |t| + t.string :type, null: false + t.integer :user_id, null: false + t.string :uid + t.string :access_token + t.string :access_secret + t.string :nickname + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 213bfe0a..2933058f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20141208111714) do +ActiveRecord::Schema.define(version: 20141212193625) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -70,6 +70,17 @@ ActiveRecord::Schema.define(version: 20141208111714) do add_index "relationships", ["source_id"], name: "index_relationships_on_source_id", using: :btree add_index "relationships", ["target_id"], name: "index_relationships_on_target_id", using: :btree + create_table "services", force: true do |t| + t.string "type", null: false + t.integer "user_id", null: false + t.string "uid" + t.string "access_token" + t.string "access_secret" + t.string "nickname" + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "smiles", force: true do |t| t.integer "user_id" t.integer "answer_id" From 12180bf1f2c8a952392aa881150b9a868c08e1cd Mon Sep 17 00:00:00 2001 From: nilsding Date: Fri, 12 Dec 2014 21:42:50 +0100 Subject: [PATCH 03/16] added OmniAuth initializer --- config/initializers/omniauth.rb | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 config/initializers/omniauth.rb diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb new file mode 100644 index 00000000..ba7ec8bc --- /dev/null +++ b/config/initializers/omniauth.rb @@ -0,0 +1,5 @@ +Rails.application.config.middleware.use OmniAuth::Builder do + if APP_CONFIG['sharing']['twitter']['enabled'] + provider :twitter, APP_CONFIG['sharing']['twitter']['consumer_key'], APP_CONFIG['sharing']['twitter']['consumer_secret'] + end +end \ No newline at end of file From f24433d84e8689767c0050887b376d89fe031ef0 Mon Sep 17 00:00:00 2001 From: nilsding Date: Fri, 12 Dec 2014 21:43:09 +0100 Subject: [PATCH 04/16] added Services controller --- app/controllers/services_controller.rb | 38 ++++++++++++++++++++++++++ app/helpers/services_helper.rb | 2 ++ app/views/services/index.html.haml | 2 ++ config/routes.rb | 10 +++++++ 4 files changed, 52 insertions(+) create mode 100644 app/controllers/services_controller.rb create mode 100644 app/helpers/services_helper.rb create mode 100644 app/views/services/index.html.haml diff --git a/app/controllers/services_controller.rb b/app/controllers/services_controller.rb new file mode 100644 index 00000000..d81ca389 --- /dev/null +++ b/app/controllers/services_controller.rb @@ -0,0 +1,38 @@ +class ServicesController < ApplicationController + + skip_before_action :verify_authenticity_token, :only => :create + before_action :authenticate_user! + + def index + @services = current_user.services + end + + def create + service = Service.initialize_from_omniauth( omniauth_hash ) + + if current_user.services << service + flash[:success] = 'Successfully added service' + else + flash[:error] = 'Could not add service :(' + end + + if origin + redirect_to origin + else + redirect_to services_path + end + end + + def failure + Rails.logger.info "oauth error: #{params.inspect}" + flash[:error] = 'An error occurred' + redirect_to services_path + end + + def destroy + @service = current_user.services.find(params[:id]) + @service.destroy + flash[:success] = 'Successfully removed service' + redirect_to services_path + end +end diff --git a/app/helpers/services_helper.rb b/app/helpers/services_helper.rb new file mode 100644 index 00000000..de584353 --- /dev/null +++ b/app/helpers/services_helper.rb @@ -0,0 +1,2 @@ +module ServicesHelper +end diff --git a/app/views/services/index.html.haml b/app/views/services/index.html.haml new file mode 100644 index 00000000..98d876a5 --- /dev/null +++ b/app/views/services/index.html.haml @@ -0,0 +1,2 @@ +

Services#index

+

Find me in app/views/services/index.html.erb

diff --git a/config/routes.rb b/config/routes.rb index 81fe8d51..0f0be3d7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -33,6 +33,16 @@ Rails.application.routes.draw do match '/settings/profile', to: 'user#edit', via: 'get', as: :edit_user_profile match '/settings/profile', to: 'user#update', via: 'patch', as: :update_user_profile + # resources :services, only: [:index, :destroy] + match '/settings/services', to: 'services#index', via: 'get', as: :services + match '/settings/services/:id', to: 'services#destroy', via: 'delete', as: :service + controller :services do + scope "/auth", as: "auth" do + get ':provider/callback' => :create + get :failure + end + end + namespace :ajax do match '/ask', to: 'question#create', via: :post, as: :ask match '/answer', to: 'inbox#destroy', via: :post, as: :answer From c91fbe43b109cfc3383143678a1802a3bfd44b0f Mon Sep 17 00:00:00 2001 From: nilsding Date: Fri, 12 Dec 2014 21:54:17 +0100 Subject: [PATCH 05/16] added Services view. --- app/views/services/index.html.haml | 21 +++++++++++++++++++-- app/views/user/_settings_tabs.html.haml | 1 + 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/app/views/services/index.html.haml b/app/views/services/index.html.haml index 98d876a5..66cf366e 100644 --- a/app/views/services/index.html.haml +++ b/app/views/services/index.html.haml @@ -1,2 +1,19 @@ -

Services#index

-

Find me in app/views/services/index.html.erb

+.container.j2-page + = render 'user/settings_tabs' + .col-md-9.col-xs-12.col-sm-9 + .panel.panel-default + .panel-body + - if @services.count > 0 + Sharing is enabled for the following services: + %ul + - @services.each do |service| + %li + %strong= service.provider + (#{service.nickname}) + = link_to 'Disconnect', service_path(service), data: { confirm: "Really disconnect service #{service.provider}?" }, method: :delete + - else + You have not connected any services yet. + + - APP_CONFIG['sharing'].each do |service, service_options| + - if service_options['enabled'] + %p=link_to "Connect to #{service.capitalize}", "/auth/#{service}" \ No newline at end of file diff --git a/app/views/user/_settings_tabs.html.haml b/app/views/user/_settings_tabs.html.haml index 9db9a477..a58d3517 100644 --- a/app/views/user/_settings_tabs.html.haml +++ b/app/views/user/_settings_tabs.html.haml @@ -4,5 +4,6 @@ %ul.nav.nav-pills.nav-stacked = nav_entry "Account", edit_user_registration_path = nav_entry "Profile", edit_user_profile_path + = nav_entry "Sharing", services_path .hidden-xs= render "shared/links" \ No newline at end of file From 23fc3009fe53acb43f4e89c68724feaadfa84f01 Mon Sep 17 00:00:00 2001 From: nilsding Date: Fri, 12 Dec 2014 22:35:23 +0100 Subject: [PATCH 06/16] bugFuchs --- app/controllers/services_controller.rb | 10 ++++++++++ app/models/service.rb | 2 ++ 2 files changed, 12 insertions(+) diff --git a/app/controllers/services_controller.rb b/app/controllers/services_controller.rb index d81ca389..d49a55a5 100644 --- a/app/controllers/services_controller.rb +++ b/app/controllers/services_controller.rb @@ -35,4 +35,14 @@ class ServicesController < ApplicationController flash[:success] = 'Successfully removed service' redirect_to services_path end + + private + + def origin + request.env['omniauth.origin'] + end + + def omniauth_hash + request.env['omniauth.auth'] + end end diff --git a/app/models/service.rb b/app/models/service.rb index 5457d168..5f924544 100644 --- a/app/models/service.rb +++ b/app/models/service.rb @@ -1,4 +1,6 @@ class Service < ActiveRecord::Base + attr_accessor :provider, :info + belongs_to :user validates_uniqueness_of :uid, scope: :type From 9168d908dde9aaf0d5e05a0bdc4d1cec34927218 Mon Sep 17 00:00:00 2001 From: nilsding Date: Fri, 12 Dec 2014 22:37:52 +0100 Subject: [PATCH 07/16] aw yeah --- app/views/services/index.html.haml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/views/services/index.html.haml b/app/views/services/index.html.haml index 66cf366e..74969555 100644 --- a/app/views/services/index.html.haml +++ b/app/views/services/index.html.haml @@ -8,12 +8,12 @@ %ul - @services.each do |service| %li - %strong= service.provider + %strong= service.provider.capitalize (#{service.nickname}) - = link_to 'Disconnect', service_path(service), data: { confirm: "Really disconnect service #{service.provider}?" }, method: :delete + = link_to 'Disconnect', service_path(service), data: { confirm: "Really disconnect service #{service.provider.capitalize}?" }, method: :delete - else You have not connected any services yet. - APP_CONFIG['sharing'].each do |service, service_options| - - if service_options['enabled'] + - if service_options['enabled'] and !@services.any? { |x| x.provider == service.to_s } %p=link_to "Connect to #{service.capitalize}", "/auth/#{service}" \ No newline at end of file From 9c59e802c153ab0357358938e5967deb62f3ba83 Mon Sep 17 00:00:00 2001 From: nilsding Date: Fri, 12 Dec 2014 22:50:54 +0100 Subject: [PATCH 08/16] boost connection pool --- config/database.yml.mysql | 2 +- config/database.yml.postgres | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config/database.yml.mysql b/config/database.yml.mysql index 9a088f9f..0860594f 100644 --- a/config/database.yml.mysql +++ b/config/database.yml.mysql @@ -7,7 +7,7 @@ production: collation: utf8_general_ci reconnect: false database: justask_production - pool: 10 + pool: 25 username: justask password: "hack me" # host: localhost diff --git a/config/database.yml.postgres b/config/database.yml.postgres index 596af027..2126da56 100644 --- a/config/database.yml.postgres +++ b/config/database.yml.postgres @@ -5,7 +5,7 @@ production: adapter: postgresql encoding: unicode database: justask_production - pool: 10 + pool: 25 # username: justask # password: # host: localhost From 7e5ce0c04872d92c41988edd6c23fcd88ca2d31d Mon Sep 17 00:00:00 2001 From: nilsding Date: Fri, 12 Dec 2014 22:59:25 +0100 Subject: [PATCH 09/16] added sidekiq.yml --- config/sidekiq.yml | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 config/sidekiq.yml diff --git a/config/sidekiq.yml b/config/sidekiq.yml new file mode 100644 index 00000000..6762520e --- /dev/null +++ b/config/sidekiq.yml @@ -0,0 +1,9 @@ +--- +:concurrency: 5 +:pidfile: ./tmp/pids/sidekiq.pid +staging: + :concurrency: 10 +production: + :concurrency: 25 +:queues: + - share \ No newline at end of file From e2cabbe9345b013fbfded9e92dd689aa0e25e044 Mon Sep 17 00:00:00 2001 From: nilsding Date: Fri, 12 Dec 2014 22:59:35 +0100 Subject: [PATCH 10/16] added foreman --- Gemfile | 2 ++ Gemfile.lock | 5 +++++ Procfile | 2 ++ 3 files changed, 9 insertions(+) create mode 100644 Procfile diff --git a/Gemfile b/Gemfile index 6e609c5d..3db7a191 100644 --- a/Gemfile +++ b/Gemfile @@ -44,6 +44,8 @@ gem 'questiongenerator', git: 'https://github.com/justask/questiongenerator.git' gem 'omniauth' gem 'omniauth-twitter' +gem 'foreman' + group :development do gem 'spring' end diff --git a/Gemfile.lock b/Gemfile.lock index 62216894..438e7b2f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -72,6 +72,7 @@ GEM warden (~> 1.2.3) diff-lcs (1.2.5) docile (1.1.5) + dotenv (1.0.2) equalizer (0.0.9) erubis (2.7.0) eventmachine (1.0.3) @@ -89,6 +90,9 @@ GEM railties (>= 3.2, < 5.0) font-kit-rails (1.1.0) rails + foreman (0.76.0) + dotenv (~> 1.0.2) + thor (~> 0.19.1) haml (4.0.6) tilt hashie (3.3.2) @@ -306,6 +310,7 @@ DEPENDENCIES faker font-awesome-rails (~> 4.2.0.0) font-kit-rails + foreman haml http_accept_language jbuilder (~> 2.2.4) diff --git a/Procfile b/Procfile new file mode 100644 index 00000000..fa125562 --- /dev/null +++ b/Procfile @@ -0,0 +1,2 @@ +unicorn: bundle exec unicorn -E production -l unix:./tmp/sockets/justask.sock +sidekiq: bundle exec sidekiq -C './config/sidekiq.yml' \ No newline at end of file From 5aab94207aaea745eddc374ebc27a48e8f6511a7 Mon Sep 17 00:00:00 2001 From: nilsding Date: Fri, 12 Dec 2014 23:14:32 +0100 Subject: [PATCH 11/16] answer is now returned in Inbox#answer --- app/models/inbox.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/models/inbox.rb b/app/models/inbox.rb index 5cafaf83..5084084a 100644 --- a/app/models/inbox.rb +++ b/app/models/inbox.rb @@ -3,12 +3,13 @@ class Inbox < ActiveRecord::Base belongs_to :question def answer(answer, user) - Answer.create!(content: answer, - user: user, - question: self.question) + answer = Answer.create!(content: answer, + user: user, + question: self.question) user.increment! :answered_count self.question.increment! :answer_count self.destroy + answer end def remove From 76c614ea66f2b4192527de2ca54c668eeae153c5 Mon Sep 17 00:00:00 2001 From: nilsding Date: Fri, 12 Dec 2014 23:45:49 +0100 Subject: [PATCH 12/16] alright --- app/controllers/ajax/inbox_controller.rb | 8 +++++++- app/workers/share_worker.rb | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 app/workers/share_worker.rb diff --git a/app/controllers/ajax/inbox_controller.rb b/app/controllers/ajax/inbox_controller.rb index 2f30106f..a8c33547 100644 --- a/app/controllers/ajax/inbox_controller.rb +++ b/app/controllers/ajax/inbox_controller.rb @@ -34,8 +34,10 @@ class Ajax::InboxController < ApplicationController return end + a = nil + begin - inbox.answer params[:answer], current_user + a = inbox.answer params[:answer], current_user rescue @status = :err @message = "An error occurred" @@ -43,6 +45,10 @@ class Ajax::InboxController < ApplicationController return end + current_user.services.each do |service| + service.post a + end + @status = :okay @message = "Successfully answered question." @success = true diff --git a/app/workers/share_worker.rb b/app/workers/share_worker.rb new file mode 100644 index 00000000..079baaee --- /dev/null +++ b/app/workers/share_worker.rb @@ -0,0 +1,9 @@ +class ShareWorker + include Sidekiq::Worker + + sidekiq_options queue: :share + + def perform(answer_id) + # fuck this… for now + end +end \ No newline at end of file From b25c419e527454a8881ad0ad7e8be00bb342ec0f Mon Sep 17 00:00:00 2001 From: nilsding Date: Fri, 12 Dec 2014 23:53:23 +0100 Subject: [PATCH 13/16] aw yeah --- app/controllers/ajax/inbox_controller.rb | 10 ++++++---- app/models/services/twitter.rb | 6 ++++-- config/justask.yml.example | 3 +++ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/app/controllers/ajax/inbox_controller.rb b/app/controllers/ajax/inbox_controller.rb index a8c33547..90bb36ff 100644 --- a/app/controllers/ajax/inbox_controller.rb +++ b/app/controllers/ajax/inbox_controller.rb @@ -34,10 +34,10 @@ class Ajax::InboxController < ApplicationController return end - a = nil + answer = nil begin - a = inbox.answer params[:answer], current_user + answer = inbox.answer params[:answer], current_user rescue @status = :err @message = "An error occurred" @@ -45,8 +45,10 @@ class Ajax::InboxController < ApplicationController return end - current_user.services.each do |service| - service.post a + Thread.new do + current_user.services.each do |service| + service.post answer + end end @status = :okay diff --git a/app/models/services/twitter.rb b/app/models/services/twitter.rb index bb47815c..69cb0d66 100644 --- a/app/models/services/twitter.rb +++ b/app/models/services/twitter.rb @@ -31,10 +31,12 @@ class Services::Twitter < Service answer_content = answer.content answer_url = show_user_answer_url( id: answer.id, - username: answer.user.screen_name + username: answer.user.screen_name, + host: APP_CONFIG['hostname'], + protocol: (APP_CONFIG['https'] ? :https : :http) ) Rails.logger.debug "answer_url => #{answer_url}" "#{question_content[0..55]}#{'…' if question_content.length > 56}" \ - " — #{answer_content[0..55]}#{'…' if answercontent.length > 56} #{answer_url}" + " — #{answer_content[0..55]}#{'…' if answer_content.length > 56} #{answer_url}" end end \ No newline at end of file diff --git a/config/justask.yml.example b/config/justask.yml.example index 7565421c..e596e92e 100644 --- a/config/justask.yml.example +++ b/config/justask.yml.example @@ -1,6 +1,9 @@ # The site name, shown everywhere. site_name: "justask" +hostname: "justask.rrerr.net" +https: true + # Name of the "Anonymous" user. (e.g. "Anonymous Coward", "Arno Nym", "Mr. X", ...) anonymous_name: "Anonymous" From 6e154bc462ccec73f34ff2037e8cb20308512943 Mon Sep 17 00:00:00 2001 From: pixeldesu Date: Sat, 13 Dec 2014 00:01:54 +0100 Subject: [PATCH 14/16] added checkbox for Twitter sharing --- app/views/inbox/_entry.html.haml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/views/inbox/_entry.html.haml b/app/views/inbox/_entry.html.haml index a8ea88ba..4817f59e 100644 --- a/app/views/inbox/_entry.html.haml +++ b/app/views/inbox/_entry.html.haml @@ -22,4 +22,7 @@ %button.btn.btn-success{name: 'ib-answer', data: { ib_id: i.id }} Answer %button.btn.btn-danger{name: 'ib-destroy', data: { ib_id: i.id }} - Delete \ No newline at end of file + Delete + %label + %input{type: 'checkbox', name: 'ib-share-twitter'} + Post to Twitter \ No newline at end of file From 013d1d44b7e5ba19fcea928b030d61d68d97afa7 Mon Sep 17 00:00:00 2001 From: nilsding Date: Sat, 13 Dec 2014 15:17:50 +0100 Subject: [PATCH 15/16] the checkbox should now share to services --- app/assets/javascripts/inbox.coffee | 8 +++++++- app/controllers/ajax/inbox_controller.rb | 5 ++++- app/views/inbox/_entry.html.haml | 8 +++++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/inbox.coffee b/app/assets/javascripts/inbox.coffee index c6d13d4b..87bd87e9 100644 --- a/app/assets/javascripts/inbox.coffee +++ b/app/assets/javascripts/inbox.coffee @@ -25,12 +25,18 @@ $(document).on "click", "button[name=ib-answer]", -> btn.button "loading" iid = btn[0].dataset.ibId $("textarea[name=ib-answer][data-id=#{iid}]").attr "readonly", "readonly" + + shareTo = [] + ($ "input[type=checkbox][name=ib-share][data-ib-id=#{iid}]:checked").each (i, share) -> + shareTo.push share.dataset.service + $.ajax - url: '/ajax/answer' # TODO: find a way to use rake routes instead of hardcoding them here + url: '/ajax/answer' type: 'POST' data: id: iid answer: $("textarea[name=ib-answer][data-id=#{iid}]").val() + share: JSON.stringify shareTo success: (data, status, jqxhr) -> if data.success $("div.inbox-box[data-id=#{iid}]").slideUp() diff --git a/app/controllers/ajax/inbox_controller.rb b/app/controllers/ajax/inbox_controller.rb index 90bb36ff..5cf8132d 100644 --- a/app/controllers/ajax/inbox_controller.rb +++ b/app/controllers/ajax/inbox_controller.rb @@ -24,6 +24,7 @@ class Ajax::InboxController < ApplicationController def destroy params.require :id params.require :answer + params.require :share inbox = Inbox.find(params[:id]) @@ -45,9 +46,11 @@ class Ajax::InboxController < ApplicationController return end + # sharing + share_to = JSON.parse params[:share] Thread.new do current_user.services.each do |service| - service.post answer + service.post answer if share_to.include? service.provider end end diff --git a/app/views/inbox/_entry.html.haml b/app/views/inbox/_entry.html.haml index 4817f59e..2aa9f6cc 100644 --- a/app/views/inbox/_entry.html.haml +++ b/app/views/inbox/_entry.html.haml @@ -23,6 +23,8 @@ Answer %button.btn.btn-danger{name: 'ib-destroy', data: { ib_id: i.id }} Delete - %label - %input{type: 'checkbox', name: 'ib-share-twitter'} - Post to Twitter \ No newline at end of file + - current_user.services.each do |service| + %label + %input{type: 'checkbox', name: 'ib-share', data: { ib_id: i.id, service: service.provider }} + Post to + = service.provider.capitalize \ No newline at end of file From 633e1337131872cfe936a9679ccc16b0aa74dae0 Mon Sep 17 00:00:00 2001 From: nilsding Date: Sat, 13 Dec 2014 15:38:16 +0100 Subject: [PATCH 16/16] sharing now works for real. --- app/controllers/ajax/inbox_controller.rb | 4 ++-- app/models/services/twitter.rb | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/controllers/ajax/inbox_controller.rb b/app/controllers/ajax/inbox_controller.rb index 5cf8132d..0a1eb923 100644 --- a/app/controllers/ajax/inbox_controller.rb +++ b/app/controllers/ajax/inbox_controller.rb @@ -47,10 +47,10 @@ class Ajax::InboxController < ApplicationController end # sharing - share_to = JSON.parse params[:share] Thread.new do + share_to = JSON.parse params[:share] current_user.services.each do |service| - service.post answer if share_to.include? service.provider + service.post(answer) if share_to.include? service.provider end end diff --git a/app/models/services/twitter.rb b/app/models/services/twitter.rb index 69cb0d66..04ea97ce 100644 --- a/app/models/services/twitter.rb +++ b/app/models/services/twitter.rb @@ -35,7 +35,6 @@ class Services::Twitter < Service host: APP_CONFIG['hostname'], protocol: (APP_CONFIG['https'] ? :https : :http) ) - Rails.logger.debug "answer_url => #{answer_url}" "#{question_content[0..55]}#{'…' if question_content.length > 56}" \ " — #{answer_content[0..55]}#{'…' if answer_content.length > 56} #{answer_url}" end