Skip to content

Commit

Permalink
fix: fix for collection
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Nov 8, 2023
1 parent dfd8841 commit 7992fc3
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 54 deletions.
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ go 1.21

require (
github.com/BurntSushi/toml v1.3.2
github.com/aliyun/aliyun-oss-go-sdk v2.2.9+incompatible
github.com/aliyun/aliyun-oss-go-sdk v3.0.1+incompatible
github.com/bsm/redislock v0.9.4
github.com/fxamacker/cbor/v2 v2.5.0
github.com/gabriel-vasile/mimetype v1.4.3
github.com/go-playground/validator/v10 v10.15.5
github.com/google/uuid v1.3.1
github.com/go-playground/validator/v10 v10.16.0
github.com/google/uuid v1.4.0
github.com/jaevor/go-nanoid v1.3.0
github.com/klauspost/compress v1.17.2
github.com/ldclabs/cose v1.1.2
github.com/pkoukk/tiktoken-go v0.1.6
github.com/pkoukk/tiktoken-go-loader v0.0.1
github.com/redis/go-redis/v9 v9.2.1
github.com/redis/go-redis/v9 v9.3.0
github.com/rs/xid v1.5.0
github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d
github.com/stretchr/testify v1.8.4
github.com/teambition/gear v1.27.3
go.uber.org/dig v1.17.1
golang.org/x/text v0.13.0
golang.org/x/text v0.14.0
)

