示例#1
0
func TagsList() martini.Handler {
	return func(w http.ResponseWriter, r *http.Request, rid httphelper.RequestId) {
		var result Result
		result.RequestId = rid

		r.ParseForm()
		param := httphelper.QueryValue(r, "getall", "none") // "none" means no "getall" parameter provided.

		getall := false
		if param != "none" {
			getall = true
		}

		list, err := model.GetTagsWithFeedList(getall)
		if err != nil {
			result.Error = ErrQueryDB
			result.IntError = err
			result.Response(w)
			return
		}

		result.Success = true
		result.Result = list
		result.Response(w)
	}
}
示例#2
0
/*
Get random items.

method:     GET
path:       /api/article/list/random

example:
    /api/article/list/random
    /api/article/list/random?n=20

You can control the number of results returned by add a "n" parameter, default number is 10.
*/
func RandomArticleList() martini.Handler {
	return func(w http.ResponseWriter, r *http.Request, rid httphelper.RequestId) {
		var result Result
		result.RequestId = rid

		r.ParseForm()
		num, err := strconv.Atoi(httphelper.QueryValue(r, "n"))
		if err != nil || num <= 0 {
			num = 10
		}

		list, err := model.GetRandomArticleList(num)
		if err != nil {
			result.Error = ErrQueryDB
			result.IntError = err
			result.Response(w)
			return
		}

		for i, _ := range list.Articles {
			utils.SanitizeSelf(&list.Articles[i].Name)
			utils.SanitizeSelf(&list.Articles[i].Author)
			utils.SanitizeSelf(&list.Articles[i].Title)
		}

		result.Success = true
		result.Result = list
		result.Response(w)
	}
}
示例#3
0
/*
Check if a feed url is already subscribed.

method:     GET
path:       /api/feed/subscription?url={}
example:    /api/feed/subscription?url=http://127.0.0.1&token=xxxx

The output is like: {"request_id":"06442e9fa31f9c8620c68ab9e2df89c4","success":true,"error":{"errcode":0,"errmsg":""},"result":false}
*/
func IsSubscribed() martini.Handler {
	return func(w http.ResponseWriter, r *http.Request, rid httphelper.RequestId) {
		var result Result

		r.ParseForm()
		url := httphelper.QueryValue(r, "url")

		ok, err := model.IsSubscribed(url)
		if err != nil {
			result.Success = false
			result.Error = ErrQueryDB
			result.Result = false
			result.IntError = err
		} else {
			result.Success = true
			result.Result = ok
		}

		result.Response(w, rid)
	}
}
示例#4
0
/*
Subscribe a feed.

method:     POST
path:       /api/feed/subscription?url={}
example:    /api/feed/subscription?url=http://127.0.0.1&token=xxxx
postdata:   nothing

The output is like: {"request_id":"ed4149c76a0910e9b4d3f5921f1cfda3","success":true,"error":{"errcode":0,"errmsg":""},"result":{"id":2,"number":2}}
*/
func Subscribe() martini.Handler {

	return func(w http.ResponseWriter, r *http.Request, rid httphelper.RequestId) {
		var result Result
		result.RequestId = rid

		r.ParseForm()
		url := httphelper.QueryValue(r, "url")

		ok, err := model.IsSubscribed(url)
		if err != nil {
			result.Success = false
			result.Error = ErrQueryDB
			result.Result = url
			result.IntError = err

			result.Response(w)
			return
		}
		if ok {
			result.Success = false
			result.Error = ErrAlreadySubscribed
			result.Result = nil

			result.Response(w)
			return
		}

		feed, items, err := model.FetchFeed(url)
		if err != nil {
			result.Success = false

			if _, ok := err.(*feedreader.ParseError); ok {
				result.Error = ErrParseError
			} else {
				result.Error = ErrFetchError
			}

			result.Result = url
			result.IntError = err
			result.Response(w)
			return
		}

		id, number, name, err := model.Subscribe(feed, items)
		if err != nil {
			result.Success = false
			result.Error = ErrQueryDB
			result.Result = url
			result.IntError = err

			result.Response(w)
			return
		}

		result.Success = true

		var t struct {
			Id     int64  `json:"id"`
			Number int64  `json:"number"`
			Name   string `json:"name"`
		}
		t.Id = id
		t.Number = number
		t.Name = name

		result.Result = t

		result.Response(w)
	}
}
示例#5
0
/*
Article list of search result.

path:       /api/articles/search/{deflimit}?q={query}&page={page}
example:    /api/articles/search/10?q=num:20&page=10
*/
func SearchList() martini.Handler {

	return func(w http.ResponseWriter, r *http.Request, params martini.Params, rid httphelper.RequestId) {
		var result Result
		result.RequestId = rid

		r.ParseForm()

		var limit, page int

		l, _ := strconv.ParseInt(params["deflimit"], 10, 0)
		p, _ := strconv.ParseInt(httphelper.QueryValue(r, "page", "0"), 10, 0)
		limit = int(l)
		page = int(p)

		if limit <= 0 {
			result.Error = ErrBadRequest
			result.IntError = fmt.Errorf("Parameter 'limit' not correct.")
			result.Response(w)
			return
		}

		if page <= 0 {
			page = 1
		}

		sq, err := model.Search(httphelper.QueryValue(r, "q"))
		if err != nil {
			result.Error = ErrSearchSyntaxError
			result.IntError = err
			result.Response(w)
			return
		}

		if sq.Num == nil {
			sq.Num = &limit
		}

		list, err := sq.List(page)
		if err != nil {
			result.Error = ErrQueryDB
			result.IntError = err
			result.Response(w)
			return
		}

		if len(list.Articles) == 0 {
			result.Error = ErrNoResultsFound
			result.IntError = fmt.Errorf(ErrNoResultsFound.Error())
			result.Response(w)
			return
		}

		for i, _ := range list.Articles {
			utils.SanitizeSelf(&list.Articles[i].Name)
			utils.SanitizeSelf(&list.Articles[i].Author)
			utils.SanitizeSelf(&list.Articles[i].Title)
		}

		var t struct {
			model.ArticleList
			Limit int `json:"limit"` // used for paging
		}
		t.ArticleList = list
		t.Limit = *sq.Num

		result.Success = true
		result.Result = t
		result.Response(w)
	}
}