Ejemplo n.º 1
0
    def GetPostList(self, svc, session, params):
        """ handle board/post_list
            List posts in this board in mode 'mode'.
            Start listing from post #'start', till post #'end',
                return at most 'count' posts.
        """
        mode = Util.GetString(params, 'mode', 'normal')
        start = Util.GetInt(params, 'start')
        end = Util.GetInt(params, 'end')
        count = Util.GetInt(params, 'count')

        allow_empty = not start and not end

        if (mode == 'normal'):
            total = self.status.total
        else:
            total = self.PostCount(mode)

        start, end = Util.CheckRange(start, end, count, DEFAULT_GET_POST_COUNT,
                                     total)
        if ((start <= end) and (start >= 1) and (end <= total)):
            bread = BRead.BReadMgr.LoadBRead(session.GetUser().name)
            if (bread != None):
                bread.Load(self.name)
            if (mode == 'normal' or mode == 'digest' or mode == 'mark'
                    or mode == 'sticky' or mode == 'thread'
                    or mode == 'origin'):
                dirf = open(self.GetDirPath(mode), 'rb')
                post = {}
                first = True
                result = '[\n'
                for i in range(start - 1, end):
                    pe = self.GetPostEntry(i, mode, dirf)
                    if (pe is None):
                        continue
                    if (not first):
                        result += ',\n'
                    first = False
                    post = pe.GetInfoExtended(session.GetUser(), self, 'post')
                    post['id'] = i + 1
                    read = True
                    if (bread != None):
                        read = not bread.QueryUnread(pe.id, self.name)
                    post['read'] = read
                    #                    post['filename'] = pe.filename
                    result += json.dumps(post, 'utf-8')
                result += '\n]'
                svc.writedata(result)
                dirf.close()
        else:
            if allow_empty:
                svc.writedata('[]')
            else:
                raise OutOfRange('out of range')

        return
Ejemplo n.º 2
0
 def GetAttachmentReq(self, svc, session, params, id):
     mode = Util.GetString(params, 'mode', 'normal')
     offset = Util.GetInt(params, 'offset')
     if (offset <= 0):
         raise WrongArgs("invalid or lacking offset")
     if ((id >= 1) and (id <= self.status.total)):
         pe = self.GetPostEntry(id - 1, mode)
         attach = Post.ReadAttachment(self.GetBoardPath() + pe.filename,
                                      offset)
         attach = {
             'name': attach[0],
             'content': base64.b64encode(attach[1])
         }
         svc.writedata(json.dumps(attach))
     else:
         raise OutOfRange("invalid post id")
Ejemplo n.º 3
0
    def GetPost(self, svc, session, params, id, start, count):
        mode = Util.GetString(params, 'mode', 'normal')
        if (mode == 'junk' or mode == 'deleted'):
            raise NoPerm("invalid mode!")
        if ((id >= 1) and (id <= self.status.total)):
            pe = self.GetPostEntry(id - 1, mode)
            postpath = self.GetBoardPath() + pe.filename
            post = pe.GetInfo('post')
            post['id'] = id
            postinfo = Post(postpath, pe)
            post = dict(post.items() + postinfo.GetInfo(start, count).items())
            if (post['picattach'] or post['otherattach']):
                post['attachlink'] = Post.GetAttachLink(session, self, pe)
            svc.writedata(json.dumps(post))
            bread = BRead.BReadMgr.LoadBRead(session.GetUser().name)
            bread.Load(self.name)
            bread.MarkRead(pe.id, self.name)
        else:
            raise OutOfRange("invalid post id")

        return
Ejemplo n.º 4
0
    def GetNextPostReq(self, svc, session, params, id):
        direction = Util.GetString(params, 'direction', 'forward')
        bfwd = True
        if (direction == 'backward'):
            bfwd = False
        # idonly / compact / detailed
        mode = svc.get_str(params, 'mode', 'idonly')
        content_len = svc.get_int(params, 'max_lines', 25)

        last_one = bool(svc.get_int(params, 'last_one', 0))
        only_new = bool(svc.get_int(params, 'only_new', 0))

        (next_id, next_xid) = self.GetNextPost(id, bfwd, last_one, only_new,
                                               session.GetUser())
        if next_id < 1:
            raise ServerError("fail to get next post")
        else:
            if mode == 'idonly':
                nextinfo = {}
                nextinfo['nextid'] = next_id
                nextinfo['nextxid'] = next_xid
                svc.writedata(json.dumps(nextinfo))
            else:
                post_info = self.ObtainPost(session, next_id, next_xid, mode,
                                            content_len)
                retry = 0
                while post_info is None and retry < 5:
                    (next_id,
                     next_xid) = self.GetNextPost(id, bfwd, last_one, only_new,
                                                  session.GetUser())
                    if next_id < 1:
                        raise ServerError("fail to get next post")
                    post_info = self.ObtainPost(session, next_id, next_xid,
                                                mode, content_len)
                    retry += 1
                if post_info is None:
                    raise ServerError("fail to get next post, retry exhausted")
                svc.writedata(json.dumps(post_info))