Skip to content

Commit 1ca2887

Browse files
committed
bugfix conversation are not overwritten
1 parent 4a788a3 commit 1ca2887

File tree

4 files changed

+23
-19
lines changed

4 files changed

+23
-19
lines changed

src/Conversation.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ protected function conversationExist()
101101
if ($this->is_fetched) {
102102
return true;
103103
}
104-
104+
//select an active conversation
105105
$conversation = ConversationDB::selectConversation($this->user_id, $this->chat_id, 1);
106106
$this->is_fetched = true;
107107

@@ -114,14 +114,14 @@ protected function conversationExist()
114114
return true;
115115
}
116116

117-
//a track with the same name was already opened store the data
117+
//a track with the same name was already opened, store the data inside the class
118118
if ($this->conversation['conversation_name'] == $this->group_name) {
119119
$this->data = json_decode($this->conversation['data'], true);
120120
return true;
121121
}
122122

123-
//a with a differet name has been opened unsetting the DB one and reacreatea a new one
124-
ConversationDB::updateConversation(['is_active' => 0], ['chat_id' => $this->chat_id, 'user_id' => $this->user_id]);
123+
//a conversation with a differet name has been opened unsetting the DB one and reacreatea a new one
124+
ConversationDB::updateConversation(['status' => 'cancelled'], ['chat_id' => $this->chat_id, 'user_id' => $this->user_id, 'status' => 'active']);
125125
return false;
126126
}
127127

@@ -156,7 +156,7 @@ public function update($data)
156156
if ($this->conversationExist()) {
157157
$fields['data'] = json_encode($data);
158158

159-
ConversationDB::updateConversation($fields, ['chat_id' => $this->chat_id, 'user_id' => $this->user_id]);
159+
ConversationDB::updateConversation($fields, ['chat_id' => $this->chat_id, 'user_id' => $this->user_id, 'status' => 'active']);
160160
//TODO verify query success before convert the private var
161161
$this->data = $data;
162162
}
@@ -174,7 +174,7 @@ public function update($data)
174174
public function stop()
175175
{
176176
if ($this->conversationExist()) {
177-
ConversationDB::updateConversation(['is_active' => 0], ['chat_id' => $this->chat_id, 'user_id' => $this->user_id]);
177+
ConversationDB::updateConversation(['status' => 'stopped'], ['chat_id' => $this->chat_id, 'user_id' => $this->user_id, 'status' => 'active']);
178178
}
179179
}
180180

src/ConversationDB.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static function selectConversation($user_id, $chat_id, $limit = null)
4444

4545
try {
4646
$query = 'SELECT * FROM `' . TB_CONVERSATION . '` ';
47-
$query .= 'WHERE `is_active` = 1 ';
47+
$query .= 'WHERE `status` = :status ';
4848
$query .= 'AND `chat_id` = :chat_id ';
4949
$query .= 'AND `user_id` = :user_id ';
5050

@@ -54,6 +54,8 @@ public static function selectConversation($user_id, $chat_id, $limit = null)
5454
}
5555
$sth = self::$pdo->prepare($query);
5656

