Esempio n. 1
0
    def process_raw(self, raw_msg_id):
        """Process a raw message for an user, ie makes it a rich 'message'."""
        raw = RawMessage.get(raw_msg_id)
        if not raw:
            log.error('Raw message <{}> not found'.format(raw_msg_id))
            raise NotFound
        log.debug('Retrieved raw message {}'.format(raw_msg_id))

        message = self.qualifier.process_inbound(raw)
        if message.external_msg_id:
            external_refs = Merl._model_class.filter(
                user_id=self.user.user_id,
                external_msg_id=message.external_msg_id)
            if external_refs:
                msg = external_refs[0]
                # message already imported, should update it with identity_id ?
                obj = Message(user=self.user, message_id=msg.message_id)
                if str(msg.identity_id) != self.identity.identity_id:
                    obj.get_db()
                    obj.unmarshall_db()
                    obj.user_identities.append(self.identity.identity_id)
                    obj.marshall_db()
                    obj.save_db()
                    obj.marshall_index()
                    obj.save_index()
                    Merl.create(self.user,
                                external_msg_id=msg.external_msg_id,
                                identity_id=self.identity.identity_id,
                                message_id=msg.message_id)
                raise DuplicateObject(DUPLICATE_MESSAGE_EXC)
        else:
            log.warn('Message without external message_id for raw {}'.format(
                raw.raw_msg_id))
        # store and index Message
        obj = Message(user=self.user)
        obj.unmarshall_dict(message.to_native())
        obj.user_id = uuid.UUID(self.user.user_id)
        obj.user_identities = [uuid.UUID(self.identity.identity_id)]
        obj.message_id = uuid.uuid4()
        obj.date_insert = datetime.datetime.now(tz=pytz.utc)
        obj.date_sort = obj.date_insert
        obj.marshall_db()
        obj.save_db()
        obj.marshall_index()
        obj.save_index()

        if message.external_msg_id:
            # store external_msg_id in lookup table
            # but do not abort if it failed
            try:
                Merl.create(self.user,
                            external_msg_id=obj.external_msg_id,
                            identity_id=obj.user_identity,
                            message_id=obj.message_id)
            except Exception as exc:
                log.exception("UserMessageDelivery failed "
                              "to store message_external_ref : {}".format(exc))
            return obj
Esempio n. 2
0
    def process_raw(self, raw_msg_id):
        """Process a raw message for an user, ie makes it a rich 'message'."""
        raw = RawMessage.get(raw_msg_id)
        if not raw:
            log.error('Raw message <{}> not found'.format(raw_msg_id))
            raise NotFound
        log.debug('Retrieved raw message {}'.format(raw_msg_id))

        message = self.qualifier.process_inbound(raw)

        external_refs = ModelMessageExternalRefLookup.filter(
            user_id=self.user.user_id,
            external_msg_id=message.external_msg_id)
        if external_refs:
            # message already imported, update it with identity_id if needed
            for external_ref in external_refs:
                obj = Message(user=self.user,
                              message_id=external_ref.message_id)
                if str(external_ref.identity_id) != self.identity.identity_id:
                    obj.get_db()
                    obj.unmarshall_db()
                    obj.user_identities.append(self.identity.identity_id)
                    obj.marshall_db()
                    obj.save_db()
                    obj.marshall_index()
                    obj.save_index()
                    MessageExternalRefLookup.create(self.user,
                                                    external_msg_id=external_ref.external_msg_id,
                                                    identity_id=self.identity.identity_id,
                                                    message_id=external_ref.message_id)
            raise DuplicateObject(DUPLICATE_MESSAGE_EXC)

        # store and index Message
        obj = Message(user=self.user)
        obj.unmarshall_dict(message.to_native())
        obj.user_id = uuid.UUID(self.user.user_id)
        obj.user_identities = [uuid.UUID(self.identity.identity_id)]
        obj.message_id = uuid.uuid4()
        obj.date_insert = datetime.datetime.now(tz=pytz.utc)
        obj.date_sort = obj.date_insert
        obj.marshall_db()
        obj.save_db()
        obj.marshall_index()
        obj.save_index()

        # store external_msg_id in lookup table
        # but do not abort if it failed
        try:
            MessageExternalRefLookup.create(self.user,
                                            external_msg_id=obj.external_msg_id,
                                            identity_id=obj.user_identity,
                                            message_id=obj.message_id)
        except Exception as exc:
            log.exception("UserMessageDelivery failed "
                          "to store message_external_ref : {}".format(exc))
        return obj
