2022-09-13 13:14:49 +00:00
< ? php
namespace Misuzu\Http\Handlers ;
2023-07-15 23:58:17 +00:00
use RuntimeException ;
2023-08-02 22:12:47 +00:00
use Index\DateTime ;
2022-09-13 13:14:49 +00:00
use Misuzu\DB ;
use Misuzu\Pagination ;
use Misuzu\Template ;
2023-07-15 17:02:46 +00:00
use Misuzu\Comments\CommentsCategory ;
2022-09-13 13:14:49 +00:00
final class HomeHandler extends Handler {
2023-07-28 23:17:33 +00:00
private const STATS = [
'users:active' ,
'users:online:recent' ,
'users:online:today' ,
'comments:posts:visible' ,
'forum:topics:visible' ,
'forum:posts:visible' ,
];
2022-09-13 13:14:49 +00:00
public function index ( $response , $request ) : void {
2023-08-02 22:12:47 +00:00
if ( $this -> context -> isLoggedIn ())
2022-09-13 13:14:49 +00:00
$this -> home ( $response , $request );
else
$this -> landing ( $response , $request );
}
public function landing ( $response , $request ) : void {
2023-08-02 22:12:47 +00:00
$users = $this -> context -> getUsers ();
2023-07-18 21:48:44 +00:00
$config = $this -> context -> getConfig ();
2023-07-28 23:17:33 +00:00
$counters = $this -> context -> getCounters ();
2023-07-18 21:48:44 +00:00
if ( $config -> getBoolean ( 'social.embed_linked' )) {
$ldr = $config -> getValues ([
[ 'site.name:s' , 'Misuzu' ],
'site.url:s' ,
'site.ext_logo:s' ,
'social.linked:a'
]);
$linkedData = [
'name' => $ldr [ 'site.name' ],
'url' => $ldr [ 'site.url' ],
'logo' => $ldr [ 'site.ext_logo' ],
'same_as' => $ldr [ 'social.linked' ],
];
} else $linkedData = null ;
2022-09-13 13:14:49 +00:00
2023-07-15 17:02:46 +00:00
$featuredNews = $this -> context -> getNews () -> getAllPosts (
onlyFeatured : true ,
pagination : new Pagination ( 3 )
);
2022-09-13 13:14:49 +00:00
2023-07-28 23:17:33 +00:00
$stats = $counters -> get ( self :: STATS );
2023-08-02 22:12:47 +00:00
$onlineUserInfos = $users -> getUsers (
lastActiveInMinutes : 5 ,
deleted : false ,
orderBy : 'random' ,
);
// can also chat here, whoa
$stats [ 'users:online:recent' ] = count ( $onlineUserInfos );
2022-09-13 13:14:49 +00:00
// TODO: don't hardcode forum ids
2023-07-18 21:48:44 +00:00
$featuredForums = $config -> getArray ( 'landing.forum_categories' );
2022-09-13 13:14:49 +00:00
$popularTopics = [];
$activeTopics = [];
if ( ! empty ( $featuredForums )) {
$getPopularTopics = DB :: prepare (
'SELECT t.`topic_id`, c.`forum_id`, t.`topic_title`, c.`forum_icon`, t.`topic_count_views`'
. ', (SELECT COUNT(*) FROM `msz_forum_posts` AS p WHERE p.`topic_id` = t.`topic_id` AND `post_deleted` IS NULL) AS `topic_count_posts`'
. ' FROM `msz_forum_topics` AS t'
. ' LEFT JOIN `msz_forum_categories` AS c ON c.`forum_id` = t.`forum_id`'
. ' WHERE c.`forum_id` IN (' . implode ( ',' , $featuredForums ) . ') AND `topic_deleted` IS NULL AND `topic_locked` IS NULL'
. ' ORDER BY (SELECT COUNT(*) FROM `msz_forum_posts` AS p WHERE p.`topic_id` = t.`topic_id` AND `post_deleted` IS NULL AND `post_created` > NOW() - INTERVAL 3 MONTH) DESC'
) -> stmt ;
$getPopularTopics -> execute ();
for ( $i = 0 ; $i < 10 ; ++ $i ) {
$topicInfo = $getPopularTopics -> fetchObject ();
if ( empty ( $topicInfo ))
break ;
$popularTopics [] = $topicInfo ;
}
$getActiveTopics = DB :: prepare (
'SELECT t.`topic_id`, c.`forum_id`, t.`topic_title`, c.`forum_icon`, t.`topic_count_views`'
. ', (SELECT COUNT(*) FROM `msz_forum_posts` AS p WHERE p.`topic_id` = t.`topic_id` AND `post_deleted` IS NULL) AS `topic_count_posts`'
. ', (SELECT MAX(`post_id`) FROM `msz_forum_posts` AS p WHERE p.`topic_id` = t.`topic_id` AND `post_deleted` IS NULL) AS `latest_post_id`'
. ' FROM `msz_forum_topics` AS t'
. ' LEFT JOIN `msz_forum_categories` AS c ON c.`forum_id` = t.`forum_id`'
. ' WHERE c.`forum_id` IN (' . implode ( ',' , $featuredForums ) . ') AND `topic_deleted` IS NULL AND `topic_locked` IS NULL'
. ' ORDER BY `topic_bumped` DESC'
) -> stmt ;
$getActiveTopics -> execute ();
for ( $i = 0 ; $i < 10 ; ++ $i ) {
$topicInfo = $getActiveTopics -> fetchObject ();
if ( empty ( $topicInfo ))
break ;
$activeTopics [] = $topicInfo ;
}
}
$response -> setContent ( Template :: renderRaw ( 'home.landing' , [
'statistics' => $stats ,
2023-08-02 22:12:47 +00:00
'online_users' => $onlineUserInfos ,
2022-09-13 13:14:49 +00:00
'featured_news' => $featuredNews ,
'linked_data' => $linkedData ,
'forum_popular' => $popularTopics ,
'forum_active' => $activeTopics ,
]));
}
public function home ( $response , $request ) : void {
2023-07-15 17:02:46 +00:00
$news = $this -> context -> getNews ();
2023-08-02 22:12:47 +00:00
$users = $this -> context -> getUsers ();
$config = $this -> context -> getConfig ();
2023-07-15 23:58:17 +00:00
$comments = $this -> context -> getComments ();
2023-07-28 23:17:33 +00:00
$counters = $this -> context -> getCounters ();
2023-07-15 17:02:46 +00:00
$featuredNews = [];
$userInfos = [];
2023-08-02 22:12:47 +00:00
$userColours = [];
2023-07-15 17:02:46 +00:00
$categoryInfos = [];
$featuredNewsInfos = $news -> getAllPosts (
onlyFeatured : true ,
pagination : new Pagination ( 5 )
);
foreach ( $featuredNewsInfos as $postInfo ) {
$userId = $postInfo -> getUserId ();
$categoryId = $postInfo -> getCategoryId ();
if ( array_key_exists ( $userId , $userInfos )) {
$userInfo = $userInfos [ $userId ];
2023-08-02 22:12:47 +00:00
$userColour = $userColours [ $userId ];
2023-07-15 17:02:46 +00:00
} else {
try {
2023-08-02 22:12:47 +00:00
$userInfo = $users -> getUser ( $userId , 'id' );
$userColour = $userColours [ $userId ] = $users -> getUserColour ( $userInfo );
2023-07-22 15:02:41 +00:00
} catch ( RuntimeException $ex ) {
2023-07-15 17:02:46 +00:00
$userInfo = null ;
2023-08-02 22:12:47 +00:00
$userColour = $userColours [ $userId ] = null ;
2023-07-15 17:02:46 +00:00
}
$userInfos [ $userId ] = $userInfo ;
}
if ( array_key_exists ( $categoryId , $categoryInfos ))
$categoryInfo = $categoryInfos [ $categoryId ];
else
$categoryInfos [ $categoryId ] = $categoryInfo = $news -> getCategoryByPost ( $postInfo );
2023-07-15 23:58:17 +00:00
$commentsCount = $postInfo -> hasCommentsCategoryId ()
? $comments -> countPosts ( $postInfo -> getCommentsCategoryId (), includeReplies : true ) : 0 ;
2023-07-15 17:02:46 +00:00
$featuredNews [] = [
'post' => $postInfo ,
'category' => $categoryInfo ,
'user' => $userInfo ,
2023-08-02 22:12:47 +00:00
'user_colour' => $userColour ,
2023-07-15 17:02:46 +00:00
'comments_count' => $commentsCount ,
];
}
2022-09-13 13:14:49 +00:00
2023-07-28 23:17:33 +00:00
$stats = $counters -> get ( self :: STATS );
2023-07-15 02:05:49 +00:00
$changelog = $this -> context -> getChangelog () -> getAllChanges ( pagination : new Pagination ( 10 ));
2022-09-13 13:14:49 +00:00
2023-08-02 22:12:47 +00:00
$birthdays = [];
$birthdayInfos = $users -> getUsers ( deleted : false , birthdate : DateTime :: now (), orderBy : 'random' );
foreach ( $birthdayInfos as $birthdayInfo )
$birthdays [] = [
'info' => $birthdayInfo ,
'colour' => $users -> getUserColour ( $birthdayInfo ),
];
$newestMember = [];
if ( empty ( $birthdays )) {
$newestMemberId = $config -> getString ( 'users.newest' );
if ( ! empty ( $newestMemberId ))
try {
$newestMemberInfo = $users -> getUser ( $newestMemberId , 'id' );
$newestMember [ 'info' ] = $newestMemberInfo ;
$newestMember [ 'colour' ] = $users -> getUserColour ( $newestMemberInfo );
} catch ( RuntimeException $ex ) {
$newestMember = [];
$config -> removeValues ( 'users.newest' );
}
}
2022-09-13 13:14:49 +00:00
2023-08-02 22:12:47 +00:00
$onlineUserInfos = $users -> getUsers (
lastActiveInMinutes : 5 ,
deleted : false ,
orderBy : 'random' ,
);
2022-09-13 13:14:49 +00:00
2023-07-28 23:17:33 +00:00
// today we cheat
2023-08-02 22:12:47 +00:00
$stats [ 'users:online:recent' ] = count ( $onlineUserInfos );
2023-07-28 23:17:33 +00:00
2022-09-13 13:14:49 +00:00
$response -> setContent ( Template :: renderRaw ( 'home.home' , [
'statistics' => $stats ,
2023-08-02 22:12:47 +00:00
'newest_member' => $newestMember ,
'online_users' => $onlineUserInfos ,
2022-09-13 13:14:49 +00:00
'birthdays' => $birthdays ,
'featured_changelog' => $changelog ,
'featured_news' => $featuredNews ,
]));
}
}