diff --git a/canal/on_subject.go b/canal/on_subject.go index 0cc87d0a..f96b7354 100644 --- a/canal/on_subject.go +++ b/canal/on_subject.go @@ -44,7 +44,11 @@ func (e *eventHandler) OnSubjectField(ctx context.Context, key json.RawMessage, func (e *eventHandler) onSubjectChange(ctx context.Context, subjectID model.SubjectID, op string) error { switch op { - case opCreate, opUpdate, opSnapshot: + case opCreate: + if err := e.search.OnSubjectAdded(ctx, subjectID); err != nil { + return errgo.Wrap(err, "search.OnSubjectAdded") + } + case opUpdate, opSnapshot: if err := e.search.OnSubjectUpdate(ctx, subjectID); err != nil { return errgo.Wrap(err, "search.OnSubjectUpdate") } diff --git a/go.mod b/go.mod index c2cc018f..064306e4 100644 --- a/go.mod +++ b/go.mod @@ -32,7 +32,7 @@ require ( github.com/trim21/errgo v0.0.3 github.com/trim21/go-phpserialize v0.1.0-alpha.5 github.com/trim21/htest v0.0.4 - github.com/trim21/pkg v0.0.3 + github.com/trim21/pkg v0.0.4 go.uber.org/fx v1.23.0 go.uber.org/zap v1.27.0 golang.org/x/crypto v0.28.0 diff --git a/go.sum b/go.sum index 7852e99c..7a7179cb 100644 --- a/go.sum +++ b/go.sum @@ -195,8 +195,8 @@ github.com/trim21/go-phpserialize v0.1.0-alpha.5 h1:bMsUpfwAgPggQzDKdafNBvkPWDCM github.com/trim21/go-phpserialize v0.1.0-alpha.5/go.mod h1:/3zMYuOzpcKOevwP3ZN0WxdVRaB3CzJh5T2i41QPgRQ= github.com/trim21/htest v0.0.4 h1:dDIzKNdIClgtB158DlO+Xf0sfwNycmx3kfo/FJuY+eE= github.com/trim21/htest v0.0.4/go.mod h1:W+zaYAGCBqx38eMrMGvXrALnbcXR6OBtZiRiHahgo+E= -github.com/trim21/pkg v0.0.3 h1:uAqfoFmmYiIMOSretKj8/tvrQs3KG57020Ff0cx8UtE= -github.com/trim21/pkg v0.0.3/go.mod h1:JrRIFidkCLeuU5j0vBP5ZN0NOp2JavagHZNr4D3AH6Q= +github.com/trim21/pkg v0.0.4 h1:0nYODKdqNUzmUaPFvqSiR420u2uXQgIYyVyiNfH7olc= +github.com/trim21/pkg v0.0.4/go.mod h1:edl6xdqBOJrhMuIGvcY2lg5L9cqp/hVuwHRM/kdzbMg= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= diff --git a/internal/mocks/SearchClient.go b/internal/mocks/SearchClient.go index cdf74f07..2311ae08 100644 --- a/internal/mocks/SearchClient.go +++ b/internal/mocks/SearchClient.go @@ -100,6 +100,53 @@ func (_c *SearchClient_Handle_Call) RunAndReturn(run func(echo.Context) error) * return _c } +// OnSubjectAdded provides a mock function with given fields: ctx, id +func (_m *SearchClient) OnSubjectAdded(ctx context.Context, id uint32) error { + ret := _m.Called(ctx, id) + + if len(ret) == 0 { + panic("no return value specified for OnSubjectAdded") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, uint32) error); ok { + r0 = rf(ctx, id) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// SearchClient_OnSubjectAdded_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'OnSubjectAdded' +type SearchClient_OnSubjectAdded_Call struct { + *mock.Call +} + +// OnSubjectAdded is a helper method to define mock.On call +// - ctx context.Context +// - id uint32 +func (_e *SearchClient_Expecter) OnSubjectAdded(ctx interface{}, id interface{}) *SearchClient_OnSubjectAdded_Call { + return &SearchClient_OnSubjectAdded_Call{Call: _e.mock.On("OnSubjectAdded", ctx, id)} +} + +func (_c *SearchClient_OnSubjectAdded_Call) Run(run func(ctx context.Context, id uint32)) *SearchClient_OnSubjectAdded_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(uint32)) + }) + return _c +} + +func (_c *SearchClient_OnSubjectAdded_Call) Return(_a0 error) *SearchClient_OnSubjectAdded_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *SearchClient_OnSubjectAdded_Call) RunAndReturn(run func(context.Context, uint32) error) *SearchClient_OnSubjectAdded_Call { + _c.Call.Return(run) + return _c +} + // OnSubjectDelete provides a mock function with given fields: ctx, id func (_m *SearchClient) OnSubjectDelete(ctx context.Context, id uint32) error { ret := _m.Called(ctx, id) diff --git a/internal/search/client.go b/internal/search/client.go index dc79300c..62a3d30e 100644 --- a/internal/search/client.go +++ b/internal/search/client.go @@ -150,6 +150,26 @@ func (c *client) Close() { } } +// OnSubjectAdded is the hook called by canal. +func (c *client) OnSubjectAdded(ctx context.Context, id model.SubjectID) error { + s, err := c.subjectRepo.Get(ctx, id, subject.Filter{}) + if err != nil { + if errors.Is(err, gerr.ErrNotFound) { + return nil + } + return errgo.Wrap(err, "subjectRepo.Get") + } + + if s.Redirect != 0 || s.Ban != 0 { + return c.OnSubjectDelete(ctx, id) + } + + extracted := extractSubject(&s) + + _, err = c.subjectIndex.UpdateDocumentsWithContext(ctx, extracted, "id") + return err +} + // OnSubjectUpdate is the hook called by canal. func (c *client) OnSubjectUpdate(ctx context.Context, id model.SubjectID) error { s, err := c.subjectRepo.Get(ctx, id, subject.Filter{}) diff --git a/internal/search/handle.go b/internal/search/handle.go index 4df44682..b5842d2f 100644 --- a/internal/search/handle.go +++ b/internal/search/handle.go @@ -38,8 +38,10 @@ import ( type Client interface { Handler - OnSubjectUpdate(ctx context.Context, id model.SubjectID) error Close() + + OnSubjectAdded(ctx context.Context, id model.SubjectID) error + OnSubjectUpdate(ctx context.Context, id model.SubjectID) error OnSubjectDelete(ctx context.Context, id model.SubjectID) error } diff --git a/internal/search/noop.go b/internal/search/noop.go index 0a929174..bf68f32d 100644 --- a/internal/search/noop.go +++ b/internal/search/noop.go @@ -28,6 +28,8 @@ var _ Client = NoopClient{} type NoopClient struct { } +func (n NoopClient) OnSubjectAdded(ctx context.Context, id model.SubjectID) error { return nil } + func (n NoopClient) Handle(c echo.Context) error { return c.String(http.StatusOK, "search is not enable") }