Browse Source

broadcast to other viewers when a tweet is claimed

master
Aaron Parecki 7 years ago
parent
commit
3bb64dbbec
No known key found for this signature in database GPG Key ID: 276C2817346D6056
6 changed files with 97 additions and 25 deletions
  1. +40
    -0
      app/Events/TweetClaimedEvent.php
  2. +5
    -1
      app/Http/Controllers/DashboardController.php
  3. +0
    -1
      app/Http/Controllers/TwitterController.php
  4. +10
    -4
      app/Tweet.php
  5. +20
    -9
      public/js/app.js
  6. +22
    -10
      resources/assets/js/components/TweetQueue.vue

+ 40
- 0
app/Events/TweetClaimedEvent.php View File

@ -0,0 +1,40 @@
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\Tweet;
class TweetClaimedEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $tweet_id;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Tweet $tweet)
{
$this->tweet_id = $tweet->id;
}
/**
* Get the channels the event should broadcast on.
*
* @return Channel|array
*/
public function broadcastOn()
{
return ['tweet-queue'];
}
}

+ 5
- 1
app/Http/Controllers/DashboardController.php View File

@ -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']);
}

+ 0
- 1
app/Http/Controllers/TwitterController.php View File

@ -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'];

+ 10
- 4
app/Tweet.php View File

@ -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');
}
}

+ 20
- 9
public/js/app.js View File

@ -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', {

+ 22
- 10
resources/assets/js/components/TweetQueue.vue View File

@ -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) {
});
}

Loading…
Cancel
Save