diff options
author | Raphael Sofaer <raphael@joindiaspora.com> | 2011-06-29 04:13:38 +0400 |
---|---|---|
committer | Raphael Sofaer <raphael@joindiaspora.com> | 2011-06-29 04:13:38 +0400 |
commit | 1af945de11739eeda625164290867138e9416610 (patch) | |
tree | cba0753a4f5c5947b03c2270a34c8bb5c3adbf5f /lib | |
parent | 7fd9b81e555ff2baa5019ca3ae3d4bb340bd2e64 (diff) | |
parent | 7ba80ce7992064956b57df7b4bfe3e53100394a5 (diff) |
Merge branch 'master' into oauth
Conflicts:
Gemfile
Gemfile.lock
app/models/app_config.rb
app/models/post.rb
db/schema.rb
public/stylesheets/sass/application.sass
spec/lib/webfinger_spec.rb
Diffstat (limited to 'lib')
-rw-r--r-- | lib/cruise/build.rb | 28 | ||||
-rw-r--r-- | lib/diaspora/encryptable.rb | 11 | ||||
-rw-r--r-- | lib/diaspora/guid.rb | 3 | ||||
-rw-r--r-- | lib/diaspora/ostatus_builder.rb | 6 | ||||
-rw-r--r-- | lib/diaspora/relayable.rb | 7 | ||||
-rw-r--r-- | lib/diaspora/user/connecting.rb | 4 | ||||
-rw-r--r-- | lib/diaspora/user/querying.rb | 22 | ||||
-rw-r--r-- | lib/log_overrider.rb | 11 | ||||
-rw-r--r-- | lib/splunk_logging.rb | 4 | ||||
-rw-r--r-- | lib/tasks/statistics.rake | 103 | ||||
-rw-r--r-- | lib/tasks/whitespace.rake | 6 | ||||
-rw-r--r-- | lib/webfinger.rb | 15 |
12 files changed, 66 insertions, 154 deletions
diff --git a/lib/cruise/build.rb b/lib/cruise/build.rb deleted file mode 100644 index 90bd5a7fb..000000000 --- a/lib/cruise/build.rb +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/env ruby -require 'fileutils' -include FileUtils - -def root_dir - @root_dir ||= File.expand_path(File.dirname(__FILE__) + '/../..') -end - -def rake(*tasks) - tasks.each do |task| - return false unless system("rake", task, 'RAILS_ENV=test') - end -end - -build_results = {} - -cd root_dir do - build_results[:bundle] = system 'bundle install' # bundling here, rather than in a task (not in Rails context) - build_results[:spec] = rake 'cruise' -end - -failures = build_results.select { |key, value| value == false } - -if failures.empty? - exit(0) -else - exit(-1) -end diff --git a/lib/diaspora/encryptable.rb b/lib/diaspora/encryptable.rb index 53cf4fc1d..708f03fff 100644 --- a/lib/diaspora/encryptable.rb +++ b/lib/diaspora/encryptable.rb @@ -1,5 +1,10 @@ module Diaspora module Encryptable + # Check that signature is a correct signature of #signable_string by person + # + # @param [String] signature The signature to be verified. + # @param [Person] person The signer. + # @return [Boolean] def verify_signature(signature, person) if person.nil? Rails.logger.info("event=verify_signature status=abort reason=no_person guid=#{self.guid}") @@ -18,6 +23,8 @@ module Diaspora validity end + # @param [OpenSSL::PKey::RSA] key An RSA key + # @return [String] A Base64 encoded signature of #signable_string with key def sign_with_key(key) sig = Base64.encode64(key.sign "SHA", signable_string) log_hash = {:event => :sign_with_key, :status => :complete} @@ -26,6 +33,7 @@ module Diaspora sig end + # @return [Array<String>] The ROXML attrs other than author_signature and parent_author_signature. def signable_accessors accessors = self.class.roxml_attrs.collect do |definition| definition.accessor @@ -36,12 +44,15 @@ module Diaspora accessors end + # @return [String] Defaults to the ROXML attrs which are not signatures. def signable_string signable_accessors.collect{ |accessor| (self.send accessor.to_sym).to_s }.join(';') end + # @abstract + # @return [String] #def signable_string # raise NotImplementedError.new("Implement this in your encryptable class") #end diff --git a/lib/diaspora/guid.rb b/lib/diaspora/guid.rb index 728752434..7ea870152 100644 --- a/lib/diaspora/guid.rb +++ b/lib/diaspora/guid.rb @@ -1,10 +1,13 @@ module Diaspora::Guid + # Creates a before_create callback which calls #set_guid and makes the guid serialize in to_xml def self.included(model) model.class_eval do before_create :set_guid xml_attr :guid end end + + # @return [String] The model's guid. def set_guid self.guid ||= ActiveSupport::SecureRandom.hex(8) end diff --git a/lib/diaspora/ostatus_builder.rb b/lib/diaspora/ostatus_builder.rb index e5fd0e855..b13674b21 100644 --- a/lib/diaspora/ostatus_builder.rb +++ b/lib/diaspora/ostatus_builder.rb @@ -63,11 +63,7 @@ module Diaspora def create_body @posts.inject("") do |xml,curr| if curr.respond_to?(:to_activity) - unless xml - curr.to_activity - else - xml + curr.to_activity - end + xml + curr.to_activity(:author => @user.person) else xml end diff --git a/lib/diaspora/relayable.rb b/lib/diaspora/relayable.rb index d92a87ab8..acdd8526d 100644 --- a/lib/diaspora/relayable.rb +++ b/lib/diaspora/relayable.rb @@ -22,6 +22,7 @@ module Diaspora def parent_guid self.parent.guid end + def parent_guid= new_parent_guid self.parent = parent_class.where(:guid => new_parent_guid).first end @@ -82,14 +83,20 @@ module Diaspora verify_signature(self.author_signature, self.author) end + # @abstract + # @return [Class] def parent_class raise NotImplementedError.new('you must override parent_class in order to enable relayable on this model') end + # @abstract + # @return An instance of Relayable#parent_class def parent raise NotImplementedError.new('you must override parent in order to enable relayable on this model') end + # @abstract + # @param parent An instance of Relayable#parent_class def parent= parent raise NotImplementedError.new('you must override parent= in order to enable relayable on this model') end diff --git a/lib/diaspora/user/connecting.rb b/lib/diaspora/user/connecting.rb index 3c083f821..668ab2709 100644 --- a/lib/diaspora/user/connecting.rb +++ b/lib/diaspora/user/connecting.rb @@ -5,6 +5,10 @@ module Diaspora module UserModules module Connecting + # This will create a contact on the side of the sharer and the sharee. + # @param [Person] person The person to start sharing with. + # @param [Aspect] aspect The aspect to add them to. + # @return [Contact] The newly made contact for the passed in person. def share_with(person, aspect) contact = self.contacts.find_or_initialize_by_person_id(person.id) unless contact.receiving? diff --git a/lib/diaspora/user/querying.rb b/lib/diaspora/user/querying.rb index f953ab221..4a6fa6680 100644 --- a/lib/diaspora/user/querying.rb +++ b/lib/diaspora/user/querying.rb @@ -38,11 +38,17 @@ module Diaspora posts_from_self = posts_from_self.joins(:aspect_visibilities).where(:aspect_visibilities => {:aspect_id => opts[:by_members_of]}) end - posts_from_others = posts_from_others.select(select_clause).limit(opts[:limit]).order(order_with_table).where(Post.arel_table[order_field].lt(opts[:max_time])) - posts_from_self = posts_from_self.select(select_clause).limit(opts[:limit]).order(order_with_table).where(Post.arel_table[order_field].lt(opts[:max_time])) + unless defined?(ActiveRecord::ConnectionAdapters::SQLite3Adapter) && ActiveRecord::Base.connection.class == ActiveRecord::ConnectionAdapters::SQLite3Adapter + posts_from_others = posts_from_others.select(select_clause).limit(opts[:limit]).order(order_with_table).where(Post.arel_table[order_field].lt(opts[:max_time])) + posts_from_self = posts_from_self.select(select_clause).limit(opts[:limit]).order(order_with_table).where(Post.arel_table[order_field].lt(opts[:max_time])) + all_posts = "(#{posts_from_others.to_sql}) UNION ALL (#{posts_from_self.to_sql}) ORDER BY #{opts[:order]} LIMIT #{opts[:limit]}" + else + posts_from_others = posts_from_others.select(select_clause) + posts_from_self = posts_from_self.select(select_clause) + all_posts = "#{posts_from_others.to_sql} UNION ALL #{posts_from_self.to_sql} ORDER BY #{opts[:order]} LIMIT #{opts[:limit]}" + end - all_posts = "(#{posts_from_others.to_sql}) UNION ALL (#{posts_from_self.to_sql}) ORDER BY #{opts[:order]} LIMIT #{opts[:limit]}" - post_ids = Post.connection.execute(all_posts).map{|r| r.first} + post_ids = Post.connection.select_values(all_posts) Post.where(:id => post_ids).select('DISTINCT posts.*').limit(opts[:limit]).order(order_with_table) end @@ -96,9 +102,13 @@ module Diaspora p = Post.arel_table post_ids = [] if contact = self.contact_for(person) - post_ids = Post.connection.execute(contact.post_visibilities.where(:hidden => false).select('post_visibilities.post_id').to_sql).map{|r| r.first} + post_ids = Post.connection.select_values( + contact.post_visibilities.where(:hidden => false).select('post_visibilities.post_id').to_sql + ) end - post_ids += Post.connection.execute(person.posts.where(:public => true).select('posts.id').to_sql).map{|r| r.first} + post_ids += Post.connection.select_values( + person.posts.where(:public => true).select('posts.id').to_sql + ) Post.where(:id => post_ids, :pending => false).select('DISTINCT posts.*').order("posts.created_at DESC") end diff --git a/lib/log_overrider.rb b/lib/log_overrider.rb index 506506616..0c0e5991e 100644 --- a/lib/log_overrider.rb +++ b/lib/log_overrider.rb @@ -1,4 +1,7 @@ class ActionView::LogSubscriber + + # In order to be more friendly to Splunk, which we use for log analysis, + # we override a few logging methods. There are not overriden if enable_splunk_logging is set to false in config/application.yml def render_template(event) count = event.payload[:count] || 1 hash = {:event => :render, @@ -18,6 +21,8 @@ end module ActionDispatch class ShowExceptions private + # This override logs in a format Splunk can more easily understand. + # @see ActionView::LogSubscriber#render_template def log_error(exception) return unless logger @@ -40,6 +45,8 @@ class ActionController::LogSubscriber #noop end + # This override logs in a format Splunk can more easily understand. + # @see ActionView::LogSubscriber#render_template def process_action(event) payload = event.payload additions = ActionController::Base.log_process_action(payload) @@ -68,6 +75,8 @@ end module Rails module Rack class Logger + # This override logs in a format Splunk can more easily understand. + # @see ActionView::LogSubscriber#render_template def before_dispatch(env) request = ActionDispatch::Request.new(env) path = request.fullpath @@ -80,6 +89,8 @@ end module ActiveRecord class LogSubscriber + # This override logs in a format Splunk can more easily understand. + # @see ActionView::LogSubscriber#render_template def sql(event) self.class.runtime += event.duration return unless logger.info? diff --git a/lib/splunk_logging.rb b/lib/splunk_logging.rb index 521631512..34706b26a 100644 --- a/lib/splunk_logging.rb +++ b/lib/splunk_logging.rb @@ -13,10 +13,10 @@ module SplunkLogging if hash.respond_to?(:keys) string = '' hash.each_pair do |key, value| - if(value.instance_of?(Symbol)||value.instance_of?(Fixnum)||value.instance_of?(Float)) + if [Symbol, Fixnum, Float, Class].include?(value.class) string << "#{key}=#{value} " else - string << "#{key}='#{value}' " + string << "#{key}=\"#{value.to_s.gsub('"', '\"')}\" " end end string diff --git a/lib/tasks/statistics.rake b/lib/tasks/statistics.rake deleted file mode 100644 index 24a77794a..000000000 --- a/lib/tasks/statistics.rake +++ /dev/null @@ -1,103 +0,0 @@ -namespace :statistics do - - desc 'on users: total, active' - namespace :users do - def set_up_user_stats - @users = User.where(:getting_started => false).count - @incomplete = User.where("getting_started = TRUE AND sign_in_count >= 0").count - @invitations = Invitation.count - @contacts_active = Contact.where(:pending => false).count - @contacts_pending = Contact.count - @contacts_active - @aspects = Aspect.count - @last_24_hrs = User.where("current_sign_in_at > '#{(Time.now - 1.day).to_date}'").count - @last_7_d = User.where("current_sign_in_at > '#{(Time.now - 7.days).to_date}'").count - - @notification_off = User.where(:disable_mail=>true).count - @notification_off_per = @notification_off.to_f / @users - @no_invites = User.where(:invites => 0).count - - @sql = ActiveRecord::Base.connection - end - - def users_with_x_posts(count) - @sql.execute( - "SELECT COUNT(*) FROM (SELECT `people`.guid, COUNT(*) AS posts_sum FROM `people` LEFT JOIN `posts` ON `people`.id = `posts`.person_id GROUP BY `people`.guid) AS t1 WHERE t1.posts_sum = #{count};" - ).first[0] - end - - def users_with_x_posts_today(count) - @sql.execute( - "SELECT COUNT(*) FROM (SELECT `people`.guid, COUNT(*) AS posts_sum FROM `people` LEFT JOIN `posts` ON `people`.id = `posts`.person_id AND `post`.created_at > '#{(Time.now - 1.days).to_date}' GROUP BY `people`.guid) AS t1 WHERE t1.posts_sum = #{count};" - ).first[0] - end - - def users_with_x_contacts(count) - @sql.execute( - "SELECT COUNT(*) FROM (SELECT `users`.id, COUNT(*) AS contact_sum FROM `users` LEFT JOIN `contacts` ON `users`.id = `contacts`.person_id AND `contacts`.pending = 0 GROUP BY `users`.id) AS t1 WHERE t1.contact_sum = #{count};" - ).first[0] - end - - task :human => :environment do - set_up_user_stats - puts "Users: %i and %i incomplete" % [@users, @incomplete] - puts "Invitations sent: %i" % @invitations - puts "Contacts: %i and %i pending" % [@contacts_active, @contacts_pending] - puts "Aspects: %i" % @aspects - puts "Users signed in in last 24h: %i" % @last_24_hrs - puts "Users signed in in last 7d: %i" % @last_7_d - - puts "Users with more than one post: %i" % users_with_x_posts(1) - puts "Users with more than five post: %i" % users_with_x_posts(5) - puts "Users with more than ten post: %i" % users_with_x_posts(10) - - puts "Users with 1 or more contacts: %i" % users_with_x_contacts(0) - puts "Users with 5 or more contacts: %i" % users_with_x_contacts(4) - puts "Users with 10 or more contacts: %i" % users_with_x_contacts(9) - end - - task :splunk => :environment do - set_up_user_stats - puts "event=statistic, type=users, count=#{@users}, "+ - "incomplete=#{@incomplete}, " + - "last_1d=#{@last_24_hrs}, "+ - "last_7d=#{@last_7_d}, " + - "notification_off=#{@notification_off}, "+ - "notification_off_%=#{@notification_off_per}, "+ - "no_invites=#{@no_invites}" - - - puts "event=statistic, type=invitations, count=#{@invitations}" - puts "event=statistic, type=contacts, active_count=#{@contacts_active}" - puts "event=statistic, type=contacts, pending_count=#{@contacts_pending}" - - puts "event=statistic, type=aspect, count=#{@aspects}" - end - end - - desc 'on content: posts, photos, status messages, comments' - namespace :content do - task :human => :environment do - puts "Services: %i Facebook, %i Twitter" % [Services::Facebook.count, Services::Twitter.count] - puts "Posts: %i and %i are public" % [Post.count, Post.where(:public => true).count] - puts "Status messages: %i" % [StatusMessage.count, StatusMessage.where(:public => true).count] - puts "Comments: %i" % Comment.count - puts "Photos: %i" % Photo.count - end - - task :splunk => :environment do - post_count = Post.count - public_count = Post.where(:public => true).count - public_per = public_count.to_f/post_count - posts_last_day = Post.where("created_at > '#{(Time.now - 1.days).to_date}'").count - posts_last_day_public = Post.where("created_at > '#{(Time.now - 1.days).to_date}' AND public = true").count - posts_last_day_public_per = posts_last_day_public.to_f/posts_last_day - - puts "event=statistic, type=posts, count=#{post_count}, " + - "public_count=#{public_count}, " + - "public_%=#{public_per}, " + - "last_day=#{posts_last_day}, " + - "last_day_public_count=#{posts_last_day_public}, " + - "last_day_public_%=#{posts_last_day_public_per}" - end - end -end diff --git a/lib/tasks/whitespace.rake b/lib/tasks/whitespace.rake index 7af099dd6..66e2b056c 100644 --- a/lib/tasks/whitespace.rake +++ b/lib/tasks/whitespace.rake @@ -2,19 +2,19 @@ namespace :whitespace do desc 'Removes trailing whitespace' task :cleanup do sh %{for f in `find . -type f | grep -v -e '.git/' -e 'public/' -e '.png'`; - do cat $f | sed 's/[ \t]*$//' > tmp; cp tmp $f; rm tmp; echo -n .; + do sed -i 's/[ \t]*$//' $f; echo -n .; done} end desc 'Converts hard-tabs into two-space soft-tabs' task :retab do sh %{for f in `find . -type f | grep -v -e '.git/' -e 'public/' -e '.png'`; - do cat $f | sed 's/\t/ /g' > tmp; cp tmp $f; rm tmp; echo -n .; + do sed -i 's/\t/ /g' $f; echo -n .; done} end desc 'Remove consecutive blank lines' task :scrub_gratuitous_newlines do sh %{for f in `find . -type f | grep -v -e '.git/' -e 'public/' -e '.png'`; - do cat $f | sed '/./,/^$/!d' > tmp; cp tmp $f; rm tmp; echo -n .; + do sed -i '/./,/^$/!d' $f; echo -n .; done} end end diff --git a/lib/webfinger.rb b/lib/webfinger.rb index bf752bc93..2b742afa4 100644 --- a/lib/webfinger.rb +++ b/lib/webfinger.rb @@ -3,9 +3,6 @@ require File.join(Rails.root, 'lib/webfinger_profile') 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 @ssl = true @@ -47,7 +44,7 @@ class Webfinger private def get_xrd begin - http = RestClient.get xrd_url, OPTS + http = Faraday.get xrd_url profile_url = webfinger_profile_url(http.body) if profile_url @@ -69,7 +66,7 @@ class Webfinger def get_webfinger_profile(profile_url) begin - http = RestClient.get(profile_url, OPTS) + http = Faraday.get(profile_url) rescue raise I18n.t('webfinger.fetch_failed', :profile_url => profile_url) @@ -77,13 +74,17 @@ class Webfinger return http.body end + def hcard_url + @wf_profile.hcard + end + def get_hcard(webfinger_profile) unless webfinger_profile.strip == "" @wf_profile = WebfingerProfile.new(@account, webfinger_profile) begin - hcard = RestClient.get(@wf_profile.hcard, OPTS) + hcard = Faraday.get(hcard_url) rescue return I18n.t('webfinger.hcard_fetch_failed', :account => @account) end @@ -97,7 +98,7 @@ class Webfinger def make_person_from_webfinger(webfinger_profile) card = get_hcard(webfinger_profile) if card && @wf_profile - p = Person.create_from_webfinger(@wf_profile, card) + Person.create_from_webfinger(@wf_profile, card) end end |