Fix empty string in channels field in GetKeys route

This commit is contained in:
Mike Schwörer 2023-06-18 02:34:04 +02:00
parent 686f89f75d
commit 90830fe384
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
2 changed files with 9 additions and 3 deletions

View File

@ -45,12 +45,12 @@ func (h APIHandler) ListUserKeys(g *gin.Context) ginresp.HTTPResponse {
return *permResp return *permResp
} }
clients, err := h.database.ListKeyTokens(ctx, u.UserID) toks, err := h.database.ListKeyTokens(ctx, u.UserID)
if err != nil { if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query keys", err) return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query keys", err)
} }
res := langext.ArrMap(clients, func(v models.KeyToken) models.KeyTokenJSON { return v.JSON() }) res := langext.ArrMap(toks, func(v models.KeyToken) models.KeyTokenJSON { return v.JSON() })
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{Keys: res})) return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{Keys: res}))
} }

View File

@ -129,6 +129,12 @@ type KeyTokenDB struct {
} }
func (k KeyTokenDB) Model() KeyToken { func (k KeyTokenDB) Model() KeyToken {
channels := make([]ChannelID, 0)
if strings.TrimSpace(k.Channels) != "" {
channels = langext.ArrMap(strings.Split(k.Channels, ";"), func(v string) ChannelID { return ChannelID(v) })
}
return KeyToken{ return KeyToken{
KeyTokenID: k.KeyTokenID, KeyTokenID: k.KeyTokenID,
Name: k.Name, Name: k.Name,
@ -136,7 +142,7 @@ func (k KeyTokenDB) Model() KeyToken {
TimestampLastUsed: timeOptFromMilli(k.TimestampLastUsed), TimestampLastUsed: timeOptFromMilli(k.TimestampLastUsed),
OwnerUserID: k.OwnerUserID, OwnerUserID: k.OwnerUserID,
AllChannels: k.AllChannels, AllChannels: k.AllChannels,
Channels: langext.ArrMap(strings.Split(k.Channels, ";"), func(v string) ChannelID { return ChannelID(v) }), Channels: channels,
Token: k.Token, Token: k.Token,
Permissions: ParseTokenPermissionList(k.Permissions), Permissions: ParseTokenPermissionList(k.Permissions),
MessagesSent: k.MessagesSent, MessagesSent: k.MessagesSent,