Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
SignerPlugin
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
4 / 4
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMethod
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 search
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
5
 rowToSearchResultItem
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4/**
5 * SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
6 * SPDX-License-Identifier: AGPL-3.0-or-later
7 */
8
9namespace OCA\Libresign\Collaboration\Collaborators;
10
11use OCA\Libresign\Db\IdentifyMethodMapper;
12use OCA\Libresign\Service\Identify\SignerSearchContext;
13use OCP\Collaboration\Collaborators\ISearchPlugin;
14use OCP\Collaboration\Collaborators\ISearchResult;
15use OCP\Collaboration\Collaborators\SearchResultType;
16use OCP\IUserSession;
17
18class SignerPlugin implements ISearchPlugin {
19    public const TYPE_SIGNER = 50; // IShare::TYPE_SIGNER = 50; It's a custom share type. Not defined in OCP\Share\IShare
20
21    public function __construct(
22        protected IdentifyMethodMapper $identifyMethodMapper,
23        private SignerSearchContext $searchContext,
24        private IUserSession $userSession,
25    ) {
26    }
27
28    private function getMethod(): string {
29        return $this->searchContext->getMethod();
30    }
31
32    /**
33     * {@inheritdoc}
34     */
35    #[\Override]
36    public function search($search, $limit, $offset, ISearchResult $searchResult): bool {
37        $user = $this->userSession->getUser()->getUID();
38        $method = $this->getMethod();
39
40        $identifiers = $this->identifyMethodMapper->searchByIdentifierValue(
41            $search,
42            $user,
43            $method,
44            $limit + 1,
45            $offset,
46        );
47
48        $result = ['wide' => [], 'exact' => []];
49
50        $hasMore = count($identifiers) > $limit;
51        if ($hasMore) {
52            array_pop($identifiers);
53        }
54
55        foreach ($identifiers as $row) {
56            $item = $this->rowToSearchResultItem($row);
57            if (strtolower($row['identifier_value']) === strtolower($search)
58                || strtolower($row['display_name']) === strtolower($search)
59            ) {
60                $result['exact'][] = $item;
61            } else {
62                $result['wide'][] = $item;
63            }
64        }
65
66        $type = new SearchResultType('signer');
67        $searchResult->addResultSet($type, $result['wide'], $result['exact']);
68
69        return $hasMore;
70    }
71
72    private function rowToSearchResultItem(array $row): array {
73        $item = [
74            'label' => $row['display_name'],
75            'shareWithDisplayNameUnique' => $row['identifier_value'],
76            'method' => $row['identifier_key'],
77            'value' => [
78                'shareWith' => $row['identifier_value'],
79                'shareType' => self::TYPE_SIGNER,
80            ]
81        ];
82
83        return $item;
84    }
85}