1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
ChatAvatar.php
См. документацию.
1<?php
2
4
11use CFile;
12
14{
15 protected Chat $chat;
16
17 public function __construct(Chat $chat)
18 {
19 $this->chat = $chat;
20 }
21
22 public function get(bool $addBlankPicture = false, bool $withDomain = false): string
23 {
24 $url = $addBlankPicture ? '/bitrix/js/im/images/blank.gif' : '';
25
26 if ($this->chat->getAvatarId() > 0)
27 {
28 $avatar = $this->getResizeAvatar();
29 if (!empty($avatar['src']))
30 {
31 $url = $avatar['src'];
32 }
33 }
34
35 if ($withDomain && $url)
36 {
38 }
39
40 return $url;
41 }
42
43 public function update(
44 int|string $avatar,
45 bool $withMessage = true,
46 bool $skipRecent = false,
47 bool $withoutSaveInChat = false
48 ): Result
49 {
50 if (!is_numeric($avatar))
51 {
52 $avatar = self::saveAvatarByString((string)$avatar) ?? 0;
53 }
54
55 return $this->updateById(
56 (int)$avatar,
57 $withMessage,
58 $skipRecent,
59 $withoutSaveInChat
60 );
61 }
62
63 protected function updateById(
64 int $avatarId,
65 bool $withMessage = true,
66 bool $skipRecent = false,
67 bool $withoutSaveInChat = false
68 ): Result
69 {
70 $result = new Result();
71
72 $oldAvatarId = $this->chat->getAvatarId();
73 $this->chat->setAvatarId($avatarId);
74
75 if (!$withoutSaveInChat)
76 {
77 $result = $this->chat->save();
78 if (!$result->isSuccess())
79 {
80 return $result;
81 }
82 }
83
84 if (isset($oldAvatarId) && $oldAvatarId > 0)
85 {
86 CFile::Delete($oldAvatarId);
87 }
88
89 $avatar = $this->getResizeAvatar();
90 if (
91 !empty($avatar['src'])
92 && Loader::includeModule("pull")
93 )
94 {
95 $this->sendPush($avatar);
96 }
97
98 if ($withMessage)
99 {
100 $this->chat->sendMessageUpdateAvatar($skipRecent);
101 }
102
103 return $result->setResult($avatarId);
104 }
105
106 protected function sendPush(array $avatar): void
107 {
108 $pushMessage = [
109 'module_id' => 'im',
110 'command' => 'chatAvatar',
111 'params' => [
112 'chatId' => $this->chat->getChatId(),
113 'avatar' => $avatar['src'],
114 ],
115 'extra' => Common::getPullExtra()
116 ];
117
118 Event::add($this->chat->getRelations()->getUserIds(), $pushMessage);
119 if ($this->chat->needToSendPublicPull())
120 {
121 \CPullWatch::AddToStack('IM_PUBLIC_' . $this->chat->getId(), $pushMessage);
122 }
123 }
124
125 protected function getResizeAvatar(): array
126 {
127 $avatar = \CFile::ResizeImageGet(
128 $this->chat->getAvatarId() ?? 0,
129 ['width' => 200, 'height' => 200],
131 false,
132 false,
133 true
134 );
135 if (empty($avatar))
136 {
137 return [];
138 }
139
140 return $avatar;
141 }
142
143 public static function saveAvatarByString(string $avatarBase64): ?int
144 {
145 $avatar = \CRestUtil::saveFile($avatarBase64);
146 $imageCheck = (new Image($avatar["tmp_name"]))->getInfo();
147
148 if (
149 !$imageCheck
150 || !$imageCheck->getWidth() || $imageCheck->getWidth() > 5000
151 || !$imageCheck->getHeight() || $imageCheck->getHeight() > 5000
152 )
153 {
154 return null;
155 }
156
157 if (!$avatar || mb_strpos($avatar['type'], "image/") !== 0)
158 {
159 return null;
160 }
161
162 $avatar = CFile::saveFile($avatar, 'im');
163
164 return is_numeric($avatar) && $avatar > 0 ? $avatar : null;
165 }
166}
Определения common.php:7
static getPullExtra()
Определения common.php:127
static getPublicDomain()
Определения common.php:8
update(int|string $avatar, bool $withMessage=true, bool $skipRecent=false, bool $withoutSaveInChat=false)
Определения ChatAvatar.php:43
static saveAvatarByString(string $avatarBase64)
Определения ChatAvatar.php:143
__construct(Chat $chat)
Определения ChatAvatar.php:17
sendPush(array $avatar)
Определения ChatAvatar.php:106
updateById(int $avatarId, bool $withMessage=true, bool $skipRecent=false, bool $withoutSaveInChat=false)
Определения ChatAvatar.php:63
Определения result.php:20
Определения event.php:5
Определения loader.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
const BX_RESIZE_IMAGE_EXACT
Определения constants.php:12
Определения Color.php:9
$url
Определения iframe.php:7