root/http/group_feed.cc

Revision 1a970cd0640f976f152341597a5249ec22acbba7, 5.7 KB (checked in by Antti-Juhani Kaijanaho <antti-juhani@…>, 21 months ago)

[msg::msg] Separate out db::msg and let msg::entity handle the rest

Signed-off-by: Antti-Juhani Kaijanaho <antti-juhani@…>

  • Property mode set to 100644
Line 
1/*  This file is part of Alue, the multiprotocol Internet discussion daemon
2
3    Copyright © 2010 Antti-Juhani Kaijanaho
4
5    Alue is free software: you can redistribute it and/or modify it
6    under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    Alue is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with Alue.  If not, see <http://www.gnu.org/licenses/>.
17
18 */
19
20#include "article_entry.hh"
21#include "error_resource.hh"
22#include "feed_resource.hh"
23#include "request.hh"
24#include "resource_exception.hh"
25
26#include "../config.hh"
27#include "../db/db.hh"
28#include "../msg/entity.hh"
29#include "../uri.hh"
30
31#include <list>
32#include <string>
33
34namespace http
35{
36        class group_feed : public feed_resource
37        {
38                db::group::const_ptr gr;
39                db::user::const_ptr u;
40                std::list<entry::const_ptr> entries;
41                boost::posix_time::ptime latest;
42        public:
43                group_feed(server::conn_cb cb, std::string path);
44        protected:
45                void prehook(boost::shared_ptr<request>);
46                std::string get_id_uri() const;
47                std::string get_alt_uri() const;
48                std::string get_title() const;
49                boost::posix_time::ptime get_updated() const;
50                const std::list<entry::const_ptr> &get_entries() const;
51        };
52
53        void group_feed::prehook(boost::shared_ptr<request> req)
54        {
55                u = req->get_user();
56                if (!gr->reading_authz(u)) {
57                        boost::shared_ptr<resource> er
58                                (new error_resource(cb, "403 Forbidden"));
59                        throw resource_exception(er);                       
60                }
61        }
62
63        group_feed::group_feed(server::conn_cb cb, std::string path)
64                        : feed_resource(cb)
65                        , latest(boost::posix_time::neg_infin)
66        {
67                try
68                {
69                        gr = cb.dbase().lookup_group(uri::percent_decode
70                                                     (path.substr(9)));
71                }
72                catch (db::no_such_group)
73                {
74                        boost::shared_ptr<resource> er
75                                (new error_resource(cb, "404 No such group"));
76                        throw resource_exception(er);
77                }
78
79                for (db::group::number i = gr->high_mark();
80                     i >= gr->low_mark(); i--)
81                {
82                        if (entries.size() > 20) break;
83                        if (!gr->is_article(i)) continue;
84                        db::msg::const_ptr art = gr->get_article(i);
85                        db::thread_node::const_ptr tn =
86                                cb.dbase().get_threaded().lookup_msgid
87                                (art->msgid());
88                        article_entry::ptr ae(new article_entry(tn,u));
89                        entries.push_back(ae);
90                        boost::posix_time::ptime date =
91                                art->get_entity()->parsed_date();
92                        if (latest < date) latest = date;
93                }
94                if (entries.empty()) latest = boost::posix_time::second_clock::
95                                              universal_time();
96        }
97
98        std::string group_feed::get_id_uri() const
99        {
100                std::ostringstream ss;
101                // FIXME: use a tag: uri
102                ss << "https://"
103                   << uri::percent_encode
104                        (config["canonical-name"].as<std::string>())
105                   << ":"
106                   << uri::percent_encode
107                        (config["https-port"].as<std::string>())
108                   << "/feed/gr/"
109                   << uri::percent_encode(gr->name());
110                return ss.str();
111        }
112
113        std::string group_feed::get_alt_uri() const
114        {
115                std::ostringstream ss;
116                ss << "https://"
117                   << uri::percent_encode
118                        (config["canonical-name"].as<std::string>())
119                   << ":"
120                   << uri::percent_encode
121                        (config["https-port"].as<std::string>())
122                   << "/gr/"
123                   << uri::percent_encode(gr->name());
124                return ss.str();
125        }
126
127        std::string group_feed::get_title() const
128        {
129                return gr->name() + " at " +
130                        config["canonical-name"].as<std::string>();
131        }
132        boost::posix_time::ptime group_feed::get_updated() const
133        {
134                return latest;
135        }
136        const std::list<feed_resource::entry::const_ptr> &
137        group_feed::get_entries() const
138        {
139                return entries;
140        }
141
142};
143
144namespace
145{
146        class factory : public server::http_resource_factory
147        {
148        public:
149                factory() {
150                        server::register_http_resource("/feed/gr", this);
151                }
152                boost::shared_ptr<http::resource> operator()
153                (server::conn_cb cb, std::string path) {
154                        boost::shared_ptr<http::resource> rv
155                                (new http::group_feed(cb, path));
156                        return rv;
157                }
158        };
159        factory f;
160}
Note: See TracBrowser for help on using the browser.