summaryrefslogtreecommitdiffstats
path: root/lib/Search/ItemSearchProvider.php
blob: 29b4bf209d9470b23171ad63804a85ec0d97da19 (plain)
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
<?php
declare(strict_types=1);

namespace OCA\News\Search;

use OCA\News\Service\ItemServiceV2;
use OCA\News\AppInfo\Application;
use OCA\News\Db\ListType;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\Search\IProvider;
use OCP\Search\ISearchQuery;
use OCP\Search\SearchResult;
use OCP\Search\SearchResultEntry;

/**
 * Class ItemSearchProvider
 *
 * @package OCA\News\Search
 */
class ItemSearchProvider implements IProvider
{
    /** @var IL10N */
    private $l10n;

    /** @var IURLGenerator */
    private $urlGenerator;

    /** @var ItemServiceV2 */
    private $service;

    public function __construct(IL10N $l10n, IURLGenerator $urlGenerator, ItemServiceV2 $service)
    {
        $this->l10n = $l10n;
        $this->urlGenerator = $urlGenerator;
        $this->service = $service;
    }

    public function getId(): string
    {
        return 'news_item';
    }

    public function getName(): string
    {
        return $this->l10n->t('News articles');
    }

    public function getOrder(string $route, array $routeParameters): int
    {
        if (strpos($route, Application::NAME . '.') === 0) {
            // Active app, prefer my results
            return 1;
        }

        return 65;
    }

    private function stripTruncate(string $string, int $length = 50): string
    {
        $string = strip_tags(trim($string));
      
        if (strlen($string) > $length) {
            $string = wordwrap($string, $length);
            $string = explode("\n", $string, 2);
            $string = $string[0];
        }
      
        return $string;
    }

    public function search(IUser $user, ISearchQuery $query): SearchResult
    {
        $list = [];
        $offset = (int) ($query->getCursor() ?? 0);
        $limit = $query->getLimit();

        $search_result = $this->service->findAllWithFilters(
            $user->getUID(),
            ListType::ALL_ITEMS,
            $limit,
            $offset,
            false,
            [$query->getTerm()]
        );

        $last = end($search_result);
        if ($last === false) {
            return SearchResult::complete(
                $this->l10n->t('News'),
                []
            );
        }

        $icon = $this->urlGenerator->imagePath('core', 'filetypes/text.svg');
        
        foreach ($search_result as $item) {
            $list[] = new SearchResultEntry(
                $icon,
                $item->getTitle(),
                $this->stripTruncate($item->getBody(), 50),
                $this->urlGenerator->linkToRoute('news.page.index') . '#/items/feeds/' . $item->getFeedId()
            );
        }

        return SearchResult::paginated($this->l10n->t('News'), $list, $last->getId());
    }
}