Implement message filter scubscription_status and sender_user_id [skip-tests]
All checks were successful
Build Docker and Deploy / Run Unit-Tests (push) Has been skipped
Build Docker and Deploy / Build Docker Container (push) Successful in 45s
Build Docker and Deploy / Deploy to Server (push) Successful in 6s

This commit is contained in:
2025-04-13 19:42:47 +02:00
parent b989a8359e
commit e9c5c5fb99
3 changed files with 171 additions and 17 deletions

View File

@@ -24,6 +24,7 @@ import (
// @Description Simply start the pagination without a next_page_token and get the next page by calling this endpoint with the returned next_page_token of the last query
// @Description If there are no more entries the token "@end" will be returned
// @Description By default we return long messages with a trimmed body, if trimmed=false is supplied we return full messages (this reduces the max page_size)
// @Description By default returns only messages with an [active+confirmed] subscription, can supply subscription_status=all to als include inactive subscriptions or owned messages without subscriptions
// @ID api-messages-list
// @Tags API-v2
//
@@ -37,19 +38,21 @@ import (
// @Router /api/v2/messages [GET]
func (h APIHandler) ListMessages(pctx ginext.PreContext) ginext.HTTPResponse {
type query struct {
PageSize *int `json:"page_size" form:"page_size"`
NextPageToken *string `json:"next_page_token" form:"next_page_token"`
Search []string `json:"search" form:"search"`
StringSearch []string `json:"string_search" form:"string_search"`
Trimmed *bool `json:"trimmed" form:"trimmed"`
Channels []string `json:"channel" form:"channel"`
ChannelIDs []string `json:"channel_id" form:"channel_id"`
Senders []string `json:"sender" form:"sender"`
TimeBefore *string `json:"before" form:"before"` // RFC3339
TimeAfter *string `json:"after" form:"after"` // RFC3339
Priority []int `json:"priority" form:"priority"`
KeyTokens []string `json:"used_key" form:"used_key"`
HasSender *bool `json:"has_sender" form:"has_sender"`
PageSize *int `json:"page_size" form:"page_size"`
NextPageToken *string `json:"next_page_token" form:"next_page_token"`
Search []string `json:"search" form:"search"`
StringSearch []string `json:"string_search" form:"string_search"`
Trimmed *bool `json:"trimmed" form:"trimmed"`
Channels []string `json:"channel" form:"channel"`
ChannelIDs []string `json:"channel_id" form:"channel_id"`
Senders []string `json:"sender" form:"sender"`
TimeBefore *string `json:"before" form:"before"` // RFC3339
TimeAfter *string `json:"after" form:"after"` // RFC3339
Priority []int `json:"priority" form:"priority"`
KeyTokens []string `json:"used_key" form:"used_key"`
HasSender *bool `json:"has_sender" form:"has_sender"`
SenderUserID []string `json:"sender_user_id" form:"sender_user_id"`
SubscriptionStatus *string `json:"subscription_status" form:"subscription_status" enums:"subscribed,all"`
}
type response struct {
Messages []models.Message `json:"messages"`
@@ -89,8 +92,30 @@ func (h APIHandler) ListMessages(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update last-read", err)
}
filter := models.MessageFilter{
ConfirmedAndActiveSubscriptionBy: langext.Ptr(userid),
filter := models.MessageFilter{}
if q.SubscriptionStatus != nil {
if *q.SubscriptionStatus == "subscribed" {
filter.ConfirmedAndActiveSubscriptionBy = langext.Ptr(userid)
} else if *q.SubscriptionStatus == "all" {
filter.ConfirmedSubscriptionOrOwnedBy = langext.Ptr(userid)
} else {
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid value for param 'subscription_status'", nil)
}
} else {
filter.ConfirmedAndActiveSubscriptionBy = langext.Ptr(userid) // default
}
if len(q.SenderUserID) != 0 {
uids := make([]models.UserID, 0, len(q.SenderUserID))
for _, v := range q.SenderUserID {
uid := models.UserID(v)
if err = uid.Valid(); err != nil {
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid sender-user-id", err)
}
uids = append(uids, uid)
}
filter.Sender = &uids
}
if len(q.Search) != 0 {