1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Auth; |
6
|
|
|
use Twitter; |
7
|
|
|
use Session; |
8
|
|
|
use App\Http\Requests; |
9
|
|
|
use Illuminate\Http\Request; |
10
|
|
|
|
11
|
|
|
class TwitterController extends Controller |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Item to be searched for, combing through twitter's massive data |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $searchItem; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Initialize the Controller with necessary arguments |
21
|
|
|
*/ |
22
|
|
|
public function __construct() |
23
|
|
|
{ |
24
|
|
|
$this->searchItem = 'laravel'; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Return all tweets to the Twitter API dashboard |
29
|
|
|
* @return mixed |
30
|
|
|
*/ |
31
|
|
|
public function getPage() |
32
|
|
|
{ |
33
|
|
|
if( Session::get('provider') !== 'twitter') { |
34
|
|
|
Auth::logout(); |
35
|
|
|
|
36
|
|
|
Session::flush(); |
37
|
|
|
|
38
|
|
|
return redirect('/auth/twitter'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$searchedTweets = json_decode($this->searchForTweets($this->searchItem), true); |
42
|
|
|
|
43
|
|
|
return view('api.twitter')->withTweets($searchedTweets['statuses']); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get the latest tweets on a user timeline |
48
|
|
|
* @return Collection |
49
|
|
|
*/ |
50
|
|
|
private function getLatestTweets() |
51
|
|
|
{ |
52
|
|
|
return Twitter::getHomeTimeline(['count' => 2, 'format' => 'json']); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Search for tweets based on a search query |
57
|
|
|
* @param string $item |
58
|
|
|
* @return Collection |
59
|
|
|
*/ |
60
|
|
|
private function searchForTweets($item) |
61
|
|
|
{ |
62
|
|
|
return Twitter::getSearch(['q' => $item, 'count' => 4, 'format' => 'json']); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Post a tweet to the timeline |
67
|
|
|
* @param Request $request |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function sendTweet(Request $request) |
71
|
|
|
{ |
72
|
|
|
$this->validate($request, [ |
73
|
|
|
'tweet' => 'required', |
74
|
|
|
]); |
75
|
|
|
|
76
|
|
|
$tweet = $request->input('tweet') . ' #LaravelHackathonStarter'; |
77
|
|
|
|
78
|
|
|
Twitter::reconfig(['token' => Auth::user()->getAccessToken(), 'secret' => Auth::user()->getAccessTokenSecret()]); |
79
|
|
|
|
80
|
|
|
Twitter::postTweet(['status' => $tweet, 'format' => 'json']); |
81
|
|
|
|
82
|
|
|
return redirect()->back()->with('info', trans('texts.twitter.success')); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|