forked from RainLoop/rainloop-webmail
-
-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sample of NextCloud Contacts Suggestions for #96
- Loading branch information
the-djmaze
committed
Oct 10, 2022
1 parent
8ae5563
commit c3a95db
Showing
4 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,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. |
This file contains 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,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; | ||
} | ||
} |
This file contains 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,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(); | ||
} | ||
} | ||
} |
This file contains 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