From 15f8a3669bc5461d7377a488671af14e386faa56 Mon Sep 17 00:00:00 2001 From: skeris Date: Sat, 20 Apr 2024 12:45:24 +0300 Subject: [PATCH 01/57] enlarge quiz name --- schema/000009_init.down.sql | 2 ++ schema/000009_init.up.sql | 2 ++ service/quiz_svc.go | 10 +++++----- 3 files changed, 9 insertions(+), 5 deletions(-) create mode 100644 schema/000009_init.down.sql create mode 100644 schema/000009_init.up.sql diff --git a/schema/000009_init.down.sql b/schema/000009_init.down.sql new file mode 100644 index 0000000..342ea06 --- /dev/null +++ b/schema/000009_init.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE quiz +ALTER COLUMN name TYPE VARCHAR(280); diff --git a/schema/000009_init.up.sql b/schema/000009_init.up.sql new file mode 100644 index 0000000..0c3038e --- /dev/null +++ b/schema/000009_init.up.sql @@ -0,0 +1,2 @@ +ALTER TABLE quiz +ALTER COLUMN name TYPE VARCHAR(1024); diff --git a/service/quiz_svc.go b/service/quiz_svc.go index 4ea228a..e312279 100644 --- a/service/quiz_svc.go +++ b/service/quiz_svc.go @@ -18,7 +18,7 @@ type CreateQuizReq struct { MailNotifications bool `json:"mail_notifications"` // set true if you want get an email with every quiz passing UniqueAnswers bool `json:"unique_answers"` // set true if we you mention only last quiz passing - Name string `json:"name"` // max 280 chars + Name string `json:"name"` // max 700 chars Description string `json:"description"` Config string `json:"config"` // serialize json with config for page rules. fill it up only if implement one form scenario Status string `json:"status"` // status of quiz as enum. see Status const. fill it up only if implement one form scenario @@ -47,8 +47,8 @@ func (s *Service) CreateQuiz(ctx *fiber.Ctx) error { } // check that we can store name - if utf8.RuneCountInString(req.Name) >= 280 { - return ctx.Status(fiber.StatusUnprocessableEntity).SendString("name field should have less then 280 chars") + if utf8.RuneCountInString(req.Name) >= 700 { + return ctx.Status(fiber.StatusUnprocessableEntity).SendString("name field should have less then 700 chars") } // status should be empty or equal one of status enum strings @@ -201,8 +201,8 @@ func (s *Service) UpdateQuiz(ctx *fiber.Ctx) error { return ctx.Status(fiber.StatusFailedDependency).SendString("need id of question for update") } - if utf8.RuneCountInString(req.Name) >= 280 { - return ctx.Status(fiber.StatusUnprocessableEntity).SendString("name field should have less then 280 chars") + if utf8.RuneCountInString(req.Name) >= 700 { + return ctx.Status(fiber.StatusUnprocessableEntity).SendString("name field should have less then 700 chars") } // status should be empty or equal one of status enum strings From 0c48a627ccdf0570956ae86e0084c665c922f11f Mon Sep 17 00:00:00 2001 From: skeris Date: Tue, 7 May 2024 19:37:25 +0300 Subject: [PATCH 02/57] fix: remove stats copying --- go.mod | 17 +++++++++-------- go.sum | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index 70af9d9..1b0b056 100644 --- a/go.mod +++ b/go.mod @@ -16,9 +16,9 @@ require ( github.com/xuri/excelize/v2 v2.8.1 go.uber.org/zap v1.27.0 google.golang.org/grpc v1.63.2 - google.golang.org/protobuf v1.33.0 + google.golang.org/protobuf v1.34.1 penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240223054633-6cb3d5ce45b6 - penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240423102606-60b55541d67e + penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240507162248-1fd151e8c254 penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 ) @@ -32,6 +32,7 @@ require ( github.com/docker/go-connections v0.5.0 // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/go-redis/redis/v8 v8.11.5 // indirect + github.com/goccy/go-json v0.10.2 // indirect github.com/golang-migrate/migrate/v4 v4.17.1 // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/google/uuid v1.6.0 // indirect @@ -45,7 +46,7 @@ require ( github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-runewidth v0.0.15 // indirect github.com/minio/md5-simd v1.1.2 // indirect - github.com/minio/minio-go/v7 v7.0.69 // indirect + github.com/minio/minio-go/v7 v7.0.70 // indirect github.com/minio/sha256-simd v1.0.1 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect @@ -68,12 +69,12 @@ require ( go.opentelemetry.io/otel/trace v1.24.0 // indirect go.uber.org/atomic v1.11.0 // indirect go.uber.org/multierr v1.11.0 // indirect - golang.org/x/crypto v0.22.0 // indirect - golang.org/x/net v0.24.0 // indirect - golang.org/x/sys v0.19.0 // indirect - golang.org/x/text v0.14.0 // indirect + golang.org/x/crypto v0.23.0 // indirect + golang.org/x/net v0.25.0 // indirect + golang.org/x/sys v0.20.0 // indirect + golang.org/x/text v0.15.0 // indirect golang.org/x/tools v0.19.0 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240506185236-b8a5c65736ae // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index eca5d33..35f0230 100644 --- a/go.sum +++ b/go.sum @@ -47,6 +47,8 @@ github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI= github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/gofiber/fiber/v2 v2.52.4 h1:P+T+4iK7VaqUsq2PALYEfBBo6bJZ4q3FP8cZ84EggTM= github.com/gofiber/fiber/v2 v2.52.4/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= @@ -109,6 +111,8 @@ github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34= github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM= github.com/minio/minio-go/v7 v7.0.69 h1:l8AnsQFyY1xiwa/DaQskY4NXSLA2yrGsW5iD9nRPVS0= github.com/minio/minio-go/v7 v7.0.69/go.mod h1:XAvOPJQ5Xlzk5o3o/ArO2NMbhSGkimC+bpW/ngRKDmQ= +github.com/minio/minio-go/v7 v7.0.70 h1:1u9NtMgfK1U42kUxcsl5v0yj6TEOPR497OAQxpJnn2g= +github.com/minio/minio-go/v7 v7.0.70/go.mod h1:4yBA8v80xGA30cfM3fz0DKYMXunWl/AV/6tWEs9ryzo= github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM= github.com/minio/sha256-simd v1.0.1/go.mod h1:Pz6AKMiUdngCLpeTL/RJY1M9rUuPMYujV5xJjtbRSN8= github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0= @@ -221,6 +225,8 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30= golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M= +golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4= golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE= @@ -243,6 +249,8 @@ golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= +golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= +golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -260,10 +268,14 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 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/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= +golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= @@ -289,6 +301,8 @@ google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98 google.golang.org/genproto v0.0.0-20200423170343-7949de9c1215/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be h1:LG9vZxsWGOmUKieR8wPAUR3u3MpnYFQZROPIMaXh7/A= google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240506185236-b8a5c65736ae h1:c55+MER4zkBS14uJhSZMGGmya0yJx5iHV4x/fpOSNRk= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240506185236-b8a5c65736ae/go.mod h1:I7Y+G38R2bu5j1aLzfFmQfTcU/WnFuqDwLZAbvKTKpM= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= @@ -298,6 +312,8 @@ google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM= google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA= google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg= +google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= @@ -323,5 +339,7 @@ penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240223054633-6cb3 penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240223054633-6cb3d5ce45b6/go.mod h1:lTmpjry+8evVkXWbEC+WMOELcFkRD1lFMc7J09mOndM= penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240423102606-60b55541d67e h1:2xfRZBAMjlhIu5lIDUSsi43in9zVfoLsCNE/sbEXvKw= penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240423102606-60b55541d67e/go.mod h1:/DcyAjBh41IbomuDu5QzhL9flZW6lWO3ZAEbUXKobk0= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240507162248-1fd151e8c254 h1:AYYW9Fm0vVubpUefcvZD+KdDH4d/BjxiOYPfVzavakk= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240507162248-1fd151e8c254/go.mod h1:/DcyAjBh41IbomuDu5QzhL9flZW6lWO3ZAEbUXKobk0= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 h1:jiO8GWO+3sCnDAV8/NAV8tQIUwae/I6/xiDilW7zf0o= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990/go.mod h1:zswBuTwmEsFHBVRu1nkG3/Fwylk5Vcm8OUm9iWxccSE= From b3f6ed082704b1394fde39438b7ced93479024db Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 10 Jun 2024 19:23:52 +0300 Subject: [PATCH 03/57] delete email from account queries, attention -> after hlog merge to staging, need fix git conflicts --- go.mod | 2 +- go.sum | 4 ++-- service/account_svc.go | 4 ++-- service/question_svc.go | 3 ++- service/quiz_svc.go | 3 ++- 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 8537691..28504d5 100644 --- a/go.mod +++ b/go.mod @@ -18,7 +18,7 @@ require ( google.golang.org/grpc v1.64.0 google.golang.org/protobuf v1.34.1 penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240223054633-6cb3d5ce45b6 - penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240603140127-1a0489c42964 + penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240610161253-75e1867c7b36 penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 ) diff --git a/go.sum b/go.sum index 22b7e98..acd8ac6 100644 --- a/go.sum +++ b/go.sum @@ -259,7 +259,7 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240223054633-6cb3d5ce45b6 h1:oV+/HNX+JPoQ3/GUx08hio7d45WpY0AMGrFs7j70QlA= penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240223054633-6cb3d5ce45b6/go.mod h1:lTmpjry+8evVkXWbEC+WMOELcFkRD1lFMc7J09mOndM= -penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240603140127-1a0489c42964 h1:I44x6hYN9nAKcZU56FaJZ8pD3S7+lwuVX/CE9KkGzww= -penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240603140127-1a0489c42964/go.mod h1:G1ZAWaQq6WW1wG9Shy57K4ZIezuhaBckQgsqQ+lhe94= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240610161253-75e1867c7b36 h1:4cVsuo0H7tKRXH2FG/vgPMNyWpvFDbM3KqG6zw8Trzc= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240610161253-75e1867c7b36/go.mod h1:n66zm88Dh12+idyfqh0vU5nd9BZYxM6Pv0XYnmy0398= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 h1:jiO8GWO+3sCnDAV8/NAV8tQIUwae/I6/xiDilW7zf0o= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990/go.mod h1:zswBuTwmEsFHBVRu1nkG3/Fwylk5Vcm8OUm9iWxccSE= diff --git a/service/account_svc.go b/service/account_svc.go index 7c708ba..e8468f6 100644 --- a/service/account_svc.go +++ b/service/account_svc.go @@ -88,7 +88,6 @@ func (s *Service) createAccount(ctx *fiber.Ctx) error { newAccount := model.Account{ UserID: accountID, CreatedAt: time.Now(), - Email: email, Deleted: false, Privileges: map[string]model.ShortPrivilege{ "quizUnlimTime": { @@ -100,7 +99,8 @@ func (s *Service) createAccount(ctx *fiber.Ctx) error { }, } - if err := s.dal.AccountRepo.CreateAccount(ctx.Context(), &newAccount); err != nil { + _, err = s.dal.AccountRepo.CreateAccount(ctx.Context(), &newAccount) + if err != nil { return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) } diff --git a/service/question_svc.go b/service/question_svc.go index d1ba465..dedcf1a 100644 --- a/service/question_svc.go +++ b/service/question_svc.go @@ -54,7 +54,8 @@ func (s *Service) CreateQuestion(ctx *fiber.Ctx) error { Page: req.Page, Content: req.Content, } - if err := s.dal.QuestionRepo.CreateQuestion(ctx.Context(), &result); err != nil { + _, err := s.dal.QuestionRepo.CreateQuestion(ctx.Context(), &result) + if err != nil { if e, ok := err.(*pq.Error); ok { if e.Constraint == "quiz_relation" { return ctx.Status(fiber.StatusFailedDependency).SendString(e.Error()) diff --git a/service/quiz_svc.go b/service/quiz_svc.go index d63bae8..0a478de 100644 --- a/service/quiz_svc.go +++ b/service/quiz_svc.go @@ -90,7 +90,8 @@ func (s *Service) CreateQuiz(ctx *fiber.Ctx) error { GroupId: req.GroupId, } - if err := s.dal.QuizRepo.CreateQuiz(ctx.Context(), &record); err != nil { + _, err := s.dal.QuizRepo.CreateQuiz(ctx.Context(), &record) + if err != nil { return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) } From c2c948cae300c20dbbe53e693a13040a29a9bcbc Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 10 Jun 2024 20:15:18 +0300 Subject: [PATCH 04/57] add all base rest methods for lead target --- go.mod | 2 +- go.sum | 4 +- service/account_svc.go | 91 ++++++++++++++++++++++++++++++++++++++++++ service/service.go | 4 ++ 4 files changed, 98 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 28504d5..122f334 100644 --- a/go.mod +++ b/go.mod @@ -18,7 +18,7 @@ require ( google.golang.org/grpc v1.64.0 google.golang.org/protobuf v1.34.1 penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240223054633-6cb3d5ce45b6 - penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240610161253-75e1867c7b36 + penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240610170852-3d0ee4560f62 penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 ) diff --git a/go.sum b/go.sum index acd8ac6..9231937 100644 --- a/go.sum +++ b/go.sum @@ -259,7 +259,7 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240223054633-6cb3d5ce45b6 h1:oV+/HNX+JPoQ3/GUx08hio7d45WpY0AMGrFs7j70QlA= penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240223054633-6cb3d5ce45b6/go.mod h1:lTmpjry+8evVkXWbEC+WMOELcFkRD1lFMc7J09mOndM= -penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240610161253-75e1867c7b36 h1:4cVsuo0H7tKRXH2FG/vgPMNyWpvFDbM3KqG6zw8Trzc= -penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240610161253-75e1867c7b36/go.mod h1:n66zm88Dh12+idyfqh0vU5nd9BZYxM6Pv0XYnmy0398= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240610170852-3d0ee4560f62 h1:jCrXJmApWkbdM4o7HKrIBnFpZ8fITsusQVSx1Aefvbk= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240610170852-3d0ee4560f62/go.mod h1:n66zm88Dh12+idyfqh0vU5nd9BZYxM6Pv0XYnmy0398= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 h1:jiO8GWO+3sCnDAV8/NAV8tQIUwae/I6/xiDilW7zf0o= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990/go.mod h1:zswBuTwmEsFHBVRu1nkG3/Fwylk5Vcm8OUm9iWxccSE= diff --git a/service/account_svc.go b/service/account_svc.go index e8468f6..742108e 100644 --- a/service/account_svc.go +++ b/service/account_svc.go @@ -8,6 +8,7 @@ import ( "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/pj_errors" "penahub.gitlab.yandexcloud.net/backend/quiz/core.git/brokers" + "strconv" "time" ) @@ -45,6 +46,11 @@ type GetAccountsResp struct { Items []model.Account `json:"items"` } +type UpdateLeadTarget struct { + ID int64 `json:"id"` + Target string `json:"target"` +} + // getCurrentAccount обработчик для получения текущего аккаунта func (s *Service) getCurrentAccount(ctx *fiber.Ctx) error { accountID, ok := middleware.GetAccountId(ctx) @@ -228,3 +234,88 @@ func (s *Service) ManualDone(ctx *fiber.Ctx) error { return ctx.SendStatus(fiber.StatusOK) } + +func (s *Service) PostLeadTarget(ctx *fiber.Ctx) error { + var req struct { + Type string `json:"type"` + QuizID int32 `json:"quizID"` + Target string `json:"target"` + } + if err := ctx.BodyParser(&req); err != nil { + return ctx.Status(fiber.StatusBadRequest).SendString("Invalid request data") + } + + accountID, ok := middleware.GetAccountId(ctx) + if !ok { + return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required") + } + + if req.Type == "" || req.Target == "" { + return ctx.Status(fiber.StatusBadRequest).SendString("Type and Target don't be nil") + } + + result, err := s.dal.AccountRepo.PostLeadTarget(ctx.Context(), model.LeadTarget{ + AccountID: accountID, + Target: req.Target, + Type: req.Type, + QuizID: req.QuizID, + }) + if err != nil { + return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) + } + + return ctx.Status(fiber.StatusOK).JSON(result) +} + +func (s *Service) DeleteLeadTarget(ctx *fiber.Ctx) error { + leadIDStr := ctx.Params("id") + leadID, err := strconv.ParseInt(leadIDStr, 10, 64) + if err != nil { + return ctx.Status(fiber.StatusBadRequest).SendString("Invalid lead ID format") + } + + err = s.dal.AccountRepo.DeleteLeadTarget(ctx.Context(), leadID) + if err != nil { + return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) + } + return ctx.SendStatus(fiber.StatusOK) +} + +func (s *Service) GetLeadTarget(ctx *fiber.Ctx) error { + accountID, ok := middleware.GetAccountId(ctx) + if !ok { + return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required") + } + + result, err := s.dal.AccountRepo.GetLeadTarget(ctx.Context(), accountID) + if err != nil { + return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) + } + + return ctx.Status(fiber.StatusOK).JSON(result) +} + +func (s *Service) UpdateLeadTarget(ctx *fiber.Ctx) error { + var req struct { + ID int64 `json:"id"` + Target string `json:"target"` + } + + if err := ctx.BodyParser(&req); err != nil { + return ctx.Status(fiber.StatusBadRequest).SendString("Invalid request data") + } + + if req.ID == 0 || req.Target == "" { + return ctx.Status(fiber.StatusBadRequest).SendString("ID and Target don't be nil") + } + + result, err := s.dal.AccountRepo.UpdateLeadTarget(ctx.Context(), model.LeadTarget{ + ID: req.ID, + Target: req.Target, + }) + if err != nil { + return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) + } + + return ctx.Status(fiber.StatusOK).JSON(result) +} diff --git a/service/service.go b/service/service.go index f5dcd27..b45276c 100644 --- a/service/service.go +++ b/service/service.go @@ -60,6 +60,10 @@ func (s *Service) Register(app *fiber.App) { app.Get("/privilege/:userId", s.getPrivilegeByUserID) app.Delete("/account/:userId", s.deleteAccountByUserID) app.Post("/account/manualdone", s.ManualDone) + app.Post("/account/leadtarget", s.PostLeadTarget) + app.Delete("/account/leadtarget/:id", s.DeleteLeadTarget) + app.Get("/account/leadtarget/:id", s.GetLeadTarget) + app.Put("/account/leadtarget", s.UpdateLeadTarget) // result handlers app.Post("/results/getResults/:quizId", s.GetResultsByQuizID) From 7b21f7459b09e31f62a441a0e9c1dbbcbccaec35 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 10 Jun 2024 20:16:07 +0300 Subject: [PATCH 05/57] add all base rest methods for lead target --- service/account_svc.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/service/account_svc.go b/service/account_svc.go index 742108e..88db969 100644 --- a/service/account_svc.go +++ b/service/account_svc.go @@ -46,11 +46,6 @@ type GetAccountsResp struct { Items []model.Account `json:"items"` } -type UpdateLeadTarget struct { - ID int64 `json:"id"` - Target string `json:"target"` -} - // getCurrentAccount обработчик для получения текущего аккаунта func (s *Service) getCurrentAccount(ctx *fiber.Ctx) error { accountID, ok := middleware.GetAccountId(ctx) From 95ab5b44548e63ea1a6b1f62765e6f61709507b3 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 10 Jun 2024 20:18:07 +0300 Subject: [PATCH 06/57] add all base rest methods for lead target --- service/service.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/service.go b/service/service.go index b45276c..c006426 100644 --- a/service/service.go +++ b/service/service.go @@ -62,7 +62,7 @@ func (s *Service) Register(app *fiber.App) { app.Post("/account/manualdone", s.ManualDone) app.Post("/account/leadtarget", s.PostLeadTarget) app.Delete("/account/leadtarget/:id", s.DeleteLeadTarget) - app.Get("/account/leadtarget/:id", s.GetLeadTarget) + app.Get("/account/leadtarget", s.GetLeadTarget) app.Put("/account/leadtarget", s.UpdateLeadTarget) // result handlers From 898366c135e791dd6f9c73390bd9bb642fe698c8 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 11 Jun 2024 18:16:18 +0300 Subject: [PATCH 07/57] update get lead target rest method --- go.mod | 2 +- go.sum | 2 ++ service/account_svc.go | 8 +++++++- service/service.go | 2 +- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 122f334..7f7cc63 100644 --- a/go.mod +++ b/go.mod @@ -18,7 +18,7 @@ require ( google.golang.org/grpc v1.64.0 google.golang.org/protobuf v1.34.1 penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240223054633-6cb3d5ce45b6 - penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240610170852-3d0ee4560f62 + penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240611135631-30262442b2d4 penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 ) diff --git a/go.sum b/go.sum index 9231937..9042bb1 100644 --- a/go.sum +++ b/go.sum @@ -261,5 +261,7 @@ penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240223054633-6cb3 penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240223054633-6cb3d5ce45b6/go.mod h1:lTmpjry+8evVkXWbEC+WMOELcFkRD1lFMc7J09mOndM= penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240610170852-3d0ee4560f62 h1:jCrXJmApWkbdM4o7HKrIBnFpZ8fITsusQVSx1Aefvbk= penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240610170852-3d0ee4560f62/go.mod h1:n66zm88Dh12+idyfqh0vU5nd9BZYxM6Pv0XYnmy0398= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240611135631-30262442b2d4 h1:1Z1B4G6DA3+izFCK9pstiaoCLFtr1ZHuzeh+JiIRcoY= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240611135631-30262442b2d4/go.mod h1:n66zm88Dh12+idyfqh0vU5nd9BZYxM6Pv0XYnmy0398= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 h1:jiO8GWO+3sCnDAV8/NAV8tQIUwae/I6/xiDilW7zf0o= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990/go.mod h1:zswBuTwmEsFHBVRu1nkG3/Fwylk5Vcm8OUm9iWxccSE= diff --git a/service/account_svc.go b/service/account_svc.go index 88db969..28612e0 100644 --- a/service/account_svc.go +++ b/service/account_svc.go @@ -282,7 +282,13 @@ func (s *Service) GetLeadTarget(ctx *fiber.Ctx) error { return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required") } - result, err := s.dal.AccountRepo.GetLeadTarget(ctx.Context(), accountID) + quizIDStr := ctx.Params("quizID") + quizID, err := strconv.ParseInt(quizIDStr, 10, 64) + if err != nil { + return ctx.Status(fiber.StatusBadRequest).SendString("Invalid quiz ID format") + } + + result, err := s.dal.AccountRepo.GetLeadTarget(ctx.Context(), accountID, int32(quizID)) if err != nil { return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) } diff --git a/service/service.go b/service/service.go index c006426..8094f86 100644 --- a/service/service.go +++ b/service/service.go @@ -62,7 +62,7 @@ func (s *Service) Register(app *fiber.App) { app.Post("/account/manualdone", s.ManualDone) app.Post("/account/leadtarget", s.PostLeadTarget) app.Delete("/account/leadtarget/:id", s.DeleteLeadTarget) - app.Get("/account/leadtarget", s.GetLeadTarget) + app.Get("/account/leadtarget/:quizID", s.GetLeadTarget) app.Put("/account/leadtarget", s.UpdateLeadTarget) // result handlers From 8196a70912ed029a736f736e24eb084b31c6ab04 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 11 Jun 2024 19:02:23 +0300 Subject: [PATCH 08/57] add new methods to openapi --- openapi.yaml | 222 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 222 insertions(+) diff --git a/openapi.yaml b/openapi.yaml index 4f2c543..fd24f06 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -754,6 +754,26 @@ components: Deleted: type: boolean description: удален? + LeadTarget: + type: object + properties: + ID: + type: integer + format: int64 + AccountID: + type: string + Type: + type: string + QuizID: + type: integer + format: int32 + Target: + type: string + Deleted: + type: boolean + CreatedAt: + type: string + paths: @@ -1519,6 +1539,208 @@ paths: properties: message: type: string + '500': + description: Internal Srv Error + content: + application/json: + schema: + type: object + properties: + message: + type: string + /account/leadtarget: + post: + description: Метод для добавления целевых мест, куда будут посылаться заявки клиенту. + security: + - Bearer: [ ] + requestBody: + content: + 'application/json': + schema: + type: object + required: + - type + - quizID + - target + properties: + type: + type: string + description: Тип цели (mail, telegram, whatsapp). + enum: + - mail + - telegram + - whatsapp + quizID: + type: integer + format: int32 + description: ID квиза, к которому прикреплено это правило (приоритет). Передавать как 0, если правило не прикрепляется к квизу и является общим. + target: + type: string + description: Адресат, куда конкретно слать (для mail - email, для telegram - ID чата, для whatsapp - номер телефона, наверное). + responses: + '200': + description: ОК, парвило добавлено + content: + application/json: + schema: + $ref: '#/components/schemas/LeadTarget' + '400': + description: Bad request, ошибка в теле запроса + content: + application/json: + schema: + type: object + properties: + message: + type: string + '401': + description: Unauthorized, не авторизован + content: + application/json: + schema: + type: object + properties: + message: + type: string + '500': + description: Internal Srv Error + content: + application/json: + schema: + type: object + properties: + message: + type: string + put: + description: Метод для обновления целевого места, куда будут посылаться заявки клиенту. + security: + - Bearer: [ ] + requestBody: + content: + 'application/json': + schema: + type: object + required: + - id + - target + properties: + id: + type: integer + format: int64 + description: id этой самой цели, primary key. + target: + type: string + description: Адресат, куда конкретно слать (для mail - email, для telegram - ID чата, для whatsapp - номер телефона, наверное). + responses: + '200': + description: ОК, парвило обновлено + content: + application/json: + schema: + $ref: '#/components/schemas/LeadTarget' + '400': + description: Bad request, ошибка в теле запроса + content: + application/json: + schema: + type: object + properties: + message: + type: string + '401': + description: Unauthorized, не авторизован + content: + application/json: + schema: + type: object + properties: + message: + type: string + '404': + description: NotFound, такого не существует + content: + application/json: + schema: + type: object + properties: + message: + type: string + '500': + description: Internal Srv Error + content: + application/json: + schema: + type: object + properties: + message: + type: string + /account/leadtarget/{id}: + delete: + description: удаление правила по id, primary key + security: + - Bearer: [ ] + responses: + '200': + description: ОК, парвило удалено + '400': + description: Bad request, ошибка в теле запроса + content: + application/json: + schema: + type: object + properties: + message: + type: string + '500': + description: Internal Srv Error + content: + application/json: + schema: + type: object + properties: + message: + type: string + /account/leadtarget/{quizID}: + get: + description: получение правила по quizID, так же стоит передавать 0 если правило не было привязано к определенному квизу, возвращает массив + security: + - Bearer: [ ] + responses: + '200': + description: ОК, парвила получены + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/LeadTarget' + '400': + description: Bad request, ошибка в теле запроса + content: + application/json: + schema: + type: object + properties: + message: + type: string + '401': + description: Unauthorized, не авторизован + content: + application/json: + schema: + type: object + properties: + message: + type: string + '404': + description: NotFound, такого не существует + content: + application/json: + schema: + type: object + properties: + message: + type: string '500': description: Internal Srv Error content: From dcac16a2e8f7cac947bd4c90f506748a38515fc0 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 11 Jun 2024 19:12:54 +0300 Subject: [PATCH 09/57] added error handling --- go.mod | 2 +- go.sum | 6 ++---- service/account_svc.go | 15 +++++++++++++-- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 7f7cc63..e60b975 100644 --- a/go.mod +++ b/go.mod @@ -18,7 +18,7 @@ require ( google.golang.org/grpc v1.64.0 google.golang.org/protobuf v1.34.1 penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240223054633-6cb3d5ce45b6 - penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240611135631-30262442b2d4 + penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240611160822-0013810bb882 penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 ) diff --git a/go.sum b/go.sum index 9042bb1..7cc32d5 100644 --- a/go.sum +++ b/go.sum @@ -259,9 +259,7 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240223054633-6cb3d5ce45b6 h1:oV+/HNX+JPoQ3/GUx08hio7d45WpY0AMGrFs7j70QlA= penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240223054633-6cb3d5ce45b6/go.mod h1:lTmpjry+8evVkXWbEC+WMOELcFkRD1lFMc7J09mOndM= -penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240610170852-3d0ee4560f62 h1:jCrXJmApWkbdM4o7HKrIBnFpZ8fITsusQVSx1Aefvbk= -penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240610170852-3d0ee4560f62/go.mod h1:n66zm88Dh12+idyfqh0vU5nd9BZYxM6Pv0XYnmy0398= -penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240611135631-30262442b2d4 h1:1Z1B4G6DA3+izFCK9pstiaoCLFtr1ZHuzeh+JiIRcoY= -penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240611135631-30262442b2d4/go.mod h1:n66zm88Dh12+idyfqh0vU5nd9BZYxM6Pv0XYnmy0398= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240611160822-0013810bb882 h1:Y64tHM2aEO1syR45bV4uiFBSauIrvEzA47XEf6v8Pq8= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240611160822-0013810bb882/go.mod h1:n66zm88Dh12+idyfqh0vU5nd9BZYxM6Pv0XYnmy0398= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 h1:jiO8GWO+3sCnDAV8/NAV8tQIUwae/I6/xiDilW7zf0o= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990/go.mod h1:zswBuTwmEsFHBVRu1nkG3/Fwylk5Vcm8OUm9iWxccSE= diff --git a/service/account_svc.go b/service/account_svc.go index 28612e0..cba8266 100644 --- a/service/account_svc.go +++ b/service/account_svc.go @@ -290,7 +290,13 @@ func (s *Service) GetLeadTarget(ctx *fiber.Ctx) error { result, err := s.dal.AccountRepo.GetLeadTarget(ctx.Context(), accountID, int32(quizID)) if err != nil { - return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) + switch { + case errors.Is(err, pj_errors.ErrNotFound): + return ctx.Status(fiber.StatusNotFound).SendString("this lead target not found") + default: + return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) + } + } return ctx.Status(fiber.StatusOK).JSON(result) @@ -315,7 +321,12 @@ func (s *Service) UpdateLeadTarget(ctx *fiber.Ctx) error { Target: req.Target, }) if err != nil { - return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) + switch { + case errors.Is(err, pj_errors.ErrNotFound): + return ctx.Status(fiber.StatusNotFound).SendString("this lead target not found") + default: + return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) + } } return ctx.Status(fiber.StatusOK).JSON(result) From b4d95db30c4e99456bdc0b8a06abbc3a6171ee77 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 12 Jun 2024 11:42:26 +0300 Subject: [PATCH 10/57] add validate lead target type in post it --- go.mod | 2 +- go.sum | 4 ++-- service/account_svc.go | 6 +++++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index e60b975..a80c10c 100644 --- a/go.mod +++ b/go.mod @@ -18,7 +18,7 @@ require ( google.golang.org/grpc v1.64.0 google.golang.org/protobuf v1.34.1 penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240223054633-6cb3d5ce45b6 - penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240611160822-0013810bb882 + penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240612083524-11882ffe22cf penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 ) diff --git a/go.sum b/go.sum index 7cc32d5..5e76e85 100644 --- a/go.sum +++ b/go.sum @@ -259,7 +259,7 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240223054633-6cb3d5ce45b6 h1:oV+/HNX+JPoQ3/GUx08hio7d45WpY0AMGrFs7j70QlA= penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240223054633-6cb3d5ce45b6/go.mod h1:lTmpjry+8evVkXWbEC+WMOELcFkRD1lFMc7J09mOndM= -penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240611160822-0013810bb882 h1:Y64tHM2aEO1syR45bV4uiFBSauIrvEzA47XEf6v8Pq8= -penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240611160822-0013810bb882/go.mod h1:n66zm88Dh12+idyfqh0vU5nd9BZYxM6Pv0XYnmy0398= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240612083524-11882ffe22cf h1:cTmv0YZE1B+ofsWfHYEiNxzToWKMy12rVW3cPOrtp30= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240612083524-11882ffe22cf/go.mod h1:n66zm88Dh12+idyfqh0vU5nd9BZYxM6Pv0XYnmy0398= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 h1:jiO8GWO+3sCnDAV8/NAV8tQIUwae/I6/xiDilW7zf0o= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990/go.mod h1:zswBuTwmEsFHBVRu1nkG3/Fwylk5Vcm8OUm9iWxccSE= diff --git a/service/account_svc.go b/service/account_svc.go index cba8266..04c3355 100644 --- a/service/account_svc.go +++ b/service/account_svc.go @@ -245,6 +245,10 @@ func (s *Service) PostLeadTarget(ctx *fiber.Ctx) error { return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required") } + if _, ok := model.ValidLeadTargetTypes[req.Type]; !ok { + return ctx.Status(fiber.StatusBadRequest).SendString("Invalid type") + } + if req.Type == "" || req.Target == "" { return ctx.Status(fiber.StatusBadRequest).SendString("Type and Target don't be nil") } @@ -252,7 +256,7 @@ func (s *Service) PostLeadTarget(ctx *fiber.Ctx) error { result, err := s.dal.AccountRepo.PostLeadTarget(ctx.Context(), model.LeadTarget{ AccountID: accountID, Target: req.Target, - Type: req.Type, + Type: model.LeadTargetType(req.Type), QuizID: req.QuizID, }) if err != nil { From 60790ff3493c32eea4d3180259f9abb33754ccb4 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 12 Jun 2024 13:30:11 +0300 Subject: [PATCH 11/57] add create base lead target for all account with type mail --- service/account_svc.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/service/account_svc.go b/service/account_svc.go index 04c3355..a0764d1 100644 --- a/service/account_svc.go +++ b/service/account_svc.go @@ -105,6 +105,16 @@ func (s *Service) createAccount(ctx *fiber.Ctx) error { return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) } + _, err = s.dal.AccountRepo.PostLeadTarget(ctx.Context(), model.LeadTarget{ + AccountID: accountID, + Target: email, + Type: model.LeadTargetEmail, + QuizID: 0, + }) + if err != nil { + return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) + } + err = s.producer.ToMailNotify(ctx.Context(), brokers.Message{ AccountID: accountID, Email: email, From c7a1364ac193400848d75f532adca9f2d1e2ff67 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 25 Jun 2024 17:37:26 +0300 Subject: [PATCH 12/57] added tg client need test --- app/app.go | 28 +++++++++++++---------- clients/telegram/tg.go | 50 ++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 ++ go.sum | 4 ++++ 4 files changed, 73 insertions(+), 11 deletions(-) create mode 100644 clients/telegram/tg.go diff --git a/app/app.go b/app/app.go index fa04d5e..d6f3eb4 100644 --- a/app/app.go +++ b/app/app.go @@ -8,7 +8,6 @@ import ( "github.com/skeris/appInit" "github.com/themakers/hlog" "go.uber.org/zap" - "go.uber.org/zap/zapcore" "penahub.gitlab.yandexcloud.net/backend/penahub_common/log_mw" "penahub.gitlab.yandexcloud.net/backend/penahub_common/privilege" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal" @@ -17,12 +16,12 @@ import ( "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model" "penahub.gitlab.yandexcloud.net/backend/quiz/core/brokers" "penahub.gitlab.yandexcloud.net/backend/quiz/core/clients/auth" + "penahub.gitlab.yandexcloud.net/backend/quiz/core/clients/telegram" "penahub.gitlab.yandexcloud.net/backend/quiz/core/initialize" "penahub.gitlab.yandexcloud.net/backend/quiz/core/models" "penahub.gitlab.yandexcloud.net/backend/quiz/core/server" "penahub.gitlab.yandexcloud.net/backend/quiz/core/service" "penahub.gitlab.yandexcloud.net/backend/quiz/core/tools" - "penahub.gitlab.yandexcloud.net/external/trashlog.git/wrappers/zaptrashlog" "time" ) @@ -69,6 +68,8 @@ type Options struct { TrashLogHost string `env:"TRASH_LOG_HOST" default:"localhost:7113"` ModuleLogger string `env:"MODULE_LOGGER" default:"core-local"` ClickHouseCred string `env:"CLICK_HOUSE_CRED" default:"tcp://10.8.0.15:9000/default?sslmode=disable"` + ApiID int32 `env:"TG_API_ID" default:""` + ApiHash string `env:"TG_API_HASH" default:""` } func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.CommonApp, error) { @@ -102,16 +103,16 @@ func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.Co zap.String("SvcBuildTime", ver.BuildTime), ) - clickHouseLogger, err := zaptrashlog.NewCore(ctx, zap.InfoLevel, options.TrashLogHost, ver.Release, ver.Commit, time.Now().Unix()) - if err != nil { - panic(err) - } + //clickHouseLogger, err := zaptrashlog.NewCore(ctx, zap.InfoLevel, options.TrashLogHost, ver.Release, ver.Commit, time.Now().Unix()) + //if err != nil { + // panic(err) + //} + // + //loggerForHlog := zapLogger.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core { + // return zapcore.NewTee(core, clickHouseLogger) + //})) - loggerForHlog := zapLogger.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core { - return zapcore.NewTee(core, clickHouseLogger) - })) - - loggerHlog := hlog.New(loggerForHlog).Module(options.ModuleLogger) + loggerHlog := hlog.New(zapLogger).Module(options.ModuleLogger) loggerHlog.With(models.AllFields{}) loggerHlog.Emit(InfoSvcStarted{}) @@ -152,6 +153,11 @@ func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.Co privilegeController := privilege.NewPrivilege(clientData, fiberClient) go tools.PublishPrivilege(privilegeController, 10, 5*time.Minute) + _, err = telegram.NewTelegramClient(options.ApiID, options.ApiHash) + if err != nil { + panic(fmt.Sprintf("Error init tg_client: %v", err)) + } + // todo подумать над реализацией всего а то пока мне кажется что немного каша получается такой предикт что через некоторое время // сложно будет разобраться что есть где grpcControllers := initialize.InitRpcControllers(pgdal) diff --git a/clients/telegram/tg.go b/clients/telegram/tg.go new file mode 100644 index 0000000..5ebf79d --- /dev/null +++ b/clients/telegram/tg.go @@ -0,0 +1,50 @@ +package telegram + +import ( + "fmt" + "github.com/zelenin/go-tdlib/client" +) + +type TelegramClient struct { + Client *client.Client +} + +func NewTelegramClient(apiID int32, apiHash string) (*TelegramClient, error) { + authorizer := client.ClientAuthorizer() + go client.CliInteractor(authorizer) + + authorizer.TdlibParameters <- &client.SetTdlibParametersRequest{ + UseTestDc: false, + //DatabaseDirectory: filepath.Join(".tdlib", "database"), + //FilesDirectory: filepath.Join(".tdlib", "files"), + UseFileDatabase: false, + UseChatInfoDatabase: false, + UseMessageDatabase: false, + UseSecretChats: true, + ApiId: apiID, + ApiHash: apiHash, + SystemLanguageCode: "en", + DeviceModel: "Server", + SystemVersion: "1.0.0", + ApplicationVersion: "1.0.0", + } + + tdlibClient, err := client.NewClient(authorizer) + if err != nil { + return nil, err + } + + me, err := tdlibClient.GetMe() + if err != nil { + return nil, err + } + fmt.Printf("Me: %s %s [%v]", me.FirstName, me.LastName, me.Usernames) + + return &TelegramClient{ + Client: tdlibClient, + }, nil +} + +func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string, error) { + return "", nil +} diff --git a/go.mod b/go.mod index 3a08dd2..9512923 100644 --- a/go.mod +++ b/go.mod @@ -24,6 +24,7 @@ require ( ) require ( + github.com/Arman92/go-tdlib v1.0.0 // indirect github.com/ClickHouse/clickhouse-go v1.5.4 // indirect github.com/andybalholm/brotli v1.1.0 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect @@ -57,6 +58,7 @@ require ( github.com/valyala/tcplisten v1.0.0 // indirect github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d // indirect github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 // indirect + github.com/zelenin/go-tdlib v0.7.2 // indirect go.etcd.io/bbolt v1.3.10 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/crypto v0.24.0 // indirect diff --git a/go.sum b/go.sum index f6269f1..5c6efda 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/Arman92/go-tdlib v1.0.0 h1:ioj9TlKXjmVfyc6LwyTb4bje1WhrhCUzlJlmYxow3w8= +github.com/Arman92/go-tdlib v1.0.0/go.mod h1:gz/NLfLyXSF3LufhmK4a+xcrlIvAUR67zxOWv0JxvsU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/ClickHouse/clickhouse-go v1.5.4 h1:cKjXeYLNWVJIx2J1K6H2CqyRmfwVJVY1OV1coaaFcI0= github.com/ClickHouse/clickhouse-go v1.5.4/go.mod h1:EaI/sW7Azgz9UATzd5ZdZHRUhHgv5+JMS9NSr2smCJI= @@ -164,6 +166,8 @@ github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 h1:hPVCafDV85blFTabnqKgNh github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/zelenin/go-tdlib v0.7.2 h1:XpD77+t3bix62TsEZbuQeDls6QQJbcovpb8SVVfyNAA= +github.com/zelenin/go-tdlib v0.7.2/go.mod h1:yqNbNZenZtXPKgf9hDuyZbsRz7qlxOxdfKOc+sAxxIE= go.etcd.io/bbolt v1.3.10 h1:+BqfJTcCzTItrop8mq/lbzL8wSGtj94UO/3U31shqG0= go.etcd.io/bbolt v1.3.10/go.mod h1:bK3UQLPJZly7IlNmV7uVHJDxfe5aK9Ll93e/74Y9oEQ= go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= From ba602eb00b4617bac30cf1aff84bb258a1b28ecc Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 25 Jun 2024 17:41:26 +0300 Subject: [PATCH 13/57] added tg client need test --- app/app.go | 20 +++++++++++--------- go.mod | 9 +++++---- go.sum | 16 ++++++---------- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/app/app.go b/app/app.go index d6f3eb4..6012999 100644 --- a/app/app.go +++ b/app/app.go @@ -8,6 +8,7 @@ import ( "github.com/skeris/appInit" "github.com/themakers/hlog" "go.uber.org/zap" + "go.uber.org/zap/zapcore" "penahub.gitlab.yandexcloud.net/backend/penahub_common/log_mw" "penahub.gitlab.yandexcloud.net/backend/penahub_common/privilege" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal" @@ -22,6 +23,7 @@ import ( "penahub.gitlab.yandexcloud.net/backend/quiz/core/server" "penahub.gitlab.yandexcloud.net/backend/quiz/core/service" "penahub.gitlab.yandexcloud.net/backend/quiz/core/tools" + "penahub.gitlab.yandexcloud.net/external/trashlog.git/wrappers/zaptrashlog" "time" ) @@ -103,16 +105,16 @@ func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.Co zap.String("SvcBuildTime", ver.BuildTime), ) - //clickHouseLogger, err := zaptrashlog.NewCore(ctx, zap.InfoLevel, options.TrashLogHost, ver.Release, ver.Commit, time.Now().Unix()) - //if err != nil { - // panic(err) - //} - // - //loggerForHlog := zapLogger.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core { - // return zapcore.NewTee(core, clickHouseLogger) - //})) + clickHouseLogger, err := zaptrashlog.NewCore(ctx, zap.InfoLevel, options.TrashLogHost, ver.Release, ver.Commit, time.Now().Unix()) + if err != nil { + panic(err) + } - loggerHlog := hlog.New(zapLogger).Module(options.ModuleLogger) + loggerForHlog := zapLogger.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core { + return zapcore.NewTee(core, clickHouseLogger) + })) + + loggerHlog := hlog.New(loggerForHlog).Module(options.ModuleLogger) loggerHlog.With(models.AllFields{}) loggerHlog.Emit(InfoSvcStarted{}) diff --git a/go.mod b/go.mod index 9512923..a746b71 100644 --- a/go.mod +++ b/go.mod @@ -14,17 +14,16 @@ require ( github.com/themakers/hlog v0.0.0-20191205140925-235e0e4baddf github.com/twmb/franz-go v1.16.1 github.com/xuri/excelize/v2 v2.8.1 + github.com/zelenin/go-tdlib v0.7.2 go.uber.org/zap v1.27.0 google.golang.org/grpc v1.64.0 google.golang.org/protobuf v1.34.2 penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240624105135-6982631f2a4b penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 - penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615192328-b2f5dffe92ae ) require ( - github.com/Arman92/go-tdlib v1.0.0 // indirect github.com/ClickHouse/clickhouse-go v1.5.4 // indirect github.com/andybalholm/brotli v1.1.0 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect @@ -46,6 +45,7 @@ require ( github.com/minio/minio-go/v7 v7.0.70 // indirect github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect github.com/pierrec/lz4/v4 v4.1.21 // indirect + github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/richardlehane/mscfb v1.0.4 // indirect github.com/richardlehane/msoleps v1.0.3 // indirect @@ -58,8 +58,7 @@ require ( github.com/valyala/tcplisten v1.0.0 // indirect github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d // indirect github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 // indirect - github.com/zelenin/go-tdlib v0.7.2 // indirect - go.etcd.io/bbolt v1.3.10 // indirect + go.etcd.io/bbolt v1.3.6 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/crypto v0.24.0 // indirect golang.org/x/net v0.26.0 // indirect @@ -68,5 +67,7 @@ require ( google.golang.org/genproto/googleapis/rpc v0.0.0-20240610135401-a8a62080eff3 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/ini.v1 v1.67.0 // indirect + gopkg.in/tucnak/telebot.v2 v2.5.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect + penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615192328-b2f5dffe92ae // indirect ) diff --git a/go.sum b/go.sum index 5c6efda..ac2a590 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,4 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -github.com/Arman92/go-tdlib v1.0.0 h1:ioj9TlKXjmVfyc6LwyTb4bje1WhrhCUzlJlmYxow3w8= -github.com/Arman92/go-tdlib v1.0.0/go.mod h1:gz/NLfLyXSF3LufhmK4a+xcrlIvAUR67zxOWv0JxvsU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/ClickHouse/clickhouse-go v1.5.4 h1:cKjXeYLNWVJIx2J1K6H2CqyRmfwVJVY1OV1coaaFcI0= github.com/ClickHouse/clickhouse-go v1.5.4/go.mod h1:EaI/sW7Azgz9UATzd5ZdZHRUhHgv5+JMS9NSr2smCJI= @@ -28,8 +26,6 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= -github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg= -github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= @@ -138,14 +134,13 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/tealeg/xlsx v1.0.5 h1:+f8oFmvY8Gw1iUXzPk+kz+4GpbDZPK1FhPiQRd+ypgE= github.com/tealeg/xlsx v1.0.5/go.mod h1:btRS8dz54TDnvKNosuAqxrM1QgN1udgk9O34bDCnORM= -github.com/themakers/bdd v0.0.0-20210316111417-6b1dfe326f33 h1:N9f/Q+2Ssa+yDcbfaoLTYvXmdeyUUxsJKdPUVsjSmiA= -github.com/themakers/bdd v0.0.0-20210316111417-6b1dfe326f33/go.mod h1:rpcH99JknBh8seZmlOlUg51gasZH6QH34oXNsIwYT6E= github.com/themakers/hlog v0.0.0-20191205140925-235e0e4baddf h1:TJJm6KcBssmbWzplF5lzixXl1RBAi/ViPs1GaSOkhwo= github.com/themakers/hlog v0.0.0-20191205140925-235e0e4baddf/go.mod h1:1FsorU3vnXO9xS9SrhUp8fRb/6H/Zfll0rPt1i4GWaA= github.com/twmb/franz-go v1.16.1 h1:rpWc7fB9jd7TgmCyfxzenBI+QbgS8ZfJOUQE+tzPtbE= @@ -168,8 +163,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/zelenin/go-tdlib v0.7.2 h1:XpD77+t3bix62TsEZbuQeDls6QQJbcovpb8SVVfyNAA= github.com/zelenin/go-tdlib v0.7.2/go.mod h1:yqNbNZenZtXPKgf9hDuyZbsRz7qlxOxdfKOc+sAxxIE= -go.etcd.io/bbolt v1.3.10 h1:+BqfJTcCzTItrop8mq/lbzL8wSGtj94UO/3U31shqG0= -go.etcd.io/bbolt v1.3.10/go.mod h1:bK3UQLPJZly7IlNmV7uVHJDxfe5aK9Ll93e/74Y9oEQ= +go.etcd.io/bbolt v1.3.6 h1:/ecaJf0sk1l4l6V4awd65v2C3ILy7MSj+s/x1ADCIMU= +go.etcd.io/bbolt v1.3.6/go.mod h1:qXsaaIqmgQH0T+OPdb99Bf+PKfBBQVAdyD6TY9G8XM4= go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A= @@ -216,12 +211,11 @@ golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= -golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200923182605-d9f96fdee20d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -294,5 +288,7 @@ penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240624105135-698 penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240624105135-6982631f2a4b/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 h1:jiO8GWO+3sCnDAV8/NAV8tQIUwae/I6/xiDilW7zf0o= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990/go.mod h1:zswBuTwmEsFHBVRu1nkG3/Fwylk5Vcm8OUm9iWxccSE= +penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615191139-35b268acbfed h1:/1hG64JeCaRTj6bQf3hzriuXzzGwOR0NAT72EaeEaVk= +penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615191139-35b268acbfed/go.mod h1:3ml0dAGT8U8RhpevKBfRgG6yKZum8EI2uJxAb2WCIy4= penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615192328-b2f5dffe92ae h1:vlGInCsQSDA464q7OMv3EWGUviQWQdEcpLc8HIRo+rE= penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615192328-b2f5dffe92ae/go.mod h1:3ml0dAGT8U8RhpevKBfRgG6yKZum8EI2uJxAb2WCIy4= From 0978807a9a57857d28bc5c37e45fe36f6fda255c Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 27 Jun 2024 12:24:23 +0300 Subject: [PATCH 14/57] test local tdlib compile passed --- clients/telegram/tg.go | 16 ++++++++++++---- go.mod | 6 ++---- go.sum | 11 ++++++----- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/clients/telegram/tg.go b/clients/telegram/tg.go index 5ebf79d..c7364a3 100644 --- a/clients/telegram/tg.go +++ b/clients/telegram/tg.go @@ -3,6 +3,7 @@ package telegram import ( "fmt" "github.com/zelenin/go-tdlib/client" + "path/filepath" ) type TelegramClient struct { @@ -14,13 +15,13 @@ func NewTelegramClient(apiID int32, apiHash string) (*TelegramClient, error) { go client.CliInteractor(authorizer) authorizer.TdlibParameters <- &client.SetTdlibParametersRequest{ - UseTestDc: false, - //DatabaseDirectory: filepath.Join(".tdlib", "database"), - //FilesDirectory: filepath.Join(".tdlib", "files"), + UseTestDc: false, + DatabaseDirectory: filepath.Join(".tdlib", "database"), + FilesDirectory: filepath.Join(".tdlib", "files"), UseFileDatabase: false, UseChatInfoDatabase: false, UseMessageDatabase: false, - UseSecretChats: true, + UseSecretChats: false, ApiId: apiID, ApiHash: apiHash, SystemLanguageCode: "en", @@ -29,6 +30,13 @@ func NewTelegramClient(apiID int32, apiHash string) (*TelegramClient, error) { ApplicationVersion: "1.0.0", } + _, err := client.SetLogVerbosityLevel(&client.SetLogVerbosityLevelRequest{ + NewVerbosityLevel: 1, + }) + if err != nil { + return nil, err + } + tdlibClient, err := client.NewClient(authorizer) if err != nil { return nil, err diff --git a/go.mod b/go.mod index a746b71..f036a57 100644 --- a/go.mod +++ b/go.mod @@ -14,13 +14,14 @@ require ( github.com/themakers/hlog v0.0.0-20191205140925-235e0e4baddf github.com/twmb/franz-go v1.16.1 github.com/xuri/excelize/v2 v2.8.1 - github.com/zelenin/go-tdlib v0.7.2 + github.com/zelenin/go-tdlib v0.7.0 go.uber.org/zap v1.27.0 google.golang.org/grpc v1.64.0 google.golang.org/protobuf v1.34.2 penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240624105135-6982631f2a4b penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 + penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615192328-b2f5dffe92ae ) require ( @@ -45,7 +46,6 @@ require ( github.com/minio/minio-go/v7 v7.0.70 // indirect github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect github.com/pierrec/lz4/v4 v4.1.21 // indirect - github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/richardlehane/mscfb v1.0.4 // indirect github.com/richardlehane/msoleps v1.0.3 // indirect @@ -67,7 +67,5 @@ require ( google.golang.org/genproto/googleapis/rpc v0.0.0-20240610135401-a8a62080eff3 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/ini.v1 v1.67.0 // indirect - gopkg.in/tucnak/telebot.v2 v2.5.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect - penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615192328-b2f5dffe92ae // indirect ) diff --git a/go.sum b/go.sum index ac2a590..9df3896 100644 --- a/go.sum +++ b/go.sum @@ -26,6 +26,8 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg= +github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= @@ -134,13 +136,14 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/tealeg/xlsx v1.0.5 h1:+f8oFmvY8Gw1iUXzPk+kz+4GpbDZPK1FhPiQRd+ypgE= github.com/tealeg/xlsx v1.0.5/go.mod h1:btRS8dz54TDnvKNosuAqxrM1QgN1udgk9O34bDCnORM= +github.com/themakers/bdd v0.0.0-20210316111417-6b1dfe326f33 h1:N9f/Q+2Ssa+yDcbfaoLTYvXmdeyUUxsJKdPUVsjSmiA= +github.com/themakers/bdd v0.0.0-20210316111417-6b1dfe326f33/go.mod h1:rpcH99JknBh8seZmlOlUg51gasZH6QH34oXNsIwYT6E= github.com/themakers/hlog v0.0.0-20191205140925-235e0e4baddf h1:TJJm6KcBssmbWzplF5lzixXl1RBAi/ViPs1GaSOkhwo= github.com/themakers/hlog v0.0.0-20191205140925-235e0e4baddf/go.mod h1:1FsorU3vnXO9xS9SrhUp8fRb/6H/Zfll0rPt1i4GWaA= github.com/twmb/franz-go v1.16.1 h1:rpWc7fB9jd7TgmCyfxzenBI+QbgS8ZfJOUQE+tzPtbE= @@ -161,8 +164,8 @@ github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 h1:hPVCafDV85blFTabnqKgNh github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -github.com/zelenin/go-tdlib v0.7.2 h1:XpD77+t3bix62TsEZbuQeDls6QQJbcovpb8SVVfyNAA= -github.com/zelenin/go-tdlib v0.7.2/go.mod h1:yqNbNZenZtXPKgf9hDuyZbsRz7qlxOxdfKOc+sAxxIE= +github.com/zelenin/go-tdlib v0.7.0 h1:6rTf+kzDUdowUJy7Nd5/RHZAHtLjDjti3qm77JYvSNw= +github.com/zelenin/go-tdlib v0.7.0/go.mod h1:Xs8fXbk5n7VaPyrSs9DP7QYoBScWYsjX+lUcWmx1DIU= go.etcd.io/bbolt v1.3.6 h1:/ecaJf0sk1l4l6V4awd65v2C3ILy7MSj+s/x1ADCIMU= go.etcd.io/bbolt v1.3.6/go.mod h1:qXsaaIqmgQH0T+OPdb99Bf+PKfBBQVAdyD6TY9G8XM4= go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= @@ -288,7 +291,5 @@ penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240624105135-698 penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240624105135-6982631f2a4b/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 h1:jiO8GWO+3sCnDAV8/NAV8tQIUwae/I6/xiDilW7zf0o= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990/go.mod h1:zswBuTwmEsFHBVRu1nkG3/Fwylk5Vcm8OUm9iWxccSE= -penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615191139-35b268acbfed h1:/1hG64JeCaRTj6bQf3hzriuXzzGwOR0NAT72EaeEaVk= -penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615191139-35b268acbfed/go.mod h1:3ml0dAGT8U8RhpevKBfRgG6yKZum8EI2uJxAb2WCIy4= penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615192328-b2f5dffe92ae h1:vlGInCsQSDA464q7OMv3EWGUviQWQdEcpLc8HIRo+rE= penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615192328-b2f5dffe92ae/go.mod h1:3ml0dAGT8U8RhpevKBfRgG6yKZum8EI2uJxAb2WCIy4= From ddbd8dc97e39f30e78014d3c654a520c7b670ef5 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 27 Jun 2024 12:29:06 +0300 Subject: [PATCH 15/57] test local tdlib compile passed --- .gitignore | 1 + app/app.go | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index ca20f47..52dacce 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,4 @@ worker/worker storer/storer answerer/answerer core +/.tdlib/ diff --git a/app/app.go b/app/app.go index 6012999..fd5cfb4 100644 --- a/app/app.go +++ b/app/app.go @@ -70,7 +70,6 @@ type Options struct { TrashLogHost string `env:"TRASH_LOG_HOST" default:"localhost:7113"` ModuleLogger string `env:"MODULE_LOGGER" default:"core-local"` ClickHouseCred string `env:"CLICK_HOUSE_CRED" default:"tcp://10.8.0.15:9000/default?sslmode=disable"` - ApiID int32 `env:"TG_API_ID" default:""` ApiHash string `env:"TG_API_HASH" default:""` } @@ -155,7 +154,7 @@ func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.Co privilegeController := privilege.NewPrivilege(clientData, fiberClient) go tools.PublishPrivilege(privilegeController, 10, 5*time.Minute) - _, err = telegram.NewTelegramClient(options.ApiID, options.ApiHash) + _, err = telegram.NewTelegramClient(0, options.ApiHash) if err != nil { panic(fmt.Sprintf("Error init tg_client: %v", err)) } From c00899f41a4ef61b07821234abc11fab94ac19c9 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 27 Jun 2024 16:27:16 +0300 Subject: [PATCH 16/57] add tg service rest --- .gitignore | 1 + service/service.go | 6 ++++++ service/telegram_svc.go | 19 +++++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 service/telegram_svc.go diff --git a/.gitignore b/.gitignore index 52dacce..e503e41 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ storer/storer answerer/answerer core /.tdlib/ +/unsetrecover.bolt diff --git a/service/service.go b/service/service.go index 8f476a9..09787d8 100644 --- a/service/service.go +++ b/service/service.go @@ -81,4 +81,10 @@ func (s *Service) Register(app *fiber.App) { app.Post("/statistic/:quizID/questions", s.GetQuestionsStatistics) app.Post("/statistic", s.AllServiceStatistics) app.Get("/statistics/:quizID/pipelines", s.GetPipelinesStatistics) + + //telegram handlers + app.Get("/telegram/pool", s.GetPoolTgBots) + app.Post("/telegram/create", s.AddingTgBot) + app.Delete("/telegram/:id", s.DeleteTgBotByID) + app.Post("/telegram/auth", s.SendAuthTgCode) } diff --git a/service/telegram_svc.go b/service/telegram_svc.go new file mode 100644 index 0000000..cb39baf --- /dev/null +++ b/service/telegram_svc.go @@ -0,0 +1,19 @@ +package service + +import "github.com/gofiber/fiber/v2" + +func (s *Service) GetPoolTgBots(ctx *fiber.Ctx) error { + return nil +} + +func (s *Service) AddingTgBot(ctx *fiber.Ctx) error { + return nil +} + +func (s *Service) DeleteTgBotByID(ctx *fiber.Ctx) error { + return nil +} + +func (s *Service) SendAuthTgCode(ctx *fiber.Ctx) error { + return nil +} From 8b00ad820f35144737d739239671692629bb50da Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 30 Jun 2024 21:02:23 +0300 Subject: [PATCH 17/57] added tg methods need todo --- app/app.go | 16 +++--- clients/telegram/tg.go | 81 ++++++++++++++-------------- go.mod | 8 +-- go.sum | 8 +-- service/service.go | 42 ++++++++------- service/telegram_svc.go | 115 +++++++++++++++++++++++++++++++++++++--- 6 files changed, 187 insertions(+), 83 deletions(-) diff --git a/app/app.go b/app/app.go index fd5cfb4..6ab44cf 100644 --- a/app/app.go +++ b/app/app.go @@ -70,7 +70,6 @@ type Options struct { TrashLogHost string `env:"TRASH_LOG_HOST" default:"localhost:7113"` ModuleLogger string `env:"MODULE_LOGGER" default:"core-local"` ClickHouseCred string `env:"CLICK_HOUSE_CRED" default:"tcp://10.8.0.15:9000/default?sslmode=disable"` - ApiHash string `env:"TG_API_HASH" default:""` } func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.CommonApp, error) { @@ -154,9 +153,9 @@ func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.Co privilegeController := privilege.NewPrivilege(clientData, fiberClient) go tools.PublishPrivilege(privilegeController, 10, 5*time.Minute) - _, err = telegram.NewTelegramClient(0, options.ApiHash) + tgClient, err := telegram.NewTelegramClient(ctx, pgdal) if err != nil { - panic(fmt.Sprintf("Error init tg_client: %v", err)) + panic(fmt.Sprintf("failed init tg clietns: %v", err)) } // todo подумать над реализацией всего а то пока мне кажется что немного каша получается такой предикт что через некоторое время @@ -180,11 +179,12 @@ func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.Co app.Get("/readiness", healthchecks.Readiness(&workerErr)) //todo parametrized readiness. should discuss ready reason svc := service.New(service.Deps{ - Dal: pgdal, - AuthClient: authClient, - Producer: producer, - ServiceName: options.ServiceName, - ChDAL: chDal, + Dal: pgdal, + AuthClient: authClient, + Producer: producer, + ServiceName: options.ServiceName, + ChDAL: chDal, + TelegramClient: tgClient, }) svc.Register(app) diff --git a/clients/telegram/tg.go b/clients/telegram/tg.go index c7364a3..813ee7c 100644 --- a/clients/telegram/tg.go +++ b/clients/telegram/tg.go @@ -1,58 +1,57 @@ package telegram import ( + "context" "fmt" - "github.com/zelenin/go-tdlib/client" - "path/filepath" + "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal" + "penahub.gitlab.yandexcloud.net/backend/tdlib/client" ) type TelegramClient struct { - Client *client.Client + repo *dal.DAL + TgClients []*client.Client + WaitingClients map[string]WaitingClient } -func NewTelegramClient(apiID int32, apiHash string) (*TelegramClient, error) { - authorizer := client.ClientAuthorizer() - go client.CliInteractor(authorizer) - - authorizer.TdlibParameters <- &client.SetTdlibParametersRequest{ - UseTestDc: false, - DatabaseDirectory: filepath.Join(".tdlib", "database"), - FilesDirectory: filepath.Join(".tdlib", "files"), - UseFileDatabase: false, - UseChatInfoDatabase: false, - UseMessageDatabase: false, - UseSecretChats: false, - ApiId: apiID, - ApiHash: apiHash, - SystemLanguageCode: "en", - DeviceModel: "Server", - SystemVersion: "1.0.0", - ApplicationVersion: "1.0.0", - } - - _, err := client.SetLogVerbosityLevel(&client.SetLogVerbosityLevelRequest{ - NewVerbosityLevel: 1, - }) - if err != nil { - return nil, err - } - - tdlibClient, err := client.NewClient(authorizer) - if err != nil { - return nil, err - } - - me, err := tdlibClient.GetMe() - if err != nil { - return nil, err - } - fmt.Printf("Me: %s %s [%v]", me.FirstName, me.LastName, me.Usernames) +type WaitingClient struct { + TdLibClient *client.Client + PreviousReq AuthTgUserReq + Authorizer *client.ClientAuthorizer +} +// todo come back saved tg accs to slice for check this status +func NewTelegramClient(ctx context.Context, repo *dal.DAL) (*TelegramClient, error) { return &TelegramClient{ - Client: tdlibClient, + repo: repo, + TgClients: make([]*client.Client, 0), + WaitingClients: make(map[string]WaitingClient), }, nil } +type AuthTgUserReq struct { + ApiID int32 `json:"api_id"` + ApiHash string `json:"api_hash"` + PhoneNumber string `json:"phone_number"` + Password string `json:"password"` +} + +func (tg *TelegramClient) AddedToMap(data WaitingClient, id string) { + fmt.Println("AddedToMap") + tg.WaitingClients[id] = data +} + +func (tg *TelegramClient) GetFromMap(id string) (WaitingClient, bool) { + fmt.Println("GetFromMap") + if data, ok := tg.WaitingClients[id]; ok { + return data, true + } + return WaitingClient{}, false +} + +func (tg *TelegramClient) SaveTgAccount() { + +} + func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string, error) { return "", nil } diff --git a/go.mod b/go.mod index f036a57..d3e8319 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module penahub.gitlab.yandexcloud.net/backend/quiz/core -go 1.21.4 +go 1.22.4 require ( github.com/gofiber/fiber/v2 v2.52.4 @@ -9,18 +9,19 @@ require ( github.com/joho/godotenv v1.5.1 github.com/lib/pq v1.10.9 github.com/pioz/faker v1.7.3 + github.com/rs/xid v1.5.0 github.com/skeris/appInit v1.0.2 github.com/stretchr/testify v1.9.0 github.com/themakers/hlog v0.0.0-20191205140925-235e0e4baddf github.com/twmb/franz-go v1.16.1 github.com/xuri/excelize/v2 v2.8.1 - github.com/zelenin/go-tdlib v0.7.0 go.uber.org/zap v1.27.0 google.golang.org/grpc v1.64.0 google.golang.org/protobuf v1.34.2 penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c - penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240624105135-6982631f2a4b + penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240627194440-f997b952c31f penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 + penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240630165148-3c27e15d9ac1 penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615192328-b2f5dffe92ae ) @@ -50,7 +51,6 @@ require ( github.com/richardlehane/mscfb v1.0.4 // indirect github.com/richardlehane/msoleps v1.0.3 // indirect github.com/rivo/uniseg v0.4.7 // indirect - github.com/rs/xid v1.5.0 // indirect github.com/tealeg/xlsx v1.0.5 // indirect github.com/twmb/franz-go/pkg/kmsg v1.8.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect diff --git a/go.sum b/go.sum index 9df3896..e3e4ab0 100644 --- a/go.sum +++ b/go.sum @@ -164,8 +164,6 @@ github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 h1:hPVCafDV85blFTabnqKgNh github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -github.com/zelenin/go-tdlib v0.7.0 h1:6rTf+kzDUdowUJy7Nd5/RHZAHtLjDjti3qm77JYvSNw= -github.com/zelenin/go-tdlib v0.7.0/go.mod h1:Xs8fXbk5n7VaPyrSs9DP7QYoBScWYsjX+lUcWmx1DIU= go.etcd.io/bbolt v1.3.6 h1:/ecaJf0sk1l4l6V4awd65v2C3ILy7MSj+s/x1ADCIMU= go.etcd.io/bbolt v1.3.6/go.mod h1:qXsaaIqmgQH0T+OPdb99Bf+PKfBBQVAdyD6TY9G8XM4= go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= @@ -287,9 +285,11 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c h1:CWb4UcuNXhd1KTNOmy2U0TJO4+Qxgxrj5cwkyFqbgrk= penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c/go.mod h1:+bPxq2wfW5S1gd+83vZYmHm33AE7nEBfznWS8AM1TKE= -penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240624105135-6982631f2a4b h1:dNBWrclJAXP/JFRYQPXWAqmF/UihGxH4oZ9Vs0lfm40= -penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240624105135-6982631f2a4b/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240627194440-f997b952c31f h1:cvXLN5J6Np+lRbL1l4Kumu1uRB/D1OR2cbXc3qATzWc= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240627194440-f997b952c31f/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 h1:jiO8GWO+3sCnDAV8/NAV8tQIUwae/I6/xiDilW7zf0o= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990/go.mod h1:zswBuTwmEsFHBVRu1nkG3/Fwylk5Vcm8OUm9iWxccSE= +penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240630165148-3c27e15d9ac1 h1:fGhu5JuLODJ4pLb4YiyrioFXSQS+UP+KUQhcLwzIeWs= +penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240630165148-3c27e15d9ac1/go.mod h1:AkE19hcbDwB7hoEASwImm7rUI+cK/8jMVJaTvMK4F+c= penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615192328-b2f5dffe92ae h1:vlGInCsQSDA464q7OMv3EWGUviQWQdEcpLc8HIRo+rE= penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615192328-b2f5dffe92ae/go.mod h1:3ml0dAGT8U8RhpevKBfRgG6yKZum8EI2uJxAb2WCIy4= diff --git a/service/service.go b/service/service.go index 09787d8..983e9c9 100644 --- a/service/service.go +++ b/service/service.go @@ -5,32 +5,36 @@ import ( "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal" "penahub.gitlab.yandexcloud.net/backend/quiz/core/brokers" "penahub.gitlab.yandexcloud.net/backend/quiz/core/clients/auth" + "penahub.gitlab.yandexcloud.net/backend/quiz/core/clients/telegram" ) // Service is an entity for http requests handling type Service struct { - dal *dal.DAL - authClient *auth.AuthClient - producer *brokers.Producer - serviceName string - chDAL *dal.ClickHouseDAL + dal *dal.DAL + authClient *auth.AuthClient + producer *brokers.Producer + serviceName string + chDAL *dal.ClickHouseDAL + telegramClient *telegram.TelegramClient } type Deps struct { - Dal *dal.DAL - AuthClient *auth.AuthClient - Producer *brokers.Producer - ServiceName string - ChDAL *dal.ClickHouseDAL + Dal *dal.DAL + AuthClient *auth.AuthClient + Producer *brokers.Producer + ServiceName string + ChDAL *dal.ClickHouseDAL + TelegramClient *telegram.TelegramClient } func New(deps Deps) *Service { return &Service{ - dal: deps.Dal, - authClient: deps.AuthClient, - producer: deps.Producer, - serviceName: deps.ServiceName, - chDAL: deps.ChDAL, + dal: deps.Dal, + authClient: deps.AuthClient, + producer: deps.Producer, + serviceName: deps.ServiceName, + chDAL: deps.ChDAL, + telegramClient: deps.TelegramClient, } } @@ -83,8 +87,8 @@ func (s *Service) Register(app *fiber.App) { app.Get("/statistics/:quizID/pipelines", s.GetPipelinesStatistics) //telegram handlers - app.Get("/telegram/pool", s.GetPoolTgBots) - app.Post("/telegram/create", s.AddingTgBot) - app.Delete("/telegram/:id", s.DeleteTgBotByID) - app.Post("/telegram/auth", s.SendAuthTgCode) + app.Get("/telegram/pool", s.GetPoolTgAccounts) + app.Post("/telegram/create", s.AddingTgAccount) + app.Delete("/telegram/:id", s.DeleteTgAccountByID) + app.Post("/telegram/setCode", s.SettingTgCode) } diff --git a/service/telegram_svc.go b/service/telegram_svc.go index cb39baf..ab1a99d 100644 --- a/service/telegram_svc.go +++ b/service/telegram_svc.go @@ -1,19 +1,120 @@ package service -import "github.com/gofiber/fiber/v2" +import ( + "fmt" + "github.com/gofiber/fiber/v2" + "github.com/rs/xid" + "path/filepath" + "penahub.gitlab.yandexcloud.net/backend/quiz/core/clients/telegram" + "penahub.gitlab.yandexcloud.net/backend/tdlib/client" +) -func (s *Service) GetPoolTgBots(ctx *fiber.Ctx) error { +type Message struct { + Type string `json:"type"` + Data string `json:"data"` +} + +func (s *Service) GetPoolTgAccounts(ctx *fiber.Ctx) error { return nil } -func (s *Service) AddingTgBot(ctx *fiber.Ctx) error { - return nil +// todo save to slice and save to db for rewoke +func (s *Service) AddingTgAccount(ctx *fiber.Ctx) error { + var req telegram.AuthTgUserReq + if err := ctx.BodyParser(&req); err != nil { + return ctx.Status(fiber.StatusBadRequest).SendString("Invalid request data") + } + authorizer := client.ClientAuthorizerr() + authorizer.TdlibParameters <- &client.SetTdlibParametersRequest{ + UseTestDc: false, + DatabaseDirectory: filepath.Join(".tdlib", "database"), + FilesDirectory: filepath.Join(".tdlib", "files"), + UseFileDatabase: false, + UseChatInfoDatabase: false, + UseMessageDatabase: false, + UseSecretChats: false, + ApiId: req.ApiID, + ApiHash: req.ApiHash, + SystemLanguageCode: "en", + DeviceModel: "Server", + SystemVersion: "1.0.0", + ApplicationVersion: "1.0.0", + } + + _, err := client.SetLogVerbosityLevel(&client.SetLogVerbosityLevelRequest{ + NewVerbosityLevel: 1, + }) + if err != nil { + return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) + } + + var tdlibClient *client.Client + + // todo проверить как завершается + go func() { + tdlibClient, err = client.NewClient(authorizer) + if err != nil { + fmt.Println("new client failed", err) + return + } + }() + + for { + state, ok := <-authorizer.State + if !ok { + return ctx.SendStatus(fiber.StatusOK) + } + fmt.Println("currnet state:", state) + switch state.AuthorizationStateType() { + case client.TypeAuthorizationStateWaitPhoneNumber: + authorizer.PhoneNumber <- req.PhoneNumber + + case client.TypeAuthorizationStateWaitCode: + id := xid.New() + s.telegramClient.AddedToMap(telegram.WaitingClient{ + TdLibClient: tdlibClient, + PreviousReq: req, + Authorizer: authorizer, + }, id.String()) + return ctx.Status(fiber.StatusOK).JSON(id.String()) + + case client.TypeAuthorizationStateLoggingOut, client.TypeAuthorizationStateClosing, client.TypeAuthorizationStateClosed: + return ctx.Status(fiber.StatusForbidden).SendString("auth failed") + } + } } -func (s *Service) DeleteTgBotByID(ctx *fiber.Ctx) error { - return nil +func (s *Service) SettingTgCode(ctx *fiber.Ctx) error { + var req struct { + Code string `json:"code"` + ID string `json:"id"` + } + if err := ctx.BodyParser(&req); err != nil { + return ctx.Status(fiber.StatusBadRequest).SendString("Invalid request data") + } + + data, ok := s.telegramClient.GetFromMap(req.ID) + if !ok { + return ctx.Status(fiber.StatusBadRequest).SendString("Invalid id, don't have data") + } + data.Authorizer.Code <- req.Code + for { + state, ok := <-data.Authorizer.State + if !ok { + return ctx.SendStatus(fiber.StatusOK) + } + fmt.Println("currnet state:", state) + switch state.AuthorizationStateType() { + case client.TypeAuthorizationStateReady: + return ctx.SendStatus(fiber.StatusOK) + case client.TypeAuthorizationStateWaitPassword: + data.Authorizer.Password <- data.PreviousReq.Password + case client.TypeAuthorizationStateLoggingOut, client.TypeAuthorizationStateClosing, client.TypeAuthorizationStateClosed: + return ctx.Status(fiber.StatusForbidden).SendString("auth failed") + } + } } -func (s *Service) SendAuthTgCode(ctx *fiber.Ctx) error { +func (s *Service) DeleteTgAccountByID(ctx *fiber.Ctx) error { return nil } From fec5374964a6ad60af32b83e202ecdd347e77fbe Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 1 Jul 2024 11:16:47 +0300 Subject: [PATCH 18/57] finish debug auth --- go.mod | 2 +- go.sum | 6 ++++++ service/telegram_svc.go | 25 ++++++++++++++----------- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index d3e8319..914c03b 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240627194440-f997b952c31f penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 - penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240630165148-3c27e15d9ac1 + penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615192328-b2f5dffe92ae ) diff --git a/go.sum b/go.sum index e3e4ab0..99b03e3 100644 --- a/go.sum +++ b/go.sum @@ -291,5 +291,11 @@ penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e0 penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990/go.mod h1:zswBuTwmEsFHBVRu1nkG3/Fwylk5Vcm8OUm9iWxccSE= penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240630165148-3c27e15d9ac1 h1:fGhu5JuLODJ4pLb4YiyrioFXSQS+UP+KUQhcLwzIeWs= penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240630165148-3c27e15d9ac1/go.mod h1:AkE19hcbDwB7hoEASwImm7rUI+cK/8jMVJaTvMK4F+c= +penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701074825-8eceb99cd2ab h1:axHShcqrVZHx6CTB0OJW8ac3Hqi7DxfJu2e5nuusxE4= +penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701074825-8eceb99cd2ab/go.mod h1:AkE19hcbDwB7hoEASwImm7rUI+cK/8jMVJaTvMK4F+c= +penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075500-8c122317a246 h1:FRQfWwaSJB2eyp8eTNdulg5/V2bC1VnmC6YcGm7mYZg= +penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075500-8c122317a246/go.mod h1:AkE19hcbDwB7hoEASwImm7rUI+cK/8jMVJaTvMK4F+c= +penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f h1:Qli89wgu0T7nG4VECXZOZ40fjE/hVVfxF3hTaSYS008= +penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f/go.mod h1:AkE19hcbDwB7hoEASwImm7rUI+cK/8jMVJaTvMK4F+c= penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615192328-b2f5dffe92ae h1:vlGInCsQSDA464q7OMv3EWGUviQWQdEcpLc8HIRo+rE= penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615192328-b2f5dffe92ae/go.mod h1:3ml0dAGT8U8RhpevKBfRgG6yKZum8EI2uJxAb2WCIy4= diff --git a/service/telegram_svc.go b/service/telegram_svc.go index ab1a99d..bf0e5ba 100644 --- a/service/telegram_svc.go +++ b/service/telegram_svc.go @@ -49,26 +49,29 @@ func (s *Service) AddingTgAccount(ctx *fiber.Ctx) error { } var tdlibClient *client.Client - - // todo проверить как завершается + // завершается уже в другом контроллере + var goErr error go func() { - tdlibClient, err = client.NewClient(authorizer) - if err != nil { + tdlibClient, goErr = client.NewClient(authorizer) + if goErr != nil { fmt.Println("new client failed", err) return } + fmt.Println("i am down") }() + if goErr != nil { + return ctx.Status(fiber.StatusInternalServerError).SendString(goErr.Error()) + } for { state, ok := <-authorizer.State if !ok { - return ctx.SendStatus(fiber.StatusOK) + return ctx.Status(fiber.StatusOK).SendString("state chan is close auth maybe ok") } fmt.Println("currnet state:", state) switch state.AuthorizationStateType() { case client.TypeAuthorizationStateWaitPhoneNumber: authorizer.PhoneNumber <- req.PhoneNumber - case client.TypeAuthorizationStateWaitCode: id := xid.New() s.telegramClient.AddedToMap(telegram.WaitingClient{ @@ -76,10 +79,10 @@ func (s *Service) AddingTgAccount(ctx *fiber.Ctx) error { PreviousReq: req, Authorizer: authorizer, }, id.String()) - return ctx.Status(fiber.StatusOK).JSON(id.String()) + return ctx.Status(fiber.StatusOK).JSON(fiber.Map{"id": id.String()}) case client.TypeAuthorizationStateLoggingOut, client.TypeAuthorizationStateClosing, client.TypeAuthorizationStateClosed: - return ctx.Status(fiber.StatusForbidden).SendString("auth failed") + return ctx.Status(fiber.StatusForbidden).SendString(fmt.Sprintf("auth failed, last state is %s", state)) } } } @@ -101,16 +104,16 @@ func (s *Service) SettingTgCode(ctx *fiber.Ctx) error { for { state, ok := <-data.Authorizer.State if !ok { - return ctx.SendStatus(fiber.StatusOK) + return ctx.Status(fiber.StatusOK).SendString("state chan is close auth maybe ok") } fmt.Println("currnet state:", state) switch state.AuthorizationStateType() { case client.TypeAuthorizationStateReady: - return ctx.SendStatus(fiber.StatusOK) + return ctx.Status(fiber.StatusOK).SendString("auth success") case client.TypeAuthorizationStateWaitPassword: data.Authorizer.Password <- data.PreviousReq.Password case client.TypeAuthorizationStateLoggingOut, client.TypeAuthorizationStateClosing, client.TypeAuthorizationStateClosed: - return ctx.Status(fiber.StatusForbidden).SendString("auth failed") + return ctx.Status(fiber.StatusForbidden).SendString(fmt.Sprintf("auth failed, last state is %s", state)) } } } From 51aa0e4aeef520ae6ca65599be516405f0f8fa67 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 1 Jul 2024 11:53:42 +0300 Subject: [PATCH 19/57] added save to slice active tg clients and to db for restore --- go.sum | 6 ------ service/telegram_svc.go | 15 +++++++++++++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/go.sum b/go.sum index 99b03e3..5ea6d3d 100644 --- a/go.sum +++ b/go.sum @@ -289,12 +289,6 @@ penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240627194440-f99 penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240627194440-f997b952c31f/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 h1:jiO8GWO+3sCnDAV8/NAV8tQIUwae/I6/xiDilW7zf0o= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990/go.mod h1:zswBuTwmEsFHBVRu1nkG3/Fwylk5Vcm8OUm9iWxccSE= -penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240630165148-3c27e15d9ac1 h1:fGhu5JuLODJ4pLb4YiyrioFXSQS+UP+KUQhcLwzIeWs= -penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240630165148-3c27e15d9ac1/go.mod h1:AkE19hcbDwB7hoEASwImm7rUI+cK/8jMVJaTvMK4F+c= -penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701074825-8eceb99cd2ab h1:axHShcqrVZHx6CTB0OJW8ac3Hqi7DxfJu2e5nuusxE4= -penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701074825-8eceb99cd2ab/go.mod h1:AkE19hcbDwB7hoEASwImm7rUI+cK/8jMVJaTvMK4F+c= -penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075500-8c122317a246 h1:FRQfWwaSJB2eyp8eTNdulg5/V2bC1VnmC6YcGm7mYZg= -penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075500-8c122317a246/go.mod h1:AkE19hcbDwB7hoEASwImm7rUI+cK/8jMVJaTvMK4F+c= penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f h1:Qli89wgu0T7nG4VECXZOZ40fjE/hVVfxF3hTaSYS008= penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f/go.mod h1:AkE19hcbDwB7hoEASwImm7rUI+cK/8jMVJaTvMK4F+c= penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615192328-b2f5dffe92ae h1:vlGInCsQSDA464q7OMv3EWGUviQWQdEcpLc8HIRo+rE= diff --git a/service/telegram_svc.go b/service/telegram_svc.go index bf0e5ba..e3be6b6 100644 --- a/service/telegram_svc.go +++ b/service/telegram_svc.go @@ -5,6 +5,7 @@ import ( "github.com/gofiber/fiber/v2" "github.com/rs/xid" "path/filepath" + "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model" "penahub.gitlab.yandexcloud.net/backend/quiz/core/clients/telegram" "penahub.gitlab.yandexcloud.net/backend/tdlib/client" ) @@ -18,7 +19,6 @@ func (s *Service) GetPoolTgAccounts(ctx *fiber.Ctx) error { return nil } -// todo save to slice and save to db for rewoke func (s *Service) AddingTgAccount(ctx *fiber.Ctx) error { var req telegram.AuthTgUserReq if err := ctx.BodyParser(&req); err != nil { @@ -109,7 +109,18 @@ func (s *Service) SettingTgCode(ctx *fiber.Ctx) error { fmt.Println("currnet state:", state) switch state.AuthorizationStateType() { case client.TypeAuthorizationStateReady: - return ctx.Status(fiber.StatusOK).SendString("auth success") + s.telegramClient.TgClients = append(s.telegramClient.TgClients, data.TdLibClient) + id, err := s.dal.TgRepo.CreateTgAccount(ctx.Context(), model.TgAccount{ + ApiID: data.PreviousReq.ApiID, + ApiHash: data.PreviousReq.ApiHash, + PhoneNumber: data.PreviousReq.PhoneNumber, + Status: model.ActiveTg, + Password: data.PreviousReq.Password, + }) + if err != nil { + return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) + } + return ctx.Status(fiber.StatusOK).JSON(fiber.Map{"id": id}) case client.TypeAuthorizationStateWaitPassword: data.Authorizer.Password <- data.PreviousReq.Password case client.TypeAuthorizationStateLoggingOut, client.TypeAuthorizationStateClosing, client.TypeAuthorizationStateClosed: From 61ea9b7969c80e5e70f1cd4e33a40607177d37ee Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 1 Jul 2024 14:15:15 +0300 Subject: [PATCH 20/57] added base logic for restore tg clients from db --- clients/telegram/tg.go | 107 ++++++++++++++++++++++++++++++++++++++-- go.mod | 2 +- go.sum | 4 +- service/telegram_svc.go | 3 +- 4 files changed, 107 insertions(+), 9 deletions(-) diff --git a/clients/telegram/tg.go b/clients/telegram/tg.go index 813ee7c..74d7815 100644 --- a/clients/telegram/tg.go +++ b/clients/telegram/tg.go @@ -2,15 +2,22 @@ package telegram import ( "context" + "database/sql" + "errors" "fmt" + "path/filepath" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal" + "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model" "penahub.gitlab.yandexcloud.net/backend/tdlib/client" + "sync" + "time" ) type TelegramClient struct { repo *dal.DAL TgClients []*client.Client WaitingClients map[string]WaitingClient + mu sync.Mutex } type WaitingClient struct { @@ -21,11 +28,92 @@ type WaitingClient struct { // todo come back saved tg accs to slice for check this status func NewTelegramClient(ctx context.Context, repo *dal.DAL) (*TelegramClient, error) { - return &TelegramClient{ + tgClient := &TelegramClient{ repo: repo, TgClients: make([]*client.Client, 0), WaitingClients: make(map[string]WaitingClient), - }, nil + } + + allTgAccounts, err := repo.TgRepo.GetAllTgAccounts(ctx) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return tgClient, nil + } + return nil, err + } + + for _, account := range allTgAccounts { + authorizer := client.ClientAuthorizerr() + authorizer.TdlibParameters <- &client.SetTdlibParametersRequest{ + UseTestDc: false, + DatabaseDirectory: filepath.Join(".tdlib", "database"), + FilesDirectory: filepath.Join(".tdlib", "files"), + UseFileDatabase: false, + UseChatInfoDatabase: false, + UseMessageDatabase: false, + UseSecretChats: false, + ApiId: account.ApiID, + ApiHash: account.ApiHash, + SystemLanguageCode: "en", + DeviceModel: "Server", + SystemVersion: "1.0.0", + ApplicationVersion: "1.0.0", + } + + _, err := client.SetLogVerbosityLevel(&client.SetLogVerbosityLevelRequest{ + NewVerbosityLevel: 1, + }) + if err != nil { + return nil, err + } + + var tdlibClient *client.Client + var goErr error + go func() { + tdlibClient, goErr = client.NewClient(authorizer) + if goErr != nil { + fmt.Println("new client failed", err) + return + } + fmt.Println("i am down") + }() + if goErr != nil { + return nil, goErr + } + + for { + state, ok := <-authorizer.State + if !ok { + break + } + fmt.Println("currnet state:", state) + switch state.AuthorizationStateType() { + case client.TypeAuthorizationStateWaitPhoneNumber: + authorizer.PhoneNumber <- account.PhoneNumber + case client.TypeAuthorizationStateWaitCode: + // todo inactive + + case client.TypeAuthorizationStateLoggingOut, client.TypeAuthorizationStateClosing, client.TypeAuthorizationStateClosed: + // todo inactive + case client.TypeAuthorizationStateReady: + // костыль так как в либе тож костыль стоит пока там ьд обновиться будет ниловый всегда клиент + time.Sleep(3 * time.Second) + me, err := tdlibClient.GetMe() + if err != nil { + return nil, err + } + fmt.Printf("Me: %s %s [%v]", me.FirstName, me.LastName, me.Usernames) + tgClient.mu.Lock() + tgClient.TgClients = append(tgClient.TgClients, tdlibClient) + tgClient.mu.Unlock() + break + case client.TypeAuthorizationStateWaitPassword: + authorizer.Password <- account.Password + } + } + } + + return tgClient, nil } type AuthTgUserReq struct { @@ -37,19 +125,30 @@ type AuthTgUserReq struct { func (tg *TelegramClient) AddedToMap(data WaitingClient, id string) { fmt.Println("AddedToMap") + tg.mu.Lock() + defer tg.mu.Unlock() tg.WaitingClients[id] = data } func (tg *TelegramClient) GetFromMap(id string) (WaitingClient, bool) { fmt.Println("GetFromMap") + tg.mu.Lock() + defer tg.mu.Unlock() if data, ok := tg.WaitingClients[id]; ok { return data, true } return WaitingClient{}, false } -func (tg *TelegramClient) SaveTgAccount() { - +func (tg *TelegramClient) SaveTgAccount(ctx context.Context, tdLibClient *client.Client, account model.TgAccount) (int64, error) { + tg.mu.Lock() + defer tg.mu.Unlock() + tg.TgClients = append(tg.TgClients, tdLibClient) + id, err := tg.repo.TgRepo.CreateTgAccount(ctx, account) + if err != nil { + return 0, err + } + return id, nil } func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string, error) { diff --git a/go.mod b/go.mod index 914c03b..02d5ff2 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,7 @@ require ( google.golang.org/grpc v1.64.0 google.golang.org/protobuf v1.34.2 penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c - penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240627194440-f997b952c31f + penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701091250-bf14e745201b penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615192328-b2f5dffe92ae diff --git a/go.sum b/go.sum index 5ea6d3d..c700cbf 100644 --- a/go.sum +++ b/go.sum @@ -285,8 +285,8 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c h1:CWb4UcuNXhd1KTNOmy2U0TJO4+Qxgxrj5cwkyFqbgrk= penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c/go.mod h1:+bPxq2wfW5S1gd+83vZYmHm33AE7nEBfznWS8AM1TKE= -penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240627194440-f997b952c31f h1:cvXLN5J6Np+lRbL1l4Kumu1uRB/D1OR2cbXc3qATzWc= -penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240627194440-f997b952c31f/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701091250-bf14e745201b h1:1ALCrJxIatwnqpIUJIx31Cq+rnxFa3WGEA8scV1idVo= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701091250-bf14e745201b/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 h1:jiO8GWO+3sCnDAV8/NAV8tQIUwae/I6/xiDilW7zf0o= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990/go.mod h1:zswBuTwmEsFHBVRu1nkG3/Fwylk5Vcm8OUm9iWxccSE= penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f h1:Qli89wgu0T7nG4VECXZOZ40fjE/hVVfxF3hTaSYS008= diff --git a/service/telegram_svc.go b/service/telegram_svc.go index e3be6b6..52df80e 100644 --- a/service/telegram_svc.go +++ b/service/telegram_svc.go @@ -109,8 +109,7 @@ func (s *Service) SettingTgCode(ctx *fiber.Ctx) error { fmt.Println("currnet state:", state) switch state.AuthorizationStateType() { case client.TypeAuthorizationStateReady: - s.telegramClient.TgClients = append(s.telegramClient.TgClients, data.TdLibClient) - id, err := s.dal.TgRepo.CreateTgAccount(ctx.Context(), model.TgAccount{ + id, err := s.telegramClient.SaveTgAccount(ctx.Context(), data.TdLibClient, model.TgAccount{ ApiID: data.PreviousReq.ApiID, ApiHash: data.PreviousReq.ApiHash, PhoneNumber: data.PreviousReq.PhoneNumber, From ce8aa02c79c7744dce40abc933fe6278759d9456 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 1 Jul 2024 16:02:26 +0300 Subject: [PATCH 21/57] change logic initialization tg client --- clients/telegram/tg.go | 136 +++++++++++++++++++++-------------------- go.mod | 2 +- go.sum | 2 + 3 files changed, 74 insertions(+), 66 deletions(-) diff --git a/clients/telegram/tg.go b/clients/telegram/tg.go index 74d7815..9610751 100644 --- a/clients/telegram/tg.go +++ b/clients/telegram/tg.go @@ -15,7 +15,7 @@ import ( type TelegramClient struct { repo *dal.DAL - TgClients []*client.Client + TgClients map[int64]*client.Client WaitingClients map[string]WaitingClient mu sync.Mutex } @@ -30,7 +30,7 @@ type WaitingClient struct { func NewTelegramClient(ctx context.Context, repo *dal.DAL) (*TelegramClient, error) { tgClient := &TelegramClient{ repo: repo, - TgClients: make([]*client.Client, 0), + TgClients: make(map[int64]*client.Client), WaitingClients: make(map[string]WaitingClient), } @@ -43,76 +43,82 @@ func NewTelegramClient(ctx context.Context, repo *dal.DAL) (*TelegramClient, err } for _, account := range allTgAccounts { - authorizer := client.ClientAuthorizerr() - authorizer.TdlibParameters <- &client.SetTdlibParametersRequest{ - UseTestDc: false, - DatabaseDirectory: filepath.Join(".tdlib", "database"), - FilesDirectory: filepath.Join(".tdlib", "files"), - UseFileDatabase: false, - UseChatInfoDatabase: false, - UseMessageDatabase: false, - UseSecretChats: false, - ApiId: account.ApiID, - ApiHash: account.ApiHash, - SystemLanguageCode: "en", - DeviceModel: "Server", - SystemVersion: "1.0.0", - ApplicationVersion: "1.0.0", - } - - _, err := client.SetLogVerbosityLevel(&client.SetLogVerbosityLevelRequest{ - NewVerbosityLevel: 1, - }) - if err != nil { - return nil, err - } - - var tdlibClient *client.Client - var goErr error - go func() { - tdlibClient, goErr = client.NewClient(authorizer) - if goErr != nil { - fmt.Println("new client failed", err) - return + if account.Status == model.ActiveTg { + authorizer := client.ClientAuthorizerr() + authorizer.TdlibParameters <- &client.SetTdlibParametersRequest{ + UseTestDc: false, + DatabaseDirectory: filepath.Join(".tdlib", "database"), + FilesDirectory: filepath.Join(".tdlib", "files"), + UseFileDatabase: false, + UseChatInfoDatabase: false, + UseMessageDatabase: false, + UseSecretChats: false, + ApiId: account.ApiID, + ApiHash: account.ApiHash, + SystemLanguageCode: "en", + DeviceModel: "Server", + SystemVersion: "1.0.0", + ApplicationVersion: "1.0.0", } - fmt.Println("i am down") - }() - if goErr != nil { - return nil, goErr - } - for { - state, ok := <-authorizer.State - if !ok { - break + _, err := client.SetLogVerbosityLevel(&client.SetLogVerbosityLevelRequest{ + NewVerbosityLevel: 1, + }) + if err != nil { + return nil, err } - fmt.Println("currnet state:", state) - switch state.AuthorizationStateType() { - case client.TypeAuthorizationStateWaitPhoneNumber: - authorizer.PhoneNumber <- account.PhoneNumber - case client.TypeAuthorizationStateWaitCode: - // todo inactive - case client.TypeAuthorizationStateLoggingOut, client.TypeAuthorizationStateClosing, client.TypeAuthorizationStateClosed: - // todo inactive - case client.TypeAuthorizationStateReady: - // костыль так как в либе тож костыль стоит пока там ьд обновиться будет ниловый всегда клиент - time.Sleep(3 * time.Second) - me, err := tdlibClient.GetMe() - if err != nil { - return nil, err + var tdlibClient *client.Client + var goErr error + go func() { + tdlibClient, goErr = client.NewClient(authorizer) + if goErr != nil { + fmt.Println("new client failed", err) + return + } + fmt.Println("i am down") + }() + if goErr != nil { + return nil, goErr + } + + for { + state, ok := <-authorizer.State + if !ok { + break + } + fmt.Println("currnet state:", state) + switch state.AuthorizationStateType() { + case client.TypeAuthorizationStateWaitPhoneNumber: + authorizer.PhoneNumber <- account.PhoneNumber + case client.TypeAuthorizationStateWaitCode: + err := tgClient.repo.TgRepo.UpdateStatusTg(ctx, account.ID, model.InactiveTg) + if err != nil { + return nil, err + } + case client.TypeAuthorizationStateLoggingOut, client.TypeAuthorizationStateClosing, client.TypeAuthorizationStateClosed: + err := tgClient.repo.TgRepo.UpdateStatusTg(ctx, account.ID, model.InactiveTg) + if err != nil { + return nil, err + } + case client.TypeAuthorizationStateReady: + // костыль так как в либе тож костыль стоит пока там ьд обновиться будет ниловый всегда клиент + time.Sleep(3 * time.Second) + me, err := tdlibClient.GetMe() + if err != nil { + return nil, err + } + fmt.Printf("Me: %s %s [%v]", me.FirstName, me.LastName, me.Usernames) + tgClient.mu.Lock() + tgClient.TgClients[account.ID] = tdlibClient + tgClient.mu.Unlock() + break + case client.TypeAuthorizationStateWaitPassword: + authorizer.Password <- account.Password } - fmt.Printf("Me: %s %s [%v]", me.FirstName, me.LastName, me.Usernames) - tgClient.mu.Lock() - tgClient.TgClients = append(tgClient.TgClients, tdlibClient) - tgClient.mu.Unlock() - break - case client.TypeAuthorizationStateWaitPassword: - authorizer.Password <- account.Password } } } - return tgClient, nil } @@ -143,11 +149,11 @@ func (tg *TelegramClient) GetFromMap(id string) (WaitingClient, bool) { func (tg *TelegramClient) SaveTgAccount(ctx context.Context, tdLibClient *client.Client, account model.TgAccount) (int64, error) { tg.mu.Lock() defer tg.mu.Unlock() - tg.TgClients = append(tg.TgClients, tdLibClient) id, err := tg.repo.TgRepo.CreateTgAccount(ctx, account) if err != nil { return 0, err } + tg.TgClients[id] = tdLibClient return id, nil } diff --git a/go.mod b/go.mod index 02d5ff2..2ae0f33 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,7 @@ require ( google.golang.org/grpc v1.64.0 google.golang.org/protobuf v1.34.2 penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c - penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701091250-bf14e745201b + penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701123546-6dbbc07f9c24 penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615192328-b2f5dffe92ae diff --git a/go.sum b/go.sum index c700cbf..8edd934 100644 --- a/go.sum +++ b/go.sum @@ -287,6 +287,8 @@ penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5 penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c/go.mod h1:+bPxq2wfW5S1gd+83vZYmHm33AE7nEBfznWS8AM1TKE= penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701091250-bf14e745201b h1:1ALCrJxIatwnqpIUJIx31Cq+rnxFa3WGEA8scV1idVo= penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701091250-bf14e745201b/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701123546-6dbbc07f9c24 h1:hcMtuK4H3dyk2Wo911EXRYcG8GV8hZe+UwuRRGOxF+I= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701123546-6dbbc07f9c24/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 h1:jiO8GWO+3sCnDAV8/NAV8tQIUwae/I6/xiDilW7zf0o= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990/go.mod h1:zswBuTwmEsFHBVRu1nkG3/Fwylk5Vcm8OUm9iWxccSE= penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f h1:Qli89wgu0T7nG4VECXZOZ40fjE/hVVfxF3hTaSYS008= From 456fbd3d2fea68723e2f3edf7c7e79b07a0b35c9 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 1 Jul 2024 16:21:15 +0300 Subject: [PATCH 22/57] guard for dont duplicate tg accounts --- clients/telegram/tg.go | 5 +++-- go.mod | 2 +- go.sum | 6 ++---- service/telegram_svc.go | 27 ++++++++++++++++++++------- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/clients/telegram/tg.go b/clients/telegram/tg.go index 9610751..9773ff8 100644 --- a/clients/telegram/tg.go +++ b/clients/telegram/tg.go @@ -2,12 +2,12 @@ package telegram import ( "context" - "database/sql" "errors" "fmt" "path/filepath" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model" + "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/pj_errors" "penahub.gitlab.yandexcloud.net/backend/tdlib/client" "sync" "time" @@ -36,7 +36,7 @@ func NewTelegramClient(ctx context.Context, repo *dal.DAL) (*TelegramClient, err allTgAccounts, err := repo.TgRepo.GetAllTgAccounts(ctx) if err != nil { - if errors.Is(err, sql.ErrNoRows) { + if errors.Is(err, pj_errors.ErrNotFound) { return tgClient, nil } return nil, err @@ -141,6 +141,7 @@ func (tg *TelegramClient) GetFromMap(id string) (WaitingClient, bool) { tg.mu.Lock() defer tg.mu.Unlock() if data, ok := tg.WaitingClients[id]; ok { + delete(tg.WaitingClients, id) return data, true } return WaitingClient{}, false diff --git a/go.mod b/go.mod index 2ae0f33..37d0420 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,7 @@ require ( google.golang.org/grpc v1.64.0 google.golang.org/protobuf v1.34.2 penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c - penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701123546-6dbbc07f9c24 + penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701131253-42ace400b730 penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615192328-b2f5dffe92ae diff --git a/go.sum b/go.sum index 8edd934..5aec6b6 100644 --- a/go.sum +++ b/go.sum @@ -285,10 +285,8 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c h1:CWb4UcuNXhd1KTNOmy2U0TJO4+Qxgxrj5cwkyFqbgrk= penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c/go.mod h1:+bPxq2wfW5S1gd+83vZYmHm33AE7nEBfznWS8AM1TKE= -penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701091250-bf14e745201b h1:1ALCrJxIatwnqpIUJIx31Cq+rnxFa3WGEA8scV1idVo= -penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701091250-bf14e745201b/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0= -penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701123546-6dbbc07f9c24 h1:hcMtuK4H3dyk2Wo911EXRYcG8GV8hZe+UwuRRGOxF+I= -penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701123546-6dbbc07f9c24/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701131253-42ace400b730 h1:Macb4cgvNb666E5Ckz3M0QQpJNJ1aWY8lHdTSyt8ZcA= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701131253-42ace400b730/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 h1:jiO8GWO+3sCnDAV8/NAV8tQIUwae/I6/xiDilW7zf0o= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990/go.mod h1:zswBuTwmEsFHBVRu1nkG3/Fwylk5Vcm8OUm9iWxccSE= penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f h1:Qli89wgu0T7nG4VECXZOZ40fjE/hVVfxF3hTaSYS008= diff --git a/service/telegram_svc.go b/service/telegram_svc.go index 52df80e..14d25c5 100644 --- a/service/telegram_svc.go +++ b/service/telegram_svc.go @@ -1,11 +1,13 @@ package service import ( + "errors" "fmt" "github.com/gofiber/fiber/v2" "github.com/rs/xid" "path/filepath" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model" + "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/pj_errors" "penahub.gitlab.yandexcloud.net/backend/quiz/core/clients/telegram" "penahub.gitlab.yandexcloud.net/backend/tdlib/client" ) @@ -24,6 +26,17 @@ func (s *Service) AddingTgAccount(ctx *fiber.Ctx) error { if err := ctx.BodyParser(&req); err != nil { return ctx.Status(fiber.StatusBadRequest).SendString("Invalid request data") } + allAccounts, err := s.dal.TgRepo.GetAllTgAccounts(ctx.Context()) + if err != nil && !errors.Is(err, pj_errors.ErrNotFound) { + return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) + } + if !errors.Is(err, pj_errors.ErrNotFound) { + for _, account := range allAccounts { + if account.ApiID == req.ApiID && account.ApiHash == req.ApiHash && account.Status == model.ActiveTg { + return ctx.Status(fiber.StatusConflict).SendString("this account already exist and active") + } + } + } authorizer := client.ClientAuthorizerr() authorizer.TdlibParameters <- &client.SetTdlibParametersRequest{ UseTestDc: false, @@ -41,7 +54,7 @@ func (s *Service) AddingTgAccount(ctx *fiber.Ctx) error { ApplicationVersion: "1.0.0", } - _, err := client.SetLogVerbosityLevel(&client.SetLogVerbosityLevelRequest{ + _, err = client.SetLogVerbosityLevel(&client.SetLogVerbosityLevelRequest{ NewVerbosityLevel: 1, }) if err != nil { @@ -73,13 +86,13 @@ func (s *Service) AddingTgAccount(ctx *fiber.Ctx) error { case client.TypeAuthorizationStateWaitPhoneNumber: authorizer.PhoneNumber <- req.PhoneNumber case client.TypeAuthorizationStateWaitCode: - id := xid.New() + signature := xid.New() s.telegramClient.AddedToMap(telegram.WaitingClient{ TdLibClient: tdlibClient, PreviousReq: req, Authorizer: authorizer, - }, id.String()) - return ctx.Status(fiber.StatusOK).JSON(fiber.Map{"id": id.String()}) + }, signature.String()) + return ctx.Status(fiber.StatusOK).JSON(fiber.Map{"signature": signature.String()}) case client.TypeAuthorizationStateLoggingOut, client.TypeAuthorizationStateClosing, client.TypeAuthorizationStateClosed: return ctx.Status(fiber.StatusForbidden).SendString(fmt.Sprintf("auth failed, last state is %s", state)) @@ -89,14 +102,14 @@ func (s *Service) AddingTgAccount(ctx *fiber.Ctx) error { func (s *Service) SettingTgCode(ctx *fiber.Ctx) error { var req struct { - Code string `json:"code"` - ID string `json:"id"` + Code string `json:"code"` + Signature string `json:"signature"` } if err := ctx.BodyParser(&req); err != nil { return ctx.Status(fiber.StatusBadRequest).SendString("Invalid request data") } - data, ok := s.telegramClient.GetFromMap(req.ID) + data, ok := s.telegramClient.GetFromMap(req.Signature) if !ok { return ctx.Status(fiber.StatusBadRequest).SendString("Invalid id, don't have data") } From ffd9f52bbdad716a72f6adf6f6301826ef2a67d0 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 1 Jul 2024 16:41:00 +0300 Subject: [PATCH 23/57] init methods delete and get all accounts --- service/account_svc.go | 1 - service/telegram_svc.go | 22 ++++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/service/account_svc.go b/service/account_svc.go index 434eba0..8affe78 100644 --- a/service/account_svc.go +++ b/service/account_svc.go @@ -316,7 +316,6 @@ func (s *Service) GetLeadTarget(ctx *fiber.Ctx) error { default: return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) } - } return ctx.Status(fiber.StatusOK).JSON(result) diff --git a/service/telegram_svc.go b/service/telegram_svc.go index 14d25c5..b3cc56b 100644 --- a/service/telegram_svc.go +++ b/service/telegram_svc.go @@ -10,6 +10,7 @@ import ( "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/pj_errors" "penahub.gitlab.yandexcloud.net/backend/quiz/core/clients/telegram" "penahub.gitlab.yandexcloud.net/backend/tdlib/client" + "strconv" ) type Message struct { @@ -18,7 +19,16 @@ type Message struct { } func (s *Service) GetPoolTgAccounts(ctx *fiber.Ctx) error { - return nil + allAccounts, err := s.dal.TgRepo.GetAllTgAccounts(ctx.Context()) + if err != nil { + switch { + case errors.Is(err, pj_errors.ErrNotFound): + return ctx.Status(fiber.StatusNotFound).SendString("not found") + default: + return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) + } + } + return ctx.Status(fiber.StatusOK).JSON(allAccounts) } func (s *Service) AddingTgAccount(ctx *fiber.Ctx) error { @@ -142,5 +152,13 @@ func (s *Service) SettingTgCode(ctx *fiber.Ctx) error { } func (s *Service) DeleteTgAccountByID(ctx *fiber.Ctx) error { - return nil + id, err := strconv.ParseInt(ctx.Params("id"), 10, 64) + if err != nil { + return ctx.Status(fiber.StatusBadRequest).SendString("invalid id format") + } + err = s.dal.TgRepo.SoftDeleteTgAccount(ctx.Context(), id) + if err != nil { + return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) + } + return ctx.SendStatus(fiber.StatusOK) } From 7b2612a8f1477e21e52910281fd3a9b1c811ff9f Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 1 Jul 2024 17:52:14 +0300 Subject: [PATCH 24/57] update openapi --- openapi.yaml | 139 +++++++++++++++++++++++++++++++++++++++- service/telegram_svc.go | 9 ++- 2 files changed, 144 insertions(+), 4 deletions(-) diff --git a/openapi.yaml b/openapi.yaml index 7406f44..1f2e5a3 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -791,9 +791,51 @@ components: type: boolean CreatedAt: type: string - - - + TgAccountStatus: + type: string + enum: + - active + - inactive + - ban + TgAccount: + type: object + properties: + ID: + type: integer + format: int64 + ApiID: + type: integer + format: int32 + ApiHash: + type: string + PhoneNumber: + type: string + Password: + type: string + Status: + $ref: '#/components/schemas/TgAccountStatus' + Deleted: + type: boolean + CreatedAt: + type: string + format: date-time + AuthTgUserReq: + type: object + required: + - ApiID + - ApiHash + - PhoneNumber + - Password + properties: + ApiID: + type: integer + format: int32 + ApiHash: + type: string + PhoneNumber: + type: string + Password: + type: string paths: /liveness: get: @@ -1791,3 +1833,94 @@ paths: description: Bad Request '500': description: Internal Server Error + /telegram/pool: + get: + description: возвращает все неудаленные аккаунты тг, активные, не активные и баны, тело пустое + responses: + '200': + description: успех + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/TgAccount' + /telegram/create: + post: + description: метод для автторизации сервера в тг аккаунте + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/AuthTgUserReq' + responses: + '200': + description: возвращает подпись, которая является идентификатором текущей сессии авторизации нужно для метода отправки кода + content: + application/json: + schema: + type: object + properties: + signature: + type: string + example: b7gh83j2k4l0 + '400': + description: неверные данные запроса + '409': + description: аккаунт уже существует и активен + '500': + description: внутренняя ошибка сервера + /telegram/{id}: + delete: + description: метод мягкого удаления аккаунта по id primary key + parameters: + - in: path + name: id + required: true + description: id primary key + schema: + type: integer + format: int64 + responses: + '200': + description: успех + '400': + description: неверные данные запроса + '500': + description: внутренняя ошибка сервера + + /telegram/setCode: + post: + description: метод для отправки кода авторизации, который пришел от телеграмма + requestBody: + content: + application/json: + schema: + type: object + required: + - code + - signature + properties: + code: + type: string + signature: + type: string + responses: + '200': + description: возвращает id primary авторизованного аккаунта + content: + application/json: + schema: + type: object + properties: + id: + type: integer + format: int64 + '204': + description: state канал закрылся до того как перешел в состояние логина или отказа от логина, возможно стоит другой статус указывать или как то побороть эту беду + '400': + description: неверные данные запроса + '403': + description: что то пошло не так связано с тг + '500': + description: внутренняя ошибка сервера \ No newline at end of file diff --git a/service/telegram_svc.go b/service/telegram_svc.go index b3cc56b..d66beb1 100644 --- a/service/telegram_svc.go +++ b/service/telegram_svc.go @@ -36,6 +36,9 @@ func (s *Service) AddingTgAccount(ctx *fiber.Ctx) error { if err := ctx.BodyParser(&req); err != nil { return ctx.Status(fiber.StatusBadRequest).SendString("Invalid request data") } + if req.ApiID == 0 || req.ApiHash == "" || req.Password == "" || req.PhoneNumber == "" { + return ctx.Status(fiber.StatusBadRequest).SendString("empty required fields") + } allAccounts, err := s.dal.TgRepo.GetAllTgAccounts(ctx.Context()) if err != nil && !errors.Is(err, pj_errors.ErrNotFound) { return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) @@ -119,6 +122,10 @@ func (s *Service) SettingTgCode(ctx *fiber.Ctx) error { return ctx.Status(fiber.StatusBadRequest).SendString("Invalid request data") } + if req.Code == "" || req.Signature == "" { + return ctx.Status(fiber.StatusBadRequest).SendString("empty required fields") + } + data, ok := s.telegramClient.GetFromMap(req.Signature) if !ok { return ctx.Status(fiber.StatusBadRequest).SendString("Invalid id, don't have data") @@ -127,7 +134,7 @@ func (s *Service) SettingTgCode(ctx *fiber.Ctx) error { for { state, ok := <-data.Authorizer.State if !ok { - return ctx.Status(fiber.StatusOK).SendString("state chan is close auth maybe ok") + return ctx.Status(fiber.StatusNoContent).SendString("state chan is close auth maybe ok") } fmt.Println("currnet state:", state) switch state.AuthorizationStateType() { From 742e96528930aa39cf28402bf09271a651b71cb2 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 2 Jul 2024 14:29:36 +0300 Subject: [PATCH 25/57] added method for create chanel and return invite link, but without bot and destroing yourself --- clients/telegram/tg.go | 80 ++++++++++++++++++++++++++++++++++++++++- service/service.go | 1 + service/telegram_svc.go | 11 ++++++ 3 files changed, 91 insertions(+), 1 deletion(-) diff --git a/clients/telegram/tg.go b/clients/telegram/tg.go index 9773ff8..4602ab5 100644 --- a/clients/telegram/tg.go +++ b/clients/telegram/tg.go @@ -159,5 +159,83 @@ func (tg *TelegramClient) SaveTgAccount(ctx context.Context, tdLibClient *client } func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string, error) { - return "", nil + tg.mu.Lock() + defer tg.mu.Unlock() + + var activeClient *client.Client + for _, c := range tg.TgClients { + activeClient = c + break + } + + if activeClient == nil { + return "", errors.New("no active Telegram clients") + } + + channel, err := activeClient.CreateNewSupergroupChat(&client.CreateNewSupergroupChatRequest{ + Title: channelName, + IsChannel: true, + Description: "private channel", + }) + if err != nil { + return "", fmt.Errorf("failed to create channel: %s", err.Error()) + } + + //resp, err := activeClient.AddChatMember(&client.AddChatMemberRequest{ + // ChatId: channel.Id, + // UserId: botID, + //}) + //if err != nil { + // return "", fmt.Errorf("failed to add bot to channel: %s", err.Error()) + //} + // + //fmt.Println("resp", resp) + // + //_, err = activeClient.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ + // ChatId: channel.Id, + // MemberId: &client.MessageSenderUser{UserId: botID}, + // Status: &client.ChatMemberStatusAdministrator{ + // CanBeEdited: true, + // Rights: &client.ChatAdministratorRights{ + // CanManageChat: false, + // CanChangeInfo: true, + // CanPostMessages: true, + // CanEditMessages: true, + // CanDeleteMessages: true, + // CanInviteUsers: true, + // CanRestrictMembers: true, + // CanPinMessages: true, + // CanManageTopics: true, + // CanPromoteMembers: true, + // CanManageVideoChats: true, + // CanPostStories: true, + // CanEditStories: true, + // CanDeleteStories: true, + // IsAnonymous: true, + // }, + // }, + //}) + //if err != nil { + // return "", fmt.Errorf("failed to make bot admin: %s", err.Error()) + //} + + //_, err = activeClient.LeaveChat(&client.LeaveChatRequest{ + // ChatId: channel.Id, + //}) + //if err != nil { + // return "", fmt.Errorf("failed to leave the channel: %s", err.Error()) + //} + + inviteLink, err := activeClient.CreateChatInviteLink(&client.CreateChatInviteLinkRequest{ + ChatId: channel.Id, + Name: channelName, + ExpirationDate: 0, + MemberLimit: 0, + CreatesJoinRequest: false, + }) + if err != nil { + return "", fmt.Errorf("failed to get invite link: %s", err.Error()) + } + + return inviteLink.InviteLink, nil } diff --git a/service/service.go b/service/service.go index 983e9c9..a948ec5 100644 --- a/service/service.go +++ b/service/service.go @@ -91,4 +91,5 @@ func (s *Service) Register(app *fiber.App) { app.Post("/telegram/create", s.AddingTgAccount) app.Delete("/telegram/:id", s.DeleteTgAccountByID) app.Post("/telegram/setCode", s.SettingTgCode) + //app.Get("/test/test", s.TEST) } diff --git a/service/telegram_svc.go b/service/telegram_svc.go index d66beb1..0dfad04 100644 --- a/service/telegram_svc.go +++ b/service/telegram_svc.go @@ -169,3 +169,14 @@ func (s *Service) DeleteTgAccountByID(ctx *fiber.Ctx) error { } return ctx.SendStatus(fiber.StatusOK) } + +//func (s *Service) TEST(ctx *fiber.Ctx) error { +// botID := 6712573453 +// chanalName := "testAAAAAA" +// link, err := s.telegramClient.CreateChannel(chanalName, int64(botID)) +// if err != nil { +// return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) +// } +// +// return ctx.Status(fiber.StatusOK).SendString(link) +//} From 2d1456d40be4045a974be46ef73ad1133a4d2d3e Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 3 Jul 2024 00:04:07 +0300 Subject: [PATCH 26/57] refactor nil client added to map after auth --- clients/telegram/tg.go | 75 +++++++++++++++++++---------------------- go.mod | 5 ++- go.sum | 8 +++++ service/telegram_svc.go | 14 ++++---- 4 files changed, 53 insertions(+), 49 deletions(-) diff --git a/clients/telegram/tg.go b/clients/telegram/tg.go index 4602ab5..bb40f89 100644 --- a/clients/telegram/tg.go +++ b/clients/telegram/tg.go @@ -21,12 +21,10 @@ type TelegramClient struct { } type WaitingClient struct { - TdLibClient *client.Client PreviousReq AuthTgUserReq Authorizer *client.ClientAuthorizer } -// todo come back saved tg accs to slice for check this status func NewTelegramClient(ctx context.Context, repo *dal.DAL) (*TelegramClient, error) { tgClient := &TelegramClient{ repo: repo, @@ -49,10 +47,10 @@ func NewTelegramClient(ctx context.Context, repo *dal.DAL) (*TelegramClient, err UseTestDc: false, DatabaseDirectory: filepath.Join(".tdlib", "database"), FilesDirectory: filepath.Join(".tdlib", "files"), - UseFileDatabase: false, - UseChatInfoDatabase: false, - UseMessageDatabase: false, - UseSecretChats: false, + UseFileDatabase: true, + UseChatInfoDatabase: true, + UseMessageDatabase: true, + UseSecretChats: true, ApiId: account.ApiID, ApiHash: account.ApiHash, SystemLanguageCode: "en", @@ -147,15 +145,17 @@ func (tg *TelegramClient) GetFromMap(id string) (WaitingClient, bool) { return WaitingClient{}, false } -func (tg *TelegramClient) SaveTgAccount(ctx context.Context, tdLibClient *client.Client, account model.TgAccount) (int64, error) { - tg.mu.Lock() - defer tg.mu.Unlock() - id, err := tg.repo.TgRepo.CreateTgAccount(ctx, account) +func (tg *TelegramClient) SaveTgAccount(appID int32, appHash string, tdLibClient *client.Client) { + account, err := tg.repo.TgRepo.SearchIDByAppIDanAppHash(context.Background(), appID, appHash) if err != nil { - return 0, err + fmt.Println("err SaveTgAccount", err) + return + } + if account.Status == model.ActiveTg { + tg.mu.Lock() + defer tg.mu.Unlock() + tg.TgClients[account.ID] = tdLibClient } - tg.TgClients[id] = tdLibClient - return id, nil } func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string, error) { @@ -190,34 +190,25 @@ func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string //} // //fmt.Println("resp", resp) - // - //_, err = activeClient.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ - // ChatId: channel.Id, - // MemberId: &client.MessageSenderUser{UserId: botID}, - // Status: &client.ChatMemberStatusAdministrator{ - // CanBeEdited: true, - // Rights: &client.ChatAdministratorRights{ - // CanManageChat: false, - // CanChangeInfo: true, - // CanPostMessages: true, - // CanEditMessages: true, - // CanDeleteMessages: true, - // CanInviteUsers: true, - // CanRestrictMembers: true, - // CanPinMessages: true, - // CanManageTopics: true, - // CanPromoteMembers: true, - // CanManageVideoChats: true, - // CanPostStories: true, - // CanEditStories: true, - // CanDeleteStories: true, - // IsAnonymous: true, - // }, - // }, - //}) - //if err != nil { - // return "", fmt.Errorf("failed to make bot admin: %s", err.Error()) - //} + + _, err = activeClient.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ + ChatId: channel.Id, + MemberId: &client.MessageSenderUser{UserId: botID}, + Status: &client.ChatMemberStatusAdministrator{ + CanBeEdited: true, + Rights: &client.ChatAdministratorRights{ + CanManageChat: true, + CanChangeInfo: true, + CanPostMessages: true, + CanInviteUsers: true, + CanRestrictMembers: true, + CanPromoteMembers: true, + }, + }, + }) + if err != nil { + return "", fmt.Errorf("failed to make bot admin: %s", err.Error()) + } //_, err = activeClient.LeaveChat(&client.LeaveChatRequest{ // ChatId: channel.Id, @@ -226,6 +217,8 @@ func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string // return "", fmt.Errorf("failed to leave the channel: %s", err.Error()) //} + fmt.Println("channel.Id", channel.Id) + inviteLink, err := activeClient.CreateChatInviteLink(&client.CreateChatInviteLinkRequest{ ChatId: channel.Id, Name: channelName, diff --git a/go.mod b/go.mod index 37d0420..7cb6321 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,7 @@ require ( google.golang.org/grpc v1.64.0 google.golang.org/protobuf v1.34.2 penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c - penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701131253-42ace400b730 + penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240702152013-c2a0ba2ac307 penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615192328-b2f5dffe92ae @@ -34,6 +34,8 @@ require ( github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/go-redis/redis/v8 v8.11.5 // indirect + github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible // indirect + github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect github.com/goccy/go-json v0.10.2 // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/google/uuid v1.6.0 // indirect @@ -52,6 +54,7 @@ require ( github.com/richardlehane/msoleps v1.0.3 // indirect github.com/rivo/uniseg v0.4.7 // indirect github.com/tealeg/xlsx v1.0.5 // indirect + github.com/technoweenie/multipartstreamer v1.0.1 // indirect github.com/twmb/franz-go/pkg/kmsg v1.8.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasthttp v1.53.0 // indirect diff --git a/go.sum b/go.sum index 5aec6b6..6e2d0ab 100644 --- a/go.sum +++ b/go.sum @@ -36,6 +36,10 @@ github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo= github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaELYAEARXRkq2LrJ0yDDv1rW7+wrTEdVL3uaU= +github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM= +github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc= +github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/gofiber/fiber/v2 v2.52.4 h1:P+T+4iK7VaqUsq2PALYEfBBo6bJZ4q3FP8cZ84EggTM= @@ -142,6 +146,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/tealeg/xlsx v1.0.5 h1:+f8oFmvY8Gw1iUXzPk+kz+4GpbDZPK1FhPiQRd+ypgE= github.com/tealeg/xlsx v1.0.5/go.mod h1:btRS8dz54TDnvKNosuAqxrM1QgN1udgk9O34bDCnORM= +github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM= +github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog= github.com/themakers/bdd v0.0.0-20210316111417-6b1dfe326f33 h1:N9f/Q+2Ssa+yDcbfaoLTYvXmdeyUUxsJKdPUVsjSmiA= github.com/themakers/bdd v0.0.0-20210316111417-6b1dfe326f33/go.mod h1:rpcH99JknBh8seZmlOlUg51gasZH6QH34oXNsIwYT6E= github.com/themakers/hlog v0.0.0-20191205140925-235e0e4baddf h1:TJJm6KcBssmbWzplF5lzixXl1RBAi/ViPs1GaSOkhwo= @@ -287,6 +293,8 @@ penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5 penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c/go.mod h1:+bPxq2wfW5S1gd+83vZYmHm33AE7nEBfznWS8AM1TKE= penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701131253-42ace400b730 h1:Macb4cgvNb666E5Ckz3M0QQpJNJ1aWY8lHdTSyt8ZcA= penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701131253-42ace400b730/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240702152013-c2a0ba2ac307 h1:wCSZWRvg3q1AboSM8TC35429GMYna4U9sKbw9YEJHr0= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240702152013-c2a0ba2ac307/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 h1:jiO8GWO+3sCnDAV8/NAV8tQIUwae/I6/xiDilW7zf0o= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990/go.mod h1:zswBuTwmEsFHBVRu1nkG3/Fwylk5Vcm8OUm9iWxccSE= penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f h1:Qli89wgu0T7nG4VECXZOZ40fjE/hVVfxF3hTaSYS008= diff --git a/service/telegram_svc.go b/service/telegram_svc.go index 0dfad04..ed66035 100644 --- a/service/telegram_svc.go +++ b/service/telegram_svc.go @@ -55,10 +55,10 @@ func (s *Service) AddingTgAccount(ctx *fiber.Ctx) error { UseTestDc: false, DatabaseDirectory: filepath.Join(".tdlib", "database"), FilesDirectory: filepath.Join(".tdlib", "files"), - UseFileDatabase: false, - UseChatInfoDatabase: false, - UseMessageDatabase: false, - UseSecretChats: false, + UseFileDatabase: true, + UseChatInfoDatabase: true, + UseMessageDatabase: true, + UseSecretChats: true, ApiId: req.ApiID, ApiHash: req.ApiHash, SystemLanguageCode: "en", @@ -83,6 +83,7 @@ func (s *Service) AddingTgAccount(ctx *fiber.Ctx) error { fmt.Println("new client failed", err) return } + s.telegramClient.SaveTgAccount(req.ApiID, req.ApiHash, tdlibClient) fmt.Println("i am down") }() if goErr != nil { @@ -101,7 +102,6 @@ func (s *Service) AddingTgAccount(ctx *fiber.Ctx) error { case client.TypeAuthorizationStateWaitCode: signature := xid.New() s.telegramClient.AddedToMap(telegram.WaitingClient{ - TdLibClient: tdlibClient, PreviousReq: req, Authorizer: authorizer, }, signature.String()) @@ -139,7 +139,7 @@ func (s *Service) SettingTgCode(ctx *fiber.Ctx) error { fmt.Println("currnet state:", state) switch state.AuthorizationStateType() { case client.TypeAuthorizationStateReady: - id, err := s.telegramClient.SaveTgAccount(ctx.Context(), data.TdLibClient, model.TgAccount{ + id, err := s.dal.TgRepo.CreateTgAccount(ctx.Context(), model.TgAccount{ ApiID: data.PreviousReq.ApiID, ApiHash: data.PreviousReq.ApiHash, PhoneNumber: data.PreviousReq.PhoneNumber, @@ -171,7 +171,7 @@ func (s *Service) DeleteTgAccountByID(ctx *fiber.Ctx) error { } //func (s *Service) TEST(ctx *fiber.Ctx) error { -// botID := 6712573453 +// botID := 542073142 // chanalName := "testAAAAAA" // link, err := s.telegramClient.CreateChannel(chanalName, int64(botID)) // if err != nil { From f3414541c64c5a94c0e579f4607b8dfdf6f7de21 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 3 Jul 2024 14:04:29 +0300 Subject: [PATCH 27/57] create channel, adding bot to channel, get invite link and leave owner tg channel success --- clients/telegram/tg.go | 35 +++++++++++++++-------------------- service/telegram_svc.go | 2 +- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/clients/telegram/tg.go b/clients/telegram/tg.go index bb40f89..63d0991 100644 --- a/clients/telegram/tg.go +++ b/clients/telegram/tg.go @@ -172,6 +172,13 @@ func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string return "", errors.New("no active Telegram clients") } + _, err := activeClient.GetUser(&client.GetUserRequest{ + UserId: botID, + }) + if err != nil { + return "", errors.New("not found this bot, make privacy off") + } + channel, err := activeClient.CreateNewSupergroupChat(&client.CreateNewSupergroupChatRequest{ Title: channelName, IsChannel: true, @@ -181,21 +188,11 @@ func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string return "", fmt.Errorf("failed to create channel: %s", err.Error()) } - //resp, err := activeClient.AddChatMember(&client.AddChatMemberRequest{ - // ChatId: channel.Id, - // UserId: botID, - //}) - //if err != nil { - // return "", fmt.Errorf("failed to add bot to channel: %s", err.Error()) - //} - // - //fmt.Println("resp", resp) - _, err = activeClient.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ ChatId: channel.Id, MemberId: &client.MessageSenderUser{UserId: botID}, Status: &client.ChatMemberStatusAdministrator{ - CanBeEdited: true, + CustomTitle: "bot", Rights: &client.ChatAdministratorRights{ CanManageChat: true, CanChangeInfo: true, @@ -210,15 +207,6 @@ func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string return "", fmt.Errorf("failed to make bot admin: %s", err.Error()) } - //_, err = activeClient.LeaveChat(&client.LeaveChatRequest{ - // ChatId: channel.Id, - //}) - //if err != nil { - // return "", fmt.Errorf("failed to leave the channel: %s", err.Error()) - //} - - fmt.Println("channel.Id", channel.Id) - inviteLink, err := activeClient.CreateChatInviteLink(&client.CreateChatInviteLinkRequest{ ChatId: channel.Id, Name: channelName, @@ -230,5 +218,12 @@ func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string return "", fmt.Errorf("failed to get invite link: %s", err.Error()) } + _, err = activeClient.LeaveChat(&client.LeaveChatRequest{ + ChatId: channel.Id, + }) + if err != nil { + return "", fmt.Errorf("failed to leave the channel: %s", err.Error()) + } + return inviteLink.InviteLink, nil } diff --git a/service/telegram_svc.go b/service/telegram_svc.go index ed66035..fb41fee 100644 --- a/service/telegram_svc.go +++ b/service/telegram_svc.go @@ -171,7 +171,7 @@ func (s *Service) DeleteTgAccountByID(ctx *fiber.Ctx) error { } //func (s *Service) TEST(ctx *fiber.Ctx) error { -// botID := 542073142 +// botID := 6712573453 // chanalName := "testAAAAAA" // link, err := s.telegramClient.CreateChannel(chanalName, int64(botID)) // if err != nil { From 9b178d2eb9a398952fec343c8432aa17d1a5321f Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 3 Jul 2024 18:21:35 +0300 Subject: [PATCH 28/57] add todo for future --- clients/telegram/tg.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/clients/telegram/tg.go b/clients/telegram/tg.go index 63d0991..05ecad4 100644 --- a/clients/telegram/tg.go +++ b/clients/telegram/tg.go @@ -179,6 +179,8 @@ func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string return "", errors.New("not found this bot, make privacy off") } + // todo нужно поймать ошибку, при которой либо бан либо медленный редим включается для того чтобы прервать + // исполнение клиента текущего аккаунта и дать задачу следующему пока поймал 1 раз и не запомнил больше не получается channel, err := activeClient.CreateNewSupergroupChat(&client.CreateNewSupergroupChatRequest{ Title: channelName, IsChannel: true, From f1cbc8bfa23d59e468afcfff62e4bd9c9699181f Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 5 Jul 2024 10:49:42 +0300 Subject: [PATCH 29/57] update logic in create tg channel --- clients/telegram/tg.go | 130 +++++++++++++++++++++-------------------- 1 file changed, 68 insertions(+), 62 deletions(-) diff --git a/clients/telegram/tg.go b/clients/telegram/tg.go index 05ecad4..2e28571 100644 --- a/clients/telegram/tg.go +++ b/clients/telegram/tg.go @@ -158,74 +158,80 @@ func (tg *TelegramClient) SaveTgAccount(appID int32, appHash string, tdLibClient } } -func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string, error) { +func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string, int64, error) { tg.mu.Lock() defer tg.mu.Unlock() - - var activeClient *client.Client - for _, c := range tg.TgClients { - activeClient = c - break + if len(tg.TgClients) == 0 { + return "", 0, errors.New("no active Telegram clients") } + var lastError error + var inviteLink string + var channelId int64 + for _, activeClient := range tg.TgClients { + _, err := activeClient.GetUser(&client.GetUserRequest{ + UserId: botID, + }) + if err != nil { + lastError = fmt.Errorf("not found this bot, make privacy off: %v", err) + continue + } - if activeClient == nil { - return "", errors.New("no active Telegram clients") - } + // todo нужно поймать ошибку, при которой либо бан либо медленный редим включается для того чтобы прервать + // исполнение клиента текущего аккаунта и дать задачу следующему пока поймал 1 раз и не запомнил больше не получается + channel, err := activeClient.CreateNewSupergroupChat(&client.CreateNewSupergroupChatRequest{ + Title: channelName, + IsChannel: true, + Description: "private channel", + }) + if err != nil { + lastError = fmt.Errorf("failed to create channel: %s", err.Error()) + continue + } - _, err := activeClient.GetUser(&client.GetUserRequest{ - UserId: botID, - }) - if err != nil { - return "", errors.New("not found this bot, make privacy off") - } - - // todo нужно поймать ошибку, при которой либо бан либо медленный редим включается для того чтобы прервать - // исполнение клиента текущего аккаунта и дать задачу следующему пока поймал 1 раз и не запомнил больше не получается - channel, err := activeClient.CreateNewSupergroupChat(&client.CreateNewSupergroupChatRequest{ - Title: channelName, - IsChannel: true, - Description: "private channel", - }) - if err != nil { - return "", fmt.Errorf("failed to create channel: %s", err.Error()) - } - - _, err = activeClient.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ - ChatId: channel.Id, - MemberId: &client.MessageSenderUser{UserId: botID}, - Status: &client.ChatMemberStatusAdministrator{ - CustomTitle: "bot", - Rights: &client.ChatAdministratorRights{ - CanManageChat: true, - CanChangeInfo: true, - CanPostMessages: true, - CanInviteUsers: true, - CanRestrictMembers: true, - CanPromoteMembers: true, + _, err = activeClient.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ + ChatId: channel.Id, + MemberId: &client.MessageSenderUser{UserId: botID}, + Status: &client.ChatMemberStatusAdministrator{ + CustomTitle: "bot", + Rights: &client.ChatAdministratorRights{ + CanManageChat: true, + CanChangeInfo: true, + CanPostMessages: true, + CanInviteUsers: true, + CanRestrictMembers: true, + CanPromoteMembers: true, + }, }, - }, - }) - if err != nil { - return "", fmt.Errorf("failed to make bot admin: %s", err.Error()) + }) + if err != nil { + lastError = fmt.Errorf("failed to make bot admin: %s", err.Error()) + continue + } + + inviteLinkResp, err := activeClient.CreateChatInviteLink(&client.CreateChatInviteLinkRequest{ + ChatId: channel.Id, + Name: channelName, + ExpirationDate: 0, + MemberLimit: 0, + CreatesJoinRequest: false, + }) + if err != nil { + lastError = fmt.Errorf("failed to get invite link: %s", err.Error()) + continue + } + + _, err = activeClient.LeaveChat(&client.LeaveChatRequest{ + ChatId: channel.Id, + }) + if err != nil { + lastError = fmt.Errorf("failed to leave the channel: %s", err.Error()) + continue + } + + inviteLink = inviteLinkResp.InviteLink + channelId = channel.Id + return inviteLink, channelId, nil } - inviteLink, err := activeClient.CreateChatInviteLink(&client.CreateChatInviteLinkRequest{ - ChatId: channel.Id, - Name: channelName, - ExpirationDate: 0, - MemberLimit: 0, - CreatesJoinRequest: false, - }) - if err != nil { - return "", fmt.Errorf("failed to get invite link: %s", err.Error()) - } - - _, err = activeClient.LeaveChat(&client.LeaveChatRequest{ - ChatId: channel.Id, - }) - if err != nil { - return "", fmt.Errorf("failed to leave the channel: %s", err.Error()) - } - - return inviteLink.InviteLink, nil + return "", 0, lastError } From e176313cee61898af8ff13445589927d04537fb2 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 7 Jul 2024 16:02:47 +0300 Subject: [PATCH 30/57] added todo for safe stable --- service/telegram_svc.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/service/telegram_svc.go b/service/telegram_svc.go index fb41fee..01c8c20 100644 --- a/service/telegram_svc.go +++ b/service/telegram_svc.go @@ -77,6 +77,7 @@ func (s *Service) AddingTgAccount(ctx *fiber.Ctx) error { var tdlibClient *client.Client // завершается уже в другом контроллере var goErr error + // todo ужно продумать завершение горутины если код вставлять не пошли go func() { tdlibClient, goErr = client.NewClient(authorizer) if goErr != nil { @@ -173,10 +174,11 @@ func (s *Service) DeleteTgAccountByID(ctx *fiber.Ctx) error { //func (s *Service) TEST(ctx *fiber.Ctx) error { // botID := 6712573453 // chanalName := "testAAAAAA" -// link, err := s.telegramClient.CreateChannel(chanalName, int64(botID)) +// link, chatID, err := s.telegramClient.CreateChannel(chanalName, int64(botID)) // if err != nil { // return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) // } +// fmt.Println(chatID) // // return ctx.Status(fiber.StatusOK).SendString(link) //} From 04665af34a5a5f689b2afa4e5ada8c91f80204be Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 8 Jul 2024 13:46:12 +0300 Subject: [PATCH 31/57] added bot listener need testing now for safety added --- app/app.go | 18 ++++++--- clients/telegram/tg.go | 5 ++- go.mod | 8 ++-- go.sum | 20 ++-------- worker/tg_listener.go | 85 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 107 insertions(+), 29 deletions(-) create mode 100644 worker/tg_listener.go diff --git a/app/app.go b/app/app.go index 6ab44cf..3263950 100644 --- a/app/app.go +++ b/app/app.go @@ -9,11 +9,9 @@ import ( "github.com/themakers/hlog" "go.uber.org/zap" "go.uber.org/zap/zapcore" - "penahub.gitlab.yandexcloud.net/backend/penahub_common/log_mw" "penahub.gitlab.yandexcloud.net/backend/penahub_common/privilege" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/healthchecks" - "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/middleware" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model" "penahub.gitlab.yandexcloud.net/backend/quiz/core/brokers" "penahub.gitlab.yandexcloud.net/backend/quiz/core/clients/auth" @@ -23,7 +21,8 @@ import ( "penahub.gitlab.yandexcloud.net/backend/quiz/core/server" "penahub.gitlab.yandexcloud.net/backend/quiz/core/service" "penahub.gitlab.yandexcloud.net/backend/quiz/core/tools" - "penahub.gitlab.yandexcloud.net/external/trashlog.git/wrappers/zaptrashlog" + "penahub.gitlab.yandexcloud.net/backend/quiz/core/worker" + "penahub.gitlab.yandexcloud.net/external/trashlog/wrappers/zaptrashlog" "time" ) @@ -70,6 +69,7 @@ type Options struct { TrashLogHost string `env:"TRASH_LOG_HOST" default:"localhost:7113"` ModuleLogger string `env:"MODULE_LOGGER" default:"core-local"` ClickHouseCred string `env:"CLICK_HOUSE_CRED" default:"tcp://10.8.0.15:9000/default?sslmode=disable"` + TgBotToken string `env:"TG_BOT_TOKEN" default:"6712573453:AAFqTOsgwe_j48ZQ1GzWKQDT5Nwr-SAWjz8"` } func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.CommonApp, error) { @@ -153,11 +153,17 @@ func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.Co privilegeController := privilege.NewPrivilege(clientData, fiberClient) go tools.PublishPrivilege(privilegeController, 10, 5*time.Minute) - tgClient, err := telegram.NewTelegramClient(ctx, pgdal) + chatIDsChan := make(chan int64, 100) + tgClient, err := telegram.NewTelegramClient(ctx, pgdal, chatIDsChan) if err != nil { panic(fmt.Sprintf("failed init tg clietns: %v", err)) } + _, err = worker.NewTelegramBotWorker(options.TgBotToken, chatIDsChan) + if err != nil { + panic(fmt.Sprintf("failed init tg listener WC: %v", err)) + } + // todo подумать над реализацией всего а то пока мне кажется что немного каша получается такой предикт что через некоторое время // сложно будет разобраться что есть где grpcControllers := initialize.InitRpcControllers(pgdal) @@ -173,8 +179,8 @@ func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.Co }) app := fiber.New() - app.Use(middleware.JWTAuth()) - app.Use(log_mw.ContextLogger(loggerHlog)) + //app.Use(middleware.JWTAuth()) + //app.Use(log_mw.ContextLogger(loggerHlog)) app.Get("/liveness", healthchecks.Liveness) app.Get("/readiness", healthchecks.Readiness(&workerErr)) //todo parametrized readiness. should discuss ready reason diff --git a/clients/telegram/tg.go b/clients/telegram/tg.go index 2e28571..9ac09db 100644 --- a/clients/telegram/tg.go +++ b/clients/telegram/tg.go @@ -18,6 +18,7 @@ type TelegramClient struct { TgClients map[int64]*client.Client WaitingClients map[string]WaitingClient mu sync.Mutex + chatIDsChan chan int64 } type WaitingClient struct { @@ -25,11 +26,12 @@ type WaitingClient struct { Authorizer *client.ClientAuthorizer } -func NewTelegramClient(ctx context.Context, repo *dal.DAL) (*TelegramClient, error) { +func NewTelegramClient(ctx context.Context, repo *dal.DAL, chatIDsChan chan int64) (*TelegramClient, error) { tgClient := &TelegramClient{ repo: repo, TgClients: make(map[int64]*client.Client), WaitingClients: make(map[string]WaitingClient), + chatIDsChan: chatIDsChan, } allTgAccounts, err := repo.TgRepo.GetAllTgAccounts(ctx) @@ -230,6 +232,7 @@ func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string inviteLink = inviteLinkResp.InviteLink channelId = channel.Id + tg.chatIDsChan <- channelId return inviteLink, channelId, nil } diff --git a/go.mod b/go.mod index 7cb6321..b9a6590 100644 --- a/go.mod +++ b/go.mod @@ -18,11 +18,11 @@ require ( go.uber.org/zap v1.27.0 google.golang.org/grpc v1.64.0 google.golang.org/protobuf v1.34.2 + gopkg.in/tucnak/telebot.v2 v2.5.0 penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240702152013-c2a0ba2ac307 penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f - penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615192328-b2f5dffe92ae ) require ( @@ -34,8 +34,6 @@ require ( github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/go-redis/redis/v8 v8.11.5 // indirect - github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible // indirect - github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect github.com/goccy/go-json v0.10.2 // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/google/uuid v1.6.0 // indirect @@ -49,12 +47,11 @@ require ( github.com/minio/minio-go/v7 v7.0.70 // indirect github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect github.com/pierrec/lz4/v4 v4.1.21 // indirect + github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/richardlehane/mscfb v1.0.4 // indirect github.com/richardlehane/msoleps v1.0.3 // indirect github.com/rivo/uniseg v0.4.7 // indirect - github.com/tealeg/xlsx v1.0.5 // indirect - github.com/technoweenie/multipartstreamer v1.0.1 // indirect github.com/twmb/franz-go/pkg/kmsg v1.8.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasthttp v1.53.0 // indirect @@ -71,4 +68,5 @@ require ( gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect + penahub.gitlab.yandexcloud.net/external/trashlog v0.1.2-0.20240704185641-304b297f19d8 // indirect ) diff --git a/go.sum b/go.sum index 6e2d0ab..c8551ca 100644 --- a/go.sum +++ b/go.sum @@ -26,8 +26,6 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= -github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg= -github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= @@ -36,10 +34,6 @@ github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo= github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= -github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaELYAEARXRkq2LrJ0yDDv1rW7+wrTEdVL3uaU= -github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM= -github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc= -github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/gofiber/fiber/v2 v2.52.4 h1:P+T+4iK7VaqUsq2PALYEfBBo6bJZ4q3FP8cZ84EggTM= @@ -140,16 +134,11 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -github.com/tealeg/xlsx v1.0.5 h1:+f8oFmvY8Gw1iUXzPk+kz+4GpbDZPK1FhPiQRd+ypgE= -github.com/tealeg/xlsx v1.0.5/go.mod h1:btRS8dz54TDnvKNosuAqxrM1QgN1udgk9O34bDCnORM= -github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM= -github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog= -github.com/themakers/bdd v0.0.0-20210316111417-6b1dfe326f33 h1:N9f/Q+2Ssa+yDcbfaoLTYvXmdeyUUxsJKdPUVsjSmiA= -github.com/themakers/bdd v0.0.0-20210316111417-6b1dfe326f33/go.mod h1:rpcH99JknBh8seZmlOlUg51gasZH6QH34oXNsIwYT6E= github.com/themakers/hlog v0.0.0-20191205140925-235e0e4baddf h1:TJJm6KcBssmbWzplF5lzixXl1RBAi/ViPs1GaSOkhwo= github.com/themakers/hlog v0.0.0-20191205140925-235e0e4baddf/go.mod h1:1FsorU3vnXO9xS9SrhUp8fRb/6H/Zfll0rPt1i4GWaA= github.com/twmb/franz-go v1.16.1 h1:rpWc7fB9jd7TgmCyfxzenBI+QbgS8ZfJOUQE+tzPtbE= @@ -268,7 +257,6 @@ google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6h google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= @@ -291,13 +279,11 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c h1:CWb4UcuNXhd1KTNOmy2U0TJO4+Qxgxrj5cwkyFqbgrk= penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c/go.mod h1:+bPxq2wfW5S1gd+83vZYmHm33AE7nEBfznWS8AM1TKE= -penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701131253-42ace400b730 h1:Macb4cgvNb666E5Ckz3M0QQpJNJ1aWY8lHdTSyt8ZcA= -penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701131253-42ace400b730/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0= penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240702152013-c2a0ba2ac307 h1:wCSZWRvg3q1AboSM8TC35429GMYna4U9sKbw9YEJHr0= penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240702152013-c2a0ba2ac307/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 h1:jiO8GWO+3sCnDAV8/NAV8tQIUwae/I6/xiDilW7zf0o= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990/go.mod h1:zswBuTwmEsFHBVRu1nkG3/Fwylk5Vcm8OUm9iWxccSE= penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f h1:Qli89wgu0T7nG4VECXZOZ40fjE/hVVfxF3hTaSYS008= penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f/go.mod h1:AkE19hcbDwB7hoEASwImm7rUI+cK/8jMVJaTvMK4F+c= -penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615192328-b2f5dffe92ae h1:vlGInCsQSDA464q7OMv3EWGUviQWQdEcpLc8HIRo+rE= -penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615192328-b2f5dffe92ae/go.mod h1:3ml0dAGT8U8RhpevKBfRgG6yKZum8EI2uJxAb2WCIy4= +penahub.gitlab.yandexcloud.net/external/trashlog v0.1.2-0.20240704185641-304b297f19d8 h1:9RiVgJk+/0vNvzObrpM3Cjga0zz6PtNsJXj1babTk4Q= +penahub.gitlab.yandexcloud.net/external/trashlog v0.1.2-0.20240704185641-304b297f19d8/go.mod h1:6NMjnrYqjRc2YVzQxNvs5Q2BfQR6x40bb3oUVdV61XI= diff --git a/worker/tg_listener.go b/worker/tg_listener.go new file mode 100644 index 0000000..7b07efe --- /dev/null +++ b/worker/tg_listener.go @@ -0,0 +1,85 @@ +package worker + +import ( + "fmt" + "gopkg.in/tucnak/telebot.v2" + "time" +) + +type TelegramBotWorker struct { + bot *telebot.Bot + channelIDs chan int64 +} + +func NewTelegramBotWorker(botToken string, channelIDs chan int64) (*TelegramBotWorker, error) { + botSettings := telebot.Settings{ + Token: botToken, + Poller: &telebot.LongPoller{Timeout: 10 * time.Second}, + } + + bot, err := telebot.NewBot(botSettings) + if err != nil { + return nil, err + } + + worker := &TelegramBotWorker{ + bot: bot, + channelIDs: channelIDs, + } + + return worker, nil +} + +func (w *TelegramBotWorker) Run() { + w.bot.Handle(telebot.OnUserJoined, func(message telebot.Message) { + w.handleNewChatMembers(&message) + }) + + w.bot.Start() +} + +func (w *TelegramBotWorker) handleNewChatMembers(message *telebot.Message) { + chatID := message.Chat.ID + var found bool + + for { + select { + case channelID := <-w.channelIDs: + if chatID == channelID { + found = true + break + } + default: + break + } + + if found { + break + } + } + + if found { + for _, newMember := range message.UsersJoined { + adminRights := telebot.ChatMember{ + User: &telebot.User{ID: newMember.ID}, + Role: telebot.Administrator, + Rights: telebot.Rights{ + CanManageChat: true, + CanChangeInfo: true, + CanPostMessages: true, + CanInviteUsers: true, + CanRestrictMembers: true, + CanPromoteMembers: true, + }, + } + + err := w.bot.Promote(message.Chat, &adminRights) + if err != nil { + fmt.Printf("error to promote user %d in chat %d: %v", newMember.ID, chatID, err) + } else { + fmt.Printf("user %d has been promoted to admin in chat %d", newMember.ID, chatID) + } + break + } + } +} From 7e2b4b265656953a7e3b5dbdbfa1dac72e270ce2 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 8 Jul 2024 13:47:16 +0300 Subject: [PATCH 32/57] added bot listener need testing now for safety added --- app/app.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/app.go b/app/app.go index 3263950..e603e51 100644 --- a/app/app.go +++ b/app/app.go @@ -9,9 +9,11 @@ import ( "github.com/themakers/hlog" "go.uber.org/zap" "go.uber.org/zap/zapcore" + "penahub.gitlab.yandexcloud.net/backend/penahub_common/log_mw" "penahub.gitlab.yandexcloud.net/backend/penahub_common/privilege" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/healthchecks" + "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/middleware" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model" "penahub.gitlab.yandexcloud.net/backend/quiz/core/brokers" "penahub.gitlab.yandexcloud.net/backend/quiz/core/clients/auth" @@ -179,8 +181,8 @@ func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.Co }) app := fiber.New() - //app.Use(middleware.JWTAuth()) - //app.Use(log_mw.ContextLogger(loggerHlog)) + app.Use(middleware.JWTAuth()) + app.Use(log_mw.ContextLogger(loggerHlog)) app.Get("/liveness", healthchecks.Liveness) app.Get("/readiness", healthchecks.Readiness(&workerErr)) //todo parametrized readiness. should discuss ready reason From fa24ff4de6055d7160ff24f76b587b3d4acd7fda Mon Sep 17 00:00:00 2001 From: pasha1coil Date: Tue, 9 Jul 2024 23:07:44 +0300 Subject: [PATCH 33/57] added logic for make user admin who sent messages need rework with event --- app/app.go | 7 ++--- clients/telegram/tg.go | 25 +++++++++++------- go.mod | 4 ++- go.sum | 13 ++++++++-- worker/tg_listener.go | 59 +++++++++++------------------------------- 5 files changed, 48 insertions(+), 60 deletions(-) diff --git a/app/app.go b/app/app.go index e603e51..9043428 100644 --- a/app/app.go +++ b/app/app.go @@ -155,17 +155,18 @@ func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.Co privilegeController := privilege.NewPrivilege(clientData, fiberClient) go tools.PublishPrivilege(privilegeController, 10, 5*time.Minute) - chatIDsChan := make(chan int64, 100) - tgClient, err := telegram.NewTelegramClient(ctx, pgdal, chatIDsChan) + tgClient, err := telegram.NewTelegramClient(ctx, pgdal) if err != nil { panic(fmt.Sprintf("failed init tg clietns: %v", err)) } - _, err = worker.NewTelegramBotWorker(options.TgBotToken, chatIDsChan) + tgWC, err := worker.NewTelegramBotWorker(options.TgBotToken) if err != nil { panic(fmt.Sprintf("failed init tg listener WC: %v", err)) } + go tgWC.Run() + // todo подумать над реализацией всего а то пока мне кажется что немного каша получается такой предикт что через некоторое время // сложно будет разобраться что есть где grpcControllers := initialize.InitRpcControllers(pgdal) diff --git a/clients/telegram/tg.go b/clients/telegram/tg.go index 9ac09db..bcdbe62 100644 --- a/clients/telegram/tg.go +++ b/clients/telegram/tg.go @@ -18,7 +18,6 @@ type TelegramClient struct { TgClients map[int64]*client.Client WaitingClients map[string]WaitingClient mu sync.Mutex - chatIDsChan chan int64 } type WaitingClient struct { @@ -26,12 +25,11 @@ type WaitingClient struct { Authorizer *client.ClientAuthorizer } -func NewTelegramClient(ctx context.Context, repo *dal.DAL, chatIDsChan chan int64) (*TelegramClient, error) { +func NewTelegramClient(ctx context.Context, repo *dal.DAL) (*TelegramClient, error) { tgClient := &TelegramClient{ repo: repo, TgClients: make(map[int64]*client.Client), WaitingClients: make(map[string]WaitingClient), - chatIDsChan: chatIDsChan, } allTgAccounts, err := repo.TgRepo.GetAllTgAccounts(ctx) @@ -196,12 +194,20 @@ func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string Status: &client.ChatMemberStatusAdministrator{ CustomTitle: "bot", Rights: &client.ChatAdministratorRights{ - CanManageChat: true, - CanChangeInfo: true, - CanPostMessages: true, - CanInviteUsers: true, - CanRestrictMembers: true, - CanPromoteMembers: true, + CanManageChat: true, + CanChangeInfo: true, + CanPostMessages: true, + CanEditMessages: true, + CanDeleteMessages: true, + CanInviteUsers: true, + CanRestrictMembers: true, + CanPinMessages: true, + CanManageTopics: true, + CanPromoteMembers: true, + CanManageVideoChats: true, + CanPostStories: true, + CanEditStories: true, + CanDeleteStories: true, }, }, }) @@ -232,7 +238,6 @@ func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string inviteLink = inviteLinkResp.InviteLink channelId = channel.Id - tg.chatIDsChan <- channelId return inviteLink, channelId, nil } diff --git a/go.mod b/go.mod index b9a6590..a2f59fb 100644 --- a/go.mod +++ b/go.mod @@ -23,6 +23,7 @@ require ( penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240702152013-c2a0ba2ac307 penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f + penahub.gitlab.yandexcloud.net/external/trashlog v0.1.2-0.20240709135426-e17a9ce9ba2c ) require ( @@ -34,6 +35,7 @@ require ( github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/go-redis/redis/v8 v8.11.5 // indirect + github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect github.com/goccy/go-json v0.10.2 // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/google/uuid v1.6.0 // indirect @@ -52,6 +54,7 @@ require ( github.com/richardlehane/mscfb v1.0.4 // indirect github.com/richardlehane/msoleps v1.0.3 // indirect github.com/rivo/uniseg v0.4.7 // indirect + github.com/tealeg/xlsx v1.0.5 // indirect github.com/twmb/franz-go/pkg/kmsg v1.8.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasthttp v1.53.0 // indirect @@ -68,5 +71,4 @@ require ( gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect - penahub.gitlab.yandexcloud.net/external/trashlog v0.1.2-0.20240704185641-304b297f19d8 // indirect ) diff --git a/go.sum b/go.sum index c8551ca..ff635e2 100644 --- a/go.sum +++ b/go.sum @@ -26,6 +26,8 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg= +github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= @@ -34,6 +36,8 @@ github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo= github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc= +github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/gofiber/fiber/v2 v2.52.4 h1:P+T+4iK7VaqUsq2PALYEfBBo6bJZ4q3FP8cZ84EggTM= @@ -139,6 +143,10 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/tealeg/xlsx v1.0.5 h1:+f8oFmvY8Gw1iUXzPk+kz+4GpbDZPK1FhPiQRd+ypgE= +github.com/tealeg/xlsx v1.0.5/go.mod h1:btRS8dz54TDnvKNosuAqxrM1QgN1udgk9O34bDCnORM= +github.com/themakers/bdd v0.0.0-20210316111417-6b1dfe326f33 h1:N9f/Q+2Ssa+yDcbfaoLTYvXmdeyUUxsJKdPUVsjSmiA= +github.com/themakers/bdd v0.0.0-20210316111417-6b1dfe326f33/go.mod h1:rpcH99JknBh8seZmlOlUg51gasZH6QH34oXNsIwYT6E= github.com/themakers/hlog v0.0.0-20191205140925-235e0e4baddf h1:TJJm6KcBssmbWzplF5lzixXl1RBAi/ViPs1GaSOkhwo= github.com/themakers/hlog v0.0.0-20191205140925-235e0e4baddf/go.mod h1:1FsorU3vnXO9xS9SrhUp8fRb/6H/Zfll0rPt1i4GWaA= github.com/twmb/franz-go v1.16.1 h1:rpWc7fB9jd7TgmCyfxzenBI+QbgS8ZfJOUQE+tzPtbE= @@ -257,6 +265,7 @@ google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6h google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= @@ -285,5 +294,5 @@ penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e0 penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990/go.mod h1:zswBuTwmEsFHBVRu1nkG3/Fwylk5Vcm8OUm9iWxccSE= penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f h1:Qli89wgu0T7nG4VECXZOZ40fjE/hVVfxF3hTaSYS008= penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f/go.mod h1:AkE19hcbDwB7hoEASwImm7rUI+cK/8jMVJaTvMK4F+c= -penahub.gitlab.yandexcloud.net/external/trashlog v0.1.2-0.20240704185641-304b297f19d8 h1:9RiVgJk+/0vNvzObrpM3Cjga0zz6PtNsJXj1babTk4Q= -penahub.gitlab.yandexcloud.net/external/trashlog v0.1.2-0.20240704185641-304b297f19d8/go.mod h1:6NMjnrYqjRc2YVzQxNvs5Q2BfQR6x40bb3oUVdV61XI= +penahub.gitlab.yandexcloud.net/external/trashlog v0.1.2-0.20240709135426-e17a9ce9ba2c h1:ZMOaDmy+OZkShN1u2x+qigtpSUi2b9V6I4n/10qBqqM= +penahub.gitlab.yandexcloud.net/external/trashlog v0.1.2-0.20240709135426-e17a9ce9ba2c/go.mod h1:6NMjnrYqjRc2YVzQxNvs5Q2BfQR6x40bb3oUVdV61XI= diff --git a/worker/tg_listener.go b/worker/tg_listener.go index 7b07efe..9caa2df 100644 --- a/worker/tg_listener.go +++ b/worker/tg_listener.go @@ -7,61 +7,32 @@ import ( ) type TelegramBotWorker struct { - bot *telebot.Bot - channelIDs chan int64 + bot *telebot.Bot } -func NewTelegramBotWorker(botToken string, channelIDs chan int64) (*TelegramBotWorker, error) { - botSettings := telebot.Settings{ +func NewTelegramBotWorker(botToken string) (*TelegramBotWorker, error) { + bot, err := telebot.NewBot(telebot.Settings{ Token: botToken, Poller: &telebot.LongPoller{Timeout: 10 * time.Second}, - } - - bot, err := telebot.NewBot(botSettings) + }) if err != nil { return nil, err } worker := &TelegramBotWorker{ - bot: bot, - channelIDs: channelIDs, + bot: bot, } return worker, nil } +// todo в канал писать нельзя надо что то придумать func (w *TelegramBotWorker) Run() { - w.bot.Handle(telebot.OnUserJoined, func(message telebot.Message) { - w.handleNewChatMembers(&message) - }) - - w.bot.Start() -} - -func (w *TelegramBotWorker) handleNewChatMembers(message *telebot.Message) { - chatID := message.Chat.ID - var found bool - - for { - select { - case channelID := <-w.channelIDs: - if chatID == channelID { - found = true - break - } - default: - break - } - - if found { - break - } - } - - if found { - for _, newMember := range message.UsersJoined { + w.bot.Handle(telebot.OnChannelPost, func(m *telebot.Message) { + if m.Text == "/admin" { + fmt.Println(m.Sender.ID) adminRights := telebot.ChatMember{ - User: &telebot.User{ID: newMember.ID}, + User: &telebot.User{ID: m.Sender.ID}, Role: telebot.Administrator, Rights: telebot.Rights{ CanManageChat: true, @@ -73,13 +44,13 @@ func (w *TelegramBotWorker) handleNewChatMembers(message *telebot.Message) { }, } - err := w.bot.Promote(message.Chat, &adminRights) + err := w.bot.Promote(m.Chat, &adminRights) if err != nil { - fmt.Printf("error to promote user %d in chat %d: %v", newMember.ID, chatID, err) + fmt.Printf("error to promote user %d in chat %d: %v", m.Sender.ID, m.Chat.ID, err) } else { - fmt.Printf("user %d has been promoted to admin in chat %d", newMember.ID, chatID) + fmt.Printf("user %d has been promoted to admin in chat %d", m.Sender.ID, m.Chat.ID) } - break } - } + }) + w.bot.Start() } From ee75ef86f5268e716f0f98b989cc709c67df4a29 Mon Sep 17 00:00:00 2001 From: pasha1coil Date: Wed, 10 Jul 2024 11:28:22 +0300 Subject: [PATCH 34/57] change chanel creating to sopergroup and promoute user to adminn by command --- clients/telegram/tg.go | 5 +++-- worker/tg_listener.go | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/clients/telegram/tg.go b/clients/telegram/tg.go index bcdbe62..8ce67a6 100644 --- a/clients/telegram/tg.go +++ b/clients/telegram/tg.go @@ -179,8 +179,9 @@ func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string // todo нужно поймать ошибку, при которой либо бан либо медленный редим включается для того чтобы прервать // исполнение клиента текущего аккаунта и дать задачу следующему пока поймал 1 раз и не запомнил больше не получается channel, err := activeClient.CreateNewSupergroupChat(&client.CreateNewSupergroupChatRequest{ - Title: channelName, - IsChannel: true, + Title: channelName, + IsForum: true, + //IsChannel: true, Description: "private channel", }) if err != nil { diff --git a/worker/tg_listener.go b/worker/tg_listener.go index 9caa2df..141f4a5 100644 --- a/worker/tg_listener.go +++ b/worker/tg_listener.go @@ -28,7 +28,7 @@ func NewTelegramBotWorker(botToken string) (*TelegramBotWorker, error) { // todo в канал писать нельзя надо что то придумать func (w *TelegramBotWorker) Run() { - w.bot.Handle(telebot.OnChannelPost, func(m *telebot.Message) { + w.bot.Handle(telebot.OnText, func(m *telebot.Message) { if m.Text == "/admin" { fmt.Println(m.Sender.ID) adminRights := telebot.ChatMember{ From 3eaf4999cca12e894e4febe93feb14cb75f60670 Mon Sep 17 00:00:00 2001 From: pasha1coil Date: Wed, 10 Jul 2024 14:46:45 +0300 Subject: [PATCH 35/57] - --- clients/telegram/tg.go | 5 ++-- service/service.go | 1 - service/telegram_svc.go | 12 --------- worker/tg_listener.go | 56 ----------------------------------------- 4 files changed, 2 insertions(+), 72 deletions(-) delete mode 100644 worker/tg_listener.go diff --git a/clients/telegram/tg.go b/clients/telegram/tg.go index 8ce67a6..bcdbe62 100644 --- a/clients/telegram/tg.go +++ b/clients/telegram/tg.go @@ -179,9 +179,8 @@ func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string // todo нужно поймать ошибку, при которой либо бан либо медленный редим включается для того чтобы прервать // исполнение клиента текущего аккаунта и дать задачу следующему пока поймал 1 раз и не запомнил больше не получается channel, err := activeClient.CreateNewSupergroupChat(&client.CreateNewSupergroupChatRequest{ - Title: channelName, - IsForum: true, - //IsChannel: true, + Title: channelName, + IsChannel: true, Description: "private channel", }) if err != nil { diff --git a/service/service.go b/service/service.go index a948ec5..983e9c9 100644 --- a/service/service.go +++ b/service/service.go @@ -91,5 +91,4 @@ func (s *Service) Register(app *fiber.App) { app.Post("/telegram/create", s.AddingTgAccount) app.Delete("/telegram/:id", s.DeleteTgAccountByID) app.Post("/telegram/setCode", s.SettingTgCode) - //app.Get("/test/test", s.TEST) } diff --git a/service/telegram_svc.go b/service/telegram_svc.go index 01c8c20..f2862fe 100644 --- a/service/telegram_svc.go +++ b/service/telegram_svc.go @@ -170,15 +170,3 @@ func (s *Service) DeleteTgAccountByID(ctx *fiber.Ctx) error { } return ctx.SendStatus(fiber.StatusOK) } - -//func (s *Service) TEST(ctx *fiber.Ctx) error { -// botID := 6712573453 -// chanalName := "testAAAAAA" -// link, chatID, err := s.telegramClient.CreateChannel(chanalName, int64(botID)) -// if err != nil { -// return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) -// } -// fmt.Println(chatID) -// -// return ctx.Status(fiber.StatusOK).SendString(link) -//} diff --git a/worker/tg_listener.go b/worker/tg_listener.go deleted file mode 100644 index 141f4a5..0000000 --- a/worker/tg_listener.go +++ /dev/null @@ -1,56 +0,0 @@ -package worker - -import ( - "fmt" - "gopkg.in/tucnak/telebot.v2" - "time" -) - -type TelegramBotWorker struct { - bot *telebot.Bot -} - -func NewTelegramBotWorker(botToken string) (*TelegramBotWorker, error) { - bot, err := telebot.NewBot(telebot.Settings{ - Token: botToken, - Poller: &telebot.LongPoller{Timeout: 10 * time.Second}, - }) - if err != nil { - return nil, err - } - - worker := &TelegramBotWorker{ - bot: bot, - } - - return worker, nil -} - -// todo в канал писать нельзя надо что то придумать -func (w *TelegramBotWorker) Run() { - w.bot.Handle(telebot.OnText, func(m *telebot.Message) { - if m.Text == "/admin" { - fmt.Println(m.Sender.ID) - adminRights := telebot.ChatMember{ - User: &telebot.User{ID: m.Sender.ID}, - Role: telebot.Administrator, - Rights: telebot.Rights{ - CanManageChat: true, - CanChangeInfo: true, - CanPostMessages: true, - CanInviteUsers: true, - CanRestrictMembers: true, - CanPromoteMembers: true, - }, - } - - err := w.bot.Promote(m.Chat, &adminRights) - if err != nil { - fmt.Printf("error to promote user %d in chat %d: %v", m.Sender.ID, m.Chat.ID, err) - } else { - fmt.Printf("user %d has been promoted to admin in chat %d", m.Sender.ID, m.Chat.ID) - } - } - }) - w.bot.Start() -} From 8f3c545b454056b789d079b773549fafc505e703 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 11 Jul 2024 12:06:24 +0300 Subject: [PATCH 36/57] added logic for set task for worker tg chanel creator and init redis in app, update openapi --- app/app.go | 30 ++++++++++++++++++++------- go.mod | 7 ++----- go.sum | 7 ++----- openapi.yaml | 17 ++++++++++------ service/account_svc.go | 43 ++++++++++++++++++++++++++++++--------- service/service.go | 4 ++++ workers/tg_worker.go | 46 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 122 insertions(+), 32 deletions(-) create mode 100644 workers/tg_worker.go diff --git a/app/app.go b/app/app.go index 9043428..7c5331f 100644 --- a/app/app.go +++ b/app/app.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "github.com/go-redis/redis/v8" "github.com/gofiber/fiber/v2" "github.com/skeris/appInit" "github.com/themakers/hlog" @@ -23,7 +24,7 @@ import ( "penahub.gitlab.yandexcloud.net/backend/quiz/core/server" "penahub.gitlab.yandexcloud.net/backend/quiz/core/service" "penahub.gitlab.yandexcloud.net/backend/quiz/core/tools" - "penahub.gitlab.yandexcloud.net/backend/quiz/core/worker" + "penahub.gitlab.yandexcloud.net/backend/quiz/core/workers" "penahub.gitlab.yandexcloud.net/external/trashlog/wrappers/zaptrashlog" "time" ) @@ -71,7 +72,10 @@ type Options struct { TrashLogHost string `env:"TRASH_LOG_HOST" default:"localhost:7113"` ModuleLogger string `env:"MODULE_LOGGER" default:"core-local"` ClickHouseCred string `env:"CLICK_HOUSE_CRED" default:"tcp://10.8.0.15:9000/default?sslmode=disable"` - TgBotToken string `env:"TG_BOT_TOKEN" default:"6712573453:AAFqTOsgwe_j48ZQ1GzWKQDT5Nwr-SAWjz8"` + BotID int `env:"BOT_ID"` // 6712573453 + RedisHost string `env:"REDIS_HOST" default:"localhost:6379"` + RedisPassword string `env:"REDIS_PASSWORD"` + RedisDB uint64 `env:"REDIS_DB" default:"2"` } func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.CommonApp, error) { @@ -146,6 +150,16 @@ func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.Co Logger: zapLogger, }) + redisClient := redis.NewClient(&redis.Options{ + Addr: options.RedisHost, + Password: options.RedisPassword, + DB: int(options.RedisDB), + }) + err = redisClient.Ping(ctx).Err() + if err != nil { + panic(fmt.Sprintf("error ping to redis db %v", err)) + } + clientData := privilege.Client{ URL: options.HubAdminUrl, ServiceName: options.ServiceName, @@ -160,12 +174,13 @@ func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.Co panic(fmt.Sprintf("failed init tg clietns: %v", err)) } - tgWC, err := worker.NewTelegramBotWorker(options.TgBotToken) - if err != nil { - panic(fmt.Sprintf("failed init tg listener WC: %v", err)) - } + tgWC := workers.NewTgListenerWC(workers.Deps{ + BotID: int64(options.BotID), + Redis: redisClient, + Dal: pgdal, + }) - go tgWC.Run() + go tgWC.Start(ctx) // todo подумать над реализацией всего а то пока мне кажется что немного каша получается такой предикт что через некоторое время // сложно будет разобраться что есть где @@ -194,6 +209,7 @@ func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.Co ServiceName: options.ServiceName, ChDAL: chDal, TelegramClient: tgClient, + RedisClient: redisClient, }) svc.Register(app) diff --git a/go.mod b/go.mod index a2f59fb..6e75ed6 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module penahub.gitlab.yandexcloud.net/backend/quiz/core go 1.22.4 require ( + github.com/go-redis/redis/v8 v8.11.5 github.com/gofiber/fiber/v2 v2.52.4 github.com/golang-jwt/jwt/v5 v5.2.1 github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 @@ -18,9 +19,8 @@ require ( go.uber.org/zap v1.27.0 google.golang.org/grpc v1.64.0 google.golang.org/protobuf v1.34.2 - gopkg.in/tucnak/telebot.v2 v2.5.0 penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c - penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240702152013-c2a0ba2ac307 + penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240711083739-b7ab43df146b penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f penahub.gitlab.yandexcloud.net/external/trashlog v0.1.2-0.20240709135426-e17a9ce9ba2c @@ -34,8 +34,6 @@ require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/dustin/go-humanize v1.0.1 // indirect - github.com/go-redis/redis/v8 v8.11.5 // indirect - github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect github.com/goccy/go-json v0.10.2 // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/google/uuid v1.6.0 // indirect @@ -49,7 +47,6 @@ require ( github.com/minio/minio-go/v7 v7.0.70 // indirect github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect github.com/pierrec/lz4/v4 v4.1.21 // indirect - github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/richardlehane/mscfb v1.0.4 // indirect github.com/richardlehane/msoleps v1.0.3 // indirect diff --git a/go.sum b/go.sum index ff635e2..8b05eea 100644 --- a/go.sum +++ b/go.sum @@ -36,8 +36,6 @@ github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo= github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= -github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc= -github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/gofiber/fiber/v2 v2.52.4 h1:P+T+4iK7VaqUsq2PALYEfBBo6bJZ4q3FP8cZ84EggTM= @@ -138,7 +136,6 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= @@ -288,8 +285,8 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c h1:CWb4UcuNXhd1KTNOmy2U0TJO4+Qxgxrj5cwkyFqbgrk= penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c/go.mod h1:+bPxq2wfW5S1gd+83vZYmHm33AE7nEBfznWS8AM1TKE= -penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240702152013-c2a0ba2ac307 h1:wCSZWRvg3q1AboSM8TC35429GMYna4U9sKbw9YEJHr0= -penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240702152013-c2a0ba2ac307/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240711083739-b7ab43df146b h1:rgQIQINsnv4nsTzLXynvFD5u5Wcq8qwWindI4pi1qGA= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240711083739-b7ab43df146b/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 h1:jiO8GWO+3sCnDAV8/NAV8tQIUwae/I6/xiDilW7zf0o= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990/go.mod h1:zswBuTwmEsFHBVRu1nkG3/Fwylk5Vcm8OUm9iWxccSE= penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f h1:Qli89wgu0T7nG4VECXZOZ40fjE/hVVfxF3hTaSYS008= diff --git a/openapi.yaml b/openapi.yaml index 1f2e5a3..17349f4 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -787,6 +787,8 @@ components: format: int32 Target: type: string + InviteLink: + type: string Deleted: type: boolean CreatedAt: @@ -1636,14 +1638,17 @@ paths: description: ID квиза, к которому прикреплено это правило (приоритет). Передавать как 0, если правило не прикрепляется к квизу и является общим. target: type: string - description: Адресат, куда конкретно слать (для mail - email, для telegram - ID чата, для whatsapp - номер телефона, наверное). + description: Адресат, куда конкретно слать (для mail - email, для telegram - ID канала, передавать не нужно канал сам создаться, для whatsapp - номер телефона, наверное). + name: + type: string + description: имя например для тг канала responses: '200': - description: ОК, парвило добавлено - content: - application/json: - schema: - $ref: '#/components/schemas/LeadTarget' + description: ОК, парвило добавлено если тип mail о сразу добавляется если тг то будет добавленно в воркере если ватсап пока тодо +# content: +# application/json: +# schema: +# $ref: '#/components/schemas/LeadTarget' '400': description: Bad request, ошибка в теле запроса content: diff --git a/service/account_svc.go b/service/account_svc.go index 8affe78..090ea01 100644 --- a/service/account_svc.go +++ b/service/account_svc.go @@ -2,7 +2,9 @@ package service import ( "database/sql" + "encoding/json" "errors" + "fmt" "github.com/gofiber/fiber/v2" "penahub.gitlab.yandexcloud.net/backend/penahub_common/log_mw" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/middleware" @@ -251,6 +253,7 @@ func (s *Service) PostLeadTarget(ctx *fiber.Ctx) error { Type string `json:"type"` QuizID int32 `json:"quizID"` Target string `json:"target"` + Name string `json:"name"` } if err := ctx.BodyParser(&req); err != nil { return ctx.Status(fiber.StatusBadRequest).SendString("Invalid request data") @@ -269,17 +272,39 @@ func (s *Service) PostLeadTarget(ctx *fiber.Ctx) error { return ctx.Status(fiber.StatusBadRequest).SendString("Type and Target don't be nil") } - result, err := s.dal.AccountRepo.PostLeadTarget(ctx.Context(), model.LeadTarget{ - AccountID: accountID, - Target: req.Target, - Type: model.LeadTargetType(req.Type), - QuizID: req.QuizID, - }) - if err != nil { - return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) + switch req.Type { + case "mail": + _, err := s.dal.AccountRepo.PostLeadTarget(ctx.Context(), model.LeadTarget{ + AccountID: accountID, + Target: req.Target, + Type: model.LeadTargetType(req.Type), + QuizID: req.QuizID, + }) + if err != nil { + return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) + } + return ctx.SendStatus(fiber.StatusOK) + case "telegram": + task := model.TgRedisTask{ + Name: req.Name, + QuizID: req.QuizID, + AccountID: accountID, + } + + taskKey := fmt.Sprintf("telegram_task:%d", time.Now().UnixNano()) + taskData, err := json.Marshal(task) + if err != nil { + return ctx.Status(fiber.StatusInternalServerError).SendString("Failed to marshal task") + } + + if err := s.redisClient.Set(ctx.Context(), taskKey, taskData, 0).Err(); err != nil { + return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) + } + case "whatsapp": + return ctx.Status(fiber.StatusOK).SendString("todo") } - return ctx.Status(fiber.StatusOK).JSON(result) + return nil } func (s *Service) DeleteLeadTarget(ctx *fiber.Ctx) error { diff --git a/service/service.go b/service/service.go index 983e9c9..ad29a5e 100644 --- a/service/service.go +++ b/service/service.go @@ -1,6 +1,7 @@ package service import ( + "github.com/go-redis/redis/v8" "github.com/gofiber/fiber/v2" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal" "penahub.gitlab.yandexcloud.net/backend/quiz/core/brokers" @@ -16,6 +17,7 @@ type Service struct { serviceName string chDAL *dal.ClickHouseDAL telegramClient *telegram.TelegramClient + redisClient *redis.Client } type Deps struct { @@ -25,6 +27,7 @@ type Deps struct { ServiceName string ChDAL *dal.ClickHouseDAL TelegramClient *telegram.TelegramClient + RedisClient *redis.Client } func New(deps Deps) *Service { @@ -35,6 +38,7 @@ func New(deps Deps) *Service { serviceName: deps.ServiceName, chDAL: deps.ChDAL, telegramClient: deps.TelegramClient, + redisClient: deps.RedisClient, } } diff --git a/workers/tg_worker.go b/workers/tg_worker.go new file mode 100644 index 0000000..1b57cef --- /dev/null +++ b/workers/tg_worker.go @@ -0,0 +1,46 @@ +package workers + +import ( + "context" + "github.com/go-redis/redis/v8" + "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal" + "time" +) + +type Deps struct { + BotID int64 + Redis *redis.Client + Dal *dal.DAL +} + +type TgListenerWorker struct { + botID int64 + redis *redis.Client + dal *dal.DAL +} + +func NewTgListenerWC(deps Deps) *TgListenerWorker { + return &TgListenerWorker{ + botID: deps.BotID, + redis: deps.Redis, + dal: deps.Dal, + } +} + +func (wc *TgListenerWorker) Start(ctx context.Context) { + ticker := time.NewTicker(10 * time.Second) + defer ticker.Stop() + + for { + select { + case <-ticker.C: + wc.processTasks(ctx) + case <-ctx.Done(): + return + } + } +} + +func (wc *TgListenerWorker) processTasks(ctx context.Context) { + +} From 334968d6fd5b334af18a0a727b5cf60144ba8997 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 11 Jul 2024 12:08:13 +0300 Subject: [PATCH 37/57] added logic for set task for worker tg chanel creator and init redis in app, update openapi --- service/account_svc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/account_svc.go b/service/account_svc.go index 090ea01..33ef300 100644 --- a/service/account_svc.go +++ b/service/account_svc.go @@ -294,7 +294,7 @@ func (s *Service) PostLeadTarget(ctx *fiber.Ctx) error { taskKey := fmt.Sprintf("telegram_task:%d", time.Now().UnixNano()) taskData, err := json.Marshal(task) if err != nil { - return ctx.Status(fiber.StatusInternalServerError).SendString("Failed to marshal task") + return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) } if err := s.redisClient.Set(ctx.Context(), taskKey, taskData, 0).Err(); err != nil { From 1665d40f27f427b3bc413ae9c8fe89e9d2c260f0 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 11 Jul 2024 14:09:17 +0300 Subject: [PATCH 38/57] start building tg worker --- workers/tg_worker.go | 64 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 9 deletions(-) diff --git a/workers/tg_worker.go b/workers/tg_worker.go index 1b57cef..df45145 100644 --- a/workers/tg_worker.go +++ b/workers/tg_worker.go @@ -2,28 +2,35 @@ package workers import ( "context" + "encoding/json" + "fmt" "github.com/go-redis/redis/v8" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal" + "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model" + "penahub.gitlab.yandexcloud.net/backend/quiz/core/clients/telegram" "time" ) type Deps struct { - BotID int64 - Redis *redis.Client - Dal *dal.DAL + BotID int64 + Redis *redis.Client + Dal *dal.DAL + TgClient *telegram.TelegramClient } type TgListenerWorker struct { - botID int64 - redis *redis.Client - dal *dal.DAL + botID int64 + redis *redis.Client + dal *dal.DAL + tgClient *telegram.TelegramClient } func NewTgListenerWC(deps Deps) *TgListenerWorker { return &TgListenerWorker{ - botID: deps.BotID, - redis: deps.Redis, - dal: deps.Dal, + botID: deps.BotID, + redis: deps.Redis, + dal: deps.Dal, + tgClient: deps.TgClient, } } @@ -42,5 +49,44 @@ func (wc *TgListenerWorker) Start(ctx context.Context) { } func (wc *TgListenerWorker) processTasks(ctx context.Context) { + var cursor uint64 + for { + var keys []string + var err error + keys, cursor, err = wc.redis.Scan(ctx, cursor, "telegram_task:*", 0).Result() + if err != nil { + fmt.Println("Failed scan for telegram tasks:", err) + break + } + for _, key := range keys { + func() { + taskBytes, err := wc.redis.GetDel(ctx, key).Result() + if err == redis.Nil { + return + } else if err != nil { + fmt.Println("Failed getdel telegram task:", err) + return + } + + defer func() { + + }() + + var task model.TgRedisTask + if json.Unmarshal([]byte(taskBytes), &task) != nil { + fmt.Println("Failed unmarshal telegram task:", err) + return + } + + inviteLink, chatID, err := wc.tgClient.CreateChannel(task.Name, wc.botID) + if err != nil { + fmt.Println("Failed create tg channel:", err) + } + }() + } + if cursor == 0 { + break + } + } } From bc8a95c85c7e6bc47bc30425708791fae2ca4f93 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 11 Jul 2024 14:15:08 +0300 Subject: [PATCH 39/57] - --- workers/tg_worker.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/workers/tg_worker.go b/workers/tg_worker.go index df45145..e31b2f0 100644 --- a/workers/tg_worker.go +++ b/workers/tg_worker.go @@ -70,7 +70,7 @@ func (wc *TgListenerWorker) processTasks(ctx context.Context) { } defer func() { - + // AAA }() var task model.TgRedisTask From 45407888654ce61d37e0cf19ae1b8fdcc6e4e8a8 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 11 Jul 2024 14:17:15 +0300 Subject: [PATCH 40/57] added defer for restore --- workers/tg_worker.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/workers/tg_worker.go b/workers/tg_worker.go index e31b2f0..a562b6d 100644 --- a/workers/tg_worker.go +++ b/workers/tg_worker.go @@ -70,7 +70,10 @@ func (wc *TgListenerWorker) processTasks(ctx context.Context) { } defer func() { - // AAA + if r := recover(); r != nil { + fmt.Println("recovering from panic or error setting redis value:", r) + _ = wc.redis.Set(ctx, key, taskBytes, 0).Err() + } }() var task model.TgRedisTask From 36ca81376b8069d4883e88cba1417eae5ef5c942 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 11 Jul 2024 16:04:18 +0300 Subject: [PATCH 41/57] coplete logic worker tg with tg lead target --- workers/tg_worker.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/workers/tg_worker.go b/workers/tg_worker.go index a562b6d..b1ef897 100644 --- a/workers/tg_worker.go +++ b/workers/tg_worker.go @@ -8,6 +8,7 @@ import ( "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model" "penahub.gitlab.yandexcloud.net/backend/quiz/core/clients/telegram" + "strconv" "time" ) @@ -85,6 +86,19 @@ func (wc *TgListenerWorker) processTasks(ctx context.Context) { inviteLink, chatID, err := wc.tgClient.CreateChannel(task.Name, wc.botID) if err != nil { fmt.Println("Failed create tg channel:", err) + return + } + + _, err = wc.dal.AccountRepo.PostLeadTarget(ctx, model.LeadTarget{ + AccountID: task.AccountID, + Type: model.LeadTargetTg, + QuizID: task.QuizID, + Target: strconv.Itoa(int(chatID)), + InviteLink: inviteLink, + }) + if err != nil { + fmt.Println("Failed create lead target in db:", err) + return } }() } From a04682a9b32a2b51767727d4d2fc895665be5a85 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 11 Jul 2024 16:07:00 +0300 Subject: [PATCH 42/57] added todo --- service/account_svc.go | 1 + workers/tg_worker.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/service/account_svc.go b/service/account_svc.go index 33ef300..48de13a 100644 --- a/service/account_svc.go +++ b/service/account_svc.go @@ -285,6 +285,7 @@ func (s *Service) PostLeadTarget(ctx *fiber.Ctx) error { } return ctx.SendStatus(fiber.StatusOK) case "telegram": + // todo before saving task to redis need check this quizID and type task := model.TgRedisTask{ Name: req.Name, QuizID: req.QuizID, diff --git a/workers/tg_worker.go b/workers/tg_worker.go index b1ef897..5f7b894 100644 --- a/workers/tg_worker.go +++ b/workers/tg_worker.go @@ -69,7 +69,7 @@ func (wc *TgListenerWorker) processTasks(ctx context.Context) { fmt.Println("Failed getdel telegram task:", err) return } - + // todo logging into tg with trashlog defer func() { if r := recover(); r != nil { fmt.Println("recovering from panic or error setting redis value:", r) From 735000ced1d6cf629f63caf1c9df8caa17b54ef9 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 11 Jul 2024 16:37:47 +0300 Subject: [PATCH 43/57] before saving task to redis need check this quizID and type --- go.mod | 2 +- go.sum | 4 ++-- service/account_svc.go | 13 ++++++++++++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 6e75ed6..986c573 100644 --- a/go.mod +++ b/go.mod @@ -20,7 +20,7 @@ require ( google.golang.org/grpc v1.64.0 google.golang.org/protobuf v1.34.2 penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c - penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240711083739-b7ab43df146b + penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240711133242-0b8534fae5b2 penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f penahub.gitlab.yandexcloud.net/external/trashlog v0.1.2-0.20240709135426-e17a9ce9ba2c diff --git a/go.sum b/go.sum index 8b05eea..48adc23 100644 --- a/go.sum +++ b/go.sum @@ -285,8 +285,8 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c h1:CWb4UcuNXhd1KTNOmy2U0TJO4+Qxgxrj5cwkyFqbgrk= penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c/go.mod h1:+bPxq2wfW5S1gd+83vZYmHm33AE7nEBfznWS8AM1TKE= -penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240711083739-b7ab43df146b h1:rgQIQINsnv4nsTzLXynvFD5u5Wcq8qwWindI4pi1qGA= -penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240711083739-b7ab43df146b/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240711133242-0b8534fae5b2 h1:0t6pQHJvA3jMeBB3FPUmA+hw8rWlksTah8KJEtf2KD8= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240711133242-0b8534fae5b2/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 h1:jiO8GWO+3sCnDAV8/NAV8tQIUwae/I6/xiDilW7zf0o= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990/go.mod h1:zswBuTwmEsFHBVRu1nkG3/Fwylk5Vcm8OUm9iWxccSE= penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f h1:Qli89wgu0T7nG4VECXZOZ40fjE/hVVfxF3hTaSYS008= diff --git a/service/account_svc.go b/service/account_svc.go index 48de13a..8ae08ae 100644 --- a/service/account_svc.go +++ b/service/account_svc.go @@ -285,7 +285,18 @@ func (s *Service) PostLeadTarget(ctx *fiber.Ctx) error { } return ctx.SendStatus(fiber.StatusOK) case "telegram": - // todo before saving task to redis need check this quizID and type + targets, err := s.dal.AccountRepo.GetLeadTarget(ctx.Context(), accountID, req.QuizID) + if err != nil && !errors.Is(err, pj_errors.ErrNotFound) { + return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) + } + if !errors.Is(err, pj_errors.ErrNotFound) { + for _, t := range targets { + if t.Type == model.LeadTargetTg { + return ctx.Status(fiber.StatusAlreadyReported).SendString("LeadTarget for this quiz already exist") + } + } + } + task := model.TgRedisTask{ Name: req.Name, QuizID: req.QuizID, From 8ee4975ef92e329f98a3d65a608fbbb40ef5b3cd Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 11 Jul 2024 18:10:16 +0300 Subject: [PATCH 44/57] added todo, so strange events with tdlib --- app/app.go | 10 +++++----- clients/telegram/tg.go | 1 + service/account_svc.go | 4 +++- workers/tg_worker.go | 2 +- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/app/app.go b/app/app.go index 7c5331f..43cdc20 100644 --- a/app/app.go +++ b/app/app.go @@ -72,9 +72,8 @@ type Options struct { TrashLogHost string `env:"TRASH_LOG_HOST" default:"localhost:7113"` ModuleLogger string `env:"MODULE_LOGGER" default:"core-local"` ClickHouseCred string `env:"CLICK_HOUSE_CRED" default:"tcp://10.8.0.15:9000/default?sslmode=disable"` - BotID int `env:"BOT_ID"` // 6712573453 RedisHost string `env:"REDIS_HOST" default:"localhost:6379"` - RedisPassword string `env:"REDIS_PASSWORD"` + RedisPassword string `env:"REDIS_PASSWORD" default:"admin"` RedisDB uint64 `env:"REDIS_DB" default:"2"` } @@ -175,9 +174,10 @@ func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.Co } tgWC := workers.NewTgListenerWC(workers.Deps{ - BotID: int64(options.BotID), - Redis: redisClient, - Dal: pgdal, + BotID: int64(6712573453), // todo убрать + Redis: redisClient, + Dal: pgdal, + TgClient: tgClient, }) go tgWC.Start(ctx) diff --git a/clients/telegram/tg.go b/clients/telegram/tg.go index bcdbe62..7f43d23 100644 --- a/clients/telegram/tg.go +++ b/clients/telegram/tg.go @@ -168,6 +168,7 @@ func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string var inviteLink string var channelId int64 for _, activeClient := range tg.TgClients { + // todo пока не понимаю это какой то рандом? в один день бот норм находится в другой уже не находится хотя абсолютно с точки зрения тг кода этой функции и бота не менялось _, err := activeClient.GetUser(&client.GetUserRequest{ UserId: botID, }) diff --git a/service/account_svc.go b/service/account_svc.go index 8ae08ae..3366be4 100644 --- a/service/account_svc.go +++ b/service/account_svc.go @@ -264,11 +264,13 @@ func (s *Service) PostLeadTarget(ctx *fiber.Ctx) error { return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required") } + //accountID := "64f2cd7a7047f28fdabf6d9e" + if _, ok := model.ValidLeadTargetTypes[req.Type]; !ok { return ctx.Status(fiber.StatusBadRequest).SendString("Invalid type") } - if req.Type == "" || req.Target == "" { + if req.Type == "" || (req.Target == "" && req.Type != string(model.LeadTargetTg)) { return ctx.Status(fiber.StatusBadRequest).SendString("Type and Target don't be nil") } diff --git a/workers/tg_worker.go b/workers/tg_worker.go index 5f7b894..84e5c90 100644 --- a/workers/tg_worker.go +++ b/workers/tg_worker.go @@ -36,7 +36,7 @@ func NewTgListenerWC(deps Deps) *TgListenerWorker { } func (wc *TgListenerWorker) Start(ctx context.Context) { - ticker := time.NewTicker(10 * time.Second) + ticker := time.NewTicker(10 * time.Second) //time.Minute defer ticker.Stop() for { From ce69833a259b6d660bd7cedc3890f26ce9e0c3db Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 11 Jul 2024 18:42:55 +0300 Subject: [PATCH 45/57] added aiming error in tg worker for correct restoring data in redis --- workers/tg_worker.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/workers/tg_worker.go b/workers/tg_worker.go index 84e5c90..80deb41 100644 --- a/workers/tg_worker.go +++ b/workers/tg_worker.go @@ -70,34 +70,37 @@ func (wc *TgListenerWorker) processTasks(ctx context.Context) { return } // todo logging into tg with trashlog + var aimErr error defer func() { - if r := recover(); r != nil { - fmt.Println("recovering from panic or error setting redis value:", r) + if r := recover(); r != nil || aimErr != nil { + fmt.Println("recovering from panic or error setting redis value:", r, aimErr) _ = wc.redis.Set(ctx, key, taskBytes, 0).Err() } }() var task model.TgRedisTask - if json.Unmarshal([]byte(taskBytes), &task) != nil { + if err = json.Unmarshal([]byte(taskBytes), &task); err != nil { fmt.Println("Failed unmarshal telegram task:", err) return } - inviteLink, chatID, err := wc.tgClient.CreateChannel(task.Name, wc.botID) - if err != nil { - fmt.Println("Failed create tg channel:", err) + var inviteLink string + var chatID int64 + inviteLink, chatID, aimErr = wc.tgClient.CreateChannel(task.Name, wc.botID) + if aimErr != nil { + fmt.Println("Failed create tg channel:", aimErr) return } - _, err = wc.dal.AccountRepo.PostLeadTarget(ctx, model.LeadTarget{ + _, aimErr = wc.dal.AccountRepo.PostLeadTarget(ctx, model.LeadTarget{ AccountID: task.AccountID, Type: model.LeadTargetTg, QuizID: task.QuizID, Target: strconv.Itoa(int(chatID)), InviteLink: inviteLink, }) - if err != nil { - fmt.Println("Failed create lead target in db:", err) + if aimErr != nil { + fmt.Println("Failed create lead target in db:", aimErr) return } }() From 7c3b18f9fb642e90b1bbe687737f77457942d3b6 Mon Sep 17 00:00:00 2001 From: skeris Date: Wed, 17 Jul 2024 13:59:48 +0300 Subject: [PATCH 46/57] debug template copy --- service/quiz_svc.go | 1 + 1 file changed, 1 insertion(+) diff --git a/service/quiz_svc.go b/service/quiz_svc.go index c8f3f3b..59e40f6 100644 --- a/service/quiz_svc.go +++ b/service/quiz_svc.go @@ -505,6 +505,7 @@ func (s *Service) TemplateCopy(ctx *fiber.Ctx) error { qizID, err := s.dal.QuizRepo.TemplateCopy(ctx.Context(), accountID, req.Qid) if err != nil { + fmt.Println("TEMPLERR", err) return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error()) } From c98076ec10fc67dd42ae94028be40ac11bf0b5ea Mon Sep 17 00:00:00 2001 From: skeris Date: Wed, 17 Jul 2024 14:03:14 +0300 Subject: [PATCH 47/57] debug template copy --- service/quiz_svc.go | 1 + 1 file changed, 1 insertion(+) diff --git a/service/quiz_svc.go b/service/quiz_svc.go index 59e40f6..2b0a496 100644 --- a/service/quiz_svc.go +++ b/service/quiz_svc.go @@ -9,6 +9,7 @@ import ( "penahub.gitlab.yandexcloud.net/backend/quiz/core/models" "time" "unicode/utf8" + "fmt" ) type CreateQuizReq struct { From 7897dc2cb1058a500c1d5480fde304afef4e3627 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 18 Jul 2024 22:22:55 +0300 Subject: [PATCH 48/57] added parse img url to another type for img and varimg --- tools/tools.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tools/tools.go b/tools/tools.go index 61e01d0..85c0bc5 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -1,6 +1,7 @@ package tools import ( + "encoding/json" "fmt" "github.com/xuri/excelize/v2" _ "image/gif" @@ -101,7 +102,12 @@ func WriteDataToExcel(buffer io.Writer, questions []model.Question, answers []mo noAccept := make(map[string]struct{}) todoMap := make(map[string]string) if tipe != "Text" && q.Type == model.TypeImages || q.Type == model.TypeVarImages { - urle := ExtractImageURL(response[index].Content) + var res model.ImageContent + err := json.Unmarshal([]byte(response[index].Content), &res) + if err != nil { + res.Image = response[index].Content + } + urle := ExtractImageURL(res.Image) urlData := strings.Split(urle, " ") if len(urlData) == 1 { u, err := url.Parse(urle) From cf974729bd2522ac878e060b0ac1aef48d20e157 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 18 Jul 2024 22:41:49 +0300 Subject: [PATCH 49/57] added s3prefix for file if this url and do not have hhtps prefix --- app/app.go | 2 ++ service/result_svc.go | 2 +- service/service.go | 3 +++ tools/tools.go | 5 ++++- 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/app.go b/app/app.go index fa04d5e..cc42572 100644 --- a/app/app.go +++ b/app/app.go @@ -69,6 +69,7 @@ type Options struct { TrashLogHost string `env:"TRASH_LOG_HOST" default:"localhost:7113"` ModuleLogger string `env:"MODULE_LOGGER" default:"core-local"` ClickHouseCred string `env:"CLICK_HOUSE_CRED" default:"tcp://10.8.0.15:9000/default?sslmode=disable"` + S3Prefix string `env:"S3_PREFIX"` } func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.CommonApp, error) { @@ -178,6 +179,7 @@ func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.Co Producer: producer, ServiceName: options.ServiceName, ChDAL: chDal, + S3Prefix: options.S3Prefix, }) svc.Register(app) diff --git a/service/result_svc.go b/service/result_svc.go index 59f5ab8..f5521d4 100644 --- a/service/result_svc.go +++ b/service/result_svc.go @@ -177,7 +177,7 @@ func (s *Service) ExportResultsToCSV(ctx *fiber.Ctx) error { buffer := new(bytes.Buffer) - if err := tools.WriteDataToExcel(buffer, questions, answers); err != nil { + if err := tools.WriteDataToExcel(buffer, questions, answers, s.s3Prefix); err != nil { return ctx.Status(fiber.StatusInternalServerError).SendString("failed to write data to Excel") } diff --git a/service/service.go b/service/service.go index a99ffad..db42bc6 100644 --- a/service/service.go +++ b/service/service.go @@ -14,6 +14,7 @@ type Service struct { producer *brokers.Producer serviceName string chDAL *dal.ClickHouseDAL + s3Prefix string } type Deps struct { @@ -22,6 +23,7 @@ type Deps struct { Producer *brokers.Producer ServiceName string ChDAL *dal.ClickHouseDAL + S3Prefix string } func New(deps Deps) *Service { @@ -31,6 +33,7 @@ func New(deps Deps) *Service { producer: deps.Producer, serviceName: deps.ServiceName, chDAL: deps.ChDAL, + s3Prefix: deps.S3Prefix, } } diff --git a/tools/tools.go b/tools/tools.go index 85c0bc5..0ca0e31 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -28,7 +28,7 @@ const ( bucketAnswers = "squizanswer" ) -func WriteDataToExcel(buffer io.Writer, questions []model.Question, answers []model.Answer) error { +func WriteDataToExcel(buffer io.Writer, questions []model.Question, answers []model.Answer, s3Prefix string) error { file := excelize.NewFile() sheet := "Sheet1" @@ -130,6 +130,9 @@ func WriteDataToExcel(buffer io.Writer, questions []model.Question, answers []mo } } else if tipe != "Text" && q.Type == model.TypeFile { urle := ExtractImageURL(response[index].Content) + if urle != "" && !strings.HasPrefix(urle, "https") { + urle = s3Prefix + urle + } display, tooltip := urle, urle if err := file.SetCellValue(sheet, cell, response[index].Content); err != nil { fmt.Println(err.Error()) From 6f55c35b7bb4423092d8aa7c8e4733d7c0c01def Mon Sep 17 00:00:00 2001 From: skeris Date: Fri, 19 Jul 2024 15:46:40 +0300 Subject: [PATCH 50/57] add qid to excel file answer path --- deployments/main/docker-compose.yaml | 1 + service/result_svc.go | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/deployments/main/docker-compose.yaml b/deployments/main/docker-compose.yaml index 4cfbcc1..e32eb14 100644 --- a/deployments/main/docker-compose.yaml +++ b/deployments/main/docker-compose.yaml @@ -22,6 +22,7 @@ services: TRASH_LOG_HOST: "10.8.0.15:7113" MODULE_LOGGER: "quiz-core-main" CLICK_HOUSE_CRED: "clickhouse://10.8.0.15:9000/default?sslmode=disable" + S3_PREFIX: "https://s3.timeweb.cloud/3c580be9-cf31f296-d055-49cf-b39e-30c7959dc17b/squizimages/" ports: - 10.8.0.9:1488:1488 - 10.8.0.9:9000:9000 diff --git a/service/result_svc.go b/service/result_svc.go index f5521d4..b06a40c 100644 --- a/service/result_svc.go +++ b/service/result_svc.go @@ -159,6 +159,11 @@ func (s *Service) ExportResultsToCSV(ctx *fiber.Ctx) error { } } + quiz, err := s.dal.QuizRepo.GetQuizById(ctx.Context(), accountID, quizID) + if err != nil { + return ctx.Status(fiber.StatusInternalServerError).SendString("failed to get quiz") + } + questions, err := s.dal.ResultRepo.GetQuestions(ctx.Context(), quizID) if err != nil { return ctx.Status(fiber.StatusInternalServerError).SendString("failed to get questions") @@ -177,7 +182,7 @@ func (s *Service) ExportResultsToCSV(ctx *fiber.Ctx) error { buffer := new(bytes.Buffer) - if err := tools.WriteDataToExcel(buffer, questions, answers, s.s3Prefix); err != nil { + if err := tools.WriteDataToExcel(buffer, questions, answers, s.s3Prefix + quiz.Qid + "/"); err != nil { return ctx.Status(fiber.StatusInternalServerError).SendString("failed to write data to Excel") } From c40276de94e170ea6209af2cc29b93350d1fe4a7 Mon Sep 17 00:00:00 2001 From: skeris Date: Fri, 19 Jul 2024 23:52:08 +0300 Subject: [PATCH 51/57] quickfix: do not extract image url for file --- tools/tools.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tools/tools.go b/tools/tools.go index 0ca0e31..d921857 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -128,13 +128,14 @@ func WriteDataToExcel(buffer io.Writer, questions []model.Question, answers []mo } else { todoMap[response[index].Content] = cell } - } else if tipe != "Text" && q.Type == model.TypeFile { - urle := ExtractImageURL(response[index].Content) + } else if q.Type == model.TypeFile { + urle := response[index].Content if urle != "" && !strings.HasPrefix(urle, "https") { urle = s3Prefix + urle } + fmt.Println("ORRRRR", urle, s3Prefix) display, tooltip := urle, urle - if err := file.SetCellValue(sheet, cell, response[index].Content); err != nil { + if err := file.SetCellValue(sheet, cell, urle); err != nil { fmt.Println(err.Error()) } if err := file.SetCellHyperLink(sheet, cell, urle, "External", excelize.HyperlinkOpts{ From d750fbf236f0c59edd50167d3559d5231f7d766c Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 23 Jul 2024 12:01:50 +0300 Subject: [PATCH 52/57] added refactored gen excel files --- tools/tools.go | 376 +++++++++++++++++-------------------------------- 1 file changed, 126 insertions(+), 250 deletions(-) diff --git a/tools/tools.go b/tools/tools.go index d921857..9628027 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -41,6 +41,36 @@ func WriteDataToExcel(buffer io.Writer, questions []model.Question, answers []mo return questions[i].Page < questions[j].Page }) + headers, mapQueRes := prepareHeaders(questions) + for col, header := range headers { + cell := ToAlphaString(col+1) + "1" + if err := file.SetCellValue(sheet, cell, header); err != nil { + return err + } + } + + sort.Slice(answers, func(i, j int) bool { + return answers[i].QuestionId < answers[j].QuestionId + }) + + standart, results := categorizeAnswers(answers) + + var wg sync.WaitGroup + row := 2 + for session := range results { + wg.Add(1) + go func(session string, response []model.Answer, row int) { + defer wg.Done() + processSession(file, sheet, session, s3Prefix, response, results, questions, mapQueRes, headers, row) + }(session, standart[session], row) + row++ + } + wg.Wait() + + return file.Write(buffer) +} + +func prepareHeaders(questions []model.Question) ([]string, map[uint64]string) { headers := []string{"Данные респондента"} mapQueRes := make(map[uint64]string) @@ -53,27 +83,14 @@ func WriteDataToExcel(buffer io.Writer, questions []model.Question, answers []mo } } } - headers = append(headers, "Результат") + return headers, mapQueRes +} - for col, header := range headers { - cell := ToAlphaString(col+1) + "1" - if err := file.SetCellValue(sheet, cell, header); err != nil { - return err - } - } - - sort.Slice(answers, func(i, j int) bool { - return answers[i].QuestionId < answers[j].QuestionId - }) - - // мапа для хранения обычных ответов респондентов +func categorizeAnswers(answers []model.Answer) (map[string][]model.Answer, map[string]model.Answer) { standart := make(map[string][]model.Answer) - - // мапа для хранения данных респондентов results := make(map[string]model.Answer) - // заполняем мапу ответами и данными респондентов for _, answer := range answers { if answer.Result { results[answer.Session] = answer @@ -81,113 +98,110 @@ func WriteDataToExcel(buffer io.Writer, questions []model.Question, answers []mo standart[answer.Session] = append(standart[answer.Session], answer) } } + return standart, results +} - processSession := func(session string, response []model.Answer, row int) { - defer func() { - if r := recover(); r != nil { - fmt.Println("Recovered from panic:", r) - } - }() - - if err := file.SetCellValue(sheet, "A"+strconv.Itoa(row), results[session].Content); err != nil { - fmt.Println(err.Error()) +func processSession(file *excelize.File, sheet, session, s3Prefix string, response []model.Answer, results map[string]model.Answer, questions []model.Question, mapQueRes map[uint64]string, headers []string, row int) { + defer func() { + if r := recover(); r != nil { + fmt.Println("Recovered from panic:", r) } - count := 2 - for _, q := range questions { - if !q.Deleted && q.Type != model.TypeResult { - index := binarySearch(response, q.Id) - if index != -1 { - cell := ToAlphaString(count) + strconv.Itoa(row) - tipe := FileSearch(response[index].Content) - noAccept := make(map[string]struct{}) - todoMap := make(map[string]string) - if tipe != "Text" && q.Type == model.TypeImages || q.Type == model.TypeVarImages { - var res model.ImageContent - err := json.Unmarshal([]byte(response[index].Content), &res) - if err != nil { - res.Image = response[index].Content - } - urle := ExtractImageURL(res.Image) - urlData := strings.Split(urle, " ") - if len(urlData) == 1 { - u, err := url.Parse(urle) - if err == nil && u.Scheme != "" && u.Host != "" { - picture, err := downloadImage(urle) - if err != nil { - fmt.Println(err.Error()) - } - file.SetColWidth(sheet, ToAlphaString(count), ToAlphaString(count), 50) - file.SetRowHeight(sheet, row, 150) - if err := file.AddPictureFromBytes(sheet, cell, picture); err != nil { - fmt.Println(err.Error()) - } - noAccept[response[index].Content] = struct{}{} - } else { - todoMap[response[index].Content] = cell - } - } else { - todoMap[response[index].Content] = cell - } - } else if q.Type == model.TypeFile { - urle := response[index].Content - if urle != "" && !strings.HasPrefix(urle, "https") { - urle = s3Prefix + urle - } - fmt.Println("ORRRRR", urle, s3Prefix) - display, tooltip := urle, urle - if err := file.SetCellValue(sheet, cell, urle); err != nil { - fmt.Println(err.Error()) - } - if err := file.SetCellHyperLink(sheet, cell, urle, "External", excelize.HyperlinkOpts{ - Display: &display, - Tooltip: &tooltip, - }); err != nil { - fmt.Println(err.Error()) - } - noAccept[response[index].Content] = struct{}{} - } else { - todoMap[response[index].Content] = cell - } - for cnt, cel := range todoMap { - if _, ok := noAccept[cnt]; !ok { - if err := file.SetCellValue(sheet, cel, cnt); err != nil { - fmt.Println(err.Error()) - } - } - } + }() - } else { - cell := ToAlphaString(count) + strconv.Itoa(row) - if err := file.SetCellValue(sheet, cell, "-"); err != nil { - fmt.Println(err.Error()) - } + if err := file.SetCellValue(sheet, "A"+strconv.Itoa(row), results[session].Content); err != nil { + fmt.Println(err.Error()) + } + count := 2 + for _, q := range questions { + if !q.Deleted && q.Type != model.TypeResult { + cell := ToAlphaString(count) + strconv.Itoa(row) + index := binarySearch(response, q.Id) + if index != -1 { + handleAnswer(file, sheet, cell, s3Prefix, response[index], q, count, row) + } else { + if err := file.SetCellValue(sheet, cell, "-"); err != nil { + fmt.Println(err.Error()) } - count++ + } + count++ + } + } + cell := ToAlphaString(len(headers)) + strconv.Itoa(row) + if err := file.SetCellValue(sheet, cell, mapQueRes[results[session].QuestionId]); err != nil { + fmt.Println(err.Error()) + } +} + +func handleAnswer(file *excelize.File, sheet, cell, s3Prefix string, answer model.Answer, question model.Question, count, row int) { + tipe := FileSearch(answer.Content) + noAccept := make(map[string]struct{}) + todoMap := make(map[string]string) + + if tipe != "Text" && question.Type == model.TypeImages || question.Type == model.TypeVarImages { + handleImage(file, sheet, cell, answer.Content, count, row, noAccept, todoMap) + } else if question.Type == model.TypeFile { + handleFile(file, sheet, cell, answer.Content, s3Prefix, noAccept) + } else { + todoMap[answer.Content] = cell + } + + for cnt, cel := range todoMap { + if _, ok := noAccept[cnt]; !ok { + if err := file.SetCellValue(sheet, cel, cnt); err != nil { + fmt.Println(err.Error()) } } - cell := ToAlphaString(len(headers)) + strconv.Itoa(row) - if err := file.SetCellValue(sheet, cell, mapQueRes[results[session].QuestionId]); err != nil { - fmt.Println(err.Error()) + } +} + +func handleImage(file *excelize.File, sheet, cell, content string, count, row int, noAccept map[string]struct{}, todoMap map[string]string) { + var res model.ImageContent + err := json.Unmarshal([]byte(content), &res) + if err != nil { + res.Image = content + } + urle := ExtractImageURL(res.Image) + urlData := strings.Split(urle, " ") + if len(urlData) == 1 { + u, err := url.Parse(urle) + if err == nil && u.Scheme != "" && u.Host != "" { + picture, err := downloadImage(urle) + if err != nil { + fmt.Println(err.Error()) + } + file.SetColWidth(sheet, ToAlphaString(count), ToAlphaString(count), 50) + file.SetRowHeight(sheet, row, 150) + if err := file.AddPictureFromBytes(sheet, cell, picture); err != nil { + fmt.Println(err.Error()) + } + noAccept[content] = struct{}{} + } else { + todoMap[content] = cell } + } else { + todoMap[content] = cell + } +} + +func handleFile(file *excelize.File, sheet, cell, content, s3Prefix string, noAccept map[string]struct{}) { + urle := content + if urle != "" && !strings.HasPrefix(urle, "https") { + urle = s3Prefix + urle } - row := 2 - var wg sync.WaitGroup - for session, _ := range results { - wg.Add(1) - go func(session string, response []model.Answer, row int) { - defer wg.Done() - processSession(session, standart[session], row) - }(session, standart[session], row) - row++ - } - wg.Wait() + fmt.Println("ORRRRR", urle, s3Prefix) + display, tooltip := urle, urle - if err := file.Write(buffer); err != nil { - return err + if err := file.SetCellValue(sheet, cell, urle); err != nil { + fmt.Println(err.Error()) } - - return nil + if err := file.SetCellHyperLink(sheet, cell, urle, "External", excelize.HyperlinkOpts{ + Display: &display, + Tooltip: &tooltip, + }); err != nil { + fmt.Println(err.Error()) + } + noAccept[content] = struct{}{} } func binarySearch(answers []model.Answer, questionID uint64) int { @@ -293,141 +307,3 @@ func ExtractImageURL(htmlContent string) string { } return htmlContent } - -//func WriteDataToExcel(buffer io.Writer, questions []model.Question, answers []model.Answer) error { -// file := excelize.NewFile() -// sheet := "Sheet1" -// -// _, err := file.NewSheet(sheet) -// if err != nil { -// return err -// } -// -// sort.Slice(questions, func(i, j int) bool { -// return questions[i].Page > questions[j].Page -// }) -// -// headers := []string{"Данные респондента"} -// mapQueRes := make(map[uint64]string) -// -// for _, q := range questions { -// if !q.Deleted { -// if q.Type == model.TypeResult { -// mapQueRes[q.Id] = q.Title + "\n" + q.Description -// } else { -// headers = append(headers, q.Title) -// } -// } -// } -// -// headers = append(headers, "Результат") -// -// // добавляем заголовки в первую строку -// for col, header := range headers { -// cell := ToAlphaString(col+1) + "1" -// if err := file.SetCellValue(sheet, cell, header); err != nil { -// return err -// } -// } -// -// // мапа для хранения обычных ответов респондентов -// standart := make(map[string][]model.Answer) -// -// // мапа для хранения данных респондентов -// results := make(map[string]model.Answer) -// -// // заполняем мапу ответами и данными респондентов -// for _, answer := range answers { -// if answer.Result { -// results[answer.Session] = answer -// } else { -// standart[answer.Session] = append(standart[answer.Session], answer) -// } -// } -// -// // записываем данные в файл -// row := 2 -// for session, _ := range results { -// response := standart[session] -// if err := file.SetCellValue(sheet, "A"+strconv.Itoa(row), results[session].Content); err != nil { -// return err -// } -// count := 2 -// for _, q := range questions { -// if !q.Deleted && q.Type != model.TypeResult { -// sort.Slice(response, func(i, j int) bool { -// return response[i].QuestionId < response[j].QuestionId -// }) -// index := binarySearch(response, q.Id) -// if index != -1 { -// cell := ToAlphaString(count) + strconv.Itoa(row) -// typeMap := FileSearch(response[index].Content) -// noAccept := make(map[string]struct{}) -// todoMap := make(map[string]string) -// for _, tipe := range typeMap { -// if tipe != "Text" && q.Type == model.TypeImages || q.Type == model.TypeVarImages { -// urle := ExtractImageURL(response[index].Content) -// urlData := strings.Split(urle, " ") -// for _, k := range urlData { -// u, err := url.Parse(k) -// if err == nil && u.Scheme != "" && u.Host != "" { -// picture, err := downloadImage(k) -// if err != nil { -// return err -// } -// file.SetColWidth(sheet, ToAlphaString(count), ToAlphaString(count), 50) -// file.SetRowHeight(sheet, row, 150) -// if err := file.AddPictureFromBytes(sheet, cell, picture); err != nil { -// return err -// } -// noAccept[response[index].Content] = struct{}{} -// } -// } -// } else if tipe != "Text" && q.Type == model.TypeFile { -// urle := ExtractImageURL(response[index].Content) -// display, tooltip := urle, urle -// if err := file.SetCellValue(sheet, cell, response[index].Content); err != nil { -// return err -// } -// if err := file.SetCellHyperLink(sheet, cell, urle, "External", excelize.HyperlinkOpts{ -// Display: &display, -// Tooltip: &tooltip, -// }); err != nil { -// return err -// } -// noAccept[response[index].Content] = struct{}{} -// } else { -// todoMap[response[index].Content] = cell -// } -// } -// for cnt, cel := range todoMap { -// if _, ok := noAccept[cnt]; !ok { -// if err := file.SetCellValue(sheet, cel, cnt); err != nil { -// return err -// } -// } -// } -// -// } else { -// cell := ToAlphaString(count) + strconv.Itoa(row) -// if err := file.SetCellValue(sheet, cell, "-"); err != nil { -// return err -// } -// } -// count++ -// } -// } -// cell := ToAlphaString(len(headers)) + strconv.Itoa(row) -// if err := file.SetCellValue(sheet, cell, mapQueRes[results[session].QuestionId]); err != nil { -// return err -// } -// row++ -// } -// -// // cохраняем данные в буфер -// if err := file.Write(buffer); err != nil { -// return err -// } -// -// return nil -//} From f7d48c57e62f85b760571a44996200363ee661b4 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 17 Sep 2024 14:09:53 +0300 Subject: [PATCH 53/57] update --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 986c573..128a6f4 100644 --- a/go.mod +++ b/go.mod @@ -23,7 +23,7 @@ require ( penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240711133242-0b8534fae5b2 penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f - penahub.gitlab.yandexcloud.net/external/trashlog v0.1.2-0.20240709135426-e17a9ce9ba2c + penahub.gitlab.yandexcloud.net/external/trashlog v0.1.6-0.20240827173635-78ce9878c387 ) require ( diff --git a/go.sum b/go.sum index 48adc23..eec2bf5 100644 --- a/go.sum +++ b/go.sum @@ -291,5 +291,5 @@ penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e0 penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990/go.mod h1:zswBuTwmEsFHBVRu1nkG3/Fwylk5Vcm8OUm9iWxccSE= penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f h1:Qli89wgu0T7nG4VECXZOZ40fjE/hVVfxF3hTaSYS008= penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f/go.mod h1:AkE19hcbDwB7hoEASwImm7rUI+cK/8jMVJaTvMK4F+c= -penahub.gitlab.yandexcloud.net/external/trashlog v0.1.2-0.20240709135426-e17a9ce9ba2c h1:ZMOaDmy+OZkShN1u2x+qigtpSUi2b9V6I4n/10qBqqM= -penahub.gitlab.yandexcloud.net/external/trashlog v0.1.2-0.20240709135426-e17a9ce9ba2c/go.mod h1:6NMjnrYqjRc2YVzQxNvs5Q2BfQR6x40bb3oUVdV61XI= +penahub.gitlab.yandexcloud.net/external/trashlog v0.1.6-0.20240827173635-78ce9878c387 h1:G+GIhkkvUsM9No2rf2D4kvQ2ExTw9KxlA8vsSnC0ywU= +penahub.gitlab.yandexcloud.net/external/trashlog v0.1.6-0.20240827173635-78ce9878c387/go.mod h1:30nezjpGpZuNThbQOCULIfa79RoJ5sray593jhfVP/Q= From b25bf0449815d3ae103cdc96d45dda85cb3ea10c Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 18 Sep 2024 15:47:57 +0300 Subject: [PATCH 54/57] added multi img result render --- tools/tools.go | 137 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 117 insertions(+), 20 deletions(-) diff --git a/tools/tools.go b/tools/tools.go index 9628027..47e4cfe 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -137,8 +137,8 @@ func handleAnswer(file *excelize.File, sheet, cell, s3Prefix string, answer mode noAccept := make(map[string]struct{}) todoMap := make(map[string]string) - if tipe != "Text" && question.Type == model.TypeImages || question.Type == model.TypeVarImages { - handleImage(file, sheet, cell, answer.Content, count, row, noAccept, todoMap) + if tipe != "Text" && (question.Type == model.TypeImages || question.Type == model.TypeVarImages) { + handleImage(file, sheet, cell, answer.Content, count, row, noAccept, todoMap, question.Title) } else if question.Type == model.TypeFile { handleFile(file, sheet, cell, answer.Content, s3Prefix, noAccept) } else { @@ -154,32 +154,129 @@ func handleAnswer(file *excelize.File, sheet, cell, s3Prefix string, answer mode } } -func handleImage(file *excelize.File, sheet, cell, content string, count, row int, noAccept map[string]struct{}, todoMap map[string]string) { - var res model.ImageContent - err := json.Unmarshal([]byte(content), &res) - if err != nil { - res.Image = content - } - urle := ExtractImageURL(res.Image) - urlData := strings.Split(urle, " ") - if len(urlData) == 1 { - u, err := url.Parse(urle) - if err == nil && u.Scheme != "" && u.Host != "" { - picture, err := downloadImage(urle) +func handleImage(file *excelize.File, sheet, cell, content string, count, row int, noAccept map[string]struct{}, todoMap map[string]string, questionTitle string) { + multiImgArr := strings.Split(content, "`,`") + if len(multiImgArr) > 1 { + var descriptions []string + mediaSheet := "Media" + flag, err := file.GetSheetIndex(mediaSheet) + if err != nil { + fmt.Println(err.Error()) + } + if flag == -1 { + _, _ = file.NewSheet(mediaSheet) + err = file.SetCellValue(mediaSheet, "A1", "Вопрос") if err != nil { fmt.Println(err.Error()) } - file.SetColWidth(sheet, ToAlphaString(count), ToAlphaString(count), 50) - file.SetRowHeight(sheet, row, 150) - if err := file.AddPictureFromBytes(sheet, cell, picture); err != nil { + } + + mediaRow := row + for i, imgContent := range multiImgArr { + if i == 0 && len(imgContent) > 1 && imgContent[0] == '`' { + imgContent = imgContent[1:] + } + + if i == len(multiImgArr)-1 && len(imgContent) > 1 && imgContent[len(imgContent)-1] == '`' { + imgContent = imgContent[:len(imgContent)-1] + } + + var res model.ImageContent + err := json.Unmarshal([]byte(imgContent), &res) + if err != nil { + res.Image = imgContent + } + + // чек на пустой дескрипшен, есмли пустой то отмечаем как вариант ответа номер по i + if res.Description != "" { + descriptions = append(descriptions, res.Description) + } else { + descriptions = append(descriptions, fmt.Sprintf("Вариант ответа №%d", i+1)) + } + + urle := ExtractImageURL(res.Image) + urlData := strings.Split(urle, " ") + if len(urlData) == 1 { + u, err := url.Parse(urle) + if err == nil && u.Scheme != "" && u.Host != "" { + picture, err := downloadImage(urle) + if err != nil { + fmt.Println(err.Error()) + continue + } + err = file.SetCellValue(mediaSheet, "A"+strconv.Itoa(mediaRow), questionTitle) + if err != nil { + fmt.Println(err.Error()) + } + + col := ToAlphaString(i + 2) + err = file.SetColWidth(mediaSheet, col, col, 50) + if err != nil { + fmt.Println(err.Error()) + } + err = file.SetRowHeight(mediaSheet, mediaRow, 150) + if err != nil { + fmt.Println(err.Error()) + } + if err := file.AddPictureFromBytes(mediaSheet, col+strconv.Itoa(mediaRow), picture); err != nil { + fmt.Println(err.Error()) + } + noAccept[content] = struct{}{} + } else { + todoMap[content] = cell + } + } else { + todoMap[imgContent] = cell + } + + descriptionsStr := strings.Join(descriptions, "`,`") + linkText := fmt.Sprintf("%s, Приложение: %s!A%d", descriptionsStr, mediaSheet, mediaRow) + + if err := file.SetCellValue(sheet, cell, linkText); err != nil { fmt.Println(err.Error()) } - noAccept[content] = struct{}{} + if err := file.SetCellHyperLink(sheet, cell, fmt.Sprintf("%s!A%d", mediaSheet, mediaRow), "Location", excelize.HyperlinkOpts{ + Display: &linkText, + }); err != nil { + fmt.Println(err.Error()) + } + } + } else { + if len(content) > 1 && content[0] == '`' && content[len(content)-1] == '`' { + content = content[1 : len(content)-1] + } + var res model.ImageContent + err := json.Unmarshal([]byte(content), &res) + if err != nil { + res.Image = content + } + urle := ExtractImageURL(res.Image) + urlData := strings.Split(urle, " ") + if len(urlData) == 1 { + u, err := url.Parse(urle) + if err == nil && u.Scheme != "" && u.Host != "" { + picture, err := downloadImage(urle) + if err != nil { + fmt.Println(err.Error()) + } + err = file.SetColWidth(sheet, ToAlphaString(count), ToAlphaString(count), 50) + if err != nil { + fmt.Println(err.Error()) + } + err = file.SetRowHeight(sheet, row, 150) + if err != nil { + fmt.Println(err.Error()) + } + if err := file.AddPictureFromBytes(sheet, cell, picture); err != nil { + fmt.Println(err.Error()) + } + noAccept[content] = struct{}{} + } else { + todoMap[content] = cell + } } else { todoMap[content] = cell } - } else { - todoMap[content] = cell } } From 8567060e1bd9b9b0e1f3262cb742ba3c1926b062 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 18 Sep 2024 18:09:54 +0300 Subject: [PATCH 55/57] added render \n --- tools/tools.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/tools/tools.go b/tools/tools.go index 47e4cfe..b8d530e 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -52,7 +52,6 @@ func WriteDataToExcel(buffer io.Writer, questions []model.Question, answers []mo sort.Slice(answers, func(i, j int) bool { return answers[i].QuestionId < answers[j].QuestionId }) - standart, results := categorizeAnswers(answers) var wg sync.WaitGroup @@ -147,7 +146,25 @@ func handleAnswer(file *excelize.File, sheet, cell, s3Prefix string, answer mode for cnt, cel := range todoMap { if _, ok := noAccept[cnt]; !ok { - if err := file.SetCellValue(sheet, cel, cnt); err != nil { + cntArr := strings.Split(cnt, "`,`") + resultCnt := cnt + if len(cntArr) > 1 { + resultCnt = strings.Join(cntArr, "\n") + } + + if len(resultCnt) > 1 && resultCnt[0] == '`' && resultCnt[len(resultCnt)-1] == '`' { + resultCnt = resultCnt[1 : len(resultCnt)-1] + } + + if len(resultCnt) > 1 && resultCnt[0] == '`' { + resultCnt = resultCnt[1:] + } + + if len(resultCnt) > 1 && resultCnt[len(resultCnt)-1] == '`' { + resultCnt = resultCnt[:len(resultCnt)-1] + } + + if err := file.SetCellValue(sheet, cel, resultCnt); err != nil { fmt.Println(err.Error()) } } @@ -229,8 +246,8 @@ func handleImage(file *excelize.File, sheet, cell, content string, count, row in todoMap[imgContent] = cell } - descriptionsStr := strings.Join(descriptions, "`,`") - linkText := fmt.Sprintf("%s, Приложение: %s!A%d", descriptionsStr, mediaSheet, mediaRow) + descriptionsStr := strings.Join(descriptions, "\n") + linkText := fmt.Sprintf("%s\n Перейти в приложение", descriptionsStr) if err := file.SetCellValue(sheet, cell, linkText); err != nil { fmt.Println(err.Error()) From 736893b2aa3277778172415ca6c07d8211a3992a Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 19 Sep 2024 10:48:25 +0300 Subject: [PATCH 56/57] drop local hyper link from excel --- tools/tools.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tools/tools.go b/tools/tools.go index b8d530e..0e18c99 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -252,11 +252,11 @@ func handleImage(file *excelize.File, sheet, cell, content string, count, row in if err := file.SetCellValue(sheet, cell, linkText); err != nil { fmt.Println(err.Error()) } - if err := file.SetCellHyperLink(sheet, cell, fmt.Sprintf("%s!A%d", mediaSheet, mediaRow), "Location", excelize.HyperlinkOpts{ - Display: &linkText, - }); err != nil { - fmt.Println(err.Error()) - } + //if err := file.SetCellHyperLink(sheet, cell, fmt.Sprintf("%s!A%d", mediaSheet, mediaRow), "Location", excelize.HyperlinkOpts{ + // Display: &linkText, + //}); err != nil { + // fmt.Println(err.Error()) + //} } } else { if len(content) > 1 && content[0] == '`' && content[len(content)-1] == '`' { From a8bfd290ebdf462aab5f846ee586a7f931df2d89 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 19 Sep 2024 10:49:05 +0300 Subject: [PATCH 57/57] drop local hyper link from excel --- tools/tools.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/tools.go b/tools/tools.go index 0e18c99..1d3ac02 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -247,7 +247,7 @@ func handleImage(file *excelize.File, sheet, cell, content string, count, row in } descriptionsStr := strings.Join(descriptions, "\n") - linkText := fmt.Sprintf("%s\n Перейти в приложение", descriptionsStr) + linkText := fmt.Sprintf("%s\n Перейти в приложение %s!A%d", descriptionsStr, mediaSheet, mediaRow) if err := file.SetCellValue(sheet, cell, linkText); err != nil { fmt.Println(err.Error())