Skip to content

Pause / resume functionality #1102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
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
80 changes: 73 additions & 7 deletions src/Conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -136,7 +145,7 @@ protected function load()
*/
public function exists()
{
return ($this->conversation !== null);
return $this->conversation !== null;
}

/**
Expand Down Expand Up @@ -172,7 +181,7 @@ protected function start()
*/
public function stop()
{
return ($this->updateStatus('stopped') && $this->clear());
return $this->updateStatus(self::STATUS_STOPPED) && $this->clear();
}

/**
Expand All @@ -183,35 +192,92 @@ 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;
}
}

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
*
Expand Down
13 changes: 7 additions & 6 deletions src/ConversationDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -44,21 +45,21 @@ 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';
}

$sth = self::$pdo->prepare($sql);

$sth->bindValue(':status', 'active');
$sth->bindValue(':user_id', $user_id);
$sth->bindValue(':chat_id', $chat_id);

Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 2 additions & 0 deletions utils/db-schema-update/0.62.0-unreleased.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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';