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.

76 lines
2.0 KiB

7 years ago
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. use App\Events\NewTweetEvent;
  5. use DB;
  6. use DateTime, DateTimeZone;
  7. class Tweet extends Model
  8. {
  9. protected $fillable = [
  10. 'tweet_id', 'player_id', 'team_id', 'text', 'photo', 'claimed_at', 'processed', 'mission_id', 'tweet_date',
  11. 'm1_transit_line_id', 'm1_non_trimet',
  12. 'm2_transit_center_id', 'm2_with_other_team',
  13. 'm3_complete', 'm4_complete', 'm5_complete', 'm5_tip',
  14. 'm6_complete', 'm7_document_id'
  15. ];
  16. public function team() {
  17. return $this->belongsTo('\App\Team');
  18. }
  19. public function player() {
  20. return $this->belongsTo('\App\Player');
  21. }
  22. public function mission() {
  23. return $this->belongsTo('\App\Mission');
  24. }
  25. public function transit_line() {
  26. return $this->belongsTo('\App\TransitLine', 'm1_transit_line_id');
  27. }
  28. public function transit_center() {
  29. return $this->belongsTo('\App\TransitCenter', 'm2_transit_center_id');
  30. }
  31. public function document() {
  32. return $this->belongsTo('\App\Document', 'm7_document_id');
  33. }
  34. public function photos() {
  35. if($this->photo) {
  36. return json_decode($this->photo);
  37. } else {
  38. return [];
  39. }
  40. }
  41. public function localtime() {
  42. $date = new DateTime($this->tweet_date);
  43. $date->setTimeZone(new DateTimeZone('US/Pacific')); // TODO: put in config or somewhere
  44. return $date;
  45. }
  46. public function twitter_permalink() {
  47. return 'https://twitter.com/'.$this->player->twitter.'/status/'.$this->tweet_id;
  48. }
  49. public static function claimed_timeout() {
  50. // time out tweets if they aren't processed after the specified time
  51. $timeout = 300;
  52. $tweets = Tweet::where('claimed_at', '<', date('Y-m-d H:i:s', time()-$timeout))->where('processed', 0)->get();
  53. foreach($tweets as $tweet) {
  54. $tweet->claimed_at = null;
  55. $tweet->save();
  56. event(new NewTweetEvent($tweet));
  57. }
  58. }
  59. public static function queued() {
  60. return Tweet::whereNull('claimed_at')->where('processed', 0)
  61. ->where('mission_id', '>', 0)->orderBy('tweet_date', 'asc');
  62. }
  63. }