Commit 8798f05f authored by Raphael's avatar Raphael

Revert "Revert redis merge to fix bugs on master"

This reverts commit 30698d67.
parent 3688b398
......@@ -48,10 +48,14 @@ gem 'mini_magick'
gem 'aws'
gem 'fastercsv', :require => false
gem 'jammit'
gem 'rest-client'
#Backups
gem "cloudfiles", :require => false
#Queue
gem 'resque'
gem 'SystemTimer'
group :test, :development do
gem 'factory_girl_rails'
gem 'ruby-debug19' if RUBY_VERSION.include? "1.9"
......
......@@ -77,6 +77,7 @@ PATH
GEM
remote: http://rubygems.org/
specs:
SystemTimer (1.2.1)
abstract (1.0.0)
actionmailer (3.0.1)
actionpack (= 3.0.1)
......@@ -296,6 +297,14 @@ GEM
rake (>= 0.8.4)
thor (~> 0.14.0)
rake (0.8.7)
redis (2.1.1)
redis-namespace (0.8.0)
redis (< 3.0.0)
resque (1.10.0)
json (~> 1.4.6)
redis-namespace (~> 0.8.0)
sinatra (>= 0.9.2)
vegas (~> 0.1.2)
rest-client (1.6.1)
mime-types (>= 1.16)
rspec (2.2.0)
......@@ -330,6 +339,9 @@ GEM
ffi (~> 0.6.3)
json_pure
rubyzip
sinatra (1.1.0)
rack (~> 1.1)
tilt (~> 1.1)
subexec (0.0.4)
systemu (1.2.0)
term-ansicolor (1.0.5)
......@@ -338,6 +350,7 @@ GEM
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
thor (0.14.6)
tilt (1.1)
treetop (1.4.9)
polyglot (>= 0.3.1)
twitter (0.9.12)
......@@ -347,6 +360,8 @@ GEM
oauth (~> 0.4.3)
tzinfo (0.3.23)
uuidtools (2.1.1)
vegas (0.1.8)
rack (>= 1.0.0)
warden (0.10.7)
rack (>= 1.0.0)
webmock (1.6.1)
......@@ -360,6 +375,7 @@ PLATFORMS
ruby
DEPENDENCIES
SystemTimer
addressable
aws
bson (= 1.1)
......@@ -393,6 +409,8 @@ DEPENDENCIES
omniauth
pubsubhubbub
rails (= 3.0.1)
resque
rest-client
roxml!
rspec (>= 2.0.0)
rspec-instafail
......
......@@ -7,5 +7,6 @@
require File.expand_path('../config/application', __FILE__)
require 'rake'
require 'resque/tasks'
Diaspora::Application.load_tasks
......@@ -116,14 +116,6 @@ class PeopleController < ApplicationController
end
end
def webfinger(account, opts = {})
finger = EMWebfinger.new(account)
finger.on_person do |response|
if response.class == Person
response.socket_to_uid(current_user.id, opts)
else
require File.join(Rails.root,'lib/diaspora/websocket')
Diaspora::WebSocket.queue_to_user(current_user.id, {:class => 'people', :status => 'fail', :query => account, :response => response}.to_json)
end
end
Resque.enqueue(Jobs::SocketWebfinger, current_user.id, account, opts)
end
end
......@@ -56,12 +56,7 @@ class PublicsController < ApplicationController
end
@user = person.owner
begin
@user.receive_salmon(params[:xml])
rescue Exception => e
Rails.logger.info("bad salmon: #{e.message}")
end
Resque.enqueue(Jobs::ReceiveSalmon, @user.id, params[:xml])
render :nothing => true, :status => 200
end
......
......@@ -2,7 +2,7 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require File.join(Rails.root, 'lib/em-webfinger')
require File.join(Rails.root, 'lib/webfinger')
class RequestsController < ApplicationController
before_filter :authenticate_user!
......
......@@ -11,7 +11,7 @@ class HandleValidator < ActiveModel::Validator
end
class Comment
require File.join(Rails.root, 'lib/diaspora/websocket')
require File.join(Rails.root, 'lib/diaspora/web_socket')
require File.join(Rails.root, 'lib/youtube_titles')
include YoutubeTitles
include MongoMapper::Document
......
module Jobs
class ReceiveSalmon
@queue = :receive
def self.perform(user_id, xml)
user = User.find(user_id)
user.receive_salmon(xml)
end
end
end
module Jobs
class SocketWebfinger
@queue = :receive
def self.perform(user_id, account, opts={})
finger = Webfinger.new(account)
begin
result = finger.fetch
result.socket_to_uid(user_id, opts)
rescue
Diaspora::WebSocket.queue_to_user(user_id, {:class => 'people', :status => 'fail', :query => account, :response => I18n.t('people.webfinger.fail')}.to_json)
end
end
end
end
......@@ -8,7 +8,7 @@ class Person
include MongoMapper::Document
include ROXML
include Encryptor::Public
require File.join(Rails.root, 'lib/diaspora/websocket')
require File.join(Rails.root, 'lib/diaspora/web_socket')
include Diaspora::Socketable
xml_accessor :_id
......
......@@ -4,7 +4,7 @@
class Post
require File.join(Rails.root, 'lib/encryptable')
require File.join(Rails.root, 'lib/diaspora/websocket')
require File.join(Rails.root, 'lib/diaspora/web_socket')
include MongoMapper::Document
include ApplicationHelper
include ROXML
......
......@@ -30,7 +30,7 @@ class Retraction
if self.type.constantize.find_by_id(post_id)
unless Post.first(:diaspora_handle => person.diaspora_handle, :id => post_id)
Rails.logger.info("event=retraction status=abort reason='no post found authored by retractor' sender=#{person.diaspora_handle} post_id=#{post_id}")
raise "#{person.inspect} is trying to retract a post that either doesn't exist or is not by them"
return
end
begin
......
This diff is collapsed.
......@@ -18,3 +18,4 @@ include_recipe "centos::image_magick"
include_recipe "centos::mongo_db"
include_recipe "common::main"
include_recipe "centos::nginx"
include_recipe "centos::redis"
execute "refresh yum" do
command "yum update -y"
end
execute "install redis" do
command "yum install -y redis"
end
cookbook_file "/usr/local/etc/redis.conf" do
source "redis.conf"
end
execute "change redis.conf permissions" do
command "chmod 755 /usr/local/etc/redis.conf"
end
execute "make the redis db directory" do
command "mkdir -p /usr/local/var/db/redis"
end
......@@ -15,6 +15,8 @@
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
#HTTPS
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT
#Resque-Web
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 7894 -j ACCEPT
#Websocket
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
#Crossdomain policy file for Flash sockets
......
......@@ -49,9 +49,33 @@ execute "executable" do
command "chmod -R 755 /service/magent"
end
execute "redis run" do
command "mkdir -p /service/redis && echo '#!/bin/sh' > /service/redis/run && echo 'cd /usr/sbin/ && exec /usr/sbin/redis-server /usr/local/etc/redis.conf' >> /service/redis/run"
end
execute "executable" do
command "chmod -R 755 /service/redis"
end
execute "nginx run" do
command "mkdir -p /service/nginx && echo '#!/bin/sh' > /service/nginx/run && echo 'exec /usr/local/nginx/sbin/nginx' >> /service/nginx/run"
end
execute "executable" do
command "chmod -R 755 /service/nginx"
end
execute "resque worker run" do
command "mkdir -p /service/resque_worker && echo '#!/bin/sh' > /service/resque_worker/run && echo 'cd /user/local/app/diaspora && RAILS_ENV=production QUEUE=* HOME=/usr/local/app/diaspora exec /usr/local/bin/rake resque:work' >> /service/resque_worker/run"
end
execute "executable" do
command "chmod -R 755 /service/resque_worker"
end
execute "resque web run" do
command "mkdir -p /service/resque_web && echo '#!/bin/sh' > /service/resque_web/run && echo 'RAILS_ENV=production HOME=/usr/local/app/diaspora exec resque-web -F' >> /service/resque_web/run"
end
execute "executable" do
command "chmod -R 755 /service/resque_web"
end
......@@ -36,6 +36,10 @@ http {
<% end %>
}
upstream resque_web {
server localhost:5678;
}
server {
listen 843;
......@@ -51,6 +55,23 @@ http {
}
server {
listen 7894;
server_name <%= @url %> www.<%= @url %>;
auth_basic "Restricted";
auth_basic_user_file htpasswd;
ssl on;
ssl_certificate /usr/local/nginx/conf/diaspora.crt;
ssl_certificate_key /usr/local/nginx/conf/diaspora.key;
location / {
proxy_set_header Host $http_host;
proxy_pass http://resque_web;
}
}
server {
listen 80;
server_name <%= @url %> www.<%= @url %>;
......@@ -67,6 +88,7 @@ http {
ssl_certificate <%= @cert_location %>;
ssl_certificate_key <%= @key_location %>;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
......@@ -85,13 +107,13 @@ http {
if (!-f $request_filename) {
proxy_pass http://thin_cluster;
break;
}
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
......
Dir[File.join(Rails.root, 'app', 'models', 'jobs', '*.rb')].each { |file| require file }
#config = YAML::load(File.open("#{Rails.root}/config/redis.yml"))
#Resque.redis = Redis.new(:host => config['host'], :port => config['port'])
require 'resque'
......@@ -6,7 +6,6 @@
# See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
en:
settings: "Settings"
profile: "Profile"
account: "Account"
......@@ -302,6 +301,8 @@ en:
add_contact: "add contact"
index:
results_for: "search results for"
webfinger:
fail: "Sorry, we couldn't find %{handle}."
show:
no_posts: "no posts to display!"
incoming_request: "You have an incoming request from this person."
......
---
default:
host: localhost
port: 6379
development:
test:
staging:
production:
require File.join(Rails.root, 'lib/em-webfinger')
require File.join(Rails.root, 'lib/webfinger')
module Diaspora
module UserModules
module Receiving
def receive_salmon salmon_xml
salmon = Salmon::SalmonSlap.parse salmon_xml, self
webfinger = EMWebfinger.new(salmon.author_email)
webfinger.on_person { |response|
if response.is_a? Person
salmon_author = response
if salmon.verified_for_key?(salmon_author.public_key)
self.receive(salmon.parsed_data, salmon_author)
end
else
Rails.logger.info("event=receive status=abort recipient=#{self.diaspora_handle} sender=#{salmon.author_email} reason='#{response}'")
end
}
webfinger = Webfinger.new(salmon.author_email)
begin
salmon_author = webfinger.fetch
rescue Exception => e
Rails.logger.info("event=receive status=abort recipient=#{self.diaspora_handle} sender=#{salmon.author_email} reason='#{e.message}'")
end
if salmon.verified_for_key?(salmon_author.public_key)
self.receive(salmon.parsed_data, salmon_author)
else
Rails.logger.info("event=receive status=abort recipient=#{self.diaspora_handle} sender=#{salmon.author_email} reason='not_verified for key'")
end
end
def receive xml, salmon_author
object = Diaspora::Parser.from_xml(xml)
Rails.logger.info("event=receive status=start recipient=#{self.diaspora_handle} payload_type=#{object.class} sender=#{salmon_author.diaspora_handle}")
if object.is_a?(Request)
salmon_author.save
object.sender_handle = salmon_author.diaspora_handle
......@@ -39,19 +39,25 @@ module Diaspora
return
end
e = EMWebfinger.new(object.diaspora_handle)
e = Webfinger.new(object.diaspora_handle)
e.on_person do |person|
if person.class == Person
object.person = person if object.respond_to? :person=
unless object.is_a?(Request) || self.contact_for(salmon_author)
Rails.logger.info("event=receive status=abort reason='sender not connected to recipient' recipient=#{self.diaspora_handle} sender=#{salmon_author.diaspora_handle} payload_type=#{object.class}")
return
else
receive_object(object,person)
Rails.logger.info("event=receive status=complete recipient=#{self.diaspora_handle} sender=#{salmon_author.diaspora_handle} payload_type#{object.class}")
return object
end
begin
person = e.fetch
rescue Exception => e
Rails.logger.info("event=receive status=abort reason='#{e.message}' payload_type=#{object.class} recipient=#{self.diaspora_handle} sender=#{salmon_author.diaspora_handle}")
return
end
if person
object.person = person if object.respond_to? :person=
unless object.is_a?(Request) || self.contact_for(salmon_author)
Rails.logger.info("event=receive status=abort reason='sender not connected to recipient' recipient=#{self.diaspora_handle} sender=#{salmon_author.diaspora_handle} payload_type=#{object.class}")
return
else
receive_object(object,person)
Rails.logger.info("event=receive status=complete recipient=#{self.diaspora_handle} sender=#{salmon_author.diaspora_handle} payload_type#{object.class}")
return object
end
end
end
......@@ -102,7 +108,7 @@ module Diaspora
def receive_comment comment
commenter = comment.person
unless comment.post.person == self.person || comment.verify_post_creator_signature
Rails.logger.info("event=receive status=abort reason='comment signature not valid' recipient=#{self.diaspora_handle} sender=#{comment.post.person.diaspora_handle} payload_type=#{comment.class} post_id=#{comment.post_id}")
return
......@@ -136,10 +142,10 @@ module Diaspora
def receive_post post
#exsists locally, but you dont know about it
#does not exsist locally, and you dont know about it
#exsists_locally?
#you know about it, and it is mutable
#you know about it, and it is not mutable
#you know about it, and it is mutable
#you know about it, and it is not mutable
#
on_pod = exsists_on_pod?(post)
if on_pod && on_pod.diaspora_handle == post.diaspora_handle
......
require 'resque/tasks'
task "resque:setup" => :environment
require File.join(Rails.root, 'lib/hcard')
require File.join(Rails.root, 'lib/webfinger_profile')
class EMWebfinger
class Webfinger
class WebfingerFailedError < RuntimeError; end
TIMEOUT = 5
REDIRECTS = 3
OPTS = {:timeout => TIMEOUT, :redirects => REDIRECTS}
def initialize(account)
@account = account.strip.gsub('acct:','').to_s
@callbacks = []
@ssl = true
Rails.logger.info("event=EMWebfinger status=initialized target=#{account}")
# Raise an error if identifier has a port number
#raise "Identifier is invalid" if(@account.strip.match(/\:\d+$/))
# Raise an error if identifier is not a valid email (generous regexp)
#raise "Identifier is invalid" if !(@account=~ /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/)
Rails.logger.info("event=webfinger status=initialized target=#{account}")
end
def fetch
if @callbacks.empty?
Rails.logger.info("event=EMWebfinger status=abort target=#{@account} callbacks=empty")
raise 'you need to set a callback before calling fetch'
end
person = Person.by_account_identifier(@account)
if person
Rails.logger.info("event=EMWebfinger status=local target=#{@account}")
process_callbacks person
else
Rails.logger.info("event=EMWebfinger status=remote target=#{@account}")
get_xrd
Rails.logger.info("event=webfinger status=success route=local target=#{@account}")
return person
end
end
def on_person(&block)
@callbacks << block
self.fetch
profile_url = get_xrd
webfinger_profile = get_webfinger_profile(profile_url)
fingered_person = make_person_from_webfinger(webfinger_profile)
if fingered_person
Rails.logger.info("event=webfinger status=success route=remote target=#{@account}")
fingered_person
else
Rails.logger.info("event=webfinger status=failure route=remote target=#{@account}")
raise WebfingerFailedError.new(@account)
end
end
private
def get_xrd
http = EventMachine::HttpRequest.new(xrd_url).get OPTS
http.callback {
profile_url = webfinger_profile_url(http.response)
if profile_url
get_webfinger_profile(profile_url)
elsif @ssl
@ssl = false
get_xrd
begin
http = RestClient.get xrd_url, OPTS
profile_url = webfinger_profile_url(http.body)
if profile_url
return profile_url
else
process_callbacks I18n.t('webfinger.not_enabled', :account => @account)
raise "no profile URL"
end
}
http.errback {
rescue Exception => e
if @ssl
@ssl = false
get_xrd
retry
else
process_callbacks I18n.t('webfinger.xrd_fetch_failed', :account => @account)
end }
raise e
raise I18n.t('webfinger.xrd_fetch_failed', :account => @account)
end
end
end
def get_webfinger_profile(profile_url)
http = EventMachine::HttpRequest.new(profile_url).get OPTS
http.callback{ make_person_from_webfinger(http.response) }
http.errback{ process_callbacks I18n.t('webfinger.fetch_failed', :profile_url => profile_url) }
begin
http = RestClient.get(profile_url, OPTS)
rescue
raise I18n.t('webfinger.fetch_failed', :profile_url => profile_url)
end
return http.body
end
def make_person_from_webfinger(webfinger_profile)
unless webfinger_profile.strip == ""
begin
wf_profile = WebfingerProfile.new(@account, webfinger_profile)
http = EventMachine::HttpRequest.new(wf_profile.hcard).get OPTS
http.callback{
begin
hcard = HCard.build http.response
p = Person.build_from_webfinger(wf_profile, hcard)
process_callbacks(p)
rescue
process_callbacks I18n.t 'webfinger.no_person_constructed'
end
}
http.errback{
process_callbacks I18n.t('webfinger.hcard_fetch_failed', :account => @account) }
rescue
process_callbacks "No person could be constructed from this webfinger profile."
end
end
end
wf_profile = WebfingerProfile.new(@account, webfinger_profile)
def process_callbacks(person)
Rails.logger.info("event=EMWebfinger status=callbacks_started target=#{@account} response='#{person.is_a?(String) ? person : person.id}'")
@callbacks.each { |c|
begin
c.call(person)
rescue Exception => e
Rails.logger.info("event=EMWebfinger status=error_on_callback error='#{e.inspect}'")
hcard = RestClient.get(wf_profile.hcard, OPTS)
rescue
return I18n.t('webfinger.hcard_fetch_failed', :account => @account)
end
}
Rails.logger.info("event=EMWebfinger status=complete target=#{@account}")
card = HCard.build hcard.body
p = Person.build_from_webfinger(wf_profile, card)
end
end
......@@ -120,7 +95,7 @@ class EMWebfinger
domain = @account.split('@')[1]
"http#{'s' if @ssl}://#{domain}/.well-known/host-meta"
end
def swizzle(template)
template.gsub '{uri}', @account
end
......
......@@ -3,7 +3,7 @@
# the COPYRIGHT file.
require File.dirname(__FILE__) + '/../config/environment'
require File.dirname(__FILE__) + '/../lib/diaspora/websocket'
require File.dirname(__FILE__) + '/../lib/diaspora/web_socket'
at_exit do
begin
......
......@@ -94,6 +94,13 @@ describe PeopleController do
end
end
describe '#webfinger' do
it 'enqueues a webfinger job' do
Resque.should_receive(:enqueue).with(Jobs::SocketWebfinger, user.id, user.diaspora_handle, anything).once
get :retrieve_remote, :diaspora_handle => user.diaspora_handle
end
end
describe '#update' do
context 'with a profile photo set' do
before do
......
......@@ -7,42 +7,20 @@ require 'spec_helper'
describe PublicsController do
render_views
let!(:user) { make_user }
let!(:user2) { make_user }
let!(:aspect1) { user.aspects.create(:name => "foo") }
let!(:aspect2) { user2.aspects.create(:name => "far") }
let!(:aspect2) { user2.aspects.create(:name => 'disciples') }
let!(:req) { user2.send_contact_request_to(user.person, aspect2) }
let!(:xml) { user2.salmon(req).xml_for(user.person) }
let(:user) { make_user }
let(:person){Factory(:person)}
before do
sign_in :user, user
end
describe '#receive' do
before do
EventMachine::HttpRequest.stub!(:new).and_return(FakeHttpRequest.new(:success))
end
context 'success cases' do
before do
@person_mock = mock()
@user_mock = mock()
@user_mock.stub!(:receive_salmon).and_return(true)
@person_mock.stub!(:owner_id).and_return(true)
@person_mock.stub!(:owner).and_return(@user_mock)
Person.stub!(:first).and_return(@person_mock)
end
let(:xml) { "<walruses></walruses>" }
context 'success cases' do
it 'should 200 on successful receipt of a request' do
post :receive, :id =>user.person.id, :xml => xml
response.code.should == '200'
end
it 'should have the xml processed as salmon on success' do
@user_mock.should_receive(:receive_salmon).and_return(true)
post :receive, :id => user.person.id, :xml => xml
it 'enqueues a receive job' do
Resque.should_receive(:enqueue).with(Jobs::ReceiveSalmon, user.id, xml).once
post :receive, :id =>user.person.id, :xml => xml
end
end
......@@ -55,8 +33,8 @@ describe PublicsController do
post :receive, :id => person.id, :xml => xml
response.code.should == '404'
end
end
end
describe '#hcard' do
it 'queries by person id' do
......@@ -97,33 +75,4 @@ describe PublicsController do
response.should be_not_found
end
end
context 'intergration tests that should not be in this file' do