func ActivityJSONHandler(ctx context.Context, rw http.ResponseWriter, r *http.Request) { dataRenderer := data.FromContext(ctx) page := int(to.Int64(r.FormValue("page"))) if page <= 0 { page = 1 } limit := int(to.Int64(r.FormValue("limit"))) if limit <= 0 { limit = 50 } var cnt int if err := models.DB.Model(&models.ListActivity{}).Count(&cnt).Error; err != nil { panic(err) } totalpages := cnt / 50 if cnt%50 != 0 { totalpages++ } if page > totalpages { page = totalpages } var activities []models.ListActivity if err := models.DB.Limit(limit).Offset((page - 1) * limit).Order("created_at desc").Find(&activities).Error; err != nil && err != gorm.ErrRecordNotFound { panic(err) } // render a better karma view var rendered []*activityJSON for _, v := range activities { // load the username... rendered = append(rendered, &activityJSON{ ListId: v.ListID, User: models.FindUserByID(v.UserID).Username, Comment: string(bluemonday.UGCPolicy().SanitizeBytes(blackfriday.MarkdownCommon([]byte(v.Activity)))), Time: v.CreatedAt, URL: render.ConvertURL("/b/" + to.String(v.ListID)), }) } dataRenderer.Data = map[string]interface{}{ "totalpages": totalpages, "page": page, "activities": rendered, } dataRenderer.Type = data.DataJSON }
// AdminGetHandler controls the central dashboard for Kahinah. func AdminGetHandler(ctx context.Context, rw http.ResponseWriter, r *http.Request) { adminCheck(r) dataRenderer := data.FromContext(ctx) toRender := map[string]interface{}{ "Loc": 0, "Tab": -1, "Title": "Admin", } user := r.FormValue("username") if user != "" { userModel := models.FindUser(user) toRender["User"] = userModel } var perms []models.UserPermission if err := models.DB.Find(&perms).Error; err != nil && err != gorm.ErrRecordNotFound { panic(err) } rendered := map[string][]string{} for _, perm := range perms { if _, ok := rendered[perm.Permission]; !ok { rendered[perm.Permission] = []string{} } rendered[perm.Permission] = append(rendered[perm.Permission], models.FindUserByID(perm.UserID).Username) } toRender["Permissions"] = rendered dataRenderer.Data = toRender dataRenderer.Template = "admin" }