|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Phosphorum\Discord; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use Phalcon\Di\Injectable; |
| 7 | +use Phalcon\Http\Client\Provider\Curl; |
| 8 | +use Phalcon\Http\Client\Request; |
| 9 | +use Phosphorum\Model\Posts; |
| 10 | +use Phosphorum\Model\PostsReplies; |
| 11 | +use Phosphorum\Model\Services\Service\Posts as PostsService; |
| 12 | + |
| 13 | +/** |
| 14 | + * Class DiscordComponent |
| 15 | + * |
| 16 | + * @property \Phalcon\Queue\Beanstalk $queue |
| 17 | + * @package Phosphorum\Discord |
| 18 | + */ |
| 19 | +class DiscordComponent extends Injectable |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var array |
| 23 | + */ |
| 24 | + protected $options; |
| 25 | + |
| 26 | + /** |
| 27 | + * Creates component |
| 28 | + * |
| 29 | + * @param $options |
| 30 | + */ |
| 31 | + public function __construct($options) |
| 32 | + { |
| 33 | + $this->options = $options; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @return mixed |
| 38 | + */ |
| 39 | + public function getToken() |
| 40 | + { |
| 41 | + return $this->options['token_bot']; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @return mixed |
| 46 | + */ |
| 47 | + public function getChannelId() |
| 48 | + { |
| 49 | + return $this->options['channel_id']; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @return mixed |
| 54 | + */ |
| 55 | + public function getGuildId() |
| 56 | + { |
| 57 | + return $this->options['guild_id']; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @return bool |
| 62 | + */ |
| 63 | + public function isMessagingAboutNewDiscussion() |
| 64 | + { |
| 65 | + return $this->options['message']['new_discussions'] == true; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @return bool |
| 70 | + */ |
| 71 | + public function isMessagingAboutReplies() |
| 72 | + { |
| 73 | + return $this->options['message']['new_replies'] == true; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @return bool |
| 78 | + */ |
| 79 | + public function isMessagingAboutSolvedDiscussion() |
| 80 | + { |
| 81 | + return $this->options['message']['solved_discussions'] == true; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @param Posts $posts |
| 86 | + */ |
| 87 | + public function addMessageAboutDiscussion(Posts $posts) |
| 88 | + { |
| 89 | + if ($this->isMessagingAboutNewDiscussion()) { |
| 90 | + /** @var PostsService $postService */ |
| 91 | + $postService = container(PostsService::class); |
| 92 | + |
| 93 | + $href = $postService->getPostUrl($posts); |
| 94 | + $message = sprintf( |
| 95 | + ":newspaper2: **New discussion `%s` was started by `%s`. Check it out on %s.**", |
| 96 | + $posts->title, |
| 97 | + $posts->user->name, |
| 98 | + $href |
| 99 | + ); |
| 100 | + $embed = [ |
| 101 | + 'title' => $posts->title, |
| 102 | + 'description' => $this->makeDescription($posts->content, 375), |
| 103 | + 'url' => $href, |
| 104 | + 'timestamp' => $posts->getUTCModifiedAt(), |
| 105 | + 'type' => 'rich', |
| 106 | + 'author' => [ |
| 107 | + 'name' => $posts->user->name, |
| 108 | + 'url' => container('config')->site->url . "/user/{$posts->users_id}/{$posts->user->login}", |
| 109 | + 'icon_url' => $this->gravatar->setSize(220)->getAvatar($posts->user->email), |
| 110 | + ], |
| 111 | + ]; |
| 112 | + $this->queue->choose('discord'); |
| 113 | + $this->queue->put( |
| 114 | + [ |
| 115 | + 'message' => $message, |
| 116 | + 'embed' => $embed, |
| 117 | + ] |
| 118 | + ); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @param PostsReplies $reply |
| 124 | + */ |
| 125 | + public function addMessageAboutReply(PostsReplies $reply) |
| 126 | + { |
| 127 | + if ($this->isMessagingAboutReplies()) { |
| 128 | + $postService = container(PostsService::class); |
| 129 | + |
| 130 | + $href = $postService->getPostUrl($reply->post); |
| 131 | + $message = sprintf( |
| 132 | + "\":newspaper2: **`%s` has commented in discussion `%s`. Check it out on %s#C%s.**", |
| 133 | + $reply->user->name, |
| 134 | + $reply->post->title, |
| 135 | + $href, |
| 136 | + $reply->id |
| 137 | + ); |
| 138 | + $embed = [ |
| 139 | + 'title' => $reply->post->title, |
| 140 | + 'description' => $this->makeDescription($reply->content, 375), |
| 141 | + 'url' => "$href#C{$reply->id}", |
| 142 | + 'timestamp' => $reply->getUTCCreatedAt(), |
| 143 | + 'type' => 'rich', |
| 144 | + 'author' => [ |
| 145 | + 'name' => $reply->user->name, |
| 146 | + 'url' => container('config')->site->url . "/user/{$reply->users_id}/{$reply->user->login}", |
| 147 | + 'icon_url' => $this->gravatar->setSize(220)->getAvatar($reply->user->email), |
| 148 | + ], |
| 149 | + ]; |
| 150 | + $this->queue->choose('discord'); |
| 151 | + $this->queue->put( |
| 152 | + [ |
| 153 | + 'message' => $message, |
| 154 | + 'embed' => $embed, |
| 155 | + ] |
| 156 | + ); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * @param PostsReplies $reply |
| 162 | + */ |
| 163 | + public function addMessageAboutSolvedDiscussion(PostsReplies $reply) |
| 164 | + { |
| 165 | + if ($this->isMessagingAboutSolvedDiscussion()) { |
| 166 | + $postService = container(PostsService::class); |
| 167 | + |
| 168 | + $href = $postService->getPostUrl($reply->post); |
| 169 | + $message = sprintf( |
| 170 | + ":newspaper2: **Discussion `%s` was marked as solved. Check out accepted answer on %s#C%s.**", |
| 171 | + $reply->post->title, |
| 172 | + $href, |
| 173 | + $reply->id |
| 174 | + ); |
| 175 | + $embed = [ |
| 176 | + 'title' => $reply->post->title, |
| 177 | + 'description' => $this->makeDescription($reply->content, 375), |
| 178 | + 'url' => "$href#C{$reply->id}", |
| 179 | + 'timestamp' => $reply->getUTCCreatedAt(), |
| 180 | + 'type' => 'rich', |
| 181 | + 'author' => [ |
| 182 | + 'name' => $reply->user->name, |
| 183 | + 'url' => container('config')->site->url . "/user/{$reply->users_id}/{$reply->user->login}", |
| 184 | + 'icon_url' => $this->gravatar->setSize(220)->getAvatar($reply->user->email), |
| 185 | + ], |
| 186 | + ]; |
| 187 | + $this->queue->choose('discord'); |
| 188 | + $this->queue->put( |
| 189 | + [ |
| 190 | + 'message' => $message, |
| 191 | + 'embed' => $embed, |
| 192 | + ] |
| 193 | + ); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * @param $description |
| 199 | + * @param $toLength |
| 200 | + * @return mixed|string |
| 201 | + */ |
| 202 | + protected function makeDescription($description, $toLength) |
| 203 | + { |
| 204 | + $description = str_replace(["\n", "\r", "\t"], ' ', $description); |
| 205 | + $description = trim($description); |
| 206 | + $padding = substr($description, $toLength); |
| 207 | + if ($padding === 0) { |
| 208 | + return $description; |
| 209 | + } |
| 210 | + $lengthDot = strpos($padding, "."); |
| 211 | + if ($lengthDot === 0) { |
| 212 | + return $description; |
| 213 | + } |
| 214 | + $lengthSpace = strpos($padding, " "); |
| 215 | + if ($lengthSpace === 0) { |
| 216 | + return $description; |
| 217 | + } |
| 218 | + |
| 219 | + return substr($description, 0, min($lengthDot + $toLength + 1, $lengthSpace + $toLength)) . "..."; |
| 220 | + } |
| 221 | +} |
0 commit comments