Skip to content

Commit

Permalink
Remove the display name of the user from model
Browse files Browse the repository at this point in the history
  • Loading branch information
threema-andre committed Aug 20, 2024
1 parent 6ad9367 commit 3dd2fe3
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 35 deletions.
9 changes: 7 additions & 2 deletions src/common/model/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export function getDisplayName(
userState: GroupUserState,
creator: LocalModelStore<Contact> | 'me',
groupMembers: Set<LocalModelStore<Contact>>,
services: Pick<ServicesForModel, 'model'>,
services: Pick<ServicesForModel, 'device' | 'model'>,
): string {
if (groupName !== '') {
return groupName;
Expand All @@ -133,8 +133,13 @@ export function getDisplayName(
if (creator !== 'me') {
memberNames.unshift(creator.get().view.displayName);
}

// TODO(DESK-1570) Move this to correctly display the user with the 'Me' string.
if (userState === GroupUserState.MEMBER) {
memberNames.push(services.model.user.displayName.get());
memberNames.push(
services.model.user.profileSettings.get().view.nickname ??
services.device.identity.string,
);
}
return memberNames.join(', ');
}
Expand Down
8 changes: 0 additions & 8 deletions src/common/model/types/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,4 @@ export type User = {

/** Profile picture color and image (derived from {@link User.profileSettings}). */
readonly profilePicture: LocalStore<ProfilePictureView>;

/**
* Display name of the user (i.e. Nickname with fallback to the Threema ID, derived from
* {@link User.profileSettings} and {@link User.identity}).
*
* @deprecated TODO(DESK-1421): Move into viewmodel?
*/
readonly displayName: LocalStore<string>;
} & ProxyMarked;
8 changes: 0 additions & 8 deletions src/common/model/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export class UserModel implements User {
public readonly mediaSettings: LocalModelStore<MediaSettings>;

public readonly profilePicture: LocalStore<ProfilePictureView>;
public readonly displayName: LocalStore<string>;

public constructor(services: ServicesForModel) {
// TODO(DESK-1468): Redundant. Consider removing this.
Expand All @@ -67,12 +66,5 @@ export class UserModel implements User {
picture: profileSettingsModel.view.profilePicture,
}),
);

// TODO(DESK-1421): Move into viewmodel?
this.displayName = derive(
[this.profileSettings],
([{currentValue: profileSettingsModel}]) =>
profileSettingsModel.view.nickname ?? this.identity,
);
}
}
2 changes: 1 addition & 1 deletion src/common/viewmodel/contact/detail/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface ContactDetailViewModelBundle<TReceiver extends AnyReceiver>
}

