Skip to content

Commit

Permalink
clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
ASAPSegfault committed Jul 31, 2024
1 parent 5443080 commit 086d193
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
15 changes: 13 additions & 2 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,18 @@ func viewWebsite(scAddress string, config *pkgConfig.Config) error {
logger.Infof("Website owner: %s", owner)

zipFile, err := webmanager.RequestWebsite(scAddress, &config.NetworkInfos)

firstCreationTimestamp, err := website.GetFirstCreationTimestamp(&config.NetworkInfos, address)

Check failure on line 200 in cmd/cli/main.go

View workflow job for this annotation

GitHub Actions / test

undefined: address

Check failure on line 200 in cmd/cli/main.go

View workflow job for this annotation

GitHub Actions / lint

undefined: address

Check failure on line 200 in cmd/cli/main.go

View workflow job for this annotation

GitHub Actions / build and upload artifacts (ubuntu-20.04, amd64, linux)

undefined: address

Check failure on line 200 in cmd/cli/main.go

View workflow job for this annotation

GitHub Actions / build and upload artifacts (macos-14, arm64, darwin)

undefined: address
if err != nil {
logger.Warnf("failed to get first creation timestamp of %s: %v", address, err)

Check failure on line 202 in cmd/cli/main.go

View workflow job for this annotation

GitHub Actions / test

undefined: address

Check failure on line 202 in cmd/cli/main.go

View workflow job for this annotation

GitHub Actions / lint

undefined: address

Check failure on line 202 in cmd/cli/main.go

View workflow job for this annotation

GitHub Actions / build and upload artifacts (ubuntu-20.04, amd64, linux)

undefined: address

Check failure on line 202 in cmd/cli/main.go

View workflow job for this annotation

GitHub Actions / build and upload artifacts (macos-14, arm64, darwin)

undefined: address
}

lastUpdateTimestamp, err := website.GetLastUpdateTimestamp(&config.NetworkInfos, address)

Check failure on line 205 in cmd/cli/main.go

View workflow job for this annotation

GitHub Actions / test

undefined: address

Check failure on line 205 in cmd/cli/main.go

View workflow job for this annotation

GitHub Actions / lint

undefined: address

Check failure on line 205 in cmd/cli/main.go

View workflow job for this annotation

GitHub Actions / build and upload artifacts (ubuntu-20.04, amd64, linux)

undefined: address

Check failure on line 205 in cmd/cli/main.go

View workflow job for this annotation

GitHub Actions / build and upload artifacts (macos-14, arm64, darwin)

undefined: address
if err != nil {
logger.Warnf("failed to get last update timestamp of %s: %v", address, err)

Check failure on line 207 in cmd/cli/main.go

View workflow job for this annotation

GitHub Actions / test

undefined: address

Check failure on line 207 in cmd/cli/main.go

View workflow job for this annotation

GitHub Actions / lint

undefined: address

Check failure on line 207 in cmd/cli/main.go

View workflow job for this annotation

GitHub Actions / build and upload artifacts (ubuntu-20.04, amd64, linux)

undefined: address

Check failure on line 207 in cmd/cli/main.go

View workflow job for this annotation

GitHub Actions / build and upload artifacts (macos-14, arm64, darwin)

undefined: address
}

websiteBytes, err := website.Fetch(&config.NetworkInfos, address)

Check failure on line 210 in cmd/cli/main.go

View workflow job for this annotation

GitHub Actions / test

websiteBytes declared and not used

Check failure on line 210 in cmd/cli/main.go

View workflow job for this annotation

GitHub Actions / test

undefined: address

Check failure on line 210 in cmd/cli/main.go

View workflow job for this annotation

GitHub Actions / lint

websiteBytes declared and not used

Check failure on line 210 in cmd/cli/main.go

View workflow job for this annotation

GitHub Actions / lint

undefined: address (typecheck)

Check failure on line 210 in cmd/cli/main.go

View workflow job for this annotation

GitHub Actions / build and upload artifacts (ubuntu-20.04, amd64, linux)

websiteBytes declared and not used

Check failure on line 210 in cmd/cli/main.go

View workflow job for this annotation

GitHub Actions / build and upload artifacts (ubuntu-20.04, amd64, linux)

undefined: address

Check failure on line 210 in cmd/cli/main.go

View workflow job for this annotation

GitHub Actions / build and upload artifacts (macos-14, arm64, darwin)

websiteBytes declared and not used

Check failure on line 210 in cmd/cli/main.go

View workflow job for this annotation

GitHub Actions / build and upload artifacts (macos-14, arm64, darwin)

undefined: address
if err != nil {
return fmt.Errorf("failed to request website: %v", err)
}
Expand All @@ -207,10 +219,9 @@ func viewWebsite(scAddress string, config *pkgConfig.Config) error {
return fmt.Errorf("failed to get file %s from zip: %v", fileName, err)
}

logger.Infof("viewing content for %s:\n %s", scAddress, indexFile)
prettyPrintUnixTimestamp(int64(firstCreationTimestamp), int64(lastUpdateTimestamp))

logger.Infof("%s content:\n %s", fileName, content)
logger.Infof("viewing content for %s:\n %s", scAddress, indexFile)

return nil
}
Expand Down
3 changes: 0 additions & 3 deletions pkg/website/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"

