Skip to content
This repository has been archived by the owner on Jan 13, 2021. It is now read-only.

Commit

Permalink
Merge branch 'master' into swarmy
Browse files Browse the repository at this point in the history
  • Loading branch information
bonedaddy authored Apr 9, 2020
2 parents 79d8e36 + 95c711b commit 8a6e82d
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 43 deletions.
12 changes: 3 additions & 9 deletions api/v2/routes_rtfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,15 +364,9 @@ func (api *API) extendPin(c *gin.Context) {
Fail(c, err)
return
}
// find usage model
usage, err := api.usage.FindByUserName(username)
usg, err := api.usage.FindByUserName(username)
if err != nil {
api.LogError(c, err, eh.UserSearchError)(http.StatusBadRequest)
return
}
// make sure they aren't a free account
if usage.Tier == models.Free {
Fail(c, errors.New("free accounts are not allowed to extend pin times"))
api.LogError(c, err, eh.UserSearchError)
return
}
// find upload
Expand All @@ -382,7 +376,7 @@ func (api *API) extendPin(c *gin.Context) {
return
}
// ensure even with pin time extension, it wont breach two year limit
if err := api.ensureTwoYearMax(upload, holdTimeInt); err != nil {
if err := api.ensureLEMaxPinTime(upload, holdTimeInt, usg.Tier); err != nil {
Fail(c, err)
return
}
Expand Down
9 changes: 5 additions & 4 deletions api/v2/routes_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,16 @@ func (api *API) handleUserCreate(c *gin.Context, forms map[string]string, create
es := queue.EmailSend{
Subject: emailSubject,
Content: fmt.Sprintf(
"%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
"%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
"Thanks for signing up with Temporal, before you get started it's important we discuss our pinning system.\n",
"When uploading to Temporal you must specify a \"hold time\" which tells our system how long your data should be around for.\n",
"When you're using Temporal via the playground, or the API directly you can configure this for up to 24 months with paid accounts, and up to 1 month for free accounts.\n",
"When you're using Temporal via the playground, or the API directly you can configure this for up to 24 months with paid accounts, and up to 12 months for free accounts.\n",
"Temporal’s free tier offers 3GB of storage on the house, paid tier rates are just $0.07/GB and partner tier rates are $0.05/GB.\n",
"<br>",
"<br>",
"When using Temporal through third-party implementations like our IPFS HTTP API reverse proxy, or the ENS management app, we use a default hold time of 1 month.\n",
"For example if you used the ENS management app to upload your website, and want it to stick around for longer than 1 month you need to extend your pin.\n",
"When using Temporal through third-party implementations like our IPFS HTTP API reverse proxy, or the ENS management app, we use a default hold time of 12 months for free users and 1 month for paid users.\n",
"We use 1 month for paid users because being in the paid tier means you have to pay for the data consumption and we don't want to overcharge paid users.\n",
"For example if you used the ENS management app to upload your website, and want it to stick around for longer than the default duration you need to extend the pin.\n",
"Pin extension can be done via the <a href=\"https://play2.temporal.cloud\">Temporal Playground</a> or via the API.\n",
"<br>",
"<br>",
Expand Down
24 changes: 14 additions & 10 deletions api/v2/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ func (api *API) extractPostForms(c *gin.Context, formNames ...string) (map[strin
// returning an int64 type of the provided hold time
func (api *API) validateHoldTime(username, holdTime string) (int64, error) {
var (
// 1 month
freeHoldTimeLimitInMonths int64 = 1
// 1 year
freeHoldTimeLimitInMonths int64 = 12
// two years
nonFreeHoldTimeLimitInMonths int64 = 24
)
Expand All @@ -248,20 +248,24 @@ func (api *API) validateHoldTime(username, holdTime string) (int64, error) {
return 0, err
}
if usageTier.Tier == models.Free && holdTimeInt > freeHoldTimeLimitInMonths {
return 0, errors.New("free accounts are limited to maximum hold times of 1 month")
return 0, errors.New("free accounts are limited to maximum hold times of 12 month")
} else if usageTier.Tier != models.Free && holdTimeInt > nonFreeHoldTimeLimitInMonths {
return 0, errors.New("non free accounts are limited to a maximum hold time of 24 months")
}
return holdTimeInt, nil
}

func (api *API) ensureTwoYearMax(upload *models.Upload, holdTime int64) error {
// get current time
now := time.Now()
// get future time while factoring for additional hold time
then := upload.GarbageCollectDate.AddDate(0, int(holdTime), 0)
// get the time difference and ensure its less than the 2 year limit
if then.Sub(now).Hours() > 17520 {
func (api *API) ensureLEMaxPinTime(upload *models.Upload, holdTime int64, tier models.DataUsageTier) error {
var limit time.Time
switch tier {
case models.Free:
limit = time.Now().AddDate(1, 0, 0)
case models.Paid, models.Partner, models.WhiteLabeled:
limit = time.Now().AddDate(2, 0, 0)
default:
return errors.New("invalid usage tier")
}
if upload.GarbageCollectDate.AddDate(0, int(holdTime), 0).After(limit) {
return errors.New(eh.MaxHoldTimeError)
}
return nil
Expand Down
49 changes: 33 additions & 16 deletions api/v2/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,40 +133,57 @@ func Test_Ensure_Two_Year_Max(t *testing.T) {
randUtils := utils.GenerateRandomUtils()
randString := randUtils.GenerateString(32, utils.LetterBytes)
um := models.NewUploadManager(db)
upload, err := um.NewUpload(
randString,
"file",
models.UploadOptions{
Username: "testuser",
NetworkName: "public",
HoldTimeInMonths: 1,
Encrypted: false,
},
)
if err != nil {
t.Fatal(err)
}
type args struct {
holdTimeInMonths int64
upload *models.Upload
tier models.DataUsageTier
}
tests := []struct {
name string
args args
wantErr bool
}{
{"12-Months", args{12, upload}, false},
{"22-Months", args{22, upload}, false},
{"25-Months", args{25, upload}, true},
{"12-Months-paid", args{12, models.Paid}, false},
{"22-Months-paid", args{22, models.Paid}, false},
{"25-Months-paid", args{25, models.Paid}, true},
{"10-Months-free", args{10, models.Free}, false},
{"11-Months-free", args{11, models.Free}, false},
{"12-Months-free", args{12, models.Free}, true},
{"22-Months-free", args{22, models.Free}, true},
{"25-Months-free", args{25, models.Free}, true},
{"12-Months-partner", args{12, models.Partner}, false},
{"22-Months-partner", args{22, models.Partner}, false},
{"25-Months-partner", args{25, models.Partner}, true},
{"12-Months-whitelabeled", args{12, models.WhiteLabeled}, false},
{"22-Months-whitelabeled", args{22, models.WhiteLabeled}, false},
{"25-Months-whitelabeled", args{25, models.WhiteLabeled}, true},
{"not-a-real-tier", args{12, models.DataUsageTier("thetierisalie")}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := api.ensureTwoYearMax(
tt.args.upload,
upload, err := um.NewUpload(
randString,
"file",
models.UploadOptions{
Username: "testuser",
NetworkName: "public",
HoldTimeInMonths: 1,
Encrypted: false,
},
)
if err != nil {
t.Fatal(err)
}
if err := api.ensureLEMaxPinTime(
upload,
tt.args.holdTimeInMonths,
tt.args.tier,
); (err != nil) != tt.wantErr {
t.Fatalf("ensureTwoYearMax err = %v, wantErr %v", err, tt.wantErr)
}
um.DB.Unscoped().Delete(upload)
})
}
}
2 changes: 1 addition & 1 deletion eh/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ const (
// PinExtendError is an error message used when someone attempts to extend the pin for content they haven't uploaded
PinExtendError = "failed to extend pin duration, this likely means you haven't actually uploaded this content before"
// MaxHoldTimeError is an error message when the current hold time value would breach set pin time limits
MaxHoldTimeError = "a hold time of this long would result in a longer maximum pin time of 2 years, please reduce your hold time and try again"
MaxHoldTimeError = "a hold time of this long would result in a longer maximum pin time than what your account allow, please reduce your hold time and try again"
// HostNameNotFoundError is an error message when api server has not hostname
HostNameNotFoundError = "an api host has not hostname, please set hostname"
)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/RTradeLtd/cmd/v2 v2.1.0
github.com/RTradeLtd/config/v2 v2.2.0
github.com/RTradeLtd/crypto/v2 v2.1.1
github.com/RTradeLtd/database/v2 v2.7.3-rc1
github.com/RTradeLtd/database/v2 v2.7.4
github.com/RTradeLtd/entropy-mnemonics v0.0.0-20170316012907-7b01a644a636
github.com/RTradeLtd/go-ipfs-api v0.0.0-20190522213636-8e3700e602fd
github.com/RTradeLtd/gpaginator v0.0.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ github.com/RTradeLtd/crypto v2.0.0+incompatible h1:3+UEo0upD0p3A+7yLJ14UJpT6aZEh
github.com/RTradeLtd/crypto v2.0.0+incompatible/go.mod h1:xhKwg748pxs2as6Ts65TiBBFrYzntioTqBIZEa1BUio=
github.com/RTradeLtd/crypto/v2 v2.1.1 h1:P59zYkkNkl6K1KiTRvW52AYwLvwmtzuzZ9+AjLWmKsU=
github.com/RTradeLtd/crypto/v2 v2.1.1/go.mod h1:saIQ67Btn4JWsOdzjn9U6Dl+aZlg+YKgg4RsQKXxjf4=
github.com/RTradeLtd/database/v2 v2.7.3-rc1 h1:kHj0H1uwXqMhAj4S3qNelRA4Mee0h3+GLa0kF5kLwHw=
github.com/RTradeLtd/database/v2 v2.7.3-rc1/go.mod h1:7t4vOzLgmvIz9qnZC0UY0cwdbpkVIhE6I9BaCKV4/us=
github.com/RTradeLtd/database/v2 v2.7.4 h1:7kYdfjMpvnccrzxt/l29DUTRXsfiWBzuNmGsY+BmVJA=
github.com/RTradeLtd/database/v2 v2.7.4/go.mod h1:2Q64z+Gdas9wgMpO72iKYW3tCsHs2SJHPzdROcj6MLE=
github.com/RTradeLtd/entropy-mnemonics v0.0.0-20170316012907-7b01a644a636 h1:i/+1LBA+YMfD1m9UnQP52A7S6y2U3C0xpMBehPkDRug=
github.com/RTradeLtd/entropy-mnemonics v0.0.0-20170316012907-7b01a644a636/go.mod h1:zpzHNRdCMCG9PM9QO5jVSldXCRMJ7lY42yJ5TEe//7M=
github.com/RTradeLtd/go-ipfs-api v0.0.0-20190308091756-8b7099fd5e21/go.mod h1:ipDfy60LjYDddlX/zluSwRVtfGR0EB1HqADazGNMUmE=
Expand Down
3 changes: 3 additions & 0 deletions utils/cmc.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ REFRESH:
}
if quotemap["quote"] != nil {
b, err = json.Marshal(quotemap["quote"])
if err != nil {
return pricer.coins[coin].price, err
}
if err := json.Unmarshal(b, &usdmap); err != nil {
fmt.Println("error: ", err)
return pricer.coins[coin].price, err
Expand Down

0 comments on commit 8a6e82d

Please sign in to comment.