2016-06-17 20:25:44 +02:00
|
|
|
'use strict';
|
|
|
|
|
2017-03-30 20:50:12 +02:00
|
|
|
const settings = require('../models/settings.js');
|
2016-06-19 19:16:40 +02:00
|
|
|
const api = require('../api.js');
|
2017-01-20 21:51:04 +01:00
|
|
|
const uri = require('../util/uri.js');
|
2016-06-19 19:16:40 +02:00
|
|
|
const AbstractList = require('./abstract_list.js');
|
2016-06-17 20:25:44 +02:00
|
|
|
const Post = require('./post.js');
|
|
|
|
|
2016-06-19 19:16:40 +02:00
|
|
|
class PostList extends AbstractList {
|
|
|
|
static getAround(id, searchQuery) {
|
2017-01-20 21:51:04 +01:00
|
|
|
return api.get(
|
|
|
|
uri.formatApiLink(
|
2017-03-30 20:50:12 +02:00
|
|
|
'post', id, 'around', {
|
|
|
|
query: PostList._decorateSearchQuery(searchQuery || ''),
|
|
|
|
fields: 'id',
|
|
|
|
}));
|
2016-06-17 20:25:44 +02:00
|
|
|
}
|
|
|
|
|
2017-02-09 00:48:06 +01:00
|
|
|
static search(text, offset, limit, fields) {
|
2017-01-20 21:51:04 +01:00
|
|
|
return api.get(
|
2020-06-04 20:09:35 +02:00
|
|
|
uri.formatApiLink(
|
|
|
|
'posts', {
|
|
|
|
query: PostList._decorateSearchQuery(text || ''),
|
|
|
|
offset: offset,
|
|
|
|
limit: limit,
|
|
|
|
fields: fields.join(','),
|
|
|
|
}))
|
2017-01-20 21:51:04 +01:00
|
|
|
.then(response => {
|
|
|
|
return Promise.resolve(Object.assign(
|
|
|
|
{},
|
|
|
|
response,
|
|
|
|
{results: PostList.fromResponse(response.results)}));
|
|
|
|
});
|
2016-06-17 20:25:44 +02:00
|
|
|
}
|
2017-03-30 20:50:12 +02:00
|
|
|
|
|
|
|
static _decorateSearchQuery(text) {
|
|
|
|
const browsingSettings = settings.get();
|
|
|
|
const disabledSafety = [];
|
2018-06-25 16:47:20 +02:00
|
|
|
if (api.safetyEnabled()) {
|
2017-03-30 20:50:12 +02:00
|
|
|
for (let key of Object.keys(browsingSettings.listPosts)) {
|
|
|
|
if (browsingSettings.listPosts[key] === false) {
|
|
|
|
disabledSafety.push(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (disabledSafety.length) {
|
|
|
|
text = `-rating:${disabledSafety.join(',')} ${text}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return text.trim();
|
|
|
|
}
|
|
|
|
|
2016-06-17 20:25:44 +02:00
|
|
|
}
|
|
|
|
|
2016-06-19 19:16:40 +02:00
|
|
|
PostList._itemClass = Post;
|
|
|
|
PostList._itemName = 'post';
|
|
|
|
|
2016-06-17 20:25:44 +02:00
|
|
|
module.exports = PostList;
|