def show_opponent_cam(request,user_id,opponent_id,app_name,room_id): ''' Request fires after user turn apponent cam on. We mark room object as with video watching. Send notivication to girl about man start watching her. [server]/api/[user_id]/[opponent_id]/[app_name]/[room_id]/show_opponent_cam Example: http://chat.localhost/api/150046/150040/tpa1com/5/show_opponent_cam Return: {'status': 0, 'message': 'ok'} ''' #tpa = Tpa.objects.get(name=app_name) #import pdb; pdb.set_trace() tpa = Tpa.objects.get(name=app_name) owner = ChatUser.objects.get(user_id=user_id, tpa=tpa) room = ChatRoom.objects.get(pk=room_id) u2r = ChatUser2Room.objects.get(user=owner,room=room) u2r.is_video_watching = True u2r.save() if owner.gender=='m': room.is_charging_video = True room.save() mes = { 'action': 'i_started_watching_you', 'user_id': owner.user_id, 'opponent_id': opponent_id } bclient.publish('%s_%s' % (app_name, opponent_id), json.dumps(mes)) if(owner.gender == 'm'): # deduct money data = [] data.append({'action': 'video', 'app_name': app_name, 'user_id': user_id, 'opponent_id': opponent_id, 'room_id': room_id, 'price': str(tpa.price_video) }) print requests.post(tpa.charge_url,data=json.dumps(data)).content flashtpl = ''' <div id="flashContent" > <object type="application/x-shockwave-flash" data="/Media/chat.swf" width="%s" height="%s"> <param name="movie" value="/Media/chat.swf" /> <param name="quality" value="high" /> <param name="bgcolor" value="#ffffff" /> <param name="play" value="true" /> <param name="loop" value="true" /> <param name="wmode" value="window" /> <param name="scale" value="showall" /> <param name="menu" value="true" /> <param name="devicefont" value="false" /> <param name="salign" value="" /> <param name="allowScriptAccess" value="sameDomain" /> <param name="FlashVars" value="streamName=%s&url=rtmp://%s/myapp&micOn=false&type=in" /> </object> </div> ''' sname = '%s_%s' % (app_name,opponent_id) html = flashtpl % ('100%', '100%', sname, SS_SERVER) return {'status': 0, 'message': 'ok', 'user_id': user_id, 'html': html}
def set_disconnected(request,app_name,user_id,source): tpa = Tpa.objects.get(name=app_name) user = ChatUser.objects.get(tpa=tpa,user_id=user_id) user.is_online = 0 user.is_camera_active = 0 user.save() mes1 = {'action': 'update_users_online'} mes2 = {'action': 'set_me_offline', 'uid': user_id} for u in ChatUser.objects.filter(is_online=1).exclude(user_id=user_id): bclient.publish('%s_%s' % (app_name,u.user_id), json.dumps(mes1)) bclient.publish('%s_%s' % (app_name,u.user_id), json.dumps(mes2)) print 'send to %s_%s' % (app_name,u.user_id) # TODO bd.update('update users set online=0 where login=%s' % user_id) # close active rooms ssql = '''select chat_chatroom.id from chat_chatroom, chat_chatuser2room, chat_chatuser where chat_chatroom.id = chat_chatuser2room.room_id and chat_chatuser2room.user_id=chat_chatuser.id and chat_chatroom.is_closed = 0 and chat_chatuser.user_id = %s''' % user_id rooms = bd.select(ssql) for r in rooms.record: print 'CLOSE ROOM %s for user %s' % (r['id'],user_id) bd.update('update chat_chatroom set is_charging_text = 0, is_charging_video = 0, is_charging_audio = 0, is_closed=1 where chat_chatroom.id = %s' % r['id']) return { 'status': 0, 'message': 'ok' }
def show_my_cam(request,user_id,app_name): ''' Request fires after adding video block into the DOM. Here we send broadcast request to users online to show video indicator. [server]/api/[user_id]/show_my_cam Example: http://chat.localhost/api/150041/show_my_cam Return: {'status': 0, 'message': 'ok'} ''' try: tpa = Tpa.objects.get(name=app_name) owner = ChatUser.objects.get(user_id=user_id, tpa=tpa) owner.is_camera_active = True owner.save() tpa = Tpa.objects.get(name=app_name) camerausers = [] for ou in ChatUser.objects.filter(is_camera_active=1).all(): camerausers.append(ou.user_id) print 'wcam %s' % camerausers users = ChatUser.objects.filter(is_online=1).all() for u in users: mes = { 'action': 'update_cam_indicators', 'data': camerausers, 'owner': owner.user_id, 'cam_status': 'on' } bclient.publish('%s_%s' % (tpa.name, u.user_id), json.dumps(mes)) return {'status': 0, 'message': 'ok'} except Exception, e: return {'status': 1, 'message': e}
def mark_watching_profile(request,app_name,user_id,opponent_id): ''' Request to mark indicator about user is watching profile. [server]/api/[app_name]/[user_id]/[opponent_id]/mark_watching_profile Example: http://chat.localhost/api/tpa1com/14/15/mark_watching_profile Responce 1: { 'status': 0, 'message': 'ok' } ''' mes = { 'action': 'mark_watching_profile', 'user_id': user_id } bclient.publish('%s_%s' % (app_name,opponent_id), json.dumps(mes)) return { 'status': 0, 'message': 'ok' }
def add_me_to_contact_if_not_exist(tpa,owner,opponent,chanel): ''' :param owner: :param opponent: :return: ''' try: cont = ChatContacts.objects.get(tpa=tpa,owner=opponent,contact=owner) except: mes = { 'action': 'add_me_in_contact_list', 'user_id': owner.user_id, 'profile': serialize_user(owner) } bclient.publish(chanel, json.dumps(mes)) print 'ssssend %s' % chanel
def show_feather(request,app_name,room_id,opponent_id): ''' Function sends message to user to show feather indicator to notice about user is started typing. [server]/api/[app_name]/[room_id]/[opponent_id]/show_feather Example: http://chat.localhost/api/tpa1com/34/150014/show_feather ''' mes = { 'action': 'show_feather', 'room_id': room_id } bclient.publish('%s_%s' % (app_name, opponent_id), json.dumps(mes)) return { 'status': 0, 'message': 'Ok' }
def invite(request,app_name,owner_id,contact_id): ''' Function send invite to opponent. REMOVED Add the opponent to the contact list. Mark contact as active Create the room. Put the user into the room. ''' # return emptyness if request went without contact_id if contact_id == '0': return {'status': 1, 'message': 'Contact does not defined'} # mark contact as does not have new message if it exists contact = _get_contact(app_name,owner_id,contact_id) rm = _get_room_or_create(app_name,owner_id,contact_id) room = ChatRoom.objects.get(pk=rm['room_id']) if(contact): contact.has_new_message = False contact.activity = time.time() contact.room = room contact.save() contact_data = {'activity': int(time.time())} else: contact_data = {} owner_chanel = '%s_%s' % (app_name, owner_id) #contact.set_active(rm['room_id']) tpa = Tpa.objects.get(name=app_name) owner = ChatUser.objects.get(user_id=owner_id,tpa=tpa) contact_user = ChatUser.objects.get(user_id=contact_id,tpa=tpa) mes = { 'action': 'put_me_in_room', 'room_id': rm['room_id'], 'owner_id': owner_id,'contact_id':contact_id, 'contact': serialize_user(contact_user), 'contact_data': contact_data, 'source': 'views/room.py'} #print "SEND TO %s %s" % (owner_chanel,mes) bclient.publish(owner_chanel, json.dumps(mes)) if(contact_user.is_online): mes = { 'action': 'show_inv_win', 'room_id': rm['room_id'], 'user_profile': serialize_user(owner)} contact_chanel = '%s_%s' % (app_name, contact_id) bclient.publish(contact_chanel, json.dumps(mes)) out = _get_room_or_create(app_name,owner_id,contact_id) out['opponent'] = serialize_user(contact_user) out['contact_data'] = contact_data return out
def alert_mic_off(request,user_id,opponent_id,app_name): ''' Request alert opponent about nessesity of turning mic off. [server]/api/[user_id]/[opponent_id]/alert_mic_on Example: http://chat.localhost/api/150041/150034/alert_mic_on Return: {'status': 0, 'message': 'ok'} ''' tpa = Tpa.objects.get(name=app_name) chanel = '%s_%s' % (app_name,opponent_id) profile = ChatUser.objects.get(user_id=opponent_id, tpa=tpa) mes = { 'action': 'alert_mic_off', 'profile': serialize_user(profile) } bclient.publish(chanel, json.dumps(mes)) return {'status': 0, 'message': 'ok'}
def multi_invitation(request): ''' Function send invite to multiply opponent. ''' b = json.loads(request.body) print b #import pdb; pdb.set_trace() tpa = Tpa.objects.get(name=b['app_name']) owner = ChatUser.objects.get(tpa=tpa,user_id=int(b['owner_id'])) id = str(b['owner_id']) data = {'message': b['message'], 'opponent': serialize_user(owner), 'id': id } mes = { 'action': 'show_multi_invite_notification', 'data': data } bclient.publish('%s_%s' % (b['app_name'], str(b['opponent_id'])), json.dumps(mes)) #print 'sent to %s_%s ' % (b['app_name'], str(b['opponent_id'])) return { 'status': 0, 'message': 'ok' }
def say_busy(request,user_id,opponent_id,app_name): ''' Send message to opponet about youa are busy now [server]/api/[app_name]/[user_id]/[opponent_id]/say_busy Example: http://chat.localhost/api/tpa1com/150040/150042/say_busy Return: {'status': 0, message: 'ok'} ''' tpa = Tpa.objects.get(name=app_name) opponent = ChatUser.objects.get(tpa=tpa,user_id=opponent_id) user = ChatUser.objects.get(tpa=tpa,user_id=user_id) message = { 'action': 'say_busy', 'message': 'Sorry but I am busy now.', 'user_profile': serialize_user(user) } opponent_chanel = '%s_%s' % (app_name, opponent_id) bclient.publish(opponent_chanel, json.dumps(message)) return {'status': 0, 'message': 'ok'}
def say_close(request,user_id,opponent_id,app_name): ''' Send message to opponet about you are close invitation window [server]/api/[app_name]/[user_id]/[opponent_id]/say_close Example: http://chat.localhost/api/tpa1com/150040/150042/say_close Return: {'status': 0, message: 'ok'} ''' tpa = Tpa.objects.get(name=app_name) opponent = ChatUser.objects.get(tpa=tpa,user_id=opponent_id) user = ChatUser.objects.get(tpa=tpa,user_id=user_id) message = { 'action': 'say_busy', 'message': '%s is occupied at the moment.' % user.name, 'user_profile': serialize_user(user) } opponent_chanel = '%s_%s' % (app_name, opponent_id) bclient.publish(opponent_chanel, json.dumps(message)) return {'status': 0, 'message': 'ok'}
def set_connected(request,app_name,user_id,source): tpa = Tpa.objects.get(name=app_name) user = ChatUser.objects.get(tpa=tpa,user_id=user_id) user.is_online = 1 user.activity = int(time.time()) user.save() #if(source=='chat_side'): mes1 = {'action': 'update_users_online'} mes2 = {'action': 'set_me_online', 'uid': user_id} for u in ChatUser.objects.filter(is_online=1).exclude(user_id=user_id): #for u in ChatUser.objects.all().exclude(user_id=user_id): bclient.publish('%s_%s' % (app_name,u.user_id), json.dumps(mes1)) bclient.publish('%s_%s' % (app_name,u.user_id), json.dumps(mes2)) print 'dddddddddddddddddddddddddddddddd %s_%s' % (app_name,u.user_id) # TODO bd.update('update users set online=1 where login=%s' % user_id) return { 'status': 0, 'message': 'ok' }
def opponent_mic_off(request,user_id,opponent_id,room_id,app_name): ''' Request gives command to opponent authomatically disable mic. [server]/api/[user_id]/[opponent_id]/both_mic_on Example: http://chat.localhost/api/150041/150034/both_mic_on Return: {'status': 0, 'message': 'ok'} ''' tpa = Tpa.objects.get(name=app_name) chanel = '%s_%s' % (app_name,opponent_id) room = ChatRoom.objects.get(pk=room_id) room.is_charging_audio = True room.save() profile = ChatUser.objects.get(user_id=opponent_id, tpa=tpa) mes = { 'action': 'opponent_mic_off', 'profile': serialize_user(profile) } bclient.publish(chanel, json.dumps(mes)) return {'status': 0, 'message': 'ok'}
def send_invitation(request): ''' Function saves invitation from woman. parameters by POST: app_name,owner_id,contact_id,message [server]/api/send_invitation Example: http://chat.localhost/api/send_invitation ''' #import pdb; pdb.set_trace() b = json.loads(request.body) print b tpa = Tpa.objects.get(name=b['app_name']) owner = ChatUser.objects.get(tpa=tpa,user_id=int(b['owner_id'])) data = {'message': b['message'], 'opponent': serialize_user(owner), 'id': int(time.time()) } mes = { 'action': 'show_invite_notification', 'data': data } bclient.publish('%s_%s' % (b['app_name'], str(b['contact_id'])), json.dumps(mes)) return { 'status': 0 }
def charge_request(request,app_name): ''' Input data [ { 'action': 'video/text_chat', 'user_id': 150040, 'opponent_id': 150042, 'room_id': 23, 'app_name': 'tpa1com', 'session': 'sefwefwe23243fef', 'price': 2 } ] Make external request to application to charge money. Send notification to men to update account on the page. ''' json_data = json.loads(request.body) print json_data check_users_for_off(json_data) tpa = Tpa.objects.get(name=app_name) #print 'request to %s ' % tpa.charge_url res = requests.post(tpa.charge_url,json=json_data).content res = json.loads(res) print res for i in res: user = ChatUser.objects.get(user_id=i['user_id']) if(user.is_online): mes = { 'action': 'update_balance', 'balance': i['balance'], 'room_id': i['room_id'], 'user_id': i['user_id'], 'status': i['status'] } chanel = '%s_%s' % (app_name, i['user_id']) bclient.publish(chanel, json.dumps(mes)) print 'send to -%s %s' % (chanel,mes) return {'status': 0, 'message': 'ok'}
def close_chat_room(request,app_name,room_id, opponent_id, user_id): ''' Function closes chat session. [server]/api/[app_name]/[room_id]/[opponent_id]/[user_id]/close_chat_room Example: http://chat.localhost/api/tpa1com/34/190023/12678/close_chat_room **user_id** = user WHO closed the room ''' room = ChatRoom.objects.get(pk = room_id) room.is_charging_text = False room.is_charging_video = False room.is_charging_audio = False room.is_closed = True room.save() mes = { 'action': 'close_room', 'room_id': room_id, 'user_id': user_id } bclient.publish('%s_%s' % (app_name, opponent_id), json.dumps(mes)) # send commant to remove this opponent from list of the active opponent in js mess_ac = { 'action': 'contact_deactivate', 'user_id': user_id } bclient.publish('%s_%s' % (app_name, opponent_id), json.dumps(mess_ac)) mess_ac = { 'action': 'delete_me_from_contact', 'opponent_id': user_id } #print mess_ac bclient.publish('%s_%s' % (app_name, opponent_id), json.dumps(mess_ac)) return { 'status': 0, 'message': 'Ok' }
def hide_opponent_cam(request,user_id,opponent_id,app_name,room_id): ''' Request fires after user turn apponent cam off. We mark room object as without video watching. [server]/api/[user_id]/[app_name]/[room_id]/hide_opponent_cam Example: http://chat.localhost/api/150046/tpa1com/hide_my_cam Return: {'status': 0, 'message': 'ok'} ''' tpa = Tpa.objects.get(name=app_name) owner = ChatUser.objects.get(user_id=user_id, tpa=tpa) room = ChatRoom.objects.get(pk=room_id) u2r = ChatUser2Room.objects.get(user=owner,room=room) u2r.is_video_watching = False u2r.save() room.is_charging_video = False room.save() mes = { 'action': 'i_stopted_watching_you', 'user_id': owner.user_id, 'opponent_id': opponent_id } bclient.publish('%s_%s' % (app_name, opponent_id), json.dumps(mes)) return {'status': 0, 'message': 'ok', 'user_id': user_id}
def hide_my_cam(request,user_id,app_name): ''' Request fires after removing video block from the DOM. Here we send broadcast request to users online to hide video indicator. [server]/api/[user_id]/hide_my_cam Example: http://chat.localhost/api/150041/hide_my_cam Return: {'status': 0, 'message': 'ok'} ''' try: tpa = Tpa.objects.get(name=app_name) owner = ChatUser.objects.get(user_id=user_id, tpa=tpa) owner.is_camera_active = False owner.save() camerausers = [] for ou in ChatUser.objects.filter(is_camera_active=1).all(): camerausers.append(ou.user_id) tpa = Tpa.objects.get(name=app_name) users = ChatUser.objects.filter(is_online=1).all() for u in users: mes = { 'action': 'update_cam_indicators', 'data': camerausers, 'owner': owner.user_id, 'cam_status': 'off' } bclient.publish('%s_%s' % (tpa.name, u.user_id), json.dumps(mes)) # mark rooms as no video charging for c2r in ChatUser2Room.objects.filter(user=owner).all(): room = c2r.room room.is_charging_video = False room.save() return {'status': 0, 'message': 'ok'} except Exception, e: return {'status': 1, 'message': e}
def save_message(request): ''' Function save message owner in DB, Check if user is available. Check message out on stop words and replace them. Add owner to opponent's contact list. If the message is first send notification to tpa side. parameters by POST: app_name,owner_id,room_id,message [server]/api/save_message Example: http://chat.localhost/api/save_message ''' #import pdb; pdb.set_trace() b = json.loads(request.body) #time.sleep(2) b['message'] = stop_words(b['message']) if(check_message(b['message']) == False): message = 'This message contains prohibited information!' else: message = b['message'] tpa = Tpa.objects.get(name=b['app_name']) owner = ChatUser.objects.get(tpa=tpa,user_id=int(b['owner_id'])) if (owner.gender=='m'): balance = get_user_balance(tpa,owner) if balance < 3: return { 'status': 1, 'message': 'Your account is emply. Please replanish your account.' } owner.activity = time.time() owner.save() room = ChatRoom.objects.get(tpa=tpa,id=int(b['room_id'])) # Set servers locale to Kiev time to save same date of message for girl and man import pytz, datetime local = pytz.utc naive = datetime.datetime.strptime (time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()), "%Y-%m-%d %H:%M:%S") local_dt = local.localize(naive, is_dst=None) utc_dt = local_dt.astimezone (pytz.timezone ("Europe/Kiev")) cm = ChatMessage() cm.tpa = tpa cm.user = owner cm.room = room cm.message = message cm.created = utc_dt gender = owner.gender cm.save() charge_for_chat(cm,room,tpa) #charging #try: arr_participants = [] for p in b['participants']: arr_participants.append(p.split('_')[1]) mes = { 'action': 'show_message', 'room_id': b['room_id'], 'message': {'id': cm.id, 'created': str(cm.created.time().strftime('%H:%M:%S')), 'message':cm.message, 'room_id':cm.room_id, 'owner': serialize_user(owner), 'participants': b['participants'] } } print 'send to %s' % p opponent = ChatUser.objects.get(user_id=p.split('_')[1]) if owner != opponent: bclient.publish(p, json.dumps(mes)) #import pdb; pdb.set_trace() if owner != opponent: # check accessebilities #import pdb; pdb.set_trace() check_avalible_url = get_url_by_name('check_accessebility',{'user_id': opponent.user_id, 'app_name': b['app_name']}) print 'REQUEST_____%s' % check_avalible_url res = json.loads(requests.get(check_avalible_url).content) if(res['status']==1): print 'noooooo' mes = { 'action': 'say_busy', 'message': 'Sorry but I am busy now.', 'user_profile': serialize_user(opponent)} owner_chanel = '%s_%s' % (b['app_name'], owner.user_id) bclient.publish(owner_chanel, json.dumps(mes)) # adding contact is_sent = False if opponent.gender == 'w': add_me_to_contact_if_not_exist(tpa,owner,opponent,p) #if it man just show multiinvite popup else: try: cont = ChatContacts.objects.get(tpa=tpa,owner=opponent,contact=owner) except: data = {'message': cm.message, 'opponent': serialize_user(owner), 'id': str(owner.user_id) } mes = { 'action': 'show_multi_invite_notification', 'data': data } bclient.publish('%s_%s' % (b['app_name'], opponent.user_id), json.dumps(mes)) is_sent = True contact = _add_contact(tpa.name,owner.user_id,opponent.user_id) mark_new_message(owner,opponent) if(opponent.is_online): mes_contact = { 'action': 'add_opponent_in_my_contact_list', 'user_id': opponent.user_id, 'profile': serialize_user(opponent) } mes_online = { 'action': 'update_users_online' } owner_chanel = '%s_%s' % (b['app_name'], owner.user_id) opponent_chanel = '%s_%s' % (b['app_name'], opponent.user_id) bclient.publish(owner_chanel, json.dumps(mes_contact)) bclient.publish(owner_chanel, json.dumps(mes_online)) if is_sent == False: data = {'message': cm.message, 'id': cm.id, 'opponent': serialize_user(owner)} mes = { 'action': 'show_new_message_notification', 'data': data } bclient.publish('%s_%s' % (tpa.name, opponent.user_id), json.dumps(mes)) # send commant to add this opponent to list of the active opponent in js mess_ac = { 'action': 'contact_activate', 'user_id': owner.user_id, 'profile': serialize_user(owner) } bclient.publish(opponent_chanel, json.dumps(mess_ac)) #except Exception, e: # print e return { 'status': 0, 'message': b['message'], 'room_id': str(room.id), 'owner_id': str(owner.id), 'participants': arr_participants }
def charge_money(): sql = """select chat_chatroom.id, chat_chatroom.activity, chat_chatroom.is_charging_text, chat_chatroom.is_charging_video, chat_chatroom.is_charging_audio, chat_tpa.price_text_chat, chat_tpa.price_video, chat_tpa.price_audio, chat_tpa.name as app_name, chat_tpa.timeout_chating from chat_chatroom, chat_tpa where chat_chatroom.tpa_id = chat_tpa.id and chat_chatroom.is_closed = 0 and (is_charging_text="%s" or is_charging_video="%s" or is_charging_audio="%s")""" % ( 1, 1, 1, ) rooms = bd.select(sql) data = [] url = False print "Charging!!!!" for room in rooms.record: # url = room['charge_url'] url = get_url_by_name("charge_request", {"app_name": room["app_name"]}) # select users from room sql = ( """ select chat_chatuser.user_id, chat_chatuser.gender from chat_chatuser, chat_chatuser2room where chat_chatuser2room.user_id = chat_chatuser.id and chat_chatuser2room.room_id = %s """ % room["id"] ) users = bd.select(sql) for u in users.record: # print 'room %s user %s' % (room['id'],u['gender']) if u["gender"] == "m": man = u["user_id"] else: woman = u["user_id"] if room["is_charging_text"] == 1: now = int(time.time()) timeout = room["activity"] + room["timeout_chating"] # print 'checking %s %s' % (now,timeout) if now > timeout: sql_update = """ update chat_chatroom set is_charging_text=0 where id = %s """ % room["id"] bd.update(sql_update) # print 'TIMOUT 00000000' # send command to remove user from contact list mess_ac = {"action": "delete_me_from_contact", "opponent_id": u["user_id"]} bclient.publish("%s_%s" % (room["app_name"], man), json.dumps(mess_ac)) mess_ac = {"action": "delete_me_from_contact", "opponent_id": man} bclient.publish("%s_%s" % (room["app_name"], u["user_id"]), json.dumps(mess_ac)) else: data.append( { "action": "text_chat", "app_name": room["app_name"], "user_id": man, "opponent_id": woman, "room_id": room["id"], "price": str(room["price_text_chat"]), } ) # print data if room["is_charging_video"] == 1: data.append( { "action": "video", "app_name": room["app_name"], "user_id": man, "opponent_id": woman, "room_id": room["id"], "price": str(room["price_video"]), } ) if room["is_charging_audio"] == 1: data.append( { "action": "audio", "app_name": room["app_name"], "user_id": man, "opponent_id": woman, "room_id": room["id"], "price": str(room["price_audio"]), } ) if url: print "Charge request to %s " % url print "DATA %s" % data print requests.post(url, json=data).content