Skip to content

Commit

Permalink
Merge pull request #1929 from traPtitech/add_HasThumbnail_to_GET/stamps
Browse files Browse the repository at this point in the history
  • Loading branch information
logica0419 authored Dec 8, 2023
2 parents 0d236c5 + 47df0e8 commit a26a60d
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 22 deletions.
46 changes: 45 additions & 1 deletion docs/v3-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ paths:
schema:
type: array
items:
$ref: '#/components/schemas/Stamp'
$ref: '#/components/schemas/StampWithThumbnail'
operationId: getStamps
tags:
- stamp
Expand Down Expand Up @@ -4805,6 +4805,50 @@ components:
required:
- stampId
- datetime
StampWithThumbnail:
title: StampWithThumbnail
type: object
description: スタンプ情報とサムネイルの有無
properties:
id:
type: string
format: uuid
description: スタンプUUID
name:
type: string
description: スタンプ名
pattern: '^[a-zA-Z0-9_-]{1,32}$'
creatorId:
type: string
description: 作成者UUID
format: uuid
createdAt:
type: string
description: 作成日時
format: date-time
updatedAt:
type: string
description: 更新日時
format: date-time
fileId:
type: string
format: uuid
description: ファイルUUID
isUnicode:
type: boolean
description: Unicode絵文字か
hasThumbnail:
type: boolean
description: サムネイルの有無
required:
- id
- name
- creatorId
- createdAt
- updatedAt
- fileId
- isUnicode
- hasThumbnail
User:
title: User
type: object
Expand Down
6 changes: 6 additions & 0 deletions model/stamps.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ type Stamp struct {
File *FileMeta `gorm:"constraint:stamps_file_id_files_id_foreign,OnUpdate:CASCADE,OnDelete:NO ACTION;foreignKey:FileID" json:"-"`
}

// StampWithThumbnail サムネイル情報を付与したスタンプ構造体
type StampWithThumbnail struct {
*Stamp
HasThumbnail bool `json:"hasThumbnail"`
}

