Esempio n. 1
0
func home_index(ctx *goku.HttpContext) goku.ActionResulter {
	u, ok := ctx.Data["user"]
	if !ok || u == nil {
		return ctx.Redirect("/discover")
	}
	user := u.(*models.User)
	if user.FriendCount+user.FtopicCount < 1 {
		return home_guideForNew(ctx)
	}
	ot := ctx.Get("o")
	if ot == "" {
		ot = "hot"
	}
	ctx.ViewData["Order"] = ot
	links, _ := models.Link_ForUser(user.Id, ot, 1, golink.PAGE_SIZE) //models.Link_GetByPage(1, 20)
	ctx.ViewData["Links"] = models.Link_ToVLink(links, ctx)
	ctx.ViewData["HasMoreLink"] = len(links) >= golink.PAGE_SIZE

	// 最新链接的未读提醒
	if ot == "hot" {
		newestUnreadCount, _ := models.NewestLinkUnread_Friends(user.Id, user.LastReadFriendLinkId)
		ctx.ViewData["NewestUnreadCount"] = models.NewestLinkUnread_ToString(user.Id, newestUnreadCount)
	} else if ot == "time" && links != nil && len(links) > 0 {
		models.NewestLinkUnread_UpdateForUser(user.Id, links[0].Id)
	}

	return ctx.View(nil)
}
Esempio n. 2
0
// 发现 首页
func discover_index(ctx *goku.HttpContext) goku.ActionResulter {
	ot := ctx.Get("o")
	if ot == "" {
		ot = "hot"
	}
	dt, _ := strconv.Atoi(ctx.Get("dt"))
	ctx.ViewData["Order"] = ot
	links, _ := models.LinkForHome_GetByPage(ot, dt, 1, golink.PAGE_SIZE)
	ctx.ViewData["Links"] = models.Link_ToVLink(links, ctx)
	ctx.ViewData["TopTab"] = "discover"
	ctx.ViewData["HasMoreLink"] = len(links) >= golink.PAGE_SIZE

	// 最新链接的未读提醒
	var userId, lastReadLinkId int64
	unreadCookieName := "newestUnrLinkId"
	u, ok := ctx.Data["user"]
	if ok && u != nil {
		user := u.(*models.User)
		userId = user.Id
		lastReadLinkId = user.LastReadLinkId
	} else {
		// 从Cook读取最后一次阅读的链接id
		cLastReadLinkId, err := ctx.Request.Cookie(unreadCookieName)
		if err == nil {
			lastReadLinkId, _ = strconv.ParseInt(cLastReadLinkId.Value, 10, 64)
		}
	}
	if ot == "hot" {
		newestUnreadCount, _ := models.NewestLinkUnread_All(userId, lastReadLinkId)
		ctx.ViewData["NewestUnreadCount"] = models.NewestLinkUnread_ToString(userId, newestUnreadCount)
	} else if ot == "time" && links != nil && len(links) > 0 {
		if userId > 0 {
			models.NewestLinkUnread_UpdateForAll(userId, links[0].Id)
		} else {
			c := &http.Cookie{
				Name:     unreadCookieName,
				Value:    fmt.Sprintf("%d", links[0].Id),
				Expires:  time.Now().AddDate(0, 1, 0),
				Path:     "/",
				HttpOnly: true,
			}
			ctx.SetCookie(c)
		}
	}

	return ctx.Render("/home/index", nil)
}