1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://dukt.net/twitter/ |
4
|
|
|
* @copyright Copyright (c) Dukt |
5
|
|
|
* @license https://github.com/dukt/twitter/blob/master/LICENSE.md |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace dukt\twitter\controllers; |
9
|
|
|
|
10
|
|
|
use Craft; |
11
|
|
|
use craft\helpers\Json; |
12
|
|
|
use craft\web\Controller; |
13
|
|
|
use dukt\twitter\errors\InvalidTweetException; |
14
|
|
|
use dukt\twitter\Plugin; |
15
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
16
|
|
|
use GuzzleHttp\Exception\RequestException; |
17
|
|
|
use yii\web\Response; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* API controller |
21
|
|
|
* |
22
|
|
|
* @author Dukt <[email protected]> |
23
|
|
|
* @since 3.0 |
24
|
|
|
*/ |
25
|
|
|
class FieldsController extends Controller |
26
|
|
|
{ |
27
|
|
|
// Public Methods |
28
|
|
|
// ========================================================================= |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Tweet field preview. |
32
|
|
|
* |
33
|
|
|
* @return Response |
34
|
|
|
* @throws GuzzleException |
35
|
|
|
* @throws \Twig_Error_Loader |
36
|
|
|
* @throws \yii\base\Exception |
37
|
|
|
*/ |
38
|
|
|
public function actionTweetFieldPreview(): Response |
39
|
|
|
{ |
40
|
|
|
$tweetId = Craft::$app->getRequest()->getParam('id'); |
41
|
|
|
|
42
|
|
|
try { |
43
|
|
|
$tweet = Plugin::getInstance()->getApi()->getTweet($tweetId); |
44
|
|
|
|
45
|
|
|
if (!$tweet) { |
46
|
|
|
throw new InvalidTweetException('No status found with that ID.'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$html = Craft::$app->getView()->renderTemplate('twitter/_components/tweet', [ |
50
|
|
|
'tweet' => $tweet |
51
|
|
|
]); |
52
|
|
|
|
53
|
|
|
return $this->asJson([ |
54
|
|
|
'html' => $html, |
55
|
|
|
]); |
56
|
|
|
} catch (RequestException $e) { |
57
|
|
|
$data = Json::decodeIfJson($e->getResponse()->getBody()->getContents()); |
58
|
|
|
|
59
|
|
|
if (isset($data['errors'][0]['message'])) { |
60
|
|
|
return $this->asErrorJson($data['errors'][0]['message']); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
Craft::error('Couldn’ load tweet preview: '.$e->getTraceAsString(), __METHOD__); |
64
|
|
|
|
65
|
|
|
return $this->asErrorJson($e->getMessage()); |
66
|
|
|
} catch (InvalidTweetException $e) { |
67
|
|
|
Craft::error('Couldn’ load tweet preview: '.$e->getTraceAsString(), __METHOD__); |
68
|
|
|
|
69
|
|
|
return $this->asErrorJson($e->getMessage()); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|