Skip to content

Instantly share code, notes, and snippets.

@freshteapot
Last active December 1, 2023 10:01
Show Gist options
  • Save freshteapot/0be8b778218e54c4b179505433309ca6 to your computer and use it in GitHub Desktop.
Save freshteapot/0be8b778218e54c4b179505433309ca6 to your computer and use it in GitHub Desktop.
Example ways of handling tier lockdown to encourage upgrades

Example of access to slideshow

func HasAccessBasedOnTierLimitsOnCreate(tier string, userInfo user.UserPreference, kind string) bool {
	if tier != "free" {
		return true
	}

	records := userInfo.Usage.Stats
	// assume keys exist?
	if _, ok := records[stats.SystemMissingKey]; ok {
		// Assume present means an issue
		return true
	}

	tolerance := 1
	limit := 1
	limitToAllow := limit + tolerance

	if kind == KindPDF {
		return records["list.slideshow.export.v7.total"] <= limitToAllow
	}

	if kind == KindVideo {
		return records["list.slideshow.export.v8.total"] <= limitToAllow
	}

	return false
}

Usage

userInfo := c.Get("userInfo").(user.UserPreference)
grant := HasAccessBasedOnTierLimitsOnCreate("free", userInfo, inputKind)
if !grant {
	response := api.HTTPResponseMessage{
		Message: "Upgrade tier",
	}
	return c.JSON(http.StatusPaymentRequired, response)
}

Example of access check on save list

func HasAccessBasedOnTierLimitsOnSaveAlist(tier string, records map[string]int, kind string) bool {
	if tier != "free" {
		return true
	}

	if _, ok := records[stats.SystemMissingKey]; ok {
		// Assume present means an issue
		return true
	}

	// assume keys exist?
	if records["list.slideshow.export.v7.total"] > 1 {
		return false
	}

	if records["list.slideshow.export.v8.total"] > 1 {
		return false
	}

	return false
}

Usage

userStats := c.Get("userStats").(map[string]int)

grant := HasAccessBasedOnTierLimitsOnSaveAlist("free", userStats, aList.Kind)
if !grant {
	response := api.HTTPResponseMessage{
	Message: "Upgrade tier",
	}
	return c.JSON(http.StatusPaymentRequired, response)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment