Skip to content

Commit e019f64

Browse files
committed
Populate defaults into exising profile config
Signed-off-by: Christopher Ng <chrng8@gmail.com>
1 parent f5bdf02 commit e019f64

1 file changed

Lines changed: 49 additions & 39 deletions

File tree

lib/private/Profile/ProfileManager.php

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -278,51 +278,61 @@ function (ILinkAction $action) use ($targetUser, $visitingUser) {
278278
}
279279

280280
/**
281-
* @inheritDoc
281+
* Return the default profile config
282+
*/
283+
private function getDefaultProfileConfig(IUser $targetUser, ?IUser $visitingUser): array {
284+
// Contruct the default config for actions
285+
$actionsConfig = [];
286+
foreach ($this->getActions($targetUser, $visitingUser) as $action) {
287+
$actionsConfig[$action->getId()] = [
288+
'displayId' => $action->getDisplayId(),
289+
'visibility' => ProfileConfig::DEFAULT_VISIBILITY,
290+
];
291+
}
292+
293+
// Map of account properties to display IDs
294+
$propertyDisplayMap = [
295+
IAccountManager::PROPERTY_ADDRESS => $this->l10nFactory->get('core')->t('Address'),
296+
IAccountManager::PROPERTY_AVATAR => $this->l10nFactory->get('core')->t('Avatar'),
297+
IAccountManager::PROPERTY_BIOGRAPHY => $this->l10nFactory->get('core')->t('About'),
298+
IAccountManager::PROPERTY_DISPLAYNAME => $this->l10nFactory->get('core')->t('Full name'),
299+
IAccountManager::PROPERTY_HEADLINE => $this->l10nFactory->get('core')->t('Headline'),
300+
IAccountManager::PROPERTY_ORGANISATION => $this->l10nFactory->get('core')->t('Organisation'),
301+
IAccountManager::PROPERTY_ROLE => $this->l10nFactory->get('core')->t('Role'),
302+
IAccountManager::PROPERTY_EMAIL => $this->l10nFactory->get('core')->t('Email'),
303+
IAccountManager::PROPERTY_PHONE => $this->l10nFactory->get('core')->t('Phone'),
304+
IAccountManager::PROPERTY_TWITTER => $this->l10nFactory->get('core')->t('Twitter'),
305+
IAccountManager::PROPERTY_WEBSITE => $this->l10nFactory->get('core')->t('Website'),
306+
];
307+
308+
// Contruct the default config for account properties
309+
$propertiesConfig = [];
310+
foreach ($propertyDisplayMap as $property => $displayId) {
311+
$propertiesConfig[$property] = [
312+
'displayId' => $displayId,
313+
'visibility' => ProfileConfig::DEFAULT_PROPERTY_VISIBILITY[$property] ?: ProfileConfig::DEFAULT_VISIBILITY,
314+
];
315+
}
316+
317+
return array_merge($actionsConfig, $propertiesConfig);
318+
}
319+
320+
/**
321+
* Return the profile config
282322
*/
283323
public function getProfileConfig(IUser $targetUser, ?IUser $visitingUser): array {
324+
$defaultProfileConfig = $this->getDefaultProfileConfig($targetUser, $visitingUser);
284325
try {
285-
$configArray = $this->configMapper->getArray($targetUser->getUID());
326+
$config = $this->configMapper->get($targetUser->getUID());
327+
// Merge defaults with the existing config in case the defaults are missing
328+
$config->setConfigArray(array_merge($defaultProfileConfig, $config->getConfigArray()));
329+
$this->configMapper->update($config);
330+
$configArray = $config->getConfigArray();
286331
} catch (DoesNotExistException $e) {
332+
// Create a new default config if it does not exist
287333
$config = new ProfileConfig();
288334
$config->setUserId($targetUser->getUID());
289-
290-
// Map of account properties to display IDs
291-
$propertyDisplayMap = [
292-
IAccountManager::PROPERTY_ADDRESS => $this->l10nFactory->get('core')->t('Address'),
293-
IAccountManager::PROPERTY_AVATAR => $this->l10nFactory->get('core')->t('Avatar'),
294-
IAccountManager::PROPERTY_BIOGRAPHY => $this->l10nFactory->get('core')->t('About'),
295-
IAccountManager::PROPERTY_DISPLAYNAME => $this->l10nFactory->get('core')->t('Full name'),
296-
IAccountManager::PROPERTY_HEADLINE => $this->l10nFactory->get('core')->t('Headline'),
297-
IAccountManager::PROPERTY_ORGANISATION => $this->l10nFactory->get('core')->t('Organisation'),
298-
IAccountManager::PROPERTY_ROLE => $this->l10nFactory->get('core')->t('Role'),
299-
IAccountManager::PROPERTY_EMAIL => $this->l10nFactory->get('core')->t('Email'),
300-
IAccountManager::PROPERTY_PHONE => $this->l10nFactory->get('core')->t('Phone'),
301-
IAccountManager::PROPERTY_TWITTER => $this->l10nFactory->get('core')->t('Twitter'),
302-
IAccountManager::PROPERTY_WEBSITE => $this->l10nFactory->get('core')->t('Website'),
303-
];
304-
305-
// Contruct the default config for account properties
306-
$propertiesConfig = [];
307-
foreach ($propertyDisplayMap as $property => $displayId) {
308-
$propertiesConfig[$property] = [
309-
'displayId' => $displayId,
310-
'visibility' => ProfileConfig::DEFAULT_PROPERTY_VISIBILITY[$property] ?: ProfileConfig::DEFAULT_VISIBILITY,
311-
];
312-
}
313-
314-
// Contruct the default config for actions
315-
$actionsConfig = [];
316-
/** @var ILinkAction $action */
317-
foreach ($this->getActions($targetUser, $visitingUser) as $action) {
318-
$actionsConfig[$action->getId()] = [
319-
'displayId' => $action->getDisplayId(),
320-
'visibility' => ProfileConfig::DEFAULT_VISIBILITY,
321-
];
322-
}
323-
324-
// Set the default config
325-
$config->setConfigArray(array_merge($propertiesConfig, $actionsConfig));
335+
$config->setConfigArray($defaultProfileConfig);
326336
$this->configMapper->insert($config);
327337
$configArray = $config->getConfigArray();
328338
}

0 commit comments

Comments
 (0)