Commit 3fc3b249 authored by Jonne Haß's avatar Jonne Haß

End the require mess

* Rename and reorganize post fetcher to fix autoloading, also let it use
  Faradays default connection so we get nice redirects
* Add initializer to load libs at a central place
* added lib dir to autoload_once paths to increase thread safety
* Moved lib/exceptions.rb to lib/diaspora/ to conform namespacing
parent 13ee6129
require Rails.root.join('lib', 'statistics')
class AdminsController < ApplicationController
before_filter :authenticate_user!
before_filter :redirect_unless_admin
......
......@@ -2,8 +2,6 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require Rails.root.join('lib', 'email_inviter')
class InvitationsController < ApplicationController
before_filter :authenticate_user!, :only => [:new, :create]
......
......@@ -2,8 +2,6 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require Rails.root.join("app", "presenters", "post_presenter")
class LikesController < ApplicationController
include ApplicationHelper
before_filter :authenticate_user!
......
......@@ -2,8 +2,6 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require Rails.root.join("lib", 'stream', "person")
class PeopleController < ApplicationController
before_filter :authenticate_user!, :except => [:show, :last_post]
before_filter :redirect_if_tag_search, :only => [:index]
......
......@@ -2,8 +2,6 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require Rails.root.join("app", "presenters", "post_presenter")
class PostsController < ApplicationController
include PostsHelper
......
......@@ -2,11 +2,7 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require Rails.root.join('lib', 'stream', 'public')
class PublicsController < ApplicationController
require Rails.root.join('lib', 'diaspora', 'parser')
require Rails.root.join('lib', 'postzord', 'receiver', 'public')
require Rails.root.join('lib', 'postzord', 'receiver', 'private')
include Diaspora::Parser
skip_before_filter :set_header_data
......
......@@ -2,15 +2,6 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require Rails.root.join("lib", "stream", "aspect")
require Rails.root.join("lib", "stream", "multi")
require Rails.root.join("lib", "stream", "comments")
require Rails.root.join("lib", "stream", "likes")
require Rails.root.join("lib", "stream", "mention")
require Rails.root.join("lib", "stream", "followed_tag")
require Rails.root.join("lib", "stream", "activity")
class StreamsController < ApplicationController
before_filter :authenticate_user!
before_filter :save_selected_aspects, :only => :aspects
......
# Copyright (c) 2010-2011, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require Rails.root.join('app', 'models', 'acts_as_taggable_on', 'tag')
require Rails.root.join('lib', 'stream', 'tag')
class TagsController < ApplicationController
skip_before_filter :set_grammatical_gender
......
......@@ -3,9 +3,6 @@
# the COPYRIGHT file.
class UsersController < ApplicationController
require Rails.root.join('lib', 'diaspora', 'exporter')
require Rails.root.join('lib', 'collect_user_photos')
before_filter :authenticate_user!, :except => [:new, :create, :public, :user_photo]
respond_to :html
......
......@@ -2,9 +2,6 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require Rails.root.join('lib', 'diaspora', 'markdownify')
require 'redcarpet/render_strip'
module MarkdownifyHelper
def markdownify(target, render_options={})
......
......@@ -17,7 +17,7 @@ module Jobs
people = Person.where(:id => person_ids)
dispatcher = dispatcher_class_as_string.constantize
hydra = HydraWrapper.new(user, people, encoded_object_xml, dispatcher)
hydra = ::HydraWrapper.new(user, people, encoded_object_xml, dispatcher)
hydra.enqueue_batch
hydra.run
......
......@@ -2,9 +2,6 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require 'uri'
require Rails.root.join('lib', 'hcard')
class Person < ActiveRecord::Base
include ROXML
include Encryptor::Public
......
......@@ -3,8 +3,6 @@
# the COPYRIGHT file.
class Photo < ActiveRecord::Base
require 'carrierwave/orm/activerecord'
include Diaspora::Federated::Shareable
include Diaspora::Commentable
include Diaspora::Shareable
......
......@@ -4,7 +4,6 @@
class Service < ActiveRecord::Base
include ActionView::Helpers::TextHelper
require Rails.root.join('app', 'helpers', 'markdownify_helper')
include MarkdownifyHelper
belongs_to :user
......@@ -39,5 +38,5 @@ class Service < ActiveRecord::Base
end
end
require Rails.root.join('app', 'models', 'services', 'facebook')
require Rails.root.join('app', 'models', 'services', 'twitter')
require 'services/facebook'
require 'services/twitter'
require 'uri'
class Services::Facebook < Service
include Rails.application.routes.url_helpers
require Rails.root.join('app', 'helpers', 'markdownify_helper')
include MarkdownifyHelper
OVERRIDE_FIELDS_ON_FB_UPDATE = [:contact_id, :person_id, :request_id, :invitation_id, :photo_url, :name, :username]
......
require 'uri'
class Services::Twitter < Service
MAX_CHARACTERS = 140
SHORTENED_URL_LENGTH = 21
......
......@@ -159,7 +159,6 @@ class StatusMessage < Post
end
def contains_oembed_url_in_text?
require 'uri'
urls = URI.extract(self.raw_message, ['http', 'https'])
self.oembed_url = urls.find{ |url| !TRUSTED_OEMBED_PROVIDERS.find(url).nil? }
end
......
......@@ -2,9 +2,6 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require Rails.root.join('lib', 'salmon', 'salmon')
require Rails.root.join('lib', 'postzord', 'dispatcher')
class User < ActiveRecord::Base
include Encryptor::Private
include Connecting
......
......@@ -2,9 +2,6 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require Rails.root.join('lib', 'evil_query')
#TODO: THIS FILE SHOULD NOT EXIST, EVIL SQL SHOULD BE ENCAPSULATED IN EvilQueries,
#throwing all of this stuff in user violates demeter like WHOA
module User::Querying
......
require 'uri'
class OEmbedPresenter
include PostsHelper
include ActionView::Helpers::TextHelper
......
require Rails.root.join('lib', 'template_picker')
class PostPresenter
attr_accessor :post, :current_user
......
......@@ -32,8 +32,8 @@ module Diaspora
# -- all .rb files in that directory are automatically loaded.
# Add additional load paths for your own custom dirs
config.autoload_paths += %W(#{config.root}/app/presenters)
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += %W{#{config.root}/app/presenters}
config.autoload_once_paths += %W{#{config.root}/lib}
# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named
......
......@@ -13,7 +13,6 @@ end
# Load the rails application
require Pathname.new(__FILE__).dirname.expand_path.join('application')
require Rails.root.join("lib", "exceptions")
# Load configuration system early
require Rails.root.join('config', 'load_config')
......@@ -27,7 +26,6 @@ USERNAME_BLACKLIST = ['admin', 'administrator', 'hostmaster', 'info', 'postmaste
# Initialize the rails application
Diaspora::Application.initialize!
require Rails.root.join('lib', 'federation_logger')
# allow token auth only for posting activitystream photos
module Devise
......
ActsAsTaggableOn.force_lowercase = true
require Rails.root.join("app", "models", "acts_as_taggable_on", "tag")
# Stdlib
require 'cgi'
require 'uri'
# Not auto required gems
require 'active_support/base64'
require 'builder/xchar'
require 'carrierwave/orm/activerecord'
require 'erb'
require 'redcarpet/render_strip'
require 'typhoeus'
# Presenters
require 'post_presenter'
# Helpers
require 'markdownify_helper'
# Our libs
require 'collect_user_photos'
require 'diaspora'
require 'email_inviter'
require 'evil_query'
require 'federation_logger'
require 'h_card'
require 'hydra_wrapper'
require 'postzord'
require 'publisher'
require 'pubsubhubbub'
require 'salmon'
require 'statistics'
require 'stream'
require 'template_picker'
require 'webfinger'
require 'webfinger_profile'
module DebuggingCukeHelpers
def start_debugging
require 'ruby-debug'
require 'debug'
debugger
true
end
......
......@@ -4,8 +4,6 @@
# instead of editing this one. Cucumber will automatically load all features/**/*.rb
# files.
require 'uri'
require 'cgi'
require File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "paths"))
module WithinHelpers
......
......@@ -3,5 +3,9 @@
# the COPYRIGHT file.
module Diaspora
autoload :Parser
require 'diaspora/exceptions'
require 'diaspora/parser'
require 'diaspora/fetcher'
require 'diaspora/markdownify'
require 'diaspora/exporter'
end
......@@ -9,8 +9,6 @@
#including this module lets you federate an object at the most basic of level
require 'builder/xchar'
module Diaspora
module Federated
module Base
......
module Diaspora
module Fetcher
require 'diaspora/fetcher/public'
end
end
# Copyright (c) 2010-2012, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
class PublicFetcher
module Diaspora; module Fetcher; class Public
# various states that can be assigned to a person to describe where
# in the process of fetching their public posts we're currently at
......@@ -23,11 +22,11 @@ class PublicFetcher
begin
retrieve_and_process_posts
rescue => e
set_fetch_status PublicFetcher::Status_Failed
set_fetch_status Public::Status_Failed
raise e
end
set_fetch_status PublicFetcher::Status_Done
set_fetch_status Public::Status_Done
end
private
......@@ -35,20 +34,20 @@ class PublicFetcher
# if it is reasonable to do so, and that they have not been fetched already
def qualifies_for_fetching?
raise ActiveRecord::RecordNotFound unless @person.present?
return false if @person.fetch_status == PublicFetcher::Status_Unfetchable
return false if @person.fetch_status == Public::Status_Unfetchable
# local users don't need to be fetched
if @person.local?
set_fetch_status PublicFetcher::Status_Unfetchable
set_fetch_status Public::Status_Unfetchable
return false
end
# this record is already being worked on
return false if @person.fetch_status > PublicFetcher::Status_Initial
return false if @person.fetch_status > Public::Status_Initial
# ok, let's go
@person.remote? &&
@person.fetch_status == PublicFetcher::Status_Initial
@person.fetch_status == Public::Status_Initial
end
# call the methods to fetch and process the public posts for the person
......@@ -72,24 +71,19 @@ class PublicFetcher
# fetch the public posts of the person from their server and save the
# JSON response to `@data`
def retrieve_posts
set_fetch_status PublicFetcher::Status_Running
set_fetch_status Public::Status_Running
FEDERATION_LOGGER.info "fetching public posts for #{@person.diaspora_handle}"
conn = Faraday.new(:url => @person.url) do |c|
c.request :json
c.response :json
c.adapter :net_http
resp = Faraday.get("#{@person.url}/people/#{@person.guid}") do |req|
req.headers[:accept] = 'application/json'
req.headers[:user_agent] = 'diaspora-fetcher'
end
conn.headers[:user_agent] = 'diaspora-fetcher'
conn.headers[:accept] = 'application/json'
resp = conn.get "/people/#{@person.guid}"
FEDERATION_LOGGER.debug resp.body.to_s[0..250]
@data = resp.body
set_fetch_status PublicFetcher::Status_Fetched
@data = JSON.parse resp.body
set_fetch_status Public::Status_Fetched
end
# process the public posts that were previously fetched with `retrieve_posts`
......@@ -124,7 +118,7 @@ class PublicFetcher
StatusMessage.set_callback :create, :set_guid
end
set_fetch_status PublicFetcher::Status_Processed
set_fetch_status Public::Status_Processed
end
# set and save the fetch status for the current person
......@@ -182,4 +176,4 @@ class PublicFetcher
type_ok
end
end
end; end; end
require 'erb'
module Diaspora
module Markdownify
class HTML < Redcarpet::Render::HTML
......
require Rails.root.join("app", "models", "acts_as_taggable_on", "tag")
module Diaspora
module Markdownify
class Email < Redcarpet::Render::HTML
......@@ -26,4 +24,4 @@ module Diaspora
end
end
end
end
\ No newline at end of file
end
......@@ -2,8 +2,6 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require Rails.root.join("app", "models", "acts_as_taggable_on", "tag")
module Diaspora
module Taggable
def self.included(model)
......
......@@ -2,9 +2,6 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require 'typhoeus'
require 'active_support/base64'
class HydraWrapper
OPTS = {:max_redirects => 3, :timeout => 25000, :method => :post,
......
......@@ -3,5 +3,6 @@
# the COPYRIGHT file.
module Postzord
require 'postzord/receiver'
require 'postzord/dispatcher'
end
......@@ -4,8 +4,8 @@
class Postzord::Dispatcher
require Rails.root.join('lib', 'postzord', 'dispatcher', 'private')
require Rails.root.join('lib', 'postzord', 'dispatcher', 'public')
require 'postzord/dispatcher/private'
require 'postzord/dispatcher/public'
attr_reader :sender, :object, :xml, :subscribers, :opts
......
......@@ -4,8 +4,9 @@
class Postzord::Receiver
require Rails.root.join('lib', 'postzord', 'receiver', 'private')
require Rails.root.join('lib', 'postzord', 'receiver', 'public')
require 'postzord/receiver/private'
require 'postzord/receiver/public'
require 'postzord/receiver/local_batch'
def perform!
self.receive!
......
......@@ -2,9 +2,6 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require Rails.root.join('lib', 'webfinger')
require Rails.root.join('lib', 'diaspora', 'parser')
class Postzord::Receiver::Private < Postzord::Receiver
def initialize(user, opts={})
......
......@@ -24,7 +24,7 @@ end
# Verify documents secured with Magic Signatures
module Salmon
autoload :Slap, File.join(Rails.root, "lib", "salmon", "slap").to_s
autoload :EncryptedSlap, File.join(Rails.root, "lib", "salmon", "encrypted_slap").to_s
autoload :MagicSigEnvelope, File.join(Rails.root, "lib", "salmon", "magic_sig_envelope").to_s
require "salmon/slap"
require "salmon/encrypted_slap"
require "salmon/magic_sig_envelope"
end
module Stream
require 'stream/activity'
require 'stream/aspect'
require 'stream/comments'
require 'stream/followed_tag'
require 'stream/likes'
require 'stream/mention'
require 'stream/multi'
require 'stream/person'
require 'stream/public'
require 'stream/tag'
end
require Rails.root.join("lib", "publisher")
class Stream::Base
TYPES_OF_POST_IN_STREAM = ['StatusMessage', 'Reshare', 'ActivityStreams::Photo']
......
......@@ -2,9 +2,6 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require Rails.root.join('lib', 'hcard')
require Rails.root.join('lib', 'webfinger_profile')
class Webfinger
attr_accessor :host_meta_xrd, :webfinger_profile_xrd,
:webfinger_profile, :hcard, :hcard_xrd, :person,
......
......@@ -3,24 +3,26 @@
# the COPYRIGHT file.
require 'spec_helper'
require Rails.root.join('lib','diaspora','fetcher','public')
# Tests fetching public posts of a person on a remote server
describe PublicFetcher do
describe Diaspora::Fetcher::Public do
before do
# the fixture is taken from an actual json request.
# it contains 10 StatusMessages and 5 Reshares, all of them public
# the guid of the person is "7445f9a0a6c28ebb"
@fixture = File.open(Rails.root.join('spec', 'fixtures', 'public_posts.json')).read
@fetcher = PublicFetcher.new
@fetcher = Diaspora::Fetcher::Public.new
@person = FactoryGirl.create(:person, {:guid => "7445f9a0a6c28ebb",
:url => "https://remote-testpod.net",
:diaspora_handle => "testuser@remote-testpod.net"})
stub_request(:get, /remote-testpod.net\/people\/.*/)
.with(:headers => {'Accept'=>'application/json'})
.to_return(:body => @fixture)
.with(headers: {
'Accept'=>'application/json',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'User-Agent'=>'diaspora-fetcher'
}).to_return(:body => @fixture)
end
describe "#retrieve_posts" do
......@@ -34,8 +36,8 @@ describe PublicFetcher do
it "sets the operation status on the person" do
@person.reload
@person.fetch_status.should_not eql(PublicFetcher::Status_Initial)
@person.fetch_status.should eql(PublicFetcher::Status_Fetched)
@person.fetch_status.should_not eql(Diaspora::Fetcher::Public::Status_Initial)
@person.fetch_status.should eql(Diaspora::Fetcher::Public::Status_Fetched)
end
it "sets the @data variable to the parsed JSON data" do
......@@ -73,8 +75,8 @@ describe PublicFetcher do
}
@person.reload
@person.fetch_status.should_not eql(PublicFetcher::Status_Initial)
@person.fetch_status.should eql(PublicFetcher::Status_Processed)
@person.fetch_status.should_not eql(Diaspora::Fetcher::Public::Status_Initial)
@person.fetch_status.should eql(Diaspora::Fetcher::Public::Status_Processed)
end
context 'created post' do
......@@ -121,7 +123,7 @@ describe PublicFetcher do
end
context "private methods" do
let(:public_fetcher) { PublicFetcher.new }
let(:public_fetcher) { Diaspora::Fetcher::Public.new }
describe '#qualifies_for_fetching?' do
it "raises an error if the person doesn't exist" do
......@@ -135,7 +137,7 @@ describe PublicFetcher do
it 'returns false if the person is unfetchable' do
public_fetcher.instance_eval {
@person = FactoryGirl.create(:person, {:fetch_status => PublicFetcher::Status_Unfetchable})
@person = FactoryGirl.create(:person, {:fetch_status => Diaspora::Fetcher::Public::Status_Unfetchable})
qualifies_for_fetching?
}.should be_false
end
......@@ -146,12 +148,12 @@ describe PublicFetcher do
@person = user.person
qualifies_for_fetching?
}.should be_false
user.person.fetch_status.should eql PublicFetcher::Status_Unfetchable
user.person.fetch_status.should eql Diaspora::Fetcher::Public::Status_Unfetchable
end
it 'returns false if the person is processing already (or has been processed)' do
person = FactoryGirl.create(:person)
person.fetch_status = PublicFetcher::Status_Fetched
person.fetch_status = Diaspora::Fetcher::Public::Status_Fetched