57+
$active = 'active';
58+
$sth->bindParam(':status', $active, \PDO::PARAM_STR);
5759
$sth->bindParam(':user_id', $user_id, \PDO::PARAM_INT);
5860
$sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_INT);
5961
$sth->bindParam(':limit', $limit, \PDO::PARAM_INT);
@@ -86,18 +88,17 @@ public static function insertConversation($conversation_command, $conversation_g
8688
try {
8789
$sth = self::$pdo->prepare('INSERT INTO `' . TB_CONVERSATION . '`
8890
(
89-
`is_active`, `conversation_command`, `conversation_name`, `user_id`, `chat_id`, `data`, `created_at`, `updated_at`
91+
`status`, `conversation_command`, `conversation_name`, `user_id`, `chat_id`, `data`, `created_at`, `updated_at`
9092
)
9193
VALUES (
92-
:is_active, :conversation_command, :conversation_name, :user_id, :chat_id, :data, :date, :date
94+
:status, :conversation_command, :conversation_name, :user_id, :chat_id, :data, :date, :date
9395
)
9496
');
95-
96-
$active = 1;
97+
$active = 'active';
9798
$data = json_encode('');
9899
$created_at = self::getTimestamp();
99100

100-
$sth->bindParam(':is_active', $active);
101+
$sth->bindParam(':status', $active);
101102
$sth->bindParam(':conversation_command', $conversation_command);
102103
$sth->bindParam(':conversation_name', $conversation_group_name);
103104
$sth->bindParam(':user_id', $user_id);
@@ -147,14 +148,16 @@ public static function update($table, array $fields_values, array $where_fields_
147148
//Values
148149
$update = '';
149150
$tokens = [];
151+
$tokens_counter = 0;
150152
$a = 0;
151153
foreach ($fields_values as $field => $value) {
152154
if ($a) {
153155
$update .= ', ';
154156
}
155157
++$a;
156-
$update .= '`'.$field.'` = :'.$a;
157-
$tokens[':'.$a] = $value;
158+
++$tokens_counter;
159+
$update .= '`'.$field.'` = :'.$tokens_counter;
160+
$tokens[':'.$tokens_counter] = $value;
158161
}
159162

160163
//Where
@@ -167,7 +170,9 @@ public static function update($table, array $fields_values, array $where_fields_
167170
++$a;
168171
$where .= 'WHERE ';
169172
}
170-
$where .= '`'.$field .'`='.$value ;
173+
++$tokens_counter;
174+
$where .= '`'.$field .'`= :'.$tokens_counter ;
175+
$tokens[':'.$tokens_counter] = $value;
171176
}
172177

173178
$query = 'UPDATE `'.$table.'` SET '.$update.' '.$where;

src/Entities/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct(array $data)
3030

3131
$this->first_name = isset($data['first_name']) ? $data['first_name'] : null;
3232
if (empty($this->first_name)) {
33-
throw new TelegramException('first_name is empty!');
33+
// throw new TelegramException('first_name is empty!');
3434
}
3535

3636
$this->last_name = isset($data['last_name']) ? $data['last_name'] : null;

structure.sql

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,11 @@ CREATE TABLE IF NOT EXISTS `telegram_update` (
135135
REFERENCES `chosen_inline_query` (`id`)
136136
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
137137

138-
139138
CREATE TABLE IF NOT EXISTS `conversation` (
140139
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Row unique id',
141140
`user_id` bigint NULL DEFAULT NULL COMMENT 'User id',
142141
`chat_id` bigint NULL DEFAULT NULL COMMENT 'Telegram chat_id can be a the user id or the chat id ',
143-
`is_active` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1 conversation is active 0 conversation has been deactivated',
142+
`status` ENUM('active', 'cancelled', 'stopped') NOT NULL DEFAULT 'active' COMMENT 'active conversation is active, cancelled conversation has been truncated before end, stopped conversation has end',
144143
`conversation_command` varchar(160) DEFAULT '' COMMENT 'Default Command to execute',
145144
`conversation_name` varchar(160) NOT NULL DEFAULT '' COMMENT 'Name of the conversation can be the command name or a generic name for conversation between multiple commands',
146145
`data` varchar(1000) DEFAULT 'NULL' COMMENT 'Data stored from command',
@@ -150,7 +149,7 @@ CREATE TABLE IF NOT EXISTS `conversation` (
150149
PRIMARY KEY (`id`),
151150
KEY `user_id` (`user_id`),
152151
KEY `chat_id` (`chat_id`),
153-
KEY `is_active` (`is_active`),
152+
KEY `status` (`status`),
154153
KEY `conversation_name` (`conversation_name`),
155154

156155
FOREIGN KEY (`user_id`)

0 commit comments

Comments
 (0)