Example #1
0
def view_conversation(session, thread_id):
    party_id = session.get('party_id')
    logger.info("Getting conversation", thread_id=thread_id, party_id=party_id)
    # TODO, do we really want to do a GET every time, even if we're POSTing? Rops does it this
    # way so we can get it working, then get it right.
    conversation = get_conversation(thread_id)
    logger.info('Successfully retrieved conversation',
                thread_id=thread_id,
                party_id=party_id)
    try:
        refined_conversation = [
            refine(message) for message in reversed(conversation['messages'])
        ]
    except KeyError as e:
        logger.error('Message is missing important data',
                     thread_id=thread_id,
                     party_id=party_id)
        raise e

    if refined_conversation[-1]['unread']:
        remove_unread_label(refined_conversation[-1]['message_id'])

    form = SecureMessagingForm(request.form)
    form.subject.data = refined_conversation[0].get('subject')

    if not conversation['is_closed']:
        if form.validate_on_submit():
            logger.info("Sending message",
                        thread_id=thread_id,
                        party_id=party_id)
            send_message(
                _get_message_json(form,
                                  refined_conversation[0],
                                  party_id=session['party_id']))
            logger.info("Successfully sent message",
                        thread_id=thread_id,
                        party_id=party_id)
            thread_url = url_for("secure_message_bp.view_conversation",
                                 thread_id=thread_id) + "#latest-message"
            flash(
                Markup('Message sent. <a href={}>View Message</a>'.format(
                    thread_url)))
            return redirect(
                url_for('secure_message_bp.view_conversation_list'))

    return render_template('secure-messages/conversation-view.html',
                           form=form,
                           conversation=refined_conversation,
                           conversation_data=conversation)
def _send_new_message(party_id, survey_id, business_id):
    logger.info("Attempting to send message",
                party_id=party_id,
                business_id=business_id)
    form = SecureMessagingForm(request.form)

    subject = form["subject"].data if form["subject"].data else form[
        "hidden_subject"].data
    message_json = {
        "msg_from": party_id,
        "msg_to": ["GROUP"],
        "subject": subject,
        "body": form["body"].data,
        "thread_id": form["thread_id"].data,
        "business_id": business_id,
        "survey_id": survey_id,
    }

    response = conversation_controller.send_message(json.dumps(message_json))

    logger.info("Secure message sent successfully",
                message_id=response["msg_id"],
                party_id=party_id,
                business_id=business_id)
    return response
Example #3
0
def _send_new_message(subject, party_id, category):
    logger.info("Attempting to send message", party_id=party_id)
    form = SecureMessagingForm(request.form)
    message_json = {
        "msg_from": party_id,
        "msg_to": ["GROUP"],
        "subject": subject,
        "body": form["body"].data,
        "thread_id": form["thread_id"].data,
        "category": category,
    }
    response = conversation_controller.send_message(json.dumps(message_json))

    logger.info("Secure message sent successfully", message_id=response["msg_id"], party_id=party_id)
    return response
Example #4
0
def send_message(party_id, survey, ru_ref):
    logger.info('Attempting to send message', party_id=party_id)
    form = SecureMessagingForm(request.form)

    subject = form['subject'].data if form['subject'].data else form[
        'hidden_subject'].data
    message_json = {
        "msg_from": party_id,
        "msg_to": ['GROUP'],
        "subject": subject,
        "body": form['body'].data,
        "thread_id": form['thread_id'].data,
        "ru_id": ru_ref,
        "survey": survey,
    }

    response = conversation_controller.send_message(json.dumps(message_json))

    logger.info('Secure message sent successfully',
                message_id=response['msg_id'],
                party_id=party_id)
    return response
Example #5
0
def _send_new_message(subject, party_id, survey, business_id):
    logger.info('Attempting to send message',
                party_id=party_id,
                business_id=business_id)
    form = SecureMessagingForm(request.form)
    message_json = {
        "msg_from": party_id,
        "msg_to": ['GROUP'],
        "subject": subject,
        "body": form['body'].data,
        "thread_id": form['thread_id'].data,
        "business_id": business_id,
        "survey": survey,
    }

    response = conversation_controller.send_message(json.dumps(message_json))

    logger.info('Secure message sent successfully',
                message_id=response['msg_id'],
                party_id=party_id,
                business_id=business_id)
    return response
