laho
Pre-releaseYii2-SimpleChat 1.0.0 RC is released
I am very pleased to announce the release of yii2-simplechat 1.0.0 RC (Release Candidate). You may follow the instructions on github to install or upgrade to this version.
This RC release includes many bug fixes. It is a result of one month of intensive development since the prior Beta release. During this period, I have received a lot of help from my colleague. I thank everyone who has contributed to yii2-simplechat and made this release possible . I thank Big Drop Inc where I acquired enough of skills to develop this extension. You are the best!
Major Enhancements in 1.0.0 RC
In this release, I have included many useful features and changes. Below I summarize some of the most important ones.
Unread Messages Count of Conversation
yii2-simplechat now has built-in support for getting unread messages count for each conversation. You can see the code below:
public function getNewMessages()
{
return $this->hasOne(static::className(), ['sender_id' => 'contact_id'])
->where(['is_new' => 1])
->groupBy('sender_id');
}
/**
* @param string $userId
* @return ConversationQuery
* @throws \yii\base\InvalidConfigException
*/
public static function conversations($userId)
{
/**@var ConversationQuery $query * */
$query = \Yii::createObject(ConversationQuery::className(),
[
get_called_class(),
['userId' => $userId]
]
);
// load unread messages count for each conversation
return $query->with([
'newMessages' => function ($msg) use ($userId) {
/**@var $msg ConversationQuery * */
$msg->andOnCondition(['receiver_id' => $userId])->select(['sender_id', 'COUNT(*) AS count']);
}
]);
}
Delete Conversation
yii2-simplechat now has built-in support for deleting conversation. is_deleted_by_sender
is set to 1 if who deletes conversation is the sender of the message and is_deleted_by_receiver
to 1 if who deletes conversation is the receiver of the message. You can see the code below:
/**
* @param string $userId
* @param string $contactId
* @return array the number of rows updated
*/
public static function deleteConversation($userId, $contactId)
{
$count = static::updateAll(
[
'is_deleted_by_sender' => new Expression('IF([[sender_id]] =:userId, 1, is_deleted_by_sender)'),
'is_deleted_by_receiver' => new Expression('IF([[receiver_id]] =:userId, 1, is_deleted_by_receiver)')
],
['or',
[
'receiver_id' => $userId,
'sender_id' => $contactId,
'is_deleted_by_receiver' => 0
],
[
'sender_id' => $userId,
'receiver_id' => $contactId,
'is_deleted_by_sender' => 0
],
],
[
':userId' => $userId
]
);
return compact('count');
}
Mark Conversation as Read
yii2-simplechat now has built-in support for marking conversation as read. is_new
is set to 0 where it is equal to 1. You can see the code below:
/**
* @param $userId
* @param $contactId
* @return array the number of rows updated
*/
public static function markConversationAsRead($userId, $contactId)
{
$count = static::updateAll(
['is_new' => 0,],
['receiver_id' => $userId, 'sender_id' => $contactId, 'is_new' => 1]
);
return compact('count');
}
Mark Conversation as Unread
yii2-simplechat now has built-in support for marking conversation as unread. Mark the last received message in the conversation as unread by setting is_new
to 1. You can see the code below:
/**
* @param $userId
* @param $contactId
* @return array
*/
public static function markConversationAsUnread($userId, $contactId)
{
/** @var self $last_received_message */
$last_received_message = static::find()
->where(['sender_id' => $contactId, 'receiver_id' => $userId])
->orderBy(['id' => SORT_DESC])
->limit(1)
->one();
$count = 0;
if ($last_received_message) {
$last_received_message->is_new = 1;
$count = intval($last_received_message->update());
}
return compact('count');
}