2014-06-27 23:37:33 +08:00
|
|
|
Meteor.methods
|
2014-10-02 02:02:29 +08:00
|
|
|
sendChatMessagetoServer: (meetingId, chatObject, requesterUserId) ->
|
|
|
|
# inside the chatObject, they store their _id as the sender
|
|
|
|
# and they pass their userId to this method as a param
|
2014-10-04 01:31:45 +08:00
|
|
|
transformedChatObject = chatObject
|
2014-10-02 02:02:29 +08:00
|
|
|
|
2014-10-04 01:31:45 +08:00
|
|
|
console.log "requesterUserId: #{requesterUserId} | from_userid: #{transformedChatObject.from_userid}"
|
2014-10-02 02:02:29 +08:00
|
|
|
requester = Meteor.Users.findOne({_id: transformedChatObject.from_userid, userId: requesterUserId})
|
2014-10-04 01:31:45 +08:00
|
|
|
forPublic = transformedChatObject.to_userid is 'public_chat_userid'
|
2014-10-02 02:02:29 +08:00
|
|
|
|
2014-10-04 01:31:45 +08:00
|
|
|
if requester? # User exists, and is valid
|
|
|
|
console.log "requester exists"
|
2014-09-30 21:56:10 +08:00
|
|
|
# check if this is a private or a public chat message
|
|
|
|
eventName = ->
|
2014-10-02 02:02:29 +08:00
|
|
|
if transformedChatObject.chat_type is "PRIVATE_CHAT"
|
2014-09-30 21:56:10 +08:00
|
|
|
"send_private_chat_message_request"
|
|
|
|
else "send_public_chat_message_request"
|
2014-09-24 03:55:25 +08:00
|
|
|
|
2014-10-02 02:02:29 +08:00
|
|
|
recipient = Meteor.Users.findOne(_id: transformedChatObject.to_userid)
|
2014-10-04 01:31:45 +08:00
|
|
|
if recipient? or forPublic
|
2014-10-02 02:02:29 +08:00
|
|
|
|
|
|
|
# translate _ids to userIds for flash
|
|
|
|
transformedChatObject.from_userid = requester.userId
|
2014-10-04 01:31:45 +08:00
|
|
|
transformedChatObject.to_userid = if forPublic then 'public_chat_userid' else recipient.userId
|
2014-10-02 02:02:29 +08:00
|
|
|
|
2014-09-30 21:56:10 +08:00
|
|
|
message =
|
|
|
|
header :
|
|
|
|
"timestamp": new Date().getTime()
|
|
|
|
"name": eventName()
|
2014-10-07 21:27:17 +08:00
|
|
|
payload:
|
2014-10-02 02:02:29 +08:00
|
|
|
"message" : transformedChatObject
|
2014-09-30 21:56:10 +08:00
|
|
|
"meeting_id": meetingId
|
2014-10-02 02:02:29 +08:00
|
|
|
"requester_id": transformedChatObject.from_userid
|
2014-09-30 21:56:10 +08:00
|
|
|
#
|
2014-10-04 01:31:45 +08:00
|
|
|
console.log JSON.stringify transformedChatObject
|
2014-09-30 21:56:10 +08:00
|
|
|
publish Meteor.config.redis.channels.toBBBApps.chat, message
|
2014-10-04 01:31:45 +08:00
|
|
|
#
|
2014-10-07 21:27:17 +08:00
|
|
|
else
|
2014-10-04 01:31:45 +08:00
|
|
|
console.log "requester no exists"
|
2014-09-24 03:55:25 +08:00
|
|
|
|
2014-09-25 23:54:14 +08:00
|
|
|
# --------------------------------------------------------------------------------------------
|
|
|
|
# Private methods on server
|
|
|
|
# --------------------------------------------------------------------------------------------
|
2014-10-01 01:12:47 +08:00
|
|
|
@deletePrivateChatMessages = (user1Id, user2Id) ->
|
|
|
|
Meteor.Chat.remove({ # find all and remove private messages between the 2 users
|
|
|
|
'message.chat_type': 'PRIVATE_CHAT',
|
|
|
|
$or: [{'message.from_userid': user1Id, 'message.to_userid': user2Id},{'message.from_userid': user2Id, 'message.to_userid': user1Id}]
|
|
|
|
})
|
2014-10-02 22:15:45 +08:00
|
|
|
# send a message to redis out about deleting conversation between these 2 users
|
|
|
|
#
|
2014-09-26 02:47:43 +08:00
|
|
|
|
|
|
|
@addChatToCollection = (meetingId, messageObject) ->
|
2014-10-04 01:31:45 +08:00
|
|
|
console.log "-------------addChatToCollection---------------------"
|
2014-10-02 02:02:29 +08:00
|
|
|
transformedChatObject = messageObject
|
|
|
|
|
2014-09-26 02:47:43 +08:00
|
|
|
# manually convert time from 1.408645053653E12 to 1408645053653 if necessary (this is the time_from that the Flash client outputs)
|
2014-10-02 02:02:29 +08:00
|
|
|
transformedChatObject.from_time = (transformedChatObject.from_time).toString().split('.').join("").split("E")[0]
|
|
|
|
|
|
|
|
fromUser = Meteor.Users.findOne({userId: transformedChatObject.from_userid})
|
|
|
|
toUser = Meteor.Users.findOne({userId: transformedChatObject.to_userid})
|
2014-10-04 01:31:45 +08:00
|
|
|
forPublic = transformedChatObject.to_userid is 'public_chat_userid'
|
2014-10-02 02:02:29 +08:00
|
|
|
|
2014-10-04 01:31:45 +08:00
|
|
|
if (fromUser? and toUser?) or forPublic
|
2014-10-02 02:02:29 +08:00
|
|
|
# translate ids from flash to html5
|
2014-10-10 04:05:29 +08:00
|
|
|
transformedChatObject.from_userid = fromUser?._id
|
|
|
|
transformedChatObject.to_userid = if forPublic then 'public_chat_userid' else toUser?._id
|
2014-10-02 02:02:29 +08:00
|
|
|
|
2014-10-04 01:31:45 +08:00
|
|
|
if transformedChatObject.from_userid? and transformedChatObject.to_userid?
|
|
|
|
entry =
|
|
|
|
meetingId: meetingId
|
|
|
|
message:
|
|
|
|
chat_type: transformedChatObject.chat_type
|
|
|
|
message: transformedChatObject.message
|
|
|
|
to_username: transformedChatObject.to_username
|
|
|
|
from_tz_offset: transformedChatObject.from_tz_offset
|
|
|
|
from_color: transformedChatObject.from_color
|
|
|
|
to_userid: transformedChatObject.to_userid
|
|
|
|
from_userid: transformedChatObject.from_userid
|
|
|
|
from_time: transformedChatObject.from_time
|
|
|
|
from_username: transformedChatObject.from_username
|
|
|
|
from_lang: transformedChatObject.from_lang
|
2014-10-02 02:02:29 +08:00
|
|
|
|
2014-10-04 01:31:45 +08:00
|
|
|
id = Meteor.Chat.insert(entry)
|
|
|
|
console.log "added chat id=[#{id}]:#{transformedChatObject.message}. Chat.size is now #{Meteor.Chat.find({meetingId: meetingId}).count()}"
|
2014-10-01 01:02:36 +08:00
|
|
|
# --------------------------------------------------------------------------------------------
|
|
|
|
# end Private methods on server
|
|
|
|
# --------------------------------------------------------------------------------------------
|