Return more data in /users/{uid}

This commit is contained in:
Mike Schwörer 2023-07-30 16:53:46 +02:00
parent ee9e858584
commit 674714f0f3
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
4 changed files with 112 additions and 31 deletions

View File

@ -349,8 +349,8 @@ func (h APIHandler) UpdateChannel(g *gin.Context) ginresp.HTTPResponse {
descName = langext.Ptr(strings.TrimSpace(*b.DescriptionName)) descName = langext.Ptr(strings.TrimSpace(*b.DescriptionName))
} }
if descName != nil && len(*descName) > user.MaxChannelDescriptionNameLength() { if descName != nil && len(*descName) > user.MaxChannelDescriptionLength() {
return ginresp.APIError(g, 400, apierr.CHANNEL_DESCRIPTION_TOO_LONG, fmt.Sprintf("Channel-Description too long (max %d characters)", user.MaxChannelNameLength()), nil) return ginresp.APIError(g, 400, apierr.CHANNEL_DESCRIPTION_TOO_LONG, fmt.Sprintf("Channel-Description too long (max %d characters)", user.MaxChannelDescriptionLength()), nil)
} }
err := h.database.UpdateChannelDescriptionName(ctx, u.ChannelID, descName) err := h.database.UpdateChannelDescriptionName(ctx, u.ChannelID, descName)

View File

