diff --git a/app/Events/TweetClaimedEvent.php b/app/Events/TweetClaimedEvent.php new file mode 100644 index 0000000..d17d873 --- /dev/null +++ b/app/Events/TweetClaimedEvent.php @@ -0,0 +1,40 @@ +tweet_id = $tweet->id; + } + + /** + * Get the channels the event should broadcast on. + * + * @return Channel|array + */ + public function broadcastOn() + { + return ['tweet-queue']; + } +} diff --git a/app/Http/Controllers/DashboardController.php b/app/Http/Controllers/DashboardController.php index 480c3a9..463e8de 100644 --- a/app/Http/Controllers/DashboardController.php +++ b/app/Http/Controllers/DashboardController.php @@ -4,7 +4,8 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; use Twitter; -use App\Tweet, App\Events\NewTweetEvent; +use App\Tweet; +use App\Events\NewTweetEvent, App\Events\TweetClaimedEvent; class DashboardController extends Controller { @@ -46,6 +47,9 @@ class DashboardController extends Controller if($tweet) { $tweet->claimed_at = date('Y-m-d H:i:s'); $tweet->save(); + + // Broadcast that this tweet was claimed + event(new TweetClaimedEvent($tweet)); } return response()->json(['result'=>'ok']); } diff --git a/app/Http/Controllers/TwitterController.php b/app/Http/Controllers/TwitterController.php index a4baaf5..6ccaaf3 100644 --- a/app/Http/Controllers/TwitterController.php +++ b/app/Http/Controllers/TwitterController.php @@ -58,7 +58,6 @@ class TwitterController extends BaseController $tweet->tweet_date = date('Y-m-d H:i:s', strtotime($data['created_at'])); $tweet->save(); - // TODO: Broadcast this to the web interface event(new NewTweetEvent($tweet)); return $data['id_str']; diff --git a/app/Tweet.php b/app/Tweet.php index f6b8545..48b7684 100644 --- a/app/Tweet.php +++ b/app/Tweet.php @@ -2,6 +2,7 @@ namespace App; use Illuminate\Database\Eloquent\Model; +use App\Events\NewTweetEvent; use DB; class Tweet extends Model @@ -27,12 +28,17 @@ class Tweet extends Model } public static function claimed_timeout() { - DB::table('tweets') - ->where('claimed_at', '<', date('Y-m-d H:i:s', time()-60)) // find tweets claimed longer than a minute ago - ->update(['claimed_at' => null]); + // find tweets claimed longer than a minute ago + $timeout = 60; + $tweets = Tweet::where('claimed_at', '<', date('Y-m-d H:i:s', time()-$timeout))->get(); + foreach($tweets as $tweet) { + $tweet->claimed_at = null; + $tweet->save(); + event(new NewTweetEvent($tweet)); + } } public static function queued() { - return Tweet::whereNull('claimed_at')->where('processed', 0)->where('mission_id', '>', 0); + return Tweet::whereNull('claimed_at')->where('processed', 0)->where('mission_id', '>', 0)->orderBy('tweet_date', 'asc'); } } diff --git a/public/js/app.js b/public/js/app.js index c8425e9..4e483c3 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -46506,22 +46506,33 @@ module.exports = { // Listen for new items on the queue Echo.channel('tweet-queue').listen('NewTweetEvent', function (e) { - console.log(e); - _this.queue.push(e); + if (_this.findTweetInQueue(e.tweet_id) === false) { + _this.queue.push(e); + } + }).listen('TweetClaimedEvent', function (e) { + _this.removeTweetFromQueue(e.tweet_id); }); }.bind(this)); }, - clickedTweet: function clickedTweet(event) { - var tweet_id = $(event.target).parents(".tweet").data('tweet-id'); - console.log(this.queue); - // Get the index of the clicked element + findTweetInQueue: function findTweetInQueue(tweet_id) { + var queueIndex = false; for (var i in this.queue) { if (parseInt(this.queue[i].tweet_id) == parseInt(tweet_id)) { - var queueIndex = i; + queueIndex = i; } } - console.log("Removing item at index " + queueIndex); - this.queue.splice(queueIndex, 1); + return queueIndex; + }, + removeTweetFromQueue: function removeTweetFromQueue(tweet_id) { + var queueIndex = this.findTweetInQueue(tweet_id); + if (queueIndex !== false) { + console.log("Removing item at index " + queueIndex); + this.queue.splice(queueIndex, 1); + } + }, + clickedTweet: function clickedTweet(event) { + var tweet_id = $(event.target).parents(".tweet").data('tweet-id'); + this.removeTweetFromQueue(tweet_id); // Mark this as claimed $.post('/dashboard/claim-tweet', { diff --git a/resources/assets/js/components/TweetQueue.vue b/resources/assets/js/components/TweetQueue.vue index e3b471c..612d34d 100644 --- a/resources/assets/js/components/TweetQueue.vue +++ b/resources/assets/js/components/TweetQueue.vue @@ -27,29 +27,41 @@ module.exports = { // Listen for new items on the queue Echo.channel('tweet-queue') .listen('NewTweetEvent', (e) => { - console.log(e); - this.queue.push(e); + if(this.findTweetInQueue(e.tweet_id) === false) { + this.queue.push(e); + } + }) + .listen('TweetClaimedEvent', (e) => { + this.removeTweetFromQueue(e.tweet_id); }); }.bind(this)); }, - clickedTweet(event) { - var tweet_id = $(event.target).parents(".tweet").data('tweet-id'); - console.log(this.queue); - // Get the index of the clicked element + findTweetInQueue(tweet_id) { + var queueIndex = false; for(var i in this.queue) { if(parseInt(this.queue[i].tweet_id) == parseInt(tweet_id)) { - var queueIndex = i; + queueIndex = i; } } - console.log("Removing item at index "+queueIndex); - this.queue.splice(queueIndex, 1); + return queueIndex; + }, + removeTweetFromQueue(tweet_id) { + var queueIndex = this.findTweetInQueue(tweet_id); + if(queueIndex !== false) { + console.log("Removing item at index "+queueIndex); + this.queue.splice(queueIndex, 1); + } + }, + clickedTweet(event) { + var tweet_id = $(event.target).parents(".tweet").data('tweet-id'); + this.removeTweetFromQueue(tweet_id); // Mark this as claimed $.post('/dashboard/claim-tweet', { tweet_id: tweet_id }, function(response) { - + }); }