Skip to content

Commit

Permalink
Merge branch 'main' into 146-stripe-integration-for-111-nft
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisommore authored Jan 22, 2024
2 parents d76ce91 + baacbc9 commit 255500e
Show file tree
Hide file tree
Showing 42 changed files with 1,030 additions and 179 deletions.
12 changes: 11 additions & 1 deletion .env-sample
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,15 @@ VPN_DEPLOYER_API_SG=abcd
EREBRUS_API_US_EAST=abcd
EREBRUS_API_SG=abcd
GOOGLE_AUDIENCE=abcd
APTOS_REPORT_FUNCTION_ID=0x75bcfe882d1a4d032ead2b47f377e4c95221594d66ab2bd09a61aded4c9d64f9
EREBRUS_API_US_EAST=abcd
EREBRUS_API_SG=abcd
EREBRUS_SG=abcd
EREBRUS_CA=abcd
EREBRUS_EU=abcd
EREBRUS_JP=abcd
SOTREUS_US=abcd
SOTREUS_SG=abcd
EREBRUS_US=abcd
STRIPE_SECRET_KEY=
STRIPE_WEBHOOK_SECRET=
STRIPE_WEBHOOK_SECRET=
15 changes: 15 additions & 0 deletions api/v1/delegateReviewCreation/delegateReviewCreation.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/NetSepio/gateway/models"
"github.com/NetSepio/gateway/util/pkg/aptos"
"github.com/NetSepio/gateway/util/pkg/logwrapper"
"github.com/NetSepio/gateway/util/pkg/openai"
"github.com/TheLazarusNetwork/go-helpers/httpo"
"github.com/gin-gonic/gin"
)
Expand All @@ -36,6 +37,19 @@ func deletegateReviewCreation(c *gin.Context) {
}

walletAddr := c.GetString(paseto.CTX_WALLET_ADDRES)

isSpam, err := openai.IsReviewSpam(request.Description)
if err != nil {
logwrapper.Error("failed to check spam", err)
httpo.NewErrorResponse(http.StatusInternalServerError, "Unexpected error occured").SendD(c)
return
}

if isSpam {
httpo.NewErrorResponse(http.StatusForbidden, "Review is spam").SendD(c)
return
}

