Skip to content

Commit b674b9d

Browse files
committed
Merge branch 'develop'
2 parents 226d405 + 0065ed8 commit b674b9d

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

cmd/lambda_worker/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func handler(ctx context.Context, sqs_event events.SQSEvent) error {
6767
arweaveMsgs = append(arweaveMsgs, &message)
6868
case types.QueueActions.Revalidate:
6969
if err := revalidate_single(ctx, &message); err != nil {
70-
return err
70+
fmt.Printf("error revalidating proof record %d: %s\n", message.ProofID, err)
7171
}
7272
default:
7373
logrus.Warnf("unsupported queue action: %s", message.Action)
@@ -217,7 +217,7 @@ func arweave_bundle_single(pc *model.ProofChain, previous *model.ProofChain) (*a
217217

218218
func revalidate_single(ctx context.Context, message *types.QueueMessage) error {
219219
proof := model.Proof{}
220-
tx := model.DB.Preload("ProofChain.Previous").First(&proof, message.ProofID)
220+
tx := model.DB.Preload("ProofChain").Preload("ProofChain.Previous").Where("id = ?", message.ProofID).First(&proof)
221221
if tx.Error != nil {
222222
return xerrors.Errorf("%w", tx.Error)
223223
}

controller/proof_exists.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func proofExists(c *gin.Context) {
5656
errorResp(c, http.StatusNotFound, xerrors.Errorf("Record not found for %s: %s", req.Platform, req.Identity))
5757
return
5858
}
59+
go triggerRevalidate(found.ID)
5960

6061
c.JSON(http.StatusOK, ProofExistsResponse{
6162
CreatedAt: strconv.FormatInt(found.CreatedAt.Unix(), 10),

controller/proof_query.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/gin-gonic/gin"
99
"github.com/nextdotid/proof-server/model"
1010
"github.com/nextdotid/proof-server/types"
11+
"github.com/nextdotid/proof-server/util/sqs"
1112
"github.com/samber/lo"
1213
"golang.org/x/xerrors"
1314
)
@@ -123,6 +124,12 @@ func performProofQuery(req ProofQueryRequest) ([]ProofQueryResponseSingle, Proof
123124
if tx.Error != nil || tx.RowsAffected == int64(0) || len(proofs) == 0 {
124125
return result, pagination
125126
}
127+
// Trigger revalidate procedure
128+
lo.ForEach(proofs, func(proof model.Proof, i int) {
129+
if proof.IsOutdated() {
130+
go triggerRevalidate(proof.ID)
131+
}
132+
})
126133

127134
personas := lo.Map(proofs, func(p model.Proof, _index int) string {
128135
return p.Persona
@@ -166,3 +173,16 @@ func performProofQuery(req ProofQueryRequest) ([]ProofQueryResponseSingle, Proof
166173
}
167174
return result, pagination
168175
}
176+
177+
func triggerRevalidate(proofID int64) error {
178+
msg := types.QueueMessage{
179+
Action: types.QueueActions.Revalidate,
180+
ProofID: proofID,
181+
}
182+
183+
if err := sqs.Send(msg); err != nil {
184+
return xerrors.Errorf("Failed to send message to queue: %w", err)
185+
}
186+
187+
return nil
188+
}

model/proof.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
"golang.org/x/xerrors"
99
)
1010

11+
// EXPIRED_IN is the time after which a proof is considered expired and should perform revalidate.
12+
const EXPIRED_IN = time.Hour * 24 * 3
13+
1114
// Proof is final proof state of a user (persona).
1215
type Proof struct {
1316
ID int64 `gorm:"primarykey"`
@@ -39,6 +42,11 @@ func FindAllProofByPersona(persona any) (proofs []Proof, err error) {
3942
return proofs, nil
4043
}
4144

45+
// IsOutdated returns true if proof is outdated and should do a revalidate.
46+
func (proof *Proof) IsOutdated() bool {
47+
return proof.LastCheckedAt.Add(EXPIRED_IN).Before(time.Now())
48+
}
49+
4250
// Revalidate validates current proof, will update `IsValid` and
4351
// `LastCheckedAt`. Must be used after `DB.Preload("ProofChain")`.
4452
func (proof *Proof) Revalidate() (result bool, err error) {

0 commit comments

Comments
 (0)