Sample of NextCloud Contacts Suggestions for #96

This commit is contained in:
the-djmaze 2022-10-10 17:45:48 +02:00
parent 8ae5563a84
commit c3a95db495
4 changed files with 142 additions and 0 deletions

20
plugins/nextcloud/LICENSE Normal file
View file

@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright (c) 2022 SnappyMail
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View file

@ -0,0 +1,74 @@
<?php
class NextcloudContactsSuggestions implements \RainLoop\Providers\Suggestions\ISuggestions
{
/**
* @var \MailSo\Log\Logger
*/
private $oLogger = null;
public function Process(\RainLoop\Model\Account $oAccount, string $sQuery, int $iLimit = 20): array
{
try
{
$sQuery = \trim($sQuery);
if ('' === $sQuery || !NextcloudPlugin::IsIntegrated()) {
return [];
}
$cm = \OC::$server->getContactsManager();
if (!$cm || !$cm->isEnabled()) {
return [];
}
$aSearchResult = $cm->search($sQuery, array('FN', 'NICKNAME', 'TITLE', 'EMAIL'));
//$this->oLogger->WriteDump($aSearchResult);
if (\is_array($aSearchResult) && 0 < \count($aSearchResult)) {
$iInputLimit = $iLimit;
$aResult = array();
foreach ($aSearchResult as $aContact) {
if (0 >= $iLimit) {
break;
}
if (!empty($aContact['UID'])) {
$sUid = $aContact['UID'];
$mEmails = isset($aContact['EMAIL']) ? $aContact['EMAIL'] : '';
$sFullName = isset($aContact['FN']) ? \trim($aContact['FN']) : '';
if (empty($sFullName) && isset($aContact['NICKNAME'])) {
$sFullName = \trim($aContact['NICKNAME']);
}
if (!\is_array($mEmails)) {
$mEmails = array($mEmails);
}
foreach ($mEmails as $sEmail) {
$sHash = $sFullName.'|'.$sEmail;
if (!isset($aResult[$sHash])) {
$aResult[$sHash] = array($sEmail, $sFullName);
--$iLimit;
}
}
}
}
return \array_slice(\array_values($aResult), 0, $iInputLimit);
}
}
catch (\Exception $oException)
{
if ($this->oLogger) {
$this->oLogger->WriteException($oException);
}
}
return [];
}
public function SetLogger(\MailSo\Log\Logger $oLogger)
{
$this->oLogger = $oLogger;
}
}

View file

@ -0,0 +1,47 @@
<?php
class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
{
const
NAME = 'Nextcloud',
VERSION = '1.0',
RELEASE = '2022-10-10',
CATEGORY = 'Integrations',
DESCRIPTION = 'Integrate with Nextcloud',
REQUIRED = '2.15.2';
public function Init() : void
{
$this->addHook('main.fabrica', 'MainFabrica');
}
public function Supported() : string
{
return static::IsIntegrated() ? '' : 'Not running inside Nextcloud';
}
public static function IsIntegrated()
{
// return !empty($_ENV['RAINLOOP_OWNCLOUD']) &&
return \class_exists('OC') && isset(\OC::$server);
}
public static function IsLoggedIn()
{
return static::IsIntegrated() && \OC::$server->getUserSession()->isLoggedIn();
}
/**
* @param mixed $mResult
*/
public function MainFabrica(string $sName, &$mResult)
{
if ('suggestions' === $sName) {
if (!\is_array($mResult)) {
$mResult = array();
}
include_once __DIR__ . '/NextcloudContactsSuggestions.php';
$mResult[] = new NextcloudContactsSuggestions();
}
}
}