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

feat: support searching character & person #671

Merged
merged 14 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
5 changes: 4 additions & 1 deletion canal/canal.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (

"github.com/bangumi/server/config"
"github.com/bangumi/server/dal"
"github.com/bangumi/server/internal/character"
"github.com/bangumi/server/internal/person"
"github.com/bangumi/server/internal/pkg/cache"
"github.com/bangumi/server/internal/pkg/driver"
"github.com/bangumi/server/internal/pkg/logger"
Expand Down Expand Up @@ -62,7 +64,8 @@ func Main() error {
fx.Provide(
driver.NewMysqlSqlDB,
driver.NewRueidisClient, logger.Copy, cache.NewRedisCache,
subject.NewMysqlRepo, search.New, session.NewMysqlRepo, session.New,
subject.NewMysqlRepo, character.NewMysqlRepo, person.NewMysqlRepo,
search.New, session.NewMysqlRepo, session.New,
driver.NewS3,
tag.NewCachedRepo,
tag.NewMysqlRepo,
Expand Down
4 changes: 4 additions & 0 deletions canal/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@
err = e.OnSubjectField(ctx, key, p)
case "chii_subjects":
err = e.OnSubject(ctx, key, p)
case "chii_characters":
err = e.OnCharacter(ctx, key, p)
case "chii_persons":
err = e.OnPerson(ctx, key, p)

Check warning on line 130 in canal/event.go

View check run for this annotation

Codecov / codecov/patch

canal/event.go#L127-L130

Added lines #L127 - L130 were not covered by tests
case "chii_members":
err = e.OnUserChange(ctx, key, p)
}
Expand Down
45 changes: 45 additions & 0 deletions canal/on_character.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package canal

import (
"context"
"encoding/json"

"github.com/trim21/errgo"
"go.uber.org/zap"

"github.com/bangumi/server/internal/model"
"github.com/bangumi/server/internal/search"
)

type CharacterKey struct {
ID model.CharacterID `json:"crt_id"`
}

func (e *eventHandler) OnCharacter(ctx context.Context, key json.RawMessage, payload Payload) error {
var k CharacterKey
if err := json.Unmarshal(key, &k); err != nil {
return err
}
return e.onCharacterChange(ctx, k.ID, payload.Op)

Check warning on line 23 in canal/on_character.go

View check run for this annotation

Codecov / codecov/patch

canal/on_character.go#L18-L23

Added lines #L18 - L23 were not covered by tests
}

func (e *eventHandler) onCharacterChange(ctx context.Context, characterID model.CharacterID, op string) error {
switch op {
case opCreate:
if err := e.search.EventAdded(ctx, characterID, search.SearchTargetCharacter); err != nil {
return errgo.Wrap(err, "search.OnCharacterAdded")
}
case opUpdate, opSnapshot:
if err := e.search.EventUpdate(ctx, characterID, search.SearchTargetCharacter); err != nil {
return errgo.Wrap(err, "search.OnCharacterUpdate")
}
case opDelete:
if err := e.search.EventDelete(ctx, characterID, search.SearchTargetCharacter); err != nil {
return errgo.Wrap(err, "search.OnCharacterDelete")
}
default:
e.log.Warn("unexpected operator", zap.String("op", op))

Check warning on line 41 in canal/on_character.go

View check run for this annotation

Codecov / codecov/patch

canal/on_character.go#L26-L41

Added lines #L26 - L41 were not covered by tests
}

return nil

Check warning on line 44 in canal/on_character.go

View check run for this annotation

Codecov / codecov/patch

canal/on_character.go#L44

Added line #L44 was not covered by tests
}
44 changes: 44 additions & 0 deletions canal/on_person.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package canal

import (
"context"
"encoding/json"

"github.com/trim21/errgo"
"go.uber.org/zap"

"github.com/bangumi/server/internal/model"
"github.com/bangumi/server/internal/search"
)

type PersonKey struct {
ID model.PersonID `json:"prsn_id"`
}

func (e *eventHandler) OnPerson(ctx context.Context, key json.RawMessage, payload Payload) error {
var k PersonKey
if err := json.Unmarshal(key, &k); err != nil {
return err
}
return e.onPersonChange(ctx, k.ID, payload.Op)

Check warning on line 23 in canal/on_person.go

View check run for this annotation

Codecov / codecov/patch

canal/on_person.go#L18-L23

Added lines #L18 - L23 were not covered by tests
}

func (e *eventHandler) onPersonChange(ctx context.Context, personID model.PersonID, op string) error {
switch op {
case opCreate:
if err := e.search.EventAdded(ctx, personID, search.SearchTargetPerson); err != nil {
return errgo.Wrap(err, "search.OnPersonAdded")
}
case opUpdate, opSnapshot:
if err := e.search.EventUpdate(ctx, personID, search.SearchTargetPerson); err != nil {
return errgo.Wrap(err, "search.OnPersonUpdate")
}
case opDelete:
if err := e.search.EventDelete(ctx, personID, search.SearchTargetPerson); err != nil {
return errgo.Wrap(err, "search.OnPersonDelete")
}
default:
e.log.Warn("unexpected operator", zap.String("op", op))

Check warning on line 41 in canal/on_person.go

View check run for this annotation

Codecov / codecov/patch

canal/on_person.go#L26-L41

Added lines #L26 - L41 were not covered by tests
}
return nil

Check warning on line 43 in canal/on_person.go

View check run for this annotation

Codecov / codecov/patch

canal/on_person.go#L43

Added line #L43 was not covered by tests
}
23 changes: 12 additions & 11 deletions canal/on_subject.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,17 @@
"go.uber.org/zap"

"github.com/bangumi/server/internal/model"
"github.com/bangumi/server/internal/search"
)

type SubjectKey struct {
ID model.SubjectID `json:"subject_id"`
}

type SubjectFieldKey struct {
ID model.SubjectID `json:"field_sid"`
}

func (e *eventHandler) OnSubject(ctx context.Context, key json.RawMessage, payload Payload) error {
var k SubjectKey
if err := json.Unmarshal(key, &k); err != nil {
Expand All @@ -45,15 +54,15 @@
func (e *eventHandler) onSubjectChange(ctx context.Context, subjectID model.SubjectID, op string) error {
switch op {
case opCreate:
if err := e.search.OnSubjectAdded(ctx, subjectID); err != nil {
if err := e.search.EventAdded(ctx, subjectID, search.SearchTargetSubject); err != nil {

Check warning on line 57 in canal/on_subject.go

View check run for this annotation

Codecov / codecov/patch

canal/on_subject.go#L57

Added line #L57 was not covered by tests
return errgo.Wrap(err, "search.OnSubjectAdded")
}
case opUpdate, opSnapshot:
if err := e.search.OnSubjectUpdate(ctx, subjectID); err != nil {
if err := e.search.EventUpdate(ctx, subjectID, search.SearchTargetSubject); err != nil {

Check warning on line 61 in canal/on_subject.go

View check run for this annotation

Codecov / codecov/patch

canal/on_subject.go#L61

Added line #L61 was not covered by tests
return errgo.Wrap(err, "search.OnSubjectUpdate")
}
case opDelete:
if err := e.search.OnSubjectDelete(ctx, subjectID); err != nil {
if err := e.search.EventDelete(ctx, subjectID, search.SearchTargetSubject); err != nil {

Check warning on line 65 in canal/on_subject.go

View check run for this annotation

Codecov / codecov/patch

canal/on_subject.go#L65

Added line #L65 was not covered by tests
return errgo.Wrap(err, "search.OnSubjectDelete")
}
default:
Expand All @@ -62,11 +71,3 @@

return nil
}

type SubjectKey struct {
ID model.SubjectID `json:"subject_id"`
}

type SubjectFieldKey struct {
ID model.SubjectID `json:"field_sid"`
}
3 changes: 2 additions & 1 deletion cmd/web/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ func start() error {
index.NewMysqlRepo, auth.NewMysqlRepo, episode.NewMysqlRepo, revision.NewMysqlRepo, infra.NewMysqlRepo,
timeline.NewMysqlRepo, pm.NewMysqlRepo, notification.NewMysqlRepo,

dam.New, subject.NewMysqlRepo, subject.NewCachedRepo, person.NewMysqlRepo,
dam.New, subject.NewMysqlRepo, subject.NewCachedRepo,
character.NewMysqlRepo, person.NewMysqlRepo,

tag.NewCachedRepo, tag.NewMysqlRepo,

Expand Down
Loading