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.

142 lines
4.3 KiB

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Twitter;
use App\Tweet;
use App\Events\NewTweetEvent, App\Events\TweetClaimedEvent, App\Events\TweetAcceptedEvent;
use DB;
class DashboardController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('dashboard', [
]);
}
public function queue() {
Tweet::claimed_timeout();
$tweets = Tweet::queued()->get();
$queue = [];
foreach($tweets as $tweet) {
$queue[] = new NewTweetEvent($tweet);
}
return response()->json(['queue'=>$queue]);
}
public function ping() {
Tweet::claimed_timeout();
return response()->json(['result'=>'ok']);
}
public function claim_tweet(Request $request) {
$tweet = Tweet::where('id', $request->input('tweet_id'))->first();
if($tweet) {
if($request->input('status') == 'unclaimed') {
$tweet->claimed_at = null;
$tweet->save();
event(new NewTweetEvent($tweet));
} else {
$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']);
}
public function reject_tweet(Request $request) {
$tweet = Tweet::where('id', $request->input('tweet_id'))->first();
if($tweet) {
$tweet->processed = 1;
$tweet->m1_transit_line_id = null;
$tweet->m1_non_trimet = null;
$tweet->m2_transit_center_id = null;
$tweet->m2_with_other_team = 0;
$tweet->m3_complete = null;
$tweet->m4_complete = null;
$tweet->m5_complete = null;
$tweet->m5_tip = null;
$tweet->m6_complete = null;
$tweet->m7_document_id = null;
$tweet->save();
}
return response()->json(['result'=>'ok']);
}
public function score_tweet(Request $request) {
$tweet = Tweet::where('id', $request->input('tweet_id'))->first();
if($tweet) {
// Calculate the previous mission status before saving this tweet
$previousMissionStatus = $tweet->mission->complete($tweet->team);
$tweet->m1_transit_line_id = null;
$tweet->m1_non_trimet = null;
$tweet->m2_transit_center_id = null;
$tweet->m2_with_other_team = 0;
$tweet->m3_complete = null;
$tweet->m4_complete = null;
$tweet->m5_complete = null;
$tweet->m5_tip = null;
$tweet->m6_complete = null;
$tweet->m7_document_id = null;
foreach($request->input('score_data') as $k=>$v) {
$tweet->{$k} = $v;
}
$tweet->processed = 1;
$tweet->save();
event(new TweetAcceptedEvent($tweet, $previousMissionStatus));
}
return response()->json(['result'=>'ok']);
}
public function load_dropdowns() {
$documents = DB::table('m7_documents')->orderBy('id')->get();
$transit_centers = DB::table('transit_centers')->orderBy('name')->get();
$transit_lines = DB::table('transit_lines')->orderBy('sort')->get();
return response()->json([
'documents' => $documents,
'transit_centers' => $transit_centers,
'transit_lines' => $transit_lines
]);
}
public function reply_to_tweet(Request $request) {
$tweet = Tweet::where('id', $request->input('tweet_id'))->first();
if($tweet) {
$params = [
'auto_populate_reply_metadata' => true,
'in_reply_to_status_id' => $tweet->tweet_id,
'status' => $request->input('text')
];
$response = Twitter::postTweet($params);
if($response && $response->id_str) {
return response()->json(['result'=>'ok']);
}
}
return response()->json(['result'=>'error']);
}
}