Beispiel #1
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)
	}
}
Beispiel #2
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)
	}
}