VRFS-282 VRFS-283 toasts / notifications for friend updates and requests

This commit is contained in:
Brian Smith 2013-03-31 14:07:46 -04:00
parent f1ac2a951e
commit 7ec4d6021d
5 changed files with 96 additions and 36 deletions

View File

@ -111,23 +111,24 @@
return Jampb::ClientMessage.new(:type => ClientMessage::Type::SESSION_INVITATION, :route_to => USER_TARGET_PREFIX + receiver_id, :session_invitation => session_invitation) return Jampb::ClientMessage.new(:type => ClientMessage::Type::SESSION_INVITATION, :route_to => USER_TARGET_PREFIX + receiver_id, :session_invitation => session_invitation)
end end
# create a friend update message
def friend_update(user_id, name, photo_url, online, msg)
friend = Jampb::FriendUpdate.new(:user_id => user_id, :name => name, :photo_url => photo_url, :online => online, :msg => msg)
return Jampb::ClientMessage.new(:type => ClientMessage::Type::FRIEND_UPDATE, :route_to => USER_TARGET_PREFIX + user_id, :friend_update => friend)
end
# create a friend request message # create a friend request message
def friend_request(user_id, name, photo_url, friend_id) def friend_request(id, user_id, name, photo_url, friend_id, msg)
friend_request = Jampb::FriendRequest.new(:user_id => user_id, :name => name, :photo_url => photo_url, :friend_id => friend_id) friend_request = Jampb::FriendRequest.new(:id => id, :user_id => user_id, :name => name, :photo_url => photo_url, :friend_id => friend_id, :msg => msg)
return Jampb::ClientMessage.new(:type => ClientMessage::Type::FRIEND_REQUEST, :route_to => USER_TARGET_PREFIX + friend_id, :friend_request => friend_request) return Jampb::ClientMessage.new(:type => ClientMessage::Type::FRIEND_REQUEST, :route_to => USER_TARGET_PREFIX + friend_id, :friend_request => friend_request)
end end
# create a friend request acceptance message # create a friend request acceptance message
def friend_request_accepted(friend_id, name, photo_url, user_id) def friend_request_accepted(friend_id, name, photo_url, user_id, msg)
friend_request_accepted = Jampb::FriendRequestAccepted.new(:friend_id => friend_id, :name => name, :photo_url => photo_url, :user_id => user_id) friend_request_accepted = Jampb::FriendRequestAccepted.new(:friend_id => friend_id, :name => name, :photo_url => photo_url, :user_id => user_id, :msg => msg)
return Jampb::ClientMessage.new(:type => ClientMessage::Type::FRIEND_REQUEST_ACCEPTED, :route_to => USER_TARGET_PREFIX + user_id, :friend_request_accepted => friend_request_accepted) return Jampb::ClientMessage.new(:type => ClientMessage::Type::FRIEND_REQUEST_ACCEPTED, :route_to => USER_TARGET_PREFIX + user_id, :friend_request_accepted => friend_request_accepted)
end end
# create a friend update message
def friend_update(user_id, online)
friend = Jampb::FriendUpdate.new(:user_id => user_id, :online => online)
return Jampb::ClientMessage.new(:type => ClientMessage::Type::FRIEND_UPDATE, :route_to => USER_TARGET_PREFIX + user_id, :friend_update => friend)
end
############## P2P CLIENT MESSAGES ################# ############## P2P CLIENT MESSAGES #################
# send a request to do a ping # send a request to do a ping

View File

@ -27,7 +27,7 @@ module JamRuby
friend_request.save friend_request.save
# send notification # send notification
# Notification.send_friend_request(user_id, friend_id) Notification.send_friend_request(friend_request.id, user_id, friend_id)
else else
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
@ -41,7 +41,7 @@ module JamRuby
Friendship.save(friend_request.user_id, friend_request.friend_id) Friendship.save(friend_request.user_id, friend_request.friend_id)
# send notification # send notification
# Notification.send_friend_request_accepted(user_id, friend_id) Notification.send_friend_request_accepted(user_id, friend_id)
end end
end end
end end

View File

