some progress
This commit is contained in:
parent
159fce3ef2
commit
cc6618d294
4 changed files with 64 additions and 132 deletions
|
|
@ -16,16 +16,20 @@ class Invitation < ActiveRecord::Base
|
||||||
|
|
||||||
attr_accessible :sender, :recipient, :aspect, :service, :identifier
|
attr_accessible :sender, :recipient, :aspect, :service, :identifier
|
||||||
|
|
||||||
before_validation :attach_recipent, :on => :create
|
before_validation :set_email_as_default_service
|
||||||
|
before_validation :attach_recipient, :on => :create
|
||||||
before_create :ensure_not_inviting_self
|
before_create :ensure_not_inviting_self
|
||||||
|
|
||||||
validate :valid_identifier?
|
validate :valid_identifier?
|
||||||
validates_uniqueness_of :sender, :scope => :recipient
|
validates_uniqueness_of :sender, :scope => :recipient
|
||||||
|
|
||||||
|
def set_email_as_default_service
|
||||||
|
self.service ||='email'
|
||||||
|
end
|
||||||
|
|
||||||
def identifier=(ident)
|
def identifier=(ident)
|
||||||
ident.downcase! if ident
|
ident.downcase! if ident
|
||||||
ident
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
def not_inviting_yourself
|
def not_inviting_yourself
|
||||||
|
|
@ -39,119 +43,7 @@ class Invitation < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def skip_invitation?
|
def skip_invitation?
|
||||||
|
self.service != 'email'
|
||||||
end
|
|
||||||
|
|
||||||
def valid_identifier?
|
|
||||||
if self.service == 'email'
|
|
||||||
unless self.identifier.match(Devise.email_regexp)
|
|
||||||
errors[:base] << 'You can not invite yourself'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# @param opts [Hash] Takes :identifier, :service, :idenfitier, :from, :message
|
|
||||||
# @return [User]
|
|
||||||
def self.invite(opts={})
|
|
||||||
# return if the current user is trying to invite himself via email
|
|
||||||
return false if opts[:identifier] == opts[:from].email
|
|
||||||
|
|
||||||
if existing_user = self.find_existing_user(opts[:service], opts[:identifier])
|
|
||||||
# Check whether or not the existing User has already been invited;
|
|
||||||
# and if so, start sharing with the Person.
|
|
||||||
elsif not existing_user.invited?
|
|
||||||
opts[:from].share_with(existing_user.person, opts[:into])
|
|
||||||
return
|
|
||||||
|
|
||||||
# If the sender has already invited the recipient, raise an error.
|
|
||||||
elsif Invitation.where(:sender_id => opts[:from].id, :recipient_id => existing_user.id).first
|
|
||||||
raise "You already invited this person"
|
|
||||||
|
|
||||||
# When everything checks out, we merge the existing user into the
|
|
||||||
# options hash to pass on to self.create_invitee.
|
|
||||||
else
|
|
||||||
opts.merge(:existing_user => existing_user)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
create_invitee(opts)
|
|
||||||
end
|
|
||||||
|
|
||||||
# @param service [String] String representation of the service invitation provider (i.e. facebook, email)
|
|
||||||
# @param identifier [String] String representation of the reciepients identity on the provider (i.e. 'bob.smith', bob@aol.com)
|
|
||||||
# @return [User]
|
|
||||||
|
|
||||||
# @params opts [Hash] Takes :from, :existing_user, :service, :identifier, :message
|
|
||||||
# @return [User]
|
|
||||||
def self.create_invitee(opts={})
|
|
||||||
invitee = opts[:existing_user]
|
|
||||||
invitee ||= User.new(:invitation_service => opts[:service], :invitation_identifier => opts[:identifier])
|
|
||||||
|
|
||||||
# (dan) I'm not sure why, but we need to call .valid? on our User.
|
|
||||||
invitee.valid?
|
|
||||||
|
|
||||||
# Return a User immediately if an invalid email is passed in
|
|
||||||
|
|
||||||
# Logic if there is an explicit sender
|
|
||||||
|
|
||||||
invitee.skip_invitation = (opts[:service] != 'email')
|
|
||||||
invitee.invite!
|
|
||||||
|
|
||||||
# Logging the invitation action
|
|
||||||
log_hash = {:event => :invitation_sent, :to => opts[:identifier], :service => opts[:service]}
|
|
||||||
log_hash.merge({:inviter => opts[:from].diaspora_handle, :invitier_uid => opts[:from].id, :inviter_created_at_unix => opts[:from].created_at.to_i}) if opts[:from]
|
|
||||||
Rails.logger.info(log_hash)
|
|
||||||
|
|
||||||
invitee
|
|
||||||
end
|
|
||||||
|
|
||||||
def resend
|
|
||||||
recipient.invite!
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# @return Contact
|
# @return Contact
|
||||||
|
|
@ -162,16 +54,40 @@ end
|
||||||
contact
|
contact
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def invite!
|
||||||
|
recipient.skip_invitation = self.skip_invitation?
|
||||||
|
recipient.invite!
|
||||||
|
|
||||||
|
# Logging the invitation action
|
||||||
|
log_hash = {:event => :invitation_sent, :to => self[:identifier], :service => self[:service]}
|
||||||
|
log_hash.merge({:inviter => self.sender.diaspora_handle, :invitier_uid => self.sender.id, :inviter_created_at_unix => self.sender.created_at.to_i}) if self.sender
|
||||||
|
Rails.logger.info(log_hash)
|
||||||
|
|
||||||
|
recipient
|
||||||
|
end
|
||||||
|
|
||||||
|
def resend
|
||||||
|
self.invite!
|
||||||
|
end
|
||||||
|
|
||||||
# @return [String]
|
# @return [String]
|
||||||
def recipient_identifier
|
def recipient_identifier
|
||||||
if recipient.invitation_service == 'email'
|
if self.service == 'email'
|
||||||
recipient.invitation_identifier
|
self.identifier
|
||||||
elsif recipient.invitation_service == 'facebook'
|
elsif self.service == 'facebook'
|
||||||
if su = ServiceUser.where(:uid => recipient.invitation_identifier).first
|
if su = ServiceUser.where(:uid => self.identifier).first
|
||||||
su.name
|
su.name
|
||||||
else
|
else
|
||||||
I18n.t('invitations.a_facebook_user')
|
I18n.t('invitations.a_facebook_user')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def valid_identifier?
|
||||||
|
if self.service == 'email'
|
||||||
|
unless self.identifier.match(Devise.email_regexp)
|
||||||
|
errors[:base] << 'invalid email'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -65,28 +65,31 @@ class User < ActiveRecord::Base
|
||||||
service = invitation.service
|
service = invitation.service
|
||||||
identifier = invitation.identifier
|
identifier = invitation.identifier
|
||||||
|
|
||||||
unless existing_user = User.joins(:invitations)
|
#find users that may already exsist
|
||||||
.where(:invitation_service => service,
|
#1.
|
||||||
:invitation_identifier => identifier).first
|
if service == 'email'
|
||||||
if service == 'email'
|
existing_user = User.where(:email => identifier).first
|
||||||
existing_user = User.where(:email => identifier).first
|
else
|
||||||
else
|
existing_user = User.joins(:services).where(:services => {:type => "Services::#{service.titleize}", :uid => identifier}).first
|
||||||
existing_user = User.joins(:services).where(:services => {:type => "Services::#{service.titleize}", :uid => identifier}).first
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if existing_user.nil?
|
||||||
|
i = Invitation.where(:service => service, :identifier => identifier).first
|
||||||
|
existing_user = i.recipient if i
|
||||||
|
end
|
||||||
|
|
||||||
if existing_user
|
if existing_user
|
||||||
return existing_user
|
return existing_user
|
||||||
else
|
else
|
||||||
|
self.create_from_invitation!(invitation)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.create_from_invitation!
|
def self.create_from_invitation!(invitation)
|
||||||
user = User.new
|
user = User.new
|
||||||
user.generate_keys
|
user.generate_keys
|
||||||
user.generate_invitation_token
|
user.send(:generate_invitation_token)
|
||||||
|
#user.invitations << invitation
|
||||||
# we need to make a custom validator here to make this safer
|
# we need to make a custom validator here to make this safer
|
||||||
user.save(:validate => false)
|
user.save(:validate => false)
|
||||||
user
|
user
|
||||||
|
|
|
||||||
11
db/migrate/20110816061820_add_fields_to_invitations.rb
Normal file
11
db/migrate/20110816061820_add_fields_to_invitations.rb
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
class AddFieldsToInvitations < ActiveRecord::Migration
|
||||||
|
def self.up
|
||||||
|
add_column :invitations, :service, :string
|
||||||
|
add_column :invitations, :identifier, :string
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.down
|
||||||
|
remove_column :invitations, :service
|
||||||
|
remove_column :invitations, :identifier
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended to check this file into your version control system.
|
# It's strongly recommended to check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(:version => 20110815210933) do
|
ActiveRecord::Schema.define(:version => 20110816061820) do
|
||||||
|
|
||||||
create_table "aspect_memberships", :force => true do |t|
|
create_table "aspect_memberships", :force => true do |t|
|
||||||
t.integer "aspect_id", :null => false
|
t.integer "aspect_id", :null => false
|
||||||
|
|
@ -104,6 +104,8 @@ ActiveRecord::Schema.define(:version => 20110815210933) do
|
||||||
t.integer "aspect_id"
|
t.integer "aspect_id"
|
||||||
t.datetime "created_at"
|
t.datetime "created_at"
|
||||||
t.datetime "updated_at"
|
t.datetime "updated_at"
|
||||||
|
t.string "service"
|
||||||
|
t.string "identifier"
|
||||||
end
|
end
|
||||||
|
|
||||||
add_index "invitations", ["aspect_id"], :name => "index_invitations_on_aspect_id"
|
add_index "invitations", ["aspect_id"], :name => "index_invitations_on_aspect_id"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue