Skip to content

Commit

Permalink
New feature: Pinned
Browse files Browse the repository at this point in the history
  • Loading branch information
yurabakhtin committed Jan 25, 2024
1 parent 22ce8fe commit 1b2da36
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 51 deletions.
45 changes: 41 additions & 4 deletions controllers/MailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function actionAddUser($id)
*/
public function actionNotificationList()
{
$query = UserMessage::findByUser(null, 'message.updated_at DESC')->limit(5);
$query = UserMessage::findByUser()->limit(5);
return $this->renderAjax('notificationList', ['userMessages' => $query->all()]);
}

Expand Down Expand Up @@ -349,7 +349,7 @@ public function actionCreate($userGuid = null, ?string $title = null, ?string $m
public function actionMarkUnread($id)
{
$this->forcePostRequest();
$this->getMessage($id, true)->markUnread(Yii::$app->user->id);
$this->getMessage($id, true)->markUnread();

$nextReadMessage = $this->getNextReadMessage($id);

Expand All @@ -359,6 +359,40 @@ public function actionMarkUnread($id)
]);
}

/**
* Pin a Conversation
*
* @param int $id
*/
public function actionPin($id)
{
$this->forcePostRequest();
$message = $this->getMessage($id, true);
$message->pin();

return $this->asJson([
'success' => true,
'redirect' => Url::toMessenger($message)
]);
}

/**
* Unpin a Conversation
*
* @param int $id
*/
public function actionUnpin($id)
{
$this->forcePostRequest();
$message = $this->getMessage($id, true);
$message->unpin();

return $this->asJson([
'success' => true,
'redirect' => Url::toMessenger($message)
]);
}

/**
* Leave Message / Conversation
*
Expand All @@ -373,7 +407,7 @@ public function actionMarkUnread($id)
public function actionLeave($id)
{
$this->forcePostRequest();
$this->getMessage($id, true)->leave(Yii::$app->user->id);
$this->getMessage($id, true)->leave();

return $this->asJson([
'success' => true,
Expand Down Expand Up @@ -484,7 +518,10 @@ private function getNextReadMessage($id): ?Message
->where(['user_id' => Yii::$app->user->id])
->andWhere(['!=', 'message_id', $id])
->andWhere(['<=', 'last_viewed', 'updated_at'])
->orderBy(['updated_at' => SORT_DESC])
->orderBy([
'user_message.pinned' => SORT_DESC,
'message.updated_at' => SORT_DESC
])
->one();
}
}
10 changes: 10 additions & 0 deletions helpers/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ public static function toMarkUnreadConversation(Message $message)
return static::to(['/mail/mail/mark-unread', 'id' => $message->id]);
}

public static function toPinConversation(Message $message)
{
return static::to(['/mail/mail/pin', 'id' => $message->id]);
}

public static function toUnpinConversation(Message $message)
{
return static::to(['/mail/mail/unpin', 'id' => $message->id]);
}

public static function toLeaveConversation(Message $message)
{
return static::to(['/mail/mail/leave', 'id' => $message->id]);
Expand Down
26 changes: 26 additions & 0 deletions migrations/m240125_080730_pinned.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use humhub\components\Migration;
use humhub\modules\mail\models\UserMessage;

/**
* Class m240125_080730_pinned
*/
class m240125_080730_pinned extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->safeAddColumn(UserMessage::tableName(), 'pinned', $this->boolean()->defaultValue(false)->notNull());
}

/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->safeDropColumn(UserMessage::tableName(), 'pinned');
}
}
60 changes: 46 additions & 14 deletions models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use humhub\components\ActiveRecord;
use humhub\modules\mail\Module;
use humhub\modules\ui\icon\widgets\Icon;
use humhub\modules\user\models\User;
use Yii;
use yii\helpers\Html;
Expand Down Expand Up @@ -105,12 +106,15 @@ public function getAuthor()
}

/**
* @param null $userId
* @param int|null $userId
* @return UserMessage|null
*/
public function getUserMessage($userId = null)
{
if (!$userId) {
if (Yii::$app->user->isGuest) {
return null;
}
$userId = Yii::$app->user->id;
}

Expand Down Expand Up @@ -236,21 +240,42 @@ public function deleteEntry($entry)
/**
* Mark this message as unread
*
* @param int $userId
* @param int|null $userId
*/
public function markUnread($userId)
public function markUnread($userId = null)
{
$userMessage = UserMessage::findOne([
'message_id' => $this->id,
'user_id' => $userId
]);

$userMessage = $this->getUserMessage($userId);
if ($userMessage) {
$userMessage->last_viewed = null;
$userMessage->save();
}
}

/**
* Pin this message
*
* @param int|null $userId
* @param bool $pin
*/
public function pin($userId = null, bool $pin = true)
{
$userMessage = $this->getUserMessage($userId);
if ($userMessage) {
$userMessage->pinned = $pin;
$userMessage->save();
}
}

/**
* Unpin this message
*
* @param int|null $userId
*/
public function unpin($userId = null)
{
$this->pin($userId, false);
}

/**
* User leaves a message
*
Expand All @@ -262,12 +287,8 @@ public function markUnread($userId)
*/
public function leave($userId)
{
$userMessage = UserMessage::findOne([
'message_id' => $this->id,
'user_id' => $userId
]);

if(!$userMessage) {
$userMessage = $this->getUserMessage($userId);
if (!$userMessage) {
return;
}

Expand Down Expand Up @@ -389,4 +410,15 @@ public function isBlocked(): bool

return false;
}

public function isPinned($userId = null): bool
{
$userMessage = $this->getUserMessage($userId);
return $userMessage && $userMessage->pinned;
}

public function getPinIcon($userId = null): ?Icon
{
return $this->isPinned($userId) ? Icon::get('thumb-tack') : null;
}
}
6 changes: 5 additions & 1 deletion models/UserMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* @property integer $created_by
* @property string $updated_at
* @property integer $updated_by
* @property integer $pinned
*
* @property-read Message $message
* @property-read User $user
Expand Down Expand Up @@ -113,7 +114,10 @@ public static function findByUser($userId = null)

return static::find()->joinWith('message')
->where(['user_message.user_id' => $userId])
->orderBy('message.updated_at DESC');
->orderBy([
'user_message.pinned' => SORT_DESC,
'message.updated_at' => SORT_DESC
]);
}

public function isUnread(): bool
Expand Down
15 changes: 2 additions & 13 deletions resources/js/humhub.mail.conversation.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,7 @@ humhub.module('mail.conversation', function (module, require, $) {
});
};

var leave = function (evt) {
client.post(evt).then(function (response) {
if (response.redirect) {
client.pjax.redirect(response.redirect);
}
}).catch(function (e) {
module.log.error(e, true);
});
};

var markUnmark = function (evt) {
var linkAction = function (evt) {
client.post(evt).then(function (response) {
if (response.redirect) {
client.pjax.redirect(response.redirect);
Expand All @@ -109,8 +99,7 @@ humhub.module('mail.conversation', function (module, require, $) {

module.export({
init,
leave,
markUnmark,
linkAction,
submitEditEntry,
deleteEntry,
});
Expand Down
15 changes: 2 additions & 13 deletions resources/js/humhub.mail.messenger.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -786,17 +786,7 @@ humhub.module('mail.conversation', function (module, require, $) {
});
};

var leave = function (evt) {
client.post(evt).then(function (response) {
if (response.redirect) {
client.pjax.redirect(response.redirect);
}
}).catch(function (e) {
module.log.error(e, true);
});
};

var markUnmark = function (evt) {
var linkAction = function (evt) {
client.post(evt).then(function (response) {
if (response.redirect) {
client.pjax.redirect(response.redirect);
Expand All @@ -808,8 +798,7 @@ humhub.module('mail.conversation', function (module, require, $) {

module.export({
init,
leave,
markUnmark,
linkAction,
submitEditEntry,
deleteEntry,
});
Expand Down
Loading

0 comments on commit 1b2da36

Please sign in to comment.