forked from nextcloud/integration_slack
-
Notifications
You must be signed in to change notification settings - Fork 2
feat(dashboard): implement Zulip dashboard widget #126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OCA\Zulip\Dashboard; | ||
|
|
||
| use OCA\Zulip\AppInfo\Application; | ||
| use OCA\Zulip\Service\SecretService; | ||
| use OCA\Zulip\Service\ZulipAPIService; | ||
| use OCP\Dashboard\IButtonWidget; | ||
| use OCP\Dashboard\IIconWidget; | ||
| use OCP\Dashboard\IReloadableWidget; | ||
| use OCP\Dashboard\Model\WidgetButton; | ||
| use OCP\Dashboard\Model\WidgetItem; | ||
| use OCP\Dashboard\Model\WidgetItems; | ||
| use OCP\IConfig; | ||
| use OCP\IL10N; | ||
| use OCP\IURLGenerator; | ||
|
|
||
| class ZulipDashboardWidget implements IReloadableWidget, IButtonWidget, IIconWidget { | ||
|
|
||
| public function __construct( | ||
| private IL10N $l10n, | ||
| private IConfig $config, | ||
| private IURLGenerator $urlGenerator, | ||
| private SecretService $secretService, | ||
| private ZulipAPIService $zulipAPIService, | ||
| private ?string $userId, | ||
| ) { | ||
| } | ||
|
|
||
| public function getId(): string { | ||
| return 'zulip_feed'; | ||
| } | ||
|
|
||
| public function getTitle(): string { | ||
| return $this->l10n->t('Zulip'); | ||
| } | ||
|
|
||
| public function getOrder(): int { | ||
| return 10; | ||
| } | ||
|
|
||
| public function getIconClass(): string { | ||
| return ''; | ||
| } | ||
|
|
||
| public function getIconUrl(): string { | ||
| return $this->urlGenerator->getAbsoluteURL( | ||
| $this->urlGenerator->imagePath(Application::APP_ID, 'app-dark.svg') | ||
| ); | ||
| } | ||
|
|
||
| public function getUrl(): ?string { | ||
| $zulipUrl = rtrim($this->config->getUserValue($this->userId ?? '', Application::APP_ID, 'url', ''), '/'); | ||
| if ($zulipUrl !== '') { | ||
| return $zulipUrl . '/#feed'; | ||
| } | ||
| return $this->urlGenerator->linkToRouteAbsolute('settings.PersonalSettings.index', ['section' => 'connected-accounts']); | ||
| } | ||
|
|
||
| public function load(): void { | ||
| } | ||
|
|
||
| public function getItemsV2(string $userId, ?string $since = null, int $limit = 7): WidgetItems { | ||
| $url = $this->config->getUserValue($userId, Application::APP_ID, 'url', ''); | ||
| $email = $this->config->getUserValue($userId, Application::APP_ID, 'email', ''); | ||
| $apiKey = $this->secretService->getEncryptedUserValue($userId, 'api_key'); | ||
| $isConnected = ($url !== '' && $email !== '' && $apiKey !== ''); | ||
|
|
||
| if (!$isConnected) { | ||
| return new WidgetItems([], $this->l10n->t('Connect your Zulip account in the settings')); | ||
| } | ||
|
|
||
| $showUnread = $this->config->getUserValue($userId, Application::APP_ID, 'dashboard_show_unread', '0') === '1'; | ||
|
|
||
| if ($showUnread) { | ||
| $messages = $this->zulipAPIService->getUnreadMessages($userId, $limit); | ||
| $emptyMessage = $this->l10n->t('No unread messages'); | ||
| } else { | ||
| $messages = $this->zulipAPIService->getRecentMessages($userId, $limit); | ||
| $emptyMessage = $this->l10n->t('No recent messages'); | ||
| } | ||
|
|
||
| if (isset($messages['error'])) { | ||
| return new WidgetItems([], $this->l10n->t('Failed to load messages')); | ||
| } | ||
|
|
||
| $zulipUrl = rtrim($this->config->getUserValue($userId, Application::APP_ID, 'url', ''), '/'); | ||
|
|
||
| $items = array_map(function (array $msg) use ($zulipUrl): WidgetItem { | ||
| return new WidgetItem( | ||
| $msg['content'] ?? '', | ||
| $this->_buildSubtitle($msg), | ||
| $this->_buildMessageUrl($msg, $zulipUrl), | ||
| $this->urlGenerator->linkToRouteAbsolute( | ||
| 'integration_zulip.ZulipAPI.getUserAvatar', | ||
| ['zulipUserId' => $msg['sender_id'] ?? 0] | ||
| ), | ||
| (string)($msg['id'] ?? ''), | ||
| ); | ||
| }, $messages); | ||
|
|
||
| return new WidgetItems($items, $emptyMessage); | ||
| } | ||
|
|
||
| public function getReloadInterval(): int { | ||
| return 60; | ||
| } | ||
|
|
||
| public function getWidgetButtons(string $userId): array { | ||
| $zulipUrl = rtrim($this->config->getUserValue($userId, Application::APP_ID, 'url', ''), '/'); | ||
| if ($zulipUrl === '') { | ||
| return []; | ||
| } | ||
| $showUnread = $this->config->getUserValue($userId, Application::APP_ID, 'dashboard_show_unread', '0') === '1'; | ||
| if ($showUnread) { | ||
| return [ | ||
| new WidgetButton( | ||
| WidgetButton::TYPE_MORE, | ||
| $zulipUrl . '/#inbox', | ||
| $this->l10n->t('Open Zulip inbox'), | ||
| ), | ||
| ]; | ||
| } | ||
| return [ | ||
| new WidgetButton( | ||
| WidgetButton::TYPE_MORE, | ||
| $zulipUrl . '/#feed', | ||
| $this->l10n->t('Open Zulip feed'), | ||
| ), | ||
| ]; | ||
| } | ||
|
|
||
| private function _buildSubtitle(array $msg): string { | ||
| if (($msg['type'] ?? '') === 'stream') { | ||
| $subject = $msg['subject'] ?? ''; | ||
| $isDefaultTopic = $subject === '' || strtolower($subject) === 'general chat'; | ||
| return '#' . ($msg['display_recipient'] ?? '') . ($isDefaultTopic ? '' : ' › ' . $subject); | ||
| } | ||
| return $this->l10n->t('Direct message from {name}', ['name' => $msg['sender_full_name'] ?? '']); | ||
| } | ||
|
|
||
| private function _buildMessageUrl(array $msg, string $zulipUrl): string { | ||
| if ($zulipUrl === '') { | ||
| return ''; | ||
| } | ||
| if (($msg['type'] ?? '') === 'private') { | ||
| $recipients = is_array($msg['display_recipient'] ?? null) ? $msg['display_recipient'] : []; | ||
| $userIds = implode(',', array_column($recipients, 'id')); | ||
| return $zulipUrl . '/#narrow/dm/' . $userIds . '/near/' . ($msg['id'] ?? ''); | ||
| } | ||
| $subject = $msg['subject'] ?? ''; | ||
| $isDefaultTopic = $subject === '' || strtolower($subject) === 'general chat'; | ||
| $base = $zulipUrl . '/#narrow/channel/' . ($msg['stream_id'] ?? ''); | ||
| if ($isDefaultTopic) { | ||
| return $base . '/near/' . ($msg['id'] ?? ''); | ||
| } | ||
| return $base . '/topic/' . str_replace('%', '.', rawurlencode($subject)) . '/with/' . ($msg['id'] ?? ''); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.