コード例 #1
0
def handle_complaint(ses_message):
    recipient_email = remove_emails_from_complaint(ses_message)[0]
    current_app.logger.info("Complaint from SES: \n{}".format(ses_message))
    try:
        reference = ses_message['mail']['messageId']
    except KeyError as e:
        current_app.logger.exception(
            "Complaint from SES failed to get reference from message", e)
        return
    notification = dao_get_notification_history_by_reference(reference)
    ses_complaint = ses_message.get('complaint', None)

    complaint = Complaint(
        notification_id=notification.id,
        service_id=notification.service_id,
        # TODO: this ID is unique, we should make it a unique index so we don't
        # store duplicate complaints.
        # See https://docs.aws.amazon.com/ses/latest/DeveloperGuide/notification-contents.html#complaint-object
        ses_feedback_id=ses_complaint.get('feedbackId', None)
        if ses_complaint else None,
        complaint_type=ses_complaint.get('complaintFeedbackType', None)
        if ses_complaint else None,
        complaint_date=ses_complaint.get('timestamp', None)
        if ses_complaint else None)
    save_complaint(complaint)
    return complaint, notification, recipient_email
コード例 #2
0
def handle_complaint(ses_message):
    recipient_email = remove_emails_from_complaint(ses_message)[0]
    current_app.logger.info("Complaint from SES: \n{}".format(
        json.dumps(ses_message).replace("{", "(").replace("}", ")")))
    try:
        reference = ses_message["mail"]["messageId"]
    except KeyError as e:
        current_app.logger.exception(
            "Complaint from SES failed to get reference from message", e)
        return
    notification = dao_get_notification_history_by_reference(reference)
    ses_complaint = ses_message.get("complaint", None)

    complaint = Complaint(
        notification_id=notification.id,
        service_id=notification.service_id,
        ses_feedback_id=ses_complaint.get("feedbackId", None)
        if ses_complaint else None,
        complaint_type=ses_complaint.get("complaintFeedbackType", None)
        if ses_complaint else None,
        complaint_date=ses_complaint.get("timestamp", None)
        if ses_complaint else None,
    )
    save_complaint(complaint)
    return complaint, notification, recipient_email
コード例 #3
0
def handle_ses_complaint(
        ses_message: dict) -> Tuple[Complaint, Notification, str]:
    recipient_email = remove_emails_from_complaint(ses_message)[0]
    current_app.logger.info("Complaint from SES: \n{}".format(
        json.dumps(ses_message).replace('{', '(').replace('}', ')')))
    try:
        reference = ses_message['mail']['messageId']
    except KeyError as e:
        current_app.logger.exception(
            "Complaint from SES failed to get reference from message", e)
        return
    notification = dao_get_notification_history_by_reference(reference)
    ses_complaint = ses_message.get('complaint', None)

    complaint_type = ses_complaint.get('complaintFeedbackType',
                                       None) if ses_complaint else None

    if not complaint_type:
        current_app.logger.info(
            f'Received null complaint type from SES for notification {notification.id}'
        )

    complaint = Complaint(notification_id=notification.id,
                          service_id=notification.service_id,
                          feedback_id=ses_complaint.get('feedbackId', None)
                          if ses_complaint else None,
                          complaint_type=complaint_type,
                          complaint_date=ses_complaint.get('timestamp', None)
                          if ses_complaint else None)
    save_complaint(complaint)
    return complaint, notification, recipient_email
コード例 #4
0
def test_fetch_complaint_by_service_returns_one(sample_service,
                                                sample_email_notification):
    complaint = Complaint(notification_id=sample_email_notification.id,
                          service_id=sample_service.id,
                          ses_feedback_id=str(uuid.uuid4()),
                          complaint_type='abuse',
                          complaint_date=datetime.utcnow())

    save_complaint(complaint)

    complaints = fetch_complaints_by_service(service_id=sample_service.id)
    assert len(complaints) == 1
    assert complaints[0] == complaint
コード例 #5
0
def test_fetch_complaint_by_service_return_many(notify_db_session):
    service_1 = create_service(service_name='first')
    service_2 = create_service(service_name='second')
    template_1 = create_template(service=service_1, template_type='email')
    template_2 = create_template(service=service_2, template_type='email')
    notification_1 = create_notification(template=template_1)
    notification_2 = create_notification(template=template_2)
    notification_3 = create_notification(template=template_2)
    complaint_1 = Complaint(notification_id=notification_1.id,
                            service_id=service_1.id,
                            ses_feedback_id=str(uuid.uuid4()),
                            complaint_type='abuse',
                            complaint_date=datetime.utcnow())
    complaint_2 = Complaint(notification_id=notification_2.id,
                            service_id=service_2.id,
                            ses_feedback_id=str(uuid.uuid4()),
                            complaint_type='abuse',
                            complaint_date=datetime.utcnow())
    complaint_3 = Complaint(notification_id=notification_3.id,
                            service_id=service_2.id,
                            ses_feedback_id=str(uuid.uuid4()),
                            complaint_type='abuse',
                            complaint_date=datetime.utcnow(),
                            created_at=datetime.utcnow() +
                            timedelta(minutes=1))

    save_complaint(complaint_1)
    save_complaint(complaint_2)
    save_complaint(complaint_3)

    complaints = fetch_complaints_by_service(service_id=service_2.id)
    assert len(complaints) == 2
    assert complaints[0] == complaint_3
    assert complaints[1] == complaint_2
コード例 #6
0
def handle_complaint(ses_message):
    recipient_email = remove_emails_from_complaint(ses_message)[0]
    current_app.logger.info("Complaint from SES: \n{}".format(ses_message))
    try:
        reference = ses_message['mail']['messageId']
    except KeyError as e:
        current_app.logger.exception(
            "Complaint from SES failed to get reference from message", e)
        return
    notification = dao_get_notification_history_by_reference(reference)
    ses_complaint = ses_message.get('complaint', None)

    complaint = Complaint(
        notification_id=notification.id,
        service_id=notification.service_id,
        ses_feedback_id=ses_complaint.get('feedbackId', None)
        if ses_complaint else None,
        complaint_type=ses_complaint.get('complaintFeedbackType', None)
        if ses_complaint else None,
        complaint_date=ses_complaint.get('timestamp', None)
        if ses_complaint else None)
    save_complaint(complaint)
    return complaint, notification, recipient_email