jam-cloud/lib/jam_ruby/models/notification.rb

206 lines
6.6 KiB
Ruby
Raw Normal View History

module JamRuby
class Notification < ActiveRecord::Base
def index(user_id)
results = Notification.where(:target_user_id => user_id).limit(50)
return results
end
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
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
def delete_all(session_id)
Notification.delete_all "(session_id = '#{session_id}')"
2013-03-23 07:50:01 +00:00
end
################### 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))
return ids
end
def format_msg(type, user)
case type
when "friend_update"
return "#{user.name} is now "
when "friend_request"
return "#{user.name} has sent you a friend request."
when "friend_request_accepted"
return "#{user.name} has accepted your friend request."
when "friend_joined_session"
when "social_media_friend_joined"
when "join_request_approved"
when "join_request_rejected"
when "session_invitation"
when "band_invitation"
when "band_invitation_accepted"
when "recording_available"
else
end
end
################### FRIEND UPDATE ###################
def send_friend_update(user_id, online, connection)
user = User.find(user_id)
# (1) create notification
online_msg = online ? "online." : "offline."
notification_msg = format_msg("friend_update", user) + online_msg
msg = @@message_factory.friend_update(user_id, user.name, user.photo_url, online, notification_msg)
# (2) get all of this user's friends
friend_ids = retrieve_friends(connection, user_id)
# (3) send notification
@@mq_router.publish_to_friends(friend_ids, msg, user_id)
end
################### FRIEND REQUEST ###################
def send_friend_request(id, user_id, friend_id)
user = User.find(user_id)
# (1) create notification
notification_msg = format_msg("friend_request", user)
msg = @@message_factory.friend_request(id, user_id, user.name, user.photo_url, friend_id, notification_msg)
# (2) send notification
@@mq_router.publish_to_user(friend_id, msg)
# (3) save to database
notification = Notification.new
notification.type = "friend_request"
notification.source_user_id = user_id
notification.target_user_id = friend_id
notification.save
end
############### FRIEND REQUEST ACCEPTED ###############
def send_friend_request_accepted(user_id, friend_id)
friend = User.find(friend_id)
# (1) create notification
2013-03-23 07:50:01 +00:00
msg = @@message_factory.friend_request_accepted(friend_id, friend.name, friend.photo_url, user_id)
# (2) send notification
@@mq_router.publish_to_user(user_id, msg)
# (3) save to database
notification = Notification.new
notification.type = "friend_request_accepted"
notification.source_user_id = friend_id
notification.target_user_id = user_id
notification.save
end
################## SESSION INVITATION ##################
def send_session_invitation(receiver_id, invitation_id)
# (1) create notification
msg = @@message_factory.session_invitation(receiver_id, invitation_id)
# (2) send notification
@@mq_router.publish_to_user(receiver_id, msg)
# (3) save to database
2013-03-23 08:08:28 +00:00
# notification = Notification.new
# notification.type = "session_invitation"
# notification.target_user_id = receiver_id
end
def send_session_left(music_session, connection, user)
# (1) create notification
msg = @@message_factory.user_left_music_session(music_session.id, user.id, user.name)
# (2) send notification
@@mq_router.server_publish_to_session(music_session, msg, sender = {:client_id => connection.client_id})
end
def send_join_request(music_session, join_request, sender, text)
# (1) create notification
msg = @@message_factory.join_request(music_session.id, join_request.id, sender.name, text)
# (2) send notification
@@mq_router.server_publish_to_session(music_session, msg)
# (3) save to database
2013-03-23 08:08:28 +00:00
# notification = Notification.new
end
def send_session_joined(connection, user)
# (1) create notification
msg = @@message_factory.user_joined_music_session(connection.music_session.id, user.id, user.name)
# (2a) send notification to session members
@@mq_router.server_publish_to_session(connection.music_session, msg, sender = {:client_id => connection.client_id})
# TODO: (2b) retrieve all friends and followers of user and send notification to them as well
# (3) save to database
end
end
end
end