Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions Controller/LikesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public function like() {
if (! $this->request->is('post')) {
return $this->throwBadRequest();
}

if ($this->Like->existsLike($this->data['Like']['content_key'])) {
$md5SessionKey = $this->__getKey();
if ($this->Like->existsLike($this->data['Like']['content_key'], $md5SessionKey)) {
return;
}

Expand All @@ -58,9 +58,27 @@ public function like() {
'conditions' => array('content_key' => $data['Like']['content_key'])
));
$data = Hash::merge($like, $data);
if (!Current::read('User.id')) {
$data['LikesUser']['session_key'] = $md5SessionKey;
}
if ($this->Like->saveLike($data)) {
return;
}
$this->NetCommons->handleValidationError($this->Like->validationErrors);
}

/**
* session_keyを返す
*
* @return string
*/
private function __getKey() {
$md5SessionKey = $this->Session->read('Likes.md5_session_key');
if ($md5SessionKey) {
return $md5SessionKey;
}
$md5SessionKey = md5(CakeSession::id());
$this->Session->write('Likes.md5_session_key', $md5SessionKey);
return $md5SessionKey;
}
}
10 changes: 7 additions & 3 deletions Model/Like.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,23 @@ public function afterSave($created, $options = array()) {
* Exists like data
*
* @param string $contentKey Content key of each plugin.
* @param string $md5SessionKey md5(SessionKey)
* @see https://github.com/researchmap/RmNetCommons3/issues/1750#issuecomment-596823362
* @return bool
*/
public function existsLike($contentKey) {
public function existsLike($contentKey, $md5SessionKey = null) {
$this->LikesUser = ClassRegistry::init('Likes.LikesUser');

$joinConditions = array(
$this->alias . '.id' . ' = ' . $this->LikesUser->alias . ' .like_id',
);
if (Current::read('User.id')) {
$joinConditions[$this->LikesUser->alias . '.user_id'] = Current::read('User.id');
} elseif (!is_null($md5SessionKey)) {
$joinConditions[$this->LikesUser->alias . '.session_key'] = (string)$md5SessionKey;
} else {
$joinConditions[$this->LikesUser->alias . '.session_key'] = (string)CakeSession::id();
// 常にインクリメント
return false;
}

$count = $this->find('count', array(
Expand Down Expand Up @@ -224,7 +229,6 @@ public function saveLike($data) {
$this->begin();

//バリデーション
$data['LikesUser']['session_key'] = CakeSession::id();
$this->set($data);
if (! $this->validates()) {
$this->rollback();
Expand Down