Skip to content

Commit

Permalink
Merge pull request #427 from CSUSTers/dev
Browse files Browse the repository at this point in the history
hotfix
  • Loading branch information
Anthony-Hoo authored Jul 24, 2024
2 parents 667f2bb + 186b11b commit 65a912f
Show file tree
Hide file tree
Showing 15 changed files with 271 additions and 41 deletions.
1 change: 1 addition & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
latest-${{ github.ref_name }}
containerfiles: |
./Dockerfile
layers: true

- name: Push To harbor
id: push-to-harbor
Expand Down
7 changes: 3 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@ on:
- published

jobs:

lint:
name: Lint
uses: CSUSTers/csust-got/.github/workflows/lint.yml@master


test:
name: Test
uses: CSUSTers/csust-got/.github/workflows/test.yml@master


build:
name: Build and Push
runs-on: ubuntu-latest
Expand All @@ -29,14 +26,16 @@ jobs:
uses: redhat-actions/buildah-build@v2
with:
image: csust/csust-got-hoo
tags: |
tags: |
latest
latest-${{ github.ref_name }}
containerfiles: |
./Dockerfile
build-args: |
TAG=${{ github.ref_name }}
BRANCH=release
layers: true

- name: Push To harbor
id: push-to-harbor
uses: CSUSTers/push-to-registry@main
Expand Down
1 change: 1 addition & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ linters-settings:
- github.com/prometheus/client_golang
- github.com/gabriel-vasile/mimetype
- github.com/u2takey/ffmpeg-go
- github.com/samber/lo
# Packages that are not allowed where the value is a suggestion.
deny:
# - pkg: "github.com/sirupsen/logrus"
Expand Down
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ RUN make deploy
# deploy image
FROM --platform=$BUILDPLATFORM alpine

RUN apk add --no-cache tzdata ffmpeg
RUN apk add --no-cache tzdata
COPY --from=hugefiver/ffmpeg:7.0.1 /ffmpeg /usr/local/bin/ffmpeg
# COPY --from=hugefiver/ffmpeg:7.0.1 /ffprobe /usr/local/bin/ffprobe

WORKDIR /app
COPY --from=buildenv /go/src/app/got .
Expand Down
180 changes: 168 additions & 12 deletions base/sticker.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// nolint: goconst
package base

