amdp3-metaforums/Application/Controllers/ApiController.php

100 lines
4.2 KiB
PHP
Raw Normal View History

2019-11-19 03:36:39 +00:00
<?php
namespace Application\Controllers;
use Application\HTTP\Request;
use Application\HTTP\Response;
use Application\Services\ServiceContainer;
use Application\Models\Category;
2019-11-20 05:19:02 +00:00
use Application\Models\Post;
2019-11-19 03:36:39 +00:00
use Application\Models\Thread;
use Application\Models\UserAction;
2019-11-20 05:19:02 +00:00
use Application\Models\UserFavorite;
use Application\Models\UserReport;
2019-11-19 03:36:39 +00:00
use Application\Foundations\QueryBuilder;
class ApiController {
public function __construct() {
}
2019-11-20 05:19:02 +00:00
public function getBans() {
2019-11-19 03:36:39 +00:00
$bans = [];
if(ServiceContainer::Authentication()->isLoggedIn()) {
$where = new QueryBuilder();
$where = $where->where('user_id',ServiceContainer::Session()->get('user_id'))->where('expired_at','>',date('Y-m-d H:i:s'))->where('action_type','ban')->orderBy('expired_at','desc');
$actions = UserAction::select($where);
$bans = array_map(function($action) {
return (int)$action->category_id;
}, $actions);
}
2019-11-20 05:19:02 +00:00
return $bans;
}
public function categories(Request $request, Response $response) {
$bans = $this->getBans();
2019-11-19 03:36:39 +00:00
$where = new QueryBuilder();
$where = $where->where('group_id',$request->id);
2019-11-20 05:19:02 +00:00
$categories = Category::select($where);
2019-11-19 03:36:39 +00:00
if(count($bans) > 0) {
$where = $where->whereNotIn('id',$bans);
}
return $response->json()->data($categories);
}
public function threads(Request $request, Response $response) {
2019-11-20 05:19:02 +00:00
$bans = $this->getBans();
if(in_array($request->id,$bans) ) {
return $response->json()->data([]);
}
2019-11-19 03:36:39 +00:00
$where = new QueryBuilder();
$where = $where->where('category_id',$request->id);
$threads = Thread::select($where);
2019-11-20 05:19:02 +00:00
usort($threads, function($a, $b) {
$a_view = ($a->view_count + ($a->post_count * 10)) / $a->thread_age;
$b_view = ($b->view_count + ($b->post_count * 10)) / $b->thread_age;
if($a->is_hot) {
$a_view += 500000;
} else if($b->is_hot) {
$b_view += 500000;
}
return ($a_view < $b_view) ? 1 : ($a_view == $b_view ? 0 : -1);
});
2019-11-19 03:36:39 +00:00
return $response->json()->data($threads);
}
2019-11-20 05:19:02 +00:00
public function reports(Request $request, Response $response) {
if(!ServiceContainer::Authentication()->isLoggedIn() || !ServiceContainer::Authentication()->user()->is_moderator) return [];
$where = new QueryBuilder();
$where = $where->select('post.id AS post')->from('thread')->join('post','post.thread_id = thread.id');
if(!isset($request->id) || $request->id != 0) {
$where = $where->where('category_id',$request->id);
}
$threads = ServiceContainer::Database()->select($where->build());
$posts = array_map(function($a) { return $a['post']; },$threads);
$where = new QueryBuilder();
$where = $where->whereIn('post_id',$posts);
return $response->json()->data(UserReport::select($where));
}
public function favorite(Request $request, Response $response) {
if(!ServiceContainer::Authentication()->isLoggedIn()) {
return $response->json()->data([ 'success' => false ]);
}
$query = new QueryBuilder();
$query = $query->where('user_id',ServiceContainer::Authentication()->user()->id)->where('post_id',$request->id);
$is_fav = UserFavorite::selectOne($query);
if(isset($is_fav)) {
$query = new QueryBuilder();
$query = $query->delete()->from('userfavorite')->where('user_id',ServiceContainer::Authentication()->user()->id)->where('post_id',$request->id);
$res = ServiceContainer::Database()->update($query->build());
return $response->json()->data([ 'success' => $res ]);
} else {
$is_fav = UserFavorite::create([
'user_id' => ServiceContainer::Authentication()->user()->id,
'post_id' => $request->id,
]);
return $response->json()->data([ 'success' => isset($is_fav) ]);
}
return $response->json()->data([ 'success' => true ]);
}
public function favorite_num(Request $request, Response $response) {
$post = Post::find($request->id);
return $response->json()->data([ "favorites" => $post->favorites ]);
}
2019-11-19 03:36:39 +00:00
}