Skip to content

Commit

Permalink
Further clean up APIs.
Browse files Browse the repository at this point in the history
  • Loading branch information
robertabcd committed Mar 21, 2017
1 parent 5830756 commit b46ea2e
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 159 deletions.
7 changes: 1 addition & 6 deletions cached_inproc.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,7 @@ func getBoardByNameCached(brdname string) (*pttbbs.Board, error) {
return brd, nil
}

bid, err := ptt.BrdName2Bid(brdname)
if err != nil {
return nil, err
}

board, err := ptt.GetBoard(bid)
board, err := pttbbs.OneBoard(ptt.GetBoards(pttbbs.BoardRefByName(brdname)))
if err != nil {
return nil, err
}
Expand Down
20 changes: 8 additions & 12 deletions cached_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,26 @@ func generateBbsIndex(key cache.Key) (cache.Cacheable, error) {
IsValid: true,
}

count, err := ptt.GetArticleCount(r.Brd.Bid)
if err != nil {
return nil, err
}

// Handle paging
paging := NewPaging(EntryPerPage, count)
paging := NewPaging(EntryPerPage, r.Brd.NumPosts)
if page == 0 {
page = paging.LastPageNo()
paging.SetPageNo(page)
} else if err = paging.SetPageNo(page); err != nil {
} else if err := paging.SetPageNo(page); err != nil {
return nil, err
}
bbsindex.TotalPage = paging.LastPageNo()

// Fetch article list
bbsindex.Articles, err = ptt.GetArticleList(r.Brd.Bid, paging.Cursor())
var err error
bbsindex.Articles, err = ptt.GetArticleList(r.Brd.Ref(), paging.Cursor())
if err != nil {
return nil, err
}

// Fetch bottoms when at last page
if page == paging.LastPageNo() {
bbsindex.Bottoms, err = ptt.GetBottomList(r.Brd.Bid)
bbsindex.Bottoms, err = ptt.GetBottomList(r.Brd.Ref())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -95,7 +91,7 @@ func generateBoardAtomFeed(key cache.Key) (cache.Cacheable, error) {
}

// Fetch article list
articles, err := ptt.GetArticleList(r.Brd.Bid, -20)
articles, err := ptt.GetArticleList(r.Brd.Ref(), -20)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -124,7 +120,7 @@ func generateBoardAtomFeed(key cache.Key) (cache.Cacheable, error) {
const SnippetHeadSize = 16 * 1024 // Enough for 8 pages of 80x24.

func getArticleSnippet(brd pttbbs.Board, filename string) (string, error) {
p, err := ptt.GetArticleSelect(brd.Bid, pttbbs.SelectHead, filename, "", 0, SnippetHeadSize)
p, err := ptt.GetArticleSelect(brd.Ref(), pttbbs.SelectHead, filename, "", 0, SnippetHeadSize)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -243,7 +239,7 @@ func generateArticlePart(key cache.Key) (cache.Cacheable, error) {
r := key.(*ArticlePartRequest)
ctx := context.WithValue(context.TODO(), CtxKeyBoardname, r)

p, err := ptt.GetArticleSelect(r.Brd.Bid, pttbbs.SelectHead, r.Filename, r.CacheKey, r.Offset, -1)
p, err := ptt.GetArticleSelect(r.Brd.Ref(), pttbbs.SelectHead, r.Filename, r.CacheKey, r.Offset, -1)
if err == pttbbs.ErrNotFound {
// Returns an invalid result
return new(ArticlePart), nil
Expand Down
31 changes: 31 additions & 0 deletions pttbbs/board_ref.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package pttbbs

import apipb "github.com/ptt/pttweb/proto/api"

type boardRefByBid BoardID

func (r boardRefByBid) boardRef() *apipb.BoardRef {
return &apipb.BoardRef{Ref: &apipb.BoardRef_Bid{uint32(r)}}
}

func BoardRefByBid(bid BoardID) BoardRef {
return boardRefByBid(bid)
}

func BoardRefsByBid(bids []BoardID) []BoardRef {
refs := make([]BoardRef, len(bids))
for i := range bids {
refs[i] = BoardRefByBid(bids[i])
}
return refs
}

type boardRefByName string

func (r boardRefByName) boardRef() *apipb.BoardRef {
return &apipb.BoardRef{Ref: &apipb.BoardRef_Name{string(r)}}
}

func BoardRefByName(name string) BoardRef {
return boardRefByName(name)
}
114 changes: 29 additions & 85 deletions pttbbs/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pttbbs

import (
"context"
"fmt"
"time"

"google.golang.org/grpc"
Expand All @@ -26,47 +25,10 @@ func NewGrpcRemotePtt(boarddAddr string) (*GrpcRemotePtt, error) {
}, nil
}

func (p *GrpcRemotePtt) getBoard(ref *apipb.BoardRef) (*apipb.Board, error) {
rep, err := p.service.Board(context.TODO(), &apipb.BoardRequest{
Ref: []*apipb.BoardRef{ref},
}, grpcCallOpts...)
if err != nil {
return nil, err
}
if len(rep.Boards) != 1 {
return nil, fmt.Errorf("%v boards received, expected 1", len(rep.Boards))
}
return rep.Boards[0], nil
}

func boardRefByBid(bid int) *apipb.BoardRef {
return &apipb.BoardRef{Ref: &apipb.BoardRef_Bid{int32(bid)}}
}

func boardRefByName(name string) *apipb.BoardRef {
return &apipb.BoardRef{Ref: &apipb.BoardRef_Name{name}}
}

func (p *GrpcRemotePtt) BrdName2Bid(brdname string) (int, error) {
b, err := p.getBoard(boardRefByName(brdname))
if err != nil {
return 0, err
}
return int(b.Bid), nil
}

func (p *GrpcRemotePtt) GetBoard(bid int) (Board, error) {
b, err := p.getBoard(boardRefByBid(bid))
if err != nil {
return Board{}, err
}
return toBoard(b), nil
}

func (p *GrpcRemotePtt) GetBoards(bids []int) ([]Board, error) {
refs := make([]*apipb.BoardRef, len(bids))
for i, bid := range bids {
refs[i] = boardRefByBid(bid)
func (p *GrpcRemotePtt) GetBoards(brefs ...BoardRef) ([]Board, error) {
refs := make([]*apipb.BoardRef, len(brefs))
for i, ref := range brefs {
refs[i] = ref.boardRef()
}
rep, err := p.service.Board(context.TODO(), &apipb.BoardRequest{
Ref: refs,
Expand All @@ -83,46 +45,36 @@ func (p *GrpcRemotePtt) GetBoards(bids []int) ([]Board, error) {

func toBoard(b *apipb.Board) Board {
return Board{
Bid: int(b.Bid),
IsBoard: !hasFlag(b.Attributes, BoardGroup),
Over18: hasFlag(b.Attributes, BoardOver18),
Hidden: false, // All returned boards are public.
BrdName: b.Name,
Title: b.Title,
Class: b.Bclass,
BM: b.RawModerators,
Parent: int(b.Parent),
Nuser: int(b.NumUsers),
Bid: BoardID(b.Bid),
IsBoard: !hasFlag(b.Attributes, BoardGroup),
Over18: hasFlag(b.Attributes, BoardOver18),
Hidden: false, // All returned boards are public.
BrdName: b.Name,
Title: b.Title,
Class: b.Bclass,
BM: b.RawModerators,
Parent: int(b.Parent),
Nuser: int(b.NumUsers),
NumPosts: int(b.NumPosts),
Children: toBoardIDs(b.Children),
}
}

func hasFlag(bits, mask uint32) bool {
return (bits & mask) == mask
}

func (p *GrpcRemotePtt) GetBoardChildren(bid int) ([]int, error) {
b, err := p.getBoard(boardRefByBid(bid))
if err != nil {
return nil, err
}
children := make([]int, len(b.Children))
for i, c := range b.Children {
children[i] = int(c)
func toBoardIDs(bids []uint32) []BoardID {
out := make([]BoardID, len(bids))
for i := range bids {
out[i] = BoardID(bids[i])
}
return children, nil
return out
}

func (p *GrpcRemotePtt) GetArticleCount(bid int) (int, error) {
b, err := p.getBoard(boardRefByBid(bid))
if err != nil {
return 0, err
}
return int(b.NumPosts), nil
func hasFlag(bits, mask uint32) bool {
return (bits & mask) == mask
}

func (p *GrpcRemotePtt) GetArticleList(bid, offset int) ([]Article, error) {
func (p *GrpcRemotePtt) GetArticleList(ref BoardRef, offset int) ([]Article, error) {
rep, err := p.service.List(context.TODO(), &apipb.ListRequest{
Ref: boardRefByBid(bid),
Ref: ref.boardRef(),
IncludePosts: true,
Offset: int32(offset),
Length: 20,
Expand All @@ -137,9 +89,9 @@ func (p *GrpcRemotePtt) GetArticleList(bid, offset int) ([]Article, error) {
return articles, nil
}

func (p *GrpcRemotePtt) GetBottomList(bid int) ([]Article, error) {
func (p *GrpcRemotePtt) GetBottomList(ref BoardRef) ([]Article, error) {
rep, err := p.service.List(context.TODO(), &apipb.ListRequest{
Ref: boardRefByBid(bid),
Ref: ref.boardRef(),
IncludeBottoms: true,
}, grpcCallOpts...)
if err != nil {
Expand All @@ -165,17 +117,9 @@ func toArticle(p *apipb.Post) Article {
}
}

func (p *GrpcRemotePtt) GetArticleContent(bid int, filename string) (content []byte, err error) {
a, err := p.GetArticleSelect(bid, SelectPart, filename, "", 0, -1)
if err != nil {
return nil, err
}
return a.Content, nil
}

func (p *GrpcRemotePtt) GetArticleSelect(bid int, meth SelectMethod, filename, cacheKey string, offset, maxlen int) (*ArticlePart, error) {
func (p *GrpcRemotePtt) GetArticleSelect(ref BoardRef, meth SelectMethod, filename, cacheKey string, offset, maxlen int) (*ArticlePart, error) {
rep, err := p.service.Content(context.TODO(), &apipb.ContentRequest{
BoardRef: boardRefByBid(bid),
BoardRef: ref.boardRef(),
Filename: filename,
ConsistencyToken: cacheKey,
PartialOptions: &apipb.PartialOptions{
Expand Down
57 changes: 38 additions & 19 deletions pttbbs/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,55 @@ package pttbbs
import (
"errors"
"time"

apipb "github.com/ptt/pttweb/proto/api"
)

var (
ErrNotFound = errors.New("not found")
)

type BoardID uint32

type BoardRef interface {
boardRef() *apipb.BoardRef
}

type Pttbbs interface {
GetBoardChildren(bid int) (children []int, err error)
GetBoard(bid int) (brd Board, err error)
GetBoards(bids []int) (brd []Board, err error)
GetArticleCount(bid int) (int, error)
GetArticleList(bid, offset int) ([]Article, error)
GetBottomList(bid int) ([]Article, error)
GetArticleContent(bid int, filename string) ([]byte, error)
BrdName2Bid(brdname string) (int, error)
GetArticleSelect(bid int, meth SelectMethod, filename, cacheKey string, offset, maxlen int) (*ArticlePart, error)
GetBoards(refs ...BoardRef) ([]Board, error)
GetArticleList(ref BoardRef, offset int) ([]Article, error)
GetBottomList(ref BoardRef) ([]Article, error)
GetArticleSelect(ref BoardRef, meth SelectMethod, filename, cacheKey string, offset, maxlen int) (*ArticlePart, error)
Hotboards() ([]Board, error)
}

func OneBoard(boards []Board, err error) (Board, error) {
if err != nil {
return Board{}, err
}
if len(boards) != 1 {
return Board{}, errors.New("expect one board")
}
return boards[0], nil
}

type Board struct {
Bid int
IsBoard bool
Over18 bool
Hidden bool
BrdName string
Title string
Class string
BM string
Parent int
Nuser int
Bid BoardID
IsBoard bool
Over18 bool
Hidden bool
BrdName string
Title string
Class string
BM string
Parent int
Nuser int
NumPosts int
Children []BoardID
}

func (b Board) Ref() BoardRef {
return BoardRefByBid(b.Bid)
}

type Article struct {
Expand Down
Loading

0 comments on commit b46ea2e

Please sign in to comment.