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.

146 lines
4.5 KiB

7 years ago
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Twitter;
  5. use App\Tweet;
  6. use App\Events\NewTweetEvent, App\Events\TweetClaimedEvent, App\Events\TweetAcceptedEvent;
  7. use DB;
  8. class DashboardController extends Controller
  9. {
  10. /**
  11. * Create a new controller instance.
  12. *
  13. * @return void
  14. */
  15. public function __construct()
  16. {
  17. $this->middleware('auth');
  18. }
  19. /**
  20. * Show the application dashboard.
  21. *
  22. * @return \Illuminate\Http\Response
  23. */
  24. public function index()
  25. {
  26. return view('dashboard', []);
  27. }
  28. public function queue() {
  29. Tweet::claimed_timeout();
  30. $tweets = Tweet::queued()->get();
  31. $queue = [];
  32. foreach($tweets as $tweet) {
  33. $queue[] = new NewTweetEvent($tweet);
  34. }
  35. return response()->json(['queue'=>$queue]);
  36. }
  37. public function ping() {
  38. Tweet::claimed_timeout();
  39. return response()->json(['result'=>'ok']);
  40. }
  41. public function claim_tweet(Request $request) {
  42. $tweet = Tweet::where('id', $request->input('tweet_id'))->first();
  43. if($tweet) {
  44. if($request->input('status') == 'unclaimed') {
  45. $tweet->claimed_at = null;
  46. $tweet->save();
  47. event(new NewTweetEvent($tweet));
  48. } else {
  49. $tweet->claimed_at = date('Y-m-d H:i:s');
  50. $tweet->save();
  51. // Broadcast that this tweet was claimed
  52. event(new TweetClaimedEvent($tweet));
  53. }
  54. }
  55. return response()->json(['result'=>'ok']);
  56. }
  57. public function reject_tweet(Request $request) {
  58. $tweet = Tweet::where('id', $request->input('tweet_id'))->first();
  59. if($tweet) {
  60. $tweet->processed = 1;
  61. $tweet->accepted = 0;
  62. $tweet->m1_transit_line_id = null;
  63. $tweet->m1_non_trimet = null;
  64. $tweet->m2_transit_center_id = null;
  65. $tweet->m2_with_other_team = 0;
  66. $tweet->m3_complete = null;
  67. $tweet->m4_complete = null;
  68. $tweet->m5_complete = null;
  69. $tweet->m5_tip = null;
  70. $tweet->m6_complete = null;
  71. $tweet->m7_document_id = null;
  72. $tweet->save();
  73. }
  74. return response()->json(['result'=>'ok']);
  75. }
  76. public function score_tweet(Request $request) {
  77. $tweet = Tweet::where('id', $request->input('tweet_id'))->first();
  78. if($tweet) {
  79. // Calculate the previous mission status before saving this tweet
  80. if($tweet->team)
  81. $previousMissionStatus = $tweet->mission->complete($tweet->team);
  82. else
  83. $previousMissionStatus = false;
  84. $tweet->m1_transit_line_id = null;
  85. $tweet->m1_non_trimet = null;
  86. $tweet->m2_transit_center_id = null;
  87. $tweet->m2_with_other_team = 0;
  88. $tweet->m3_complete = null;
  89. $tweet->m4_complete = null;
  90. $tweet->m5_complete = null;
  91. $tweet->m5_tip = null;
  92. $tweet->m6_complete = null;
  93. $tweet->m7_document_id = null;
  94. foreach($request->input('score_data') as $k=>$v) {
  95. $tweet->{$k} = $v;
  96. }
  97. $tweet->processed = 1;
  98. $tweet->accepted = 1;
  99. $tweet->save();
  100. event(new TweetAcceptedEvent($tweet, $previousMissionStatus));
  101. }
  102. return response()->json(['result'=>'ok']);
  103. }
  104. public function load_dropdowns() {
  105. $documents = DB::table('m7_documents')->orderBy('id')->get();
  106. $transit_centers = DB::table('transit_centers')->orderBy('name')->get();
  107. $transit_lines = DB::table('transit_lines')->orderBy('sort')->get();
  108. return response()->json([
  109. 'documents' => $documents,
  110. 'transit_centers' => $transit_centers,
  111. 'transit_lines' => $transit_lines
  112. ]);
  113. }
  114. public function reply_to_tweet(Request $request) {
  115. $tweet = Tweet::where('id', $request->input('tweet_id'))->first();
  116. if($tweet) {
  117. $params = [
  118. 'auto_populate_reply_metadata' => true,
  119. 'in_reply_to_status_id' => $tweet->tweet_id,
  120. 'status' => $request->input('text')
  121. ];
  122. $response = Twitter::postTweet($params);
  123. if($response && $response->id_str) {
  124. return response()->json(['result'=>'ok']);
  125. }
  126. }
  127. return response()->json(['result'=>'error']);
  128. }
  129. }