Esempio n. 3
0
    def delete(self):
        message_id = self.request.swagger_data["message_id"]
        message = ObjectMessage(user=self.user, message_id=message_id)

        try:
            message.get_db()
            message.get_index()
        except NotFound:
            raise ResourceNotFound

        try:
            message.delete_db()
            message.delete_index()
        except Exception as exc:
            raise HTTPServerError(exc)

        return Response(None, 204)
Esempio n. 4
0
    def delete(self):
        message_id = self.request.swagger_data["message_id"]
        message = ObjectMessage(self.user.user_id, message_id=message_id)

        try:
            message.get_db()
            message.get_index()
        except NotFound:
            raise ResourceNotFound

        try:
            message.delete_db()
            message.delete_index()
        except Exception as exc:
            raise HTTPServerError(exc)

        return Response(None, 204)
Esempio n. 5
0
 def update_external_references(self, user):
     """
     copy externals references from current draft's ancestor
     and change parent_id to reflect new message's hierarchy
     :return:
     """
     from caliopen_main.message.objects.message import Message
     parent_msg = Message(user, message_id=self.parent_id)
     parent_msg.get_db()
     parent_msg.unmarshall_db()
     if parent_msg:
         self.external_references = ExternalReferences(
             vars(parent_msg.external_references))
         self.external_references.ancestors_ids.append(
             parent_msg.external_references.message_id)
         self.external_references.parent_id = parent_msg.external_references.message_id
         self.external_references.message_id = ""  # will be set by broker at sending time
Esempio n. 6
0
 def _update_external_references(self, user):
     """
     copy externals references from current draft's ancestor
     and change parent_id to reflect new message's hierarchy
     :return:
     """
     from caliopen_main.message.objects.message import Message
     parent_msg = Message(user, message_id=self.parent_id)
     parent_msg.get_db()
     parent_msg.unmarshall_db()
     if parent_msg:
         self.external_references = ExternalReferences(
             vars(parent_msg.external_references))
         self.external_references.ancestors_ids.append(
             parent_msg.external_references.message_id)
         self.external_references.parent_id = parent_msg.external_references.message_id
         self.external_references.message_id = ""  # will be set by broker at sending time
Esempio n. 7
0
    def _check_discussion_consistency(self, user):
        from caliopen_main.message.objects.message import Message
        new_discussion = False
        if not hasattr(self, 'discussion_id') or self.discussion_id == "" \
                or self.discussion_id is None:
            # no discussion_id provided. Try to find one with draft's parent_id
            # or create new discussion
            if hasattr(self, 'parent_id') \
                    and self.parent_id is not None \
                    and self.parent_id != "":
                parent_msg = Message(user, message_id=self.parent_id)
                try:
                    parent_msg.get_db()
                    parent_msg.unmarshall_db()
                except NotFound:
                    raise err.PatchError(message="parent message not found")
                self.discussion_id = parent_msg.discussion_id
            else:
                discussion = Discussion.create_from_message(user, self)
                self.discussion_id = discussion.discussion_id
                new_discussion = True
        if not new_discussion:
            dim = DIM(user)
            d_id = self.discussion_id
            last_message = dim.get_last_message(d_id, -10, 10, True)
            if last_message == {}:
                raise err.PatchError(
                    message='No such discussion {}'.format(d_id))
            is_a_reply = (str(last_message.message_id) != str(self.message_id))
            if is_a_reply:
                # check participants consistency
                if hasattr(self,
                           "participants") and len(self.participants) > 0:
                    participants = [p['address'] for p in self.participants]
                    last_msg_participants = [
                        p['address'] for p in last_message.participants
                    ]
                    if len(participants) != len(last_msg_participants):
                        raise err.PatchError(
                            message="list of participants "
                            "is not consistent for this discussion")
                    participants.sort()
                    last_msg_participants.sort()

                    for i, participant in enumerate(participants):
                        if participant != last_msg_participants[i]:
                            raise err.PatchConflict(
                                message="list of participants "
                                "is not consistent for this discussion")
                else:
                    self.build_participants_for_reply(user)

                # check parent_id consistency
                if 'parent_id' in self and self.parent_id != "" \
                        and self.parent_id is not None:
                    if not dim.message_belongs_to(
                            discussion_id=self.discussion_id,
                            message_id=self.parent_id):
                        raise err.PatchConflict(message="provided message "
                                                "parent_id does not belong "
                                                "to this discussion")
                else:
                    self.parent_id = last_message.parent_id

                self.update_external_references(user)

            else:
                last_message = None
        else:
            last_message = None

        return last_message