1package server
2
3import (
4 "github.com/haileyok/cocoon/internal/helpers"
5 "github.com/haileyok/cocoon/models"
6 "github.com/ipfs/go-cid"
7 "github.com/labstack/echo/v4"
8)
9
10type ComAtprotoServerCheckAccountStatusResponse struct {
11 Activated bool `json:"activated"`
12 ValidDid bool `json:"validDid"`
13 RepoCommit string `json:"repoCommit"`
14 RepoRev string `json:"repoRev"`
15 RepoBlocks int64 `json:"repoBlocks"`
16 IndexedRecords int64 `json:"indexedRecords"`
17 PrivateStateValues int64 `json:"privateStateValues"`
18 ExpectedBlobs int64 `json:"expectedBlobs"`
19 ImportedBlobs int64 `json:"importedBlobs"`
20}
21
22func (s *Server) handleServerCheckAccountStatus(e echo.Context) error {
23 ctx := e.Request().Context()
24 logger := s.logger.With("name", "handleServerCheckAccountStatus")
25
26 urepo := e.Get("repo").(*models.RepoActor)
27
28 resp := ComAtprotoServerCheckAccountStatusResponse{
29 Activated: true, // TODO: should allow for deactivation etc.
30 ValidDid: true, // TODO: should probably verify?
31 RepoRev: urepo.Rev,
32 ImportedBlobs: 0, // TODO: ???
33 }
34
35 rootcid, err := cid.Cast(urepo.Root)
36 if err != nil {
37 logger.Error("error casting cid", "error", err)
38 return helpers.ServerError(e, nil)
39 }
40 resp.RepoCommit = rootcid.String()
41
42 type CountResp struct {
43 Ct int64
44 }
45
46 var blockCtResp CountResp
47 if err := s.db.Raw(ctx, "SELECT COUNT(*) AS ct FROM blocks WHERE did = ?", nil, urepo.Repo.Did).Scan(&blockCtResp).Error; err != nil {
48 logger.Error("error getting block count", "error", err)
49 return helpers.ServerError(e, nil)
50 }
51 resp.RepoBlocks = blockCtResp.Ct
52
53 var recCtResp CountResp
54 if err := s.db.Raw(ctx, "SELECT COUNT(*) AS ct FROM records WHERE did = ?", nil, urepo.Repo.Did).Scan(&recCtResp).Error; err != nil {
55 logger.Error("error getting record count", "error", err)
56 return helpers.ServerError(e, nil)
57 }
58 resp.IndexedRecords = recCtResp.Ct
59
60 var blobCtResp CountResp
61 if err := s.db.Raw(ctx, "SELECT COUNT(*) AS ct FROM blobs WHERE did = ?", nil, urepo.Repo.Did).Scan(&blobCtResp).Error; err != nil {
62 logger.Error("error getting record count", "error", err)
63 return helpers.ServerError(e, nil)
64 }
65 resp.ExpectedBlobs = blobCtResp.Ct
66
67 return e.JSON(200, resp)
68}