diff options
Diffstat (limited to 'routers/user/home.go')
-rw-r--r-- | routers/user/home.go | 104 |
1 files changed, 35 insertions, 69 deletions
diff --git a/routers/user/home.go b/routers/user/home.go index 96202ed080..39ceaed2e4 100644 --- a/routers/user/home.go +++ b/routers/user/home.go @@ -52,6 +52,37 @@ func getDashboardContextUser(ctx *middleware.Context) *models.User { return ctxUser } +func retrieveFeeds(ctx *middleware.Context, uid, offset int64, isProfile bool) { + actions, err := models.GetFeeds(uid, offset, isProfile) + if err != nil { + ctx.Handle(500, "GetFeeds", err) + return + } + + // Check access of private repositories. + feeds := make([]*models.Action, 0, len(actions)) + unameAvatars := make(map[string]string) + for _, act := range actions { + // Cache results to reduce queries. + _, ok := unameAvatars[act.ActUserName] + if !ok { + u, err := models.GetUserByName(act.ActUserName) + if err != nil { + if models.IsErrUserNotExist(err) { + continue + } + ctx.Handle(500, "GetUserByName", err) + return + } + unameAvatars[act.ActUserName] = u.AvatarLink() + } + + act.ActAvatar = unameAvatars[act.ActUserName] + feeds = append(feeds, act) + } + ctx.Data["Feeds"] = feeds +} + func Dashboard(ctx *middleware.Context) { ctx.Data["Title"] = ctx.Tr("dashboard") ctx.Data["PageIsDashboard"] = true @@ -100,46 +131,10 @@ func Dashboard(ctx *middleware.Context) { ctx.Data["MirrorCount"] = len(mirrors) ctx.Data["Mirrors"] = mirrors - // Get feeds. - actions, err := models.GetFeeds(ctxUser.Id, 0, false) - if err != nil { - ctx.Handle(500, "GetFeeds", err) + retrieveFeeds(ctx, ctx.User.Id, 0, false) + if ctx.Written() { return } - - // Check access of private repositories. - feeds := make([]*models.Action, 0, len(actions)) - unameAvatars := make(map[string]string) - for _, act := range actions { - if act.IsPrivate { - // This prevents having to retrieve the repository for each action - repo := &models.Repository{ID: act.RepoID, IsPrivate: true} - if act.RepoUserName != ctx.User.LowerName { - if has, _ := models.HasAccess(ctx.User, repo, models.ACCESS_MODE_READ); !has { - continue - } - } - - } - - // Cache results to reduce queries. - _, ok := unameAvatars[act.ActUserName] - if !ok { - u, err := models.GetUserByName(act.ActUserName) - if err != nil { - if models.IsErrUserNotExist(err) { - continue - } - ctx.Handle(500, "GetUserByName", err) - return - } - unameAvatars[act.ActUserName] = u.AvatarLink() - } - - act.ActAvatar = unameAvatars[act.ActUserName] - feeds = append(feeds, act) - } - ctx.Data["Feeds"] = feeds ctx.HTML(200, DASHBOARD) } @@ -356,39 +351,10 @@ func Profile(ctx *middleware.Context) { ctx.Data["TabName"] = tab switch tab { case "activity": - actions, err := models.GetFeeds(u.Id, 0, true) - if err != nil { - ctx.Handle(500, "GetFeeds", err) + retrieveFeeds(ctx, u.Id, 0, true) + if ctx.Written() { return } - feeds := make([]*models.Action, 0, len(actions)) - for _, act := range actions { - if act.IsPrivate { - if !ctx.IsSigned { - continue - } - // This prevents having to retrieve the repository for each action - repo := &models.Repository{ID: act.RepoID, IsPrivate: true} - if act.RepoUserName != ctx.User.LowerName { - if has, _ := models.HasAccess(ctx.User, repo, models.ACCESS_MODE_READ); !has { - continue - } - } - - } - // FIXME: cache results? - u, err := models.GetUserByName(act.ActUserName) - if err != nil { - if models.IsErrUserNotExist(err) { - continue - } - ctx.Handle(500, "GetUserByName", err) - return - } - act.ActAvatar = u.AvatarLink() - feeds = append(feeds, act) - } - ctx.Data["Feeds"] = feeds default: ctx.Data["Repos"], err = models.GetRepositories(u.Id, ctx.IsSigned && ctx.User.Id == u.Id) if err != nil { |