summaryrefslogtreecommitdiffstats
path: root/lib/Service/FolderServiceV2.php
blob: 1e710a37b4afbbef13abc5a7fae2af2547638c52 (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
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
<?php
/**
 * Nextcloud - News
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author    Alessandro Cosentino <cosenal@gmail.com>
 * @author    Bernhard Posselt <dev@bernhard-posselt.com>
 * @copyright 2012 Alessandro Cosentino
 * @copyright 2012-2014 Bernhard Posselt
 */

namespace OCA\News\Service;

use OC\AppFramework\Utility\TimeFactory;
use OCA\News\Db\Folder;
use OCA\News\Db\FolderMapperV2;
use OCA\News\Service\Exceptions\ServiceConflictException;
use OCA\News\Service\Exceptions\ServiceNotFoundException;
use OCP\AppFramework\Db\Entity;
use Psr\Log\LoggerInterface;

/**
 * Class FolderService
 *
 * @package OCA\News\Service
 */
class FolderServiceV2 extends Service
{
    /**
     * @var FeedServiceV2
     */
    private $feedService;
    /**
     * @var TimeFactory
     */
    private $timeFactory;

    public function __construct(
        FolderMapperV2 $mapper,
        FeedServiceV2 $feedService,
        TimeFactory $timeFactory,
        LoggerInterface $logger
    ) {
        parent::__construct($mapper, $logger);
        $this->feedService = $feedService;
        $this->timeFactory = $timeFactory;
    }

    /**
     * Finds all folders of a user
     *
     * @param string $userId The name/ID of the user
     * @param array  $params Filter parameters
     *
     * @return Folder[]
     */
    public function findAllForUser(string $userId, array $params = []): array
    {
        return $this->mapper->findAllFromUser($userId, $params);
    }

    /**
     * Find all folders and it's feeds.
     *
     * @param string $userId The name/ID of the owner
     *
     * @return Folder[]
     */
    public function findAllForUserRecursive(string $userId): array
    {
        $folders = $this->findAllForUser($userId);
        foreach ($folders as $folder) {
            $feeds = $this->feedService->findAllFromFolder($folder->getId());
            $folder->feeds = $feeds;
        }

        return $folders;
    }

    /**
     * Find all folders.
     *
     * @return Folder[]
     */
    public function findAll(): array
    {
        return $this->mapper->findAll();
    }

    /**
     * Create a folder
     *
     * @param string   $userId
     * @param string   $name
     * @param int|null $parent
     *
     * @return Folder
     */
    public function create(string $userId, string $name, ?int $parent = null): Entity
    {
        $folder = new Folder();
        $folder->setUserId($userId)
               ->setName($name)
               ->setParentId($parent)
               ->setOpened(true);

        return $this->mapper->insert($folder);
    }

    /**
     * Purge all deleted folders.
     *
     * @param string|null $userID       The user to purge
     * @param int|null    $minTimestamp The timestamp to purge from
     *
     * @return void
     */
    public function purgeDeleted(?string $userID, ?int $minTimestamp): void
    {
        $this->mapper->purgeDeleted($userID, $minTimestamp);
    }

    /**
     * Rename a folder
     *
     * @param string $userId   Folder owner
     * @param int    $folderId Folder ID
     * @param string $newName  New name
     *
     * @return Folder
     * @throws ServiceConflictException
     * @throws ServiceNotFoundException
     */
    public function rename(string $userId, int $folderId, string $newName): Entity
    {
        $folder = $this->find($userId, $folderId);
        $folder->setName($newName);

        return $this->mapper->update($folder);
    }

    /**
     * Mark a folder as deleted
     *
     * @param string $userId   Folder owner
     * @param int    $folderId Folder ID
     * @param bool   $mark     If the mark should be added or removed
     *
     * @return Folder
     * @throws ServiceConflictException
     * @throws ServiceNotFoundException
     */
    public function markDelete(string $userId, int $folderId, bool $mark): Entity
    {
        $folder = $this->find($userId, $folderId);
        $time = $mark ? $this->timeFactory->getTime() : 0;
        $folder->setDeletedAt($time);

        return $this->mapper->update($folder);
    }

    /**
     * Mark a folder as opened
     *
     * @param string   $userId   Folder owner
     * @param int|null $folderId Folder ID
     * @param bool     $open     If the mark should be added or removed
     *
     * @return Folder
     * @throws ServiceConflictException
     * @throws ServiceNotFoundException
     */
    public function open(string $userId, ?int $folderId, bool $open): Entity
    {
        $folder = $this->find($userId, $folderId);
        $folder->setOpened($open);
        return $this->mapper->update($folder);
    }

    /**
     * Mark a folder as read
     *
     * @param string   $userId    Folder owner
     * @param int      $id        Folder ID
     * @param int|null $newestItemId Highest item ID to mark as read
     *
     * @return int
     *
     * @throws ServiceConflictException
     * @throws ServiceNotFoundException
     */
    public function read(string $userId, int $id, ?int $newestItemId = null): int
    {
        $folder = $this->find($userId, $id);

        return $this->mapper->read($userId, $folder->getId(), $newestItemId);
    }
}