func fetchPost(res *wcg.Response, req *wcg.Request, blog *models.Blog) (*models.Post, error) { driver := models.NewPostDriver(gae.NewContext(req), req.Logger) id := req.Param("post_id") blogId := req.Param("blog_id") post, err := driver.FindPostById(id, blogId) if err != nil { if err == models.ErrPostNotFound { res.WriteHeader(404) res.WriteString("Not found") res.End() return nil, err } res.RenderInternalError(err.Error()) return nil, err } if post.PostState() != models.PostStatePublished { if post.OwnerId != req.User.Id() { res.WriteHeader(404) res.WriteString("Not found") res.End() return nil, err } } return post, err }
func fetchBlog(res *wcg.Response, req *wcg.Request, includePosts bool) (*models.Blog, *supports.Page, error) { // default query for posts. id := req.Param("blog_id") driver := models.NewBlogDriver(gae.NewContext(req), req.Logger) blog, query, err := driver.PostQuery(id) if err != nil { if err == models.ErrBlogNotFound { res.WriteHeader(404) res.WriteString("Not found") res.End() return nil, nil, err } res.RenderInternalError(err.Error()) return nil, nil, err } if !includePosts { return blog, supports.EmptyPage, nil } query = query.Filter("IsNew =", false).Filter("IsDraft =", false).Order("-PostDate").Order("-UpdatedAt") per_page := wcg.ParseInt(req.Query("num"), AppConfig.DefaultPostsFetched, 0, AppConfig.MaxPostsFetched) current := wcg.ParseInt(req.Query("p"), 0, 0, wcg.ParseIntMax) page, err := supports.NewPage(current, per_page, query) if err != nil { res.RenderInternalError("Post pagination error: %v", err) return nil, nil, err } return blog, page, nil }
func fetchPostForUpdate(req *wcg.Request, res *wcg.Response, blog *models.Blog) (*models.Post, error) { driver := models.NewPostDriver(gae.NewContext(req), req.Logger) id := req.Param("post_id") blogId := req.Param("blog_id") post, err := driver.FindPostById(id, blogId) if err != nil { if err == models.ErrPostNotFound { res.WriteHeader(404) res.WriteString("Not found") res.End() return nil, err } res.RenderInternalError(err.Error()) return nil, err } if blog.Id != post.BlogId { res.WriteHeader(403) res.WriteString("You could not manage that blog.") res.End() } if post.OwnerId != req.User.Id() { res.WriteHeader(403) res.WriteString("You could not manage this post.") res.End() return nil, err } return post, err }
func crawlSpecifiedMembers(res *wcg.Response, req *wcg.Request, app *App) { member, ok := app.Members[req.Param("member")] if !ok { lib.NotFound(res, req) return } crawl(res, req, member, app) }
func loadEvent(app *App, req *wcg.Request, key string) (*event.Event, error) { var e event.Event d := NewEventDriver(lib.NewAppContextFromRequest(req)) if err := d.Get(d.NewKey(req.Param(key), 0, nil), &e); err != nil { return nil, err } else { return &e, nil } }
func indexSpecifiedMember(res *wcg.Response, req *wcg.Request, app *App) { var appCtx = lib.NewAppContextFromRequest(req) member, ok := app.Members[req.Param("member")] if !ok { lib.NotFound(res, req) return } num := wcg.ParseInt(req.Param("n"), 0, 0, wcg.ParseIntMax) if num == 0 { num = wcg.ParseIntMax } // Crawling crawler := ameblo.NewCrawler(appCtx.NewHttpClient()) prefix := strings.TrimSuffix(member.BlogUrl, ".html") // xxxx.html => xxxx-{num}.html entries := make([]*ameblo.AmebloEntry, 0) for i := 1; i < num; i += 1 { url := fmt.Sprintf("%s-%d.html", prefix, i) req.Logger.Info("Indexing from %s ... ", url) newentries, err := crawler.CrawlEntryList(url) if err != nil { lib.InternalError(res, req, err) return } if len(newentries) > 20 { panic(fmt.Errorf("Unexpected number of entries (%d) are returned during indexing.", len(newentries))) } if len(newentries) == 0 { break } if len(newentries) < 20 { entries = append(entries, newentries...) break } if len(entries) > 0 && entries[len(entries)-1].Url == newentries[len(newentries)-1].Url { break } entries = append(entries, newentries...) } // Save and return resutls results := make([]string, 0) for _, ent := range entries { ent.Owner = member.Name results = append(results, ent.Url) } if err := updateIndexes(appCtx, entries); err != nil { req.Logger.Error("Failed to update the entry: %v", err) lib.InternalError(res, req, err) // stopped. } else { time.Sleep(10 * time.Second) // TODO: wait for all indexes are updated on datastore. mc := appCtx.NewMemcacheDriver() mckey := fmt.Sprintf(MC_KEY_HISTORY, app.Key, member.Name) mc.Delete(mckey) res.WriteJson(results) } }
func deleteBlogHandler(res *wcg.Response, req *wcg.Request) { driver := models.NewBlogDriver(gae.NewContext(req), req.Logger) err := driver.DeleteBlog(req.Param("blog_id"), req.User.Id()) if err != nil { if supports.IsValidationError(err) { res.WriteJsonWithStatus(400, nil, err) } else if err == models.ErrBlogNotOwned { res.WriteJsonWithStatus(403, nil, no_permission) } else if err == models.ErrBlogNotFound { res.WriteJsonWithStatus(404, nil, not_found) } else { res.RenderInternalError(err.Error()) } return } res.WriteJsonWithStatus(200, nil, ok) }
func historyInsights(res *wcg.Response, req *wcg.Request, app *App) { var appCtx = lib.NewAppContextFromRequest(req) member, ok := app.Members[req.Param("member")] if !ok { lib.NotFound(res, req) return } var insights amebloHistoryInsights mckey := fmt.Sprintf(MC_KEY_HISTORY, app.Key, member.Name) dent := NewAmebloEntryDriver(appCtx) dref := NewAmebloRefDriver(appCtx) mc := appCtx.NewMemcacheDriver() err := mc.CachedObject(mckey, &insights, func() (interface{}, error) { return getAmebloHistoryInsights(member, dent, dref) }, req.Query("force") == "1") if err != nil { lib.Error(res, req, err) return } res.WriteJson(insights) }