3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Content\Conversation\Repository;
24 use Friendica\BaseCollection;
25 use Friendica\Content\Conversation\Collection\UserDefinedChannels;
26 use Friendica\Content\Conversation\Entity;
27 use Friendica\Content\Conversation\Factory;
28 use Friendica\Database\Database;
29 use Psr\Log\LoggerInterface;
31 class UserDefinedChannel extends \Friendica\BaseRepository
33 protected static $table_name = 'channel';
35 public function __construct(Database $database, LoggerInterface $logger, Factory\UserDefinedChannel $factory)
37 parent::__construct($database, $logger, $factory);
41 * @param array $condition
42 * @param array $params
43 * @return UserDefinedChannels
46 protected function _select(array $condition, array $params = []): BaseCollection
48 $rows = $this->db->selectToArray(static::$table_name, [], $condition, $params);
50 $Entities = new UserDefinedChannels();
51 foreach ($rows as $fields) {
52 $Entities[] = $this->factory->createFromTableRow($fields);
59 * Fetch a single user channel
61 * @param int $id The id of the user defined channel
62 * @param int $uid The user that this channel belongs to. (Not part of the primary key)
63 * @return Entity\UserDefinedChannel
64 * @throws \Friendica\Network\HTTPException\NotFoundException
66 public function selectById(int $id, int $uid): Entity\UserDefinedChannel
68 return $this->_selectOne(['id' => $id, 'uid' => $uid]);
72 * Checks if the provided channel id exists for this user
78 public function existsById(int $id, int $uid): bool
80 return $this->exists(['id' => $id, 'uid' => $uid]);
84 * Delete the given channel
90 public function deleteById(int $id, int $uid): bool
92 return $this->db->delete('channel', ['id' => $id, 'uid' => $uid]);
96 * Fetch all user channels
99 * @return UserDefinedChannels
102 public function selectByUid(int $uid): UserDefinedChannels
104 return $this->_select(['uid' => $uid]);
107 public function save(Entity\UserDefinedChannel $Channel): Entity\UserDefinedChannel
110 'label' => $Channel->label,
111 'description' => $Channel->description,
112 'access-key' => $Channel->accessKey,
113 'uid' => $Channel->uid,
114 'circle' => $Channel->circle,
115 'include-tags' => $Channel->includeTags,
116 'exclude-tags' => $Channel->excludeTags,
117 'full-text-search' => $Channel->fullTextSearch,
118 'media-type' => $Channel->mediaType,
121 if ($Channel->code) {
122 $this->db->update(self::$table_name, $fields, ['uid' => $Channel->uid, 'id' => $Channel->code]);
124 $this->db->insert(self::$table_name, $fields, Database::INSERT_IGNORE);
126 $newChannelId = $this->db->lastInsertId();
128 $Channel = $this->selectById($newChannelId, $Channel->uid);