summaryrefslogtreecommitdiff
path: root/src/rssfeed.cpp
blob: 232f444f3cce6864ce71376cca1ad6e4d690e10f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include "rssfeed.h"

#include <algorithm>
#include <cerrno>
#include <cinttypes>
#include <cstring>
#include <curl/curl.h>
#include <functional>
#include <iostream>
#include <langinfo.h>
#include <sstream>
#include <sys/utsname.h>
#include <string.h>
#include <time.h>

#include "cache.h"
#include "config.h"
#include "configcontainer.h"
#include "confighandlerexception.h"
#include "dbexception.h"
#include "htmlrenderer.h"
#include "logger.h"
#include "scopemeasure.h"
#include "strprintf.h"
#include "tagsouppullparser.h"
#include "utils.h"

namespace newsboat {

RssFeed::RssFeed(Cache* c)
	: pubDate_(0)
	, ch(c)
	, search_feed(false)
	, is_rtl_(false)
	, idx(0)
	, order(0)
	, status_(DlStatus::SUCCESS)
{
}

RssFeed::~RssFeed()
{
}

unsigned int RssFeed::unread_item_count() const
{
	std::lock_guard<std::mutex> lock(item_mutex);
	return std::count_if(items_.begin(),
			items_.end(),
	[](const std::shared_ptr<RssItem>& item) {
		return item->unread();
	});
}

bool RssFeed::matches_tag(const std::string& tag)
{
	return std::find_if(
	tags_.begin(), tags_.end(), [&](const std::string& t) {
		return tag == t;
	}) != tags_.end();
}

std::string RssFeed::get_firsttag()
{
	for (const auto& t : tags_) {
		if (t.substr(0, 1) != "~") {
			return t;
		}
	}
	return "";
}

std::string RssFeed::get_tags() const
{
	std::string tags;
	for (const auto& t : tags_) {
		if (t.substr(0, 1) != "~" && t.substr(0, 1) != "!") {
			tags.append(t);
			tags.append(" ");
		}
	}
	return tags;
}

void RssFeed::set_tags(const std::vector<std::string>& tags)
{
	tags_ = tags;
}

std::string RssFeed::title() const
{
	bool found_title = false;
	std::string alt_title;
	for (const auto& tag : tags_) {
		if (tag.substr(0, 1) == "~") {
			found_title = true;
			alt_title = tag.substr(1, tag.length() - 1);
			break;
		}
	}
	return found_title
		? alt_title
		: utils::utf8_to_locale(title_);
}

bool RssFeed::hidden() const
{
	return std::any_of(tags_.begin(),
			tags_.end(),
	[](const std::string& tag) {
		return tag.substr(0, 1) == "!";
	});
}

std::shared_ptr<RssItem> RssFeed::get_item_by_guid(const std::string& guid)
{
	std::lock_guard<std::mutex> lock(item_mutex);
	return get_item_by_guid_unlocked(guid);
}

std::shared_ptr<RssItem> RssFeed::get_item_by_guid_unlocked(
	const std::string& guid)
{
	auto it = items_guid_map.find(guid);
	if (it != items_guid_map.end()) {
		return it->second;
	}
	LOG(Level::DEBUG,
		"RssFeed::get_item_by_guid_unlocked: hit dummy item!");
	LOG(Level::DEBUG,
		"RssFeed::get_item_by_guid_unlocked: items_guid_map.size = %" PRIu64,
		static_cast<uint64_t>(items_guid_map.size()));

	// should never happen!
	return std::shared_ptr<RssItem>(new RssItem(ch));
}

nonstd::optional<std::string> RssFeed::attribute_value(const std::string&
	attribname) const
{
	if (attribname == "feedtitle") {
		return title();
	} else if (attribname == "description") {
		return utils::utf8_to_locale(description());
	} else if (attribname == "feedlink") {
		return link();
	} else if (attribname == "feeddate") {
		return pubDate();
	} else if (attribname == "rssurl") {
		return rssurl();
	} else if (attribname == "unread_count") {
		return std::to_string(unread_item_count());
	} else if (attribname == "total_count") {
		return std::to_string(items_.size());
	} else if (attribname == "tags") {
		return get_tags();
	} else if (attribname == "feedindex") {
		return std::to_string(idx);
	}
	return nonstd::nullopt;
}

void RssFeed::update_items(std::vector<std::shared_ptr<RssFeed>> feeds)
{
	std::lock_guard<std::mutex> lock(item_mutex);
	if (query.empty()) {
		return;
	}

	LOG(Level::DEBUG, "RssFeed::update_items: query = `%s'", query);

	ScopeMeasure sm("RssFeed::update_items");

	Matcher m(query);

	items_.clear();
	items_guid_map.clear();

	for (const auto& feed : feeds) {
		if (feed->is_query_feed()) {
			// don't fetch items from other query feeds!
			continue;
		}
		for (const auto& item : feed->items()) {
			if (!item->deleted() && m.matches(item.get())) {
				LOG(Level::DEBUG, "RssFeed::update_items: Matcher matches!");
				item->set_feedptr(feed);
				items_.push_back(item);
				items_guid_map[item->guid()] = item;
			}
		}
	}

	sm.stopover("matching");

	std::sort(items_.begin(), items_.end());

	sm.stopover("sorting");
}

void RssFeed::set_rssurl(const std::string& u)
{
	rssurl_ = u;
	if (utils::is_query_url(u)) {
		/* Query string looks like this:
		 *
		 * query:Title:unread = "yes" and age between 0:7
		 *
		 * So we split by colons to get title and the query itself. */
		const auto tokens = utils::tokenize(u, ":");

		if (tokens.size() < 3) {
			throw _s("too few arguments");
		}

		/* "Between" operator requires a range, which contains a colon.
		 * Since we've been tokenizing by colon, we might've
		 * inadertently split the query itself. Let's reconstruct it! */
		auto query = tokens[2];
		for (auto it = tokens.begin() + 3; it != tokens.end(); ++it) {
			query += ":";
			query += *it;
		}
		// Have to check if the result is a valid query, just in case
		Matcher m;
		if (!m.parse(query)) {
			throw strprintf::fmt(
				_("`%s' is not a valid filter expression"),
				query);
		}

		LOG(Level::DEBUG,
			"RssFeed::set_rssurl: query name = `%s' expr = `%s'",
			tokens[1],
			query);

		set_title(tokens[1]);
		set_query(query);
	}
}

void RssFeed::sort(const ArticleSortStrategy& sort_strategy)
{
	std::lock_guard<std::mutex> lock(item_mutex);
	sort_unlocked(sort_strategy);
}

void RssFeed::sort_unlocked(const ArticleSortStrategy& sort_strategy)
{
	switch (sort_strategy.sm) {
	case ArtSortMethod::TITLE:
		std::stable_sort(items_.begin(),
			items_.end(),
			[&](const std::shared_ptr<RssItem>& a,
		const std::shared_ptr<RssItem>& b) {
			const auto cmp = utils::strnaturalcmp(utils::utf8_to_locale(a->title()),
					utils::utf8_to_locale(b->title()));
			return sort_strategy.sd == SortDirection::DESC ? (cmp > 0) : (cmp < 0);
		});
		break;
	case ArtSortMethod::FLAGS:
		std::stable_sort(items_.begin(),
			items_.end(),
			[&](const std::shared_ptr<RssItem>& a,
		const std::shared_ptr<RssItem>& b) {
			return sort_strategy.sd ==
				SortDirection::DESC
				? (strcmp(a->flags().c_str(),
						b->flags().c_str()) > 0)
				: (strcmp(a->flags().c_str(),
						b->flags().c_str()) < 0);
		});
		break;
	case ArtSortMethod::AUTHOR:
		std::stable_sort(items_.begin(),
			items_.end(),
			[&](const std::shared_ptr<RssItem>& a,
		const std::shared_ptr<RssItem>& b) {
			const auto author_a = utils::utf8_to_locale(a->author());
			const auto author_b = utils::utf8_to_locale(b->author());
			const auto cmp = strcmp(author_a.c_str(), author_b.c_str());
			return sort_strategy.sd == SortDirection::DESC ? (cmp > 0) : (cmp < 0);
		});
		break;
	case ArtSortMethod::LINK:
		std::stable_sort(items_.begin(),
			items_.end(),
			[&](const std::shared_ptr<RssItem>& a,
		const std::shared_ptr<RssItem>& b) {
			return sort_strategy.sd ==
				SortDirection::DESC
				? (strcmp(a->link().c_str(),
						b->link().c_str()) > 0)
				: (strcmp(a->link().c_str(),
						b->link().c_str()) < 0);
		});
		break;
	case ArtSortMethod::GUID:
		std::stable_sort(items_.begin(),
			items_.end(),
			[&](const std::shared_ptr<RssItem>& a,
		const std::shared_ptr<RssItem>& b) {
			return sort_strategy.sd ==
				SortDirection::DESC
				? (strcmp(a->guid().c_str(),
						b->guid().c_str()) > 0)
				: (strcmp(a->guid().c_str(),
						b->guid().c_str()) < 0);
		});
		break;
	case ArtSortMethod::DATE:
		std::stable_sort(items_.begin(),
			items_.end(),
			[&](const std::shared_ptr<RssItem>& a,
		const std::shared_ptr<RssItem>& b) {
			// date is descending by default
			return sort_strategy.sd == SortDirection::ASC
				? (a->pubDate_timestamp() >
					b->pubDate_timestamp())
				: (a->pubDate_timestamp() <
					b->pubDate_timestamp());
		});
		break;
	case ArtSortMethod::RANDOM:
		std::random_shuffle(items_.begin(), items_.end());
		break;
	}
}

void RssFeed::purge_deleted_items()
{
	std::lock_guard<std::mutex> lock(item_mutex);
	ScopeMeasure m1("RssFeed::purge_deleted_items");

	// Purge in items_guid_map
	{
		std::lock_guard<std::mutex> lock2(items_guid_map_mutex);
		for (const auto& item : items_) {
			if (item->deleted()) {
				items_guid_map.erase(item->guid());
			}
		}
	}

	items_.erase(std::remove_if(items_.begin(),
			items_.end(),
	[](const std::shared_ptr<RssItem> item) {
		return item->deleted();
	}),
	items_.end());
}

void RssFeed::set_feedptrs(std::shared_ptr<RssFeed> self)
{
	std::lock_guard<std::mutex> lock(item_mutex);
	for (const auto& item : items_) {
		item->set_feedptr(self);
	}
}

std::string RssFeed::get_status()
{
	std::lock_guard<std::mutex> guard(status_mutex_);

	switch (status_) {
	case DlStatus::SUCCESS:
		return " ";
	case DlStatus::TO_BE_DOWNLOADED:
		return "_";
	case DlStatus::DURING_DOWNLOAD:
		return ".";
	case DlStatus::DL_ERROR:
		return "x";
	}
	return "?";
}

void RssFeed::unload()
{
	std::lock_guard<std::mutex> lock(item_mutex);
	for (const auto& item : items_) {
		item->unload();
	}
}

void RssFeed::load()
{
	std::lock_guard<std::mutex> lock(item_mutex);
	ch->fetch_descriptions(this);
}

void RssFeed::mark_all_items_read()
{
	std::lock_guard<std::mutex> lock(item_mutex);
	for (const auto& item : items_) {
		item->set_unread_nowrite(false);
	}
}

} // namespace newsboat