Okay bài toán này trước đây mình đã xử lý. Tuy nhiên giờ thì xử lý triệt để hơn.
- Gửi notifications / email khi có favorited movies. Dựa trên genres / casts
- Gửi email khi có movie mới lên WordPress
Về cơ bản 2 bài toán này similar nhau.
- Observe table
movies
khi cócreated
- Kiểm tra favorites và send notification hoặc email
WordPress thì sử dụng tính năng Post by Email
Tuy nhiên sẽ có 1 vài notes
- Đưa email vào queue để kiểm soát việc gửi email liên tục
- Notifications bản chất chỉ để gửi các thông tin ngắn gọn. Do đó cần xử lý thích hợp notifications gì cần send out và tới channel nào
- Handle các movies nào đã được posted để tránh duplicate
- Cá nhân mình khi post WordPress sẽ default để mode là
draft
vì cần kiểm tra nội dung trước khi post. Cũng như tránh publish lên social vì dính 18+
[status publish | pending | draft | private]
public function movieCreated(MovieCreated $event)
{
$movie = $event->movie;
// Trigger notifications
foreach ($movie->tags()->cursor() as $tag) {
if ($tag->favorite()->exists()) {
$movie->notify(new FavoritedMovie());
break;
}
}
foreach ($movie->idols()->cursor() as $idol) {
if ($idol->favorite()->exists()) {
$movie->notify(new FavoritedMovie());
break;
}
}
// Do not create WordPress is we have no cover
if (!$movie->cover) {
return;
}
// Send movie post
if (WordPressPost::where(['title' => $movie->dvd_id])->exists()) {
return;
}
Mail::send(new WordPressMoviePost($movie));
WordPressPost::create(['title' => $movie->dvd_id]);
}
Leave a Reply