Beispiel #1
0
// getHistory returns browse history.
func getHistory(ctx *middleware.Context) []*models.PkgInfo {
	pairs := strings.Split(ctx.GetCookie("user_history"), "|")
	pkgs := make([]*models.PkgInfo, 0, len(pairs))

	for _, pair := range pairs {
		infos := strings.Split(pair, ":")
		if len(infos) != 2 {
			continue
		}

		pid := com.StrTo(infos[0]).MustInt64()
		if pid == 0 {
			continue
		}

		pinfo, _ := models.GetPkgInfoById(pid)
		if pinfo == nil {
			continue
		}

		pinfo.LastView = com.StrTo(infos[1]).MustInt64()
		pkgs = append(pkgs, pinfo)
	}
	return pkgs
}
Beispiel #2
0
// updateHistory updates browser history.
func updateHistory(ctx *middleware.Context, id int64) {
	pairs := make([]string, 1, 10)
	pairs[0] = com.ToStr(id) + ":" + com.ToStr(time.Now().UTC().Unix())

	count := 0
	for _, pair := range strings.Split(ctx.GetCookie("user_history"), "|") {
		infos := strings.Split(pair, ":")
		if len(infos) != 2 {
			continue
		}

		pid := com.StrTo(infos[0]).MustInt64()
		if pid == 0 || pid == id {
			continue
		}

		pairs = append(pairs, pair)

		count++
		if count == 9 {
			break
		}
	}
	ctx.SetCookie("user_history", strings.Join(pairs, "|"), 9999999)
}