txResult, err := aptos.DelegateReview(aptos.DelegateReviewParams{Voter: walletAddr, MetaDataUri: request.MetaDataUri, Category: request.Category, DomainAddress: request.DomainAddress, SiteUrl: request.SiteUrl, SiteType: request.SiteType, SiteTag: request.SiteTag, SiteSafety: request.SiteSafety})
if err != nil {
if errors.Is(err, aptos.ErrMetadataDuplicated) {
Expand Down Expand Up @@ -63,6 +77,7 @@ func deletegateReviewCreation(c *gin.Context) {
SiteIpfsHash: "",
TransactionHash: txResult.Result.TransactionHash,
TransactionVersion: txResult.Result.Version,
SiteRating: request.SiteRating,
}
go webreview.Publish(request.MetaDataUri, strings.TrimSuffix(request.SiteUrl, "/"))
if err := db.Create(newReview).Error; err != nil {
Expand Down
2 changes: 2 additions & 0 deletions api/v1/delegateReviewCreation/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ type DelegateReviewCreationRequest struct {
SiteType string `json:"siteType" binding:"required"`
SiteTag string `json:"siteTag" binding:"required"`
SiteSafety string `json:"siteSafety" binding:"required"`
Description string `json:"description" binding:"required"`
SiteRating int `json:"siteRating" binding:"required,gte=0,lte=10"`
}

type DelegateReviewCreationPayload struct {
Expand Down
14 changes: 14 additions & 0 deletions api/v1/domain/claim/claim.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package claim

import (
"github.com/gin-gonic/gin"
)

// ApplyRoutes applies router to gin Router
func ApplyRoutes(r *gin.RouterGroup) {
g := r.Group("/claim")
{
g.POST("start", startClaimDomain)
g.POST("finish", finishClaimDomain)
}
}
118 changes: 118 additions & 0 deletions api/v1/domain/claim/finish.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package claim

import (
"errors"
"fmt"
"net"
"net/http"

"github.com/NetSepio/gateway/api/middleware/auth/paseto"
"github.com/NetSepio/gateway/config/dbconfig"
"github.com/NetSepio/gateway/models"
"github.com/NetSepio/gateway/util/pkg/logwrapper"
"github.com/TheLazarusNetwork/go-helpers/httpo"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)

// finish claim, check if txt record in dns matches the domain name, if matches then add current user to admin
// verify txt using dig +short gmail.com txt check above code for refereance
func finishClaimDomain(c *gin.Context) {
db := dbconfig.GetDb()
var request FinishClaimDomainRequest
err := c.BindJSON(&request)
if err != nil {
httpo.NewErrorResponse(http.StatusBadRequest, fmt.Sprintf("payload is invalid %s", err)).SendD(c)
return
}

// fetch domainData from db
var domainData models.Domain
err = db.Model(&domainData).Where("id = ?", request.DomainId).First(&domainData).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
httpo.NewErrorResponse(http.StatusNotFound, "domain not found").SendD(c)
return
}
httpo.NewErrorResponse(http.StatusInternalServerError, "internal server error").SendD(c)
return
}
userId := c.GetString(paseto.CTX_USER_ID)
var claimTxt models.DomainClaim
err = db.Where("domain_id = ? and admin_id = ?", request.DomainId, userId).First(&claimTxt).Error
if err != nil {
httpo.NewErrorResponse(http.StatusForbidden, "claim record not found").SendD(c)
return
}

var domainAdmin models.DomainAdmin
err = db.Where("domain_id = ?", request.DomainId).First(&domainAdmin).Error
if err == nil {
httpo.NewErrorResponse(http.StatusForbidden, "domain is already claimed").SendD(c)
return
}
if err != gorm.ErrRecordNotFound {
logwrapper.Errorf("failed to query domain admin: %s", err)
httpo.NewErrorResponse(http.StatusNotFound, "domain admin not found").SendD(c)
return
}

txts, err := net.LookupTXT(domainData.DomainName)
if err != nil {
var dnsError *net.DNSError
if errors.As(err, &dnsError) {
if dnsError.IsNotFound {
httpo.NewErrorResponse(http.StatusNotFound, "record not found in dns").SendD(c)
return
}
}
logwrapper.Errorf("failed to lookup domain: %s", err)
httpo.NewErrorResponse(http.StatusInternalServerError, "internal server error").SendD(c)
return
}
if len(txts) == 0 {
httpo.NewErrorResponse(http.StatusNotFound, "txt records are empty").SendD(c)
return
}

validTxtFound := false
for _, txt := range txts {
if txt == *domainData.TxtValue {
validTxtFound = true
break
}
}
if !validTxtFound {
httpo.NewErrorResponse(400, "no valid txt record found").SendD(c)
return
}

// Add current user to admin
newDomainAdmin := models.DomainAdmin{
DomainId: request.DomainId,
AdminId: userId,
UpdatedById: userId,
}

// Insert into db
err = db.Transaction(func(tx *gorm.DB) error {
if err := tx.Create(&newDomainAdmin).Error; err != nil {
return fmt.Errorf("failed to create domain admin: %s", err)
}

// update domain set verified to true
if err := tx.Model(&domainData).Where("id = ?", request.DomainId).Update("verified", true).Update("claimable", false).Error; err != nil {
return fmt.Errorf("failed to update domain: %s", err)
}

return nil
})

if err != nil {
logwrapper.Errorf("failed to complete transaction: %s", err)
httpo.NewErrorResponse(http.StatusInternalServerError, "internal server error").SendD(c)
return
}

httpo.NewSuccessResponse(200, "domain claim completed").SendD(c)
}
72 changes: 72 additions & 0 deletions api/v1/domain/claim/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package claim

import (
"fmt"
"net/http"

"github.com/NetSepio/gateway/api/middleware/auth/paseto"
"github.com/NetSepio/gateway/config/dbconfig"
"github.com/NetSepio/gateway/models"
"github.com/NetSepio/gateway/util/pkg/logwrapper"
"github.com/TheLazarusNetwork/go-helpers/httpo"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"gorm.io/gorm"
)

func startClaimDomain(c *gin.Context) {
db := dbconfig.GetDb()
var request ClaimDomainRequest
err := c.BindJSON(&request)
if err != nil {
httpo.NewErrorResponse(http.StatusBadRequest, fmt.Sprintf("payload is invalid %s", err)).SendD(c)
return
}

var domain models.Domain
err = db.Where("id = ?", request.DomainId).First(&domain).Error
if err != nil {
httpo.NewErrorResponse(http.StatusNotFound, "domain not found").SendD(c)
return
}

if !domain.Claimable {
httpo.NewErrorResponse(http.StatusForbidden, "domain is not claimable").SendD(c)
return
}

txtValue := fmt.Sprintf("netsepio_verification=%s", uuid.NewString())
userId := c.GetString(paseto.CTX_USER_ID)
err = db.Transaction(func(tx *gorm.DB) error {
// insert txt record in new model
newClaimTxt := models.DomainClaim{
ID: uuid.NewString(),
DomainID: request.DomainId,
Txt: txtValue,
AdminId: userId,
}

//remove all txt claim records for this domain and current user as admin
if err := tx.Where("domain_id = ? and admin_id = ?", request.DomainId, userId).Delete(&models.DomainClaim{}).Error; err != nil {
return fmt.Errorf("failed to delete claim txt: %s", err)
}
// insert into db
if err := tx.Create(&newClaimTxt).Error; err != nil {
return fmt.Errorf("failed to create claim txt: %s", err)
}

return nil
})

if err != nil {
logwrapper.Errorf("failed to create claim txt: %s", err)
httpo.NewErrorResponse(http.StatusInternalServerError, "internal server error").SendD(c)
return
}
//return txt record
payload := ClaimDomainResponse{
TxtValue: txtValue,
}
httpo.NewSuccessResponseP(200, "domain claimed", payload).SendD(c)

}
13 changes: 13 additions & 0 deletions api/v1/domain/claim/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package claim

type ClaimDomainRequest struct {
DomainId string `json:"domainId" binding:"required"`
}

type ClaimDomainResponse struct {
TxtValue string `json:"txtValue"`
}

type FinishClaimDomainRequest struct {
DomainId string `json:"domainId" binding:"required"`
}
2 changes: 2 additions & 0 deletions api/v1/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package domain
import (
"github.com/NetSepio/gateway/api/middleware/auth/paseto"
"github.com/NetSepio/gateway/api/v1/domain/admin"
"github.com/NetSepio/gateway/api/v1/domain/claim"
"github.com/gin-gonic/gin"
)

Expand All @@ -17,6 +18,7 @@ func ApplyRoutes(r *gin.RouterGroup) {
g.DELETE("", deleteDomain)
g.PATCH("", patchDomain)
g.PATCH("/verify", verifyDomain)
claim.ApplyRoutes(g)
admin.ApplyRoutes(g)
}
}
21 changes: 14 additions & 7 deletions api/v1/domain/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func queryDomain(c *gin.Context) {
httpo.NewErrorResponse(http.StatusBadRequest, fmt.Sprintf("payload is invalid %s", err)).SendD(c)
return
}
limit := 10
limit := 12
offset := (*queryRequest.Page - 1) * limit
var domains []struct {
Id string `json:"id"`
Expand All @@ -39,13 +39,22 @@ func queryDomain(c *gin.Context) {
Blockchain string `json:"blockchain"`
CreatedBy string `json:"createdBy"`
CreatorName string `json:"creatorName"`
Claimable string `json:"claimable"`
TxtValue string `json:"txtValue,omitempty"`
}

model := db.Limit(10).Offset(offset).Model(&models.Domain{})
model := db.Limit(limit).Offset(offset).Model(&models.Domain{}).Order("title ASC")
if queryRequest.DomainName != "" {
model = model.
Where("domain_name like ?", fmt.Sprintf("%%%s%%", queryRequest.DomainName))
Where("domain_name like ?", fmt.Sprintf("%%%s%%", queryRequest.DomainName)).Where(&models.Domain{Id: queryRequest.DomainId})
}

if queryRequest.VerifiedWithClaimable {
model = model.
Where("verified = true or claimable = true")
} else {
model = model.
Where(&models.Domain{Verified: queryRequest.Verified})
}

if queryRequest.OnlyAdmin {
Expand All @@ -54,9 +63,8 @@ func queryDomain(c *gin.Context) {
httpo.NewErrorResponse(http.StatusBadRequest, "auth token required if onlyAdmin is true").SendD(c)
return
}
if err := model.
Where(&models.Domain{Verified: queryRequest.Verified, Id: queryRequest.DomainId}).Where("da.admin_id = ?", userId).
Select("id, domain_name, verified, created_at, title, headline, description, cover_image_hash, logo_hash, category, blockchain, created_by_id created_by, u.name creator_name, txt_value").
if err := model.Where("da.admin_id = ?", userId).
Select("id, domain_name, verified, created_at, title, headline, description, cover_image_hash, logo_hash, category, blockchain, created_by_id created_by, u.name creator_name, txt_value, claimable").
Joins("INNER JOIN users u ON u.user_id = created_by_id").
Joins("INNER JOIN domain_admins da ON da.domain_id = domains.id").
Find(&domains).
Expand All @@ -67,7 +75,6 @@ func queryDomain(c *gin.Context) {
}
} else {
if err := model.
Where(&models.Domain{Verified: queryRequest.Verified, Id: queryRequest.DomainId}).
Select("id, domain_name, verified, created_at, title, headline, description, cover_image_hash, logo_hash, category, blockchain, created_by_id created_by, u.name creator_name").
Joins("INNER JOIN users u ON u.user_id = created_by_id").
Find(&domains).
Expand Down
11 changes: 6 additions & 5 deletions api/v1/domain/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ type VerifyDomainRequest struct {
}

type GetDomainsQuery struct {
DomainId string `form:"domainId"`
DomainName string `form:"domainName"`
Verified *bool `form:"verified"`
OnlyAdmin bool `form:"onlyAdmin"`
Page *int `form:"page" binding:"required,min=1"`
DomainId string `form:"domainId"`
DomainName string `form:"domainName"`
VerifiedWithClaimable bool `form:"verifiedWithClaimable"`
Verified *bool `form:"verified"`
OnlyAdmin bool `form:"onlyAdmin"`
Page *int `form:"page" binding:"required,min=1"`
}

type PatchDomainRequest struct {
Expand Down
Loading

0 comments on commit 255500e

Please sign in to comment.