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 get_contact_list(request,app_name,user_id): ''' Function returns user's contact list [server]/api/[app_name]/[user_id]/get_contact_list Example: http://chat.localhost/api/tpa1com/1/get_contact_list Responce: {"status": 0, "message": "ok", "contact_list": [{"image": "/static/images/avatar.jpg", "is_invisible": false, "is_invitation_enabled": true, "culture": "ru", "birthday": "1996-12-08", "id": 44, "city": "Washava", "user_id": 3, "name": "serg", "country": "Poland", "gender": "m", "profile_url": "serg_plofile_url", "is_camera_active": false}]} ''' contactlst = [] tpa = Tpa.objects.get(name=app_name) if(user_id=='undefined'): return { 'status': 1, 'message': 'undefined user' } owner = ChatUser.objects.filter(tpa=tpa,user_id=user_id) for c in ChatContacts.objects.filter(owner=owner): contact = serialize_user(c.contact) contact['has_new_message'] = c.has_new_message contact['activity'] = c.activity #try: # contact['is_room_closed'] = c.room.is_closed #except: # pass contact['contact_id'] = c.id contactlst.append(contact) return { 'status': 0, 'message': 'ok', 'user_list': contactlst }
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. ''' #contact = _add_contact(app_name,owner_id,contact_id) #mes = { 'action': 'update_contact' } owner_chanel = '%s_%s' % (app_name, owner_id) #bclient.publish(owner_chanel, json.dumps(mes)) contact_chanel = '%s_%s' % (app_name, contact_id) rm = _get_room_or_create(app_name,owner_id,contact_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) } bclient.publish(owner_chanel, json.dumps(mes)) mes = { 'action': 'show_inv_win', 'room_id': rm['room_id'], 'user_profile': serialize_user(owner)} bclient.publish(contact_chanel, json.dumps(mes)) return _get_room_or_create(app_name,owner_id,contact_id)
def _get_room_or_create(app_name,caler_id,opponent_id): ''' Function return existed room identifier or create new room. Also it create two records in ChatUser2Room model. ''' tpa = Tpa.objects.get(name=app_name) opponent = ChatUser.objects.get(tpa=tpa,user_id=opponent_id) caler = ChatUser.objects.get(tpa=tpa,user_id=caler_id) r = bd.select(""" SELECT DISTINCT u1.room_id AS id, u1.user_id AS opponent, u2.user_id AS caler FROM chat_chatuser2room u1, chat_chatuser2room u2, chat_chatroom WHERE chat_chatroom.is_closed=False AND chat_chatroom.id = u1.room_id AND u1.user_id = '%s' AND u2.user_id = '%s' AND u1.room_id = u2.room_id AND u1.tpa_id = u2.tpa_id AND u1.tpa_id = '%s' ORDER BY u1.room_id DESC LIMIT 1 """ % (int(opponent.id), int(caler.id), int(tpa.id))) #import pdb; pdb.set_trace() if r.rowcount > 0 : room_id = ChatRoom.objects.get(pk = r.record[0]['id']) return { 'status': 0, 'message': 'Room is exist', 'room_id': str(room_id.id) } if r.rowcount == 0 : room = ChatRoom() room.tpa = tpa room.save() room.add_user(caler) room.add_user(opponent) room.save() participans = { str(caler.user_id) : serialize_user(caler), str(opponent.user_id) : serialize_user(opponent) } return { 'status': 0, 'message': 'Room was created', 'room_id': str(room.id), 'participans': participans }
def only_mic_on(request,user_id,opponent_id,app_name): ''' Request alert opponent about nessesity of turning mic on whem woman turn only mic. [server]/api/[user_id]/[opponent_id]/only_mic_on Example: http://chat.localhost/api/150041/150034/only_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': 'only_mic_on', 'profile': serialize_user(profile) } bclient.publish(chanel, json.dumps(mes)) profile = ChatUser.objects.get(user_id=opponent_id, tpa=tpa) my_chanel = '%s_%s' % (app_name,user_id) mes = { 'action': 'opponent_mic_on', 'profile': serialize_user(profile) } #bclient.publish(my_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 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 get_online_except_contact(request,app_name,user_id): ''' Function return list of users with opposite gender who are online except those who is not in contact list. [server]/api/[app_name]/[user_id]/get_online_except_contact Example: http://chat.localhost/api/tpa1com/150031/get_online_except_contact ''' userlst_profile = [] tpa = Tpa.objects.get(name=app_name) if user_id == 'undefined': users_online = ChatUser.objects.filter(tpa=tpa,is_online=1) else: owner = ChatUser.objects.get(tpa=tpa,user_id=user_id) users_online = ChatUser.objects.filter(tpa=tpa,is_online=1).exclude(gender=owner.gender) cc = ChatContacts.objects.filter(tpa=tpa,owner=owner) for c in cc: users_online = users_online.exclude(id=c.contact.id) for u in users_online: userlst_profile.append(serialize_user(u)) return { 'status': 0, 'message': 'ok', 'user_list': userlst_profile }
def get_profile(request,user_id,app_name): from djapp.settings import TPA_SERVER try: tpa = Tpa.objects.get(name=app_name) u_name = ChatUser.objects.get(user_id=user_id,tpa=tpa) out = { 'status': 0, 'user_profile': serialize_user(u_name) } return HttpResponse(json.dumps(out), content_type='application/json') except: url = get_url_by_name('get_profile_from_tpa',{'user_id':user_id,'app_name':app_name,'signal_server': TPA_SERVER}) print 'REQUEST TO %s' % url responce = requests.get(url) outdata = json.loads(responce.content) #print outdata out = { 'status': 0, 'message': 'ok', 'outdata': outdata } return HttpResponse(json.dumps(out), content_type='application/json')
def get_online(request,app_name,user_id): ''' Function return list of users with opposite gender who are on line [server]/api/[app_name]/[user_id]/get_online Example: http://chat.localhost/api/tpa1com/150031/get_online ''' userlst_profile = [] tpa = Tpa.objects.get(name=app_name) if user_id == 'undefined': return { 'status': 0, 'message': 'ok', 'user_list': {} } else: try: owner = ChatUser.objects.get(tpa=tpa,user_id=user_id) except: url = get_url_by_name('get_profile_from_tpa',{'user_id':user_id,'app_name':app_name,'signal_server': TPA_SERVER}) responce = requests.get(url) owner = ChatUser.objects.get(tpa=tpa,user_id=user_id) users_online = ChatUser.objects.filter(tpa=tpa,is_online=1).exclude(gender=owner.gender) for u in users_online: userlst_profile.append(serialize_user(u)) return { 'status': 0, 'message': 'ok', 'user_list': userlst_profile }
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 get_messages(request,room_id): ''' Function get message in DB for room and app_name [server]/api/[room_id]/get_message Example: http://chat.localhost/api/23/get_message ''' #import pdb; pdb.set_trace() lst_chat_message = [] room = ChatRoom.objects.get(id=int(room_id)) message = ChatMessage.objects.filter(room=room).order_by("id") for m in message: user_info = ChatUser.objects.get(user_id=m.user.user_id) lst_chat_message.append({'id':m.id, 'created': str(m.created.time()), 'owner': serialize_user(m.user), 'message':m.message }) return { 'status': 0, 'message': lst_chat_message }
def save_message(request): ''' Function save message owner in DB, 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) 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) print balance if balance < 3: return { 'status': 1, 'message': 'Your account is emply. Please replanish your account.' } room = ChatRoom.objects.get(tpa=tpa,id=int(b['room_id'])) cm = ChatMessage() cm.tpa = tpa cm.user = owner cm.room = room cm.message = message 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) } } bclient.publish(p, json.dumps(mes)) opponent = ChatUser.objects.get(user_id=p.split('_')[1]) #import pdb; pdb.set_trace() if owner != opponent: # adding contact add_me_to_contact_if_not_exist(tpa,owner,opponent,p) contact = _add_contact(tpa.name,owner.user_id,opponent.user_id) mes_contact = { 'action': 'update_contact' } mes_online = { 'action': 'update_users_online' } owner_chanel = '%s_%s' % (b['app_name'], owner.user_id) bclient.publish(owner_chanel, json.dumps(mes_contact)) bclient.publish(owner_chanel, json.dumps(mes_online)) if room.get_count_messages()<2999: 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)) except Exception, e: print e
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 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 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