Skip to content

Commit

Permalink
Merge pull request #886 from traPtitech/chores
Browse files Browse the repository at this point in the history
Chores
  • Loading branch information
wtks authored May 5, 2020
2 parents 72a1b2f + 7ad4e65 commit 39b1e52
Show file tree
Hide file tree
Showing 35 changed files with 261 additions and 338 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: CI

on: [push]
on:
push:
branches:
- '**'

jobs:
mod:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 100
- uses: actions/setup-go@v1
with:
go-version: 1.14
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ goreleaser-snapshot:
.PHONY: update-frontend
update-frontend:
@mkdir -p ./dev/frontend
@curl -L -Ss https://github.com/traPtitech/traQ_R-UI/releases/latest/download/dist.tar.gz | tar zxv -C ./dev/frontend/ --strip-components=2
@curl -L -Ss https://github.com/traPtitech/traQ_S-UI/releases/latest/download/dist.tar.gz | tar zxv -C ./dev/frontend/ --strip-components=2

.PHONY: reset-frontend
reset-frontend:
Expand Down
22 changes: 3 additions & 19 deletions bot/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,6 @@ func botJoinedAndLeftHandler(p *Processor, ev string, fields hub.Fields) {
p.logger.Error("failed to GetChannel", zap.Error(err), zap.Stringer("id", channelID))
return
}
path, err := p.repo.GetChannelPath(channelID)
if err != nil {
p.logger.Error("failed to GetChannelPath", zap.Error(err), zap.Stringer("id", channelID))
return
}
user, err := p.repo.GetUser(ch.CreatorID, false)
if err != nil && err != repository.ErrNotFound {
p.logger.Error("failed to GetUser", zap.Error(err), zap.Stringer("id", ch.CreatorID))
Expand All @@ -146,7 +141,7 @@ func botJoinedAndLeftHandler(p *Processor, ev string, fields hub.Fields) {

payload := joinAndLeftPayload{
basePayload: makeBasePayload(),
Channel: makeChannelPayload(ch, path, user),
Channel: makeChannelPayload(ch, p.repo.GetChannelTree().GetChannelPath(channelID), user),
}

buf, release, err := p.makePayloadJSON(&payload)
Expand Down Expand Up @@ -193,11 +188,6 @@ func channelCreatedHandler(p *Processor, _ string, fields hub.Fields) {
return
}

path, err := p.repo.GetChannelPath(ch.ID)
if err != nil {
p.logger.Error("failed to GetChannelPath", zap.Error(err), zap.Stringer("id", ch.ID))
return
}
user, err := p.repo.GetUser(ch.CreatorID, false)
if err != nil {
p.logger.Error("failed to GetUser", zap.Error(err), zap.Stringer("id", ch.CreatorID))
Expand All @@ -206,7 +196,7 @@ func channelCreatedHandler(p *Processor, _ string, fields hub.Fields) {

multicast(p, model.BotEventChannelCreated, &channelCreatedPayload{
basePayload: makeBasePayload(),
Channel: makeChannelPayload(ch, path, user),
Channel: makeChannelPayload(ch, p.repo.GetChannelTree().GetChannelPath(ch.ID), user),
}, bots)
}
}
Expand All @@ -231,12 +221,6 @@ func channelTopicUpdatedHandler(p *Processor, _ string, fields hub.Fields) {
return
}

path, err := p.repo.GetChannelPath(ch.ID)
if err != nil {
p.logger.Error("failed to GetChannelPath", zap.Error(err), zap.Stringer("id", ch.ID))
return
}

chCreator, err := p.repo.GetUser(ch.CreatorID, false)
if err != nil && err != repository.ErrNotFound {
p.logger.Error("failed to GetUser", zap.Error(err), zap.Stringer("id", ch.CreatorID))
Expand All @@ -251,7 +235,7 @@ func channelTopicUpdatedHandler(p *Processor, _ string, fields hub.Fields) {

multicast(p, model.BotEventChannelTopicChanged, &channelTopicChangedPayload{
basePayload: makeBasePayload(),
Channel: makeChannelPayload(ch, path, chCreator),
Channel: makeChannelPayload(ch, p.repo.GetChannelTree().GetChannelPath(ch.ID), chCreator),
Topic: topic,
Updater: makeUserPayload(user),
}, bots)
Expand Down
29 changes: 12 additions & 17 deletions dev/Caddyfile
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
{
admin off
}

:80

log stdout
errors stderr
log
root * /usr/share/caddy

root /srv
gzip {
level 1
not /api /img
handle /api/* {
reverse_proxy backend:3000
}

header /service-worker.js Cache-Control "max-age=0"
header /sw.js Cache-Control "max-age=0"

rewrite {
regexp .*
if {path} not_starts_with /api
to {path} /
}
handle {
file_server
try_files {path} /index.html

proxy /api backend:3000 {
transparent
websocket
header /sw.js Cache-Control "max-age=0"
}
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ services:
- mysql

frontend:
image: abiosoft/caddy:1.0.3-no-stats
image: caddy:latest
restart: always
expose:
- "80"
Expand All @@ -28,8 +28,8 @@ services:
depends_on:
- backend
volumes:
- ./dev/Caddyfile:/etc/Caddyfile:ro
- ./dev/frontend:/srv:ro
- ./dev/Caddyfile:/etc/caddy/Caddyfile:ro
- ./dev/frontend:/usr/share/caddy:ro

mysql:
image: mariadb:10.0.19
Expand Down
8 changes: 8 additions & 0 deletions model/user_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ func (ug *UserGroup) IsMember(uid uuid.UUID) bool {
return false
}

func (ug *UserGroup) AdminIDArray() []uuid.UUID {
result := make([]uuid.UUID, len(ug.Admins))
for i, admin := range ug.Admins {
result[i] = admin.UserID
}
return result
}

// UserGroupMember ユーザーグループメンバー構造体
type UserGroupMember struct {
GroupID uuid.UUID `gorm:"type:char(36);not null;primary_key"`
Expand Down
6 changes: 1 addition & 5 deletions notification/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,7 @@ func messageCreatedHandler(ns *Service, ev hub.Message) {
fcmPayload.Path = "/users/" + mUser.GetName()
fcmPayload.SetBodyWithEllipsis(parsed.OneLine())
} else {
path, err := ns.repo.GetChannelPath(m.ChannelID)
if err != nil {
logger.Error("failed to GetChannelPath", zap.Error(err), zap.Stringer("channelId", m.ChannelID))
return
}
path := ns.repo.GetChannelTree().GetChannelPath(m.ChannelID)
fcmPayload.Title = "#" + path
fcmPayload.Path = "/channels/" + path
fcmPayload.SetBodyWithEllipsis(mUser.GetResponseDisplayName() + ": " + parsed.OneLine())
Expand Down
8 changes: 1 addition & 7 deletions repository/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,6 @@ type ChannelRepository interface {
// 存在しないチャンネルを指定した場合は空配列とnilを返します。
// DBによるエラーを返すことがあります。
GetChildrenChannelIDs(channelID uuid.UUID) ([]uuid.UUID, error)
// GetChannelPath 指定したチャンネルのパス文字列を取得する
//
// 成功した場合、パス文字列とnilを返します。
// 存在しないチャンネルを指定した場合、ErrNotFoundを返します。
// DBによるエラーを返すことがあります。
GetChannelPath(id uuid.UUID) (string, error)
// GetPrivateChannelMemberIDs 指定したプライベートチャンネルのメンバーのUUIDを全て取得する
//
// 成功した場合、UUIDの配列とnilを返します。
Expand Down Expand Up @@ -169,6 +163,6 @@ type ChannelRepository interface {
// 存在しないチャンネルを指定した場合、ErrNotFoundを返します。
// DBによるエラーを返すことがあります。
GetChannelStats(channelID uuid.UUID) (*ChannelStats, error)
// GetChannelTree チャンネルツリーを取得します
// GetChannelTree 公開チャンネルツリーを取得します
GetChannelTree() ChannelTree
}
9 changes: 0 additions & 9 deletions repository/channel_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,15 +418,6 @@ func (repo *GormRepository) GetChildrenChannelIDs(channelID uuid.UUID) (children
return repo.chTree.GetChildrenIDs(channelID), nil
}

// GetChannelPath implements ChannelRepository interface.
func (repo *GormRepository) GetChannelPath(id uuid.UUID) (string, error) {
path := repo.chTree.GetChannelPath(id)
if len(path) > 0 {
return path, nil
}
return "", ErrNotFound
}

// GetPrivateChannelMemberIDs implements ChannelRepository interface.
func (repo *GormRepository) GetPrivateChannelMemberIDs(channelID uuid.UUID) (users []uuid.UUID, err error) {
users = make([]uuid.UUID, 0)
Expand Down
87 changes: 0 additions & 87 deletions repository/channel_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,61 +112,6 @@ func TestRepositoryImpl_GetChannel(t *testing.T) {
})
}

func TestRepositoryImpl_GetChannelPath(t *testing.T) {
t.Parallel()
repo, _, _ := setup(t, common)

ch1 := mustMakeChannelDetail(t, repo, uuid.Nil, random, uuid.Nil)
ch2 := mustMakeChannelDetail(t, repo, uuid.Nil, random, ch1.ID)
ch3 := mustMakeChannelDetail(t, repo, uuid.Nil, random, ch2.ID)

t.Run("ch1", func(t *testing.T) {
t.Parallel()
assert := assert.New(t)

path, err := repo.GetChannelPath(ch1.ID)
if assert.NoError(err) {
assert.Equal(ch1.Name, path)
}
})

t.Run("ch2", func(t *testing.T) {
t.Parallel()
assert := assert.New(t)

path, err := repo.GetChannelPath(ch2.ID)
if assert.NoError(err) {
assert.Equal(fmt.Sprintf("%s/%s", ch1.Name, ch2.Name), path)
}
})

t.Run("ch3", func(t *testing.T) {
t.Parallel()
assert := assert.New(t)

path, err := repo.GetChannelPath(ch3.ID)
if assert.NoError(err) {
assert.Equal(fmt.Sprintf("%s/%s/%s", ch1.Name, ch2.Name, ch3.Name), path)
}
})

t.Run("NotExists1", func(t *testing.T) {
t.Parallel()
assert := assert.New(t)

_, err := repo.GetChannelPath(uuid.Nil)
assert.Error(err)
})

t.Run("NotExists2", func(t *testing.T) {
t.Parallel()
assert := assert.New(t)

_, err := repo.GetChannelPath(uuid.Must(uuid.NewV4()))
assert.Error(err)
})
}

func TestRepositoryImpl_ChangeChannelName(t *testing.T) {
t.Parallel()
repo, _, _, parent := setupWithChannel(t, common)
Expand Down Expand Up @@ -234,38 +179,6 @@ func TestRepositoryImpl_ChangeChannelParent(t *testing.T) {
})
}

func TestRepositoryImpl_GetChildrenChannelIDs(t *testing.T) {
t.Parallel()
repo, _, _, c1 := setupWithChannel(t, common)

c2 := mustMakeChannelDetail(t, repo, uuid.Nil, random, c1.ID)
c3 := mustMakeChannelDetail(t, repo, uuid.Nil, random, c2.ID)
c4 := mustMakeChannelDetail(t, repo, uuid.Nil, random, c2.ID)

cases := []struct {
name string
ch uuid.UUID
expect []uuid.UUID
}{
{"c1", c1.ID, []uuid.UUID{c2.ID}},
{"c2", c2.ID, []uuid.UUID{c3.ID, c4.ID}},
{"c3", c3.ID, []uuid.UUID{}},
{"c4", c4.ID, []uuid.UUID{}},
}

for _, v := range cases {
v := v
t.Run(v.name, func(t *testing.T) {
t.Parallel()

ids, err := repo.GetChildrenChannelIDs(v.ch)
if assert.NoError(t, err) {
assert.ElementsMatch(t, ids, v.expect)
}
})
}
}

func TestGormRepository_ChangeChannelSubscription(t *testing.T) {
t.Parallel()
repo, _, _ := setup(t, common)
Expand Down
9 changes: 9 additions & 0 deletions repository/channel_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,23 @@ import (
"sync"
)

// ChannelTree 公開チャンネルのチャンネル階層木
type ChannelTree interface {
// GetChildrenIDs 子チャンネルのIDの配列を取得する
GetChildrenIDs(id uuid.UUID) []uuid.UUID
// GetDescendantIDs 子孫チャンネルのIDの配列を取得する
GetDescendantIDs(id uuid.UUID) []uuid.UUID
// GetAscendantIDs 祖先チャンネルのIDの配列を取得する
GetAscendantIDs(id uuid.UUID) []uuid.UUID
// GetChannelDepth 指定したチャンネル木の深さを取得する
GetChannelDepth(id uuid.UUID) int
// IsChildPresent 指定したnameのチャンネルが指定したチャンネルの子に存在するか
IsChildPresent(name string, parent uuid.UUID) bool
// GetChannelPath 指定したチャンネルのパスを取得する
GetChannelPath(id uuid.UUID) string
// IsChannelPresent 指定したIDのチャンネルが存在するかどうかを取得する
IsChannelPresent(id uuid.UUID) bool
// GetChannelIDFromPath チャンネルパスからチャンネルIDを取得する
GetChannelIDFromPath(path string) uuid.UUID
json.Marshaler
}
Expand Down
1 change: 1 addition & 0 deletions router/consts/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ const (
KeyParamChannel = "paramChannel"
KeyParamFile = "paramFile"
KeyParamClipFolder = "paramClipFolder"
KeyRepo = "_repo"
)
12 changes: 9 additions & 3 deletions router/extension/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import (
"github.com/gofrs/uuid"
jsoniter "github.com/json-iterator/go"
"github.com/labstack/echo/v4"
"github.com/traPtitech/traQ/repository"
"github.com/traPtitech/traQ/router/consts"
"github.com/traPtitech/traQ/router/extension/herror"
"github.com/traPtitech/traQ/router/utils"
)

// CtxKey context.Context用のキータイプ
Expand Down Expand Up @@ -41,9 +44,12 @@ func json(c echo.Context, code int, i interface{}, cfg jsoniter.API) error {
}

// Wrap カスタムコンテキストラッパー
func Wrap() echo.MiddlewareFunc {
func Wrap(repo repository.Repository) echo.MiddlewareFunc {
return func(n echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error { return n(&Context{Context: c}) }
return func(c echo.Context) error {
c.Set(consts.KeyRepo, repo)
return n(&Context{Context: c})
}
}
}

Expand All @@ -57,7 +63,7 @@ func BindAndValidate(c echo.Context, i interface{}) error {
if err := c.Bind(i); err != nil {
return err
}
if err := vd.Validate(i); err != nil {
if err := vd.ValidateWithContext(utils.NewRequestValidateContext(c), i); err != nil {
if e, ok := err.(vd.InternalError); ok {
return herror.InternalServerError(e.InternalError())
}
Expand Down
Loading

0 comments on commit 39b1e52

Please sign in to comment.