Example #1
0
func getArticles(u content.User, dbo *db.DB, logger webfw.Logger, opts data.ArticleQueryOptions, sorting content.ArticleSorting, join, where string, args []interface{}) (ua []content.UserArticle) {
	if u.HasErr() {
		return
	}

	var err error
	if getArticlesTemplate == nil {
		getArticlesTemplate, err = template.New("read-state-update-sql").
			Parse(dbo.SQL().User.GetArticlesTemplate)

		if err != nil {
			u.Err(fmt.Errorf("Error generating get-articles-update template: %v", err))
			return
		}
	}

	/* Much faster than using 'ORDER BY read'
	 * TODO: potential overall improvement for fetching pages other than the
	 * first by using the unread count and moving the offset based on it
	 */
	if opts.UnreadFirst && opts.Offset == 0 {
		originalUnreadOnly := opts.UnreadOnly

		opts.UnreadFirst = false
		opts.UnreadOnly = true

		ua = internalGetArticles(u, dbo, logger, opts, sorting, join, where, args)

		if !originalUnreadOnly && (opts.Limit == 0 || opts.Limit > len(ua)) {
			if opts.Limit > 0 {
				opts.Limit -= len(ua)
			}
			opts.UnreadOnly = false
			opts.ReadOnly = true

			readOnly := internalGetArticles(u, dbo, logger, opts, sorting, join, where, args)

			ua = append(ua, readOnly...)
		}

		return
	}

	return internalGetArticles(u, dbo, logger, opts, sorting, join, where, args)
}