Beispiel #1
0
func setFeedTags(user content.User, id data.FeedId, tagValues []data.TagValue) (resp responseError) {
	resp = newResponse()

	feed := user.FeedById(id)
	if feed.HasErr() {
		resp.err = feed.Err()
		return
	}

	repo := user.Repo()
	tf := repo.TaggedFeed(user)
	tf.Data(feed.Data())
	tags := make([]content.Tag, len(tagValues))
	for i := range tagValues {
		tags[i] = repo.Tag(user)
		tags[i].Value(tagValues[i])
	}
	tf.Tags(tags)
	if tf.UpdateTags(); tf.HasErr() {
		resp.err = tf.Err()
		return
	}

	resp.val["Success"] = true
	resp.val["Id"] = id
	return
}
Beispiel #2
0
func getFeedTags(user content.User, id data.FeedId) (resp responseError) {
	resp = newResponse()

	repo := user.Repo()
	tf := repo.TaggedFeed(user)
	if uf := user.FeedById(id); uf.HasErr() {
		resp.err = uf.Err()
		return
	} else {
		tf.Data(uf.Data())
	}

	tags := tf.Tags()

	if tf.HasErr() {
		resp.err = tf.Err()
		return
	}

	t := make([]string, len(tags))
	for _, tag := range tags {
		t = append(t, tag.String())
	}

	resp.val["Tags"] = t
	return
}
Beispiel #3
0
func removeUser(user content.User, login data.Login) (resp responseError) {
	resp = newResponse()
	resp.val["Login"] = login

	if !user.Data().Admin {
		resp.err = errForbidden
		resp.errType = errTypeForbidden
		return
	}

	if user.Data().Login == login {
		resp.err = errCurrentUser
		resp.errType = errTypeCurrentUser
		return
	}

	u := user.Repo().UserByLogin(login)
	u.Delete()

	if resp.err = u.Err(); resp.err != nil {
		return
	}

	resp.val["Success"] = true
	return
}
Beispiel #4
0
func listUsers(user content.User) (resp responseError) {
	resp = newResponse()

	if !user.Data().Admin {
		resp.err = errForbidden
		resp.errType = errTypeForbidden
		return
	}

	repo := user.Repo()
	resp.val["Users"], resp.err = repo.AllUsers(), repo.Err()
	return
}
Beispiel #5
0
func readState(user content.User, id string, beforeId data.ArticleId, timestamp int64) (resp responseError) {
	resp = newResponse()

	var ar content.ArticleRepo

	o := data.ArticleUpdateStateOptions{}

	if timestamp > 0 {
		t := time.Unix(timestamp/1000, 0)
		o.BeforeDate = t
	}

	if beforeId > 0 {
		o.BeforeId = beforeId
	}

	switch {
	case id == "all":
		ar = user
	case id == "favorite":
		o.FavoriteOnly = true
		ar = user
	case strings.HasPrefix(id, "popular:"):
		// Can't bulk set state to popular articles
	case strings.HasPrefix(id, "tag:"):
		tag := user.Repo().Tag(user)
		tag.Data(data.Tag{Value: data.TagValue(id[4:])})

		ar = tag
	default:
		var feedId int64
		if feedId, resp.err = strconv.ParseInt(id, 10, 64); resp.err != nil {
			/* TODO: non-fatal error */
			return
		}

		ar = user.FeedById(data.FeedId(feedId))
	}

	if ar != nil {
		ar.ReadState(true, o)

		if e, ok := ar.(content.Error); ok && e.HasErr() {
			resp.err = e.Err()
			return
		}
	}

	resp.val["Success"] = true
	return
}
Beispiel #6
0
func addUser(user content.User, login data.Login, password string, secret []byte) (resp responseError) {
	resp = newResponse()
	resp.val["Login"] = login

	if !user.Data().Admin {
		resp.err = errForbidden
		resp.errType = errTypeForbidden
		return
	}

	repo := user.Repo()
	u := repo.UserByLogin(login)

	if !u.HasErr() {
		/* TODO: non-fatal error */
		resp.err = errUserExists
		resp.errType = errTypeUserExists
		return
	} else {
		err := u.Err()
		if err != content.ErrNoContent {
			resp.err = err
			return
		}
	}

	resp.err = nil

	in := data.User{Login: login}
	u = repo.User()

	u.Data(in)
	u.Password(password, secret)
	u.Update()

	if resp.err = u.Err(); resp.err != nil {
		return
	}

	resp.val["Success"] = true

	return
}
Beispiel #7
0
func search(user content.User, searchIndex readeef.SearchIndex, query, highlight, feedId string) (resp responseError) {
	resp = newResponse()

	if strings.HasPrefix(feedId, "tag:") {
		tag := user.Repo().Tag(user)
		tag.Value(data.TagValue(feedId[4:]))

		tag.Highlight(highlight)
		resp.val["Articles"], resp.err = tag.Query(query, searchIndex.Index), tag.Err()
	} else {
		if id, err := strconv.ParseInt(feedId, 10, 64); err == nil {
			f := user.FeedById(data.FeedId(id))
			resp.val["Articles"], resp.err = f.Query(query, searchIndex.Index), f.Err()
		} else {
			user.Highlight(highlight)
			resp.val["Articles"], resp.err = user.Query(query, searchIndex.Index), user.Err()
		}
	}

	return
}
Beispiel #8
0
func setAttributeForUser(user content.User, secret []byte, login data.Login, attr string, value []byte) (resp responseError) {
	if !user.Data().Admin {
		resp.err = errForbidden
		resp.errType = errTypeForbidden
		return
	}

	if user.Data().Login == login {
		resp.err = errCurrentUser
		resp.errType = errTypeCurrentUser
		return
	}

	if u := user.Repo().UserByLogin(login); u.HasErr() {
		resp.err = u.Err()
		return
	} else {
		resp = setUserAttribute(u, secret, attr, value)
	}

	return
}
Beispiel #9
0
func performSearch(user content.User, sp content.SearchProvider, query, feedId string, limit, offset int) (ua []content.UserArticle, err error) {
	defer func() {
		if rec := recover(); rec != nil {
			err = fmt.Errorf("Error during search: %s", rec)
		}
	}()

	if strings.HasPrefix(feedId, "tag:") {
		tag := user.Repo().Tag(user)
		tag.Data(data.Tag{Value: data.TagValue(feedId[4:])})

		ua, err = tag.Query(query, sp, limit, offset), tag.Err()
	} else {
		if id, err := strconv.ParseInt(feedId, 10, 64); err == nil {
			f := user.FeedById(data.FeedId(id))
			ua, err = f.Query(query, sp, limit, offset), f.Err()
		} else {
			ua, err = user.Query(query, sp, limit, offset), user.Err()
		}
	}

	return
}
Beispiel #10
0
func markFeedAsRead(user content.User, id string, timestamp int64) (resp responseError) {
	resp = newResponse()

	t := time.Unix(timestamp/1000, 0)

	switch {
	case id == "all":
		if user.ReadBefore(t, true); user.HasErr() {
			resp.err = user.Err()
			return
		}
	case id == "favorite" || strings.HasPrefix(id, "popular:"):
		// Favorites are assumbed to have been read already
	case strings.HasPrefix(id, "tag:"):
		tag := user.Repo().Tag(user)
		tag.Value(data.TagValue(id[4:]))
		if tag.ReadBefore(t, true); tag.HasErr() {
			resp.err = tag.Err()
			return
		}
	default:
		var feedId int64
		if feedId, resp.err = strconv.ParseInt(id, 10, 64); resp.err != nil {
			/* TODO: non-fatal error */
			return
		}

		feed := user.FeedById(data.FeedId(feedId))
		if feed.ReadBefore(t, true); feed.HasErr() {
			resp.err = feed.Err()
			return
		}
	}

	resp.val["Success"] = true
	return
}
Beispiel #11
0
func setFeedTags(user content.User, id data.FeedId, tagValues []data.TagValue) (resp responseError) {
	resp = newResponse()

	feed := user.FeedById(id)
	if feed.HasErr() {
		resp.err = feed.Err()
		return
	}

	repo := user.Repo()
	tf := repo.TaggedFeed(user)
	tf.Data(feed.Data())

	filtered := make([]data.TagValue, 0, len(tagValues))
	for _, v := range tagValues {
		v = data.TagValue(strings.TrimSpace(string(v)))
		if v != "" {
			filtered = append(filtered, v)
		}
	}

	tags := make([]content.Tag, len(filtered))
	for i := range filtered {
		tags[i] = repo.Tag(user)
		tags[i].Data(data.Tag{Value: filtered[i]})
	}
	tf.Tags(tags)
	if tf.UpdateTags(); tf.HasErr() {
		resp.err = tf.Err()
		return
	}

	resp.val["Success"] = true
	resp.val["Id"] = id
	return
}
Beispiel #12
0
func formatArticle(user content.User, id data.ArticleId, extractor content.Extractor, webfwConfig webfw.Config, readeefConfig readeef.Config) (resp responseError) {
	resp = newResponse()

	article := user.ArticleById(id)
	if user.HasErr() {
		resp.err = user.Err()
		return
	}

	extract := article.Extract()
	if article.HasErr() {
		resp.err = article.Err()
		return
	}

	extractData := extract.Data()
	if extract.HasErr() {
		switch err := extract.Err(); err {
		case content.ErrNoContent:
			if extractor == nil {
				resp.err = fmt.Errorf("Error formatting article: A valid extractor is reequired")
				return
			}

			extractData, resp.err = extractor.Extract(article.Data().Link)
			if resp.err != nil {
				return
			}

			extractData.ArticleId = article.Data().Id
			extract.Data(extractData)
			extract.Update()
			if extract.HasErr() {
				resp.err = extract.Err()
				return
			}
		default:
			resp.err = err
			return
		}
	}

	processors := user.Repo().ArticleProcessors()
	if len(processors) > 0 {
		a := user.Repo().UserArticle(user)
		a.Data(data.Article{Description: extractData.Content})

		ua := []content.UserArticle{a}

		if extractData.TopImage != "" {
			a = user.Repo().UserArticle(user)
			a.Data(data.Article{
				Description: fmt.Sprintf(`<img src="%s">`, extractData.TopImage),
			})

			ua = append(ua, a)
		}

		for _, p := range processors {
			ua = p.ProcessArticles(ua)
		}

		extractData.Content = ua[0].Data().Description

		if extractData.TopImage != "" {
			content := ua[1].Data().Description

			content = strings.Replace(content, `<img src="`, "", -1)
			i := strings.Index(content, `"`)
			content = content[:i]

			extractData.TopImage = content
		}
	}

	s := summarize.NewFromString(extractData.Title, search.StripTags(extractData.Content))

	s.Language = extractData.Language
	keyPoints := s.KeyPoints()

	for i := range keyPoints {
		keyPoints[i] = html.UnescapeString(keyPoints[i])
	}

	resp.val["KeyPoints"] = keyPoints
	resp.val["Content"] = extractData.Content
	resp.val["TopImage"] = extractData.TopImage
	resp.val["Id"] = id
	return
}
Beispiel #13
0
func getFeedArticles(user content.User, sp content.SearchProvider,
	id string, minId, maxId data.ArticleId, limit int, offset int, olderFirst bool,
	unreadOnly bool) (resp responseError) {

	resp = newResponse()

	if limit > 200 {
		limit = 200
	}

	var as content.ArticleSorting
	var ar content.ArticleRepo
	var ua []content.UserArticle

	o := data.ArticleQueryOptions{Limit: limit, Offset: offset, UnreadOnly: unreadOnly, UnreadFirst: true}

	if maxId > 0 {
		o.AfterId = maxId
		resp.val["MaxId"] = maxId
	}

	if id == "favorite" {
		o.FavoriteOnly = true
		ar = user
		as = user
	} else if id == "all" {
		ar = user
		as = user
	} else if strings.HasPrefix(id, "popular:") {
		o.IncludeScores = true
		o.HighScoredFirst = true
		o.BeforeDate = time.Now()
		o.AfterDate = time.Now().AddDate(0, 0, -5)

		if id == "popular:all" {
			ar = user
			as = user
		} else if strings.HasPrefix(id, "popular:tag:") {
			tag := user.Repo().Tag(user)
			tag.Data(data.Tag{Value: data.TagValue(id[12:])})

			ar = tag
			as = tag
		} else {
			var f content.UserFeed

			var feedId int64
			feedId, resp.err = strconv.ParseInt(id[8:], 10, 64)

			if resp.err != nil {
				resp.err = errors.New("Unknown feed id " + id)
				return
			}

			if f = user.FeedById(data.FeedId(feedId)); f.HasErr() {
				/* TODO: non-fatal error */
				resp.err = f.Err()
				return
			}

			ar = f
			as = f
		}
	} else if strings.HasPrefix(id, "search:") && sp != nil {
		var query string
		id = id[7:]
		parts := strings.Split(id, ":")

		if parts[0] == "tag" {
			id = strings.Join(parts[:2], ":")
			query = strings.Join(parts[2:], ":")
		} else {
			id = strings.Join(parts[:1], ":")
			query = strings.Join(parts[1:], ":")
		}

		sp.SortingByDate()
		if olderFirst {
			sp.Order(data.AscendingOrder)
		} else {
			sp.Order(data.DescendingOrder)
		}

		ua, resp.err = performSearch(user, sp, query, id, limit, offset)
	} else if strings.HasPrefix(id, "tag:") {
		tag := user.Repo().Tag(user)
		tag.Data(data.Tag{Value: data.TagValue(id[4:])})

		as = tag
		ar = tag
	} else {
		var f content.UserFeed

		var feedId int64
		feedId, resp.err = strconv.ParseInt(id, 10, 64)

		if resp.err != nil {
			resp.err = errors.New("Unknown feed id " + id)
			return
		}

		if f = user.FeedById(data.FeedId(feedId)); f.HasErr() {
			/* TODO: non-fatal error */
			resp.err = f.Err()
			return
		}

		as = f
		ar = f
	}

	if as != nil {
		as.SortingByDate()
		if olderFirst {
			as.Order(data.AscendingOrder)
		} else {
			as.Order(data.DescendingOrder)
		}
	}

	if ar != nil {
		ua = ar.Articles(o)

		if minId > 0 {
			qo := data.ArticleIdQueryOptions{BeforeId: maxId + 1, AfterId: minId - 1}

			qo.UnreadOnly = true
			resp.val["UnreadIds"] = ar.Ids(qo)

			qo.UnreadOnly = false
			qo.FavoriteOnly = true
			resp.val["FavoriteIds"] = ar.Ids(qo)

			resp.val["MinId"] = minId
		}

		if e, ok := ar.(content.Error); ok && e.HasErr() {
			resp.err = e.Err()
		}
	}

	resp.val["Articles"] = ua
	resp.val["Limit"] = limit
	resp.val["Offset"] = offset

	return
}
Beispiel #14
0
func internalGetArticles(u content.User, dbo *db.DB, logger webfw.Logger, opts data.ArticleQueryOptions, sorting content.ArticleSorting, join, where string, args []interface{}) (ua []content.UserArticle) {
	renderData := getArticlesData{}
	s := dbo.SQL()
	if opts.IncludeScores {
		renderData.Columns += ", asco.score"
		renderData.Join += s.User.GetArticlesScoreJoin
	}

	if opts.UntaggedOnly {
		renderData.Join += s.User.GetArticlesUntaggedJoin
	}

	if join != "" {
		renderData.Join += " " + join
	}

	args = append([]interface{}{u.Data().Login}, args...)

	whereSlice := []string{}

	if opts.UnreadOnly {
		whereSlice = append(whereSlice, "au.article_id IS NOT NULL")
	} else if opts.ReadOnly {
		whereSlice = append(whereSlice, "au.article_id IS NULL")
	}

	if opts.UntaggedOnly {
		whereSlice = append(whereSlice, "uft.feed_id IS NULL")
	}

	if where != "" {
		whereSlice = append(whereSlice, where)
	}

	if opts.BeforeId > 0 {
		whereSlice = append(whereSlice, fmt.Sprintf("a.id < $%d", len(args)+1))
		args = append(args, opts.BeforeId)
	}
	if opts.AfterId > 0 {
		whereSlice = append(whereSlice, fmt.Sprintf("a.id > $%d", len(args)+1))
		args = append(args, opts.AfterId)
	}

	if opts.FavoriteOnly {
		whereSlice = append(whereSlice, "af.article_id IS NOT NULL")
	}

	if !opts.BeforeDate.IsZero() {
		whereSlice = append(whereSlice, fmt.Sprintf("(a.date IS NULL OR a.date < $%d)", len(args)+1))
		args = append(args, opts.BeforeDate)
	}

	if !opts.AfterDate.IsZero() {
		whereSlice = append(whereSlice, fmt.Sprintf("a.date > $%d", len(args)+1))
		args = append(args, opts.AfterDate)
	}

	if len(whereSlice) > 0 {
		renderData.Where = "WHERE " + strings.Join(whereSlice, " AND ")
	}

	sortingField := sorting.Field()
	sortingOrder := sorting.Order()

	fields := []string{}

	if opts.IncludeScores && opts.HighScoredFirst {
		field := "asco.score"
		if sortingOrder == data.DescendingOrder {
			field += " DESC"
		}
		fields = append(fields, field)
	}

	if opts.UnreadFirst {
		fields = append(fields, "read")
	}

	switch sortingField {
	case data.SortById:
		fields = append(fields, "a.id")
	case data.SortByDate:
		fields = append(fields, "a.date")
	}
	if len(fields) > 0 {
		renderData.Order = " ORDER BY " + strings.Join(fields, ", ")

		if sortingOrder == data.DescendingOrder {
			renderData.Order += " DESC"
		}
	}

	if opts.Limit > 0 {
		renderData.Limit = fmt.Sprintf(" LIMIT $%d OFFSET $%d", len(args)+1, len(args)+2)
		args = append(args, opts.Limit, opts.Offset)
	}

	buf := util.BufferPool.GetBuffer()
	defer util.BufferPool.Put(buf)

	if err := getArticlesTemplate.Execute(buf, renderData); err != nil {
		u.Err(fmt.Errorf("Error executing get-articles template: %v", err))
		return
	}

	sql := buf.String()
	var data []data.Article
	logger.Debugf("Articles SQL:\n%s\nArgs:%v\n", sql, args)
	if err := dbo.Select(&data, sql, args...); err != nil {
		u.Err(err)
		return
	}

	ua = make([]content.UserArticle, len(data))
	for i := range data {
		ua[i] = u.Repo().UserArticle(u)
		ua[i].Data(data[i])
	}

	processors := u.Repo().ArticleProcessors()
	if !opts.SkipProcessors && len(processors) > 0 {
		for _, p := range processors {
			if opts.SkipSessionProcessors {
				if _, ok := p.(processor.ProxyHTTP); ok {
					continue
				}
			}
			ua = p.ProcessArticles(ua)
		}
	}

	return
}
Beispiel #15
0
func getFeedArticles(user content.User, id string, limit int, offset int, newerFirst bool, unreadOnly bool) (resp responseError) {
	resp = newResponse()

	if limit > 50 {
		limit = 50
	}

	user.SortingByDate()
	if newerFirst {
		user.Order(data.DescendingOrder)
	} else {
		user.Order(data.AscendingOrder)
	}
	if id == "favorite" {
		resp.val["Articles"], resp.err = user.FavoriteArticles(limit, offset), user.Err()
	} else if id == "popular:all" {
		resp.val["Articles"], resp.err = user.ScoredArticles(time.Now().AddDate(0, 0, -5), time.Now(), limit, offset), user.Err()
	} else if id == "all" {
		if unreadOnly {
			resp.val["Articles"], resp.err = user.UnreadArticles(limit, offset), user.Err()
		} else {
			resp.val["Articles"], resp.err = user.Articles(limit, offset), user.Err()
		}
	} else if strings.HasPrefix(id, "popular:") {
		if strings.HasPrefix(id, "popular:tag:") {
			tag := user.Repo().Tag(user)
			tag.Value(data.TagValue(id[12:]))

			tag.SortingByDate()
			if newerFirst {
				tag.Order(data.DescendingOrder)
			} else {
				tag.Order(data.AscendingOrder)
			}
			resp.val["Articles"], resp.err = tag.ScoredArticles(time.Now().AddDate(0, 0, -5), time.Now(), limit, offset), tag.Err()
		} else {
			var f content.UserFeed

			var feedId int64
			feedId, resp.err = strconv.ParseInt(id[8:], 10, 64)

			if resp.err != nil {
				resp.err = errors.New("Unknown feed id " + id)
				return
			}

			if f = user.FeedById(data.FeedId(feedId)); f.HasErr() {
				/* TODO: non-fatal error */
				resp.err = f.Err()
				return
			}

			f.SortingByDate()
			if newerFirst {
				f.Order(data.DescendingOrder)
			} else {
				f.Order(data.AscendingOrder)
			}

			resp.val["Articles"], resp.err = f.ScoredArticles(time.Now().AddDate(0, 0, -5), time.Now(), limit, offset), f.Err()
		}
	} else if strings.HasPrefix(id, "tag:") {
		tag := user.Repo().Tag(user)
		tag.Value(data.TagValue(id[4:]))

		tag.SortingByDate()
		if newerFirst {
			tag.Order(data.DescendingOrder)
		} else {
			tag.Order(data.AscendingOrder)
		}

		if unreadOnly {
			resp.val["Articles"], resp.err = tag.UnreadArticles(limit, offset), tag.Err()
		} else {
			resp.val["Articles"], resp.err = tag.Articles(limit, offset), tag.Err()
		}
	} else {
		var f content.UserFeed

		var feedId int64
		feedId, resp.err = strconv.ParseInt(id, 10, 64)

		if resp.err != nil {
			resp.err = errors.New("Unknown feed id " + id)
			return
		}

		if f = user.FeedById(data.FeedId(feedId)); f.HasErr() {
			/* TODO: non-fatal error */
			resp.err = f.Err()
			return
		}

		if newerFirst {
			f.Order(data.DescendingOrder)
		} else {
			f.Order(data.AscendingOrder)
		}

		f.SortingByDate()
		if unreadOnly {
			resp.val["Articles"], resp.err = f.UnreadArticles(limit, offset), f.Err()
		} else {
			resp.val["Articles"], resp.err = f.Articles(limit, offset), f.Err()
		}
	}

	return
}
Beispiel #16
0
func getArticles(u content.User, dbo *db.DB, logger webfw.Logger, sorting content.ArticleSorting, columns, join, where, order string, args []interface{}, paging ...int) (ua []content.UserArticle) {
	if u.HasErr() {
		return
	}

	sql := dbo.SQL("get_article_columns")
	if columns != "" {
		sql += ", " + columns
	}

	sql += dbo.SQL("get_article_tables")
	if join != "" {
		sql += " " + join
	}

	sql += dbo.SQL("get_article_joins")

	args = append([]interface{}{u.Data().Login}, args...)
	if where != "" {
		sql += " AND " + where
	}

	sortingField := sorting.Field()
	sortingOrder := sorting.Order()

	fields := []string{}
	if order != "" {
		fields = append(fields, order)
	}
	switch sortingField {
	case data.SortById:
		fields = append(fields, "a.id")
	case data.SortByDate:
		fields = append(fields, "a.date")
	}
	if len(fields) > 0 {
		sql += " ORDER BY "

		sql += strings.Join(fields, ",")

		if sortingOrder == data.DescendingOrder {
			sql += " DESC"
		}
	}

	if len(paging) > 0 {
		limit, offset := pagingLimit(paging)

		sql += fmt.Sprintf(" LIMIT $%d OFFSET $%d", len(args)+1, len(args)+2)
		args = append(args, limit, offset)
	}

	var data []data.Article
	logger.Debugf("Articles SQL:\n%s\nArgs:%q\n", sql, args)
	if err := dbo.Select(&data, sql, args...); err != nil {
		u.Err(err)
		return
	}

	ua = make([]content.UserArticle, len(data))
	for i := range data {
		ua[i] = u.Repo().UserArticle(u)
		ua[i].Data(data[i])
	}

	return
}