2013-05-07 00:39:01 -04:00
|
|
|
require_dependency 'trashable'
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
class Invite < ActiveRecord::Base
|
2013-05-07 00:39:01 -04:00
|
|
|
include Trashable
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :topic
|
2013-02-28 13:54:12 -05:00
|
|
|
belongs_to :invited_by, class_name: 'User'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
has_many :topic_invites
|
2013-02-07 10:45:24 -05:00
|
|
|
has_many :topics, through: :topic_invites, source: :topic
|
2013-02-05 14:16:51 -05:00
|
|
|
validates_presence_of :email
|
|
|
|
validates_presence_of :invited_by_id
|
|
|
|
|
|
|
|
before_create do
|
|
|
|
self.invite_key ||= SecureRandom.hex
|
|
|
|
end
|
|
|
|
|
|
|
|
before_save do
|
2013-04-14 20:20:33 -04:00
|
|
|
self.email = Email.downcase(email)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
validate :user_doesnt_already_exist
|
|
|
|
attr_accessor :email_already_exists
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def user_doesnt_already_exist
|
|
|
|
@email_already_exists = false
|
|
|
|
return if email.blank?
|
2014-01-21 16:53:46 -05:00
|
|
|
u = User.where("email = ?", Email.downcase(email)).first
|
|
|
|
if u && u.id != self.user_id
|
2013-02-05 14:16:51 -05:00
|
|
|
@email_already_exists = true
|
|
|
|
errors.add(:email)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def redeemed?
|
|
|
|
redeemed_at.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def expired?
|
|
|
|
created_at < SiteSetting.invite_expiry_days.days.ago
|
|
|
|
end
|
|
|
|
|
2014-01-21 16:53:46 -05:00
|
|
|
# link_valid? indicates whether the invite link can be used to log in to the site
|
|
|
|
def link_valid?
|
|
|
|
invalidated_at.nil?
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def redeem
|
2014-01-21 16:53:46 -05:00
|
|
|
InviteRedeemer.new(self).redeem unless expired? || destroyed? || !link_valid?
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-11-06 12:56:26 -05:00
|
|
|
|
|
|
|
# Create an invite for a user, supplying an optional topic
|
|
|
|
#
|
|
|
|
# Return the previously existing invite if already exists. Returns nil if the invite can't be created.
|
|
|
|
def self.invite_by_email(email, invited_by, topic=nil)
|
|
|
|
lower_email = Email.downcase(email)
|
2014-01-21 15:13:55 -05:00
|
|
|
invite = Invite.with_deleted
|
|
|
|
.where('invited_by_id = ? and email = ?', invited_by.id, lower_email)
|
|
|
|
.order('created_at DESC')
|
|
|
|
.first
|
|
|
|
|
|
|
|
if invite && invite.expired?
|
|
|
|
invite.destroy
|
|
|
|
invite = nil
|
|
|
|
end
|
2013-11-06 12:56:26 -05:00
|
|
|
|
|
|
|
if invite.blank?
|
|
|
|
invite = Invite.create(invited_by: invited_by, email: lower_email)
|
|
|
|
unless invite.valid?
|
|
|
|
topic.grant_permission_to_user(lower_email) if topic.present? && topic.email_already_exists_for?(invite)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Recover deleted invites if we invite them again
|
|
|
|
invite.recover! if invite.deleted_at.present?
|
|
|
|
|
|
|
|
topic.topic_invites.create(invite_id: invite.id) if topic.present?
|
|
|
|
Jobs.enqueue(:invite_email, invite_id: invite.id)
|
|
|
|
invite
|
|
|
|
end
|
|
|
|
|
2013-11-08 14:11:41 -05:00
|
|
|
def self.find_all_invites_from(inviter)
|
|
|
|
Invite.where(invited_by_id: inviter.id)
|
|
|
|
.includes(:user => :user_stat)
|
|
|
|
.order('CASE WHEN invites.user_id IS NOT NULL THEN 0 ELSE 1 END',
|
|
|
|
'user_stats.time_read DESC',
|
|
|
|
'invites.redeemed_at DESC')
|
|
|
|
.limit(SiteSetting.invites_shown)
|
|
|
|
.references('user_stats')
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.find_redeemed_invites_from(inviter)
|
|
|
|
find_all_invites_from(inviter).where('invites.user_id IS NOT NULL')
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.filter_by(email_or_username)
|
|
|
|
if email_or_username
|
|
|
|
where(
|
|
|
|
'(LOWER(invites.email) LIKE :filter) or (LOWER(users.username) LIKE :filter)',
|
|
|
|
filter: "%#{email_or_username.downcase}%"
|
|
|
|
)
|
|
|
|
else
|
2013-12-12 03:37:04 -05:00
|
|
|
rails4? ? all : scoped
|
2013-11-08 14:11:41 -05:00
|
|
|
end
|
|
|
|
end
|
2014-01-21 16:53:46 -05:00
|
|
|
|
|
|
|
def self.invalidate_for_email(email)
|
|
|
|
i = Invite.where(email: Email.downcase(email)).first
|
|
|
|
if i
|
|
|
|
i.invalidated_at = Time.zone.now
|
|
|
|
i.save
|
|
|
|
end
|
|
|
|
i
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-05-23 22:48:32 -04:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: invites
|
|
|
|
#
|
2014-02-06 19:07:36 -05:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# invite_key :string(32) not null
|
|
|
|
# email :string(255) not null
|
|
|
|
# invited_by_id :integer not null
|
|
|
|
# user_id :integer
|
|
|
|
# redeemed_at :datetime
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# deleted_at :datetime
|
|
|
|
# deleted_by_id :integer
|
|
|
|
# invalidated_at :datetime
|
2013-05-23 22:48:32 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_invites_on_email_and_invited_by_id (email,invited_by_id) UNIQUE
|
|
|
|
# index_invites_on_invite_key (invite_key) UNIQUE
|
|
|
|
#
|