Skip to content

Commit

Permalink
Verification tasks twitter changes (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-myles authored Nov 19, 2024
1 parent 0e206ea commit b14a26d
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 38 deletions.
2 changes: 1 addition & 1 deletion cmd/eskimo-hut/eskimo_hut.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (s *service) Init(ctx context.Context, cancel context.CancelFunc) {
s.quizRepository = kycquiz.NewRepository(ctx, s.usersProcessor)
s.usersLinker = linkerkyc.NewAccountLinker(ctx, cfg.Host)
s.faceKycClient = facekyc.New(ctx, s.usersProcessor, s.usersLinker)
s.verificationScenariosRepository = verificationscenarios.New(ctx, s.usersProcessor, s.usersLinker, cfg.Host)
s.verificationScenariosRepository = verificationscenarios.New(ctx, s.usersProcessor, s.usersLinker, s.socialRepository, cfg.Host)
}

func (s *service) Close(ctx context.Context) error {
Expand Down
12 changes: 5 additions & 7 deletions cmd/eskimo-hut/kyc.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,13 +392,14 @@ func (s *service) ForwardToFaceKYC(
// @Router /v1w/kyc/verifyCoinDistributionEligibility/users/{userId}/scenarios/{scenarioEnum} [POST].
func (s *service) VerifyKYCScenarios( //nolint:gocritic // .
ctx context.Context,
req *server.Request[verificationscenarios.VerificationMetadata, any],
) (*server.Response[any], *server.Response[server.ErrorResponse]) {
req *server.Request[verificationscenarios.VerificationMetadata, verificationscenarios.Verification],
) (*server.Response[verificationscenarios.Verification], *server.Response[server.ErrorResponse]) {
if err := validateScenariosData(req.Data); err != nil {
return nil, server.UnprocessableEntity(errors.Wrapf(err, "validations failed for %#v", req.Data), invalidPropertiesErrorCode)
}
ctx = users.ContextWithAuthorization(ctx, req.Data.Authorization) //nolint:revive // .
if err := s.verificationScenariosRepository.VerifyScenarios(ctx, req.Data); err != nil {
res, err := s.verificationScenariosRepository.VerifyScenarios(ctx, req.Data)
if err != nil {
switch {
case errors.Is(err, verificationscenarios.ErrVerificationNotPassed):
return nil, server.BadRequest(err, kycVerificationScenariosVadidationFailedErrorCode)
Expand All @@ -417,7 +418,7 @@ func (s *service) VerifyKYCScenarios( //nolint:gocritic // .
}
}

return server.OK[any](nil), nil
return server.OK[verificationscenarios.Verification](res), nil
}

//nolint:funlen,gocognit,revive // .
Expand All @@ -428,9 +429,6 @@ func validateScenariosData(data *verificationscenarios.VerificationMetadata) err
return errors.Errorf("empty cmc profile link `%v`", data.CMCProfileLink)
}
case verificationscenarios.CoinDistributionScenarioTwitter:
if data.TweetURL == "" {
return errors.Errorf("empty tweet url `%v`", data.TweetURL)
}
if data.Language == "" {
return errors.Errorf("empty language `%v`", data.Language)
}
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ require (
golang.org/x/tools v0.27.0 // indirect
google.golang.org/api v0.206.0 // indirect
google.golang.org/appengine/v2 v2.0.6 // indirect
google.golang.org/genproto v0.0.0-20241113202542-65e8d215514f // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20241113202542-65e8d215514f // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241113202542-65e8d215514f // indirect
google.golang.org/genproto v0.0.0-20241118233622-e639e219e697 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20241118233622-e639e219e697 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241118233622-e639e219e697 // indirect
google.golang.org/grpc v1.68.0 // indirect
google.golang.org/grpc/stats/opentelemetry v0.0.0-20241028142157-ada6787961b3 // indirect
google.golang.org/protobuf v1.35.2 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -572,12 +572,12 @@ google.golang.org/appengine/v2 v2.0.6/go.mod h1:WoEXGoXNfa0mLvaH5sV3ZSGXwVmy8yf7
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
google.golang.org/genproto v0.0.0-20241113202542-65e8d215514f h1:zDoHYmMzMacIdjNe+P2XiTmPsLawi/pCbSPfxt6lTfw=
google.golang.org/genproto v0.0.0-20241113202542-65e8d215514f/go.mod h1:Q5m6g8b5KaFFzsQFIGdJkSJDGeJiybVenoYFMMa3ohI=
google.golang.org/genproto/googleapis/api v0.0.0-20241113202542-65e8d215514f h1:M65LEviCfuZTfrfzwwEoxVtgvfkFkBUbFnRbxCXuXhU=
google.golang.org/genproto/googleapis/api v0.0.0-20241113202542-65e8d215514f/go.mod h1:Yo94eF2nj7igQt+TiJ49KxjIH8ndLYPZMIRSiRcEbg0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241113202542-65e8d215514f h1:C1QccEa9kUwvMgEUORqQD9S17QesQijxjZ84sO82mfo=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241113202542-65e8d215514f/go.mod h1:GX3210XPVPUjJbTUbvwI8f2IpZDMZuPJWDzDuebbviI=
google.golang.org/genproto v0.0.0-20241118233622-e639e219e697 h1:ToEetK57OidYuqD4Q5w+vfEnPvPpuTwedCNVohYJfNk=
google.golang.org/genproto v0.0.0-20241118233622-e639e219e697/go.mod h1:JJrvXBWRZaFMxBufik1a4RpFw4HhgVtBBWQeQgUj2cc=
google.golang.org/genproto/googleapis/api v0.0.0-20241118233622-e639e219e697 h1:pgr/4QbFyktUv9CtQ/Fq4gzEE6/Xs7iCXbktaGzLHbQ=
google.golang.org/genproto/googleapis/api v0.0.0-20241118233622-e639e219e697/go.mod h1:+D9ySVjN8nY8YCVjc5O7PZDIdZporIDY3KaGfJunh88=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241118233622-e639e219e697 h1:LWZqQOEjDyONlF1H6afSWpAL/znlREo2tHfLoe+8LMA=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241118233622-e639e219e697/go.mod h1:5uTbfoYQed2U9p3KIj2/Zzm02PYhndfdmML0qC3q3FU=
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=
Expand Down
1 change: 1 addition & 0 deletions kyc/social/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type (
io.Closer
VerifyPost(ctx context.Context, metadata *VerificationMetadata) (*Verification, error)
SkipVerification(ctx context.Context, kycStep users.KYCStep, userID string) error
ExpectedPostTemplateText(user *users.User, vm *VerificationMetadata) string
}
UserRepository interface {
GetUserByID(ctx context.Context, userID string) (*users.UserProfile, error)
Expand Down
5 changes: 5 additions & 0 deletions kyc/social/social.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,11 @@ func (r *repository) expectedPostText(user *users.User, vm *VerificationMetadata
if r.expectedPostTextIsExactMatch(vm) {
return r.expectedPostSubtext(user, vm)
}

return r.ExpectedPostTemplateText(user, vm)
}

func (r *repository) ExpectedPostTemplateText(user *users.User, vm *VerificationMetadata) string {
var (
templ *languageTemplate
tname = tenantName(r.cfg.TenantName)
Expand Down
22 changes: 13 additions & 9 deletions kyc/verification_scenarios/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type (
Token = string
TenantScenario = string
Repository interface {
VerifyScenarios(ctx context.Context, metadata *VerificationMetadata) error
VerifyScenarios(ctx context.Context, metadata *VerificationMetadata) (res *Verification, err error)
GetPendingVerificationScenarios(ctx context.Context, userID string) ([]Scenario, error)
}
UserRepository interface {
Expand All @@ -68,6 +68,9 @@ type (
TweetURL string `json:"tweetUrl" required:"false" example:"some tweet"`
TelegramUsername string `json:"telegramUsername" required:"false" example:"some telegram username"`
}
Verification struct {
ExpectedPostText string `json:"expectedPostText,omitempty" example:"This is a verification post!"`
}
)

// Private API.
Expand All @@ -84,14 +87,14 @@ var (
//nolint:gochecknoglobals,gomnd // We need it to sort scenarios.
scenarioOrder = map[Scenario]int{
CoinDistributionScenarioCmc: 0,
CoinDistributionScenarioTwitter: 1,
CoinDistributionScenarioTelegram: 2,
Scenario(CoinDistributionScenarioSignUpSunwaves): 3,
Scenario(CoinDistributionScenarioSignUpCallfluent): 4,
Scenario(CoinDistributionScenarioSignUpDoctorx): 5,
Scenario(CoinDistributionScenarioSignUpSauces): 6,
Scenario(CoinDistributionScenarioSignUpSealsend): 7,
Scenario(CoinDistributionScenarioSignUpTokero): 8,
Scenario(CoinDistributionScenarioSignUpSunwaves): 1,
Scenario(CoinDistributionScenarioSignUpCallfluent): 2,
Scenario(CoinDistributionScenarioSignUpDoctorx): 3,
Scenario(CoinDistributionScenarioSignUpSauces): 4,
Scenario(CoinDistributionScenarioSignUpSealsend): 5,
Scenario(CoinDistributionScenarioSignUpTokero): 6,
CoinDistributionScenarioTwitter: 7,
CoinDistributionScenarioTelegram: 8,
}
)

Expand All @@ -102,6 +105,7 @@ type (
twitterVerifier scraper.Verifier
cmcVerifier scraper.Verifier
linkerRepo linking.Linker
socialRepo social.Repository
host string
}
config struct {
Expand Down
31 changes: 19 additions & 12 deletions kyc/verification_scenarios/verification_scenarios.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/ice-blockchain/wintr/time"
)

func New(ctx context.Context, usrRepo UserRepository, linker linking.Linker, host string) Repository {
func New(ctx context.Context, usrRepo UserRepository, linker linking.Linker, socialRepo social.Repository, host string) Repository {
var cfg config
appcfg.MustLoadFromKey(applicationYamlKey, &cfg)
repo := &repository{
Expand All @@ -38,35 +38,42 @@ func New(ctx context.Context, usrRepo UserRepository, linker linking.Linker, hos
twitterVerifier: scraper.New(scraper.StrategyTwitter),
cmcVerifier: scraper.New(scraper.StrategyCMC),
linkerRepo: linker,
socialRepo: socialRepo,
}
go repo.startKYCConfigJSONSyncer(ctx)

return repo
}

//nolint:funlen,gocognit,gocyclo,revive,cyclop // .
func (r *repository) VerifyScenarios(ctx context.Context, metadata *VerificationMetadata) error {
func (r *repository) VerifyScenarios(ctx context.Context, metadata *VerificationMetadata) (res *Verification, err error) {
now := time.Now()
userIDScenarioMap := make(map[TenantScenario]users.UserID, 0)
usr, err := r.userRepo.GetUserByID(ctx, metadata.UserID)
if err != nil {
return errors.Wrapf(err, "failed to get user by id: %v", metadata.UserID)
return nil, errors.Wrapf(err, "failed to get user by id: %v", metadata.UserID)
}
pendingScenarios, err := r.getPendingScenarios(ctx, usr.User)
if err != nil {
return errors.Wrapf(err, "can't call getPendingScenarios for %v", usr.ID)
return nil, errors.Wrapf(err, "can't call getPendingScenarios for %v", usr.ID)
}
if len(pendingScenarios) == 0 || !isScenarioPending(pendingScenarios, string(metadata.ScenarioEnum)) {
return errors.Wrapf(ErrNoPendingScenarios, "no pending scenarios for user: %v", metadata.UserID)
return nil, errors.Wrapf(ErrNoPendingScenarios, "no pending scenarios for user: %v", metadata.UserID)
}
switch metadata.ScenarioEnum {
case CoinDistributionScenarioCmc:
if vErr := r.VerifyCMCProfile(ctx, metadata); vErr != nil {
return errors.Wrapf(vErr, "haven't passed the CMC verification for userID:%v", metadata.UserID)
return nil, errors.Wrapf(vErr, "haven't passed the CMC verification for userID:%v", metadata.UserID)
}
case CoinDistributionScenarioTwitter:
if sErr := r.VerifyTwitterPost(ctx, metadata); sErr != nil {
return errors.Wrapf(sErr, "failed to call VerifyPostForDistibutionVerification for userID:%v", metadata.UserID)
if metadata.TweetURL == "" {
return &Verification{
ExpectedPostText: r.socialRepo.ExpectedPostTemplateText(usr.User, &social.VerificationMetadata{
Language: metadata.Language,
KYCStep: users.Social1KYCStep,
Social: social.TwitterType,
}),
}, nil
}
case CoinDistributionScenarioTelegram:
case CoinDistributionScenarioSignUpTenants:
Expand All @@ -82,16 +89,16 @@ func (r *repository) VerifyScenarios(ctx context.Context, metadata *Verification
tenantTokens[splitted[1]] = token
}
if skippedTokenCount == len(metadata.TenantTokens) {
return errors.Wrapf(ErrWrongTenantTokens, "no pending tenant tokens for userID:%v", metadata.UserID)
return nil, errors.Wrapf(ErrWrongTenantTokens, "no pending tenant tokens for userID:%v", metadata.UserID)
}
var links linking.LinkedProfiles
links, _, err = r.linkerRepo.Verify(ctx, now, usr.ID, tenantTokens)
if err != nil {
if errors.Is(err, linking.ErrRemoteUserNotFound) {
return errors.Wrapf(linking.ErrNotOwnRemoteUser, "foreign token of userID:%v", metadata.UserID)
return nil, errors.Wrapf(linking.ErrNotOwnRemoteUser, "foreign token of userID:%v", metadata.UserID)
}

return errors.Wrapf(err, "failed to verify linking user for userID:%v", metadata.UserID)
return nil, errors.Wrapf(err, "failed to verify linking user for userID:%v", metadata.UserID)
}
for tenant, linkedUser := range links {
for inputTenant := range tenantTokens {
Expand All @@ -102,7 +109,7 @@ func (r *repository) VerifyScenarios(ctx context.Context, metadata *Verification
}
}

return errors.Wrapf(r.setCompletedDistributionScenario(ctx, usr.User, metadata.ScenarioEnum, userIDScenarioMap, pendingScenarios),
return res, errors.Wrapf(r.setCompletedDistributionScenario(ctx, usr.User, metadata.ScenarioEnum, userIDScenarioMap, pendingScenarios),
"failed to setCompletedDistributionScenario for userID:%v", metadata.UserID)
}

Expand Down

0 comments on commit b14a26d

Please sign in to comment.