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

6 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. 'geo', 'place'
  16. ];
  17. public function team() {
  18. return $this->belongsTo('\App\Team');
  19. }
  20. public function player() {
  21. return $this->belongsTo('\App\Player');
  22. }
  23. public function mission() {
  24. return $this->belongsTo('\App\Mission');
  25. }
  26. public function transit_line() {
  27. return $this->belongsTo('\App\TransitLine', 'm1_transit_line_id');
  28. }
  29. public function transit_center() {
  30. return $this->belongsTo('\App\TransitCenter', 'm2_transit_center_id');
  31. }
  32. public function document() {
  33. return $this->belongsTo('\App\Document', 'm7_document_id');
  34. }
  35. public function photos() {
  36. if($this->photo) {
  37. return json_decode($this->photo);
  38. } else {
  39. return [];
  40. }
  41. }
  42. public function localtime() {
  43. $date = new DateTime($this->tweet_date);
  44. $date->setTimeZone(new DateTimeZone('US/Pacific')); // TODO: put in config or somewhere
  45. return $date;
  46. }
  47. public function twitter_permalink() {
  48. return 'https://twitter.com/'.($this->player ? $this->player->twitter : $this->author_username).'/status/'.$this->tweet_id;
  49. }
  50. public static function claimed_timeout() {
  51. // time out tweets if they aren't processed after the specified time
  52. $timeout = 300;
  53. $tweets = Tweet::where('claimed_at', '<', date('Y-m-d H:i:s', time()-$timeout))->where('processed', 0)->get();
  54. foreach($tweets as $tweet) {
  55. $tweet->claimed_at = null;
  56. $tweet->save();
  57. event(new NewTweetEvent($tweet));
  58. }
  59. }
  60. public static function queued() {
  61. return Tweet::whereNull('claimed_at')->where('processed', 0)
  62. ->where('mission_id', '>', 0)->orderBy('tweet_date', 'asc');
  63. }
  64. }