// SPDX-License-Identifier: AGPL-3.0-or-later namespace OCA\Forum\Db; use OCA\Forum\AppInfo\Application; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\QBMapper; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; /** * @template-extends QBMapper */ class PostMapper extends QBMapper { public function __construct( IDBConnection $db, ) { parent::__construct($db, Application::tableName('forum_posts'), Post::class); } /** * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException * @throws DoesNotExistException */ public function find(int $id): Post { /* @var $qb IQueryBuilder */ $qb = $this->db->getQueryBuilder(); $qb->select('*') ->from($this->getTableName()) ->where( $qb->expr() ->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)) ) ->andWhere( $qb->expr()->isNull('deleted_at') ); return $this->findEntity($qb); } /** * @return array */ public function findByThreadId(int $threadId, int $limit = 50, int $offset = 0): array { /* @var $qb IQueryBuilder */ $qb = $this->db->getQueryBuilder(); $qb->select('*') ->from($this->getTableName()) ->where( $qb->expr()->eq('thread_id', $qb->createNamedParameter($threadId, IQueryBuilder::PARAM_INT)) ) ->andWhere( $qb->expr()->isNull('deleted_at') ) ->orderBy('created_at', 'ASC') ->setMaxResults($limit) ->setFirstResult($offset); return $this->findEntities($qb); } /** * @return array */ public function findByAuthorId(string $authorId, int $limit = 50, int $offset = 0, bool $excludeFirstPosts = false): array { /* @var $qb IQueryBuilder */ $qb = $this->db->getQueryBuilder(); $qb->select('p.*') ->from($this->getTableName(), 'p') ->innerJoin('p', 'forum_threads', 't', $qb->expr()->eq('p.thread_id', 't.id')) ->where( $qb->expr()->eq('p.author_id', $qb->createNamedParameter($authorId, IQueryBuilder::PARAM_STR)) ) ->andWhere( $qb->expr()->isNull('p.deleted_at') ) ->andWhere( $qb->expr()->isNull('t.deleted_at') ); if ($excludeFirstPosts) { $qb->andWhere( $qb->expr()->eq('p.is_first_post', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL)) ); } $qb->orderBy('p.created_at', 'DESC') ->setMaxResults($limit) ->setFirstResult($offset); return $this->findEntities($qb); } /** * Find posts by multiple IDs * * @param array $ids * @return array */ public function findByIds(array $ids): array { if (empty($ids)) { return []; } $qb = $this->db->getQueryBuilder(); $qb->select('*') ->from($this->getTableName()) ->where($qb->expr()->in('id', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY))) ->andWhere($qb->expr()->isNull('deleted_at')); return $this->findEntities($qb); } /** * @return array */ public function findAll(): array { /* @var $qb IQueryBuilder */ $qb = $this->db->getQueryBuilder(); $qb->select('p.*') ->from($this->getTableName(), 'p') ->innerJoin('p', 'forum_threads', 't', $qb->expr()->eq('p.thread_id', 't.id')) ->where( $qb->expr()->isNull('p.deleted_at') ) ->andWhere( $qb->expr()->isNull('t.deleted_at') ) ->orderBy('p.created_at', 'DESC'); return $this->findEntities($qb); } /** * Count all replies (posts excluding first posts) */ public function countAll(): int { $qb = $this->db->getQueryBuilder(); $qb->select($qb->func()->count('*', 'count')) ->from($this->getTableName(), 'p') ->innerJoin('p', 'forum_threads', 't', $qb->expr()->eq('p.thread_id', 't.id')) ->where( $qb->expr()->isNull('p.deleted_at') ) ->andWhere( $qb->expr()->isNull('t.deleted_at') ) ->andWhere( $qb->expr()->eq('p.is_first_post', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL)) ); $result = $qb->executeQuery(); $row = $result->fetch(); $result->closeCursor(); return (int)($row['count'] ?? 0); } /** * Count posts created since a timestamp */ public function countSince(int $timestamp): int { $qb = $this->db->getQueryBuilder(); $qb->select($qb->func()->count('*', 'count')) ->from($this->getTableName(), 'p') ->innerJoin('p', 'forum_threads', 't', $qb->expr()->eq('p.thread_id', 't.id')) ->where($qb->expr()->gte('p.created_at', $qb->createNamedParameter($timestamp, IQueryBuilder::PARAM_INT))) ->andWhere( $qb->expr()->isNull('p.deleted_at') ) ->andWhere( $qb->expr()->isNull('t.deleted_at') ) ->andWhere( $qb->expr()->eq('p.is_first_post', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL)) ); $result = $qb->executeQuery(); $row = $result->fetch(); $result->closeCursor(); return (int)($row['count'] ?? 0); } /** * Find the latest non-deleted post in a thread, excluding a specific post ID * * @param int $threadId Thread ID * @param int|null $excludePostId Post ID to exclude (typically the one being deleted) * @return Post|null Latest post or null if no posts found */ public function findLatestByThreadId(int $threadId, ?int $excludePostId = null): ?Post { $qb = $this->db->getQueryBuilder(); $qb->select('*') ->from($this->getTableName()) ->where($qb->expr()->eq('thread_id', $qb->createNamedParameter($threadId, IQueryBuilder::PARAM_INT))) ->andWhere($qb->expr()->isNull('deleted_at')); if ($excludePostId !== null) { $qb->andWhere($qb->expr()->neq('id', $qb->createNamedParameter($excludePostId, IQueryBuilder::PARAM_INT))); } $qb->orderBy('created_at', 'DESC') ->setMaxResults(1); try { return $this->findEntity($qb); } catch (DoesNotExistException $e) { return null; } } /** * Count unread posts in a thread after a specific post ID * * @param int $threadId Thread ID * @param int $afterPostId Post ID to count after (0 to count all posts) * @return int Number of posts after the given post ID */ public function countUnreadInThread(int $threadId, int $afterPostId = 0): int { $qb = $this->db->getQueryBuilder(); $qb->select($qb->func()->count('*', 'count')) ->from($this->getTableName()) ->where($qb->expr()->eq('thread_id', $qb->createNamedParameter($threadId, IQueryBuilder::PARAM_INT))) ->andWhere($qb->expr()->isNull('deleted_at')); if ($afterPostId > 0) { $qb->andWhere($qb->expr()->gt('id', $qb->createNamedParameter($afterPostId, IQueryBuilder::PARAM_INT))); } $result = $qb->executeQuery(); $row = $result->fetch(); $result->closeCursor(); return (int)($row['count'] ?? 0); } /** * Find the first post in a thread * * @param int $threadId Thread ID * @return Post|null First post or null if not found */ public function findFirstPostByThreadId(int $threadId): ?Post { $qb = $this->db->getQueryBuilder(); $qb->select('*') ->from($this->getTableName()) ->where($qb->expr()->eq('thread_id', $qb->createNamedParameter($threadId, IQueryBuilder::PARAM_INT))) ->andWhere($qb->expr()->eq('is_first_post', $qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL))) ->andWhere($qb->expr()->isNull('deleted_at')) ->setMaxResults(1); try { return $this->findEntity($qb); } catch (DoesNotExistException $e) { return null; } } /** * Find replies (non-first posts) in a thread with pagination * * @param int $threadId Thread ID * @param int $limit Maximum results * @param int $offset Results offset * @return array */ public function findRepliesByThreadId(int $threadId, int $limit = 50, int $offset = 0): array { $qb = $this->db->getQueryBuilder(); $qb->select('*') ->from($this->getTableName()) ->where($qb->expr()->eq('thread_id', $qb->createNamedParameter($threadId, IQueryBuilder::PARAM_INT))) ->andWhere($qb->expr()->eq('is_first_post', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))) ->andWhere($qb->expr()->isNull('deleted_at')) ->orderBy('created_at', 'ASC') ->setMaxResults($limit) ->setFirstResult($offset); return $this->findEntities($qb); } /** * Count replies (non-first posts) in a thread * * @param int $threadId Thread ID * @return int Number of replies */ public function countRepliesByThreadId(int $threadId): int { $qb = $this->db->getQueryBuilder(); $qb->select($qb->func()->count('*', 'count')) ->from($this->getTableName()) ->where($qb->expr()->eq('thread_id', $qb->createNamedParameter($threadId, IQueryBuilder::PARAM_INT))) ->andWhere($qb->expr()->eq('is_first_post', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))) ->andWhere($qb->expr()->isNull('deleted_at')); $result = $qb->executeQuery(); $row = $result->fetch(); $result->closeCursor(); return (int)($row['count'] ?? 0); } /** * Find the oldest unread reply in a thread * * @param int $threadId Thread ID * @param int $afterPostId Post ID to look after (last read post ID) * @return Post|null The oldest unread reply or null if all are read */ public function findOldestUnreadReply(int $threadId, int $afterPostId): ?Post { $qb = $this->db->getQueryBuilder(); $qb->select('*') ->from($this->getTableName()) ->where($qb->expr()->eq('thread_id', $qb->createNamedParameter($threadId, IQueryBuilder::PARAM_INT))) ->andWhere($qb->expr()->eq('is_first_post', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))) ->andWhere($qb->expr()->gt('id', $qb->createNamedParameter($afterPostId, IQueryBuilder::PARAM_INT))) ->andWhere($qb->expr()->isNull('deleted_at')) ->orderBy('created_at', 'ASC') ->setMaxResults(1); try { return $this->findEntity($qb); } catch (DoesNotExistException $e) { return null; } } /** * Get the position (0-indexed) of a reply in the replies list ordered by created_at ASC * * @param int $threadId Thread ID * @param int $postId Post ID to find position of * @return int Position (0-indexed) */ public function getReplyPosition(int $threadId, int $postId): int { // First get the created_at of the target post $targetQb = $this->db->getQueryBuilder(); $targetQb->select('created_at') ->from($this->getTableName()) ->where($targetQb->expr()->eq('id', $targetQb->createNamedParameter($postId, IQueryBuilder::PARAM_INT))); $targetResult = $targetQb->executeQuery(); $targetRow = $targetResult->fetch(); $targetResult->closeCursor(); if (!$targetRow) { return 0; } $targetCreatedAt = (int)$targetRow['created_at']; // Count replies created before the target post $qb = $this->db->getQueryBuilder(); $qb->select($qb->func()->count('*', 'position')) ->from($this->getTableName()) ->where($qb->expr()->eq('thread_id', $qb->createNamedParameter($threadId, IQueryBuilder::PARAM_INT))) ->andWhere($qb->expr()->eq('is_first_post', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))) ->andWhere($qb->expr()->isNull('deleted_at')) ->andWhere($qb->expr()->lt('created_at', $qb->createNamedParameter($targetCreatedAt, IQueryBuilder::PARAM_INT))); $result = $qb->executeQuery(); $row = $result->fetch(); $result->closeCursor(); return (int)($row['position'] ?? 0); } /** * Find recent replies (non-first posts) in specified categories * * @param array $categoryIds Category IDs to filter by * @param int $limit Maximum results * @return array */ public function findRecentReplies(array $categoryIds, int $limit = 7): array { if (empty($categoryIds)) { return []; } $qb = $this->db->getQueryBuilder(); $qb->select('p.*') ->from($this->getTableName(), 'p') ->innerJoin('p', 'forum_threads', 't', $qb->expr()->eq('p.thread_id', 't.id')) ->where($qb->expr()->in('t.category_id', $qb->createNamedParameter($categoryIds, IQueryBuilder::PARAM_INT_ARRAY))) ->andWhere($qb->expr()->eq('p.is_first_post', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))) ->andWhere($qb->expr()->isNull('p.deleted_at')) ->andWhere($qb->expr()->isNull('t.deleted_at')) ->andWhere($qb->expr()->eq('t.is_hidden', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))) ->orderBy('p.created_at', 'DESC') ->setMaxResults($limit); return $this->findEntities($qb); } /** * Search posts by content (replies only, excluding first posts) * * @param IQueryBuilder $qb QueryBuilder instance (with parameters already bound) * @param \OCP\DB\QueryBuilder\ICompositeExpression $whereConditions WHERE expression from QueryParser * @param array $categoryIds Category IDs to search in * @param int $limit Maximum results * @param int $offset Results offset * @return array */ public function search(IQueryBuilder $qb, \OCP\DB\QueryBuilder\ICompositeExpression $whereConditions, array $categoryIds, int $limit = 50, int $offset = 0): array { // Select posts with JOIN to threads for category filtering $qb->select('p.*') ->from($this->getTableName(), 'p') ->innerJoin('p', 'forum_threads', 't', $qb->expr()->eq('p.thread_id', 't.id')) ->where($qb->expr()->in('t.category_id', $qb->createNamedParameter($categoryIds, IQueryBuilder::PARAM_INT_ARRAY))) ->andWhere($qb->expr()->eq('p.is_first_post', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))) ->andWhere($qb->expr()->isNull('p.deleted_at')) ->andWhere($qb->expr()->isNull('t.deleted_at')) ->andWhere($qb->expr()->eq('t.is_hidden', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))) ->andWhere($whereConditions) ->orderBy('p.created_at', 'DESC') ->setMaxResults($limit) ->setFirstResult($offset); return $this->findEntities($qb); } /** * Find a post by ID including soft-deleted posts * * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException * @throws DoesNotExistException */ public function findIncludingDeleted(int $id): Post { $qb = $this->db->getQueryBuilder(); $qb->select('*') ->from($this->getTableName()) ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))); return $this->findEntity($qb); } /** * Find soft-deleted replies (non-first-posts) with pagination, search, and sorting * * @return array */ public function findDeletedReplies(int $limit = 20, int $offset = 0, string $search = '', string $sort = 'newest'): array { $qb = $this->db->getQueryBuilder(); $qb->select('*') ->from($this->getTableName()) ->where($qb->expr()->isNotNull('deleted_at')) ->andWhere($qb->expr()->eq('is_first_post', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))); if ($search !== '') { $qb->andWhere($qb->expr()->iLike('content', $qb->createNamedParameter('%' . $this->db->escapeLikeParameter($search) . '%'))); } $qb->orderBy('deleted_at', $sort === 'oldest' ? 'ASC' : 'DESC') ->setMaxResults($limit) ->setFirstResult($offset); return $this->findEntities($qb); } /** * Count soft-deleted replies */ public function countDeletedReplies(string $search = ''): int { $qb = $this->db->getQueryBuilder(); $qb->select($qb->func()->count('*', 'count')) ->from($this->getTableName()) ->where($qb->expr()->isNotNull('deleted_at')) ->andWhere($qb->expr()->eq('is_first_post', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))); if ($search !== '') { $qb->andWhere($qb->expr()->iLike('content', $qb->createNamedParameter('%' . $this->db->escapeLikeParameter($search) . '%'))); } $result = $qb->executeQuery(); $count = (int)($result->fetchOne() ?? 0); $result->closeCursor(); return $count; } /** * Count all posts for a thread, including deleted posts */ public function countByThreadIdIncludingDeleted(int $threadId): int { $qb = $this->db->getQueryBuilder(); $qb->select($qb->func()->count('*', 'count')) ->from($this->getTableName()) ->where($qb->expr()->eq('thread_id', $qb->createNamedParameter($threadId, IQueryBuilder::PARAM_INT))); $result = $qb->executeQuery(); $count = (int)($result->fetchOne() ?? 0); $result->closeCursor(); return $count; } /** * Reassign all posts from one author to another * * @param string $fromAuthorId Current author ID (e.g., "guest:abc123") * @param string $toAuthorId New author ID (e.g., "john") * @return int Number of posts updated */ public function reassignAuthor(string $fromAuthorId, string $toAuthorId): int { $qb = $this->db->getQueryBuilder(); $qb->update($this->getTableName()) ->set('author_id', $qb->createNamedParameter($toAuthorId, IQueryBuilder::PARAM_STR)) ->set('updated_at', $qb->createNamedParameter(time(), IQueryBuilder::PARAM_INT)) ->where($qb->expr()->eq('author_id', $qb->createNamedParameter($fromAuthorId, IQueryBuilder::PARAM_STR))); return $qb->executeStatement(); } /** * Count posts by author (including deleted) */ public function countByAuthorId(string $authorId): array { $qb = $this->db->getQueryBuilder(); $qb->select( $qb->func()->count('*', 'total'), ) ->from($this->getTableName()) ->where($qb->expr()->eq('author_id', $qb->createNamedParameter($authorId, IQueryBuilder::PARAM_STR))); $result = $qb->executeQuery(); $row = $result->fetch(); $result->closeCursor(); // Count first posts (threads) vs replies separately $qb2 = $this->db->getQueryBuilder(); $qb2->select($qb2->func()->count('*', 'count')) ->from($this->getTableName()) ->where($qb2->expr()->eq('author_id', $qb2->createNamedParameter($authorId, IQueryBuilder::PARAM_STR))) ->andWhere($qb2->expr()->eq('is_first_post', $qb2->createNamedParameter(true, IQueryBuilder::PARAM_BOOL))) ->andWhere($qb2->expr()->isNull('deleted_at')); $result2 = $qb2->executeQuery(); $row2 = $result2->fetch(); $result2->closeCursor(); $qb3 = $this->db->getQueryBuilder(); $qb3->select($qb3->func()->count('*', 'count')) ->from($this->getTableName()) ->where($qb3->expr()->eq('author_id', $qb3->createNamedParameter($authorId, IQueryBuilder::PARAM_STR))) ->andWhere($qb3->expr()->eq('is_first_post', $qb3->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))) ->andWhere($qb3->expr()->isNull('deleted_at')); $result3 = $qb3->executeQuery(); $row3 = $result3->fetch(); $result3->closeCursor(); return [ 'total' => (int)($row['total'] ?? 0), 'threads' => (int)($row2['count'] ?? 0), 'replies' => (int)($row3['count'] ?? 0), ]; } /** * Find all posts for a thread, including deleted posts * * @return array */ public function findByThreadIdIncludingDeleted(int $threadId, int $limit = 50, int $offset = 0): array { $qb = $this->db->getQueryBuilder(); $qb->select('*') ->from($this->getTableName()) ->where($qb->expr()->eq('thread_id', $qb->createNamedParameter($threadId, IQueryBuilder::PARAM_INT))) ->orderBy('created_at', 'ASC') ->setMaxResults($limit) ->setFirstResult($offset); return $this->findEntities($qb); } }