|
|
@ -8,10 +8,14 @@ import (
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/logic"
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/logic"
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/models"
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/models"
|
|
|
|
"database/sql"
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/mathext"
|
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/mathext"
|
|
|
|
|
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/timeext"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type APIHandler struct {
|
|
|
|
type APIHandler struct {
|
|
|
@ -61,17 +65,17 @@ func (h APIHandler) CreateUser(g *gin.Context) ginresp.HTTPResponse {
|
|
|
|
} else if b.ClientType == string(models.ClientTypeIOS) {
|
|
|
|
} else if b.ClientType == string(models.ClientTypeIOS) {
|
|
|
|
clientType = models.ClientTypeIOS
|
|
|
|
clientType = models.ClientTypeIOS
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
return ginresp.InternAPIError(g, 400, apierr.INVALID_CLIENTTYPE, "Invalid ClientType", nil)
|
|
|
|
return ginresp.APIError(g, 400, apierr.INVALID_CLIENTTYPE, "Invalid ClientType", nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if b.ProToken != nil {
|
|
|
|
if b.ProToken != nil {
|
|
|
|
ptok, err := h.app.VerifyProToken(*b.ProToken)
|
|
|
|
ptok, err := h.app.VerifyProToken(*b.ProToken)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.FAILED_VERIFY_PRO_TOKEN, "Failed to query purchase status", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.FAILED_VERIFY_PRO_TOKEN, "Failed to query purchase status", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if !ptok {
|
|
|
|
if !ptok {
|
|
|
|
return ginresp.InternAPIError(g, 400, apierr.INVALID_PRO_TOKEN, "Purchase token could not be verified", nil)
|
|
|
|
return ginresp.APIError(g, 400, apierr.INVALID_PRO_TOKEN, "Purchase token could not be verified", nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -81,13 +85,13 @@ func (h APIHandler) CreateUser(g *gin.Context) ginresp.HTTPResponse {
|
|
|
|
|
|
|
|
|
|
|
|
err := h.database.ClearFCMTokens(ctx, b.FCMToken)
|
|
|
|
err := h.database.ClearFCMTokens(ctx, b.FCMToken)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to clear existing fcm tokens", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to clear existing fcm tokens", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if b.ProToken != nil {
|
|
|
|
if b.ProToken != nil {
|
|
|
|
err := h.database.ClearProTokens(ctx, *b.ProToken)
|
|
|
|
err := h.database.ClearProTokens(ctx, *b.ProToken)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to clear existing fcm tokens", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to clear existing fcm tokens", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -98,12 +102,12 @@ func (h APIHandler) CreateUser(g *gin.Context) ginresp.HTTPResponse {
|
|
|
|
|
|
|
|
|
|
|
|
userobj, err := h.database.CreateUser(ctx, readKey, sendKey, adminKey, b.ProToken, username)
|
|
|
|
userobj, err := h.database.CreateUser(ctx, readKey, sendKey, adminKey, b.ProToken, username)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to create user in db", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create user in db", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_, err = h.database.CreateClient(ctx, userobj.UserID, clientType, b.FCMToken, b.AgentModel, b.AgentVersion)
|
|
|
|
_, err = h.database.CreateClient(ctx, userobj.UserID, clientType, b.FCMToken, b.AgentModel, b.AgentVersion)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to create user in db", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create user in db", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, userobj.JSON()))
|
|
|
|
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, userobj.JSON()))
|
|
|
@ -141,10 +145,10 @@ func (h APIHandler) GetUser(g *gin.Context) ginresp.HTTPResponse {
|
|
|
|
|
|
|
|
|
|
|
|
user, err := h.database.GetUser(ctx, u.UserID)
|
|
|
|
user, err := h.database.GetUser(ctx, u.UserID)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return ginresp.InternAPIError(g, 404, apierr.USER_NOT_FOUND, "User not found", err)
|
|
|
|
return ginresp.APIError(g, 404, apierr.USER_NOT_FOUND, "User not found", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to query user", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query user", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, user.JSON()))
|
|
|
|
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, user.JSON()))
|
|
|
@ -194,34 +198,34 @@ func (h APIHandler) UpdateUser(g *gin.Context) ginresp.HTTPResponse {
|
|
|
|
|
|
|
|
|
|
|
|
err := h.database.UpdateUserUsername(ctx, u.UserID, b.Username)
|
|
|
|
err := h.database.UpdateUserUsername(ctx, u.UserID, b.Username)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to update user", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update user", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if b.ProToken != nil {
|
|
|
|
if b.ProToken != nil {
|
|
|
|
ptok, err := h.app.VerifyProToken(*b.ProToken)
|
|
|
|
ptok, err := h.app.VerifyProToken(*b.ProToken)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.FAILED_VERIFY_PRO_TOKEN, "Failed to query purchase status", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.FAILED_VERIFY_PRO_TOKEN, "Failed to query purchase status", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if !ptok {
|
|
|
|
if !ptok {
|
|
|
|
return ginresp.InternAPIError(g, 400, apierr.INVALID_PRO_TOKEN, "Purchase token could not be verified", nil)
|
|
|
|
return ginresp.APIError(g, 400, apierr.INVALID_PRO_TOKEN, "Purchase token could not be verified", nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = h.database.ClearProTokens(ctx, *b.ProToken)
|
|
|
|
err = h.database.ClearProTokens(ctx, *b.ProToken)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to clear existing fcm tokens", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to clear existing fcm tokens", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = h.database.UpdateUserProToken(ctx, u.UserID, b.ProToken)
|
|
|
|
err = h.database.UpdateUserProToken(ctx, u.UserID, b.ProToken)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to update user", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update user", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
user, err := h.database.GetUser(ctx, u.UserID)
|
|
|
|
user, err := h.database.GetUser(ctx, u.UserID)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to query (updated) user", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query (updated) user", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, user.JSON()))
|
|
|
|
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, user.JSON()))
|
|
|
@ -262,7 +266,7 @@ func (h APIHandler) ListClients(g *gin.Context) ginresp.HTTPResponse {
|
|
|
|
|
|
|
|
|
|
|
|
clients, err := h.database.ListClients(ctx, u.UserID)
|
|
|
|
clients, err := h.database.ListClients(ctx, u.UserID)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to query clients", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query clients", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
res := langext.ArrMap(clients, func(v models.Client) models.ClientJSON { return v.JSON() })
|
|
|
|
res := langext.ArrMap(clients, func(v models.Client) models.ClientJSON { return v.JSON() })
|
|
|
@ -304,10 +308,10 @@ func (h APIHandler) GetClient(g *gin.Context) ginresp.HTTPResponse {
|
|
|
|
|
|
|
|
|
|
|
|
client, err := h.database.GetClient(ctx, u.UserID, u.ClientID)
|
|
|
|
client, err := h.database.GetClient(ctx, u.UserID, u.ClientID)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return ginresp.InternAPIError(g, 404, apierr.CLIENT_NOT_FOUND, "Client not found", err)
|
|
|
|
return ginresp.APIError(g, 404, apierr.CLIENT_NOT_FOUND, "Client not found", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, client.JSON()))
|
|
|
|
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, client.JSON()))
|
|
|
@ -354,7 +358,7 @@ func (h APIHandler) AddClient(g *gin.Context) ginresp.HTTPResponse {
|
|
|
|
} else if b.ClientType == string(models.ClientTypeIOS) {
|
|
|
|
} else if b.ClientType == string(models.ClientTypeIOS) {
|
|
|
|
clientType = models.ClientTypeIOS
|
|
|
|
clientType = models.ClientTypeIOS
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
return ginresp.InternAPIError(g, 400, apierr.INVALID_CLIENTTYPE, "Invalid ClientType", nil)
|
|
|
|
return ginresp.APIError(g, 400, apierr.INVALID_CLIENTTYPE, "Invalid ClientType", nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
|
|
|
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
|
|
@ -363,7 +367,7 @@ func (h APIHandler) AddClient(g *gin.Context) ginresp.HTTPResponse {
|
|
|
|
|
|
|
|
|
|
|
|
client, err := h.database.CreateClient(ctx, u.UserID, clientType, b.FCMToken, b.AgentModel, b.AgentVersion)
|
|
|
|
client, err := h.database.CreateClient(ctx, u.UserID, clientType, b.FCMToken, b.AgentModel, b.AgentVersion)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to create user in db", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create user in db", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, client.JSON()))
|
|
|
|
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, client.JSON()))
|
|
|
@ -403,15 +407,15 @@ func (h APIHandler) DeleteClient(g *gin.Context) ginresp.HTTPResponse {
|
|
|
|
|
|
|
|
|
|
|
|
client, err := h.database.GetClient(ctx, u.UserID, u.ClientID)
|
|
|
|
client, err := h.database.GetClient(ctx, u.UserID, u.ClientID)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return ginresp.InternAPIError(g, 404, apierr.CLIENT_NOT_FOUND, "Client not found", err)
|
|
|
|
return ginresp.APIError(g, 404, apierr.CLIENT_NOT_FOUND, "Client not found", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = h.database.DeleteClient(ctx, u.ClientID)
|
|
|
|
err = h.database.DeleteClient(ctx, u.ClientID)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete client", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete client", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, client.JSON()))
|
|
|
|
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, client.JSON()))
|
|
|
@ -452,7 +456,7 @@ func (h APIHandler) ListChannels(g *gin.Context) ginresp.HTTPResponse {
|
|
|
|
|
|
|
|
|
|
|
|
clients, err := h.database.ListChannels(ctx, u.UserID)
|
|
|
|
clients, err := h.database.ListChannels(ctx, u.UserID)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channels", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channels", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
res := langext.ArrMap(clients, func(v models.Channel) models.ChannelJSON { return v.JSON() })
|
|
|
|
res := langext.ArrMap(clients, func(v models.Channel) models.ChannelJSON { return v.JSON() })
|
|
|
@ -494,10 +498,10 @@ func (h APIHandler) GetChannel(g *gin.Context) ginresp.HTTPResponse {
|
|
|
|
|
|
|
|
|
|
|
|
channel, err := h.database.GetChannel(ctx, u.UserID, u.ChannelID)
|
|
|
|
channel, err := h.database.GetChannel(ctx, u.UserID, u.ChannelID)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return ginresp.InternAPIError(g, 404, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
|
|
|
|
return ginresp.APIError(g, 404, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, channel.JSON()))
|
|
|
|
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, channel.JSON()))
|
|
|
@ -558,33 +562,33 @@ func (h APIHandler) ListChannelMessages(g *gin.Context) ginresp.HTTPResponse {
|
|
|
|
|
|
|
|
|
|
|
|
channel, err := h.database.GetChannel(ctx, u.ChannelUserID, u.ChannelID)
|
|
|
|
channel, err := h.database.GetChannel(ctx, u.ChannelUserID, u.ChannelID)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return ginresp.InternAPIError(g, 404, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
|
|
|
|
return ginresp.APIError(g, 404, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
userid := *ctx.GetPermissionUserID()
|
|
|
|
userid := *ctx.GetPermissionUserID()
|
|
|
|
|
|
|
|
|
|
|
|
sub, err := h.database.GetSubscriptionBySubscriber(ctx, userid, channel.ChannelID)
|
|
|
|
sub, err := h.database.GetSubscriptionBySubscriber(ctx, userid, channel.ChannelID)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return ginresp.InternAPIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
|
|
|
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !sub.Confirmed {
|
|
|
|
if !sub.Confirmed {
|
|
|
|
return ginresp.InternAPIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
|
|
|
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
tok, err := cursortoken.Decode(langext.Coalesce(q.NextPageToken, ""))
|
|
|
|
tok, err := cursortoken.Decode(langext.Coalesce(q.NextPageToken, ""))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.PAGETOKEN_ERROR, "Failed to decode next_page_token", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.PAGETOKEN_ERROR, "Failed to decode next_page_token", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
messages, npt, err := h.database.ListChannelMessages(ctx, channel.ChannelID, pageSize, tok)
|
|
|
|
messages, npt, err := h.database.ListChannelMessages(ctx, channel.ChannelID, pageSize, tok)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to query messages", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query messages", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var res []models.MessageJSON
|
|
|
|
var res []models.MessageJSON
|
|
|
@ -632,7 +636,7 @@ func (h APIHandler) ListUserSubscriptions(g *gin.Context) ginresp.HTTPResponse {
|
|
|
|
|
|
|
|
|
|
|
|
clients, err := h.database.ListSubscriptionsByOwner(ctx, u.UserID)
|
|
|
|
clients, err := h.database.ListSubscriptionsByOwner(ctx, u.UserID)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channels", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channels", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
res := langext.ArrMap(clients, func(v models.Subscription) models.SubscriptionJSON { return v.JSON() })
|
|
|
|
res := langext.ArrMap(clients, func(v models.Subscription) models.SubscriptionJSON { return v.JSON() })
|
|
|
@ -677,15 +681,15 @@ func (h APIHandler) ListChannelSubscriptions(g *gin.Context) ginresp.HTTPRespons
|
|
|
|
|
|
|
|
|
|
|
|
_, err := h.database.GetChannel(ctx, u.UserID, u.ChannelID)
|
|
|
|
_, err := h.database.GetChannel(ctx, u.UserID, u.ChannelID)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return ginresp.InternAPIError(g, 404, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
|
|
|
|
return ginresp.APIError(g, 404, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channels", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channels", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
clients, err := h.database.ListSubscriptionsByChannel(ctx, u.ChannelID)
|
|
|
|
clients, err := h.database.ListSubscriptionsByChannel(ctx, u.ChannelID)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channels", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channels", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
res := langext.ArrMap(clients, func(v models.Subscription) models.SubscriptionJSON { return v.JSON() })
|
|
|
|
res := langext.ArrMap(clients, func(v models.Subscription) models.SubscriptionJSON { return v.JSON() })
|
|
|
@ -727,14 +731,14 @@ func (h APIHandler) GetSubscription(g *gin.Context) ginresp.HTTPResponse {
|
|
|
|
|
|
|
|
|
|
|
|
subscription, err := h.database.GetSubscription(ctx, u.SubscriptionID)
|
|
|
|
subscription, err := h.database.GetSubscription(ctx, u.SubscriptionID)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return ginresp.InternAPIError(g, 404, apierr.SUBSCRIPTION_NOT_FOUND, "Subscription not found", err)
|
|
|
|
return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_NOT_FOUND, "Subscription not found", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if subscription.SubscriberUserID != u.UserID {
|
|
|
|
if subscription.SubscriberUserID != u.UserID {
|
|
|
|
return ginresp.InternAPIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
|
|
|
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, subscription.JSON()))
|
|
|
|
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, subscription.JSON()))
|
|
|
@ -774,19 +778,19 @@ func (h APIHandler) CancelSubscription(g *gin.Context) ginresp.HTTPResponse {
|
|
|
|
|
|
|
|
|
|
|
|
subscription, err := h.database.GetSubscription(ctx, u.SubscriptionID)
|
|
|
|
subscription, err := h.database.GetSubscription(ctx, u.SubscriptionID)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return ginresp.InternAPIError(g, 404, apierr.SUBSCRIPTION_NOT_FOUND, "Subscription not found", err)
|
|
|
|
return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_NOT_FOUND, "Subscription not found", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if subscription.SubscriberUserID != u.UserID && subscription.ChannelOwnerUserID != u.UserID {
|
|
|
|
if subscription.SubscriberUserID != u.UserID && subscription.ChannelOwnerUserID != u.UserID {
|
|
|
|
return ginresp.InternAPIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
|
|
|
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = h.database.DeleteSubscription(ctx, u.SubscriptionID)
|
|
|
|
err = h.database.DeleteSubscription(ctx, u.SubscriptionID)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete subscription", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete subscription", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, subscription.JSON()))
|
|
|
|
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, subscription.JSON()))
|
|
|
@ -835,19 +839,19 @@ func (h APIHandler) CreateSubscription(g *gin.Context) ginresp.HTTPResponse {
|
|
|
|
|
|
|
|
|
|
|
|
channel, err := h.database.GetChannelByName(ctx, b.ChannelOwnerUserID, h.app.NormalizeChannelName(b.Channel))
|
|
|
|
channel, err := h.database.GetChannelByName(ctx, b.ChannelOwnerUserID, h.app.NormalizeChannelName(b.Channel))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if channel == nil {
|
|
|
|
if channel == nil {
|
|
|
|
return ginresp.InternAPIError(g, 400, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
|
|
|
|
return ginresp.APIError(g, 400, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if channel.OwnerUserID != u.UserID && (q.ChanSubscribeKey == nil || *q.ChanSubscribeKey != channel.SubscribeKey) {
|
|
|
|
if channel.OwnerUserID != u.UserID && (q.ChanSubscribeKey == nil || *q.ChanSubscribeKey != channel.SubscribeKey) {
|
|
|
|
ginresp.InternAPIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
|
|
|
ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub, err := h.database.CreateSubscription(ctx, u.UserID, *channel, channel.OwnerUserID == u.UserID)
|
|
|
|
sub, err := h.database.CreateSubscription(ctx, u.UserID, *channel, channel.OwnerUserID == u.UserID)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to create subscription", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create subscription", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, sub.JSON()))
|
|
|
|
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, sub.JSON()))
|
|
|
@ -891,26 +895,26 @@ func (h APIHandler) UpdateSubscription(g *gin.Context) ginresp.HTTPResponse {
|
|
|
|
|
|
|
|
|
|
|
|
subscription, err := h.database.GetSubscription(ctx, u.SubscriptionID)
|
|
|
|
subscription, err := h.database.GetSubscription(ctx, u.SubscriptionID)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return ginresp.InternAPIError(g, 404, apierr.SUBSCRIPTION_NOT_FOUND, "Subscription not found", err)
|
|
|
|
return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_NOT_FOUND, "Subscription not found", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if subscription.ChannelOwnerUserID != u.UserID {
|
|
|
|
if subscription.ChannelOwnerUserID != u.UserID {
|
|
|
|
return ginresp.InternAPIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
|
|
|
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if b.Confirmed != nil {
|
|
|
|
if b.Confirmed != nil {
|
|
|
|
err = h.database.UpdateSubscriptionConfirmed(ctx, u.SubscriptionID, *b.Confirmed)
|
|
|
|
err = h.database.UpdateSubscriptionConfirmed(ctx, u.SubscriptionID, *b.Confirmed)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to update subscription", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update subscription", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
subscription, err = h.database.GetSubscription(ctx, u.SubscriptionID)
|
|
|
|
subscription, err = h.database.GetSubscription(ctx, u.SubscriptionID)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, subscription.JSON()))
|
|
|
|
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, subscription.JSON()))
|
|
|
@ -968,17 +972,17 @@ func (h APIHandler) ListMessages(g *gin.Context) ginresp.HTTPResponse {
|
|
|
|
|
|
|
|
|
|
|
|
tok, err := cursortoken.Decode(langext.Coalesce(q.NextPageToken, ""))
|
|
|
|
tok, err := cursortoken.Decode(langext.Coalesce(q.NextPageToken, ""))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.PAGETOKEN_ERROR, "Failed to decode next_page_token", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.PAGETOKEN_ERROR, "Failed to decode next_page_token", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = h.database.UpdateUserLastRead(ctx, userid)
|
|
|
|
err = h.database.UpdateUserLastRead(ctx, userid)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to update last-read", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update last-read", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
messages, npt, err := h.database.ListMessages(ctx, userid, pageSize, tok)
|
|
|
|
messages, npt, err := h.database.ListMessages(ctx, userid, pageSize, tok)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to query messages", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query messages", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var res []models.MessageJSON
|
|
|
|
var res []models.MessageJSON
|
|
|
@ -1026,10 +1030,10 @@ func (h APIHandler) GetMessage(g *gin.Context) ginresp.HTTPResponse {
|
|
|
|
|
|
|
|
|
|
|
|
msg, err := h.database.GetMessage(ctx, u.MessageID)
|
|
|
|
msg, err := h.database.GetMessage(ctx, u.MessageID)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return ginresp.InternAPIError(g, 404, apierr.MESSAGE_NOT_FOUND, "message not found", err)
|
|
|
|
return ginresp.APIError(g, 404, apierr.MESSAGE_NOT_FOUND, "message not found", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to query message", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query message", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if !ctx.CheckPermissionMessageReadDirect(msg) {
|
|
|
|
if !ctx.CheckPermissionMessageReadDirect(msg) {
|
|
|
@ -1040,21 +1044,21 @@ func (h APIHandler) GetMessage(g *gin.Context) ginresp.HTTPResponse {
|
|
|
|
if uid := ctx.GetPermissionUserID(); uid != nil && ctx.IsPermissionUserRead() {
|
|
|
|
if uid := ctx.GetPermissionUserID(); uid != nil && ctx.IsPermissionUserRead() {
|
|
|
|
sub, err := h.database.GetSubscriptionBySubscriber(ctx, *uid, msg.ChannelID)
|
|
|
|
sub, err := h.database.GetSubscriptionBySubscriber(ctx, *uid, msg.ChannelID)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if sub == nil {
|
|
|
|
if sub == nil {
|
|
|
|
// not subbed
|
|
|
|
// not subbed
|
|
|
|
return ginresp.InternAPIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
|
|
|
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !sub.Confirmed {
|
|
|
|
if !sub.Confirmed {
|
|
|
|
// sub not confirmed
|
|
|
|
// sub not confirmed
|
|
|
|
return ginresp.InternAPIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
|
|
|
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// => perm okay
|
|
|
|
// => perm okay
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// auth-key is not set or not a user:x variant
|
|
|
|
// auth-key is not set or not a user:x variant
|
|
|
|
return ginresp.InternAPIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
|
|
|
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -1095,29 +1099,185 @@ func (h APIHandler) DeleteMessage(g *gin.Context) ginresp.HTTPResponse {
|
|
|
|
|
|
|
|
|
|
|
|
msg, err := h.database.GetMessage(ctx, u.MessageID)
|
|
|
|
msg, err := h.database.GetMessage(ctx, u.MessageID)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return ginresp.InternAPIError(g, 404, apierr.MESSAGE_NOT_FOUND, "message not found", err)
|
|
|
|
return ginresp.APIError(g, 404, apierr.MESSAGE_NOT_FOUND, "message not found", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to query message", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query message", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if !ctx.CheckPermissionMessageReadDirect(msg) {
|
|
|
|
if !ctx.CheckPermissionMessageReadDirect(msg) {
|
|
|
|
return ginresp.InternAPIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
|
|
|
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = h.database.DeleteMessage(ctx, msg.SCNMessageID)
|
|
|
|
err = h.database.DeleteMessage(ctx, msg.SCNMessageID)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete message", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete message", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = h.database.CancelPendingDeliveries(ctx, msg.SCNMessageID)
|
|
|
|
err = h.database.CancelPendingDeliveries(ctx, msg.SCNMessageID)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.InternAPIError(g, 500, apierr.DATABASE_ERROR, "Failed to cancel deliveries", err)
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to cancel deliveries", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, msg.FullJSON()))
|
|
|
|
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, msg.FullJSON()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (h APIHandler) SendMessage(g *gin.Context) ginresp.HTTPResponse {
|
|
|
|
// CreateMessage swaggerdoc
|
|
|
|
return ginresp.NotImplemented(g) //TODO
|
|
|
|
//
|
|
|
|
|
|
|
|
// @Summary Create a new message
|
|
|
|
|
|
|
|
// @Description This is similar to the main route `POST -> https://scn.blackfrestbytes.com/`
|
|
|
|
|
|
|
|
// @Description But this route can change in the future, for long-living scripts etc. it's better to use the normal POST route
|
|
|
|
|
|
|
|
// @ID api-messages-create
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// @Param post_data query handler.CreateMessage.body false " "
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// @Success 200 {object} models.MessageJSON
|
|
|
|
|
|
|
|
// @Failure 400 {object} ginresp.apiError
|
|
|
|
|
|
|
|
// @Failure 401 {object} ginresp.apiError
|
|
|
|
|
|
|
|
// @Failure 404 {object} ginresp.apiError
|
|
|
|
|
|
|
|
// @Failure 500 {object} ginresp.apiError
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// @Router /api-v2/messages [POST]
|
|
|
|
|
|
|
|
func (h APIHandler) CreateMessage(g *gin.Context) ginresp.HTTPResponse {
|
|
|
|
|
|
|
|
type body struct {
|
|
|
|
|
|
|
|
Channel *string `json:"channel"`
|
|
|
|
|
|
|
|
ChanKey *string `json:"chan_key"`
|
|
|
|
|
|
|
|
Title *string `json:"title"`
|
|
|
|
|
|
|
|
Content *string `json:"content"`
|
|
|
|
|
|
|
|
Priority *int `json:"priority"`
|
|
|
|
|
|
|
|
UserMessageID *string `json:"msg_id"`
|
|
|
|
|
|
|
|
SendTimestamp *float64 `json:"timestamp"`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var b body
|
|
|
|
|
|
|
|
ctx, errResp := h.app.StartRequest(g, nil, nil, &b, nil)
|
|
|
|
|
|
|
|
if errResp != nil {
|
|
|
|
|
|
|
|
return *errResp
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
defer ctx.Cancel()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if permResp := ctx.CheckPermissionSend(); permResp != nil {
|
|
|
|
|
|
|
|
return *permResp
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
userID := *ctx.GetPermissionUserID()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if b.Title != nil {
|
|
|
|
|
|
|
|
b.Title = langext.Ptr(strings.TrimSpace(*b.Title))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.UserMessageID != nil {
|
|
|
|
|
|
|
|
b.UserMessageID = langext.Ptr(strings.TrimSpace(*b.UserMessageID))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if b.Title == nil {
|
|
|
|
|
|
|
|
return ginresp.APIError(g, 400, apierr.MISSING_TITLE, "Missing parameter [[title]]", nil)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.SendTimestamp != nil && mathext.Abs(*b.SendTimestamp-float64(time.Now().Unix())) > (24*time.Hour).Seconds() {
|
|
|
|
|
|
|
|
return ginresp.SendAPIError(g, 400, apierr.TIMESTAMP_OUT_OF_RANGE, -1, "The timestamp mus be within 24 hours of now()", nil)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.Priority != nil && (*b.Priority != 0 && *b.Priority != 1 && *b.Priority != 2) {
|
|
|
|
|
|
|
|
return ginresp.SendAPIError(g, 400, apierr.INVALID_PRIO, 105, "Invalid priority", nil)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(*b.Title) == 0 {
|
|
|
|
|
|
|
|
return ginresp.SendAPIError(g, 400, apierr.NO_TITLE, 103, "No title specified", nil)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.UserMessageID != nil && len(*b.UserMessageID) > 64 {
|
|
|
|
|
|
|
|
return ginresp.SendAPIError(g, 400, apierr.USR_MSG_ID_TOO_LONG, -1, "MessageID too long (64 characters)", nil)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
channelName := h.app.DefaultChannel
|
|
|
|
|
|
|
|
if b.Channel != nil {
|
|
|
|
|
|
|
|
channelName = h.app.NormalizeChannelName(*b.Channel)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
user, err := h.database.GetUser(ctx, userID)
|
|
|
|
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
|
|
|
|
return ginresp.SendAPIError(g, 400, apierr.USER_NOT_FOUND, -1, "User not found", nil)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, -1, "Failed to query user", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(*b.Title) > user.MaxTitleLength() {
|
|
|
|
|
|
|
|
return ginresp.SendAPIError(g, 400, apierr.TITLE_TOO_LONG, 103, fmt.Sprintf("Title too long (max %d characters)", user.MaxTitleLength()), nil)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.Content != nil && len(*b.Content) > user.MaxContentLength() {
|
|
|
|
|
|
|
|
return ginresp.SendAPIError(g, 400, apierr.CONTENT_TOO_LONG, 104, fmt.Sprintf("Content too long (%d characters; max := %d characters)", len(*b.Content), user.MaxContentLength()), nil)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if b.UserMessageID != nil {
|
|
|
|
|
|
|
|
msg, err := h.database.GetMessageByUserMessageID(ctx, *b.UserMessageID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, -1, "Failed to query existing message", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg != nil {
|
|
|
|
|
|
|
|
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, msg.FullJSON()))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if user.QuotaRemainingToday() <= 0 {
|
|
|
|
|
|
|
|
return ginresp.SendAPIError(g, 403, apierr.QUOTA_REACHED, -1, fmt.Sprintf("Daily quota reached (%d)", user.QuotaPerDay()), nil)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
channel, err := h.app.GetOrCreateChannel(ctx, userID, channelName)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, -1, "Failed to query/create channel", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var sendTimestamp *time.Time = nil
|
|
|
|
|
|
|
|
if b.SendTimestamp != nil {
|
|
|
|
|
|
|
|
sendTimestamp = langext.Ptr(timeext.UnixFloatSeconds(*b.SendTimestamp))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
priority := langext.Coalesce(b.Priority, 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
msg, err := h.database.CreateMessage(ctx, userID, channel, sendTimestamp, *b.Title, b.Content, priority, b.UserMessageID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, -1, "Failed to create message in db", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
subscriptions, err := h.database.ListSubscriptionsByChannel(ctx, channel.ChannelID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, -1, "Failed to query subscriptions", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = h.database.IncUserMessageCounter(ctx, user)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, -1, "Failed to inc user msg-counter", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = h.database.IncChannelMessageCounter(ctx, channel)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, -1, "Failed to inc channel msg-counter", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for _, sub := range subscriptions {
|
|
|
|
|
|
|
|
clients, err := h.database.ListClients(ctx, sub.SubscriberUserID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, -1, "Failed to query clients", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !sub.Confirmed {
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for _, client := range clients {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fcmDelivID, err := h.app.DeliverMessage(ctx, client, msg)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
_, err = h.database.CreateRetryDelivery(ctx, client, msg)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, -1, "Failed to create delivery", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
_, err = h.database.CreateSuccessDelivery(ctx, client, msg, *fcmDelivID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, -1, "Failed to create delivery", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, msg.FullJSON()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|