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.

70 lines
1.9 KiB

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Log;
use App\Player, App\Team, App\Tweet, App\TransitCenter, App\TransitLine, App\Document, App\Mission;
use App\Events\NewTweetEvent;
class TwitterController extends BaseController
{
public function input(Request $request)
{
$data = $request->all();
// Find the user who tweeted this
$twitter_user_id = $data['user']['id_str'];
$player = Player::where('twitter_user_id', $twitter_user_id)->first();
if(isset($data['extended_tweet']['full_text']))
$text = $data['extended_tweet']['full_text'];
else
$text = $data['text'];
// Unshorten URLs
if(isset($data['entities']['urls'])) {
foreach($data['entities']['urls'] as $url) {
$text = str_replace($url['url'], $url['expanded_url'], $text);
}
}
// Remove media URLs from tweet text
$photos = [];
if(isset($data['extended_entities']['media'])) {
foreach($data['extended_entities']['media'] as $media) {
$text = str_replace($media['url'], '', $text);
$photos[] = $media['media_url_https'];
}
$text = trim($text);
}
// Find the mission hashtag
$mission_id = 0;
foreach(Mission::all() as $mission) {
if(strpos($text, $mission->hashtag) !== false) {
$mission_id = $mission->id;
}
}
$tweet = new Tweet;
$tweet->tweet_id = $data['id_str'];
$tweet->player_id = $player ? $player->id : 0;
$tweet->team_id = $player ? $player->team->id : 0;
$tweet->text = $text;
$tweet->photo = json_encode($photos, JSON_UNESCAPED_SLASHES);
$tweet->mission_id = $mission_id;
$tweet->tweet_date = date('Y-m-d H:i:s', strtotime($data['created_at']));
$tweet->save();
if($tweet->mission_id) {
event(new NewTweetEvent($tweet));
}
return $data['id_str'];
}
}