-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathBttvController.php
125 lines (101 loc) · 3.39 KB
/
BttvController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Helpers\Helper;
use App\Repositories\BttvApiRepository;
use App\Repositories\TwitchApiRepository;
use Exception;
use App\Exceptions\BttvApiException;
use App\Exceptions\TwitchApiException;
class BttvController extends Controller
{
/**
* @var App\Repositories\BttvApiRepository
*/
private $bttvApi;
public function __construct(BttvApiRepository $bttvRepo)
{
$this->bttvApi = $bttvRepo;
}
/**
* The BTTV route homepage view
*
* @param Request $request
* @return Response
*/
public function home(Request $request)
{
$channel = trim($request->input('channel', null));
$msg = null;
$data = [
'channel' => $channel,
'message' => $msg,
'page' => 'home'
];
if (empty($channel)) {
return view('bttv.home', $data);
}
try {
$user = $this->bttvApi->userByTwitchName($channel);
}
catch (Exception $ex)
{
$data['message'] = 'Unable to retrieve BetterTTV details for channel: ' . $channel;
return view('bttv.home', $data);
}
$emotes = $user['emotes'];
// Merge channel and 'shared' emotes
$emotes = array_merge($emotes['channel'], $emotes['shared']);
$data['user'] = $user;
$data['emotes'] = $emotes;
return view('bttv.home', $data);
}
/**
* Retrieves the available BetterTTV channel emotes for the specified channel.
*
* @param Request $request
* @param String $emotes Route name
* @param String $channel The channel name
* @return Response
*/
public function emotes(Request $request, $emotes = null, $channel = null)
{
$channel = $channel ?: $request->input('channel', null);
$types = $request->input('types', 'all');
if (empty($channel)) {
return Helper::text('You have to specify a channel name');
}
try {
$user = $this->bttvApi->userByTwitchName($channel);
}
catch (Exception $ex)
{
return Helper::text('Unable to retrieve BetterTTV details for channel: ' . $channel);
}
$types = explode(',', $types);
$emotes = $user['emotes'];
$channelEmotes = $emotes['channel'];
$sharedEmotes = $emotes['shared'];
if (count($channelEmotes) === 0 && count($sharedEmotes) === 0) {
return Helper::text($channel . ' does not have any BetterTTV emotes.');
}
/**
* Only allow emotes that are 'live', approved and matches the correct type.
*/
$emoteFilter = function($emote) use ($types) {
$isLive = $emote['live'];
// Filter by emote type: `all` is the default value and includes all emote types.
$isType = in_array('all', $types) || in_array($emote['type'], $types);
return $isLive && $isType;
};
$allEmotes = array_merge($channelEmotes, $sharedEmotes);
$allEmotes = array_filter($allEmotes, $emoteFilter);
$getEmoteCodes = function($emote) {
return $emote['code'];
};
$codes = array_map($getEmoteCodes, $allEmotes);
return Helper::text(implode(' ', $codes));
}
}