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

  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. }
  29. public function queue() {
  30. Tweet::claimed_timeout();
  31. $tweets = Tweet::queued()->get();
  32. $queue = [];
  33. foreach($tweets as $tweet) {
  34. $queue[] = new NewTweetEvent($tweet);
  35. }
  36. return response()->json(['queue'=>$queue]);
  37. }
  38. public function ping() {
  39. Tweet::claimed_timeout();
  40. return response()->json(['result'=>'ok']);
  41. }
  42. public function claim_tweet(Request $request) {
  43. $tweet = Tweet::where('id', $request->input('tweet_id'))->first();
  44. if($tweet) {
  45. if($request->input('status') == 'unclaimed') {
  46. $tweet->claimed_at = null;
  47. $tweet->save();
  48. event(new NewTweetEvent($tweet));
  49. } else {
  50. $tweet->claimed_at = date('Y-m-d H:i:s');
  51. $tweet->save();
  52. // Broadcast that this tweet was claimed
  53. event(new TweetClaimedEvent($tweet));
  54. }
  55. }
  56. return response()->json(['result'=>'ok']);
  57. }
  58. public function reject_tweet(Request $request) {
  59. $tweet = Tweet::where('id', $request->input('tweet_id'))->first();
  60. if($tweet) {
  61. $tweet->processed = 1;
  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. $previousMissionStatus = $tweet->mission->complete($tweet->team);
  81. $tweet->m1_transit_line_id = null;
  82. $tweet->m1_non_trimet = null;
  83. $tweet->m2_transit_center_id = null;
  84. $tweet->m2_with_other_team = 0;
  85. $tweet->m3_complete = null;
  86. $tweet->m4_complete = null;
  87. $tweet->m5_complete = null;
  88. $tweet->m5_tip = null;
  89. $tweet->m6_complete = null;
  90. $tweet->m7_document_id = null;
  91. foreach($request->input('score_data') as $k=>$v) {
  92. $tweet->{$k} = $v;
  93. }
  94. $tweet->processed = 1;
  95. $tweet->save();
  96. event(new TweetAcceptedEvent($tweet, $previousMissionStatus));
  97. }
  98. return response()->json(['result'=>'ok']);
  99. }
  100. public function load_dropdowns() {
  101. $documents = DB::table('m7_documents')->orderBy('id')->get();
  102. $transit_centers = DB::table('transit_centers')->orderBy('name')->get();
  103. $transit_lines = DB::table('transit_lines')->orderBy('sort')->get();
  104. return response()->json([
  105. 'documents' => $documents,
  106. 'transit_centers' => $transit_centers,
  107. 'transit_lines' => $transit_lines
  108. ]);
  109. }
  110. public function reply_to_tweet(Request $request) {
  111. $tweet = Tweet::where('id', $request->input('tweet_id'))->first();
  112. if($tweet) {
  113. $params = [
  114. 'auto_populate_reply_metadata' => true,
  115. 'in_reply_to_status_id' => $tweet->tweet_id,
  116. 'status' => $request->input('text')
  117. ];
  118. $response = Twitter::postTweet($params);
  119. if($response && $response->id_str) {
  120. return response()->json(['result'=>'ok']);
  121. }
  122. }
  123. return response()->json(['result'=>'error']);
  124. }
  125. }