diff --git a/src/Conversation.php b/src/Conversation.php index 42406e098..664ef9840 100644 --- a/src/Conversation.php +++ b/src/Conversation.php @@ -21,6 +21,11 @@ */ class Conversation { + const STATUS_ACTIVE = 'active'; + const STATUS_PAUSED = 'paused'; + const STATUS_STOPPED = 'stopped'; + const STATUS_CANCELLED = 'cancelled'; + /** * All information fetched from the database * @@ -64,7 +69,7 @@ class Conversation protected $command; /** - * Conversation contructor to initialize a new conversation + * Conversation constructor to initialize a new conversation * * @param int $user_id * @param int $chat_id @@ -124,6 +129,10 @@ protected function load() //Load the conversation notes $this->protected_notes = json_decode($this->conversation['notes'], true); $this->notes = $this->protected_notes; + + if ($this->isPaused()) { + $this->resume(); + } } return $this->exists(); @@ -136,7 +145,7 @@ protected function load() */ public function exists() { - return ($this->conversation !== null); + return $this->conversation !== null; } /** @@ -172,7 +181,7 @@ protected function start() */ public function stop() { - return ($this->updateStatus('stopped') && $this->clear()); + return $this->updateStatus(self::STATUS_STOPPED) && $this->clear(); } /** @@ -183,28 +192,55 @@ public function stop() */ public function cancel() { - return ($this->updateStatus('cancelled') && $this->clear()); + return $this->updateStatus(self::STATUS_CANCELLED) && $this->clear(); + } + + /** + * Cancel the current conversation + * + * @return bool + * @throws TelegramException + */ + public function pause() + { + return $this->updateStatus(self::STATUS_PAUSED); + } + + /** + * Resume the current conversation + * + * @return bool + * @throws TelegramException + */ + public function resume() + { + return $this->updateStatus(self::STATUS_ACTIVE, true); } /** * Update the status of the current conversation * * @param string $status + * @param bool $was_paused * * @return bool * @throws TelegramException */ - protected function updateStatus($status) + protected function updateStatus($status, $was_paused = false) { if ($this->exists()) { $fields = ['status' => $status]; - $where = [ + + $where = [ 'id' => $this->conversation['id'], - 'status' => 'active', 'user_id' => $this->user_id, 'chat_id' => $this->chat_id, ]; + + $where['status'] = $was_paused ? self::STATUS_PAUSED : self::STATUS_ACTIVE; + if (ConversationDB::updateConversation($fields, $where)) { + $this->conversation['status'] = $status; return true; } } @@ -212,6 +248,36 @@ protected function updateStatus($status) return false; } + /** + * Get current conversation status + * + * @return string + */ + protected function getStatus() + { + return $this->conversation['status']; + } + + /** + * Check if the conversation is active + * + * @return bool + */ + public function isActive() + { + return $this->getStatus() === self::STATUS_ACTIVE; + } + + /** + * Check if the conversation is paused + * + * @return bool + */ + public function isPaused() + { + return $this->getStatus() === self::STATUS_PAUSED; + } + /** * Store the array/variable in the database with json_encode() function * diff --git a/src/ConversationDB.php b/src/ConversationDB.php index 91241b086..009f9bfb6 100644 --- a/src/ConversationDB.php +++ b/src/ConversationDB.php @@ -33,6 +33,7 @@ public static function initializeConversation() * @param string $user_id * @param string $chat_id * @param int|null $limit + * @param bool $paused * * @return array|bool * @throws TelegramException @@ -44,13 +45,14 @@ public static function selectConversation($user_id, $chat_id, $limit = null) } try { - $sql = ' + $sql = " SELECT * - FROM `' . TB_CONVERSATION . '` - WHERE `status` = :status + FROM `" . TB_CONVERSATION . "` + WHERE (`status` = '" . Conversation::STATUS_ACTIVE . "' + OR `status` = '" . Conversation::STATUS_PAUSED . "') AND `chat_id` = :chat_id AND `user_id` = :user_id - '; + "; if ($limit !== null) { $sql .= ' LIMIT :limit'; @@ -58,7 +60,6 @@ public static function selectConversation($user_id, $chat_id, $limit = null) $sth = self::$pdo->prepare($sql); - $sth->bindValue(':status', 'active'); $sth->bindValue(':user_id', $user_id); $sth->bindValue(':chat_id', $chat_id); @@ -99,7 +100,7 @@ public static function insertConversation($user_id, $chat_id, $command) $date = self::getTimestamp(); - $sth->bindValue(':status', 'active'); + $sth->bindValue(':status', Conversation::STATUS_ACTIVE); $sth->bindValue(':command', $command); $sth->bindValue(':user_id', $user_id); $sth->bindValue(':chat_id', $chat_id); diff --git a/structure.sql b/structure.sql index de74dd48c..53532adbf 100644 --- a/structure.sql +++ b/structure.sql @@ -280,7 +280,7 @@ CREATE TABLE IF NOT EXISTS `conversation` ( `id` bigint(20) unsigned AUTO_INCREMENT COMMENT 'Unique identifier for this entry', `user_id` bigint NULL DEFAULT NULL COMMENT 'Unique user identifier', `chat_id` bigint NULL DEFAULT NULL COMMENT 'Unique user or chat identifier', - `status` ENUM('active', 'cancelled', 'stopped') NOT NULL DEFAULT 'active' COMMENT 'Conversation state', + `status` ENUM('active', 'paused', 'stopped', 'cancelled') NOT NULL DEFAULT 'active' COMMENT 'Conversation state', `command` varchar(160) DEFAULT '' COMMENT 'Default command to execute', `notes` text DEFAULT NULL COMMENT 'Data stored from command', `created_at` timestamp NULL DEFAULT NULL COMMENT 'Entry date creation', diff --git a/utils/db-schema-update/0.62.0-unreleased.sql b/utils/db-schema-update/0.62.0-unreleased.sql index efe2ff05c..5fe25b3c2 100644 --- a/utils/db-schema-update/0.62.0-unreleased.sql +++ b/utils/db-schema-update/0.62.0-unreleased.sql @@ -6,3 +6,5 @@ ALTER TABLE `poll` ADD COLUMN `close_date` timestamp NULL DEFAULT NULL COMMENT ' ALTER TABLE `poll_answer` DROP PRIMARY KEY, ADD PRIMARY KEY (`poll_id`, `user_id`); ALTER TABLE `message` DROP CONSTRAINT `message_ibfk_6`; + +ALTER TABLE `conversation` CHANGE `status` `status` ENUM('active', 'paused', 'stopped', 'cancelled') NOT NULL DEFAULT 'active' COMMENT 'Conversation state';