Skip to content

Commit

Permalink
New features: Unread & Pinned
Browse files Browse the repository at this point in the history
  • Loading branch information
yurabakhtin committed Jan 24, 2024
1 parent 1f902c7 commit 22ce8fe
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 34 deletions.
40 changes: 33 additions & 7 deletions controllers/MailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,24 @@ public function actionCreate($userGuid = null, ?string $title = null, ?string $m
]);
}

/**
* Mark a Conversation as unread
*
* @param int $id
*/
public function actionMarkUnread($id)
{
$this->forcePostRequest();
$this->getMessage($id, true)->markUnread(Yii::$app->user->id);

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

return $this->asJson([
'success' => true,
'redirect' => $nextReadMessage ? Url::toMessenger($nextReadMessage) : Url::to(['/dashboard'])
]);
}

/**
* Leave Message / Conversation
*
Expand Down Expand Up @@ -441,18 +459,15 @@ public function actionGetNewMessageCountJson()
*
* @param int $id
* @param bool $throw
* @return Message
* @return Message|null
* @throws HttpException
*/
private function getMessage($id, $throw = false)
private function getMessage($id, $throw = false): ?Message
{
$message = Message::findOne(['id' => $id]);

if ($message) {
$userMessage = $message->getUserMessage();
if ($userMessage != null) {
return $message;
}
if ($message && $message->getUserMessage() !== null) {
return $message;
}

if ($throw) {
Expand All @@ -461,4 +476,15 @@ private function getMessage($id, $throw = false)

return null;
}

private function getNextReadMessage($id): ?Message
{
return Message::find()
->leftJoin('user_message', 'user_message.message_id = message.id')
->where(['user_id' => Yii::$app->user->id])
->andWhere(['!=', 'message_id', $id])
->andWhere(['<=', 'last_viewed', 'updated_at'])
->orderBy(['updated_at' => SORT_DESC])
->one();
}
}
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
Unreleased
------------------------
- Fix #369: Misplaced Online dot
- Enh #377: New features: Unread & Pinned

3.1.2 (January 22, 2024)
------------------------
Expand Down
9 changes: 7 additions & 2 deletions helpers/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,14 @@ public static function toConversationUserList(Message $message)
return static::to(['/mail/mail/user-list', 'id' => $message->id]);
}

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

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

public static function toMessenger(Message $message = null, $scheme = false)
Expand Down Expand Up @@ -131,4 +136,4 @@ public static function toLoadMoreMessages()
{
return static::to(['/mail/mail/load-more']);
}
}
}
18 changes: 18 additions & 0 deletions models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,24 @@ public function deleteEntry($entry)
}
}

/**
* Mark this message as unread
*
* @param int $userId
*/
public function markUnread($userId)
{
$userMessage = UserMessage::findOne([
'message_id' => $this->id,
'user_id' => $userId
]);

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

/**
* User leaves a message
*
Expand Down
10 changes: 1 addition & 9 deletions models/UserMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,8 @@ public static function findByUser($userId = null)
->orderBy('message.updated_at DESC');
}

public function isUnread($userId = null)
public function isUnread(): bool
{
if ($userId === null) {
$userId = Yii::$app->user->id;
}

if($this->message->lastEntry && ($this->message->lastEntry->user_id === $userId)) {
return false;
}

return $this->message->updated_at > $this->last_viewed;
}

Expand Down
2 changes: 1 addition & 1 deletion resources/css/humhub.mail.css
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@
.mail-conversation-entry.hideUserInfo .author-label {
display: none;
}
.mail-conversation-entry.hideUserInfo .author-image img {
.mail-conversation-entry.hideUserInfo .author-image a {
visibility: hidden;
margin-top: 0;
}
Expand Down
2 changes: 1 addition & 1 deletion resources/css/humhub.mail.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 16 additions & 5 deletions resources/js/humhub.mail.conversation.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,21 @@ humhub.module('mail.conversation', function (module, require, $) {
});
};

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

module.export({
init: init,
leave: leave,
submitEditEntry: submitEditEntry,
deleteEntry: deleteEntry,
init,
leave,
markUnmark,
submitEditEntry,
deleteEntry,
});
});
});
27 changes: 19 additions & 8 deletions resources/js/humhub.mail.messenger.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ humhub.module('mail.ConversationView', function (module, require, $) {

module.export = ConversationView;
});


humhub.module('mail.ConversationEntry', function (module, require, $) {

var Widget = require('ui.widget').Widget;
Expand All @@ -439,7 +439,7 @@ humhub.module('mail.ConversationEntry', function (module, require, $) {
};

module.export = ConversationEntry;
});
});
humhub.module('mail.inbox', function (module, require, $) {

var Widget = require('ui.widget').Widget;
Expand Down Expand Up @@ -696,7 +696,7 @@ humhub.module('mail.inbox', function (module, require, $) {
setTagFilter: setTagFilter,
toggleInbox: toggleInbox
});
});
});
humhub.module('mail.conversation', function (module, require, $) {
var Widget = require('ui.widget').Widget;
var modal = require('ui.modal');
Expand Down Expand Up @@ -796,10 +796,21 @@ humhub.module('mail.conversation', function (module, require, $) {
});
};

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

module.export({
init: init,
leave: leave,
submitEditEntry: submitEditEntry,
deleteEntry: deleteEntry,
init,
leave,
markUnmark,
submitEditEntry,
deleteEntry,
});
});
});
Loading

0 comments on commit 22ce8fe

Please sign in to comment.