Skip to content

Commit

Permalink
Merge pull request #2476 from Eraxyso/master
Browse files Browse the repository at this point in the history
feat: add API to get channel info from channel path and get channel path from channel id
  • Loading branch information
ramdos0207 authored Nov 3, 2024
2 parents 0e137ee + c9d1d1b commit c3dae37
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 2 deletions.
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 @@ -4299,7 +4304,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 @@ -7316,6 +7338,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 @@ -194,6 +194,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.

0 comments on commit c3dae37

Please sign in to comment.