mattermost-mobile/app/screens/edit_profile/index.ts
Guillermo Vayá db569fe2c3
[MM-62565] Custom Profile attributes websocket changes (#8758)
* user_info websocket listening

* add to edit profile

* add tests

* fix user_info test

* improved types

* add websocket tests

* fix tests to adapt to the database

* temp

* fix test, remove unneeded file

* Update app/actions/remote/custom_profile.ts

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* fix test and storing fields and attributes

* consistency

* remove await from loop

* remove db from component

* simplify functions

* address reviews

* adress review comments

* fix promises not being fulfilled before ordering

* fix tests

* address comments

* address review comments

* test_helper

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-05-27 16:24:40 +02:00

74 lines
3.8 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {withDatabase, withObservables} from '@nozbe/watermelondb/react';
import {of as of$, combineLatest} from 'rxjs';
import {switchMap} from 'rxjs/operators';
import {observeCustomProfileAttributesByUserId, observeCustomProfileFields} from '@queries/servers/custom_profile';
import {observeConfigBooleanValue} from '@queries/servers/system';
import {observeCurrentUser} from '@queries/servers/user';
import {sortCustomProfileAttributes, convertToAttributesMap, convertProfileAttributesToCustomAttributes} from '@utils/user';
import EditProfile from './edit_profile';
import type {CustomAttribute} from '@typings/api/custom_profile_attributes';
import type {WithDatabaseArgs} from '@typings/database/database';
const enhanced = withObservables([], ({database}: WithDatabaseArgs) => {
const currentUser = observeCurrentUser(database);
const ldapFirstNameAttributeSet = observeConfigBooleanValue(database, 'LdapFirstNameAttributeSet');
const ldapLastNameAttributeSet = observeConfigBooleanValue(database, 'LdapLastNameAttributeSet');
const ldapNicknameAttributeSet = observeConfigBooleanValue(database, 'LdapNicknameAttributeSet');
const ldapPositionAttributeSet = observeConfigBooleanValue(database, 'LdapPositionAttributeSet');
const samlFirstNameAttributeSet = observeConfigBooleanValue(database, 'SamlFirstNameAttributeSet');
const samlLastNameAttributeSet = observeConfigBooleanValue(database, 'SamlLastNameAttributeSet');
const samlNicknameAttributeSet = observeConfigBooleanValue(database, 'SamlNicknameAttributeSet');
const samlPositionAttributeSet = observeConfigBooleanValue(database, 'SamlPositionAttributeSet');
const customAttributesEnabled = observeConfigBooleanValue(database, 'FeatureFlagCustomProfileAttributes');
const rawCustomAttributes = currentUser.pipe(
switchMap((u) => (u ? observeCustomProfileAttributesByUserId(database, u.id) : of$([]))),
);
let formattedCustomAttributes;
let customFields;
if (customAttributesEnabled) {
customFields = observeCustomProfileFields(database);
// Convert attributes to the format expected by the component
formattedCustomAttributes = combineLatest([rawCustomAttributes, customFields]).pipe(
switchMap(([attributes, fields]) => {
if (!attributes?.length) {
return of$([] as CustomAttribute[]);
}
return of$(convertProfileAttributesToCustomAttributes(attributes, fields, sortCustomProfileAttributes));
}),
switchMap((converted) => of$(convertToAttributesMap(converted))),
);
}
return {
currentUser,
lockedFirstName: combineLatest([ldapFirstNameAttributeSet, samlFirstNameAttributeSet]).pipe(
switchMap(([ldap, saml]) => of$(ldap || saml)),
),
lockedLastName: combineLatest([ldapLastNameAttributeSet, samlLastNameAttributeSet]).pipe(
switchMap(([ldap, saml]) => of$(ldap || saml)),
),
lockedNickname: combineLatest([ldapNicknameAttributeSet, samlNicknameAttributeSet]).pipe(
switchMap(([ldap, saml]) => of$(ldap || saml)),
),
lockedPosition: combineLatest([ldapPositionAttributeSet, samlPositionAttributeSet]).pipe(
switchMap(([ldap, saml]) => of$(ldap || saml)),
),
lockedPicture: observeConfigBooleanValue(database, 'LdapPictureAttributeSet'),
enableCustomAttributes: observeConfigBooleanValue(database, 'FeatureFlagCustomProfileAttributes'),
userCustomAttributes: rawCustomAttributes,
customFields,
customAttributesSet: formattedCustomAttributes,
};
});
export default withDatabase(enhanced(EditProfile));