Changeset 43f521291669f483d361949fa6c886c0fbd94daf
- Timestamp:
- 08/26/10 20:21:08 (18 months ago)
- Author:
- Antti-Juhani Kaijanaho <antti-juhani@…>
- Children:
- 6c86e7ccc32f1c99d204262844a52b1b06359145
- Parents:
- 7ccc366e52cc1e51e123b39fccaf56267361ca58
- git-committer:
- Antti-Juhani Kaijanaho <antti-juhani@…> (08/26/10 20:21:08)
- Message:
-
Add support for paginating thread and article lists.
Signed-off-by: Antti-Juhani Kaijanaho <antti-juhani@…>
- Files:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r751fac6
|
r43f5212
|
|
| 36 | 36 | #include "../tlate/tlate.hh" |
| 37 | 37 | #include "../uri.hh" |
| | 38 | #include "../util.hh" |
| 38 | 39 | |
| 39 | 40 | #include <boost/shared_ptr.hpp> |
| … |
… |
|
| 103 | 104 | if (!is_single) |
| 104 | 105 | { |
| 105 | | tlate::value::ptr tv(new tlate::thread_value(tn, u)); |
| | 106 | std::string f = req->get_form_field("s"); |
| | 107 | size_t start = util::is_number(f) |
| | 108 | ? util::parse_nonnegative_integer<size_t>(f) |
| | 109 | : 0; |
| | 110 | f = req->get_form_field("n"); |
| | 111 | size_t length = util::is_number(f) |
| | 112 | ? util::parse_nonnegative_integer<size_t>(f) |
| | 113 | : 10; |
| | 114 | |
| | 115 | tlate::thread_value::ptr tv |
| | 116 | (new tlate::thread_value(tn, u)); |
| | 117 | tv->slice_window(start, length); |
| 106 | 118 | am->insert("thread", tv); |
| | 119 | |
| | 120 | tlate::data_model::ptr pg(new tlate::data_model); |
| | 121 | pg->insert("first", start); |
| | 122 | pg->insert("last", start + tv->size()); |
| | 123 | pg->insert("size", tv->size()); |
| | 124 | pg->insert("max_size", length); |
| | 125 | |
| | 126 | if (length > tv->size()) |
| | 127 | { |
| | 128 | ::uri u = req->get_uri(); |
| | 129 | u.replace_query_param("s", start + length); |
| | 130 | u.replace_query_param("n", length); |
| | 131 | pg->insert("next", u.to_string()); |
| | 132 | } |
| | 133 | |
| | 134 | if (start > 0) |
| | 135 | { |
| | 136 | ::uri u = req->get_uri(); |
| | 137 | u.replace_query_param("s", |
| | 138 | start > length |
| | 139 | ? start - length |
| | 140 | : 0); |
| | 141 | u.replace_query_param("n", length); |
| | 142 | pg->insert("prev", u.to_string()); |
| | 143 | } |
| | 144 | am->insert("page", pg); |
| 107 | 145 | } |
| 108 | 146 | |
-
|
r751fac6
|
r43f5212
|
|
| 94 | 94 | else |
| 95 | 95 | { |
| | 96 | std::string f = req->get_form_field("s"); |
| | 97 | size_t start = util::is_number(f) |
| | 98 | ? util::parse_nonnegative_integer<size_t>(f) |
| | 99 | : 0; |
| | 100 | f = req->get_form_field("n"); |
| | 101 | size_t length = util::is_number(f) |
| | 102 | ? util::parse_nonnegative_integer<size_t>(f) |
| | 103 | : 10; |
| | 104 | |
| 96 | 105 | tlate::list_value::ptr tl(new tlate::list_value); |
| 97 | 106 | std::list<db::thread_node::ptr> thrs |
| … |
… |
|
| 100 | 109 | thrs.sort(db::thread_node::cmp_latest_date_rev); |
| 101 | 110 | |
| | 111 | size_t cur = 0; |
| | 112 | bool more = false; |
| 102 | 113 | for (std::list<db::thread_node::ptr>::const_iterator |
| 103 | 114 | it = thrs.begin(); |
| 104 | 115 | it != thrs.end(); it++) |
| 105 | 116 | { |
| 106 | | tl->push_back(tlate::msg_value::mk(*it, u)); |
| | 117 | cur++; |
| | 118 | if (cur >= start + length) |
| | 119 | { |
| | 120 | more = true; |
| | 121 | break; |
| | 122 | } |
| | 123 | if (cur > start) |
| | 124 | tl->push_back |
| | 125 | (tlate::msg_value::mk(*it, u)); |
| 107 | 126 | } |
| 108 | 127 | am->insert("threads", tl); |
| | 128 | |
| | 129 | tlate::data_model::ptr pg(new tlate::data_model); |
| | 130 | pg->insert("first", start); |
| | 131 | pg->insert("last", cur - 1); |
| | 132 | pg->insert("size", cur - start); |
| | 133 | pg->insert("max_size", length); |
| | 134 | |
| | 135 | if (more) |
| | 136 | { |
| | 137 | ::uri u = req->get_uri(); |
| | 138 | u.replace_query_param("s", start + length); |
| | 139 | u.replace_query_param("n", length); |
| | 140 | pg->insert("next", u.to_string()); |
| | 141 | } |
| | 142 | |
| | 143 | if (start > 0) |
| | 144 | { |
| | 145 | ::uri u = req->get_uri(); |
| | 146 | u.replace_query_param("s", |
| | 147 | start > length |
| | 148 | ? start - length |
| | 149 | : 0); |
| | 150 | u.replace_query_param("n", length); |
| | 151 | pg->insert("prev", u.to_string()); |
| | 152 | } |
| | 153 | am->insert("page", pg); |
| 109 | 154 | } |
| 110 | 155 | } |
-
|
r868b365
|
r43f5212
|
|
| 1 | 1 | /* This file is part of Alue, the multiprotocol Internet discussion daemon |
| 2 | 2 | |
| 3 | | Copyright © 2009 Antti-Juhani Kaijanaho |
| | 3 | Copyright © 2009, 2010 Antti-Juhani Kaijanaho |
| 4 | 4 | |
| 5 | 5 | Alue is free software: you can redistribute it and/or modify it |
| … |
… |
|
| 44 | 44 | insert(var, new string_value(val)); |
| 45 | 45 | } |
| | 46 | void insert(std::string var, int val, unsigned md = 1) { |
| | 47 | insert(var, new string_value(val, md)); |
| | 48 | } |
| 46 | 49 | void insert(std::string var, value::const_ptr val) { |
| 47 | 50 | myassert(var.find('.') == std::string::npos); |
-
|
r299b297
|
r43f5212
|
|
| 20 | 20 | #include "thread_value.hh" |
| 21 | 21 | |
| | 22 | #include <list> |
| | 23 | |
| 22 | 24 | namespace tlate |
| 23 | 25 | { |
| … |
… |
|
| 26 | 28 | : u(u) |
| 27 | 29 | { |
| | 30 | typedef std::list<db::thread_node::ptr> tlnc; |
| | 31 | |
| 28 | 32 | tnl.push_back(tn); |
| 29 | 33 | for (tl::const_iterator it = tnl.begin(); it != tnl.end(); it++) |
| … |
… |
|
| 36 | 40 | } |
| 37 | 41 | } |
| 38 | | tnl.sort(db::thread_node::cmp_date); |
| | 42 | std::stable_sort(tnl.begin(), tnl.end(), |
| | 43 | db::thread_node::cmp_date); |
| | 44 | } |
| | 45 | void thread_value::slice_window(size_t start, size_t length) |
| | 46 | { |
| | 47 | if (start > tnl.size()) |
| | 48 | { |
| | 49 | tnl.clear(); |
| | 50 | return; |
| | 51 | } |
| | 52 | if (start > 0) tnl.erase(tnl.begin(), tnl.begin()+start); |
| | 53 | if (length > tnl.size()) |
| | 54 | tnl.erase(tnl.begin()+length, tnl.end()); |
| 39 | 55 | } |
| 40 | 56 | } |
-
|
r868b365
|
r43f5212
|
|
| 1 | 1 | /* This file is part of Alue, the multiprotocol Internet discussion daemon |
| 2 | 2 | |
| 3 | | Copyright © 2009 Antti-Juhani Kaijanaho |
| | 3 | Copyright © 2009, 2010 Antti-Juhani Kaijanaho |
| 4 | 4 | |
| 5 | 5 | Alue is free software: you can redistribute it and/or modify it |
| … |
… |
|
| 26 | 26 | #include "../db/threaded.hh" |
| 27 | 27 | |
| 28 | | #include <list> |
| | 28 | #include <vector> |
| 29 | 29 | |
| 30 | 30 | namespace db { class user; } |
| … |
… |
|
| 34 | 34 | class thread_value : public sequential_value |
| 35 | 35 | { |
| 36 | | typedef std::list<db::thread_node::const_ptr> tl; |
| 37 | | typedef std::list<db::thread_node::ptr> tlnc; |
| | 36 | typedef std::vector<db::thread_node::const_ptr> tl; |
| 38 | 37 | tl tnl; |
| 39 | 38 | boost::shared_ptr<const db::user> u; |
| … |
… |
|
| 69 | 68 | |
| 70 | 69 | public: |
| | 70 | typedef boost::shared_ptr<thread_value> ptr; |
| | 71 | typedef boost::shared_ptr<const thread_value> const_ptr; |
| | 72 | |
| 71 | 73 | const_iterator begin() const { return new vit(tnl.begin(), u); } |
| 72 | 74 | const_iterator end() const { return new vit(tnl.end(), u); } |
| … |
… |
|
| 74 | 76 | thread_value(db::thread_node::const_ptr tn, |
| 75 | 77 | boost::shared_ptr<const db::user> u); |
| | 78 | void slice_window(size_t start, size_t length); |
| | 79 | size_t size() const { return tnl.size(); } |
| 76 | 80 | }; |
| 77 | 81 | } |
-
|
r868b365
|
r43f5212
|
|
| 1 | 1 | /* This file is part of Alue, the multiprotocol Internet discussion daemon |
| 2 | 2 | |
| 3 | | Copyright © 2009 Antti-Juhani Kaijanaho |
| | 3 | Copyright © 2009, 2010 Antti-Juhani Kaijanaho |
| 4 | 4 | |
| 5 | 5 | Alue is free software: you can redistribute it and/or modify it |
| … |
… |
|
| 118 | 118 | } |
| 119 | 119 | |
| | 120 | void uri::replace_query_param(std::string name, int val) |
| | 121 | { |
| | 122 | std::ostringstream os; |
| | 123 | os << val; |
| | 124 | replace_query_param(name, os.str()); |
| | 125 | } |
| | 126 | |
| 120 | 127 | void uri::remove_query_param(std::string name) |
| 121 | 128 | { |
| … |
… |
|
| 123 | 130 | { |
| 124 | 131 | size_t nix = query.find("&", ix+1); |
| 125 | | if (query.substr(ix, name.length()) == name) |
| | 132 | if (query.substr(ix, name.length()+1) == name + "=") |
| 126 | 133 | query.erase(ix, nix - ix); |
| 127 | 134 | else |
-
|
r868b365
|
r43f5212
|
|
| 1 | 1 | /* This file is part of Alue, the multiprotocol Internet discussion daemon |
| 2 | 2 | |
| 3 | | Copyright © 2009 Antti-Juhani Kaijanaho |
| | 3 | Copyright © 2009, 2010 Antti-Juhani Kaijanaho |
| 4 | 4 | |
| 5 | 5 | Alue is free software: you can redistribute it and/or modify it |
| … |
… |
|
| 58 | 58 | std::string get_query_param(std::string name) const; |
| 59 | 59 | void replace_query_param(std::string name, std::string new_value); |
| | 60 | void replace_query_param(std::string name, int new_value); |
| 60 | 61 | void remove_query_param(std::string name); |
| 61 | 62 | |
-
|
r12f5162
|
r43f5212
|
|
| 38 | 38 | bool is_number(std::string s) |
| 39 | 39 | { |
| | 40 | if (s.empty()) return false; |
| 40 | 41 | for (size_t i = 0; i < s.length(); i++) { |
| 41 | 42 | if (!is_digit(s[i])) return false; |