def participants(self):
     "one sender only for now"
     return [
         TwitterParticipant("To",
                            clean_twitter_address(self.recipient_name)),
         TwitterParticipant("From", clean_twitter_address(self.sender_name))
     ]
 def __init__(self, type, screen_name):
     """Parse a twitter address and create a participant."""
     self.type = type
     self.address = clean_twitter_address(screen_name)
     self.label = screen_name
    def process_inbound(self, raw):
        """
        Process inbound message.

        @param raw: a RawMessage object
            which should be a json conforming to
            https://developer.twitter.com/en/docs/direct-messages/\
            sending-and-receiving/guides/message-create-object
        @rtype: NewMessage
        """
        tweet = TwitterDM(raw.raw_data)
        new_message = NewInboundMessage()
        new_message.raw_msg_id = raw.raw_msg_id
        new_message.body_plain = tweet.body_plain
        new_message.date = tweet.date
        new_message.protocol = tweet.protocol
        new_message.is_unread = True
        new_message.is_draft = False
        new_message.is_answered = False
        new_message.is_received = True
        new_message.importance_level = 0  # XXX tofix on parser
        new_message.external_references = tweet.external_references

        participants = []
        for p in tweet.participants:
            p.address = clean_twitter_address(p.address)
            participant, contact = self.get_participant(tweet, p)
            new_message.participants.append(participant)
            participants.append((participant, contact))

        if not participants:
            raise Exception("no participant found in raw tweet {}".format(
                raw.raw_msg_id))

        # Compute PI !!
        # TODO

        # compute tags
        self._get_tags(new_message)
        if new_message.tags:
            log.debug('Resolved tags {}'.format(new_message.tags))

        # build discussion_id from lookup_sequence
        lookup_sequence, discussion_id = self.lookup_discussion_sequence(
            new_message)
        log.debug('Lookup with sequence {} gives {}'.format(
            lookup_sequence, discussion_id))
        new_message.discussion_id = discussion_id

        # upsert lookup tables
        discuss = Discussion(self.user)
        discuss.upsert_lookups_for_participants(new_message.participants)
        # Format features
        new_message.privacy_features = \
            marshal_features(new_message.privacy_features)
        try:
            new_message.validate()
        except Exception as exc:
            log.error("validation failed with error : « {} » \
                for new_message {}[dump : {}]".format(exc, new_message,
                                                      vars(new_message)))
            raise exc

        return new_message