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

Improve text parsing on name registration #44

Merged
merged 2 commits into from
May 5, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ clean:
rm -rf $(FUNC_BOT)/vendor
rm -rf $(FUNC_BOT)/messaging
rm -rf pkg/mock
rm -rf pkg/wire/wire_gen.go
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wire_gen.go を削除してからでないと go generate がSEGVで死ぬ、うーんなぜだ


prepare:
cd pkg \
Expand All @@ -20,12 +21,13 @@ prepare:

generate:
cd pkg \
&& GOFLAGS=-mod=mod go generate ./...
&& go generate ./... \
&& cd wire && wire gen

test: generate
test: clean generate
cd pkg && PHASE=test gotest -v ./... $(ARGS)

ci-test: prepare build
ci-test: build
cd pkg && PHASE=test go test -json ./... | tee /tmp/gotest.log | gotestfmt

develop:
Expand Down
23 changes: 17 additions & 6 deletions pkg/messaging/handler/bot_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,12 @@ func (bh *BotHandlerImpl) handleTextMessage(event *linebot.Event, message *lineb
if group.Status == store.GROUP_CREATED {
// This group is under setting up as it doesn't have sufficient members.
// Handle messages to join to group only.
if strings.HasSuffix(message.Text, JOIN_MESSAGE_SUFFIX) {
if err := bh.addNewMember(event, group, message.Text); err != nil {
memberName := bh.extractMemberName(message.Text)
if memberName != "" {
if err := bh.addNewMember(event, group, memberName); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -232,13 +234,22 @@ func (bh *BotHandlerImpl) handleTextMessage(event *linebot.Event, message *lineb
return nil
}

func (bh *BotHandlerImpl) addNewMember(event *linebot.Event, group *store.Group, text string) error {
func (bh *BotHandlerImpl) extractMemberName(text string) string {
trimmed := strings.Split(text, "\n")[0]
trimmed = strings.TrimRight(trimmed, "ー〜!!")
if !strings.HasSuffix(trimmed, JOIN_MESSAGE_SUFFIX) {
return ""
}

name := strings.TrimSuffix(trimmed, JOIN_MESSAGE_SUFFIX)
name = strings.Trim(name, "  ")
return name
}

func (bh *BotHandlerImpl) addNewMember(event *linebot.Event, group *store.Group, memberName string) error {
// FIXME: Need to consider multiple users are added to the group simultaneously.
// FIXME: Need to consider nickname (user) validation.

memberName := strings.TrimSuffix(text, JOIN_MESSAGE_SUFFIX)
memberName = strings.Trim(memberName, " \n")

senderID := event.Source.UserID
group.Members[senderID] = store.NewUser(senderID, memberName, 0)

Expand Down
21 changes: 17 additions & 4 deletions pkg/messaging/handler/bot_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"testing"
"time"

"go.uber.org/mock/gomock"
"github.com/line/line-bot-sdk-go/v7/linebot"
"github.com/raahii/haraiai/pkg/store"
"github.com/raahii/haraiai/pkg/timeutil"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)

func TestHandleTextMessage_addNewMember_firstPerson_success(t *testing.T) {
Expand All @@ -25,6 +25,12 @@ func TestHandleTextMessage_addNewMember_firstPerson_success(t *testing.T) {
[]*store.User{},
)

s.
EXPECT().
GetGroup(group.ID).
Return(group, nil).
Times(1)

// Expect to reply text message.
expectedMessage := "太郎さんだね!👍"
b.
Expand Down Expand Up @@ -56,7 +62,8 @@ func TestHandleTextMessage_addNewMember_firstPerson_success(t *testing.T) {

// Test handler.handleTextMessage call.
event := newTestMessageEvent(REPLY_TOKEN, linebot.EventSourceTypeGroup, GROUP_ID, SENDER_ID)
err := target.addNewMember(event, group, "太郎だよ")
message := newTextMessage("太郎だよ")
err := target.handleTextMessage(event, message)

assert.Nil(t, err)
}
Expand All @@ -72,6 +79,11 @@ func TestHandleTextMessage_addNewMember_secondPerson_success(t *testing.T) {
store.GROUP_CREATED,
[]*store.User{newTaroUser(0)},
)
s.
EXPECT().
GetGroup(group.ID).
Return(group, nil).
Times(1)

// Expect to reply text message.
expectedMessage := "花子さんだね!👍"
Expand Down Expand Up @@ -104,9 +116,10 @@ func TestHandleTextMessage_addNewMember_secondPerson_success(t *testing.T) {
assert.Equal(t, int64(0), newUser.InitialPayAmount)
})

// Test handler.handleTextMessage call.
// Test handler.handleTextMessage call.
event := newTestMessageEvent(REPLY_TOKEN, linebot.EventSourceTypeGroup, GROUP_ID, SENDER_ID)
err := target.addNewMember(event, group, "花子だよ")
message := newTextMessage("花子 だよ〜!")
err := target.handleTextMessage(event, message)

assert.Nil(t, err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/wire/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading