Topics: add view count and mark read on visit.

このコミットが含まれているのは:
テクニカル諏訪子 2018-04-26 17:39:18 +09:00
コミット e08a53e599
2個のファイルの変更133行の追加12行の削除

ファイルの表示

@ -43,29 +43,111 @@ class BoardController extends Controller {
->get();
}
public function readPost(Request $request) { // /api/rpc/board/post/read
public function checkRead($for, $top, $username, $password) {
$check = $this->objAuth->checkLegit($username, $password);
if ($check == 0) {
$yetToRead = false;
}
else {
$valid = $this->objAuth->getPermissions($username, $password);
if ($valid['for_canview']) {
$res = array();
$isRead = array();
if ($for != 0) {
$isRead = DB::table('for_threads')
->select('id')
->where('for_id', $for)
->get();
}
else {
$isRead[] = array('id' => $top);
}
foreach ($isRead as $r) {
$yetToRead = false;
$get = DB::table('for_read')
->join('for_posts', 'for_posts.top_id', 'for_read.top_id')
->where('for_read.user_id', $check)
->where('for_read.top_id', $r['id'])
->get(array(
'for_posts.id',
'for_posts.user_id',
'for_posts.top_id',
'for_posts.post_date',
'for_read.view_time',
));
if (!$get->count()) {
$yetToRead = true;
$res[] = array(
'top_id' => $r['id'],
'read' => true
);
}
else {
foreach($get as $g) {
if ($g->post_date > $g->view_time) {
$yetToRead = true;
}
else {
$yetToRead = false;
}
$res[] = array(
'top_id' => $g->top_id,
'read' => $yetToRead
);
}
}
}
}
else {
$yetToRead = false;
}
}
return $yetToRead;
}
public function readTopic(Request $request) { // /api/rpc/board/topic/read
$check = $this->objAuth->checkLegit($request->username, $request->password);
if ($check == 0) {
return 'Err!';
return 'Guests can\'t read';
}
else {
$valid = $this->objAuth->getPermissions($request->username, $request->password);
if ($valid['for_canview']) {
$getViews = DB::table('for_threads')
->select('views')
->where('id', $request->id)
$getRead = DB::table('for_read')
->select('view_time')
->where('user_id', $check)
->where('top_id', $request->top_id)
->first();
// id, user_id, for_id, last_read
// $getReadTopic = DB::table('for_readthreads')
$getPost = DB::table('for_posts')
->select('post_date')
->where('top_id', $request->top_id)
->orderBy('post_date', 'desc')
->limit(1)
->first();
// id, user_id, top_id, page_id, last_read
// $getReadPost = DB::table('for_readposts')
// ->select('*')
if ($getRead->view_time < $getPost->post_date) {
DB::table('for_read')
->where('user_id', $check)
->where('top_id', $request->top_id)
->update([
'view_time' => $getPost->post_date
]);
// return $something;
return 'Read.';
}
return '';
}
else {
return 'Permission denied.';
@ -1372,6 +1454,7 @@ class BoardController extends Controller {
$i = 0;
foreach($tops as $t) {
$read = $this->checkRead(0, $t->id, $request->username, $request->password);
$replysuu = DB::table('for_posts')->where('top_id', $t->id)->count();
$fplp = $this->getFirstAndLastPosts($t->id);
@ -1441,7 +1524,7 @@ class BoardController extends Controller {
't_sticky' => $t->sticky,
't_lock' => $t->lock,
't_poll' => $t->poll,
't_read' => $t->read,
't_read' => $read,
't_lang_id' => $t->lang_id,
'u_first_uid' => $t->started_by,
'u_last_uid' => $t->last_uid,
@ -1620,6 +1703,7 @@ class BoardController extends Controller {
else $uid = $this->getPostsOfUser($id, $from, $to, $request);
$udat = array();
$key = 0;
$ldate = 0;
foreach ($uid as $i) {
$showName = '';
@ -1681,9 +1765,45 @@ class BoardController extends Controller {
'showgroup' => $showGroupName,
'country' => $i->country,
]);
$ldate = $i->post_date;
$key++;
}
$check = $this->objAuth->checkLegit($request->username, $request->password);
if ($tp == 't' && $check != 0) {
$view = DB::table('for_threads')
->select('views')
->where('id', $id)
->first();
DB::table('for_threads')
->where('id', $id)
->update(['views' => $view->views+1]);
$checkTime = DB::table('for_read')
->select('view_time')
->where('user_id', $check)
->where('top_id', $id)
->first();
if (empty($checkTime)) {
DB::table('for_read')
->insert([
'user_id' => $check,
'top_id' => $id,
'view_time' => $ldate
]);
}
else if ($ldate > $checkTime->view_time) {
DB::table('for_read')
->where('user_id', $check)
->where('top_id', $id)
->update(['view_time' => $ldate]);
}
}
// Assign group names.
return $udat;
}

ファイルの表示

@ -45,6 +45,7 @@ Route::get('/api/rpc/board/topic/gettopiclock/{id}', 'BoardController@getTopicLo
Route::get('/api/rpc/board/topic/getforumidfromtopic/{top_id}', 'BoardController@getForumIdFromTopic');
Route::get('/api/rpc/board/topic/gettopicname/{id}', 'BoardController@getTopicName');
Route::post('/api/rpc/board/topic/read', 'BoardController@readTopic');
Route::post('/api/rpc/board/topic/addtopic', 'BoardController@addTopic');
Route::post('/api/rpc/board/topic/lock', 'BoardController@lockTopic');
Route::post('/api/rpc/board/topic/unlock', 'BoardController@unlockTopic');