2013-03-22 03:18:41 +00:00
|
|
|
module JamRuby
|
|
|
|
|
class Notification < ActiveRecord::Base
|
|
|
|
|
|
2013-04-04 17:12:06 +00:00
|
|
|
self.primary_key = 'id'
|
|
|
|
|
|
|
|
|
|
default_scope order('created_at DESC')
|
|
|
|
|
|
|
|
|
|
belongs_to :target_user, :class_name => "JamRuby::User", :foreign_key => "target_user_id"
|
|
|
|
|
belongs_to :source_user, :class_name => "JamRuby::User", :foreign_key => "source_user_id"
|
|
|
|
|
belongs_to :band, :class_name => "JamRuby::Band", :foreign_key => "band_id"
|
|
|
|
|
belongs_to :session, :class_name => "JamRuby::MusicSession", :foreign_key => "session_id"
|
|
|
|
|
belongs_to :recording, :class_name => "JamRuby::Recording", :foreign_key => "recording_id"
|
|
|
|
|
|
2013-03-31 18:07:46 +00:00
|
|
|
def index(user_id)
|
|
|
|
|
results = Notification.where(:target_user_id => user_id).limit(50)
|
|
|
|
|
return results
|
|
|
|
|
end
|
|
|
|
|
|
2013-04-04 17:12:06 +00:00
|
|
|
def photo_url
|
2013-04-07 01:43:00 +00:00
|
|
|
unless self.source_user.nil?
|
|
|
|
|
self.source_user.photo_url
|
|
|
|
|
end
|
2013-04-04 17:12:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# used for persisted notifications
|
2013-03-31 18:07:46 +00:00
|
|
|
def formatted_msg
|
|
|
|
|
target_user, source_user, band, session, recording, invitation, join_request = nil
|
|
|
|
|
|
|
|
|
|
unless self.target_user_id.nil?
|
|
|
|
|
target_user = User.find(self.target_user_id)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
unless self.source_user_id.nil?
|
|
|
|
|
source_user = User.find(self.source_user_id)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
unless self.band_id.nil?
|
|
|
|
|
band = Band.find(self.band_id)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
unless self.session_id.nil?
|
|
|
|
|
session = MusicSession.find(self.session_id)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
unless self.recording_id.nil?
|
|
|
|
|
recording = Recording.find(self.recording_id)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
unless self.invitation_id.nil?
|
|
|
|
|
invitation = Invitation.find(self.invitation_id)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
unless self.join_request_id.nil?
|
|
|
|
|
join_request = JoinRequest.find(self.join_request_id)
|
|
|
|
|
end
|
2013-04-04 17:12:06 +00:00
|
|
|
|
|
|
|
|
return self.class.format_msg(self.description, source_user)
|
2013-03-31 18:07:46 +00:00
|
|
|
end
|
|
|
|
|
|
2013-03-22 03:18:41 +00:00
|
|
|
# TODO: MAKE ALL METHODS BELOW ASYNC SO THE CLIENT DOESN'T BLOCK ON NOTIFICATION LOGIC
|
|
|
|
|
# TODO: ADD TESTS FOR THIS CLASS
|
|
|
|
|
|
|
|
|
|
class << self
|
|
|
|
|
|
|
|
|
|
@@mq_router = MQRouter.new
|
|
|
|
|
@@message_factory = MessageFactory.new
|
|
|
|
|
|
2013-03-31 18:07:46 +00:00
|
|
|
def delete_all(session_id)
|
|
|
|
|
Notification.delete_all "(session_id = '#{session_id}')"
|
2013-03-23 07:50:01 +00:00
|
|
|
end
|
|
|
|
|
|
2013-03-22 03:18:41 +00:00
|
|
|
################### HELPERS ###################
|
|
|
|
|
def retrieve_friends(connection, user_id)
|
|
|
|
|
friend_ids = []
|
|
|
|
|
connection.exec("SELECT f.friend_id as friend_id FROM friendships f WHERE f.user_id = $1", [user_id]) do |friend_results|
|
|
|
|
|
friend_results.each do |friend_result|
|
|
|
|
|
friend_ids.push(friend_result['friend_id'])
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return friend_ids
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def retrieve_followers(connection, user_id)
|
|
|
|
|
follower_ids = []
|
|
|
|
|
connection.exec("SELECT uf.follower_id as friend_id FROM users_followers uf WHERE uf.user_id = $1", [user_id]) do |follower_results|
|
|
|
|
|
follower_results.each do |follower_result|
|
|
|
|
|
follower_ids.push(follower_result['follower_id'])
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return follower_ids
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def retrieve_friends_and_followers(connection, user_id)
|
|
|
|
|
ids = retrieve_friends(connection, user_id)
|
|
|
|
|
ids.concat(retrieve_followers(connection, user_id))
|
2013-06-26 03:10:21 +00:00
|
|
|
ids.uniq! {|id| id}
|
|
|
|
|
return ids
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def retrieve_friends_and_followers_not_in_session(connection, user_id, session_id)
|
|
|
|
|
ids = retrieve_friends_and_followers(connection, user_id)
|
|
|
|
|
connection.exec("SELECT c.user_id as musician_id FROM connections c WHERE c.music_session_id = $1", [session_id]) do |musicians|
|
|
|
|
|
musicians.each do |musician_result|
|
|
|
|
|
# remove users who are in the session
|
|
|
|
|
ids.reject! {|item| item == musician_result['musician_id']}
|
|
|
|
|
end
|
|
|
|
|
end
|
2013-03-22 03:18:41 +00:00
|
|
|
return ids
|
|
|
|
|
end
|
|
|
|
|
|
2013-10-16 07:23:43 +00:00
|
|
|
def format_msg(description, user = nil)
|
2013-10-13 23:57:37 +00:00
|
|
|
name = ""
|
|
|
|
|
unless user.nil?
|
|
|
|
|
name = user.name
|
|
|
|
|
else
|
|
|
|
|
name = "Someone"
|
|
|
|
|
end
|
|
|
|
|
|
2013-04-04 17:12:06 +00:00
|
|
|
case description
|
2013-03-31 21:49:47 +00:00
|
|
|
when NotificationTypes::FRIEND_UPDATE
|
2013-10-13 23:57:37 +00:00
|
|
|
return "#{name} is now "
|
2013-03-31 18:07:46 +00:00
|
|
|
|
2013-03-31 21:49:47 +00:00
|
|
|
when NotificationTypes::FRIEND_REQUEST
|
2013-10-13 23:57:37 +00:00
|
|
|
return "#{name} has sent you a friend request."
|
2013-03-31 18:07:46 +00:00
|
|
|
|
2013-03-31 21:49:47 +00:00
|
|
|
when NotificationTypes::FRIEND_REQUEST_ACCEPTED
|
2013-10-13 23:57:37 +00:00
|
|
|
return "#{name} has accepted your friend request."
|
2013-03-31 18:07:46 +00:00
|
|
|
|
2013-06-26 03:10:21 +00:00
|
|
|
when NotificationTypes::FRIEND_SESSION_JOIN
|
2013-10-13 23:57:37 +00:00
|
|
|
return "#{name} has joined the session."
|
2013-06-26 03:10:21 +00:00
|
|
|
|
|
|
|
|
when NotificationTypes::MUSICIAN_SESSION_JOIN
|
2013-10-13 23:57:37 +00:00
|
|
|
return "#{name} has joined the session."
|
2013-06-26 03:10:21 +00:00
|
|
|
|
|
|
|
|
when NotificationTypes::MUSICIAN_SESSION_DEPART
|
2013-10-13 23:57:37 +00:00
|
|
|
return "#{name} has left the session."
|
2013-06-26 03:10:21 +00:00
|
|
|
|
2013-10-03 07:16:27 +00:00
|
|
|
when NotificationTypes::SESSION_INVITATION
|
2013-10-13 23:57:37 +00:00
|
|
|
return "#{name} has invited you to a session."
|
2013-10-03 07:16:27 +00:00
|
|
|
|
2013-10-16 07:23:43 +00:00
|
|
|
when NotificationTypes::JOIN_REQUEST
|
|
|
|
|
return "#{name} has requested to join your session."
|
|
|
|
|
|
|
|
|
|
when NotificationTypes::JOIN_REQUEST_APPROVED
|
|
|
|
|
return "#{name} has approved your request to join the session."
|
|
|
|
|
|
|
|
|
|
when NotificationTypes::JOIN_REQUEST_REJECTED
|
|
|
|
|
return "We're sorry, but you cannot join the session at this time."
|
|
|
|
|
|
2013-03-31 21:49:47 +00:00
|
|
|
# when "social_media_friend_joined"
|
|
|
|
|
# when "band_invitation"
|
|
|
|
|
# when "band_invitation_accepted"
|
|
|
|
|
# when "recording_available"
|
2013-06-26 03:10:21 +00:00
|
|
|
else
|
|
|
|
|
return ""
|
2013-03-31 18:07:46 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2013-03-22 03:18:41 +00:00
|
|
|
################### FRIEND UPDATE ###################
|
|
|
|
|
def send_friend_update(user_id, online, connection)
|
|
|
|
|
|
2013-06-26 03:10:21 +00:00
|
|
|
# (1) get all of this user's friends
|
2013-03-22 03:18:41 +00:00
|
|
|
friend_ids = retrieve_friends(connection, user_id)
|
|
|
|
|
|
2013-06-26 03:10:21 +00:00
|
|
|
if friend_ids.length > 0
|
|
|
|
|
user = User.find(user_id)
|
|
|
|
|
|
|
|
|
|
# (2) create notification
|
|
|
|
|
online_msg = online ? "online." : "offline."
|
|
|
|
|
notification_msg = format_msg(NotificationTypes::FRIEND_UPDATE, user) + online_msg
|
|
|
|
|
msg = @@message_factory.friend_update(user_id, user.name, user.photo_url, online, notification_msg)
|
|
|
|
|
|
|
|
|
|
# (3) send notification
|
|
|
|
|
@@mq_router.publish_to_friends(friend_ids, msg, user_id)
|
|
|
|
|
end
|
2013-03-22 03:18:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
################### FRIEND REQUEST ###################
|
2013-04-14 02:58:31 +00:00
|
|
|
def send_friend_request(friend_request_id, user_id, friend_id)
|
2013-03-22 03:18:41 +00:00
|
|
|
user = User.find(user_id)
|
|
|
|
|
|
2013-04-04 17:12:06 +00:00
|
|
|
# (1) save to database
|
2013-03-31 18:07:46 +00:00
|
|
|
notification = Notification.new
|
2013-04-04 17:12:06 +00:00
|
|
|
notification.description = NotificationTypes::FRIEND_REQUEST
|
2013-03-31 18:07:46 +00:00
|
|
|
notification.source_user_id = user_id
|
|
|
|
|
notification.target_user_id = friend_id
|
2013-04-14 02:58:31 +00:00
|
|
|
notification.friend_request_id = friend_request_id
|
2013-03-31 18:07:46 +00:00
|
|
|
notification.save
|
2013-04-04 17:12:06 +00:00
|
|
|
|
|
|
|
|
# (2) create notification
|
2013-10-16 07:23:43 +00:00
|
|
|
notification_msg = format_msg(notification.description, user)
|
2013-04-14 02:58:31 +00:00
|
|
|
msg = @@message_factory.friend_request(friend_request_id, user_id, user.name, user.photo_url, friend_id, notification_msg, notification.id, notification.created_at.to_s)
|
2013-04-04 17:12:06 +00:00
|
|
|
|
|
|
|
|
# (3) send notification
|
|
|
|
|
@@mq_router.publish_to_user(friend_id, msg)
|
2013-03-22 03:18:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
############### FRIEND REQUEST ACCEPTED ###############
|
|
|
|
|
def send_friend_request_accepted(user_id, friend_id)
|
|
|
|
|
friend = User.find(friend_id)
|
|
|
|
|
|
2013-04-04 17:12:06 +00:00
|
|
|
# (1) save to database
|
2013-03-31 18:07:46 +00:00
|
|
|
notification = Notification.new
|
2013-04-04 17:12:06 +00:00
|
|
|
notification.description = NotificationTypes::FRIEND_REQUEST_ACCEPTED
|
2013-03-31 18:07:46 +00:00
|
|
|
notification.source_user_id = friend_id
|
|
|
|
|
notification.target_user_id = user_id
|
|
|
|
|
notification.save
|
2013-04-04 17:12:06 +00:00
|
|
|
|
|
|
|
|
# (2) create notification
|
2013-10-16 07:23:43 +00:00
|
|
|
notification_msg = format_msg(notification.description, friend)
|
2013-04-04 17:12:06 +00:00
|
|
|
msg = @@message_factory.friend_request_accepted(friend_id, friend.name, friend.photo_url, user_id, notification_msg, notification.id, notification.created_at.to_s)
|
|
|
|
|
|
|
|
|
|
# (3) send notification
|
|
|
|
|
@@mq_router.publish_to_user(user_id, msg)
|
2013-03-22 03:18:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
################## SESSION INVITATION ##################
|
2013-10-05 13:04:07 +00:00
|
|
|
def send_session_invitation(receiver_id, sender, session_id)
|
2013-03-22 03:18:41 +00:00
|
|
|
|
2013-04-04 17:12:06 +00:00
|
|
|
# (1) save to database
|
|
|
|
|
notification = Notification.new
|
2013-04-14 02:58:31 +00:00
|
|
|
notification.description = NotificationTypes::SESSION_INVITATION
|
2013-10-05 13:04:07 +00:00
|
|
|
notification.source_user_id = sender.id
|
2013-04-04 17:12:06 +00:00
|
|
|
notification.target_user_id = receiver_id
|
2013-10-03 07:16:27 +00:00
|
|
|
notification.session_id = session_id
|
2013-04-05 03:57:16 +00:00
|
|
|
notification.save
|
2013-04-04 17:12:06 +00:00
|
|
|
|
|
|
|
|
# (2) create notification
|
2013-10-05 13:04:07 +00:00
|
|
|
msg = @@message_factory.session_invitation(receiver_id, sender.name, session_id, notification.id, notification.created_at.to_s)
|
2013-10-03 07:16:27 +00:00
|
|
|
|
2013-04-04 17:12:06 +00:00
|
|
|
# (3) send notification
|
2013-03-22 03:18:41 +00:00
|
|
|
@@mq_router.publish_to_user(receiver_id, msg)
|
|
|
|
|
end
|
|
|
|
|
|
2013-06-26 03:10:21 +00:00
|
|
|
def send_musician_session_join(music_session, connection, user)
|
2013-03-22 03:18:41 +00:00
|
|
|
|
|
|
|
|
# (1) create notification
|
2013-06-26 03:10:21 +00:00
|
|
|
msg = @@message_factory.musician_session_join(music_session.id, user.id, user.name, user.photo_url)
|
2013-03-22 03:18:41 +00:00
|
|
|
|
|
|
|
|
# (2) send notification
|
|
|
|
|
@@mq_router.server_publish_to_session(music_session, msg, sender = {:client_id => connection.client_id})
|
|
|
|
|
end
|
|
|
|
|
|
2013-08-07 15:35:27 +00:00
|
|
|
def send_musician_session_depart(music_session, client_id, user)
|
2013-03-22 03:18:41 +00:00
|
|
|
|
2013-06-26 03:10:21 +00:00
|
|
|
# (1) create notification
|
|
|
|
|
msg = @@message_factory.musician_session_depart(music_session.id, user.id, user.name, user.photo_url)
|
2013-04-04 17:12:06 +00:00
|
|
|
|
2013-06-26 03:10:21 +00:00
|
|
|
# (2) send notification
|
2013-08-07 15:35:27 +00:00
|
|
|
@@mq_router.server_publish_to_session(music_session, msg, sender = {:client_id => client_id})
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def send_musician_session_fresh(music_session, client_id, user)
|
|
|
|
|
|
|
|
|
|
# (1) create notification
|
|
|
|
|
msg = @@message_factory.musician_session_fresh(music_session.id, user.id, user.name, user.photo_url)
|
|
|
|
|
|
|
|
|
|
# (2) send notification
|
|
|
|
|
@@mq_router.server_publish_to_session(music_session, msg, sender = {:client_id => client_id})
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def send_musician_session_stale(music_session, client_id, user)
|
|
|
|
|
|
|
|
|
|
# (1) create notification
|
|
|
|
|
msg = @@message_factory.musician_session_stale(music_session.id, user.id, user.name, user.photo_url)
|
|
|
|
|
|
|
|
|
|
# (2) send notification
|
|
|
|
|
@@mq_router.server_publish_to_session(music_session, msg, sender = {:client_id => client_id})
|
2013-06-26 03:10:21 +00:00
|
|
|
end
|
2013-03-22 03:18:41 +00:00
|
|
|
|
2013-06-26 03:10:21 +00:00
|
|
|
def send_friend_session_join(db_conn, connection, user)
|
|
|
|
|
ids = retrieve_friends_and_followers_not_in_session(db_conn, user.id, connection.music_session.id)
|
|
|
|
|
|
|
|
|
|
if ids.length > 0
|
|
|
|
|
# (1) save to database
|
|
|
|
|
|
|
|
|
|
# (2) create notification
|
|
|
|
|
msg = @@message_factory.friend_session_join(connection.music_session.id, user.id, user.name, user.photo_url)
|
|
|
|
|
|
|
|
|
|
# (3) send notification
|
|
|
|
|
@@mq_router.publish_to_friends(ids, msg, sender = {:client_id => connection.client_id})
|
|
|
|
|
end
|
2013-03-22 03:18:41 +00:00
|
|
|
end
|
|
|
|
|
|
2013-10-16 07:23:43 +00:00
|
|
|
def send_join_request(music_session, join_request, text)
|
|
|
|
|
|
|
|
|
|
# (1) save to database
|
|
|
|
|
notification = Notification.new
|
|
|
|
|
notification.description = NotificationTypes::JOIN_REQUEST
|
|
|
|
|
notification.source_user_id = join_request.user.id
|
|
|
|
|
notification.target_user_id = music_session.creator.id
|
|
|
|
|
notification.session_id = music_session.id
|
|
|
|
|
notification.save
|
|
|
|
|
|
|
|
|
|
# (2) create notification
|
|
|
|
|
notification_msg = format_msg(notification.description, join_request.user)
|
|
|
|
|
msg = @@message_factory.join_request(join_request.id, music_session.id, join_request.user.name, join_request.user.photo_url, notification_msg, notification.id, notification.created_at.to_s)
|
|
|
|
|
|
|
|
|
|
# (3) send notification
|
|
|
|
|
@@mq_router.publish_to_user(music_session.creator.id, msg)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def send_join_request_approved(music_session, join_request)
|
2013-03-22 03:18:41 +00:00
|
|
|
|
2013-04-04 17:12:06 +00:00
|
|
|
# (1) save to database
|
2013-10-16 07:23:43 +00:00
|
|
|
notification = Notification.new
|
|
|
|
|
notification.description = NotificationTypes::JOIN_REQUEST_APPROVED
|
|
|
|
|
notification.source_user_id = music_session.creator.id
|
|
|
|
|
notification.target_user_id = join_request.user.id
|
|
|
|
|
notification.session_id = music_session.id
|
|
|
|
|
notification.save
|
|
|
|
|
|
|
|
|
|
# (2) create notification
|
|
|
|
|
notification_msg = format_msg(notification.description, music_session.creator.name)
|
|
|
|
|
msg = @@message_factory.join_request_approved(join_request.id, music_session.id, music_session.creator.name, music_session.creator.photo_url, notification_msg, notification.id, notification.created_at.to_s)
|
|
|
|
|
|
|
|
|
|
# (3) send notification
|
|
|
|
|
@@mq_router.publish_to_user(join_request.user.id, msg)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def send_join_request_rejected(music_session, join_request)
|
|
|
|
|
|
|
|
|
|
# (1) save to database
|
|
|
|
|
notification = Notification.new
|
|
|
|
|
notification.description = NotificationTypes::JOIN_REQUEST_REJECTED
|
|
|
|
|
notification.source_user_id = music_session.creator.id
|
|
|
|
|
notification.target_user_id = join_request.user.id
|
|
|
|
|
notification.session_id = music_session.id
|
|
|
|
|
notification.save
|
2013-04-04 17:12:06 +00:00
|
|
|
|
|
|
|
|
# (2) create notification
|
2013-10-16 07:23:43 +00:00
|
|
|
notification_msg = format_msg(notification.description, music_session.creator.name)
|
|
|
|
|
msg = @@message_factory.join_request_rejected(join_request.id, music_session.id, music_session.creator.name, music_session.creator.photo_url, notification_msg, notification.id, notification.created_at.to_s)
|
2013-03-22 03:18:41 +00:00
|
|
|
|
2013-06-26 03:10:21 +00:00
|
|
|
# (3) send notification
|
2013-10-16 07:23:43 +00:00
|
|
|
@@mq_router.publish_to_user(join_request.user.id, msg)
|
2013-03-22 03:18:41 +00:00
|
|
|
end
|
2013-06-26 03:10:21 +00:00
|
|
|
|
2013-03-22 03:18:41 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|