export function getContactDetailViewModelBundle<TReceiver extends AnyReceiver>(
services: Pick<ServicesForViewModel, 'endpoint' | 'logging' | 'model'>,
services: Pick<ServicesForViewModel, 'device' | 'endpoint' | 'logging' | 'model'>,
receiverModelStore: ReceiverStoreFor<TReceiver>,
): ContactDetailViewModelBundle<TReceiver> {
const {endpoint} = services;
Expand Down
2 changes: 1 addition & 1 deletion src/common/viewmodel/contact/detail/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type ContactDetailViewModelStore<TReceiver extends AnyReceiver> = LocalSt
>;

export function getContactDetailViewModelStore<TReceiver extends AnyReceiver>(
services: Pick<ServicesForViewModel, 'endpoint' | 'logging' | 'model'>,
services: Pick<ServicesForViewModel, 'device' | 'endpoint' | 'logging' | 'model'>,
receiverModelStore: ReceiverStoreFor<TReceiver>,
): ContactDetailViewModelStore<TReceiver> {
const {endpoint} = services;
Expand Down
2 changes: 1 addition & 1 deletion src/common/viewmodel/contact/list/item/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface ContactListItemViewModelBundle<TReceiver extends AnyReceiver>
}

export function getContactListItemViewModelBundle<TReceiver extends AnyReceiver>(
services: Pick<ServicesForViewModel, 'endpoint' | 'logging' | 'model'>,
services: Pick<ServicesForViewModel, 'device' | 'endpoint' | 'logging' | 'model'>,
receiverModelStore: ReceiverStoreFor<TReceiver>,
): ContactListItemViewModelBundle<TReceiver> {
const {endpoint} = services;
Expand Down
2 changes: 1 addition & 1 deletion src/common/viewmodel/contact/list/item/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type ContactListItemViewModelStore<TReceiver extends AnyReceiver> = Local
>;

export function getContactListItemViewModelStore<TReceiver extends AnyReceiver>(
services: Pick<ServicesForViewModel, 'endpoint' | 'logging' | 'model'>,
services: Pick<ServicesForViewModel, 'device' | 'endpoint' | 'logging' | 'model'>,
receiverModelStore: ReceiverStoreFor<TReceiver>,
): ContactListItemViewModelStore<TReceiver> {
const {endpoint} = services;
Expand Down
6 changes: 5 additions & 1 deletion src/common/viewmodel/conversation/main/message/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {ConversationRegularMessageViewModelBundle} from '~/common/viewmodel
import type {ConversationStatusMessageViewModelBundle} from '~/common/viewmodel/conversation/main/message/status-message';
import type {ContactReceiverData} from '~/common/viewmodel/utils/receiver';
import type {SenderDataSelf} from '~/common/viewmodel/utils/sender';
import {getUserDisplayName} from '~/common/viewmodel/utils/user';

/**
* Union of all possible MessageViewModelBundle types.
Expand Down Expand Up @@ -80,7 +81,10 @@ export function getMessageSenderData(

case MessageDirection.OUTBOUND: {
const profilePicture = getAndSubscribe(services.model.user.profilePicture);
const displayName = getAndSubscribe(services.model.user.displayName);
const displayName = getUserDisplayName(
services,
getAndSubscribe(services.model.user.profileSettings).view.nickname,
);

return {
type: 'self',
Expand Down
2 changes: 1 addition & 1 deletion src/common/viewmodel/group-call/activity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface OngoingGroupCallViewModelBundle extends PropertiesMarked {
}

export function getOngoingGroupCallViewModelBundle(
services: Pick<ServicesForViewModel, 'endpoint' | 'model'>,
services: Pick<ServicesForViewModel, 'device' | 'endpoint' | 'model'>,
ongoing: OngoingGroupCall,
): OngoingGroupCallViewModelBundle {
const controller = ongoing.get().controller;
Expand Down
2 changes: 1 addition & 1 deletion src/common/viewmodel/search/nav/store/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export function getMessageSearchResults(
* {@link searchViewModelController}.
*/
export function getReceiverSearchResults(
services: Pick<ServicesForViewModel, 'endpoint' | 'logging' | 'model'>,
services: Pick<ServicesForViewModel, 'device' | 'endpoint' | 'logging' | 'model'>,
searchParams: SearchParams,
getAndSubscribe: GetAndSubscribeFunction,
): SearchViewModel['receiverSearchResults'] {
Expand Down
6 changes: 3 additions & 3 deletions src/common/viewmodel/utils/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface GroupCallData {
}

export function getGroupCallParticipantReceiverData(
services: Pick<ServicesForViewModel, 'model'>,
services: Pick<ServicesForViewModel, 'device' | 'model'>,
participant: LocalModelStore<Contact> | 'me',
getAndSubscribe: GetAndSubscribeFunction,
): GroupCallParticipantReceiverData {
Expand All @@ -37,7 +37,7 @@ export function getGroupCallParticipantReceiverData(

/** Collect group call data from a _chosen_ group call. */
function getGroupCallData(
services: Pick<ServicesForViewModel, 'model'>,
services: Pick<ServicesForViewModel, 'device' | 'model'>,
call: ChosenGroupCall,
getAndSubscribe: GetAndSubscribeFunction,
): GroupCallData {
Expand Down Expand Up @@ -76,7 +76,7 @@ function getGroupCallData(

/** Collect any call data from a receiver. */
export function getCallData(
services: Pick<ServicesForViewModel, 'model'>,
services: Pick<ServicesForViewModel, 'device' | 'model'>,
receiver: AnyReceiver,
getAndSubscribe: GetAndSubscribeFunction,
): AnyCallData | undefined {
Expand Down
6 changes: 5 additions & 1 deletion src/common/viewmodel/utils/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {IdentityString} from '~/common/network/types';
import {unreachable} from '~/common/utils/assert';
import type {GetAndSubscribeFunction} from '~/common/utils/store/derived-store';
import type {ReceiverBadgeType} from '~/common/viewmodel/types';
import {getUserDisplayName} from '~/common/viewmodel/utils/user';

/** A contact that has been removed from the contact list of the user. */
export interface RemovedContactData {
Expand All @@ -30,7 +31,10 @@ export function getContactDisplayName(
getAndSubscribe: GetAndSubscribeFunction,
): string {
if (identity === services.device.identity.string) {
return getAndSubscribe(services.model.user.displayName);
return getUserDisplayName(
services,
getAndSubscribe(services.model.user.profileSettings).view.nickname,
);
}
const contact = services.model.contacts.getByIdentity(identity);
if (contact === undefined) {
Expand Down
16 changes: 10 additions & 6 deletions src/common/viewmodel/utils/receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import type {GetAndSubscribeFunction} from '~/common/utils/store/derived-store';
import type {ServicesForViewModel} from '~/common/viewmodel';
import type {ReceiverBadgeType} from '~/common/viewmodel/types';
import {getContactBadge} from '~/common/viewmodel/utils/contact';
import {getUserDisplayName} from '~/common/viewmodel/utils/user';

export type AnyReceiverData =
| ContactReceiverData
Expand Down Expand Up @@ -64,7 +65,7 @@ export type ReceiverUpdateDataFor<TReceiver extends AnyReceiver> = {
* make sure that the controller is active.
*/
export function getConversationReceiverData(
services: Pick<ServicesForViewModel, 'model'>,
services: Pick<ServicesForViewModel, 'device' | 'model'>,
conversationModel: Conversation,
getAndSubscribe: GetAndSubscribeFunction,
): AnyReceiverData {
Expand All @@ -79,7 +80,7 @@ export function getConversationReceiverData(
* Collects the respective receiver data for the given `receiverModel`.
*/
export function getReceiverData<TReceiver extends AnyReceiver>(
services: Pick<ServicesForViewModel, 'model'>,
services: Pick<ServicesForViewModel, 'device' | 'model'>,
receiverModel: TReceiver,
getAndSubscribe: GetAndSubscribeFunction,
): ReceiverDataFor<TReceiver> {
Expand Down Expand Up @@ -156,13 +157,16 @@ export function getCommonReceiverData(receiverModel: AnyReceiver): CommonReceive
* Returns the collected {@link SelfReceiverData} object for the user themself.
*/
export function getSelfReceiverData(
services: Pick<ServicesForViewModel, 'model'>,
services: Pick<ServicesForViewModel, 'device' | 'model'>,
getAndSubscribe: GetAndSubscribeFunction,
): SelfReceiverData {
const {user} = services.model;

const profilePicture = getAndSubscribe(user.profilePicture);
const displayName = getAndSubscribe(user.displayName);
const displayName = getUserDisplayName(
services,
getAndSubscribe(user.profileSettings).view.nickname,
);
const profileSettings = getAndSubscribe(user.profileSettings);

return {
Expand Down Expand Up @@ -214,7 +218,7 @@ export function getContactReceiverData(
* {@link ReceiverType.GROUP}.
*/
function getGroupReceiverData(
services: Pick<ServicesForViewModel, 'model'>,
services: Pick<ServicesForViewModel, 'device' | 'model'>,
groupModel: Group,
getAndSubscribe: GetAndSubscribeFunction,
): GroupReceiverData {
Expand Down Expand Up @@ -422,7 +426,7 @@ function isGroupReceiverLeft(receiverModel: Group): boolean {
* Returns the {@link ContactReceiverData} of a group's creator identity.
*/
function getGroupCreatorData(
services: Pick<ServicesForViewModel, 'model'>,
services: Pick<ServicesForViewModel, 'device' | 'model'>,
groupModel: Group,
getAndSubscribe: GetAndSubscribeFunction,
): SelfReceiverData | ContactReceiverData {
Expand Down
9 changes: 9 additions & 0 deletions src/common/viewmodel/utils/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type {Nickname} from '~/common/network/types';
import type {ServicesForViewModel} from '~/common/viewmodel';

export function getUserDisplayName(
services: Pick<ServicesForViewModel, 'device'>,
nickname: Nickname | undefined,
): string {
return nickname ?? services.device.identity.string;
}

0 comments on commit 3dd2fe3

Please sign in to comment.