require (
Expand All @@ -39,7 +39,7 @@ require (
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/time v0.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
28 changes: 14 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/aliyun/aliyun-oss-go-sdk v2.2.9+incompatible h1:Sg/2xHwDrioHpxTN6WMiwbXTpUEinBpHsN7mG21Rc2k=
github.com/aliyun/aliyun-oss-go-sdk v2.2.9+incompatible/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8=
github.com/aliyun/aliyun-oss-go-sdk v3.0.1+incompatible h1:so4m5rRA32Tc5GgKg/5gKUu0CRsYmVO3ThMP6T3CwLc=
github.com/aliyun/aliyun-oss-go-sdk v3.0.1+incompatible/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
Expand Down Expand Up @@ -32,10 +32,10 @@ github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/o
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.15.5 h1:LEBecTWb/1j5TNY1YYG2RcOUN3R7NLylN+x8TTueE24=
github.com/go-playground/validator/v10 v10.15.5/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/go-playground/validator/v10 v10.16.0 h1:x+plE831WK4vaKHO/jpgUGsvLKIqRRkz6M78GuJAfGE=
github.com/go-playground/validator/v10 v10.16.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jaevor/go-nanoid v1.3.0 h1:nD+iepesZS6pr3uOVf20vR9GdGgJW1HPaR46gtrxzkg=
github.com/jaevor/go-nanoid v1.3.0/go.mod h1:SI+jFaPuddYkqkVQoNGHs81navCtH388TcrH0RqFKgY=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
Expand All @@ -53,8 +53,8 @@ github.com/pkoukk/tiktoken-go-loader v0.0.1 h1:aOB2gRFzZTCCPi3YsOQXJO771P/5876JA
github.com/pkoukk/tiktoken-go-loader v0.0.1/go.mod h1:4mIkYyZooFlnenDlormIo6cd5wrlUKNr97wp9nGgEKo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis/v9 v9.2.1 h1:WlYJg71ODF0dVspZZCpYmoF1+U1Jjk9Rwd7pq6QmlCg=
github.com/redis/go-redis/v9 v9.2.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/redis/go-redis/v9 v9.3.0 h1:RiVDjmig62jIWp7Kk4XVLs0hzV6pI3PyTnnL0cnn0u0=
github.com/redis/go-redis/v9 v9.3.0/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d h1:hrujxIzL1woJ7AwssoOcM/tq5JjjG2yYOc8odClEiXA=
Expand All @@ -80,12 +80,12 @@ golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.4.0 h1:Z81tqI5ddIoXDPvVQ7/7CC9TnLM7ubaFG2qXYd5BbYY=
golang.org/x/time v0.4.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
Expand Down
94 changes: 80 additions & 14 deletions src/api/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,32 +271,30 @@ func (a *Collection) UpdateInfo(ctx *gear.Context) error {
if err != nil {
return gear.ErrInternalServerError.From(err)
}
if err != nil {
return gear.ErrInternalServerError.From(err)
}
if *msg.Version != input.Version {
return gear.ErrBadRequest.WithMsg("version mismatch")
}
if *msg.Language == *input.Language {
return gear.ErrBadRequest.WithMsg("language is the same")
}

var kv bll.KVMessage
if err := cbor.Unmarshal(*msg.Message, &kv); err != nil {
srcMsg, err := bll.FromContent[*bll.ArrayMessage](*msg.Message)
if err != nil {
return gear.ErrInternalServerError.From(err)
}

langKV := make(bll.KVMessage)
dstMsg := srcMsg.New()
if data, ok := msg.I18nMessages[lang]; ok {
if err := cbor.Unmarshal(data, &langKV); err != nil {
if err = dstMsg.UnmarshalCBOR(data); err != nil {
return gear.ErrInternalServerError.From(err)
}
for k := range langKV {
delete(kv, k) // don't need to translate
}
}

if len(kv) == 0 {
if err := srcMsg.DiffWith(dstMsg); err != nil {
return gear.ErrInternalServerError.From(err)
}

if srcMsg.IsEmpty() {
return gear.ErrBadRequest.WithMsg("no need to translate")
}

Expand All @@ -310,7 +308,7 @@ func (a *Collection) UpdateInfo(ctx *gear.Context) error {
return gear.ErrPaymentRequired.WithMsg("insufficient balance")
}

teContents := kv.ToTEContents()
teContents := srcMsg.ToTEContents()
teData, err := cbor.Marshal(teContents)
if err != nil {
return gear.ErrInternalServerError.From(err)
Expand Down Expand Up @@ -374,7 +372,7 @@ func (a *Collection) UpdateInfo(ctx *gear.Context) error {
})

if err == nil {
err = langKV.WithContent(tmOutput.Content)
err = bll.WithContent(dstMsg, tmOutput.Content)
}

if err == nil {
Expand All @@ -398,7 +396,7 @@ func (a *Collection) UpdateInfo(ctx *gear.Context) error {
ID: wallet.Txn,
}

data, err := cbor.Marshal(langKV)
data, err := cbor.Marshal(dstMsg)
if err == nil {
input.Message = util.Ptr(util.Bytes(data))
_, err = a.blls.Writing.UpdateCollectionInfo(gctx, input)
Expand Down Expand Up @@ -515,6 +513,39 @@ func (a *Collection) RemoveChild(ctx *gear.Context) error {
return ctx.OkSend(bll.SuccessResponse[bool]{Result: output})
}

func (a *Collection) Bookmark(ctx *gear.Context) error {
input := &bll.CreateBookmarkInput{}
if err := ctx.ParseBody(input); err != nil {
return err
}

if _, err := a.tryReadOne(ctx, &bll.QueryGidID{
GID: input.GID,
ID: input.CID,
}); err != nil {
return gear.ErrForbidden.From(err)
}

input.Kind = 2
output, err := a.blls.Writing.CreateBookmark(ctx, input)
if err != nil {
return gear.ErrInternalServerError.From(err)
}

sess := gear.CtxValue[middleware.Session](ctx)
if _, err = a.blls.Logbase.Log(ctx, bll.LogActionUserBookmark, 1, sess.UserID, &bll.LogPayload{
GID: input.GID,
CID: input.CID,
Language: &input.Language,
Version: &input.Version,
Kind: util.Ptr(int8(2)),
}); err != nil {
logging.SetTo(ctx, "writeLogError", err.Error())
}

return ctx.OkSend(bll.SuccessResponse[*bll.BookmarkOutput]{Result: output})
}

func (a *Collection) UploadFile(ctx *gear.Context) error {
input := &bll.QueryGidID{}
if err := ctx.ParseURL(input); err != nil {
Expand Down Expand Up @@ -573,3 +604,38 @@ func (a *Collection) checkWritePermission(ctx *gear.Context, gid util.ID) error

return nil
}

func (a *Collection) tryReadOne(ctx *gear.Context, input *bll.QueryGidID) (*bll.CollectionOutput, error) {
sess := gear.CtxValue[middleware.Session](ctx)
if sess == nil || sess.UserID.Compare(util.MinID) <= 0 {
return nil, gear.ErrForbidden.WithMsg("no permission")
}

role, _ := a.checkReadPermission(ctx, input.GID)
status := int8(2)
switch role {
case 2, 1:
status = -1
case 0:
status = 0
case -1:
status = 1
case -2:
input.GID = util.ZeroID
}
input.Fields = "gid,status,updated_at"
output, err := a.blls.Writing.GetCollection(ctx, input, status)
if err != nil {
return nil, gear.ErrInternalServerError.From(err)
}

if output.Status == nil {
return nil, gear.ErrInternalServerError.WithMsg("invalid collection")
}

if role < -1 && *output.Status < 2 {
return nil, gear.ErrForbidden.WithMsg("no permission")
}

return output, nil
}
9 changes: 9 additions & 0 deletions src/api/creation.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func (a *Creation) Create(ctx *gear.Context) error {
CID: output.ID,
Language: output.Language,
Version: output.Version,
Kind: util.Ptr(int8(0)),
}); err != nil {
logging.SetTo(ctx, "writeLogError", err.Error())
}
Expand Down Expand Up @@ -129,6 +130,7 @@ func (a *Creation) Update(ctx *gear.Context) error {
CID: output.ID,
Language: output.Language,
Version: output.Version,
Kind: util.Ptr(int8(0)),
}); err != nil {
logging.SetTo(ctx, "writeLogError", err.Error())
}
Expand Down Expand Up @@ -159,6 +161,7 @@ func (a *Creation) Delete(ctx *gear.Context) error {
if _, err = a.blls.Logbase.Log(ctx, bll.LogActionCreationDelete, 1, input.GID, &bll.LogPayload{
GID: input.GID,
CID: input.ID,
Kind: util.Ptr(int8(0)),
Status: util.Ptr(int8(-2)),
}); err != nil {
logging.SetTo(ctx, "writeLogError", err.Error())
Expand Down Expand Up @@ -237,6 +240,7 @@ func (a *Creation) Archive(ctx *gear.Context) error {
if _, err = a.blls.Logbase.Log(ctx, bll.LogActionCreationUpdate, 1, input.GID, &bll.LogPayload{
GID: input.GID,
CID: input.ID,
Kind: util.Ptr(int8(0)),
Status: util.Ptr(int8(-1)),
}); err != nil {
logging.SetTo(ctx, "writeLogError", err.Error())
Expand Down Expand Up @@ -265,6 +269,7 @@ func (a *Creation) Redraft(ctx *gear.Context) error {
if _, err = a.blls.Logbase.Log(ctx, bll.LogActionCreationUpdate, 1, input.GID, &bll.LogPayload{
GID: input.GID,
CID: input.ID,
Kind: util.Ptr(int8(0)),
Status: util.Ptr(int8(0)),
}); err != nil {
logging.SetTo(ctx, "writeLogError", err.Error())
Expand Down Expand Up @@ -329,6 +334,7 @@ func (a *Creation) Release(ctx *gear.Context) error {
CID: creation.ID,
Language: creation.Language,
Version: creation.Version,
Kind: util.Ptr(int8(0)),
})

if err != nil {
Expand Down Expand Up @@ -378,6 +384,7 @@ func (a *Creation) Release(ctx *gear.Context) error {
CID: creation.ID,
Language: creation.Language,
Version: creation.Version,
Kind: util.Ptr(int8(0)),
})
}

Expand Down Expand Up @@ -479,6 +486,7 @@ func (a *Creation) UpdateContent(ctx *gear.Context) error {
CID: input.ID,
Language: output.Language,
Version: output.Version,
Kind: util.Ptr(int8(0)),
}); err != nil {
logging.SetTo(ctx, "writeLogError", err.Error())
}
Expand Down Expand Up @@ -528,6 +536,7 @@ func (a *Creation) UpdatePrice(ctx *gear.Context) error {
if _, err = a.blls.Logbase.Log(ctx, bll.LogActionCreationUpdate, 1, input.GID, &bll.LogPayload{
GID: input.GID,
CID: input.ID,
Kind: util.Ptr(int8(0)),
Price: &input.Price,
}); err != nil {
logging.SetTo(ctx, "writeLogError", err.Error())
Expand Down
33 changes: 18 additions & 15 deletions src/api/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ func (a *Message) Create(ctx *gear.Context) error {
return err
}

var kv bll.KVMessage
if err := cbor.Unmarshal(input.Message, &kv); err != nil {
if err := bll.ValidMessage(input.Message); err != nil {
return gear.ErrBadRequest.From(err)
}

Expand Down Expand Up @@ -59,8 +58,7 @@ func (a *Message) Update(ctx *gear.Context) error {
}

if input.Message != nil {
var kv bll.KVMessage
if err := cbor.Unmarshal(*input.Message, &kv); err != nil {
if err := bll.ValidMessage(*input.Message); err != nil {
return gear.ErrBadRequest.From(err)
}
}
Expand Down Expand Up @@ -117,22 +115,27 @@ func (a *Message) UpdateI18n(ctx *gear.Context) error {
return gear.ErrForbidden.WithMsg("no permission")
}

var kv bll.KVMessage
if err := cbor.Unmarshal(*msg.Message, &kv); err != nil {
var srcMsg bll.MessageContainer
srcMsg, err = bll.FromContent[*bll.KVMessage](*msg.Message)
if err != nil {
srcMsg, err = bll.FromContent[*bll.ArrayMessage](*msg.Message)
}
if err != nil {
return gear.ErrInternalServerError.From(err)
}

langKV := make(bll.KVMessage)
dstMsg := srcMsg.New()
if data, ok := msg.I18nMessages[lang]; ok {
if err := cbor.Unmarshal(data, &langKV); err != nil {
if err = dstMsg.UnmarshalCBOR(data); err != nil {
return gear.ErrInternalServerError.From(err)
}
for k := range langKV {
delete(kv, k) // don't need to translate
}
}

if len(kv) == 0 {
if err := srcMsg.DiffWith(dstMsg); err != nil {
return gear.ErrInternalServerError.From(err)
}

if srcMsg.IsEmpty() {
return gear.ErrBadRequest.WithMsg("no need to translate")
}

Expand All @@ -146,7 +149,7 @@ func (a *Message) UpdateI18n(ctx *gear.Context) error {
return gear.ErrPaymentRequired.WithMsg("insufficient balance")
}

teContents := kv.ToTEContents()
teContents := srcMsg.ToTEContents()
teData, err := cbor.Marshal(teContents)
if err != nil {
return gear.ErrInternalServerError.From(err)
Expand Down Expand Up @@ -210,7 +213,7 @@ func (a *Message) UpdateI18n(ctx *gear.Context) error {
})

if err == nil {
err = langKV.WithContent(tmOutput.Content)
err = bll.WithContent(dstMsg, tmOutput.Content)
}

if err == nil {
Expand All @@ -234,7 +237,7 @@ func (a *Message) UpdateI18n(ctx *gear.Context) error {
ID: wallet.Txn,
}

data, err := cbor.Marshal(langKV)
data, err := cbor.Marshal(dstMsg)
if err == nil {
input.Message = util.Ptr(util.Bytes(data))
_, err = a.blls.Writing.UpdateMessage(gctx, input)
Expand Down
Loading

0 comments on commit 7992fc3

Please sign in to comment.