880 lines
29 KiB
Ruby
880 lines
29 KiB
Ruby
module JamRuby
|
|
class Notification < ActiveRecord::Base
|
|
|
|
@@log = Logging.logger[Notification]
|
|
|
|
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"
|
|
|
|
validates :target_user, :presence => true
|
|
validates :message, length: {minimum: 1, maximum: 400}, no_profanity: true, if: :text_message?
|
|
validate :different_source_target, if: :text_message?
|
|
|
|
def different_source_target
|
|
unless target_user_id.nil? || source_user_id.nil?
|
|
errors.add(:target_user, ValidationMessages::DIFFERENT_SOURCE_TARGET) if target_user_id == source_user_id
|
|
end
|
|
end
|
|
|
|
def index(user_id)
|
|
Notification.where(:target_user_id => user_id).limit(50)
|
|
end
|
|
|
|
def created_date
|
|
self.created_at.getutc.iso8601.to_s
|
|
end
|
|
|
|
def photo_url
|
|
unless self.source_user.nil?
|
|
self.source_user.photo_url
|
|
end
|
|
end
|
|
|
|
# used for persisted notifications
|
|
def formatted_msg
|
|
# target_user, band, session, recording, invitation, join_request = nil
|
|
source_user, band = nil
|
|
|
|
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
|
|
|
|
self.class.format_msg(self.description, source_user, band)
|
|
end
|
|
|
|
# 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
|
|
|
|
################### 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_user_followers(connection, user_id)
|
|
follower_ids = []
|
|
connection.exec("SELECT u.user_id as follower_id FROM follows f WHERE f.followable_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_not_in_session(connection, user_id, session_id)
|
|
ids = retrieve_friends(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
|
|
return ids
|
|
end
|
|
|
|
def retrieve_friends_and_followers(connection, user_id)
|
|
ids = retrieve_friends(connection, user_id)
|
|
ids.concat(retrieve_user_followers(connection, user_id))
|
|
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
|
|
return ids
|
|
end
|
|
|
|
def format_msg(description, user = nil, band = nil)
|
|
name, band_name = ""
|
|
unless user.nil?
|
|
name = user.name
|
|
else
|
|
name = "Someone"
|
|
end
|
|
|
|
if !band.nil?
|
|
band_name = band.name
|
|
end
|
|
|
|
case description
|
|
|
|
# friend notifications
|
|
when NotificationTypes::FRIEND_UPDATE
|
|
return "#{name} is now "
|
|
|
|
when NotificationTypes::FRIEND_REQUEST
|
|
return "#{name} has sent you a friend request."
|
|
|
|
when NotificationTypes::FRIEND_REQUEST_ACCEPTED
|
|
return "#{name} has accepted your friend request."
|
|
|
|
when NotificationTypes::NEW_USER_FOLLOWER
|
|
return "#{name} is now following you on JamKazam."
|
|
|
|
when NotificationTypes::NEW_BAND_FOLLOWER
|
|
return "#{name} is now following your band #{band.name} on JamKazam."
|
|
|
|
# session notifications
|
|
when NotificationTypes::SESSION_INVITATION
|
|
return "#{name} has invited you to a session."
|
|
|
|
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."
|
|
|
|
when NotificationTypes::SESSION_JOIN
|
|
return "#{name} has joined the session."
|
|
|
|
when NotificationTypes::SESSION_DEPART
|
|
return "#{name} has left the session."
|
|
|
|
when NotificationTypes::MUSICIAN_SESSION_JOIN
|
|
return "#{name} is now in a session."
|
|
|
|
when NotificationTypes::BAND_SESSION_JOIN
|
|
return "#{band_name} is now in a session."
|
|
|
|
|
|
# recording notifications
|
|
when NotificationTypes::MUSICIAN_RECORDING_SAVED
|
|
return "#{name} has made a new recording."
|
|
|
|
when NotificationTypes::BAND_RECORDING_SAVED
|
|
return "#{band.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
|
|
when NotificationTypes::BAND_INVITATION
|
|
return "You have been invited to join the band #{band_name}."
|
|
|
|
when NotificationTypes::BAND_INVITATION_ACCEPTED
|
|
return "#{name} has accepted your band invitation to join #{band_name}."
|
|
|
|
else
|
|
return ""
|
|
end
|
|
end
|
|
|
|
def send_friend_update(user_id, online, connection)
|
|
|
|
friend_ids = retrieve_friends(connection, user_id)
|
|
|
|
unless friend_ids.empty?
|
|
user = User.find(user_id)
|
|
|
|
online_msg = online ? "online." : "offline."
|
|
notification_msg = format_msg(NotificationTypes::FRIEND_UPDATE, user) + online_msg
|
|
msg = @@message_factory.friend_update(
|
|
user.id,
|
|
user.photo_url,
|
|
online,
|
|
notification_msg
|
|
)
|
|
|
|
@@mq_router.publish_to_friends(friend_ids, msg, user_id)
|
|
end
|
|
end
|
|
|
|
def send_friend_request(friend_request_id, user_id, friend_id)
|
|
user = User.find(user_id)
|
|
friend = User.find(friend_id)
|
|
|
|
notification = Notification.new
|
|
notification.description = NotificationTypes::FRIEND_REQUEST
|
|
notification.source_user_id = user_id
|
|
notification.target_user_id = friend_id
|
|
notification.friend_request_id = friend_request_id
|
|
notification.save
|
|
|
|
notification_msg = format_msg(notification.description, user)
|
|
|
|
if friend.online
|
|
msg = @@message_factory.friend_request(
|
|
friend.id,
|
|
friend_request_id,
|
|
user.photo_url,
|
|
notification_msg,
|
|
notification.id,
|
|
notification.created_date
|
|
)
|
|
|
|
@@mq_router.publish_to_user(friend_id, msg)
|
|
|
|
else
|
|
UserMailer.friend_request(friend.email, notification_msg).deliver
|
|
end
|
|
end
|
|
|
|
def send_friend_request_accepted(user_id, friend_id)
|
|
friend = User.find(friend_id)
|
|
user = User.find(user_id)
|
|
|
|
notification = Notification.new
|
|
notification.description = NotificationTypes::FRIEND_REQUEST_ACCEPTED
|
|
notification.source_user_id = friend_id
|
|
notification.target_user_id = user_id
|
|
notification.save
|
|
|
|
notification_msg = format_msg(notification.description, friend)
|
|
|
|
if user.online
|
|
msg = @@message_factory.friend_request_accepted(
|
|
user.id,
|
|
friend.photo_url,
|
|
notification_msg,
|
|
notification.id,
|
|
notification.created_date
|
|
)
|
|
|
|
@@mq_router.publish_to_user(user.id, msg)
|
|
|
|
else
|
|
UserMailer.friend_request_accepted(user.email, notification_msg).deliver
|
|
end
|
|
end
|
|
|
|
def send_new_user_follower(follower, user)
|
|
|
|
notification = Notification.new
|
|
notification.description = NotificationTypes::NEW_USER_FOLLOWER
|
|
notification.source_user_id = follower.id
|
|
notification.target_user_id = user.id
|
|
notification.save
|
|
|
|
notification_msg = format_msg(notification.description, follower)
|
|
|
|
if follower.id != user.id
|
|
if user.online
|
|
msg = @@message_factory.new_user_follower(
|
|
user.id,
|
|
follower.photo_url,
|
|
notification_msg,
|
|
notification.id,
|
|
notification.created_date
|
|
)
|
|
|
|
@@mq_router.publish_to_user(user.id, msg)
|
|
|
|
else
|
|
UserMailer.new_user_follower(user.email, notification_msg).deliver
|
|
end
|
|
end
|
|
end
|
|
|
|
def send_new_band_follower(follower, band)
|
|
|
|
notifications = []
|
|
|
|
band.band_musicians.each.each do |bm|
|
|
|
|
notification = Notification.new
|
|
notification.description = NotificationTypes::NEW_BAND_FOLLOWER
|
|
notification.source_user_id = follower.id
|
|
notification.target_user_id = bm.user_id
|
|
notification.band_id = band.id
|
|
notification.save
|
|
|
|
notification_msg = format_msg(notification.description, follower, band)
|
|
|
|
# this protects against sending the notification to a band member who decides to follow the band
|
|
if follower.id != bm.user.id
|
|
if bm.user.online
|
|
msg = @@message_factory.new_user_follower(
|
|
bm.user_id,
|
|
follower.photo_url,
|
|
notification_msg,
|
|
notification.id,
|
|
notification.created_date
|
|
)
|
|
|
|
@@mq_router.publish_to_user(bm.user_id, msg)
|
|
|
|
else
|
|
UserMailer.new_band_follower(bm.user.email, notification_msg).deliver
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
def send_session_invitation(receiver, sender, session_id)
|
|
|
|
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
|
|
|
|
notification_msg = format_msg(NotificationTypes::SESSION_INVITATION, sender)
|
|
|
|
if receiver.online
|
|
msg = @@message_factory.session_invitation(
|
|
receiver.id,
|
|
session_id,
|
|
notification_msg,
|
|
notification.id,
|
|
notification.created_date
|
|
)
|
|
|
|
@@mq_router.publish_to_user(receiver.id, msg)
|
|
|
|
else
|
|
UserMailer.session_invitation(receiver.email, notification_msg).deliver
|
|
end
|
|
end
|
|
|
|
def send_session_ended(session_id)
|
|
|
|
notifications = Notification.where(:session_id => session_id)
|
|
|
|
# publish to all users who have a notification for this session
|
|
# TODO: do this in BULK or in async block
|
|
notifications.each do |n|
|
|
msg = @@message_factory.session_ended(n.target_user_id, session_id)
|
|
@@mq_router.publish_to_user(n.target_user_id, msg)
|
|
end
|
|
|
|
Notification.delete_all "(session_id = '#{session_id}')"
|
|
end
|
|
|
|
def send_join_request(music_session, join_request, text)
|
|
|
|
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
|
|
|
|
notification_msg = format_msg(notification.description, join_request.user)
|
|
|
|
msg = @@message_factory.join_request(
|
|
join_request.id,
|
|
music_session.id,
|
|
join_request.user.photo_url,
|
|
notification_msg,
|
|
notification.id,
|
|
notification.created_date
|
|
)
|
|
|
|
@@mq_router.publish_to_user(music_session.creator.id, msg)
|
|
end
|
|
|
|
def send_join_request_approved(music_session, join_request)
|
|
|
|
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
|
|
|
|
notification_msg = format_msg(notification.description, music_session.creator)
|
|
|
|
msg = @@message_factory.join_request_approved(
|
|
join_request.id,
|
|
music_session.id,
|
|
music_session.creator.photo_url,
|
|
notification_msg,
|
|
notification.id,
|
|
notification.created_date
|
|
)
|
|
|
|
@@mq_router.publish_to_user(join_request.user.id, msg)
|
|
end
|
|
|
|
def send_join_request_rejected(music_session, join_request)
|
|
|
|
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
|
|
|
|
notification_msg = format_msg(notification.description, music_session.creator)
|
|
|
|
msg = @@message_factory.join_request_rejected(
|
|
join_request.id,
|
|
music_session.id,
|
|
music_session.creator.photo_url,
|
|
notification_msg,
|
|
notification.id,
|
|
notification.created_date
|
|
)
|
|
|
|
@@mq_router.publish_to_user(join_request.user.id, msg)
|
|
end
|
|
|
|
def send_session_join(music_session, connection, user)
|
|
|
|
notification_msg = format_msg(NotificationTypes::SESSION_JOIN, user)
|
|
|
|
msg = @@message_factory.session_join(
|
|
music_session.id,
|
|
user.photo_url,
|
|
notification_msg,
|
|
music_session.track_changes_counter
|
|
)
|
|
|
|
@@mq_router.server_publish_to_session(music_session, msg, sender = {:client_id => connection.client_id})
|
|
end
|
|
|
|
def send_session_depart(music_session, client_id, user, recordingId)
|
|
|
|
notification_msg = format_msg(NotificationTypes::SESSION_DEPART, user)
|
|
|
|
msg = @@message_factory.session_depart(
|
|
music_session.id,
|
|
user.photo_url,
|
|
notification_msg,
|
|
recordingId,
|
|
music_session.track_changes_counter
|
|
)
|
|
|
|
@@mq_router.server_publish_to_session(music_session, msg, sender = {:client_id => client_id})
|
|
end
|
|
|
|
def send_tracks_changed(music_session)
|
|
msg = @@message_factory.tracks_changed(
|
|
music_session.id, music_session.track_changes_counter
|
|
)
|
|
|
|
@@mq_router.server_publish_to_session(music_session, msg)
|
|
end
|
|
|
|
def send_musician_session_join(music_session, connection, user)
|
|
|
|
if music_session.musician_access || music_session.fan_access
|
|
|
|
friends = Friendship.where(:friend_id => user.id)
|
|
user_followers = user.followers
|
|
|
|
# construct an array of User objects representing friends and followers
|
|
friend_users = friends.map { |fu| fu.user }
|
|
follower_users = user_followers.map { |uf| uf.user }
|
|
friends_and_followers = friend_users.concat(follower_users).uniq
|
|
|
|
# remove anyone in the session and invited musicians
|
|
friends_and_followers = friends_and_followers - music_session.users - music_session.invited_musicians
|
|
notifications, online_ff, offline_ff = [], [], []
|
|
notification_msg = format_msg(NotificationTypes::MUSICIAN_SESSION_JOIN, user)
|
|
|
|
friends_and_followers.each do |ff|
|
|
notification = Notification.new
|
|
notification.description = NotificationTypes::MUSICIAN_SESSION_JOIN
|
|
notification.source_user_id = user.id
|
|
notification.target_user_id = ff.id
|
|
notification.session_id = music_session.id
|
|
notification.save
|
|
|
|
if ff.online
|
|
msg = @@message_factory.musician_session_join(
|
|
ff.id,
|
|
music_session.id,
|
|
user.photo_url,
|
|
music_session.fan_access,
|
|
music_session.musician_access,
|
|
music_session.approval_required,
|
|
notification_msg,
|
|
notification.id,
|
|
notification.created_date
|
|
)
|
|
|
|
@@mq_router.publish_to_user(ff.id, msg)
|
|
else
|
|
offline_ff << ff
|
|
end
|
|
end
|
|
|
|
# send email notifications
|
|
if !offline_ff.empty? && music_session.fan_access
|
|
begin
|
|
UserMailer.musician_session_join(offline_ff.map! {|f| f.email}, notification_msg, music_session.id).deliver if APP_CONFIG.send_join_session_email_notifications
|
|
rescue => e
|
|
@@log.error("unable to send email to offline participants #{e}")
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
def send_band_session_join(music_session, band)
|
|
|
|
# if the session is private, don't send any notifications
|
|
if music_session.musician_access || music_session.fan_access
|
|
|
|
notifications, online_followers, offline_followers = [], [], []
|
|
notification_msg = format_msg(NotificationTypes::BAND_SESSION_JOIN, nil, band)
|
|
|
|
followers = band.followers.map { |bf| bf.user }
|
|
|
|
# do not send band session notifications to band members
|
|
followers = followers - band.users
|
|
|
|
followers.each do |f|
|
|
follower = f
|
|
notification = Notification.new
|
|
notification.band_id = band.id
|
|
notification.description = NotificationTypes::BAND_SESSION_JOIN
|
|
notification.target_user_id = follower.id
|
|
notification.session_id = music_session.id
|
|
notification.save
|
|
|
|
if follower.online
|
|
msg = @@message_factory.band_session_join(
|
|
follower.id,
|
|
music_session.id,
|
|
band.photo_url,
|
|
music_session.fan_access,
|
|
music_session.musician_access,
|
|
music_session.approval_required,
|
|
notification_msg,
|
|
notification.id,
|
|
notification.created_date
|
|
)
|
|
|
|
@@mq_router.publish_to_user(follower.id, msg)
|
|
else
|
|
offline_followers << follower
|
|
end
|
|
end
|
|
|
|
# send email notifications
|
|
if !offline_followers.empty? && music_session.fan_access
|
|
UserMailer.band_session_join(offline_followers.map! {|f| f.email}, notification_msg, music_session.id).deliver if APP_CONFIG.send_join_session_email_notifications
|
|
end
|
|
end
|
|
end
|
|
|
|
def send_musician_recording_saved(recording)
|
|
|
|
user = recording.owner
|
|
|
|
friends = Friendship.where(:friend_id => user.id)
|
|
user_followers = user.followers
|
|
|
|
# construct an array of User objects representing friends and followers
|
|
friend_users = friends.map { |fu| fu.friend }
|
|
follower_users = user_followers.map { |uf| uf.user }
|
|
friends_and_followers = friend_users.concat(follower_users).uniq
|
|
|
|
notifications, online_ff, offline_ff = [], [], []
|
|
notification_msg = format_msg(NotificationTypes::MUSICIAN_RECORDING_SAVED, user)
|
|
|
|
friends_and_followers.each do |ff|
|
|
notification = Notification.new
|
|
notification.description = NotificationTypes::MUSICIAN_RECORDING_SAVED
|
|
notification.source_user_id = user.id
|
|
notification.target_user_id = ff.id
|
|
notification.recording_id = recording.id
|
|
notification.save
|
|
|
|
if ff.online
|
|
msg = @@message_factory.musician_recording_saved(
|
|
ff.id,
|
|
recording.id,
|
|
user.photo_url,
|
|
notification_msg,
|
|
notification.id,
|
|
notification.created_date
|
|
)
|
|
|
|
@@mq_router.publish_to_user(ff.id, notification_msg)
|
|
else
|
|
offline_ff << ff
|
|
end
|
|
end
|
|
|
|
# send email notifications
|
|
unless offline_ff.empty?
|
|
UserMailer.musician_recording_saved(offline_ff.map! {|f| f.email}, notification_msg).deliver
|
|
end
|
|
end
|
|
|
|
def send_band_recording_saved(recording)
|
|
|
|
notification_msg = format_msg(NotificationTypes::BAND_RECORDING_SAVED, nil, recording.band)
|
|
|
|
band.followers.each do |bf|
|
|
follower = bf.user
|
|
notification = Notification.new
|
|
notification.description = NotificationTypes::BAND_RECORDING_SAVED
|
|
notification.band_id = band.id
|
|
notification.target_user_id = follower.id
|
|
notification.recording_id = recording.id
|
|
notification.save
|
|
|
|
if follower.online
|
|
msg = @@message_factory.band_recording_saved(
|
|
follower.id,
|
|
recording.id,
|
|
band.photo_url,
|
|
notification_msg,
|
|
notification.id,
|
|
notification.created_date
|
|
)
|
|
|
|
@@mq_router.publish_to_user(of.id, notification_msg)
|
|
else
|
|
offline_followers << follower
|
|
end
|
|
end
|
|
|
|
# send email notifications
|
|
unless offline_followers.empty?
|
|
UserMailer.band_recording_saved(offline_followers.map! {|f| f.email}, notification_msg).deliver
|
|
end
|
|
end
|
|
|
|
def send_recording_started(music_session, connection, user)
|
|
|
|
notification_msg = format_msg(NotificationTypes::RECORDING_STARTED, user)
|
|
|
|
music_session.users.each do |musician|
|
|
if musician.id != user.id
|
|
msg = @@message_factory.recording_started(
|
|
musician.id,
|
|
user.photo_url,
|
|
notification_msg
|
|
)
|
|
|
|
@@mq_router.publish_to_user(musician.id, msg)
|
|
end
|
|
end
|
|
end
|
|
|
|
def send_recording_ended(music_session, connection, user)
|
|
|
|
notification_msg = format_msg(NotificationTypes::RECORDING_ENDED, user)
|
|
|
|
music_session.users.each do |musician|
|
|
if musician.id != user.id
|
|
msg = @@message_factory.recording_ended(
|
|
musician.id,
|
|
user.photo_url,
|
|
notification_msg
|
|
)
|
|
|
|
@@mq_router.publish_to_user(musician.id, msg)
|
|
end
|
|
end
|
|
end
|
|
|
|
def send_recording_master_mix_complete(recording)
|
|
|
|
# only people who get told about mixes are folks who claimed it... not everyone in the session
|
|
recording.claimed_recordings.each do |claimed_recording|
|
|
|
|
notification = Notification.new
|
|
notification.band_id = recording.band.id if recording.band
|
|
notification.recording_id = recording.id
|
|
notification.target_user_id = claimed_recording.user_id
|
|
notification.description = NotificationTypes::RECORDING_MASTER_MIX_COMPLETE
|
|
notification.save
|
|
|
|
notification_msg = format_msg(notification.description, nil, recording.band)
|
|
|
|
msg = @@message_factory.recording_master_mix_complete(
|
|
claimed_recording.user_id,
|
|
recording.id,
|
|
notification.band_id,
|
|
notification_msg,
|
|
notification.id,
|
|
notification.created_date)
|
|
|
|
@@mq_router.publish_to_user(claimed_recording.user_id, msg)
|
|
end
|
|
end
|
|
|
|
def send_text_message(message, sender, receiver)
|
|
|
|
notification = Notification.new
|
|
notification.description = NotificationTypes::TEXT_MESSAGE
|
|
notification.message = message
|
|
notification.source_user_id = sender.id
|
|
notification.target_user_id = receiver.id if receiver
|
|
if notification.save
|
|
if receiver.online
|
|
clip_at = 200
|
|
msg_is_clipped = message.length > clip_at
|
|
truncated_msg = message[0..clip_at - 1]
|
|
msg = @@message_factory.text_message(
|
|
receiver.id,
|
|
sender.photo_url,
|
|
sender.name,
|
|
sender.id,
|
|
truncated_msg,
|
|
msg_is_clipped,
|
|
notification.id,
|
|
notification.created_date)
|
|
|
|
@@mq_router.publish_to_user(receiver.id, msg)
|
|
|
|
else
|
|
UserMailer.text_message(receiver.email, sender.id, sender.name, sender.resolved_photo_url, message).deliver
|
|
end
|
|
end
|
|
|
|
notification
|
|
end
|
|
|
|
def send_band_invitation(band, band_invitation, sender, receiver)
|
|
|
|
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
|
|
|
|
notification_msg = format_msg(notification.description, nil, band)
|
|
|
|
if receiver.online
|
|
msg = @@message_factory.band_invitation(
|
|
receiver.id,
|
|
band_invitation.id,
|
|
band.id,
|
|
sender.photo_url,
|
|
notification_msg,
|
|
notification.id,
|
|
notification.created_date
|
|
)
|
|
|
|
@@mq_router.publish_to_user(receiver.id, msg)
|
|
|
|
else
|
|
UserMailer.band_invitation(receiver.email, notification_msg).deliver
|
|
end
|
|
end
|
|
|
|
def send_band_invitation_accepted(band, band_invitation, sender, receiver)
|
|
|
|
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
|
|
|
|
notification_msg = format_msg(notification.description, sender, band)
|
|
|
|
if receiver.online
|
|
msg = @@message_factory.band_invitation_accepted(
|
|
receiver.id,
|
|
band_invitation.id,
|
|
sender.photo_url,
|
|
notification_msg,
|
|
notification.id,
|
|
notification.created_date
|
|
)
|
|
@@mq_router.publish_to_user(receiver.id, msg)
|
|
|
|
else
|
|
UserMailer.band_invitation_accepted(receiver.email, notification_msg).deliver
|
|
end
|
|
end
|
|
|
|
def send_musician_session_fresh(music_session, client_id, user)
|
|
|
|
msg = @@message_factory.musician_session_fresh(
|
|
music_session.id,
|
|
user.id,
|
|
user.name,
|
|
user.photo_url
|
|
)
|
|
|
|
@@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)
|
|
|
|
msg = @@message_factory.musician_session_stale(
|
|
music_session.id,
|
|
user.id,
|
|
user.name,
|
|
user.photo_url
|
|
)
|
|
|
|
@@mq_router.server_publish_to_session(music_session, msg, sender = {:client_id => client_id})
|
|
end
|
|
|
|
def send_download_available(user_id)
|
|
msg = @@message_factory.download_available
|
|
|
|
@@mq_router.publish_to_user(user_id, msg)
|
|
end
|
|
|
|
def send_source_up_requested(music_session, host, port, mount, source_user, source_pass, bitrate)
|
|
msg = @@message_factory.source_up_requested(music_session.id, host, port, mount, source_user, source_pass, bitrate)
|
|
|
|
@@mq_router.server_publish_to_session(music_session, msg)
|
|
end
|
|
|
|
def send_source_down_requested(music_session, mount)
|
|
msg = @@message_factory.source_down_requested(music_session.id, mount)
|
|
|
|
@@mq_router.server_publish_to_session(music_session, msg)
|
|
end
|
|
|
|
def send_source_up(music_session)
|
|
msg = @@message_factory.source_up(music_session.id)
|
|
|
|
@@mq_router.server_publish_to_everyone_in_session(music_session, msg)
|
|
end
|
|
|
|
def send_source_down(music_session)
|
|
msg = @@message_factory.source_down(music_session.id)
|
|
|
|
@@mq_router.server_publish_to_everyone_in_session(music_session, msg)
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def text_message?
|
|
description == 'TEXT_MESSAGE'
|
|
end
|
|
end
|
|
end |