async def get_unhandled_staffs_for_visitor_route(request, *, req_args=None, query_params=None, requester=None, **kwargs): req_args = req_args or {} query_params = query_params or {} if "all" in req_args and req_args["all"].lower() in {"true", "1"}: # unhandled_visitors = await get_non_normal_visitors( # ChatUnhandled, **query_params # ) unhandled_visitors = await get_staff_unhandled_visitors( StaffSubscriptionChat, None, **query_params) else: # Return the staff's unhandled visitors staff_id = requester["id"] unhandled_visitors = await get_staff_unhandled_visitors( StaffSubscriptionChat, staff_id, **query_params) return json({ "data": unhandled_visitors, "links": generate_pagination_links(request.url, unhandled_visitors), })
async def get_subscribed_visitors_for_staff_route(request, *, req_args=None, query_params=None, requester=None, **kwargs): req_args = req_args or {} exclude_unhandled = req_args.get("exclude_unhandled", "false") exclude_unhandled = exclude_unhandled.lower() in {"1", "true"} # Return the staff's unhandled visitors staff_id = requester["id"] subscribed_visitors = await get_self_subscribed_visitors( Visitor, Chat, StaffSubscriptionChat, staff_id, **query_params, exclude_unhandled=exclude_unhandled, ) return json({ "data": subscribed_visitors, "links": generate_pagination_links(request.url, subscribed_visitors), })
async def visitor_get_many_bookmarked( request, *, req_args=None, req_body=None, query_params=None, requester=None, **kwargs, ): visitors = await get_bookmarked_visitors(Visitor, BookmarkVisitor, requester["id"], **query_params) return json({ "data": visitors, "links": generate_pagination_links(request.url, visitors) })
async def get_subscribed_staffs_for_visitor_route(request, visitor_id, *, requester=None, req_args=None, query_params=None, **kwargs): visitor_id = visitor_id.strip() subscribed_staffs = await get_subscribed_staffs_for_visitor(visitor_id) return json({ "data": subscribed_staffs, "links": generate_pagination_links(request.url, subscribed_staffs), })
async def visitor_get_many(request, *, req_args, query_params, **kwargs): query_params = query_params or {} exclude_unhandled = req_args.pop("exclude_unhandled", "false") exclude_unhandled = exclude_unhandled.lower() in {"1", "true"} if exclude_unhandled: visitors = await get_handled_chats(Visitor, **query_params) else: visitors = await Visitor.get(many=True, decrease=True, **req_args, **query_params) return { "data": visitors, "links": generate_pagination_links(request.url, visitors) }
async def noti_staff_retrieve(request, *, req_args=None, query_params=None, **kwargs): notifs = await NotificationStaff.get(**req_args, **query_params, many=True, decrease=True) staff_id = req_args["staff_id"] number_of_unread_notis = await get_number_of_unread_notifications_for_staff( staff_id, NotificationStaffRead, NotificationStaff) return { "data": notifs, "num_of_unread": number_of_unread_notis, "links": generate_pagination_links(request.url, notifs), }
async def get_subscribed_staffs_for_visitor_route(request, *, query_params=None, **kwargs): query_params = query_params or {} flagged_visitors = await get_non_normal_visitors( ChatFlagged, **query_params, extra_fields=[ "chat_flagged.flag_message AS flag_message", "chat_flagged.created_at AS flagged_timestamp", ], ) return json({ "data": flagged_visitors, "links": generate_pagination_links(request.url, flagged_visitors), })
async def user_retrieve(req, *, req_args, req_body, requester=None, many=True, query_params, **kwargs): # The requester can only get the users from his org if "organisation_id" not in requester: raise_permission_exception() req_args["organisation_id"] = requester["organisation_id"] data = await User.get(**req_args, many=many, **query_params) if many: return { "data": data, "links": generate_pagination_links(req.url, data) } return {"data": data}
async def get_chat_messages_of_visitor(request, visitor_id, *, requester, req_args=None, query_params=None, **kwargs): visitor_id = visitor_id.strip() req_args = req_args or {} query_params = query_params or {} starts_from_unread = to_boolean(req_args.pop("starts_from_unread", False)) # Take the before_id, after_id and starts_from_unread allowed_args = {"before_id", "after_id"} for key in set(list(req_args.keys()) + list(query_params.keys())): if key in allowed_args: kwargs[key] = req_args.pop(key, None) or query_params.pop( key, None) before_id = kwargs.pop("before_id", None) after_id = kwargs.pop("after_id", None) if before_id and starts_from_unread: raise InvalidUsage( "Both fields 'before_id' and 'starts_from_unread' cannot present in the same request" ) if after_id and starts_from_unread: raise InvalidUsage( "Both fields 'after_id' and 'starts_from_unread' cannot present in the same request" ) if after_id and before_id: raise InvalidUsage( "Both fields 'before_id' and 'after_id' cannot present in the same request" ) # Ensure that the chat exists try: chat = await Chat.get(visitor_id=visitor_id) except NotFound: messages = [] else: last_read_msg_id = None exclude = after_id is not None or before_id is not None if starts_from_unread: last_read_message = await ChatMessageSeen.get_or_create( staff_id=requester["id"], chat_id=chat["id"]) last_read_msg_id = last_read_message["last_seen_msg_id"] # If the chat hasnt been read at all, starts from top if not last_read_msg_id: first_msg = await ChatMessage.get_first_message_of_chat( chat["id"]) # If there is no chat messages at all, return [] if not first_msg: return json({"data": [], "links": {}}) before_id = None after_id = first_msg["id"] exclude = False else: before_id = last_read_msg_id exclude = False # after_id = after_id or last_read_msg_id messages = await ChatMessage.get( chat_id=chat["id"], **req_args, **query_params, before_id=before_id, after_id=after_id, exclude=exclude, ) prev_link = generate_pagination_links( request.url, messages, field="before_id", index=0, exclude={"after_id", "starts_from_unread"}, ).get("next") next_link = generate_pagination_links( request.url, messages, field="after_id", index=-1, exclude={"before_id", "starts_from_unread"}, ).get("next") links = {} if prev_link: links["prev"] = prev_link if next_link: links["next"] = next_link return json({"data": messages, "links": links})