// TableName スタンプテーブル名を取得します
func (*Stamp) TableName() string {
return "stamps"
Expand Down
49 changes: 39 additions & 10 deletions repository/gorm/stamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type stampRepository struct {
db *gorm.DB
hub *hub.Hub
stamps *sc.Cache[struct{}, map[uuid.UUID]*model.Stamp]
perType *sc.Cache[repository.StampType, []*model.Stamp]
perType *sc.Cache[repository.StampType, []*model.StampWithThumbnail]
}

func makeStampRepository(db *gorm.DB, hub *hub.Hub) *stampRepository {
Expand All @@ -48,26 +48,55 @@ func (r *stampRepository) loadStamps(_ context.Context, _ struct{}) (map[uuid.UU
return stampsMap, nil
}

func (r *stampRepository) loadFilteredStamps(ctx context.Context, stampType repository.StampType) ([]*model.Stamp, error) {
func (r *stampRepository) loadFilteredStamps(ctx context.Context, stampType repository.StampType) ([]*model.StampWithThumbnail, error) {
stamps, err := r.stamps.Get(ctx, struct{}{})
if err != nil {
return nil, err
}
arr := make([]*model.Stamp, 0, len(stamps))
arr := make([]*model.StampWithThumbnail, 0, len(stamps))

IDs := make([]uuid.UUID, 0, len(stamps))
stampsWithThumbnail := make([]*model.StampWithThumbnail, 0, len(stamps))
for _, stamp := range stamps {
IDs = append(IDs, stamp.FileID)
}

thumbnails := make([]uuid.UUID, 0, len(stamps))
if err := r.db.
Table("files_thumbnails").
Select("file_id").
Where("file_id IN (?)", IDs).
Find(&thumbnails).
Error; err != nil {
return nil, err
}
thumbnailExists := make(map[uuid.UUID]struct{}, len(thumbnails))
for _, v := range thumbnails {
thumbnailExists[v] = struct{}{}
}

for _, stamp := range stamps {
_, ok := thumbnailExists[stamp.FileID]
stampsWithThumbnail = append(stampsWithThumbnail, &model.StampWithThumbnail{
Stamp: stamp,
HasThumbnail: ok,
})
}

if err != nil {
return nil, err
}
switch stampType {
case repository.StampTypeAll:
for _, s := range stamps {
arr = append(arr, s)
}
arr = append(arr, stampsWithThumbnail...)
case repository.StampTypeUnicode:
for _, s := range stamps {
for _, s := range stampsWithThumbnail {
if s.IsUnicode {
arr = append(arr, s)
}
}
case repository.StampTypeOriginal:
for _, s := range stamps {
for _, s := range stampsWithThumbnail {
if !s.IsUnicode {
arr = append(arr, s)
}
Expand Down Expand Up @@ -270,8 +299,8 @@ func (r *stampRepository) DeleteStamp(id uuid.UUID) (err error) {
return repository.ErrNotFound
}

// GetAllStamps implements StampRepository interface.
func (r *stampRepository) GetAllStamps(stampType repository.StampType) (stamps []*model.Stamp, err error) {
// GetAllStampsWithThumbnail implements StampRepository interface.
func (r *stampRepository) GetAllStampsWithThumbnail(stampType repository.StampType) (stampsWithThumbnail []*model.StampWithThumbnail, err error) {
return r.perType.Get(context.Background(), stampType)
}

Expand Down
44 changes: 38 additions & 6 deletions repository/gorm/stamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,51 @@ func TestRepositoryImpl_DeleteStamp(t *testing.T) {
})
}

func TestRepositoryImpl_GetAllStamps(t *testing.T) {
func TestRepositoryImpl_GetAllStampsWithThumbnail(t *testing.T) {
t.Parallel()
repo, assert, _ := setup(t, ex1)
repo, assert, require := setup(t, ex1)

n := 10

for i := 0; i < 10; i++ {
mustMakeStamp(t, repo, rand, uuid.Nil)
}

arr, err := repo.GetAllStamps(repository.StampTypeAll)
if assert.NoError(err) {
assert.Len(arr, n)
for i := 0; i < 10; i++ {
stamp := mustMakeStamp(t, repo, rand, uuid.Nil)
err := repo.DeleteFileMeta(stamp.FileID)
require.NoError(err)
}

t.Run("without thumbnail", func(t *testing.T) {
t.Parallel()
arr, err := repo.GetAllStampsWithThumbnail(repository.StampTypeAll)
if !assert.NoError(err) {
t.FailNow()
}
assert.Len(arr, n*2)
cnt := 0
for _, s := range arr {
if !s.HasThumbnail {
cnt++
}
}
assert.Equal(n, cnt)
})
t.Run("with thumbnail", func(t *testing.T) {
t.Parallel()
arr, err := repo.GetAllStampsWithThumbnail(repository.StampTypeAll)
if !assert.NoError(err) {
t.FailNow()
}
assert.Len(arr, n*2)
cnt := 0
for _, s := range arr {
if s.HasThumbnail {
cnt++
}
}
assert.Equal(n, cnt)
})
}

func TestRepositoryImpl_StampExists(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions repository/stamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ type StampRepository interface {
// 引数にuuid.Nilを指定した場合、ErrNilIDを返します。
// DBによるエラーを返すことがあります。
DeleteStamp(id uuid.UUID) (err error)
// GetAllStamps 全てのスタンプを取得します
// GetAllStampsWithThumbnail 全てのスタンプとサムネイルの有無を取得します
//
// 成功した場合、スタンプのIDでソートされた配列とnilを返します。
// DBによるエラーを返すことがあります。
GetAllStamps(stampType StampType) (stamps []*model.Stamp, err error)
GetAllStampsWithThumbnail(stampType StampType) (stamps []*model.StampWithThumbnail, err error)
// StampExists 指定したIDのスタンプが存在するかどうかを返します
//
// 存在する場合、trueとnilを返します。
Expand Down
4 changes: 2 additions & 2 deletions router/v1/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (c *EmojiCache) Purge() {

func emojiJSONGenerator(repo repository.Repository) func(_ context.Context, _ struct{}) ([]byte, error) {
return func(_ context.Context, _ struct{}) ([]byte, error) {
stamps, err := repo.GetAllStamps(repository.StampTypeAll)
stamps, err := repo.GetAllStampsWithThumbnail(repository.StampTypeAll)
if err != nil {
return nil, err
}
Expand All @@ -141,7 +141,7 @@ func emojiJSONGenerator(repo repository.Repository) func(_ context.Context, _ st

func emojiCSSGenerator(repo repository.Repository) func(_ context.Context, _ struct{}) ([]byte, error) {
return func(_ context.Context, _ struct{}) ([]byte, error) {
stamps, err := repo.GetAllStamps(repository.StampTypeAll)
stamps, err := repo.GetAllStampsWithThumbnail(repository.StampTypeAll)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion router/v3/stamps.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (h *Handlers) GetStamps(c echo.Context) error {
stampType = repository.StampTypeOriginal
}

stamps, err := h.Repo.GetAllStamps(stampType)
stamps, err := h.Repo.GetAllStampsWithThumbnail(stampType)
if err != nil {
return herror.InternalServerError(err)
}
Expand Down

0 comments on commit a26a60d

Please sign in to comment.