Skip to content

Commit

Permalink
add notify mail delay
Browse files Browse the repository at this point in the history
  • Loading branch information
summerblue committed Mar 22, 2017
1 parent e041411 commit a5a2b70
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
4 changes: 3 additions & 1 deletion app/Http/Controllers/MessagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ public function store(MessageRequest $request, Markdown $markdown)
$thread->addParticipant($recipient->id);

// Notify user by Email
dispatch(new SendNotifyMail('new_message', Auth::user(), $recipient, null, null, $message));
$job = (new SendNotifyMail('new_message', Auth::user(), $recipient, null, null, $message))
->delay(config('phphub.notify_delay'));
dispatch($job);

// notifications count
$recipient->message_count++;
Expand Down
9 changes: 7 additions & 2 deletions app/Models/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ public static function batchNotify($type, User $fromUser, $users, Topic $topic,
if (count($data)) {
Notification::insert($data);
foreach ($users as $toUser) {
dispatch(new SendNotifyMail($type, $fromUser, $toUser, $topic, $reply, $content));
$job = (new SendNotifyMail($type, $fromUser, $toUser, $topic, $reply, $content))
->delay(config('phphub.notify_delay'));
dispatch($job);
}
}

Expand Down Expand Up @@ -114,7 +116,10 @@ public static function notify($type, User $fromUser, User $toUser, Topic $topic

Notification::insert([$data]);

dispatch(new SendNotifyMail($type, $fromUser, $toUser, $topic, $reply));
$job = (new SendNotifyMail($type, $fromUser, $toUser, $topic, $reply))
->delay(config('phphub.notify_delay'));
dispatch($job);

self::pushNotification($data);
}

Expand Down
26 changes: 22 additions & 4 deletions app/Phphub/Handler/EmailHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@ public function sendActivateMail(User $user)
public function sendNotifyMail($type, User $fromUser, User $toUser, Topic $topic = null, Reply $reply = null, $body = null)
{
if (
!isset($this->methodMap[$type])
|| $toUser->email_notify_enabled != 'yes'
|| $toUser->id == $fromUser->id
|| !$toUser->email || $toUser->verified != 1
!isset($this->methodMap[$type]) // 不是运行的类型
|| $toUser->email_notify_enabled != 'yes' // 没开启邮件通知
|| $toUser->id == $fromUser->id // 发件和收件是同一个人
|| !$toUser->email // 不存在邮件
|| $toUser->verified != 1 // 还未验证
|| $this->_checkNecessary($type, $toUser) // 因延迟触发的,用户可能已读过站内通知
) {
return false;
}
Expand Down Expand Up @@ -210,4 +212,20 @@ private function _send($topic, $user, $subject, $action, $content, $mailog = '')
$this->generateMailLog($mailog);
});
}

private function _checkNecessary($type, User $toUser)
{
// 从数据库中重新读取用户
$user = User::find($toUser->id);

// 私信,如果已读
if ($type == 'new_message' || $user->message_count <= 0) {
return true;
// 通知,如果已读
} elseif ($user->notification_count <= 0) {
return true;
}

return false;
}
}
2 changes: 2 additions & 0 deletions config/phphub.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@

'wiki_topic_id' => env('WIKI_TOPIC_ID') ?:1,
'admin_board_cid' => env('ADMIN_BOARD_CID') ?:0,

'notify_delay' => 180,
];

0 comments on commit a5a2b70

Please sign in to comment.