You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.1 KiB

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Events\NewTweetEvent;
use DB;
use DateTime, DateTimeZone;
class Tweet extends Model
{
protected $fillable = [
'tweet_id', 'player_id', 'team_id', 'text', 'photo', 'claimed_at', 'processed', 'mission_id', 'tweet_date',
'm1_transit_line_id', 'm1_non_trimet',
'm2_transit_center_id', 'm2_with_other_team',
'm3_complete', 'm4_complete', 'm5_complete', 'm5_tip',
'm6_complete', 'm7_document_id',
'geo', 'place'
];
public function team() {
return $this->belongsTo('\App\Team');
}
public function player() {
return $this->belongsTo('\App\Player');
}
public function mission() {
return $this->belongsTo('\App\Mission');
}
public function transit_line() {
return $this->belongsTo('\App\TransitLine', 'm1_transit_line_id');
}
public function transit_center() {
return $this->belongsTo('\App\TransitCenter', 'm2_transit_center_id');
}
public function document() {
return $this->belongsTo('\App\Document', 'm7_document_id');
}
public function photos() {
if($this->photo) {
return json_decode($this->photo);
} else {
return [];
}
}
public function localtime() {
$date = new DateTime($this->tweet_date);
$date->setTimeZone(new DateTimeZone('US/Pacific')); // TODO: put in config or somewhere
return $date;
}
public function twitter_permalink() {
return 'https://twitter.com/'.($this->player ? $this->player->twitter : '_').'/status/'.$this->tweet_id;
}
public static function claimed_timeout() {
// time out tweets if they aren't processed after the specified time
$timeout = 300;
$tweets = Tweet::where('claimed_at', '<', date('Y-m-d H:i:s', time()-$timeout))->where('processed', 0)->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)->orderBy('tweet_date', 'asc');
}
}