Skip to content

Commit

Permalink
When a user is updated, and now they have been removed from a group, …
Browse files Browse the repository at this point in the history
…they need to be deleted from the index... but the user record does not have a mechanism for showing what the old group(s) it was part of.. so, if the user record is not part of any synced group, we must trigger a delete of the record from any synced index the might have been included in
  • Loading branch information
markmiddleton committed Apr 29, 2024
1 parent fda291e commit 982d150
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion src/services/AlgoliaSyncService.php
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,50 @@ public function sanitizeFieldName($fieldName) {
return str_replace(' ', '_', $fieldName);
}

public function getSyncedMemberGroups() {
// this is used when deleting a user record
// as Craft doesn't give us what groups they "used to be" in
// when a record gets updated. If they are not part of any group,
// we need to actively delete them from any member group that is being synced

$env = $this->getEnvironment();

// user groups list
$userGroups = Craft::$app->userGroups->getAllGroups();
$userGroupsConfig = [];
foreach ($userGroups AS $group) {
$userGroupsConfig[$group->id] = $env.'_user_'.$group->handle;
}

$syncedGroups = [];

$algoliaSettings = AlgoliaSync::$plugin->getSettings();

if (isset($algoliaSettings->algoliaElements['user'])) {
$syncedGroupsArray = $algoliaSettings->algoliaElements['user'];
if (count($syncedGroupsArray) > 0) {
foreach ($syncedGroupsArray AS $groupId => $groupData) {
if (!empty($groupData['sync'])) {
// does this have a custom index name?
$potentialIndexOverride = $groupData['customIndex'];

if (!empty($potentialIndexOverride)) {
// is that name an env variable?
$syncedGroups[] = App::parseEnv($potentialIndexOverride);
}
else {
// otherwise, use the default convention
if (isset($userGroupsConfig[$groupId])) {
$syncedGroups[] = $userGroupsConfig[$groupId];
}
}
}
}
}
}
return $syncedGroups;
}

public function getEventElementInfo($element, $processRecords = true) {

$elementTypeSlugArray = explode("\\", get_class($element));
Expand Down Expand Up @@ -785,8 +829,9 @@ public function getEventElementInfo($element, $processRecords = true) {
// this is where we send a quick message to Algolia to purge out their record

if ($deleteFromAlgolia && $processRecords) {

$elementData = [];
$elementData['index'] = AlgoliaSync::$plugin->algoliaSyncService->getAlgoliaIndex($element);
$elementData['index'] = AlgoliaSync::$plugin->algoliaSyncService->getSyncedMemberGroups();
$elementData['attributes'] = [];
$elementData['attributes']['objectID'] = $element->id;

Expand Down

0 comments on commit 982d150

Please sign in to comment.