import (
Expand All @@ -22,13 +23,16 @@ import (

"csust-got/entities"
"csust-got/log"
"csust-got/orm"
"csust-got/util"
"csust-got/util/ffconv"
)

type stickerOpts struct {
format string
pack bool
vf string
sf string
}

func defaultOpts() stickerOpts {
Expand All @@ -38,6 +42,69 @@ func defaultOpts() stickerOpts {
}
}

func defaultOptsWithConfig(m map[string]string) stickerOpts {
o := defaultOpts()
o = o.merge(m)
return o
}

func (o stickerOpts) merge(m map[string]string) stickerOpts {
sets := make(map[string]struct{}, len(m))
for k := range m {
ok, kk, _ := normalizeParams(k, "")
if ok {
sets[kk] = struct{}{}
}
}

if _, ok := sets["format"]; ok {
if _, ok = sets["videoformat"]; !ok {
o.vf = ""
}

if _, ok = sets["stickerformat"]; !ok {
o.sf = ""
}
}

for k, v := range m {
k = strings.ToLower(k)
switch k {
case "format", "f":
o.format = v
// clear videoformat and stickerformat
o.vf = v
o.sf = v
case "pack", "p":
if slices.Contains([]string{"", "true", "1"}, strings.ToLower(v)) {
o.pack = true
} else if strings.ToLower(v) == "false" {
o.pack = false
}
case "vf", "videoformat":
o.vf = v
case "sf", "stickerformat":
o.sf = v
}
}

return o
}

func (o stickerOpts) VideoFormat() string {
if o.vf != "" {
return o.vf
}
return o.format
}

func (o stickerOpts) StickerFormat() string {
if o.sf != "" {
return o.sf
}
return o.format
}

// GetSticker will download sticker file, and convert to expected format, and send to chat
func GetSticker(ctx tb.Context) error {
var msg = ctx.Message()
Expand All @@ -57,7 +124,13 @@ func GetSticker(ctx tb.Context) error {
return nil
}

userID := msg.Sender.ID
opt := defaultOpts()
userConfig, err := orm.GetIWantConfig(userID)
if err == nil {
opt = defaultOptsWithConfig(userConfig)
}

if msg.Text != "" {
o, err := parseOpts(msg.Text)
if err != nil {
Expand All @@ -68,7 +141,7 @@ func GetSticker(ctx tb.Context) error {
}
return err
}
opt = o
opt = opt.merge(o)
}

// nolint: nestif // will fix in future
Expand All @@ -95,7 +168,7 @@ func GetSticker(ctx tb.Context) error {

// send video is sticker is video
if sticker.Video {
switch opt.format {
switch opt.VideoFormat() {
case "", "webm":
sendFile := &tb.Document{
File: tb.FromReader(reader),
Expand Down Expand Up @@ -174,7 +247,7 @@ func GetSticker(ctx tb.Context) error {
}

// send origin file with `format=[webp]`
switch opt.format {
switch opt.StickerFormat() {
case "", "webp":
sendFile := &tb.Document{
File: tb.FromReader(reader),
Expand All @@ -194,7 +267,7 @@ func GetSticker(ctx tb.Context) error {
}

bs := bytes.NewBuffer(nil)
switch opt.format {
switch opt.StickerFormat() {
case "jpg", "jpeg":
filename += ".jpg"
err := jpeg.Encode(bs, img, &jpeg.Options{Quality: 100})
Expand Down Expand Up @@ -235,29 +308,112 @@ func GetSticker(ctx tb.Context) error {

}

func parseOpts(text string) (stickerOpts, error) {
opts := defaultOpts()

func parseOpts(text string) (map[string]string, error) {
cmd, _, err := entities.CommandFromText(text, -1)
if err != nil {
return opts, err
return nil, err
}

ret := make(map[string]string, 4)
for _, arg := range cmd.Args() {
k, v := util.ParseKeyValueMapStr(arg)

ok, k, v := normalizeParams(k, v)
if !ok {
continue
}

switch strings.ToLower(k) {
case "format", "f":
f := strings.ToLower(v)
if slices.Contains([]string{"", "webp", "jpg", "jpeg", "png", "mp4", "gif", "webm"}, f) {
opts.format = f
ret[k] = v
}
case "pack", "p":
if slices.Contains([]string{"", "true", "1"}, strings.ToLower(v)) {
opts.pack = true
ret[k] = "true"
} else if strings.ToLower(v) == "false" {
opts.pack = false
ret[k] = "false"
}
case "vf", "videoformat":
f := strings.ToLower(v)
if slices.Contains([]string{"", "mp4", "gif", "webm"}, f) {
ret[k] = v
}
case "sf", "stickerformat":
f := strings.ToLower(v)
if slices.Contains([]string{"", "webp", "jpg", "jpeg", "png", "gif"}, f) {
ret[k] = v
}
}
}
return opts, nil
return ret, nil
}

func normalizeParams(k, v string) (bool, string, string) {
k = strings.ToLower(k)
switch k {
case "format", "f":
k = "format"
case "pack", "p":
k = "pack"
case "vf", "videoformat":
k = "videoformat"
case "sf", "stickerformat":
k = "stickerformat"
default:
return false, k, v
}

return true, k, v
}

// SetStickerConfig is command for set sticker config
func SetStickerConfig(ctx tb.Context) error {
cmd, _, err := entities.CommandFromText(ctx.Message().Text, -1)
if err != nil {
_ = ctx.Reply("failed to parse params")
return err
}

userID := ctx.Sender().ID
m := make(map[string]string)
clearConf := false
for _, arg := range cmd.Args() {
k, v := util.ParseKeyValueMapStr(arg)
if k == "~clear" {
clearConf = true
clear(m)
}
ok, k, v := normalizeParams(k, v)
if ok {
m[k] = v
}
}

msg := ""
if clearConf {
err = orm.ClearIWantConfig(userID)
if err != nil {
_ = ctx.Reply("failed to clear iwant config")
return err
}
msg = "config cleared, "
}

if len(m) == 0 {
return ctx.Reply(msg + "no params applied")
}

err = orm.SetIWantConfig(userID, m)
if err != nil {
_ = ctx.Reply(msg + "failed to set iwant config")
return err
}

ss := make([]string, 0, len(m))
for k, v := range m {
ss = append(ss, fmt.Sprintf("%s=%s", k, v))
}
return ctx.Reply(msg + "iwant config set: " + strings.Join(ss, " "))
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/prometheus/client_golang v1.19.1
github.com/prometheus/common v0.55.0
github.com/quic-go/quic-go v0.45.1
github.com/redis/go-redis/v9 v9.5.4
github.com/redis/go-redis/v9 v9.6.0
github.com/sashabaranov/go-openai v1.26.3
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.9.0
Expand Down Expand Up @@ -52,6 +52,7 @@ require (
github.com/quic-go/qpack v0.4.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/samber/lo v1.46.0
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,8 @@ github.com/quic-go/qpack v0.4.0 h1:Cr9BXA1sQS2SmDUWjSofMPNKmvF6IiIfDRmgU0w1ZCo=
github.com/quic-go/qpack v0.4.0/go.mod h1:UZVnYIfi5GRk+zI9UMaCPsmZ2xKJP7XBUvVyT1Knj9A=
github.com/quic-go/quic-go v0.45.1 h1:tPfeYCk+uZHjmDRwHHQmvHRYL2t44ROTujLeFVBmjCA=
github.com/quic-go/quic-go v0.45.1/go.mod h1:1dLehS7TIR64+vxGR70GDcatWTOtMX2PUtnKsjbTurI=
github.com/redis/go-redis/v9 v9.5.4 h1:vOFYDKKVgrI5u++QvnMT7DksSMYg7Aw/Np4vLJLKLwY=
github.com/redis/go-redis/v9 v9.5.4/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/redis/go-redis/v9 v9.6.0 h1:NLck+Rab3AOTHw21CGRpvQpgTrAU4sgdCswqGtlhGRA=
github.com/redis/go-redis/v9 v9.6.0/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
Expand All @@ -431,6 +431,8 @@ github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6ke
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
github.com/samber/lo v1.46.0 h1:w8G+oaCPgz1PoCJztqymCFaKwXt+5cCXn51uPxExFfQ=
github.com/samber/lo v1.46.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU=
github.com/sashabaranov/go-openai v1.26.3 h1:Tjnh4rcvsSU68f66r05mys+Zou4vo4qyvkne6AIRJPI=
github.com/sashabaranov/go-openai v1.26.3/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
Expand Down
Loading

0 comments on commit 65a912f

Please sign in to comment.