misuzu/src/Forum/topic.php

710 lines
25 KiB
PHP
Raw Normal View History

2018-05-23 03:41:57 +02:00
<?php
define('MSZ_TOPIC_TYPE_DISCUSSION', 0);
define('MSZ_TOPIC_TYPE_STICKY', 1);
define('MSZ_TOPIC_TYPE_ANNOUNCEMENT', 2);
2018-12-30 19:58:30 +01:00
define('MSZ_TOPIC_TYPE_GLOBAL_ANNOUNCEMENT', 3);
2018-05-23 03:41:57 +02:00
define('MSZ_TOPIC_TYPES', [
MSZ_TOPIC_TYPE_DISCUSSION,
MSZ_TOPIC_TYPE_STICKY,
MSZ_TOPIC_TYPE_ANNOUNCEMENT,
2018-12-30 19:58:30 +01:00
MSZ_TOPIC_TYPE_GLOBAL_ANNOUNCEMENT,
2018-05-23 03:41:57 +02:00
]);
2018-12-30 19:58:30 +01:00
define('MSZ_TOPIC_TYPE_ORDER', [ // in which order to display topics, only add types here that should appear above others
MSZ_TOPIC_TYPE_GLOBAL_ANNOUNCEMENT,
MSZ_TOPIC_TYPE_ANNOUNCEMENT,
MSZ_TOPIC_TYPE_STICKY,
]);
2019-06-10 19:04:53 +02:00
function forum_topic_is_valid_type(int $type): bool {
2018-12-30 19:58:30 +01:00
return in_array($type, MSZ_TOPIC_TYPES, true);
}
2019-06-10 19:04:53 +02:00
function forum_topic_create(
int $forumId,
int $userId,
string $title,
int $type = MSZ_TOPIC_TYPE_DISCUSSION
): int {
if(empty($title) || !forum_topic_is_valid_type($type)) {
2018-12-30 19:58:30 +01:00
return 0;
}
2019-09-29 00:38:39 +02:00
$createTopic = \Misuzu\DB::prepare('
2018-05-23 03:41:57 +02:00
INSERT INTO `msz_forum_topics`
2018-12-30 19:58:30 +01:00
(`forum_id`, `user_id`, `topic_title`, `topic_type`)
2018-05-23 03:41:57 +02:00
VALUES
2018-12-30 19:58:30 +01:00
(:forum_id, :user_id, :topic_title, :topic_type)
2018-05-23 03:41:57 +02:00
');
2019-09-29 00:38:39 +02:00
$createTopic->bind('forum_id', $forumId);
$createTopic->bind('user_id', $userId);
$createTopic->bind('topic_title', $title);
$createTopic->bind('topic_type', $type);
2018-05-23 03:41:57 +02:00
2019-09-29 00:38:39 +02:00
return $createTopic->execute() ? \Misuzu\DB::lastId() : 0;
2018-05-23 03:41:57 +02:00
}
2019-06-10 19:04:53 +02:00
function forum_topic_update(int $topicId, ?string $title, ?int $type = null): bool {
if($topicId < 1) {
2018-12-30 19:58:30 +01:00
return false;
}
// make sure it's null and not some other kinda empty
2019-06-10 19:04:53 +02:00
if(empty($title)) {
2018-12-30 19:58:30 +01:00
$title = null;
}
2019-06-10 19:04:53 +02:00
if($type !== null && !forum_topic_is_valid_type($type)) {
return false;
}
2019-09-29 00:38:39 +02:00
$updateTopic = \Misuzu\DB::prepare('
UPDATE `msz_forum_topics`
SET `topic_title` = COALESCE(:topic_title, `topic_title`),
2018-12-30 19:58:30 +01:00
`topic_type` = COALESCE(:topic_type, `topic_type`)
WHERE `topic_id` = :topic_id
');
2019-09-29 00:38:39 +02:00
$updateTopic->bind('topic_id', $topicId);
$updateTopic->bind('topic_title', $title);
$updateTopic->bind('topic_type', $type);
2018-12-30 19:58:30 +01:00
return $updateTopic->execute();
}
2019-06-10 19:04:53 +02:00
function forum_topic_get(int $topicId, bool $allowDeleted = false): array {
2019-09-29 00:38:39 +02:00
$getTopic = \Misuzu\DB::prepare(sprintf(
2019-01-03 16:57:17 +01:00
'
SELECT
t.`topic_id`, t.`forum_id`, t.`topic_title`, t.`topic_type`, t.`topic_locked`, t.`topic_created`,
2019-05-07 21:43:54 +02:00
f.`forum_archived` AS `topic_archived`, t.`topic_deleted`, t.`topic_bumped`, f.`forum_type`,
2019-05-02 23:36:57 +02:00
tp.`poll_id`, tp.`poll_max_votes`, tp.`poll_expires`, tp.`poll_preview_results`, tp.`poll_change_vote`,
2019-04-18 01:59:33 +02:00
(tp.`poll_expires` < CURRENT_TIMESTAMP) AS `poll_expired`,
2019-05-02 23:36:57 +02:00
fp.`topic_id` AS `author_post_id`, fp.`user_id` AS `author_user_id`,
2019-01-03 16:57:17 +01:00
(
2019-01-12 00:00:53 +01:00
SELECT COUNT(`post_id`)
2019-01-03 16:57:17 +01:00
FROM `msz_forum_posts`
WHERE `topic_id` = t.`topic_id`
2019-01-12 00:00:53 +01:00
AND `post_deleted` IS NULL
2019-05-02 23:36:57 +02:00
) AS `topic_count_posts`,
2019-01-03 16:57:17 +01:00
(
SELECT COUNT(`post_id`)
FROM `msz_forum_posts`
WHERE `topic_id` = t.`topic_id`
2019-01-12 00:00:53 +01:00
AND `post_deleted` IS NOT NULL
2019-05-02 23:36:57 +02:00
) AS `topic_count_posts_deleted`,
(
SELECT COUNT(*)
FROM `msz_forum_polls_answers`
WHERE `poll_id` = tp.`poll_id`
) AS `poll_votes`
FROM `msz_forum_topics` AS t
LEFT JOIN `msz_forum_categories` AS f
2019-01-03 16:57:17 +01:00
ON f.`forum_id` = t.`forum_id`
2019-05-02 23:36:57 +02:00
LEFT JOIN `msz_forum_posts` AS fp
2019-01-12 00:00:53 +01:00
ON fp.`post_id` = (
SELECT MIN(`post_id`)
FROM `msz_forum_posts`
WHERE `topic_id` = t.`topic_id`
)
2019-04-18 01:59:33 +02:00
LEFT JOIN `msz_forum_polls` AS tp
ON tp.`poll_id` = t.`poll_id`
2019-01-03 16:57:17 +01:00
WHERE t.`topic_id` = :topic_id
2019-01-12 00:00:53 +01:00
%s
2019-01-03 16:57:17 +01:00
',
2019-01-12 00:00:53 +01:00
$allowDeleted ? '' : 'AND t.`topic_deleted` IS NULL'
2019-01-03 16:57:17 +01:00
));
2019-09-29 00:38:39 +02:00
$getTopic->bind('topic_id', $topicId);
return $getTopic->fetch();
}
2019-06-10 19:04:53 +02:00
function forum_topic_bump(int $topicId): bool {
2019-09-29 00:38:39 +02:00
$bumpTopic = \Misuzu\DB::prepare('
2018-05-23 03:41:57 +02:00
UPDATE `msz_forum_topics`
SET `topic_bumped` = NOW()
WHERE `topic_id` = :topic_id
2019-01-03 16:57:17 +01:00
AND `topic_deleted` IS NULL
2018-05-23 03:41:57 +02:00
');
2019-09-29 00:38:39 +02:00
$bumpTopic->bind('topic_id', $topicId);
2018-05-23 03:41:57 +02:00
return $bumpTopic->execute();
}
2019-06-10 19:04:53 +02:00
function forum_topic_views_increment(int $topicId): void {
if($topicId < 1) {
2019-03-04 21:25:20 +01:00
return;
}
2019-09-29 00:38:39 +02:00
$bumpViews = \Misuzu\DB::prepare('
2019-03-04 21:25:20 +01:00
UPDATE `msz_forum_topics`
SET `topic_count_views` = `topic_count_views` + 1
WHERE `topic_id` = :topic_id
');
2019-09-29 00:38:39 +02:00
$bumpViews->bind('topic_id', $topicId);
2019-03-04 21:25:20 +01:00
$bumpViews->execute();
}
2019-06-10 19:04:53 +02:00
function forum_topic_mark_read(int $userId, int $topicId, int $forumId): void {
if($userId < 1) {
return;
}
2019-03-04 21:25:20 +01:00
// previously a TRIGGER was used to achieve this behaviour,
// but those explode when running on a lot of queries (like forum_mark_read() does)
// so instead we get to live with this garbage now
try {
2019-09-29 00:38:39 +02:00
$markAsRead = \Misuzu\DB::prepare('
2019-03-04 21:25:20 +01:00
INSERT INTO `msz_forum_topics_track`
(`user_id`, `topic_id`, `forum_id`, `track_last_read`)
VALUES
(:user_id, :topic_id, :forum_id, NOW())
');
2019-09-29 00:38:39 +02:00
$markAsRead->bind('user_id', $userId);
$markAsRead->bind('topic_id', $topicId);
$markAsRead->bind('forum_id', $forumId);
2019-03-04 21:25:20 +01:00
2019-06-10 19:04:53 +02:00
if($markAsRead->execute()) {
2019-03-04 21:25:20 +01:00
forum_topic_views_increment($topicId);
}
2019-06-10 19:04:53 +02:00
} catch(PDOException $ex) {
2019-09-29 00:38:39 +02:00
if($ex->getCode() != '23000') {
2019-03-04 21:25:20 +01:00
throw $ex;
}
2019-09-29 00:38:39 +02:00
$markAsRead = \Misuzu\DB::prepare('
2019-03-04 21:25:20 +01:00
UPDATE `msz_forum_topics_track`
2019-04-02 21:22:45 +02:00
SET `track_last_read` = NOW(),
`forum_id` = :forum_id
2019-03-04 21:25:20 +01:00
WHERE `user_id` = :user_id
AND `topic_id` = :topic_id
');
2019-09-29 00:38:39 +02:00
$markAsRead->bind('user_id', $userId);
$markAsRead->bind('topic_id', $topicId);
$markAsRead->bind('forum_id', $forumId);
2019-03-04 21:25:20 +01:00
$markAsRead->execute();
}
}
2019-05-07 11:26:42 +02:00
function forum_topic_listing(
int $forumId, int $userId,
int $offset = 0, int $take = 0,
bool $showDeleted = false, bool $sortByPriority = false
): array {
$hasPagination = $offset >= 0 && $take > 0;
2019-09-29 00:38:39 +02:00
$getTopics = \Misuzu\DB::prepare(sprintf(
'
SELECT
:user_id AS `target_user_id`,
t.`topic_id`, t.`topic_title`, t.`topic_locked`, t.`topic_type`, t.`topic_created`,
2019-05-07 11:26:42 +02:00
t.`topic_bumped`, t.`topic_deleted`, t.`topic_count_views`, f.`forum_type`,
COALESCE(SUM(tp.`topic_priority`), 0) AS `topic_priority`,
au.`user_id` AS `author_id`, au.`username` AS `author_name`,
COALESCE(au.`user_colour`, ar.`role_colour`) AS `author_colour`,
lp.`post_id` AS `response_id`,
lp.`post_created` AS `response_created`,
lu.`user_id` AS `respondent_id`,
lu.`username` AS `respondent_name`,
COALESCE(lu.`user_colour`, lr.`role_colour`) AS `respondent_colour`,
(
SELECT COUNT(`post_id`)
FROM `msz_forum_posts`
WHERE `topic_id` = t.`topic_id`
2019-01-03 16:57:17 +01:00
%5$s
) AS `topic_count_posts`,
(
SELECT CEIL(COUNT(`post_id`) / %6$d)
FROM `msz_forum_posts`
WHERE `topic_id` = t.`topic_id`
%5$s
) AS `topic_pages`,
(
SELECT
`target_user_id` > 0
AND
t.`topic_bumped` > NOW() - INTERVAL 1 MONTH
AND (
SELECT COUNT(ti.`topic_id`) < 1
FROM `msz_forum_topics_track` AS tt
RIGHT JOIN `msz_forum_topics` AS ti
ON ti.`topic_id` = tt.`topic_id`
WHERE ti.`topic_id` = t.`topic_id`
AND tt.`user_id` = `target_user_id`
2019-01-03 16:57:17 +01:00
AND `track_last_read` >= `topic_bumped`
)
) AS `topic_unread`,
(
SELECT COUNT(`post_id`) > 0
FROM `msz_forum_posts`
WHERE `topic_id` = t.`topic_id`
AND `user_id` = `target_user_id`
LIMIT 1
) AS `topic_participated`
FROM `msz_forum_topics` AS t
2019-05-07 11:26:42 +02:00
LEFT JOIN `msz_forum_topics_priority` AS tp
ON tp.`topic_id` = t.`topic_id`
LEFT JOIN `msz_forum_categories` AS f
ON f.`forum_id` = t.`forum_id`
LEFT JOIN `msz_users` AS au
ON t.`user_id` = au.`user_id`
LEFT JOIN `msz_roles` AS ar
ON ar.`role_id` = au.`display_role`
LEFT JOIN `msz_forum_posts` AS lp
ON lp.`post_id` = (
SELECT `post_id`
FROM `msz_forum_posts`
WHERE `topic_id` = t.`topic_id`
2019-01-03 16:57:17 +01:00
%5$s
ORDER BY `post_id` DESC
LIMIT 1
)
LEFT JOIN `msz_users` AS lu
ON lu.`user_id` = lp.`user_id`
LEFT JOIN `msz_roles` AS lr
ON lr.`role_id` = lu.`display_role`
2018-12-30 19:58:30 +01:00
WHERE (
t.`forum_id` = :forum_id
OR t.`topic_type` = %3$d
)
%1$s
2019-05-07 11:26:42 +02:00
GROUP BY t.`topic_id`
ORDER BY FIELD(t.`topic_type`, %4$s) DESC, %7$s t.`topic_bumped` DESC
%2$s
',
$showDeleted ? '' : 'AND t.`topic_deleted` IS NULL',
2018-12-30 19:58:30 +01:00
$hasPagination ? 'LIMIT :offset, :take' : '',
MSZ_TOPIC_TYPE_GLOBAL_ANNOUNCEMENT,
2019-01-03 16:57:17 +01:00
implode(',', array_reverse(MSZ_TOPIC_TYPE_ORDER)),
$showDeleted ? '' : 'AND `post_deleted` IS NULL',
2019-05-07 11:26:42 +02:00
MSZ_FORUM_POSTS_PER_PAGE,
$sortByPriority ? '`topic_priority` DESC,' : ''
));
2019-09-29 00:38:39 +02:00
$getTopics->bind('forum_id', $forumId);
$getTopics->bind('user_id', $userId);
2019-06-10 19:04:53 +02:00
if($hasPagination) {
2019-09-29 00:38:39 +02:00
$getTopics->bind('offset', $offset);
$getTopics->bind('take', $take);
}
2019-09-29 00:38:39 +02:00
return $getTopics->fetchAll();
}
2019-01-12 00:00:53 +01:00
2019-06-10 19:04:53 +02:00
function forum_topic_count_user(int $authorId, int $userId, bool $showDeleted = false): int {
2019-09-29 00:38:39 +02:00
$getTopics = \Misuzu\DB::prepare(sprintf(
2019-03-10 22:18:33 +01:00
'
SELECT COUNT(`topic_id`)
FROM `msz_forum_topics` AS t
WHERE t.`user_id` = :author_id
%1$s
',
$showDeleted ? '' : 'AND t.`topic_deleted` IS NULL'
));
2019-09-29 00:38:39 +02:00
$getTopics->bind('author_id', $authorId);
//$getTopics->bind('user_id', $userId);
2019-03-10 22:18:33 +01:00
2019-09-29 00:38:39 +02:00
return (int)$getTopics->fetchColumn();
2019-03-10 22:18:33 +01:00
}
// Remove unneccesary stuff from the sql stmt
2019-06-10 19:04:53 +02:00
function forum_topic_listing_user(
int $authorId,
int $userId,
int $offset = 0,
int $take = 0,
bool $showDeleted = false
): array {
2019-03-10 22:18:33 +01:00
$hasPagination = $offset >= 0 && $take > 0;
2019-09-29 00:38:39 +02:00
$getTopics = \Misuzu\DB::prepare(sprintf(
2019-03-10 22:18:33 +01:00
'
SELECT
:user_id AS `target_user_id`,
t.`topic_id`, t.`topic_title`, t.`topic_locked`, t.`topic_type`, t.`topic_created`,
t.`topic_bumped`, t.`topic_deleted`, t.`topic_count_views`, f.`forum_type`,
2019-03-10 22:18:33 +01:00
au.`user_id` AS `author_id`, au.`username` AS `author_name`,
COALESCE(au.`user_colour`, ar.`role_colour`) AS `author_colour`,
lp.`post_id` AS `response_id`,
lp.`post_created` AS `response_created`,
lu.`user_id` AS `respondent_id`,
lu.`username` AS `respondent_name`,
COALESCE(lu.`user_colour`, lr.`role_colour`) AS `respondent_colour`,
(
SELECT COUNT(`post_id`)
FROM `msz_forum_posts`
WHERE `topic_id` = t.`topic_id`
%5$s
) AS `topic_count_posts`,
(
SELECT CEIL(COUNT(`post_id`) / %6$d)
FROM `msz_forum_posts`
WHERE `topic_id` = t.`topic_id`
%5$s
) AS `topic_pages`,
(
SELECT
`target_user_id` > 0
AND
t.`topic_bumped` > NOW() - INTERVAL 1 MONTH
AND (
SELECT COUNT(ti.`topic_id`) < 1
FROM `msz_forum_topics_track` AS tt
RIGHT JOIN `msz_forum_topics` AS ti
ON ti.`topic_id` = tt.`topic_id`
WHERE ti.`topic_id` = t.`topic_id`
AND tt.`user_id` = `target_user_id`
AND `track_last_read` >= `topic_bumped`
)
) AS `topic_unread`,
(
SELECT COUNT(`post_id`) > 0
FROM `msz_forum_posts`
WHERE `topic_id` = t.`topic_id`
AND `user_id` = `target_user_id`
LIMIT 1
) AS `topic_participated`
FROM `msz_forum_topics` AS t
LEFT JOIN `msz_forum_categories` AS f
ON f.`forum_id` = t.`forum_id`
2019-03-10 22:18:33 +01:00
LEFT JOIN `msz_users` AS au
ON t.`user_id` = au.`user_id`
LEFT JOIN `msz_roles` AS ar
ON ar.`role_id` = au.`display_role`
LEFT JOIN `msz_forum_posts` AS lp
ON lp.`post_id` = (
SELECT `post_id`
FROM `msz_forum_posts`
WHERE `topic_id` = t.`topic_id`
%5$s
ORDER BY `post_id` DESC
LIMIT 1
)
LEFT JOIN `msz_users` AS lu
ON lu.`user_id` = lp.`user_id`
LEFT JOIN `msz_roles` AS lr
ON lr.`role_id` = lu.`display_role`
WHERE au.`user_id` = :author_id
%1$s
ORDER BY FIELD(t.`topic_type`, %4$s) DESC, t.`topic_bumped` DESC
%2$s
',
$showDeleted ? '' : 'AND t.`topic_deleted` IS NULL',
$hasPagination ? 'LIMIT :offset, :take' : '',
MSZ_TOPIC_TYPE_GLOBAL_ANNOUNCEMENT,
implode(',', array_reverse(MSZ_TOPIC_TYPE_ORDER)),
$showDeleted ? '' : 'AND `post_deleted` IS NULL',
MSZ_FORUM_POSTS_PER_PAGE
));
2019-09-29 00:38:39 +02:00
$getTopics->bind('author_id', $authorId);
$getTopics->bind('user_id', $userId);
2019-03-10 22:18:33 +01:00
2019-06-10 19:04:53 +02:00
if($hasPagination) {
2019-09-29 00:38:39 +02:00
$getTopics->bind('offset', $offset);
$getTopics->bind('take', $take);
2019-03-10 22:18:33 +01:00
}
2019-09-29 00:38:39 +02:00
return $getTopics->fetchAll();
2019-03-10 22:18:33 +01:00
}
2019-06-10 19:04:53 +02:00
function forum_topic_listing_search(string $query, int $userId): array {
2019-09-29 00:38:39 +02:00
$getTopics = \Misuzu\DB::prepare(sprintf(
2019-05-01 22:31:59 +02:00
'
SELECT
:user_id AS `target_user_id`,
t.`topic_id`, t.`topic_title`, t.`topic_locked`, t.`topic_type`, t.`topic_created`,
t.`topic_bumped`, t.`topic_deleted`, t.`topic_count_views`, f.`forum_type`,
2019-05-01 22:31:59 +02:00
au.`user_id` AS `author_id`, au.`username` AS `author_name`,
COALESCE(au.`user_colour`, ar.`role_colour`) AS `author_colour`,
lp.`post_id` AS `response_id`,
lp.`post_created` AS `response_created`,
lu.`user_id` AS `respondent_id`,
lu.`username` AS `respondent_name`,
COALESCE(lu.`user_colour`, lr.`role_colour`) AS `respondent_colour`,
(
SELECT COUNT(`post_id`)
FROM `msz_forum_posts`
WHERE `topic_id` = t.`topic_id`
AND `post_deleted` IS NULL
) AS `topic_count_posts`,
(
SELECT CEIL(COUNT(`post_id`) / %2$d)
FROM `msz_forum_posts`
WHERE `topic_id` = t.`topic_id`
AND `post_deleted` IS NULL
) AS `topic_pages`,
(
SELECT
`target_user_id` > 0
AND
t.`topic_bumped` > NOW() - INTERVAL 1 MONTH
AND (
SELECT COUNT(ti.`topic_id`) < 1
FROM `msz_forum_topics_track` AS tt
RIGHT JOIN `msz_forum_topics` AS ti
ON ti.`topic_id` = tt.`topic_id`
WHERE ti.`topic_id` = t.`topic_id`
AND tt.`user_id` = `target_user_id`
AND `track_last_read` >= `topic_bumped`
)
) AS `topic_unread`,
(
SELECT COUNT(`post_id`) > 0
FROM `msz_forum_posts`
WHERE `topic_id` = t.`topic_id`
AND `user_id` = `target_user_id`
LIMIT 1
) AS `topic_participated`
FROM `msz_forum_topics` AS t
LEFT JOIN `msz_forum_categories` AS f
ON f.`forum_id` = t.`forum_id`
2019-05-01 22:31:59 +02:00
LEFT JOIN `msz_users` AS au
ON t.`user_id` = au.`user_id`
LEFT JOIN `msz_roles` AS ar
ON ar.`role_id` = au.`display_role`
LEFT JOIN `msz_forum_posts` AS lp
ON lp.`post_id` = (
SELECT `post_id`
FROM `msz_forum_posts`
WHERE `topic_id` = t.`topic_id`
AND `post_deleted` IS NULL
ORDER BY `post_id` DESC
LIMIT 1
)
LEFT JOIN `msz_users` AS lu
ON lu.`user_id` = lp.`user_id`
LEFT JOIN `msz_roles` AS lr
ON lr.`role_id` = lu.`display_role`
WHERE MATCH(`topic_title`)
AGAINST (:query IN NATURAL LANGUAGE MODE)
AND t.`topic_deleted` IS NULL
ORDER BY FIELD(t.`topic_type`, %1$s) DESC, t.`topic_bumped` DESC
',
implode(',', array_reverse(MSZ_TOPIC_TYPE_ORDER)),
MSZ_FORUM_POSTS_PER_PAGE
));
2019-09-29 00:38:39 +02:00
$getTopics->bind('query', $query);
$getTopics->bind('user_id', $userId);
2019-05-01 22:31:59 +02:00
2019-09-29 00:38:39 +02:00
return $getTopics->fetchAll();
2019-05-01 22:31:59 +02:00
}
2019-06-10 19:04:53 +02:00
function forum_topic_lock(int $topicId): bool {
if($topicId < 1) {
2019-01-12 00:00:53 +01:00
return false;
}
2019-09-29 00:38:39 +02:00
$markLocked = \Misuzu\DB::prepare('
2019-01-12 00:00:53 +01:00
UPDATE `msz_forum_topics`
SET `topic_locked` = NOW()
WHERE `topic_id` = :topic
AND `topic_locked` IS NULL
');
2019-09-29 00:38:39 +02:00
$markLocked->bind('topic', $topicId);
2019-01-12 00:00:53 +01:00
return $markLocked->execute();
}
2019-06-10 19:04:53 +02:00
function forum_topic_unlock(int $topicId): bool {
if($topicId < 1) {
2019-01-12 00:00:53 +01:00
return false;
}
2019-09-29 00:38:39 +02:00
$markUnlocked = \Misuzu\DB::prepare('
2019-01-12 00:00:53 +01:00
UPDATE `msz_forum_topics`
SET `topic_locked` = NULL
WHERE `topic_id` = :topic
AND `topic_locked` IS NOT NULL
');
2019-09-29 00:38:39 +02:00
$markUnlocked->bind('topic', $topicId);
2019-01-12 00:00:53 +01:00
return $markUnlocked->execute();
}
define('MSZ_E_FORUM_TOPIC_DELETE_OK', 0); // deleting is fine
define('MSZ_E_FORUM_TOPIC_DELETE_USER', 1); // invalid user
define('MSZ_E_FORUM_TOPIC_DELETE_TOPIC', 2); // topic doesn't exist
define('MSZ_E_FORUM_TOPIC_DELETE_DELETED', 3); // topic is already marked as deleted
define('MSZ_E_FORUM_TOPIC_DELETE_OWNER', 4); // you may only delete your own topics
define('MSZ_E_FORUM_TOPIC_DELETE_OLD', 5); // topic has existed for too long to be deleted
define('MSZ_E_FORUM_TOPIC_DELETE_PERM', 6); // you aren't allowed to delete topics
define('MSZ_E_FORUM_TOPIC_DELETE_POSTS', 7); // the topic already has replies
// only allow topics made within a day of posting to be deleted by normal users
define('MSZ_FORUM_TOPIC_DELETE_TIME_LIMIT', 60 * 60 * 24);
// only allow topics with a single post to be deleted, includes soft deleted posts
define('MSZ_FORUM_TOPIC_DELETE_POST_LIMIT', 1);
// set $userId to null for system request, make sure this is NEVER EVER null on user request
// $topicId can also be a the return value of forum_topic_get if you already grabbed it once before
2019-06-10 19:04:53 +02:00
function forum_topic_can_delete($topicId, ?int $userId = null): int {
if($userId !== null && $userId < 1) {
2019-01-12 00:00:53 +01:00
return MSZ_E_FORUM_TOPIC_DELETE_USER;
}
2019-06-10 19:04:53 +02:00
if(is_array($topicId)) {
2019-01-12 00:00:53 +01:00
$topic = $topicId;
} else {
$topic = forum_topic_get((int)$topicId, true);
}
2019-06-10 19:04:53 +02:00
if(empty($topic)) {
2019-01-12 00:00:53 +01:00
return MSZ_E_FORUM_TOPIC_DELETE_TOPIC;
}
$isSystemReq = $userId === null;
$perms = $isSystemReq ? 0 : forum_perms_get_user($topic['forum_id'], $userId)[MSZ_FORUM_PERMS_GENERAL];
2019-01-12 00:00:53 +01:00
$canDeleteAny = $isSystemReq ? true : perms_check($perms, MSZ_FORUM_PERM_DELETE_ANY_POST);
$canViewPost = $isSystemReq ? true : perms_check($perms, MSZ_FORUM_PERM_VIEW_FORUM);
$postIsDeleted = !empty($topic['topic_deleted']);
2019-06-10 19:04:53 +02:00
if(!$canViewPost) {
2019-01-12 00:00:53 +01:00
return MSZ_E_FORUM_TOPIC_DELETE_TOPIC;
}
2019-06-10 19:04:53 +02:00
if($postIsDeleted) {
2019-01-12 00:00:53 +01:00
return $canDeleteAny ? MSZ_E_FORUM_TOPIC_DELETE_DELETED : MSZ_E_FORUM_TOPIC_DELETE_TOPIC;
}
2019-06-10 19:04:53 +02:00
if($isSystemReq) {
2019-01-12 00:00:53 +01:00
return MSZ_E_FORUM_TOPIC_DELETE_OK;
}
2019-06-10 19:04:53 +02:00
if(!$canDeleteAny) {
if(!perms_check($perms, MSZ_FORUM_PERM_DELETE_POST)) {
2019-01-12 00:00:53 +01:00
return MSZ_E_FORUM_TOPIC_DELETE_PERM;
}
2019-06-10 19:04:53 +02:00
if($topic['author_user_id'] !== $userId) {
2019-01-12 00:00:53 +01:00
return MSZ_E_FORUM_TOPIC_DELETE_OWNER;
}
2019-06-10 19:04:53 +02:00
if(strtotime($topic['topic_created']) <= time() - MSZ_FORUM_TOPIC_DELETE_TIME_LIMIT) {
2019-01-12 00:00:53 +01:00
return MSZ_E_FORUM_TOPIC_DELETE_OLD;
}
$totalReplies = $topic['topic_count_posts'] + $topic['topic_count_posts_deleted'];
2019-01-12 00:00:53 +01:00
2019-06-10 19:04:53 +02:00
if($totalReplies > MSZ_E_FORUM_TOPIC_DELETE_POSTS) {
2019-01-12 00:00:53 +01:00
return MSZ_E_FORUM_TOPIC_DELETE_POSTS;
}
}
return MSZ_E_FORUM_TOPIC_DELETE_OK;
}
2019-06-10 19:04:53 +02:00
function forum_topic_delete(int $topicId): bool {
if($topicId < 1) {
2019-01-12 00:00:53 +01:00
return false;
}
2019-09-29 00:38:39 +02:00
$markTopicDeleted = \Misuzu\DB::prepare('
2019-01-12 00:00:53 +01:00
UPDATE `msz_forum_topics`
SET `topic_deleted` = NOW()
WHERE `topic_id` = :topic
AND `topic_deleted` IS NULL
');
2019-09-29 00:38:39 +02:00
$markTopicDeleted->bind('topic', $topicId);
2019-01-12 00:00:53 +01:00
2019-06-10 19:04:53 +02:00
if(!$markTopicDeleted->execute()) {
2019-01-12 00:00:53 +01:00
return false;
}
2019-09-29 00:38:39 +02:00
$markPostsDeleted = \Misuzu\DB::prepare('
2019-01-12 00:00:53 +01:00
UPDATE `msz_forum_posts` as p
SET p.`post_deleted` = (
SELECT `topic_deleted`
FROM `msz_forum_topics`
WHERE `topic_id` = p.`topic_id`
)
WHERE p.`topic_id` = :topic
AND p.`post_deleted` IS NULL
');
2019-09-29 00:38:39 +02:00
$markPostsDeleted->bind('topic', $topicId);
2019-01-12 00:00:53 +01:00
return $markPostsDeleted->execute();
}
2019-06-10 19:04:53 +02:00
function forum_topic_restore(int $topicId): bool {
if($topicId < 1) {
2019-01-12 00:00:53 +01:00
return false;
}
2019-09-29 00:38:39 +02:00
$markPostsRestored = \Misuzu\DB::prepare('
2019-01-12 00:00:53 +01:00
UPDATE `msz_forum_posts` as p
SET p.`post_deleted` = NULL
WHERE p.`topic_id` = :topic
AND p.`post_deleted` = (
SELECT `topic_deleted`
FROM `msz_forum_topics`
WHERE `topic_id` = p.`topic_id`
)
');
2019-09-29 00:38:39 +02:00
$markPostsRestored->bind('topic', $topicId);
2019-01-12 00:00:53 +01:00
2019-06-10 19:04:53 +02:00
if(!$markPostsRestored->execute()) {
2019-01-12 00:00:53 +01:00
return false;
}
2019-09-29 00:38:39 +02:00
$markTopicRestored = \Misuzu\DB::prepare('
2019-01-12 00:00:53 +01:00
UPDATE `msz_forum_topics`
SET `topic_deleted` = NULL
WHERE `topic_id` = :topic
AND `topic_deleted` IS NOT NULL
');
2019-09-29 00:38:39 +02:00
$markTopicRestored->bind('topic', $topicId);
2019-01-12 00:00:53 +01:00
return $markTopicRestored->execute();
}
2019-06-10 19:04:53 +02:00
function forum_topic_nuke(int $topicId): bool {
if($topicId < 1) {
2019-01-12 00:00:53 +01:00
return false;
}
2019-09-29 00:38:39 +02:00
$nukeTopic = \Misuzu\DB::prepare('
2019-01-12 00:00:53 +01:00
DELETE FROM `msz_forum_topics`
WHERE `topic_id` = :topic
');
2019-09-29 00:38:39 +02:00
$nukeTopic->bind('topic', $topicId);
2019-01-12 00:00:53 +01:00
return $nukeTopic->execute();
}
2019-05-07 19:16:08 +02:00
2019-06-10 19:04:53 +02:00
function forum_topic_priority(int $topic): array {
2019-05-07 19:16:08 +02:00
if($topic < 1) {
return [];
}
2019-09-29 00:38:39 +02:00
$getPriority = \Misuzu\DB::prepare('
2019-05-07 21:43:54 +02:00
SELECT
tp.`topic_id`, tp.`topic_priority`,
u.`user_id`, u.`username`,
COALESCE(u.`user_colour`, r.`role_colour`) AS `user_colour`
2019-05-07 19:16:08 +02:00
FROM `msz_forum_topics_priority` AS tp
LEFT JOIN `msz_users` AS u
ON u.`user_id` = tp.`user_id`
2019-05-07 21:43:54 +02:00
LEFT JOIN `msz_roles` AS r
ON u.`display_role` = r.`role_id`
WHERE `topic_id` = :topic
2019-05-07 19:16:08 +02:00
');
2019-09-29 00:38:39 +02:00
$getPriority->bind('topic', $topic);
2019-05-07 19:16:08 +02:00
2019-09-29 00:38:39 +02:00
return $getPriority->fetchAll();
2019-05-07 19:16:08 +02:00
}
2019-05-08 17:46:03 +02:00
2019-06-10 19:04:53 +02:00
function forum_topic_priority_increase(int $topic, int $user, int $bump = 1): void {
2019-05-08 17:46:03 +02:00
if($topic < 1 || $user < 1 || $bump === 0) {
return;
}
2019-09-29 00:38:39 +02:00
$bumpPriority = \Misuzu\DB::prepare('
2019-05-08 17:46:03 +02:00
INSERT INTO `msz_forum_topics_priority`
(`topic_id`, `user_id`, `topic_priority`)
VALUES
(:topic, :user, :bump1)
ON DUPLICATE KEY UPDATE
`topic_priority` = `topic_priority` + :bump2
');
2019-09-29 00:38:39 +02:00
$bumpPriority->bind('topic', $topic);
$bumpPriority->bind('user', $user);
$bumpPriority->bind('bump1', $bump);
$bumpPriority->bind('bump2', $bump);
2019-05-08 17:46:03 +02:00
$bumpPriority->execute();
}