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
|
2013-11-27 05:57:49 +00:00
|
|
|
# target_user, band, session, recording, invitation, join_request = nil
|
|
|
|
|
source_user = nil
|
2013-03-31 18:07:46 +00:00
|
|
|
|
2013-11-27 05:57:49 +00:00
|
|
|
# unless self.target_user_id.nil?
|
|
|
|
|
# target_user = User.find(self.target_user_id)
|
|
|
|
|
# end
|
2013-03-31 18:07:46 +00:00
|
|
|
|
|
|
|
|
unless self.source_user_id.nil?
|
|
|
|
|
source_user = User.find(self.source_user_id)
|
|
|
|
|
end
|
|
|
|
|
|
2013-11-27 05:57:49 +00:00
|
|
|
# unless self.band_id.nil?
|
|
|
|
|
# band = Band.find(self.band_id)
|
|
|
|
|
# end
|
2013-03-31 18:07:46 +00:00
|
|
|
|
2013-11-27 05:57:49 +00:00
|
|
|
# unless self.session_id.nil?
|
|
|
|
|
# session = MusicSession.find(self.session_id)
|
|
|
|
|
# end
|
2013-03-31 18:07:46 +00:00
|
|
|
|
2013-11-27 05:57:49 +00:00
|
|
|
# unless self.recording_id.nil?
|
|
|
|
|
# recording = Recording.find(self.recording_id)
|
|
|
|
|
# end
|
2013-03-31 18:07:46 +00:00
|
|
|
|
2013-11-27 05:57:49 +00:00
|
|
|
# unless self.invitation_id.nil?
|
|
|
|
|
# invitation = Invitation.find(self.invitation_id)
|
|
|
|
|
# end
|
2013-03-31 18:07:46 +00:00
|
|
|
|
2013-11-27 05:57:49 +00:00
|
|
|
# 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
|
|
|
|
|
|
2013-12-29 04:51:35 +00:00
|
|
|
def retrieve_user_followers(connection, user_id)
|
2013-03-22 03:18:41 +00:00
|
|
|
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)
|
2013-12-29 04:51:35 +00:00
|
|
|
ids.concat(retrieve_user_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-12-29 04:51:35 +00:00
|
|
|
def retrieve_band_followers(connection, band_id)
|
|
|
|
|
follower_ids = []
|
|
|
|
|
connection.exec("SELECT bf.follower_id as follower_id FROM bands_followers bf WHERE bf.band_id = $1", [band_id]) do |follower_results|
|
|
|
|
|
follower_results.each do |follower_result|
|
|
|
|
|
follower_ids.push(follower_result['follower_id'])
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return follower_ids
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def format_msg(description, user = nil, band = 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-12-29 04:51:35 +00:00
|
|
|
|
|
|
|
|
# friend notifications
|
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
|
|
|
|
2013-12-29 04:51:35 +00:00
|
|
|
when NotificationTypes::NEW_USER_FOLLOWER
|
|
|
|
|
return "#{name} is now following you on JamKazam."
|
2013-06-26 03:10:21 +00:00
|
|
|
|
2013-12-29 04:51:35 +00:00
|
|
|
when NotificationTypes::NEW_BAND_FOLLOWER
|
|
|
|
|
return "#{name} is now following your band #{band.name} on JamKazam."
|
2013-06-26 03:10:21 +00:00
|
|
|
|
2013-12-29 04:51:35 +00:00
|
|
|
# session notifications
|
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-12-29 04:51:35 +00:00
|
|
|
|
|
|
|
|
# musician notifications
|
|
|
|
|
when NotificationTypes::MUSICIAN_SESSION_JOIN
|
|
|
|
|
return "#{name} has joined the session."
|
|
|
|
|
|
|
|
|
|
when NotificationTypes::MUSICIAN_SESSION_DEPART
|
|
|
|
|
return "#{name} has left the session."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# recording notifications
|
|
|
|
|
when NotificationTypes::MUSICIAN_RECORDING_SAVED
|
|
|
|
|
return "#{name} has made a new recording."
|
|
|
|
|
|
|
|
|
|
when NotificationTypes::BAND_RECORDING_SAVED
|
|
|
|
|
return "#{name} has made a new recording."
|
|
|
|
|
|
|
|
|
|
when NotificationTypes::RECORDING_STARTED
|
|
|
|
|
return "#{name} has started a recording."
|
|
|
|
|
|
|
|
|
|
when NotificationTypes::RECORDING_ENDED
|
|
|
|
|
return "#{name} has stopped recording."
|
|
|
|
|
|
|
|
|
|
when NotificationTypes::RECORDING_MASTER_MIX_COMPLETE
|
|
|
|
|
return "This recording has been mastered and mixed and is ready to share."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# band notifications
|
2013-11-26 07:47:56 +00:00
|
|
|
when NotificationTypes::BAND_INVITATION
|
|
|
|
|
return "You have been invited to join the band #{name}."
|
|
|
|
|
|
|
|
|
|
when NotificationTypes::BAND_INVITATION_ACCEPTED
|
|
|
|
|
return "#{name} has accepted your band invitation."
|
|
|
|
|
|
2013-12-29 04:51:35 +00:00
|
|
|
when NotificationTypes::BAND_SESSION_JOIN
|
|
|
|
|
return "#{name} is now in a session."
|
|
|
|
|
|
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-11-26 07:47:56 +00:00
|
|
|
unless friend_ids.blank?
|
2013-06-26 03:10:21 +00:00
|
|
|
user = User.find(user_id)
|
|
|
|
|
|
|
|
|
|
# (2) create notification
|
|
|
|
|
online_msg = online ? "online." : "offline."
|
|
|
|
|
notification_msg = format_msg(NotificationTypes::FRIEND_UPDATE, user) + online_msg
|
2013-11-26 07:47:56 +00:00
|
|
|
msg = @@message_factory.friend_update(
|
|
|
|
|
user_id, user.name,
|
|
|
|
|
user.photo_url,
|
|
|
|
|
online,
|
|
|
|
|
notification_msg
|
|
|
|
|
)
|
2013-06-26 03:10:21 +00:00
|
|
|
|
|
|
|
|
# (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-12-30 01:49:44 +00:00
|
|
|
friend = User.find(friend_id)
|
2013-03-22 03:18:41 +00:00
|
|
|
|
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-12-30 01:49:44 +00:00
|
|
|
if user.online
|
|
|
|
|
notification_msg = format_msg(notification.description, user)
|
|
|
|
|
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
|
|
|
|
2013-12-30 01:49:44 +00:00
|
|
|
# (2a) send notification if user is online
|
|
|
|
|
@@mq_router.publish_to_user(friend_id, msg)
|
|
|
|
|
else
|
|
|
|
|
# (2b) send email if user is offline
|
|
|
|
|
UserMailer.friend_request(user, friend)
|
|
|
|
|
end
|
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-12-30 01:49:44 +00:00
|
|
|
user = User.find(user_id)
|
2013-03-22 03:18:41 +00:00
|
|
|
|
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-12-30 01:49:44 +00:00
|
|
|
if friend.online
|
|
|
|
|
notification_msg = format_msg(notification.description, friend)
|
|
|
|
|
msg = @@message_factory.friend_request_accepted(
|
|
|
|
|
friend_id,
|
|
|
|
|
friend.name,
|
|
|
|
|
friend.photo_url,
|
|
|
|
|
user_id, notification_msg,
|
|
|
|
|
notification.id,
|
|
|
|
|
notification.created_at.to_s
|
|
|
|
|
)
|
2013-04-04 17:12:06 +00:00
|
|
|
|
2013-12-30 01:49:44 +00:00
|
|
|
# (3) send notification
|
|
|
|
|
@@mq_router.publish_to_user(user_id, msg)
|
|
|
|
|
else
|
|
|
|
|
# (2b) send email if user is offline
|
|
|
|
|
UserMailer.friend_request(friend, user)
|
|
|
|
|
end
|
2013-03-22 03:18:41 +00:00
|
|
|
end
|
|
|
|
|
|
2013-12-29 04:51:35 +00:00
|
|
|
################## FRIEND SESSION JOIN ##################
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
unless ids.empty?
|
|
|
|
|
# (1) bulk save to database
|
|
|
|
|
notifications = []
|
|
|
|
|
ids.each do |id|
|
|
|
|
|
notification = Notification.new
|
|
|
|
|
notification.description = NotificationTypes::FRIEND_SESSION_JOIN
|
|
|
|
|
notification.source_user_id = user.id
|
|
|
|
|
notification.target_user_id = id
|
|
|
|
|
notifications << notification
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
Notification.import notifications
|
|
|
|
|
|
|
|
|
|
# (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
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
############### NEW FOLLOWER ###############
|
|
|
|
|
def send_new_user_follower(follower, user)
|
2013-03-22 03:18:41 +00:00
|
|
|
|
2013-04-04 17:12:06 +00:00
|
|
|
# (1) save to database
|
|
|
|
|
notification = Notification.new
|
2013-12-29 04:51:35 +00:00
|
|
|
notification.description = NotificationTypes::NEW_USER_FOLLOWER
|
2013-12-29 19:40:21 +00:00
|
|
|
notification.source_user_id = follower.id
|
|
|
|
|
notification.target_user_id = user.id
|
2013-04-05 03:57:16 +00:00
|
|
|
notification.save
|
2013-04-04 17:12:06 +00:00
|
|
|
|
|
|
|
|
# (2) create notification
|
2013-12-30 01:49:44 +00:00
|
|
|
if user.online
|
|
|
|
|
notification_msg = format_msg(notification.description, follower)
|
2013-10-03 07:16:27 +00:00
|
|
|
|
2013-12-30 01:49:44 +00:00
|
|
|
msg = @@message_factory.new_user_follower(
|
|
|
|
|
user.id,
|
|
|
|
|
follower.name,
|
|
|
|
|
follower.photo_url,
|
|
|
|
|
notification_msg,
|
|
|
|
|
notification.id,
|
|
|
|
|
notification.created_at.to_s
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# (2a) send notification if user is online
|
|
|
|
|
@@mq_router.publish_to_user(user.id, msg)
|
|
|
|
|
else
|
|
|
|
|
# (2b) send email if user is offline
|
|
|
|
|
UserMailer.new_user_follower(follower, user)
|
|
|
|
|
end
|
2013-03-22 03:18:41 +00:00
|
|
|
end
|
|
|
|
|
|
2013-12-29 04:51:35 +00:00
|
|
|
def send_new_band_follower(follower, band)
|
2013-12-29 19:40:21 +00:00
|
|
|
|
2013-12-30 01:49:44 +00:00
|
|
|
notifications = []
|
2013-12-29 19:40:21 +00:00
|
|
|
notification_msg = format_msg(notification.description, follower, band)
|
|
|
|
|
|
2013-12-30 01:49:44 +00:00
|
|
|
band.band_musicians.each.each do |bm|
|
|
|
|
|
|
|
|
|
|
# create notifications (saved below in bulk)
|
|
|
|
|
notification = Notification.new
|
|
|
|
|
notification.description = NotificationTypes::NEW_BAND_FOLLOWER
|
|
|
|
|
notification.source_user_id = follower.id
|
|
|
|
|
notification.target_user_id = bm.user.id
|
|
|
|
|
notifications << notification
|
|
|
|
|
|
|
|
|
|
# send notification
|
|
|
|
|
if bm.user.online
|
|
|
|
|
msg = @@message_factory.new_user_follower(
|
|
|
|
|
bm.user_id,
|
|
|
|
|
follower.name,
|
|
|
|
|
follower.photo_url,
|
|
|
|
|
notification_msg,
|
|
|
|
|
notification.id,
|
|
|
|
|
notification.created_at.to_s
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# (2a) send notification if user is online
|
|
|
|
|
@@mq_router.publish_to_user(user.id, msg)
|
|
|
|
|
else
|
|
|
|
|
# (2b) send email if user is offline
|
|
|
|
|
UserMailer.new_band_follower(follower, user, band)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
Notification.import notifications
|
2013-08-07 15:35:27 +00:00
|
|
|
end
|
|
|
|
|
|
2013-12-29 04:51:35 +00:00
|
|
|
################## SESSION INVITATION ##################
|
|
|
|
|
def send_session_invitation(receiver, sender, session_id)
|
2013-08-07 15:35:27 +00:00
|
|
|
|
2013-12-29 04:51:35 +00:00
|
|
|
# (1) save to database
|
|
|
|
|
notification = Notification.new
|
|
|
|
|
notification.description = NotificationTypes::SESSION_INVITATION
|
|
|
|
|
notification.source_user_id = sender.id
|
|
|
|
|
notification.target_user_id = receiver.id
|
|
|
|
|
notification.session_id = session_id
|
|
|
|
|
notification.save
|
2013-08-07 15:35:27 +00:00
|
|
|
|
2013-12-29 04:51:35 +00:00
|
|
|
# (2) create notification
|
|
|
|
|
msg = @@message_factory.session_invitation(
|
2013-12-30 01:49:44 +00:00
|
|
|
receiver.id,
|
2013-12-29 04:51:35 +00:00
|
|
|
sender.name,
|
|
|
|
|
session_id,
|
|
|
|
|
notification.id,
|
|
|
|
|
notification.created_at.to_s
|
2013-11-26 07:47:56 +00:00
|
|
|
)
|
2013-08-07 15:35:27 +00:00
|
|
|
|
2013-12-29 04:51:35 +00:00
|
|
|
# (3) send notification
|
|
|
|
|
@@mq_router.publish_to_user(receiver.id, msg)
|
2013-06-26 03:10:21 +00:00
|
|
|
end
|
2013-03-22 03:18:41 +00:00
|
|
|
|
2013-12-29 04:51:35 +00:00
|
|
|
################## SESSION ENDED ##################
|
|
|
|
|
def send_session_ended(music_session, connection)
|
2013-06-26 03:10:21 +00:00
|
|
|
|
2013-12-29 04:51:35 +00:00
|
|
|
# TODO: this should actually publish to all users who have a notification for this session
|
|
|
|
|
@@mq_router.server_publish_to_session(music_session, nil, sender = {:client_id => connection.client_id})
|
2013-03-22 03:18:41 +00:00
|
|
|
end
|
|
|
|
|
|
2013-11-26 07:47:56 +00:00
|
|
|
################## JOIN REQUEST ##################
|
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)
|
2013-11-26 07:47:56 +00:00
|
|
|
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
|
|
|
|
|
)
|
2013-10-16 07:23:43 +00:00
|
|
|
|
|
|
|
|
# (3) send notification
|
|
|
|
|
@@mq_router.publish_to_user(music_session.creator.id, msg)
|
|
|
|
|
end
|
|
|
|
|
|
2013-11-26 07:47:56 +00:00
|
|
|
################## JOIN REQUEST APPROVED ##################
|
2013-10-16 07:23:43 +00:00
|
|
|
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
|
2013-10-16 08:07:30 +00:00
|
|
|
notification_msg = format_msg(notification.description, music_session.creator)
|
2013-11-26 07:47:56 +00:00
|
|
|
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
|
|
|
|
|
)
|
2013-10-16 07:23:43 +00:00
|
|
|
|
|
|
|
|
# (3) send notification
|
|
|
|
|
@@mq_router.publish_to_user(join_request.user.id, msg)
|
|
|
|
|
end
|
|
|
|
|
|
2013-11-26 07:47:56 +00:00
|
|
|
################## JOIN REQUEST REJECTED ##################
|
2013-10-16 07:23:43 +00:00
|
|
|
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 08:07:30 +00:00
|
|
|
notification_msg = format_msg(notification.description, music_session.creator)
|
2013-11-26 07:47:56 +00:00
|
|
|
msg = @@message_factory.join_request_rejected(
|
|
|
|
|
join_request.id,
|
|
|
|
|
music_session.id,
|
|
|
|
|
music_session.creator.name,
|
|
|
|
|
music_session.creator.photo_url,
|
|
|
|
|
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-12-29 04:51:35 +00:00
|
|
|
################## MUSICIAN SESSION JOIN ##################
|
|
|
|
|
def send_musician_session_join(music_session, connection, user)
|
|
|
|
|
|
|
|
|
|
# (1) create notification
|
|
|
|
|
msg = @@message_factory.musician_session_join(
|
|
|
|
|
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 => connection.client_id})
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
################## MUSICIAN SESSION DEPART ##################
|
|
|
|
|
def send_musician_session_depart(music_session, client_id, user, recordingId = nil)
|
|
|
|
|
|
|
|
|
|
# (1) create notification
|
|
|
|
|
msg = @@message_factory.musician_session_depart(
|
|
|
|
|
music_session.id,
|
|
|
|
|
user.id,
|
|
|
|
|
user.name,
|
|
|
|
|
user.photo_url,
|
|
|
|
|
recordingId
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# (2) send notification
|
|
|
|
|
@@mq_router.server_publish_to_session(music_session, msg, sender = {:client_id => client_id})
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
################## MUSICIAN RECORDING SAVED ##################
|
|
|
|
|
def send_musician_recording_saved(recording, user)
|
|
|
|
|
|
|
|
|
|
ids = retrieve_friends_and_followers(connection, user_id)
|
|
|
|
|
|
|
|
|
|
# (1) save to database
|
|
|
|
|
notification = Notification.new
|
|
|
|
|
notification.description = NotificationTypes::MUSICIAN_RECORDING_SAVED
|
|
|
|
|
notification.source_user_id = user.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, user)
|
|
|
|
|
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_friends(ids, msg, sender = {:client_id => connection.client_id})
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
################## BAND RECORDING SAVED ##################
|
|
|
|
|
|
|
|
|
|
################## RECORDING STARTED ##################
|
|
|
|
|
|
|
|
|
|
################## RECORDING ENDED ##################
|
|
|
|
|
|
|
|
|
|
################## RECORDING MASTER MIX COMPLETE ##################
|
|
|
|
|
|
2013-11-26 07:47:56 +00:00
|
|
|
################## BAND INVITATION ##################
|
|
|
|
|
def send_band_invitation(band, band_invitation, sender, receiver)
|
|
|
|
|
|
|
|
|
|
# (1) save to database
|
|
|
|
|
notification = Notification.new
|
|
|
|
|
notification.band_id = band.id
|
|
|
|
|
notification.band_invitation_id = band_invitation.id
|
|
|
|
|
notification.description = NotificationTypes::BAND_INVITATION
|
|
|
|
|
notification.source_user_id = sender.id
|
|
|
|
|
notification.target_user_id = receiver.id
|
|
|
|
|
notification.save
|
|
|
|
|
|
|
|
|
|
# (2) create notification
|
|
|
|
|
notification_msg = format_msg(notification.description, band)
|
|
|
|
|
msg = @@message_factory.band_invitation(
|
|
|
|
|
band_invitation.id,
|
|
|
|
|
band.id,
|
|
|
|
|
receiver.id,
|
|
|
|
|
sender.name,
|
|
|
|
|
sender.photo_url,
|
|
|
|
|
band.name,
|
|
|
|
|
notification_msg,
|
|
|
|
|
notification.id,
|
|
|
|
|
notification.created_at.to_s
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# (3) send notification
|
|
|
|
|
@@mq_router.publish_to_user(receiver.id, msg)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
################## BAND INVITATION ACCEPTED ##################
|
|
|
|
|
def send_band_invitation_accepted(band, band_invitation, sender, receiver)
|
|
|
|
|
|
|
|
|
|
# (1) save to database
|
|
|
|
|
notification = Notification.new
|
|
|
|
|
notification.band_id = band.id
|
|
|
|
|
notification.description = NotificationTypes::BAND_INVITATION_ACCEPTED
|
|
|
|
|
notification.source_user_id = sender.id
|
|
|
|
|
notification.target_user_id = receiver.id
|
|
|
|
|
notification.save
|
|
|
|
|
|
|
|
|
|
# (2) create notification
|
|
|
|
|
notification_msg = format_msg(notification.description, sender)
|
|
|
|
|
msg = @@message_factory.band_invitation_accepted(
|
|
|
|
|
band_invitation.id,
|
|
|
|
|
receiver.id,
|
|
|
|
|
sender.name,
|
|
|
|
|
sender.photo_url,
|
|
|
|
|
band.name,
|
|
|
|
|
notification_msg,
|
|
|
|
|
notification.id,
|
|
|
|
|
notification.created_at.to_s
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# (3) send notification
|
|
|
|
|
@@mq_router.publish_to_user(receiver.id, msg)
|
|
|
|
|
end
|
|
|
|
|
|
2013-12-29 04:51:35 +00:00
|
|
|
################## BAND SESSION JOIN ##################
|
|
|
|
|
|
|
|
|
|
################## MUSICIAN SESSION FRESH ##################
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
################## MUSICIAN SESSION STALE ##################
|
|
|
|
|
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})
|
|
|
|
|
end
|
|
|
|
|
|
2013-03-22 03:18:41 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|