jam-cloud/ruby/lib/jam_ruby/models/friend_request.rb

105 lines
3.5 KiB
Ruby
Raw Normal View History

2012-10-14 02:18:20 +00:00
module JamRuby
2012-10-14 04:21:52 +00:00
class FriendRequest < ActiveRecord::Base
include HtmlSanitize
html_sanitize strict: [:message]
2012-10-14 02:18:20 +00:00
2012-10-14 04:21:52 +00:00
self.primary_key = 'id'
2012-10-14 02:18:20 +00:00
STATUS = %w(accept block spam ignore)
2014-03-25 15:29:08 +00:00
belongs_to :user, :class_name => "JamRuby::User", :foreign_key => 'user_id'
belongs_to :friend, :class_name => "JamRuby::User", :foreign_key => 'friend_id'
2012-10-14 02:18:20 +00:00
2012-10-15 02:02:55 +00:00
validates :user_id, :presence => true
validates :friend_id, :presence => true
#validates :status, :inclusion => {:in => STATUS}
2013-07-26 08:07:24 +00:00
validates :message, no_profanity: true
2012-10-15 02:02:55 +00:00
2020-10-14 02:05:08 +00:00
validate :same_school_protection
def same_school_protection
if self.user_id && self.user.nil?
self.user = User.find(user_id)
end
if self.friend_id && self.friend.nil?
self.friend = User.find(friend_id)
end
if self.user && self.friend
if !self.user.is_platform_instructor && !self.friend.is_platform_instructor
if self.user.school_id != self.friend.school_id
errors.add(:friend, ValidationMessages::CAN_ONLY_CHAT_SAME_SCHOOL)
end
end
end
end
def to_s
2020-04-04 22:51:36 +00:00
"#{self.id} => #{self.user.to_s}:#{self.friend.to_s}"
end
2020-04-04 22:51:36 +00:00
def self.invited_path(invited_user, sender, message)
friend_request = self.save(nil, sender.id, invited_user.id, nil, message, supress_initial: true)
self.save(friend_request.id, sender.id, invited_user.id, 'accept', message, supress_initial: true)
end
def self.save(id, user_id, friend_id, status, message, supress_initial: false)
# new friend request
if id.nil?
2020-04-04 22:51:36 +00:00
friend_request = FriendRequest.new
friend_request = validate_friend_request(friend_request, user_id, friend_id)
friend_request.user_id = user_id
friend_request.friend_id = friend_id
friend_request.message = message
2020-10-14 02:05:08 +00:00
if friend_request.save
# send notification
Notification.send_friend_request(friend_request.id, user_id, friend_id) unless supress_initial
end
2020-04-04 22:51:36 +00:00
else
ActiveRecord::Base.transaction do
2012-12-30 14:39:41 +00:00
friend_request = FriendRequest.find(id)
friend_request.status = status
friend_request.save
duplicate_requests = FriendRequest.where("user_id = ? AND friend_id = ?", user_id, friend_id)
duplicate_requests.each do |req|
req.status = status
req.save
end
# create both records for this friendship
2012-12-30 14:39:41 +00:00
if friend_request.status == "accept"
2020-10-14 02:05:08 +00:00
if Friendship.save(friend_request.user_id, friend_request.friend_id)
# send notification
Notification.send_friend_request_accepted(friend_request.user_id, friend_request.friend_id)
end
end
end
end
return friend_request
end
private
def self.validate_friend_request(friend_request, user_id, friend_id)
friend_requests = FriendRequest.where("user_id='#{user_id}' AND friend_id='#{friend_id}'")
# check if there are any friend requests for this source/target user combo, and if
# any have been marked as spam or blocked, set the status of this friend request
# to match so it doesn't show up in the queue
unless friend_requests.nil? || friend_requests.size == 0
2012-12-30 14:39:41 +00:00
if friend_requests.exists?(:status => "spam")
friend_request.status = "spam"
2012-10-28 02:35:28 +00:00
2012-12-30 14:39:41 +00:00
elsif friend_requests.exists?(:status => "block")
friend_request.status = "block"
end
end
return friend_request
end
2012-10-14 04:21:52 +00:00
end
2013-07-26 08:07:24 +00:00
end