Skip to content

Commit

Permalink
Some code for Nextcloud "unread mail" widet #569
Browse files Browse the repository at this point in the history
  • Loading branch information
the-djmaze committed Nov 28, 2023
1 parent c101749 commit 3db4ff2
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 0 deletions.
4 changes: 4 additions & 0 deletions integrations/nextcloud/snappymail/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use OCA\SnappyMail\Util\SnappyMailHelper;
use OCA\SnappyMail\Controller\FetchController;
use OCA\SnappyMail\Controller\PageController;
use OCA\SnappyMail\Dashboard\UnreadMailWidget;
use OCA\SnappyMail\Search\Provider;

use OCP\AppFramework\App;
Expand Down Expand Up @@ -62,6 +63,9 @@ public function register(IRegistrationContext $context): void
);

$context->registerSearchProvider(Provider::class);

// TODO: Not working yet, needs a Vue UI
// $context->registerDashboardWidget(UnreadMailWidget::class);
}

public function boot(IBootContext $context): void
Expand Down
164 changes: 164 additions & 0 deletions integrations/nextcloud/snappymail/lib/Dashboard/UnreadMailWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?php

namespace OCA\SnappyMail\Dashboard;

use OCA\SnappyMail\Util\SnappyMailHelper;

use OCP\AppFramework\Services\IInitialState;
use OCP\Dashboard\IAPIWidget;
use OCP\Dashboard\IIconWidget;
use OCP\Dashboard\IOptionWidget;
use OCP\Dashboard\Model\WidgetItem;
use OCP\Dashboard\Model\WidgetOptions;
use OCP\IL10N;
use OCP\IURLGenerator;

class UnreadMailWidget implements IAPIWidget, IIconWidget/*, IOptionWidget*/
{
protected IL10N $l10n;
protected IURLGenerator $urlGenerator;
protected IInitialState $initialState;

public function __construct(IL10N $l10n, IURLGenerator $urlGenerator, IInitialState $initialState)
{
$this->l10n = $l10n;
$this->urlGenerator = $urlGenerator;
$this->initialState = $initialState;
}

// IWidget

/**
* @return string Unique id that identifies the widget, e.g. the app id
* @since 20.0.0
*/
public function getId(): string
{
return 'snappymail-unread';
}

/**
* @return string User facing title of the widget
* @since 20.0.0
*/
public function getTitle(): string
{
return $this->l10n->t('Unread mail');
}

/**
* @return int Initial order for widget sorting
* @since 20.0.0
*/
public function getOrder(): int
{
return 3;
}

/**
* @return string css class that displays an icon next to the widget title
* @since 20.0.0
*/
public function getIconClass(): string
{
return 'icon-mail';
}

/**
* @return string|null The absolute url to the apps own view
* @since 20.0.0
*/
public function getUrl(): ?string
{
return $this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute('snappymail.page.index'));
}

/**
* Execute widget bootstrap code like loading scripts and providing initial state
* @since 20.0.0
*/
public function load(): void
{
// SnappyMailHelper::loadApp();
}

// IAPIWidget

/**
* @return \OCP\Dashboard\Model\WidgetItem[] The widget items
* @since 22.0.0
*/
public function getItems(string $userId, ?string $since = null, int $limit = 7): array
{
$result = [];
SnappyMailHelper::startApp();
$oActions = \RainLoop\Api::Actions();
// $oAccount = $oActions->getMainAccountFromToken(false); // Issue: when account switched, wrong email is shown
$oAccount = $oActions->getAccountFromToken(false);
if ($oAccount) {
$oConfig = $oActions->Config();

$oParams = new \MailSo\Mail\MessageListParams;
$oParams->sFolderName = 'INBOX'; // or \All ?
$oParams->sSearch = 'unseen';
$oParams->oCacher = ($oConfig->Get('cache', 'enable', true) && $oConfig->Get('cache', 'server_uids', false))
? $oActions->Cacher($oAccount) : null;
$oParams->bUseSortIfSupported = !!$oConfig->Get('labs', 'use_imap_sort', true);
// $oParams->sSort = 'DATE DESC';
$oParams->iLimit = $iLimit;

$oMailClient = $oActions->MailClient();
if (!$oMailClient->IsLoggined()) {
$oAccount->ImapConnectAndLoginHelper($oActions->Plugins(), $oMailClient->ImapClient(), $oConfig);
}

// instanceof \MailSo\Mail\MessageCollection
$MessageCollection = $oMailClient->MessageList($oParams);

$baseURL = $this->urlGenerator->linkToRoute('snappymail.page.index') . '#';

foreach ($MessageCollection as $Message) {
$result[] = new WidgetItem(
// title
$Message->From()->ToString(),
// subtitle
$Message->Subject(),
// link
$baseURL . '/mailbox/INBOX/m' . $Message->Uid(),
// iconUrl use the avatar?
'',
// sinceId
$Message->ETag('')
);
}
}

return $result;
}

// IIconWidget

/**
* Get the absolute url for the widget icon
*
* @return string
* @since 25.0.0
*/
public function getIconUrl(): string
{
SnappyMailHelper::loadApp();
// return $this->urlGenerator->getAbsoluteURL(
return \RainLoop\Utils::WebStaticPath('images/snappymail-logo.png');
}

// IOptionWidget

/**
* Get additional options for the widget
* @since 25.0.0
*/
public function getWidgetOptions(): WidgetOptions
{
return new WidgetOptions(true);
}
}

0 comments on commit 3db4ff2

Please sign in to comment.