@ -1,6 +1,43 @@
module JamRuby module JamRuby
class Notification < ActiveRecord::Base 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: MAKE ALL METHODS BELOW ASYNC SO THE CLIENT DOESN'T BLOCK ON NOTIFICATION LOGIC
# TODO: ADD TESTS FOR THIS CLASS # TODO: ADD TESTS FOR THIS CLASS
@ -9,9 +46,8 @@ module JamRuby
@@mq_router = MQRouter.new @@mq_router = MQRouter.new
@@message_factory = MessageFactory.new @@message_factory = MessageFactory.new
def index(user_id) def delete_all(session_id)
results = Notification.where(:user_id => user_id).limit(50) Notification.delete_all "(session_id = '#{session_id}')"
return results
end end
################### HELPERS ################### ################### HELPERS ###################
@ -41,10 +77,37 @@ module JamRuby
return ids return ids
end 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 ################### ################### FRIEND UPDATE ###################
def send_friend_update(user_id, online, connection) def send_friend_update(user_id, online, connection)
user = User.find(user_id)
# (1) create notification # (1) create notification
msg = @@message_factory.friend_update(user_id, online) 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 # (2) get all of this user's friends
friend_ids = retrieve_friends(connection, user_id) friend_ids = retrieve_friends(connection, user_id)
@ -54,21 +117,22 @@ module JamRuby
end end
################### FRIEND REQUEST ################### ################### FRIEND REQUEST ###################
def send_friend_request(user_id, friend_id) def send_friend_request(id, user_id, friend_id)
user = User.find(user_id) user = User.find(user_id)
# (1) create notification # (1) create notification
msg = @@message_factory.friend_request(user_id, user.name, user.photo_url, friend_id) 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 # (2) send notification
@@mq_router.publish_to_user(friend_id, msg) @@mq_router.publish_to_user(friend_id, msg)
# (3) save to database # (3) save to database
# notification = Notification.new notification = Notification.new
# notification.type = "friend_request" notification.type = "friend_request"
# notification.source_user_id = user_id notification.source_user_id = user_id
# notification.target_user_id = friend_id notification.target_user_id = friend_id
# notification.save notification.save
end end
############### FRIEND REQUEST ACCEPTED ############### ############### FRIEND REQUEST ACCEPTED ###############
@ -82,11 +146,11 @@ module JamRuby
@@mq_router.publish_to_user(user_id, msg) @@mq_router.publish_to_user(user_id, msg)
# (3) save to database # (3) save to database
# notification = Notification.new notification = Notification.new
# notification.type = "friend_request_accepted" notification.type = "friend_request_accepted"
# notification.source_user_id = friend_id notification.source_user_id = friend_id
# notification.target_user_id = user_id notification.target_user_id = user_id
# notification.save notification.save
end end
################## SESSION INVITATION ################## ################## SESSION INVITATION ##################
@ -137,9 +201,6 @@ module JamRuby
# (3) save to database # (3) save to database
end end
# TODO: add methods to delete Notifications based on user id, session id, etc.
end end
end end
end end

View File

@ -61,6 +61,10 @@ module JamRuby
has_many :favorites, :class_name => "JamRuby::UserFavorite", :foreign_key => "user_id" has_many :favorites, :class_name => "JamRuby::UserFavorite", :foreign_key => "user_id"
has_many :inverse_favorites, :through => :favorites, :class_name => "JamRuby::User" has_many :inverse_favorites, :through => :favorites, :class_name => "JamRuby::User"
# notifications
has_many :notifications, :class_name => "JamRuby::Notification", :foreign_key => "target_user_id"
has_many :inverse_notifications, :through => :notifications, :class_name => "JamRuby::User"
# friends # friends
has_many :friendships, :class_name => "JamRuby::Friendship", :foreign_key => "user_id" has_many :friendships, :class_name => "JamRuby::Friendship", :foreign_key => "user_id"
has_many :friends, :through => :friendships, :class_name => "JamRuby::User" has_many :friends, :through => :friendships, :class_name => "JamRuby::User"

View File

@ -11,9 +11,7 @@ class MQRouter
@@log = Logging.logger[MQRouter] @@log = Logging.logger[MQRouter]
end end
def access_music_session(music_session, user) def access_music_session(music_session, user)
if music_session.nil? if music_session.nil?
raise ArgumentError, 'specified session not found' raise ArgumentError, 'specified session not found'
end end
@ -50,7 +48,6 @@ class MQRouter
# sends a message to a client with no checking of permissions (RAW USAGE) # sends a message to a client with no checking of permissions (RAW USAGE)
# this method deliberately has no database interactivity/active_record objects # this method deliberately has no database interactivity/active_record objects
def publish_to_client(client_id, client_msg, sender = {:client_id => ""}) def publish_to_client(client_id, client_msg, sender = {:client_id => ""})
EM.schedule do EM.schedule do
sender_client_id = sender[:client_id] sender_client_id = sender[:client_id]
@ -64,7 +61,6 @@ class MQRouter
# sends a message to a session with no checking of permissions (RAW USAGE) # sends a message to a session with no checking of permissions (RAW USAGE)
# this method deliberately has no database interactivity/active_record objects # this method deliberately has no database interactivity/active_record objects
def publish_to_session(music_session_id, client_ids, client_msg, sender = {:client_id => ""}) def publish_to_session(music_session_id, client_ids, client_msg, sender = {:client_id => ""})
EM.schedule do EM.schedule do
sender_client_id = sender[:client_id] sender_client_id = sender[:client_id]
@ -81,7 +77,6 @@ class MQRouter
# sends a message to a user with no checking of permissions (RAW USAGE) # sends a message to a user with no checking of permissions (RAW USAGE)
# this method deliberately has no database interactivity/active_record objects # this method deliberately has no database interactivity/active_record objects
def publish_to_user(user_id, user_msg) def publish_to_user(user_id, user_msg)
EM.schedule do EM.schedule do
@@log.debug "publishing to user:#{user_id} from server" @@log.debug "publishing to user:#{user_id} from server"
# put it on the topic exchange for users # put it on the topic exchange for users
@ -92,7 +87,6 @@ class MQRouter
# sends a message to a list of friends with no checking of permissions (RAW USAGE) # sends a message to a list of friends with no checking of permissions (RAW USAGE)
# this method deliberately has no database interactivity/active_record objects # this method deliberately has no database interactivity/active_record objects
def publish_to_friends(friend_ids, user_msg, from_user_id) def publish_to_friends(friend_ids, user_msg, from_user_id)
EM.schedule do EM.schedule do
friend_ids.each do |friend_id| friend_ids.each do |friend_id|
@@log.debug "publishing to friend:#{friend_id} from user #{from_user_id}" @@log.debug "publishing to friend:#{friend_id} from user #{from_user_id}"