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: add API to get channel info from channel path and get channel path from channel id #2476

Merged
merged 8 commits into from
Nov 3, 2024
37 changes: 36 additions & 1 deletion docs/v3-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1984,6 +1984,11 @@ paths:
in: query
name: include-dm
description: ダイレクトメッセージチャンネルをレスポンスに含めるかどうか
- schema:
type: string
in: query
name: path
description: パスが一致するチャンネルのみを取得する
'/users/{userId}/tags':
parameters:
- $ref: '#/components/parameters/userIdInPath'
Expand Down Expand Up @@ -4279,7 +4284,24 @@ paths:
- me
operationId: changeMyNotifyCitation
description: メッセージ引用通知の設定情報を変更します

'/channels/{channelId}/path':
parameters:
- $ref: '#/components/parameters/channelIdInPath'
get:
summary: 指定したチャンネルパスを取得
tags:
- channel
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/ChannelPath'
'404':
description: Not Found
operationId: getChannelPath
description: 指定したチャンネルのパスを取得します。
components:
securitySchemes:
cookieAuth:
Expand Down Expand Up @@ -7274,6 +7296,19 @@ components:
description: メッセージ引用通知の設定情報
required:
- notifyCitation
ChannelPath:
title: ChannelPath
type: object
description: チャンネルパス
properties:
path:
type: string
description: チャンネルパス
pattern: '^(\/[a-zA-Z0-9-_]+)+$'
required:
- path
example:
path: '/general'
Session:
type: object
properties:
Expand Down
24 changes: 24 additions & 0 deletions router/v3/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,25 @@ import (

// GetChannels GET /channels
func (h *Handlers) GetChannels(c echo.Context) error {
if isTrue(c.QueryParam("include-dm")) && len(c.QueryParam("path")) > 0 {
return herror.BadRequest("include-dm and path cannot be specified at the same time")
}

res := echo.Map{
"public": h.ChannelManager.PublicChannelTree(),
}

if len(c.QueryParam("path")) > 0 {
channelPath := c.QueryParam("path")
channel, err := h.ChannelManager.GetChannelFromPath(channelPath)
if err != nil {
return herror.InternalServerError(err)
}
res = echo.Map{
"public": channel,
}
}

if isTrue(c.QueryParam("include-dm")) {
mapping, err := h.ChannelManager.GetDMChannelMapping(getRequestUserID(c))
if err != nil {
Expand Down Expand Up @@ -387,3 +402,12 @@ func (h *Handlers) GetUserDMChannel(c echo.Context) error {

return c.JSON(http.StatusOK, &DMChannel{ID: ch.ID, UserID: userID})
}

// GetChannelPath GET /channels/:channelID/path
func (h *Handlers) GetChannelPath(c echo.Context) error {
channelID := getParamAsUUID(c, consts.ParamChannelID)

channelPath := h.ChannelManager.GetChannelPathFromID(channelID)

return c.JSON(http.StatusOK, echo.Map{"path": channelPath})
}
1 change: 1 addition & 0 deletions router/v3/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ func (h *Handlers) Setup(e *echo.Group) {
apiChannelsCID.PATCH("/subscribers", h.EditChannelSubscribers, requires(permission.EditChannelSubscription))
apiChannelsCID.GET("/bots", h.GetChannelBots, requires(permission.GetChannel))
apiChannelsCID.GET("/events", h.GetChannelEvents, requires(permission.GetChannel))
apiChannelsCID.GET("/path", h.GetChannelPath, requires(permission.GetChannel))
}
}
apiMessages := api.Group("/messages")
Expand Down
3 changes: 3 additions & 0 deletions service/channel/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var (
ErrChannelNotFound = errors.New("channel not found")
ErrChannelNameConflicts = errors.New("channel name conflicts")
ErrInvalidChannelName = errors.New("invalid channel name")
ErrInvalidChannelPath = errors.New("invalid channel path")
ErrInvalidParentChannel = errors.New("invalid parent channel")
ErrTooDeepChannel = errors.New("too deep channel")
ErrChannelArchived = errors.New("channel archived")
Expand All @@ -23,6 +24,8 @@ var (

type Manager interface {
GetChannel(id uuid.UUID) (*model.Channel, error)
GetChannelPathFromID(id uuid.UUID) string
GetChannelFromPath(path string) (*model.Channel, error)
CreatePublicChannel(name string, parent, creatorID uuid.UUID) (*model.Channel, error)
UpdateChannel(id uuid.UUID, args repository.UpdateChannelArgs) error
PublicChannelTree() Tree
Expand Down
12 changes: 12 additions & 0 deletions service/channel/manager_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ func (m *managerImpl) GetChannel(id uuid.UUID) (*model.Channel, error) {
return ch, nil
}

func (m *managerImpl) GetChannelPathFromID(id uuid.UUID) string {
return m.T.getChannelPath(id)
}

func (m *managerImpl) GetChannelFromPath(path string) (*model.Channel, error) {
id := m.T.getChannelIDFromPath(path)
if id == uuid.Nil {
return nil, ErrInvalidChannelPath
}
return m.GetChannel(id)
}

func (m *managerImpl) CreatePublicChannel(name string, parent, creatorID uuid.UUID) (*model.Channel, error) {
m.T.Lock()
defer m.T.Unlock()
Expand Down
31 changes: 30 additions & 1 deletion service/channel/mock_channel/mock_manager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.