示例#1
0
文件: reddit_base.py 项目: vin/reddit
def set_recent_reddits():
    names = read_user_cookie('recent_reddits')
    c.recent_reddits = []
    if names:
        names = filter(None, names.split(','))
        c.recent_reddits = Subreddit._by_fullname(names, data = True,
                                                  return_dict = False)
示例#2
0
def set_recent_reddits():
    names = read_user_cookie("recent_reddits")
    c.recent_reddits = []
    if names:
        names = filter(None, names.strip("[]").split(","))
        try:
            c.recent_reddits = Subreddit._by_fullname(names, data=True, return_dict=False)
        except NotFound:
            pass
示例#3
0
def set_recent_reddits():
    names = read_user_cookie('recent_reddits')
    c.recent_reddits = []
    if names:
        names = filter(None, names.strip('[]').split(','))
        try:
            c.recent_reddits = Subreddit._by_fullname(names, data = True,
                                                      return_dict = False)
        except NotFound:
            pass
示例#4
0
def set_recent_reddits():
    names = read_user_cookie('recent_reddits')
    c.recent_reddits = []
    if names:
        try:
            names = filter(None, names.split(','))
            srs = Subreddit._by_fullname(names, data = True,
                                         return_dict = False)
            # Ensure all the objects returned are Subreddits. Due to the nature
            # of _by_fullname its possible to get any type back
            c.recent_reddits = filter(lambda x: isinstance(x, Subreddit), names)
        except:
            pass
示例#5
0
def set_recent_reddits():
    names = read_user_cookie('recent_reddits')
    c.recent_reddits = []
    if names:
        try:
            names = filter(None, names.split(','))
            srs = Subreddit._by_fullname(names, data=True, return_dict=False)
            # Ensure all the objects returned are Subreddits. Due to the nature
            # of _by_fullname its possible to get any type back
            c.recent_reddits = filter(lambda x: isinstance(x, Subreddit),
                                      names)
        except:
            pass
示例#6
0
    def to_serializable(self, authors_dict=None, entity=None,
                        all_messages=False, current_user=None):
        # Lookup authors if they are not passed
        if not authors_dict:
            from r2.models import Account
            authors_dict = Account._byID(
                    set(self.author_ids) | set(self.mod_action_account_ids),
                    return_dict=True)

        # Lookup entity if it is not passed
        if not entity:
            entity = Subreddit._by_fullname(self.owner_fullname)

        serializable_authors = []

        for message in self.messages:
            author = authors_dict.get(message.author_id)
            serializable_authors.append(
                to_serializable_author(author, entity,
                                       current_user,
                                       is_hidden=message.is_author_hidden)
            )

        last_unread = getattr(self, 'last_unread', None)
        if last_unread is not None:
            last_unread = last_unread.isoformat()

        parsed_last_user_update = None
        parsed_last_mod_update = None

        min_tz_aware_datetime = datetime.min.replace(tzinfo=g.tz)
        if self.last_user_update != min_tz_aware_datetime:
            parsed_last_user_update = self.last_user_update.isoformat()

        if self.last_mod_update != min_tz_aware_datetime:
            parsed_last_mod_update = self.last_mod_update.isoformat()

        result_dict = {
            'state': self.state,
            'lastUpdated': self.last_updated.isoformat(),
            'lastUserUpdate': parsed_last_user_update,
            'lastModUpdate': parsed_last_mod_update,
            'lastUnread': last_unread,
            'isInternal': self.is_internal,
            'isAuto': self.is_auto,
            'numMessages': self.num_messages,
            'owner': {
                'id': self.owner_fullname,
                'type': entity._type_name,
                'displayName': entity.name
            },
            'isHighlighted': self.is_highlighted,
            'id': to36(self.id),
            'subject': self.subject,
            'authors': serializable_authors,
        }

        if all_messages:
            for mod_action in self.mod_actions:
                author = authors_dict.get(mod_action.account_id)
                serializable_authors.append(
                    to_serializable_author(author, entity,
                                           current_user,
                                           is_hidden=False)
                )

            result_dict.update({
                'objIds': self.ordered_msg_and_action_ids,
                'messages': {
                    to36(message.id): message.to_serializable(
                        entity,
                        authors_dict.get(message.author_id),
                        current_user
                    )
                    for message in self.messages
                },
                'modActions': {
                    to36(mod_action.id): mod_action.to_serializable()
                    for mod_action in self.mod_actions
                }
            })
        else:
            result_dict.update({
                'objIds': [
                    {'key': 'messages', 'id': to36(self.messages[0].id)}
                ]
            })

        return result_dict
示例#7
0
    def add_message(self, author, body,
                    is_author_hidden=False,
                    is_internal=False):
        """Add a new message to the conversation."""
        sr = Subreddit._by_fullname(self.owner_fullname)

        # if the conversation is internal, make the message
        # an internal message
        if self.is_internal:
            is_internal = True

        message = ModmailMessage(
            self, author, body, is_author_hidden, is_internal)
        Session.add(message)

        self.num_messages += 1

        is_first_message = (self.num_messages == 1)
        if sr.is_moderator_with_perms(author, 'mail'):
            # Check if a mod who is not the original author of the
            # conversation is responding and if so change the state
            # of the conversation to 'inprogress'. Internal
            # conversations also should not change state to 'inprogress'.
            # Lastly if a conversation is 'archived' change the state
            # to 'inprogress' regardless if the mod has participated or not
            if (not self.is_internal and
                    not is_first_message and
                    (author._id not in self.author_ids or
                     self.state == self.STATE['archived'])):
                self.state = self.STATE['inprogress']

            self.last_mod_update = message.date
        else:
            # Set the state to 'inprogress' only if a mod has responded
            # with a message in the conversation already and the conversation
            # is not already in an 'inprogress' state.
            if (self.last_mod_update is not None and
                    (self.last_mod_update !=
                     datetime.min.replace(tzinfo=g.tz)) and
                    self.state != self.STATE['inprogress']):
                self.state = self.STATE['inprogress']

            self.last_user_update = message.date

        try:
            Session.commit()
        except Exception as e:
            g.log.error('Failed to save message: {}'.format(e))
            Session.rollback()
            raise

        update_sr_mods_modmail_icon(sr)

        # create unread records for all except the author of
        # the newly created message
        ModmailConversationUnreadState.create_unreads(
            self.id,
            list((set(sr.moderators) | set(self.author_ids)) -
                 set([author._id]))
        )

        return message