From dbcab8ffe3974df453d56e275591487a41986f0d Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Tue, 4 Jul 2017 13:35:24 -0700 Subject: [PATCH] simple tweet viewer for teams and missions --- app/Http/Controllers/DashboardController.php | 2 ++ app/Http/Controllers/ShowTweetsController.php | 35 +++++++++++++++++++ app/Http/Controllers/TeamController.php | 1 + app/Mission.php | 4 +++ app/Team.php | 6 +++- app/Tweet.php | 21 ++++++++++- .../2017_07_04_200516_team_slug.php | 32 +++++++++++++++++ .../2017_07_04_201225_mission_slug.php | 32 +++++++++++++++++ ...17_07_04_201516_explicit_mark_accepted.php | 32 +++++++++++++++++ database/seeds/MissionSeeder.php | 2 +- public/css/app.css | 13 +++++++ resources/assets/sass/app.scss | 14 ++++++++ resources/views/components/tweet.blade.php | 23 ++++++++++++ resources/views/mission-tweets.blade.php | 15 ++++++++ resources/views/scoreboard.blade.php | 4 +-- resources/views/team-tweets.blade.php | 15 ++++++++ 16 files changed, 246 insertions(+), 5 deletions(-) create mode 100644 app/Http/Controllers/ShowTweetsController.php create mode 100644 database/migrations/2017_07_04_200516_team_slug.php create mode 100644 database/migrations/2017_07_04_201225_mission_slug.php create mode 100644 database/migrations/2017_07_04_201516_explicit_mark_accepted.php create mode 100644 resources/views/components/tweet.blade.php create mode 100644 resources/views/mission-tweets.blade.php create mode 100644 resources/views/team-tweets.blade.php diff --git a/app/Http/Controllers/DashboardController.php b/app/Http/Controllers/DashboardController.php index 4624323..3093049 100644 --- a/app/Http/Controllers/DashboardController.php +++ b/app/Http/Controllers/DashboardController.php @@ -69,6 +69,7 @@ class DashboardController extends Controller $tweet = Tweet::where('id', $request->input('tweet_id'))->first(); if($tweet) { $tweet->processed = 1; + $tweet->accepted = 0; $tweet->m1_transit_line_id = null; $tweet->m1_non_trimet = null; $tweet->m2_transit_center_id = null; @@ -104,6 +105,7 @@ class DashboardController extends Controller $tweet->{$k} = $v; } $tweet->processed = 1; + $tweet->accepted = 1; $tweet->save(); event(new TweetAcceptedEvent($tweet, $previousMissionStatus)); } diff --git a/app/Http/Controllers/ShowTweetsController.php b/app/Http/Controllers/ShowTweetsController.php new file mode 100644 index 0000000..3045f49 --- /dev/null +++ b/app/Http/Controllers/ShowTweetsController.php @@ -0,0 +1,35 @@ +id) + ->where('processed', 1) + ->where('accepted', 1) + ->orderBy('tweet_date', 'desc')->get(); + + return view('mission-tweets', [ + 'mission' => $mission, + 'tweets' => $tweets + ]); + } + + public function team(Team $team) { + $tweets = Tweet::where('team_id', $team->id) + ->where('processed', 1) + ->where('accepted', 1) + ->orderBy('tweet_date', 'desc')->get(); + + return view('team-tweets', [ + 'team' => $team, + 'tweets' => $tweets + ]); + } + +} diff --git a/app/Http/Controllers/TeamController.php b/app/Http/Controllers/TeamController.php index 2ae2add..1c92d64 100644 --- a/app/Http/Controllers/TeamController.php +++ b/app/Http/Controllers/TeamController.php @@ -75,6 +75,7 @@ class TeamController extends Controller $team = new \App\Team; $team->name = $next; + $team->slug = strtolower($next); $team->color = $colors[$next]; $team->save(); diff --git a/app/Mission.php b/app/Mission.php index a1601cd..c1462cc 100644 --- a/app/Mission.php +++ b/app/Mission.php @@ -10,6 +10,10 @@ class Mission extends Model 'hashtag', ]; + public function getRouteKeyName() { + return 'slug'; + } + public function tweets() { return $this->hasMany('App\Tweet'); } diff --git a/app/Team.php b/app/Team.php index 26c631d..894cb08 100644 --- a/app/Team.php +++ b/app/Team.php @@ -7,9 +7,13 @@ use Illuminate\Database\Eloquent\Model; class Team extends Model { protected $fillable = [ - 'name', 'color' + 'name', 'slug', 'color' ]; + public function getRouteKeyName() { + return 'slug'; + } + public function players() { return $this->hasMany('App\Player'); } diff --git a/app/Tweet.php b/app/Tweet.php index 4af9474..bed14dc 100644 --- a/app/Tweet.php +++ b/app/Tweet.php @@ -4,6 +4,7 @@ namespace App; use Illuminate\Database\Eloquent\Model; use App\Events\NewTweetEvent; use DB; +use DateTime, DateTimeZone; class Tweet extends Model { @@ -39,9 +40,27 @@ class Tweet extends Model 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->twitter.'/status/'.$this->tweet_id; + } + public static function claimed_timeout() { // time out tweets if they aren't processed after the specified time - $timeout = 520; + $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; diff --git a/database/migrations/2017_07_04_200516_team_slug.php b/database/migrations/2017_07_04_200516_team_slug.php new file mode 100644 index 0000000..6a31104 --- /dev/null +++ b/database/migrations/2017_07_04_200516_team_slug.php @@ -0,0 +1,32 @@ +string('slug', 255); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('teams', function (Blueprint $table) { + $table->dropColumn('slug'); + }); + } +} diff --git a/database/migrations/2017_07_04_201225_mission_slug.php b/database/migrations/2017_07_04_201225_mission_slug.php new file mode 100644 index 0000000..a41cd13 --- /dev/null +++ b/database/migrations/2017_07_04_201225_mission_slug.php @@ -0,0 +1,32 @@ +string('slug', 255); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('missions', function (Blueprint $table) { + $table->dropColumn('slug'); + }); + } +} diff --git a/database/migrations/2017_07_04_201516_explicit_mark_accepted.php b/database/migrations/2017_07_04_201516_explicit_mark_accepted.php new file mode 100644 index 0000000..75847ac --- /dev/null +++ b/database/migrations/2017_07_04_201516_explicit_mark_accepted.php @@ -0,0 +1,32 @@ +boolean('accepted')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('tweets', function (Blueprint $table) { + $table->dropColumn('accepted'); + }); + } +} diff --git a/database/seeds/MissionSeeder.php b/database/seeds/MissionSeeder.php index 6683ee9..4712185 100644 --- a/database/seeds/MissionSeeder.php +++ b/database/seeds/MissionSeeder.php @@ -21,6 +21,6 @@ class MissionSeeder extends Seeder '#document', ]; foreach($missions as $i=>$m) - DB::table('missions')->insert(['id' => $i+1, 'hashtag' => $m]); + DB::table('missions')->insert(['id' => $i+1, 'hashtag' => $m, 'slug' => trim($m, '#')]); } } diff --git a/public/css/app.css b/public/css/app.css index 92fbea3..1ec1406 100644 --- a/public/css/app.css +++ b/public/css/app.css @@ -8753,3 +8753,16 @@ button.close { display: inline-block; } +.tweet { + border: 1px #d3e0e9 solid; + border-radius: 4px; + padding: 6px; + margin-bottom: 6px; +} + +.tweet .text { + white-space: pre-wrap; + font-size: 1.5em; + margin: 8px 0; +} + diff --git a/resources/assets/sass/app.scss b/resources/assets/sass/app.scss index 7dcb75d..8e0c62e 100644 --- a/resources/assets/sass/app.scss +++ b/resources/assets/sass/app.scss @@ -252,3 +252,17 @@ } +.tweet { + border: 1px $laravel-border-color solid; + border-radius: 4px; + padding: 6px; + margin-bottom: 6px; + + .text { + white-space: pre-wrap; + font-size: 1.5em; + margin: 8px 0; + } + +} + diff --git a/resources/views/components/tweet.blade.php b/resources/views/components/tweet.blade.php new file mode 100644 index 0000000..44f2c65 --- /dev/null +++ b/resources/views/components/tweet.blade.php @@ -0,0 +1,23 @@ +
+ + + +
{!! Twitter::linkify($tweet->text) !!}
+ + @if($tweet->photos()) +
+ @foreach($tweet->photos() as $photo) +
+ +
+ @endforeach +
+
+ @endif + + {{ $tweet->localtime()->format('F j, g:ia') }} + +
diff --git a/resources/views/mission-tweets.blade.php b/resources/views/mission-tweets.blade.php new file mode 100644 index 0000000..2e796b3 --- /dev/null +++ b/resources/views/mission-tweets.blade.php @@ -0,0 +1,15 @@ +@extends('layouts.app') + +@section('content') +
+
+
+ +

{{ $mission->hashtag }}

+ + @each('components.tweet', $tweets, 'tweet') + +
+
+
+@endsection diff --git a/resources/views/scoreboard.blade.php b/resources/views/scoreboard.blade.php index a5c61e2..9d236d7 100644 --- a/resources/views/scoreboard.blade.php +++ b/resources/views/scoreboard.blade.php @@ -12,7 +12,7 @@ @foreach($missions as $mission)

{{ $mission->id }}

- {{ $mission->hashtag }} + {{ $mission->hashtag }} @endforeach Score @@ -24,7 +24,7 @@
- {{ $team->name }} + {{ $team->name }}
@foreach($team->players as $player) diff --git a/resources/views/team-tweets.blade.php b/resources/views/team-tweets.blade.php new file mode 100644 index 0000000..b3a7f3b --- /dev/null +++ b/resources/views/team-tweets.blade.php @@ -0,0 +1,15 @@ +@extends('layouts.app') + +@section('content') +
+
+
+ +

Team {{ $team->name }}

+ + @each('components.tweet', $tweets, 'tweet') + +
+
+
+@endsection