def get_filters(request): try: user_uuid, password = utils.retrieve_username_password_from_authorization( request) if not Users.objects.filter(user_uuid=user_uuid, active=True).exists(): return error_response("Unauthorized Access", 401) user = Users.objects.get(user_uuid=user_uuid) filters = user.filters if user.filters else None res_data = {"filters": filters} return JsonResponse(res_data) except Exception as e: logger.exception(e.message) return error_response("Server Error", 500)
def next_chat(request): try: user_uuid, password = utils.retrieve_username_password_from_authorization( request) if not Users.objects.filter(user_uuid=user_uuid, active=True).exists(): return error_response("Unauthorized Access", 401) chat, on_going_chat = get_current_or_next_chat_for_user(user_uuid) if chat is None or chat.rating_by_userA or chat.rating_by_userB: return JsonResponse({"chat": None}) other_user = chat.userA if chat.userA.user_uuid != user_uuid else chat.userB time_diff = abs(chat.chat_time - now()) time_diff = (time_diff.days * 24 * 60 * 60) + time_diff.seconds if not on_going_chat else 0 res_data = { "chat": { "seconds_left_for_chat_start": time_diff, "chat_start_time": chat.chat_time, "chat_end_time": chat.chat_time + timedelta(0, SECONDS), "current_time": now().isoformat(), "user": { "gender": other_user.gender, "fcm_token": other_user.fcm_token } } } if time_diff <= 5: if not chat.opentok_session_id: opentok_session_id = generate_opentok_session() chat.opentok_session_id = opentok_session_id chat.save() token, api_key, session_id = get_opentok_details( chat.opentok_session_id) res_data['chat']['session_data'] = { "token": token, "sessionId": session_id, "apiKey": api_key } return JsonResponse(res_data) except Exception as e: logger.exception(e.message) return error_response("Server Error", 500)
def retrieve_users_and_chats(request): username, password = retrieve_username_password_from_authorization(request) if username != Opentok.get_api_key(): return error_response('Unauthorized Access', 401) users = Users.objects.values('user_uuid', 'fb_link', 'fb_profile_data', 'name', 'email', 'filters', 'timezone').all() chats = Chats.objects.values('id', 'userA__user_uuid', 'userB__user_uuid', 'chat_time', 'userA__name', 'userA__email', 'userB__name', 'userB__name').all() for chat in chats: chat['chat_time'] = chat['chat_time'].__str__() data = { "users": [user for user in users], "chats": [chat for chat in chats] } return JsonResponse({"data": data})
def update_user_details(request): try: user_uuid, password = utils.retrieve_username_password_from_authorization( request) if not Users.objects.filter(user_uuid=user_uuid, active=True).exists(): return error_response("Unauthorized Access", 401) user = Users.objects.filter(user_uuid=user_uuid).first() data = json.loads(request.body) fb_data = data.get('fb_data', None) filters = data.get('filters', None) user.fb_data = fb_data if fb_data else user.fb_data user.filters = filters if filters else user.filters user.save() return JsonResponse({"user_uuid": user.user_uuid}) except Exception as e: logger.exception(e.message) return error_response("Server Error", 500)
def notification(request): try: data = json.loads(request.body) user_uuid, password = utils.retrieve_username_password_from_authorization( request) if not Users.objects.filter(user_uuid=user_uuid, active=True).exists(): return error_response("Unauthorized Access", 401) fcm_token = data['fcm_token'] notification_type = data['notification_type'] if notification_type == "CALL ENDED NOTIFICATION": GCMNotificaiton().send_call_ended_notification(fcm_token) return JsonResponse({"status": "ok"}) except Exception as e: logger.exception(e.message) return error_response("Server Error", 500)
def update_ratings(request): try: user_uuid, password = utils.retrieve_username_password_from_authorization( request) if not Users.objects.filter(user_uuid=user_uuid, active=True).exists(): return error_response("Unauthorized Access", 401) user = Users.objects.get(user_uuid=user_uuid) data = json.loads(request.body) opentok_session_id = data['opentok_session_id'] ratings = data['ratings'] chat = Chats.objects.get(opentok_session_id=opentok_session_id) if chat.userA == user: other_user = chat.userB rating_existed = True if chat.rating_by_userA else False chat.rating_by_userA = ratings else: other_user = chat.userA rating_existed = True if chat.rating_by_userB else False chat.rating_by_userB = ratings chat.save() if not rating_existed: if chat.rating_by_userA and chat.rating_by_userB: # Notification # send notificaiton only when both user completed GCMNotificaiton().send_ratings_feedback_notification(chat.id) total_counts = other_user.avg_rating.get('total_rating_counts', 0) for key, val in ratings['rating_params'].items(): avg_val = other_user.avg_rating.get(key, 0) avg_val = ((avg_val * total_counts) + val) / ( (total_counts + 1) * 1.0) other_user.avg_rating[key] = avg_val other_user.avg_rating[TOTAL_RATING_COUNT] = total_counts + 1 other_user.save() return JsonResponse({"status": "ok"}) else: return error_response( "You have already rated this User for Same Call", 400) except Exception as e: logger.exception(e.message) return error_response("Server Error", 500)
def get_session(request): try: user_uuid, password = utils.retrieve_username_password_from_authorization( request) if not Users.objects.filter(user_uuid=user_uuid, active=True).exists(): return error_response("Unauthorized Access", 401) user = Users.objects.filter(user_uuid=user_uuid).first() chat, on_going_chat = get_current_or_next_chat_for_user( user_uuid=user_uuid) time_diff = abs(chat.chat_time - now()) time_diff = (time_diff.days * 24 * 60 * 60) + time_diff.seconds if chat is None or time_diff >= SECONDS: return JsonResponse({"session": None}) if not chat.opentok_session_id: opentok_session_id = generate_opentok_session() chat.opentok_session_id = opentok_session_id chat.save() else: opentok_session_id = chat.opentok_session_id token, api_key, session_id = get_opentok_details(opentok_session_id) api_key = Opentok.get_api_key() session_id = opentok_session_id data = {"token": token, "sessionId": session_id, "apiKey": api_key} other_user = chat.userB if chat.userA == user else chat.userA GCMNotificaiton().send_ringing_notification(other_user.fcm_token, user.gender, chat) return JsonResponse({"session": data}) except Exception as e: logger.exception(e.message) return error_response("Server Error", 500)