-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1021 from traPtitech/bot_message_stamps_update_event
BOT_MESSAGE_STAMPS_UPDATEDイベント実装
- Loading branch information
Showing
16 changed files
with
328 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// +build tools | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"net/http" | ||
"net/http/httputil" | ||
) | ||
|
||
func main() { | ||
var port int | ||
flag.IntVar(&port, "p", 5555, "listen port") | ||
flag.Parse() | ||
http.HandleFunc("/", botHandler) | ||
http.ListenAndServe(fmt.Sprintf(":%d", port), nil) | ||
} | ||
|
||
func botHandler(w http.ResponseWriter, r *http.Request) { | ||
dump, err := httputil.DumpRequest(r, true) | ||
if err != nil { | ||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) | ||
return | ||
} | ||
fmt.Printf("%s\n", dump) | ||
|
||
w.WriteHeader(http.StatusNoContent) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
service/bot/event/payload/ev_bot_message_stamps_updated.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package payload | ||
|
||
import ( | ||
"github.com/gofrs/uuid" | ||
"github.com/traPtitech/traQ/model" | ||
"time" | ||
) | ||
|
||
// BotMessageStampsUpdated BOT_MESSAGE_STAMPS_UPDATEDイベントペイロード | ||
type BotMessageStampsUpdated struct { | ||
Base | ||
MessageID uuid.UUID `json:"messageId"` | ||
Stamps []model.MessageStamp `json:"stamps"` | ||
} | ||
|
||
func MakeBotMessageStampsUpdated(eventTime time.Time, mid uuid.UUID, stamps []model.MessageStamp) *BotMessageStampsUpdated { | ||
return &BotMessageStampsUpdated{ | ||
Base: MakeBase(eventTime), | ||
MessageID: mid, | ||
Stamps: stamps, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package handler | ||
|
||
import ( | ||
"fmt" | ||
"github.com/leandro-lugaresi/hub" | ||
"github.com/traPtitech/traQ/service/bot/event" | ||
"github.com/traPtitech/traQ/service/bot/event/payload" | ||
"github.com/traPtitech/traQ/service/message" | ||
"time" | ||
) | ||
|
||
func MessageStampsUpdated(ctx Context, datetime time.Time, _ string, fields hub.Fields) error { | ||
m := fields["message"].(message.Message) | ||
|
||
bot, err := ctx.GetBotByBotUserID(m.GetUserID()) | ||
if err != nil { | ||
return fmt.Errorf("failed to GetBotByBotUserID: %w", err) | ||
} | ||
if bot == nil || !bot.SubscribeEvents.Contains(event.BotMessageStampsUpdated) { | ||
return nil | ||
} | ||
|
||
if err := ctx.Unicast( | ||
event.BotMessageStampsUpdated, | ||
payload.MakeBotMessageStampsUpdated(datetime, m.GetID(), m.GetStamps()), | ||
bot, | ||
); err != nil { | ||
return fmt.Errorf("failed to unicast: %w", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package handler | ||
|
||
import ( | ||
"github.com/gofrs/uuid" | ||
"github.com/golang/mock/gomock" | ||
"github.com/leandro-lugaresi/hub" | ||
"github.com/stretchr/testify/assert" | ||
intevent "github.com/traPtitech/traQ/event" | ||
"github.com/traPtitech/traQ/model" | ||
"github.com/traPtitech/traQ/service/bot/event" | ||
"github.com/traPtitech/traQ/service/bot/event/payload" | ||
"github.com/traPtitech/traQ/service/bot/handler/mock_handler" | ||
"github.com/traPtitech/traQ/service/message" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestMessageStampsUpdated(t *testing.T) { | ||
t.Parallel() | ||
|
||
b := &model.Bot{ | ||
ID: uuid.NewV3(uuid.Nil, "b"), | ||
BotUserID: uuid.NewV3(uuid.Nil, "bu"), | ||
SubscribeEvents: model.BotEventTypesFromArray([]string{event.BotMessageStampsUpdated.String()}), | ||
State: model.BotActive, | ||
} | ||
|
||
t.Run("success", func(t *testing.T) { | ||
t.Parallel() | ||
ctrl := gomock.NewController(t) | ||
handlerCtx := mock_handler.NewMockContext(ctrl) | ||
|
||
registerBot(t, handlerCtx, b) | ||
|
||
m := &messageImpl{ | ||
ID: uuid.NewV3(uuid.Nil, "m"), | ||
UID: uuid.NewV3(uuid.Nil, "bu"), | ||
Stamps: []model.MessageStamp{}, | ||
} | ||
et := time.Now() | ||
|
||
expectUnicast(handlerCtx, event.BotMessageStampsUpdated, payload.MakeBotMessageStampsUpdated(et, m.ID, m.Stamps), b) | ||
assert.NoError(t, MessageStampsUpdated(handlerCtx, et, intevent.MessageStampsUpdated, hub.Fields{ | ||
"message": m, | ||
"message_id": m.ID, | ||
})) | ||
}) | ||
|
||
t.Run("not subscribe BotMessageStampsUpdated", func(t *testing.T) { | ||
t.Parallel() | ||
ctrl := gomock.NewController(t) | ||
handlerCtx := mock_handler.NewMockContext(ctrl) | ||
|
||
b := &model.Bot{ | ||
ID: uuid.NewV3(uuid.Nil, "b"), | ||
BotUserID: uuid.NewV3(uuid.Nil, "bu"), | ||
SubscribeEvents: model.BotEventTypesFromArray([]string{event.MessageCreated.String()}), | ||
State: model.BotActive, | ||
} | ||
registerBot(t, handlerCtx, b) | ||
|
||
m := &messageImpl{ | ||
ID: uuid.NewV3(uuid.Nil, "m"), | ||
UID: b.BotUserID, | ||
Stamps: []model.MessageStamp{}, | ||
} | ||
et := time.Now() | ||
|
||
assert.NoError(t, MessageStampsUpdated(handlerCtx, et, intevent.MessageStampsUpdated, hub.Fields{ | ||
"message": m, | ||
"message_id": m.ID, | ||
})) | ||
}) | ||
} | ||
|
||
type messageImpl struct { | ||
message.Message | ||
ID uuid.UUID | ||
UID uuid.UUID | ||
Stamps []model.MessageStamp | ||
} | ||
|
||
func (m *messageImpl) GetID() uuid.UUID { | ||
return m.ID | ||
} | ||
|
||
func (m *messageImpl) GetUserID() uuid.UUID { | ||
return m.UID | ||
} | ||
|
||
func (m *messageImpl) GetStamps() []model.MessageStamp { | ||
return m.Stamps | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.