Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update GetUserArticleRecordFile #75

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
87 changes: 86 additions & 1 deletion bbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bbs
import (
"fmt"
"log"
"strings"
"time"
)

Expand Down Expand Up @@ -157,6 +158,22 @@ type WriteBoardConnector interface {
RemoveBoardRecordFileRecord(name string, index uint) error
}

// UserArticleConnector is a connector for bbs who support cached user article records
type UserArticleConnector interface {

// GetUserArticleRecordsPath should return the file path which user article record stores.
GetUserArticleRecordsPath(userID string) (string, error)

// ReadUserArticleRecordFile should return the article record in file.
ReadUserArticleRecordFile(name string) ([]UserArticleRecord, error)

// WriteUserArticleRecordFile write user article records into file.
WriteUserArticleRecordFile(name string, records []UserArticleRecord) error

// AppendUserArticleRecordFile append user article records into file.
AppendUserArticleRecordFile(name string, record UserArticleRecord) error
}

var drivers = make(map[string]Connector)

func Register(drivername string, connector Connector) {
Expand Down Expand Up @@ -248,7 +265,10 @@ func (db *DB) ReadBoardArticleRecordsFile(boardID string) ([]ArticleRecord, erro

recs, err := db.connector.ReadArticleRecordsFile(path)
if err != nil {
log.Println("bbs: get user rec error:", err)
if strings.Contains(err.Error(), "no such file or directory") {
return []ArticleRecord{}, nil
}
log.Println("bbs: ReadArticleRecordsFile error:", err)
return nil, err
}
return recs, nil
Expand Down Expand Up @@ -342,3 +362,68 @@ func (db *DB) ReadBoardRecord(index uint) (*BoardRecord, error) {
func (db *DB) RemoveBoardRecord(index uint) error {
return fmt.Errorf("not implement")
}

// GetUserArticleRecordFile returns aritcle file which user posted.
func (db *DB) GetUserArticleRecordFile(userID string) ([]UserArticleRecord, error) {

recs := []UserArticleRecord{}
uac, ok := db.connector.(UserArticleConnector)
if ok {

path, err := uac.GetUserArticleRecordsPath(userID)
if err != nil {
log.Println("bbs: open file error:", err)
return nil, err
}
log.Println("path:", path)

recs, err = uac.ReadUserArticleRecordFile(path)
if err != nil {
log.Println("bbs: ReadUserArticleRecordFile error:", err)
return nil, err
}
if len(recs) != 0 {
return recs, nil
}

}

boardRecords, err := db.ReadBoardRecords()
if err != nil {
log.Println("bbs: ReadBoardRecords error:", err)
return nil, err
}

shouldSkip := func(boardID string) bool {
if boardID == "ALLPOST" {
return true
}
return false
}

for _, r := range boardRecords {
if shouldSkip(r.BoardID()) {
continue
}

ars, err := db.ReadBoardArticleRecordsFile(r.BoardID())
if err != nil {
log.Println("bbs: ReadBoardArticleRecordsFile error:", err)
return nil, err
}
for _, ar := range ars {
if ar.Owner() == userID {
log.Println("board: ", r.BoardID(), len(recs))
r := userArticleRecord{
"board_id": r.BoardID(),
"title": ar.Title(),
"owner": ar.Owner(),
"article_id": ar.Filename(),
}
recs = append(recs, r)
}
}
}

return recs, nil
}
34 changes: 34 additions & 0 deletions cmd/bbstool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func main() {
case "showuserlist":
showuserlist()
return
case "showuserarticlelist":
// Example: go run ./ --bbshome=../../home/bbs showuserarticlelist --user_id pichu
showuserarticlelist()
return
}

}
Expand Down Expand Up @@ -163,3 +167,33 @@ func showuserlist() {
}

}

func showuserarticlelist() {
err := chkIsDir(bbshome)
if err != nil {
fmt.Printf("showuserarticlelist: error: %v\n", err)
return
}

bbsDB, err := bbs.Open(driverName, "file://"+bbshome)
if err != nil {
fmt.Printf("showuserarticlelist: open db: %v\n", err)
return
}

args := parseArgsToMap(flag.Args())

records, err := bbsDB.GetUserArticleRecordFile(args["user_id"].(string))
if err != nil {
fmt.Printf("showuserarticlelist: ReadUserRecords: %v\n", err)
return
}

for _, r := range records {
// if r.UserID() == "" {
// continue
// }
fmt.Println("titlex:", r.Title())
}

}
23 changes: 23 additions & 0 deletions user_article_record.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package bbs

type UserArticleRecord interface {
BoardID() string
Title() string
Owner() string
ArticleID() string
}

type userArticleRecord map[string]string

func (r userArticleRecord) BoardID() string {
return r["board_id"]
}
func (r userArticleRecord) Title() string {
return r["title"]
}
func (r userArticleRecord) Owner() string {
return r["owner"]
}
func (r userArticleRecord) ArticleID() string {
return r["article_id"]
}