From 9fe425913e36324e3a197089c327ea2ad0963cfd Mon Sep 17 00:00:00 2001 From: mengqi <5b5f7426@gmail.com> Date: Mon, 17 Aug 2015 22:54:53 +0800 Subject: [PATCH] sync with dev:1b08df1 fix bug: 1. paging error in search query page 2. cannot subscribe a feed if the feedurl contains parameters --- README.md | 12 ++-- api/feed.go | 93 +++++++++++++++++++++++++----- model/feed.go | 2 +- model/search.go | 10 ++-- qreader.go | 2 +- server/server.go | 2 +- sitedata/client/include/qreader.js | 7 ++- 7 files changed, 100 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index a23730d..576f542 100644 --- a/README.md +++ b/README.md @@ -11,12 +11,13 @@ QReader 是一个单用户的阅读器,不适合多人共同使用。 [直接下载 QReader 可执行程序](https://github.com/m3ng9i/qreader/releases)。 -最新版本:v0.2.1 +最新版本:v0.2.2 ## 更新说明 -- 2015-07-03 v0.2.1 发布。新增搜索功能;文章页增加其他文章链接。 -- 2015-05-22 v0.2 发布 +- 2015-08-18 v0.2.2 发布:修复若干bug +- 2015-07-03 v0.2.1 发布:新增搜索功能;文章页增加其他文章链接 +- 2015-05-22 v0.2 发布 ## 更新方式 @@ -26,7 +27,10 @@ QReader 是一个单用户的阅读器,不适合多人共同使用。 4. 将之前备份的 `feed.db` 和 `config.ini` 文件放回到更新后的 `sitedata` 目录下; 5. 重新启动 QReader。 -注意:在更新程序及 `sitedata` 后不要进行初始化,否则你的历史订阅数据将会被清空。 +注意: + +- 在更新程序及 `sitedata` 后不要进行初始化,否则你的历史订阅数据将会被清空。 +- 如果在下载界面没有找到适合你的操作系统的可执行程序,请自行编译程序。 ## 0. 功能 diff --git a/api/feed.go b/api/feed.go index e0eb7ea..73f2ebc 100644 --- a/api/feed.go +++ b/api/feed.go @@ -560,10 +560,6 @@ example: /api/articles/tag/blog/10/100 route: starred path: /api/articles/starred/{limit}/{offset} example: /api/articles/starred/10/100 - -route: search -path: /api/articles/search/{limit}/{offset}?q={query} -example: /api/articles/search/10/100?q={query} */ func ArticleList(route string) martini.Handler { return func(w http.ResponseWriter, r *http.Request, params martini.Params, rid httphelper.RequestId) { @@ -623,16 +619,6 @@ func ArticleList(route string) martini.Handler { } else if route == "starred" { list ,err = model.GetStarredArticleList(limit, offset) - } else if route == "search" { - sq, err := model.Search(httphelper.QueryValue(r, "q")) - if err != nil { - result.Error = ErrSearchSyntaxError - result.IntError = err - result.Response(w) - return - } - list, err = sq.List(limit, offset) - } else { result.Error = ErrUnexpectedError result.IntError = fmt.Errorf(ErrUnexpectedError.ErrMsg) @@ -666,6 +652,85 @@ func ArticleList(route string) martini.Handler { } +/* +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) + } +} + + /* Get feedinfo. method: GET diff --git a/model/feed.go b/model/feed.go index 0894b01..6525978 100644 --- a/model/feed.go +++ b/model/feed.go @@ -225,7 +225,7 @@ type Article struct { type ArticleList struct { Articles []*Article `xorm:"extends"` - Number int64 + Number int64 // amount of all articles } diff --git a/model/search.go b/model/search.go index 7414394..8cce8eb 100644 --- a/model/search.go +++ b/model/search.go @@ -333,7 +333,7 @@ func Search(q string) (sq SearchQuery, err error) { // Get article list of search query. -func (this *SearchQuery) List(limit, offset int) (list ArticleList, err error) { +func (this *SearchQuery) List(page int) (list ArticleList, err error) { session := global.Orm.NewSession() defer session.Close() @@ -443,11 +443,13 @@ func (this *SearchQuery) List(limit, offset int) (list ArticleList, err error) { sql += " " + s } - if this.Num != nil && *this.Num > 0 { - limit = *this.Num + if this.Num == nil { + err = fmt.Errorf("'num' not provide for search query.") + session.Rollback() + return } - sql = fmt.Sprintf("%s limit %d, %d", sql, offset, limit) + sql = fmt.Sprintf("%s limit %d, %d", sql, (page - 1) * *this.Num, *this.Num) err = session.Sql(sql).Find(&list.Articles) diff --git a/qreader.go b/qreader.go index 4f7524b..d6b9490 100644 --- a/qreader.go +++ b/qreader.go @@ -16,7 +16,7 @@ import "github.com/m3ng9i/qreader/utils" import "github.com/m3ng9i/qreader/server" -var _version_ = "v0.2.1" // program version, from git tag +var _version_ = "v0.2.2" // program version, from git tag var _branch_ = "unknown" // git branch var _commitId_ = "0000000" // git commit id var _buildTime_ = "0000-00-00 00:00" // build time diff --git a/server/server.go b/server/server.go index ba685d5..e5b85aa 100644 --- a/server/server.go +++ b/server/server.go @@ -54,7 +54,7 @@ func createRouter() martini.Router { router.Get( "/api/articles/fid/:fid/:limit/:offset", api.ArticleList("fid")) router.Get( "/api/articles/tag/:tag/:limit/:offset", api.ArticleList("tag")) router.Get( "/api/articles/starred/:limit/:offset", api.ArticleList("starred")) - router.Get( "/api/articles/search/:limit/:offset", api.ArticleList("search")) + router.Get( "/api/articles/search/:deflimit", api.SearchList()) router.Put( "/api/articles/read", api.MarkArticlesRead()) router.Put( "/api/articles/starred", api.MarkArticlesStarred()) router.Get( "/api/article/content/:id", api.Article()) diff --git a/sitedata/client/include/qreader.js b/sitedata/client/include/qreader.js index d4cef7f..3e00748 100644 --- a/sitedata/client/include/qreader.js +++ b/sitedata/client/include/qreader.js @@ -325,13 +325,13 @@ QReader.app.controller("ArticleListController", function($location, $http, $rout } else if (route == "search") { - var query = $location.search().q || ""; + var query = encodeURIComponent($location.search().q || ""); if (query == "") { QDoc.SetError("路由错误:query 值无效。"); return } - request_url = QReader.api.articlesSearch + limit + "/" + offset + "?q=" + query; + request_url = QReader.api.articlesSearch + limit + "?q=" + query + "&page=" + page; $scope.pagelinkPrefix = "/#/articles/search?q=" + query + "&"; } } else if (route == "random") { @@ -371,6 +371,7 @@ QReader.app.controller("ArticleListController", function($location, $http, $rout if (data.success) { if (route == "unread" || route == "fid" || route == "tag" || route == "starred" || route == "search") { data.result.page = page; + limit = data.result.limit || limit; // result of api.SearchList() contains a variable named limit data.result.pagenum = Math.ceil(data.result.Number / limit); } $scope.data = data.result; @@ -516,7 +517,7 @@ QReader.app.controller("FeedListController", function($http, $scope, $route, $lo } var subscriptionRequestUrl = function(feedurl) { - var url = QReader.api.subscription + "?url=" + feedurl; + var url = QReader.api.subscription + "?url=" + encodeURIComponent(feedurl); return url; }