You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/internal/api/third.go

105 lines
2.8 KiB

2 years ago
package api
import (
"context"
2 years ago
"fmt"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/a2r"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/third"
2 years ago
"github.com/gin-gonic/gin"
2 years ago
"net/http"
2 years ago
"strconv"
2 years ago
)
var _ context.Context // 解决goland编辑器bug
2 years ago
func NewThird(c discoveryregistry.SvcDiscoveryRegistry) *Third {
return &Third{c: c}
2 years ago
}
type Third struct {
2 years ago
c discoveryregistry.SvcDiscoveryRegistry
2 years ago
}
func (o *Third) client() (third.ThirdClient, error) {
2 years ago
conn, err := o.c.GetConn(config.Config.RpcRegisterName.OpenImThirdName)
2 years ago
if err != nil {
return nil, err
}
return third.NewThirdClient(conn), nil
}
func (o *Third) ApplyPut(c *gin.Context) {
a2r.Call(third.ThirdClient.ApplyPut, o.client, c)
}
func (o *Third) GetPut(c *gin.Context) {
a2r.Call(third.ThirdClient.GetPut, o.client, c)
}
func (o *Third) ConfirmPut(c *gin.Context) {
a2r.Call(third.ThirdClient.ConfirmPut, o.client, c)
}
func (o *Third) GetSignalInvitationInfo(c *gin.Context) {
a2r.Call(third.ThirdClient.GetSignalInvitationInfo, o.client, c)
}
func (o *Third) GetSignalInvitationInfoStartApp(c *gin.Context) {
a2r.Call(third.ThirdClient.GetSignalInvitationInfoStartApp, o.client, c)
}
func (o *Third) FcmUpdateToken(c *gin.Context) {
a2r.Call(third.ThirdClient.FcmUpdateToken, o.client, c)
}
func (o *Third) SetAppBadge(c *gin.Context) {
a2r.Call(third.ThirdClient.SetAppBadge, o.client, c)
}
2 years ago
func (o *Third) GetURL(c *gin.Context) {
if c.Request.Method == http.MethodPost {
a2r.Call(third.ThirdClient.GetUrl, o.client, c)
return
}
2 years ago
operationID := c.Query("operationID")
if operationID == "" {
c.String(http.StatusBadRequest, "operationID is empty")
return
}
2 years ago
name := c.Query("name")
if name == "" {
c.String(http.StatusBadRequest, "name is empty")
return
}
2 years ago
expires, err := strconv.ParseInt(c.Query("expires"), 10, 64)
if err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("expires is invalid: %s", err.Error()))
return
}
2 years ago
client, err := o.client()
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
2 years ago
c.Set(constant.OperationID, operationID)
resp, err := client.GetUrl(mcontext.SetOperationID(c, operationID), &third.GetUrlReq{Name: name, Expires: expires})
2 years ago
if err != nil {
if errs.ErrArgs.Is(err) {
c.String(http.StatusBadRequest, err.Error())
return
}
if errs.ErrRecordNotFound.Is(err) {
c.String(http.StatusNotFound, err.Error())
return
}
c.String(http.StatusInternalServerError, err.Error())
return
}
c.Redirect(http.StatusTemporaryRedirect, resp.Url)
}