Skip to content

Commit

Permalink
Merge pull request #268 from kyho4515/feature/#231-#push-record-usecase
Browse files Browse the repository at this point in the history
  • Loading branch information
PichuChen authored Sep 10, 2021
2 parents 96bbae1 + fcf6a69 commit 785d34e
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 2 deletions.
38 changes: 37 additions & 1 deletion internal/repository/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,34 @@ type PushRecord interface {
Time() time.Time
}

type Push struct {
appendType string
id string
ipAddr string
text string
time time.Time
}

func (p *Push) Type() string {
return p.appendType
}

func (p *Push) ID() string {
return p.id
}

func (p *Push) IPAddr() string {
return p.ipAddr
}

func (p *Push) Text() string {
return p.text
}

func (p *Push) Time() time.Time {
return p.time
}

func (repo *repository) GetPopularArticles(ctx context.Context) ([]PopularArticleRecord, error) {
// Note: go-bbs has not implemented this yet
// TODO: delegate to repo.db when it is ready
Expand All @@ -37,7 +65,15 @@ func (repo *repository) AppendComment(ctx context.Context, userID, boardID, file
if err != nil {
return nil, err
}
return nil, nil

p := &Push{
appendType: appendType,
id: userID,
ipAddr: "", // not sure how to get IPAddr
text: text,
time: time.Now(),
}
return p, nil
}

func (repo *repository) AppendArticle(ctx context.Context, userID, boardID, title, content string) (bbs.ArticleRecord, error) {
Expand Down
57 changes: 56 additions & 1 deletion internal/usecase/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package usecase
import (
"context"
"fmt"
"strings"
"unicode/utf8"

"github.com/Ptt-official-app/go-bbs"

Expand All @@ -19,7 +21,60 @@ func (usecase *usecase) GetPopularArticles(ctx context.Context) ([]repository.Po
}

func (usecase *usecase) UpdateUsefulness(ctx context.Context, userID, boardID, filename, appendType string) (repository.PushRecord, error) {
return nil, nil
articleRecords, err := usecase.repo.GetBoardArticleRecords(ctx, boardID)

if err != nil {
return nil, fmt.Errorf("UpdateUsefulness error: %w", err)
}

var owner string
for _, record := range articleRecords {
if record.Filename() == filename {
owner = record.Owner()
}
}

if owner == userID {
return nil, fmt.Errorf("UpdateUsefuleness error: Owners cannot push their own article")
}

article, err := usecase.GetBoardArticle(ctx, boardID, filename)
if err != nil {
return nil, fmt.Errorf("UpdateUsefulness error: %w", err)
}

articleStr := string(article)

cur := 0
numRecommend := 0
for {
cur = strings.Index(articleStr[cur:], userID)
if cur < 0 {
break
}

r, _ := utf8.DecodeLastRuneInString(articleStr[:cur])
if r == utf8.RuneError {
return nil, fmt.Errorf("UpdateUsefulness error: DecodeLastRuneError")
}
if numRecommend < 1 && string(r) == "\u2191" {
numRecommend++
}

if numRecommend > -1 && string(r) == "\u2193" {
numRecommend--
}
}

if (appendType == "\u2191" && numRecommend == 1) || (appendType == "\u2193" && numRecommend == -1) {
return nil, fmt.Errorf("User with userID:%s has already pushed", userID)
}

p, err := usecase.repo.AppendComment(ctx, userID, boardID, filename, appendType, "")
if err != nil {
return nil, fmt.Errorf("UpdateUsefulness error: %w", err)
}
return p, nil
}

// AppendComment append comment to specific article
Expand Down
24 changes: 24 additions & 0 deletions internal/usecase/article_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,30 @@ func TestGetPopularArticles(t *testing.T) {
}
}

func TestUpdateUsefulness(t *testing.T) {
repo := &MockRepository{}
userID := "mockUserID"
boardID := "board1"
filename := "filename1"
appendType := "↑"

usecase := NewUsecase(&config.Config{}, repo)

record, err := usecase.UpdateUsefulness(context.TODO(), userID, boardID, filename, appendType)

if err != nil {
t.Errorf("UpdateUsefulness expected err == nil, got %v", err)
}

if record.Type() != appendType {
t.Errorf("Push record with incorrect appendType, want %s, get %s", appendType, record.Type())
}

if record.ID() != userID {
t.Errorf("Push record with incorrect userID, want %s, get %s", userID, record.ID())
}
}

func TestForwardArticleToEmail(t *testing.T) {
repo := &MockRepository{}

Expand Down

0 comments on commit 785d34e

Please sign in to comment.