Add id_user to received and sended

This commit is contained in:
osaajani 2020-03-05 23:07:07 +01:00
parent 42422db1c3
commit 0124bc2627
7 changed files with 55 additions and 382 deletions

View File

@ -32,6 +32,7 @@ namespace controllers\internals;
/**
* Create a received.
*
* @param $id_user : Id of user to create received for
* @param $at : Reception date
* @param $text : Text of the message
* @param string $origin : Number of the sender
@ -41,9 +42,10 @@ namespace controllers\internals;
*
* @return bool : false on error, new received id else
*/
public function create($at, string $text, string $origin, string $destination, string $status = 'unread', bool $command = false): bool
public function create(int $id_user, $at, string $text, string $origin, string $destination, string $status = 'unread', bool $command = false): bool
{
$received = [
'id_user' => $id_user,
'at' => $at,
'text' => $text,
'origin' => $origin,

View File

@ -18,6 +18,7 @@ namespace controllers\internals;
/**
* Create a sended.
*
* @param int $id_user : Id of user to create sended message for
* @param $at : Reception date
* @param $text : Text of the message
* @param string $origin : Number of the sender
@ -29,9 +30,10 @@ namespace controllers\internals;
*
* @return bool : false on error, new sended id else
*/
public function create($at, string $text, string $origin, string $destination, string $uid, string $adapter, bool $flash = false, ?string $status = 'unknown'): bool
public function create(int $id_user, $at, string $text, string $origin, string $destination, string $uid, string $adapter, bool $flash = false, ?string $status = 'unknown'): bool
{
$sended = [
'id_user' => $id_user,
'at' => $at,
'text' => $text,
'origin' => $origin,
@ -141,14 +143,15 @@ namespace controllers\internals;
/**
* Return sended for an uid and an adapter.
*
* @param int $id_user : user id
* @param string $uid : Uid of the sended
* @param string $adapter : Adapter used to send the message
*
* @return array
*/
public function get_by_uid_and_adapter(string $uid, string $adapter)
public function get_by_uid_and_adapter_for_user(int $id_user, string $uid, string $adapter)
{
return $this->get_model()->get_by_uid_and_adapter($uid, $adapter);
return $this->get_model()->get_by_uid_and_adapter_for_user($id_user, $uid, $adapter);
}
/**

View File

@ -103,7 +103,7 @@ use Monolog\Logger;
return false;
}
$sended = $this->internal_sended->get_by_uid_and_adapter($callback_return['uid'], $adapter_classname);
$sended = $this->internal_sended->get_by_uid_and_adapter_for_user($this->user['id'], $callback_return['uid'], $adapter_classname);
if (!$sended)
{
$this->logger->error('Callback status try update inexisting message with uid = ' . $callback_return['uid'] . '.');
@ -112,7 +112,7 @@ use Monolog\Logger;
}
$this->logger->info('Callback status update message with uid ' . $callback_return['uid'] . '.');
$this->internal_sended->update_status($sended['id'], $callback_return['status']);
$this->internal_sended->update_status_for_user($this->user['id'], $sended['id'], $callback_return['status']);
return true;
}

View File

@ -149,7 +149,7 @@ class Phone extends AbstractDaemon
if (!$sended_sms_uid)
{
$this->logger->error('Failed send message : ' . json_encode($message));
$internal_sended->create($at, $message['text'], $message['origin'], $message['destination'], $sended_sms_uid, $this->phone['adapter'], $message['flash'], 'failed');
$internal_sended->create($this->phone['id_user'], $at, $message['text'], $message['origin'], $message['destination'], $sended_sms_uid, $this->phone['adapter'], $message['flash'], 'failed');
continue;
}
@ -161,7 +161,7 @@ class Phone extends AbstractDaemon
$this->logger->info('Successfully send message : ' . json_encode($message));
$internal_sended->create($at, $message['text'], $message['origin'], $message['destination'], $sended_sms_uid, $this->phone['adapter'], $message['flash']);
$internal_sended->create($this->phone['id_user'], $at, $message['text'], $message['origin'], $message['destination'], $sended_sms_uid, $this->phone['adapter'], $message['flash']);
}
}
@ -196,7 +196,7 @@ class Phone extends AbstractDaemon
$this->process_for_transfer($sms, $user_settings);
$internal_received->create($sms['at'], $sms['text'], $sms['origin'], $sms['destination'], 'unread', $is_command);
$internal_received->create($this->phone['id_user'], $sms['at'], $sms['text'], $sms['origin'], $sms['destination'], 'unread', $is_command);
}
}

View File

@ -0,0 +1,22 @@
<?php
use Phinx\Migration\AbstractMigration;
class AddIdUserSendedAndReceived extends AbstractMigration
{
/**
* Add id_user field in sended and received table
*/
public function change()
{
$this->table('received')
->addColumn('id_user', 'integer', ['null' => false])
->addForeignKey('id_user', 'user', 'id', ['delete'=> 'CASCADE', 'update'=> 'CASCADE'])
->save();
$this->table('sended')
->addColumn('id_user', 'integer', ['null' => false])
->addForeignKey('id_user', 'user', 'id', ['delete'=> 'CASCADE', 'update'=> 'CASCADE'])
->save();
}
}

View File

@ -16,77 +16,6 @@ namespace models;
*/
class Received extends StandardModel
{
/**
* Return an entry by his id for a user.
*
* @param int $id_user : user id
* @param int $id : entry id
*
* @return array
*/
public function get_for_user(int $id_user, int $id)
{
$query = '
SELECT * FROM `' . $this->get_table_name() . '`
WHERE destination IN (SELECT number FROM phone WHERE id_user = :id_user)
AND id = :id
';
$params = [
'id_user' => $id_user,
'id' => $id,
];
$receiveds = $this->_run_query($query, $params);
return $receiveds[0] ?? [];
}
/**
* Return all entries for a user.
*
* @param int $id_user : user id
*
* @return array
*/
public function gets_for_user(int $id_user)
{
$query = '
SELECT * FROM `' . $this->get_table_name() . '`
WHERE destination IN (SELECT number FROM phone WHERE id_user = :id_user)
';
$params = [
'id_user' => $id_user,
];
$receiveds = $this->_run_query($query, $params);
}
/**
* Return a list of received for a user.
*
* @param int $id_user : User id
* @param int $limit : Max results to return
* @param int $offset : Number of results to ignore
*/
public function list_for_user($id_user, $limit, $offset)
{
$limit = (int) $limit;
$offset = (int) $offset;
$query = '
SELECT * FROM received
WHERE destination IN (SELECT number FROM phone WHERE id_user = :id_user)
LIMIT ' . $limit . ' OFFSET ' . $offset;
$params = [
'id_user' => $id_user,
];
return $this->_run_query($query, $params);
}
/**
* Return a list of unread received for a user.
*
@ -101,8 +30,8 @@ namespace models;
$query = '
SELECT * FROM received
WHERE destination IN (SELECT number FROM phone WHERE id_user = :id_user)
AND status = \'unread\'
WHERE status = \'unread\'
AND id_user = :id_user
LIMIT ' . $limit . ' OFFSET ' . $offset;
$params = [
@ -112,112 +41,6 @@ namespace models;
return $this->_run_query($query, $params);
}
/**
* Return a list of receiveds in a group of ids and for a user.
*
* @param int $id_user : user id
* @param array $ids : ids of receiveds to find
*
* @return array
*/
public function gets_in_for_user(int $id_user, $ids)
{
$query = '
SELECT * FROM received
WHERE destination IN (SELECT number FROM phone WHERE id_user = :id_user)
AND id ';
//On génère la clause IN et les paramètres adaptés depuis le tableau des id
$generated_in = $this->_generate_in_from_array($ids);
$query .= $generated_in['QUERY'];
$params = $generated_in['PARAMS'];
$params['id_user'] = $id_user;
return $this->_run_query($query, $params);
}
/**
* Delete a entry by his id for a user.
*
* @param int $id_user : User id
* @param int $id : Entry id
*
* @return int : Number of removed rows
*/
public function delete_for_user(int $id_user, int $id)
{
$query = '
DELETE FROM received
WHERE id = :id
AND destination IN (SELECT number FROM phone WHERE id_user = :id_user)
';
$params = ['id_user' => $id_user, 'id' => $id];
return $this->_run_query($query, $params, self::ROWCOUNT);
}
/**
* Update a received sms for a user.
*
* @param int $id_user : User id
* @param int $id : Entry id
* @param array $datas : datas to update
*
* @return int : number of modified rows
*/
public function update_for_user(int $id_user, int $id, array $datas)
{
$params = [];
$sets = [];
foreach ($datas as $label => $value)
{
$label = preg_replace('#[^a-zA-Z0-9_]#', '', $label);
$params['set_' . $label] = $value;
$sets[] = '`' . $label . '` = :set_' . $label . ' ';
}
$query = '
UPDATE `received`
SET ' . implode(', ', $sets) . '
WHERE id = :id
AND destination IN (SELECT number FROM phone WHERE id_user = :id_user)
';
//If try to update destination, also check it does belong to user
if ($sets['set_destination'] ?? false)
{
$query .= ' AND :set_destination IN (SELECT number FROM phone WHERE id_user = :id_user)';
}
$params['id'] = $id;
$params['id_user'] = $id_user;
return $this->_run_query($query, $params, self::ROWCOUNT);
}
/**
* Count number of received sms for user.
*
* @param int $id_user : user id
*
* @return int : Number of received SMS for user
*/
public function count_for_user(int $id_user)
{
$query = '
SELECT COUNT(id) as nb
FROM received
WHERE destination IN (SELECT number FROM phone WHERE id_user = :id_user)
';
$params = [
'id_user' => $id_user,
];
return $this->_run_query($query, $params)[0]['nb'] ?? 0;
}
/**
* Count number of unread received sms for user.
@ -231,7 +54,7 @@ namespace models;
$query = '
SELECT COUNT(id) as nb
FROM received
WHERE destination IN (SELECT number FROM phone WHERE id_user = :id_user)
WHERE id_user = :id_user
AND status = \'unread\'
';
@ -257,7 +80,7 @@ namespace models;
$query = '
SELECT *
FROM received
WHERE destination IN (SELECT number FROM phone WHERE id_user = :id_user)
WHERE id_user = :id_user
ORDER BY at ASC
LIMIT ' . $nb_entry;
@ -281,7 +104,7 @@ namespace models;
$query = '
SELECT *
FROM received
WHERE destination IN (SELECT number FROM phone WHERE id_user = :id_user)
WHERE id_user = :id_user
AND origin = :origin
';
@ -307,7 +130,7 @@ namespace models;
SELECT COUNT(id) as nb, DATE_FORMAT(at, '%Y-%m-%d') as at_ymd
FROM received
WHERE at > :date
AND destination IN (SELECT number FROM phone WHERE id_user = :id_user)
AND id_user = :id_user
GROUP BY at_ymd
";
@ -332,10 +155,10 @@ namespace models;
SELECT at, number
FROM (
SELECT at, destination as number FROM sended
WHERE origin IN (SELECT number FROM phone WHERE id_user = :id_user)
WHERE id_user = :id_user
UNION (
SELECT at, origin as number FROM received
WHERE destination IN (SELECT number FROM phone WHERE id_user = :id_user)
WHERE id_user = :id_user
)
) as discussions
GROUP BY number
@ -361,7 +184,7 @@ namespace models;
SELECT *
FROM received
WHERE at > STR_TO_DATE(:date, '%Y-%m-%d %h:%i:%s')
AND destination IN (SELECT number FROM phone WHERE id_user = :id_user)
AND id_user = :id_user
ORDER BY at ASC";
$params = [
@ -388,7 +211,7 @@ namespace models;
FROM received
WHERE at > STR_TO_DATE(:date, '%Y-%m-%d %h:%i:%s')
AND origin = :origin
AND destination IN (SELECT number FROM phone WHERE id_user = :id_user)
AND id_user = :id_user
ORDER BY at ASC
";
@ -415,7 +238,7 @@ namespace models;
SELECT *
FROM received
WHERE origin = :origin
AND destination IN (SELECT number FROM phone WHERE id_user = :id_user)
AND id_user = :id_user
ORDER BY at DESC
LIMIT 0,1
';

View File

@ -16,184 +16,6 @@ namespace models;
*/
class Sended extends StandardModel
{
/**
* Return an entry by his id for a user.
*
* @param int $id_user : user id
* @param int $id : entry id
*
* @return array
*/
public function get_for_user(int $id_user, int $id)
{
$query = '
SELECT * FROM `' . $this->get_table_name() . '`
WHERE origin IN (SELECT number FROM phone WHERE id_user = :id_user)
AND id = :id
';
$params = [
'id' => $id,
'id_user' => $id_user,
];
$receiveds = $this->_run_query($query, $params);
return $receiveds[0] ?? [];
}
/**
* Return all entries for a user.
*
* @param int $id_user : user id
*
* @return array
*/
public function gets_for_user(int $id_user)
{
$query = '
SELECT * FROM `' . $this->get_table_name() . '`
WHERE origin IN (SELECT number FROM phone WHERE id_user = :id_user)
';
$params = [
'id_user' => $id_user,
];
$receiveds = $this->_run_query($query, $params);
}
/**
* Return a list of sended for a user.
*
* @param int $id_user : User id
* @param int $limit : Max results to return
* @param int $offset : Number of results to ignore
*/
public function list_for_user($id_user, $limit, $offset)
{
$limit = (int) $limit;
$offset = (int) $offset;
$query = '
SELECT * FROM sended
WHERE origin IN (SELECT number FROM phone WHERE id_user = :id_user)
LIMIT ' . $limit . ' OFFSET ' . $offset;
$params = [
'id_user' => $id_user,
];
return $this->_run_query($query, $params);
}
/**
* Return a list of sendeds in a group of ids and for a user.
*
* @param int $id_user : user id
* @param array $ids : ids of sendeds to find
*
* @return array
*/
public function gets_in_for_user(int $id_user, $ids)
{
$query = '
SELECT * FROM sended
WHERE origin IN (SELECT number FROM phone WHERE id_user = :id_user)
AND id ';
//On génère la clause IN et les paramètres adaptés depuis le tableau des id
$generated_in = $this->_generate_in_from_array($ids);
$query .= $generated_in['QUERY'];
$params = $generated_in['PARAMS'];
$params['id_user'] = $id_user;
return $this->_run_query($query, $params);
}
/**
* Delete a entry by his id for a user.
*
* @param int $id_user : User id
* @param int $id : Entry id
*
* @return int : Number of removed rows
*/
public function delete_for_user(int $id_user, int $id)
{
$query = '
DELETE FROM sended
WHERE id = :id
AND origin IN (SELECT number FROM phone WHERE id_user = :id_user)
';
$params = ['id_user' => $id_user, 'id' => $id];
return $this->_run_query($query, $params, self::ROWCOUNT);
}
/**
* Update a sended sms for a user.
*
* @param int $id_user : User id
* @param int $id : Entry id
* @param array $datas : datas to update
*
* @return int : number of modified rows
*/
public function update_for_user(int $id_user, int $id, array $datas)
{
$params = [];
$sets = [];
foreach ($datas as $label => $value)
{
$label = preg_replace('#[^a-zA-Z0-9_]#', '', $label);
$params['set_' . $label] = $value;
$sets[] = '`' . $label . '` = :set_' . $label . ' ';
}
$query = '
UPDATE `sended`
SET ' . implode(', ', $sets) . '
WHERE id = :id
AND origin IN (SELECT number FROM phone WHERE id_user = :id_user)
';
//If try to update origin, also check it does belong to user
if ($sets['set_origin'] ?? false)
{
$query .= ' AND :set_origin IN (SELECT number FROM phone WHERE id_user = :id_user)';
}
$params['id'] = $id;
$params['id_user'] = $id_user;
return $this->_run_query($query, $params, self::ROWCOUNT);
}
/**
* Count number of sended sms for user.
*
* @param int $id_user : user id
*
* @return int : Number of sended SMS for user
*/
public function count_for_user($id_user)
{
$query = '
SELECT COUNT(id) as nb
FROM sended
WHERE origin IN (SELECT number FROM phone WHERE id_user = :id_user)
';
$params = [
'id_user' => $id_user,
];
return $this->_run_query($query, $params)[0]['nb'] ?? 0;
}
/**
* Return x last sendeds message for a user, order by date.
*
@ -209,7 +31,7 @@ namespace models;
$query = '
SELECT *
FROM sended
WHERE origin IN (SELECT number FROM phone WHERE id_user = :id_user)
WHERE id_user = :id_user
ORDER BY at ASC
LIMIT ' . $nb_entry;
@ -233,7 +55,7 @@ namespace models;
$query = '
SELECT *
FROM sended
WHERE origin IN (SELECT number FROM phone WHERE id_user = :id_user)
WHERE id_user = :id_user
AND destination = :destination
';
@ -248,14 +70,15 @@ namespace models;
/**
* Return sended for an uid and an adapter.
*
* @param int $id_user : Id of the user
* @param string $uid : Uid of the sended
* @param string $adapter : Adapter used to send the message
*
* @return array
*/
public function get_by_uid_and_adapter(string $uid, string $adapter)
public function get_by_uid_and_adapter_for_user(int $id_user, string $uid, string $adapter)
{
return $this->_select_one('sended', ['uid' => $uid, 'adapter' => $adapter]);
return $this->_select_one('sended', ['id_user' => $id_user, 'uid' => $uid, 'adapter' => $adapter]);
}
/**
@ -272,7 +95,7 @@ namespace models;
SELECT COUNT(id) as nb, DATE_FORMAT(at, '%Y-%m-%d') as at_ymd
FROM sended
WHERE at > :date
AND origin IN (SELECT number FROM phone WHERE id_user = :id_user)
AND id_user = :id_user
GROUP BY at_ymd
";
@ -298,7 +121,7 @@ namespace models;
SELECT *
FROM sended
WHERE at > STR_TO_DATE(:date, '%Y-%m-%d %h:%i:%s')
AND origin IN (SELECT number FROM phone WHERE id_user = :id_user)
AND id_user = :id_user
ORDER BY at ASC";
$params = [
@ -323,7 +146,7 @@ namespace models;
SELECT *
FROM sended
WHERE destination = :destination
AND origin IN (SELECT number FROM phone WHERE id_user = :id_user)
AND id_user = :id_user
ORDER BY at DESC
LIMIT 0,1
';