Ejemplo n.º 1
0
func discoverFeeds(user content.User, fm *readeef.FeedManager, link string) (resp responseError) {
	resp = newResponse()

	if u, err := url.Parse(link); err != nil {
		resp.err = readeef.ErrNoAbsolute
		resp.errType = errTypeNoAbsolute
		return
	} else if !u.IsAbs() {
		u.Scheme = "http"
		if u.Host == "" {
			parts := strings.SplitN(u.Path, "/", 2)
			u.Host = parts[0]
			if len(parts) > 1 {
				u.Path = "/" + parts[1]
			} else {
				u.Path = ""
			}
		}
		link = u.String()
	}

	feeds, err := fm.DiscoverFeeds(link)
	if err != nil {
		resp.val["Feeds"] = []content.Feed{}
		return
	}

	uf := user.AllFeeds()
	if user.HasErr() {
		resp.err = user.Err()
		return
	}

	userFeedIdMap := make(map[data.FeedId]bool)
	userFeedLinkMap := make(map[string]bool)
	for i := range uf {
		in := uf[i].Data()
		userFeedIdMap[in.Id] = true
		userFeedLinkMap[in.Link] = true

		u, err := url.Parse(in.Link)
		if err == nil && strings.HasPrefix(u.Host, "www.") {
			u.Host = u.Host[4:]
			userFeedLinkMap[u.String()] = true
		}
	}

	respFeeds := []content.Feed{}
	for i := range feeds {
		in := feeds[i].Data()
		if !userFeedIdMap[in.Id] && !userFeedLinkMap[in.Link] {
			respFeeds = append(respFeeds, feeds[i])
		}
	}

	resp.val["Feeds"] = respFeeds
	return
}
Ejemplo n.º 2
0
func addFeeds(user content.User, fm *readeef.FeedManager, links []string) (resp responseError) {
	resp = newResponse()

	var err error
	errs := make([]addFeedError, 0, len(links))

	for _, link := range links {
		var u *url.URL
		if u, err = url.Parse(link); err != nil {
			resp.err = err
			errs = append(errs, addFeedError{Link: link, Error: "Error parsing link"})
			continue
		} else if !u.IsAbs() {
			resp.err = errors.New("Feed has no link")
			errs = append(errs, addFeedError{Link: link, Error: resp.err.Error()})
			continue
		} else {
			var f content.Feed
			if f, err = fm.AddFeedByLink(link); err != nil {
				resp.err = err
				errs = append(errs, addFeedError{Link: link, Error: "Error adding feed to the database"})
				continue
			}

			uf := user.AddFeed(f)
			if uf.HasErr() {
				resp.err = f.Err()
				errs = append(errs, addFeedError{Link: link, Title: f.Data().Title, Error: "Error adding feed to the database"})
				continue
			}

			tags := strings.SplitN(u.Fragment, ",", -1)
			if u.Fragment != "" && len(tags) > 0 {
				repo := uf.Repo()
				tf := repo.TaggedFeed(user)
				tf.Data(uf.Data())

				t := make([]content.Tag, len(tags))
				for i := range tags {
					t[i] = repo.Tag(user)
					t[i].Data(data.Tag{Value: data.TagValue(tags[i])})
				}

				tf.Tags(t)
				if tf.UpdateTags(); tf.HasErr() {
					resp.err = tf.Err()
					errs = append(errs, addFeedError{Link: link, Title: f.Data().Title, Error: "Error adding feed to the database"})
					continue
				}
			}
		}
	}

	resp.val["Errors"] = errs
	resp.val["Success"] = len(errs) < len(links)
	return
}
Ejemplo n.º 3
0
func addFeed(user content.User, fm *readeef.FeedManager, links []string) (resp responseError) {
	resp = newResponse()

	success := false

	for _, link := range links {
		var u *url.URL
		if u, resp.err = url.Parse(link); resp.err != nil {
			/* TODO: non-fatal error */
			return
		} else if !u.IsAbs() {
			/* TODO: non-fatal error */
			resp.err = errors.New("Feed has no link")
			return
		} else {
			var f content.Feed
			if f, resp.err = fm.AddFeedByLink(link); resp.err != nil {
				return
			}

			uf := user.AddFeed(f)
			if uf.HasErr() {
				resp.err = f.Err()
				return
			}

			tags := strings.SplitN(u.Fragment, ",", -1)
			if u.Fragment != "" && len(tags) > 0 {
				repo := uf.Repo()
				tf := repo.TaggedFeed(user)
				tf.Data(uf.Data())

				t := make([]content.Tag, len(tags))
				for i := range tags {
					t[i] = repo.Tag(user)
					t[i].Value(data.TagValue(tags[i]))
				}

				tf.Tags(t)
				if tf.UpdateTags(); tf.HasErr() {
					resp.err = tf.Err()
					return
				}
			}

			success = true
		}
	}

	resp.val["Success"] = success
	return
}
Ejemplo n.º 4
0
func removeFeed(user content.User, fm *readeef.FeedManager, id data.FeedId) (resp responseError) {
	resp = newResponse()

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

	fm.RemoveFeed(feed)

	resp.val["Success"] = true
	return
}
Ejemplo n.º 5
0
func parseOpml(user content.User, fm *readeef.FeedManager, opmlData []byte) (resp responseError) {
	resp = newResponse()

	var opml parser.Opml
	if opml, resp.err = parser.ParseOpml(opmlData); resp.err != nil {
		return
	}

	uf := user.AllFeeds()
	if user.HasErr() {
		resp.err = user.Err()
		return
	}

	userFeedMap := make(map[data.FeedId]bool)
	for i := range uf {
		userFeedMap[uf[i].Data().Id] = true
	}

	var feeds []content.Feed
	for _, opmlFeed := range opml.Feeds {
		discovered, err := fm.DiscoverFeeds(opmlFeed.Url)
		if err != nil {
			continue
		}

		for _, f := range discovered {
			in := f.Data()
			if !userFeedMap[in.Id] {
				if len(opmlFeed.Tags) > 0 {
					in.Link += "#" + strings.Join(opmlFeed.Tags, ",")
				}

				f.Data(in)

				feeds = append(feeds, f)
			}
		}
	}

	resp.val["Feeds"] = feeds
	return
}