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