Example #6
0
def view_conversation(session, thread_id):
    """Endpoint to view conversations by thread_id"""
    party_id = session.get_party_id()
    logger.info("Getting conversation", thread_id=thread_id, party_id=party_id)
    conversation = get_conversation(thread_id)
    # secure message will send category in case the conversation is technical or miscellaneous
    is_survey_category = (
        False if "category" in conversation
        and conversation["category"] in ["TECHNICAL", "MISC"] else True)
    # sets appropriate message category
    category = "SURVEY" if is_survey_category else conversation["category"]
    logger.info("Successfully retrieved conversation",
                thread_id=thread_id,
                party_id=party_id)
    try:
        refined_conversation = [
            refine(message) for message in reversed(conversation["messages"])
        ]
    except KeyError:
        logger.error("Message is missing important data",
                     thread_id=thread_id,
                     party_id=party_id)
        raise

    if refined_conversation[-1]["unread"]:
        remove_unread_label(refined_conversation[-1]["message_id"])

    form = SecureMessagingForm(request.form)
    form.subject.data = refined_conversation[0].get("subject")

    if not conversation["is_closed"]:
        if form.validate_on_submit():
            logger.info("Sending message",
                        thread_id=thread_id,
                        party_id=party_id)
            msg_to = get_msg_to(refined_conversation)
            if is_survey_category:
                send_message(
                    _get_message_json(form,
                                      refined_conversation[0],
                                      msg_to=msg_to,
                                      msg_from=party_id))
            else:
                send_message(
                    _get_non_survey_message_json(form,
                                                 refined_conversation[0],
                                                 msg_to=msg_to,
                                                 msg_from=party_id,
                                                 category=category))
            logger.info("Successfully sent message",
                        thread_id=thread_id,
                        party_id=party_id)
            thread_url = url_for("secure_message_bp.view_conversation",
                                 thread_id=thread_id) + "#latest-message"
            flash(
                Markup(f"Message sent. <a href={thread_url}>View Message</a>"))
            return redirect(
                url_for("secure_message_bp.view_conversation_list"))

    unread_message_count = {
        "unread_message_count": get_message_count_from_api(session)
    }
    survey_name = None
    business_name = None
    if is_survey_category:
        try:
            survey_name = get_survey(
                app.config["SURVEY_URL"], app.config["BASIC_AUTH"],
                refined_conversation[-1]["survey_id"]).get("longName")
        except ApiError as exc:
            logger.info("Failed to get survey name, setting to None",
                        status_code=exc.status_code)
        try:
            business_name = conversation["messages"][-1]["@business_details"][
                "name"]
        except (KeyError, TypeError):
            logger.info("Failed to get business name, setting to None")

    return render_template(
        "secure-messages/conversation-view.html",
        form=form,
        conversation=refined_conversation,
        conversation_data=conversation,
        unread_message_count=unread_message_count,
        survey_name=survey_name,
        business_name=business_name,
        category=category,
    )
def view_conversation(session, thread_id):
    """Endpoint to view conversations by thread_id"""
    party_id = session.get_party_id()
    logger.info("Getting conversation", thread_id=thread_id, party_id=party_id)
    conversation = get_conversation(thread_id)
    logger.info('Successfully retrieved conversation',
                thread_id=thread_id,
                party_id=party_id)
    try:
        refined_conversation = [
            refine(message) for message in reversed(conversation['messages'])
        ]
    except KeyError:
        logger.error('Message is missing important data',
                     thread_id=thread_id,
                     party_id=party_id)
        raise

    if refined_conversation[-1]['unread']:
        remove_unread_label(refined_conversation[-1]['message_id'])

    form = SecureMessagingForm(request.form)
    form.subject.data = refined_conversation[0].get('subject')

    if not conversation['is_closed']:
        if form.validate_on_submit():
            logger.info("Sending message",
                        thread_id=thread_id,
                        party_id=party_id)
            msg_to = get_msg_to(refined_conversation)
            send_message(
                _get_message_json(form,
                                  refined_conversation[0],
                                  msg_to=msg_to,
                                  msg_from=party_id))
            logger.info("Successfully sent message",
                        thread_id=thread_id,
                        party_id=party_id)
            thread_url = url_for("secure_message_bp.view_conversation",
                                 thread_id=thread_id) + "#latest-message"
            flash(
                Markup(f'Message sent. <a href={thread_url}>View Message</a>'))
            return redirect(
                url_for('secure_message_bp.view_conversation_list'))

    unread_message_count = {
        'unread_message_count': get_message_count_from_api(session)
    }
    survey_name = None
    try:
        survey_name = get_survey(
            app.config['SURVEY_URL'], app.config['BASIC_AUTH'],
            refined_conversation[-1]['survey_id']).get('longName')
    except ApiError as exc:
        logger.info('Failed to get survey name, setting to None',
                    status_code=exc.status_code)

    business_name = None
    try:
        business_name = conversation['messages'][-1]['@business_details'][
            'name']
    except KeyError:
        logger.info('Failed to get business name, setting to None')

    return render_template('secure-messages/conversation-view.html',
                           form=form,
                           conversation=refined_conversation,
                           conversation_data=conversation,
                           unread_message_count=unread_message_count,
                           survey_name=survey_name,
                           business_name=business_name)