@ -172,11 +172,11 @@ func (h MessageHandler) sendMessageInternal(g *gin.Context, ctx *logic.AppContex
if len(strings.TrimSpace(channelInternalName)) == 0 { if len(strings.TrimSpace(channelInternalName)) == 0 {
return nil, langext.Ptr(ginresp.SendAPIError(g, 400, apierr.CHANNEL_NAME_EMPTY, hl.CHANNEL, fmt.Sprintf("Channel internalname cannot be empty"), nil)) return nil, langext.Ptr(ginresp.SendAPIError(g, 400, apierr.CHANNEL_NAME_EMPTY, hl.CHANNEL, fmt.Sprintf("Channel internalname cannot be empty"), nil))
} }
if SenderName != nil && len(*SenderName) > user.MaxSenderName() { if SenderName != nil && len(*SenderName) > user.MaxSenderNameLength() {
return nil, langext.Ptr(ginresp.SendAPIError(g, 400, apierr.SENDERNAME_TOO_LONG, hl.SENDER_NAME, fmt.Sprintf("SenderName too long (max %d characters)", user.MaxSenderName()), nil)) return nil, langext.Ptr(ginresp.SendAPIError(g, 400, apierr.SENDERNAME_TOO_LONG, hl.SENDER_NAME, fmt.Sprintf("SenderName too long (max %d characters)", user.MaxSenderNameLength()), nil))
} }
if UserMessageID != nil && len(*UserMessageID) > user.MaxUserMessageID() { if UserMessageID != nil && len(*UserMessageID) > user.MaxUserMessageIDLength() {
return nil, langext.Ptr(ginresp.SendAPIError(g, 400, apierr.USR_MSG_ID_TOO_LONG, hl.USER_MESSAGE_ID, fmt.Sprintf("MessageID too long (max %d characters)", user.MaxUserMessageID()), nil)) return nil, langext.Ptr(ginresp.SendAPIError(g, 400, apierr.USR_MSG_ID_TOO_LONG, hl.USER_MESSAGE_ID, fmt.Sprintf("MessageID too long (max %d characters)", user.MaxUserMessageIDLength()), nil))
} }
if SendTimestamp != nil && mathext.Abs(*SendTimestamp-float64(time.Now().Unix())) > timeext.FromHours(user.MaxTimestampDiffHours()).Seconds() { if SendTimestamp != nil && mathext.Abs(*SendTimestamp-float64(time.Now().Unix())) > timeext.FromHours(user.MaxTimestampDiffHours()).Seconds() {
return nil, langext.Ptr(ginresp.SendAPIError(g, 400, apierr.TIMESTAMP_OUT_OF_RANGE, hl.NONE, fmt.Sprintf("The timestamp mus be within %d hours of now()", user.MaxTimestampDiffHours()), nil)) return nil, langext.Ptr(ginresp.SendAPIError(g, 400, apierr.TIMESTAMP_OUT_OF_RANGE, hl.NONE, fmt.Sprintf("The timestamp mus be within %d hours of now()", user.MaxTimestampDiffHours()), nil))

View File

@ -23,17 +23,24 @@ type User struct {
func (u User) JSON() UserJSON { func (u User) JSON() UserJSON {
return UserJSON{ return UserJSON{
UserID: u.UserID, UserID: u.UserID,
Username: u.Username, Username: u.Username,
TimestampCreated: u.TimestampCreated.Format(time.RFC3339Nano), TimestampCreated: u.TimestampCreated.Format(time.RFC3339Nano),
TimestampLastRead: timeOptFmt(u.TimestampLastRead, time.RFC3339Nano), TimestampLastRead: timeOptFmt(u.TimestampLastRead, time.RFC3339Nano),
TimestampLastSent: timeOptFmt(u.TimestampLastSent, time.RFC3339Nano), TimestampLastSent: timeOptFmt(u.TimestampLastSent, time.RFC3339Nano),
MessagesSent: u.MessagesSent, MessagesSent: u.MessagesSent,
QuotaUsed: u.QuotaUsedToday(), QuotaUsed: u.QuotaUsedToday(),
QuotaPerDay: u.QuotaPerDay(), QuotaPerDay: u.QuotaPerDay(),
QuotaRemaining: u.QuotaRemainingToday(), QuotaRemaining: u.QuotaRemainingToday(),
IsPro: u.IsPro, IsPro: u.IsPro,
DefaultChannel: u.DefaultChannel(), DefaultChannel: u.DefaultChannel(),
MaxBodySize: u.MaxContentLength(),
MaxTitleLength: u.MaxTitleLength(),
DefaultPriority: u.DefaultPriority(),
MaxChannelNameLength: u.MaxChannelNameLength(),
MaxChannelDescriptionLength: u.MaxChannelDescriptionLength(),
MaxSenderNameLength: u.MaxSenderNameLength(),
MaxUserMessageIDLength: u.MaxUserMessageIDLength(),
} }
} }
@ -92,15 +99,15 @@ func (u User) MaxChannelNameLength() int {
return 120 return 120
} }
func (u User) MaxChannelDescriptionNameLength() int { func (u User) MaxChannelDescriptionLength() int {
return 300 return 300
} }
func (u User) MaxSenderName() int { func (u User) MaxSenderNameLength() int {
return 120 return 120
} }
func (u User) MaxUserMessageID() int { func (u User) MaxUserMessageIDLength() int {
return 64 return 64
} }
@ -109,17 +116,24 @@ func (u User) MaxTimestampDiffHours() int {
} }
type UserJSON struct { type UserJSON struct {
UserID UserID `json:"user_id"` UserID UserID `json:"user_id"`
Username *string `json:"username"` Username *string `json:"username"`
TimestampCreated string `json:"timestamp_created"` TimestampCreated string `json:"timestamp_created"`
TimestampLastRead *string `json:"timestamp_lastread"` TimestampLastRead *string `json:"timestamp_lastread"`
TimestampLastSent *string `json:"timestamp_lastsent"` TimestampLastSent *string `json:"timestamp_lastsent"`
MessagesSent int `json:"messages_sent"` MessagesSent int `json:"messages_sent"`
QuotaUsed int `json:"quota_used"` QuotaUsed int `json:"quota_used"`
QuotaRemaining int `json:"quota_remaining"` QuotaRemaining int `json:"quota_remaining"`
QuotaPerDay int `json:"quota_max"` QuotaPerDay int `json:"quota_max"`
IsPro bool `json:"is_pro"` IsPro bool `json:"is_pro"`
DefaultChannel string `json:"default_channel"` DefaultChannel string `json:"default_channel"`
MaxBodySize int `json:"max_body_size"`
MaxTitleLength int `json:"max_title_length"`
DefaultPriority int `json:"default_priority"`
MaxChannelNameLength int `json:"max_channel_name_length"`
MaxChannelDescriptionLength int `json:"max_channel_description_length"`
MaxSenderNameLength int `json:"max_sender_name_length"`
MaxUserMessageIDLength int `json:"max_user_message_id_length"`
} }
type UserJSONWithClientsAndKeys struct { type UserJSONWithClientsAndKeys struct {

View File

@ -428,3 +428,70 @@ func TestUserMessageCounter(t *testing.T) {
assertCounter(5) assertCounter(5)
} }
func TestGetUserNoPro(t *testing.T) {
_, baseUrl, stop := tt.StartSimpleWebserver(t)
defer stop()
r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/v2/users", gin.H{
"no_client": true,
})
uid := fmt.Sprintf("%v", r0["user_id"])
readtok := r0["read_key"].(string)
r1 := tt.RequestAuthGet[gin.H](t, readtok, baseUrl, "/api/v2/users/"+uid)
tt.AssertEqual(t, "user_id", uid, fmt.Sprintf("%v", r1["user_id"]))
tt.AssertEqual(t, "username", nil, r1["username"])
tt.AssertNotEqual(t, "timestamp_created", nil, r1["timestamp_created"])
tt.AssertEqual(t, "timestamp_lastread", nil, r1["timestamp_lastread"])
tt.AssertEqual(t, "timestamp_lastsent", nil, r1["timestamp_lastsent"])
tt.AssertEqual(t, "messages_sent", "0", fmt.Sprintf("%v", r1["messages_sent"]))
tt.AssertEqual(t, "quota_used", "0", fmt.Sprintf("%v", r1["quota_used"]))
tt.AssertEqual(t, "quota_remaining", "50", fmt.Sprintf("%v", r1["quota_remaining"]))
tt.AssertEqual(t, "quota_max", "50", fmt.Sprintf("%v", r1["quota_max"]))
tt.AssertEqual(t, "is_pro", "false", fmt.Sprintf("%v", r1["is_pro"]))
tt.AssertEqual(t, "default_channel", "main", fmt.Sprintf("%v", r1["default_channel"]))
tt.AssertEqual(t, "max_body_size", "2048", fmt.Sprintf("%v", r1["max_body_size"]))
tt.AssertEqual(t, "max_title_length", "120", fmt.Sprintf("%v", r1["max_title_length"]))
tt.AssertEqual(t, "default_priority", "1", fmt.Sprintf("%v", r1["default_priority"]))
tt.AssertEqual(t, "max_channel_name_length", "120", fmt.Sprintf("%v", r1["max_channel_name_length"]))
tt.AssertEqual(t, "max_channel_description_length", "300", fmt.Sprintf("%v", r1["max_channel_description_length"]))
tt.AssertEqual(t, "max_sender_name_length", "120", fmt.Sprintf("%v", r1["max_sender_name_length"]))
tt.AssertEqual(t, "max_user_message_id_length", "64", fmt.Sprintf("%v", r1["max_user_message_id_length"]))
}
func TestGetUserPro(t *testing.T) {
_, baseUrl, stop := tt.StartSimpleWebserver(t)
defer stop()
r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/v2/users", gin.H{
"no_client": true,
"pro_token": "ANDROID|v2|PURCHASED:DUMMY_TOK_XX",
})
uid := fmt.Sprintf("%v", r0["user_id"])
readtok := r0["read_key"].(string)
r1 := tt.RequestAuthGet[gin.H](t, readtok, baseUrl, "/api/v2/users/"+uid)
tt.AssertEqual(t, "user_id", uid, fmt.Sprintf("%v", r1["user_id"]))
tt.AssertEqual(t, "username", nil, r1["username"])
tt.AssertNotEqual(t, "timestamp_created", nil, r1["timestamp_created"])
tt.AssertEqual(t, "timestamp_lastread", nil, r1["timestamp_lastread"])
tt.AssertEqual(t, "timestamp_lastsent", nil, r1["timestamp_lastsent"])
tt.AssertEqual(t, "messages_sent", "0", fmt.Sprintf("%v", r1["messages_sent"]))
tt.AssertEqual(t, "quota_used", "0", fmt.Sprintf("%v", r1["quota_used"]))
tt.AssertEqual(t, "quota_remaining", "5000", fmt.Sprintf("%v", r1["quota_remaining"]))
tt.AssertEqual(t, "quota_max", "5000", fmt.Sprintf("%v", r1["quota_max"]))
tt.AssertEqual(t, "is_pro", "true", fmt.Sprintf("%v", r1["is_pro"]))
tt.AssertEqual(t, "default_channel", "main", fmt.Sprintf("%v", r1["default_channel"]))
tt.AssertEqual(t, "max_body_size", "2097152", fmt.Sprintf("%d", (int64)(r1["max_body_size"].(float64))))
tt.AssertEqual(t, "max_title_length", "120", fmt.Sprintf("%v", r1["max_title_length"]))
tt.AssertEqual(t, "default_priority", "1", fmt.Sprintf("%v", r1["default_priority"]))
tt.AssertEqual(t, "max_channel_name_length", "120", fmt.Sprintf("%v", r1["max_channel_name_length"]))
tt.AssertEqual(t, "max_channel_description_length", "300", fmt.Sprintf("%v", r1["max_channel_description_length"]))
tt.AssertEqual(t, "max_sender_name_length", "120", fmt.Sprintf("%v", r1["max_sender_name_length"]))
tt.AssertEqual(t, "max_user_message_id_length", "64", fmt.Sprintf("%v", r1["max_user_message_id_length"]))
}