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.

144 lines
4.4 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->accepted = 0;
  63. $tweet->m1_transit_line_id = null;
  64. $tweet->m1_non_trimet = null;
  65. $tweet->m2_transit_center_id = null;
  66. $tweet->m2_with_other_team = 0;
  67. $tweet->m3_complete = null;
  68. $tweet->m4_complete = null;
  69. $tweet->m5_complete = null;
  70. $tweet->m5_tip = null;
  71. $tweet->m6_complete = null;
  72. $tweet->m7_document_id = null;
  73. $tweet->save();
  74. }
  75. return response()->json(['result'=>'ok']);
  76. }
  77. public function score_tweet(Request $request) {
  78. $tweet = Tweet::where('id', $request->input('tweet_id'))->first();
  79. if($tweet) {
  80. // Calculate the previous mission status before saving this tweet
  81. $previousMissionStatus = $tweet->mission->complete($tweet->team);
  82. $tweet->m1_transit_line_id = null;
  83. $tweet->m1_non_trimet = null;
  84. $tweet->m2_transit_center_id = null;
  85. $tweet->m2_with_other_team = 0;
  86. $tweet->m3_complete = null;
  87. $tweet->m4_complete = null;
  88. $tweet->m5_complete = null;
  89. $tweet->m5_tip = null;
  90. $tweet->m6_complete = null;
  91. $tweet->m7_document_id = null;
  92. foreach($request->input('score_data') as $k=>$v) {
  93. $tweet->{$k} = $v;
  94. }
  95. $tweet->processed = 1;
  96. $tweet->accepted = 1;
  97. $tweet->save();
  98. event(new TweetAcceptedEvent($tweet, $previousMissionStatus));
  99. }
  100. return response()->json(['result'=>'ok']);
  101. }
  102. public function load_dropdowns() {
  103. $documents = DB::table('m7_documents')->orderBy('id')->get();
  104. $transit_centers = DB::table('transit_centers')->orderBy('name')->get();
  105. $transit_lines = DB::table('transit_lines')->orderBy('sort')->get();
  106. return response()->json([
  107. 'documents' => $documents,
  108. 'transit_centers' => $transit_centers,
  109. 'transit_lines' => $transit_lines
  110. ]);
  111. }
  112. public function reply_to_tweet(Request $request) {
  113. $tweet = Tweet::where('id', $request->input('tweet_id'))->first();
  114. if($tweet) {
  115. $params = [
  116. 'auto_populate_reply_metadata' => true,
  117. 'in_reply_to_status_id' => $tweet->tweet_id,
  118. 'status' => $request->input('text')
  119. ];
  120. $response = Twitter::postTweet($params);
  121. if($response && $response->id_str) {
  122. return response()->json(['result'=>'ok']);
  123. }
  124. }
  125. return response()->json(['result'=>'error']);
  126. }
  127. }