"github.com/massalabs/DeWeb/pkg/config"
"github.com/massalabs/station/pkg/logger"
"github.com/massalabs/station/pkg/node/sendoperation"
"github.com/massalabs/station/pkg/node/sendoperation/signer"
"github.com/massalabs/station/pkg/onchain"
Expand All @@ -31,7 +30,5 @@ func Delete(conf *config.Config, address string) (*string, error) {
return nil, fmt.Errorf("deleting website: %w", err)
}

logger.Infof("Event received : %s", res.Event)

return &res.OperationResponse.OperationID, nil
}
30 changes: 21 additions & 9 deletions pkg/website/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,33 +65,45 @@ func fetchAllChunks(client *node.Client, websiteAddress string, chunkNumber int3
}

// GetOwner retrieves the owner of the website.
func GetOwner(network *config.NetworkInfos, websiteAddress string) (string, uint64, uint64, error) {
func GetOwner(network *config.NetworkInfos, websiteAddress string) (string, error) {
client := node.NewClient(network.NodeURL)

ownerResponse, err := node.FetchDatastoreEntry(client, websiteAddress, convert.ToBytes(ownerKey))
if err != nil {
return "", 0, 0, fmt.Errorf("fetching website owner: %w", err)
return "", fmt.Errorf("fetching website owner: %w", err)
}

return string(ownerResponse.FinalValue), nil
}

func GetFirstCreationTimestamp(network *config.NetworkInfos, websiteAddress string) (uint64, error) {
client := node.NewClient(network.NodeURL)

firstCreationTimestampResponse, err := node.FetchDatastoreEntry(client, websiteAddress, convert.ToBytes(firstCreationTimestampKey))
if err != nil {
return "", 0, 0, fmt.Errorf("fetching website first creation timestamp: %w", err)
return 0, fmt.Errorf("fetching website first creation timestamp: %w", err)
}

lastUpdateTimestampResponse, err := node.FetchDatastoreEntry(client, websiteAddress, convert.ToBytes(lastUpdateTimestampKey))
castedFCTimestamp, err := convert.BytesToU64(firstCreationTimestampResponse.FinalValue)
if err != nil {
return "", 0, 0, fmt.Errorf("fetching website last update timestamp: %w", err)
return 0, fmt.Errorf("converting website first creation timestamp: %w", err)
}

castedFCTimestamp, err := convert.BytesToU64(firstCreationTimestampResponse.FinalValue)
return castedFCTimestamp, nil
}

func GetLastUpdateTimestamp(network *config.NetworkInfos, websiteAddress string) (uint64, error) {
client := node.NewClient(network.NodeURL)

lastUpdateTimestampResponse, err := node.FetchDatastoreEntry(client, websiteAddress, convert.ToBytes(firstCreationTimestampKey))
if err != nil {
return "", 0, 0, fmt.Errorf("converting website first creation timestamp: %w", err)
return 0, fmt.Errorf("fetching website last update timestamp: %w", err)
}

castedLUTimestamp, err := convert.BytesToU64(lastUpdateTimestampResponse.FinalValue)
if err != nil {
return "", 0, 0, fmt.Errorf("converting website last update timestamp: %w", err)
return 0, fmt.Errorf("converting website last update timestamp: %w", err)
}

return string(ownerResponse.FinalValue), castedFCTimestamp, castedLUTimestamp, nil
return castedLUTimestamp, nil
}

0 comments on commit 086d193

Please sign in to comment.