Skip to content

profile: Display user email #844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/api/model/initial_snapshot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ class InitialSnapshot {

final List<CustomProfileField> customProfileFields;

/// The realm-level policy, on pre-FL 163 servers, for visibility of real email addresses.
///
/// Search for "email_address_visibility" in https://zulip.com/api/register-queue.
///
/// This field is removed in Zulip 7.0 (FL 163) and replaced with a user-level
/// setting:
/// * https://zulip.com/api/update-settings#parameter-email_address_visibility
/// * https://zulip.com/api/update-realm-user-settings-defaults#parameter-email_address_visibility
final EmailAddressVisibility? emailAddressVisibility; // TODO(server-7): remove

// TODO(server-8): Remove the default values.
@JsonKey(defaultValue: 15000)
final int serverTypingStartedExpiryPeriodMilliseconds;
Expand Down Expand Up @@ -94,6 +104,7 @@ class InitialSnapshot {
required this.zulipMergeBase,
required this.alertWords,
required this.customProfileFields,
required this.emailAddressVisibility,
required this.serverTypingStartedExpiryPeriodMilliseconds,
required this.serverTypingStoppedWaitPeriodMilliseconds,
required this.serverTypingStartedWaitPeriodMilliseconds,
Expand All @@ -117,6 +128,14 @@ class InitialSnapshot {
Map<String, dynamic> toJson() => _$InitialSnapshotToJson(this);
}

enum EmailAddressVisibility {
@JsonValue(1) everyone,
@JsonValue(2) members,
@JsonValue(3) admins,
@JsonValue(4) nobody,
@JsonValue(5) moderators,
}

/// An item in `realm_default_external_accounts`.
///
/// For docs, search for "realm_default_external_accounts:"
Expand Down
12 changes: 12 additions & 0 deletions lib/api/model/initial_snapshot.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions lib/model/store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ class PerAccountStore extends ChangeNotifier with ChannelStore, MessageStore {
realmDefaultExternalAccounts: initialSnapshot.realmDefaultExternalAccounts,
realmEmoji: initialSnapshot.realmEmoji,
customProfileFields: _sortCustomProfileFields(initialSnapshot.customProfileFields),
emailAddressVisibility: initialSnapshot.emailAddressVisibility,
accountId: accountId,
selfUserId: account.userId,
userSettings: initialSnapshot.userSettings,
Expand Down Expand Up @@ -269,6 +270,7 @@ class PerAccountStore extends ChangeNotifier with ChannelStore, MessageStore {
required this.realmDefaultExternalAccounts,
required this.realmEmoji,
required this.customProfileFields,
required this.emailAddressVisibility,
required this.accountId,
required this.selfUserId,
required this.userSettings,
Expand Down Expand Up @@ -311,6 +313,8 @@ class PerAccountStore extends ChangeNotifier with ChannelStore, MessageStore {
final Map<String, RealmDefaultExternalAccount> realmDefaultExternalAccounts;
Map<String, RealmEmojiItem> realmEmoji;
List<CustomProfileField> customProfileFields;
/// For docs, please see [InitialSnapshot.emailAddressVisibility].
final EmailAddressVisibility? emailAddressVisibility; // TODO(#668): update this realm setting

////////////////////////////////
// Data attached to the self-account on the realm.
Expand Down
35 changes: 34 additions & 1 deletion lib/widgets/profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/zulip_localizations.dart';

import '../api/model/initial_snapshot.dart';
import '../api/model/model.dart';
import '../model/content.dart';
import '../model/narrow.dart';
import '../model/store.dart';
import 'content.dart';
import 'message_list.dart';
import 'page.dart';
Expand Down Expand Up @@ -33,6 +35,33 @@ class ProfilePage extends StatelessWidget {
page: ProfilePage(userId: userId));
}

/// The given user's real email address, if known, for displaying in the UI.
///
/// Returns null if self-user isn't able to see [user]'s real email address.
String? _getDisplayEmailFor(User user, {required PerAccountStore store}) {
if (store.account.zulipFeatureLevel >= 163) { // TODO(server-7)
// A non-null value means self-user has access to [user]'s real email,
// while a null value means it doesn't have access to the email.
// Search for "delivery_email" in https://zulip.com/api/register-queue.
return user.deliveryEmail;
} else {
if (user.deliveryEmail != null) {
// A non-null value means self-user has access to [user]'s real email,
// while a null value doesn't necessarily mean it doesn't have access
// to the email, ....
return user.deliveryEmail;
} else if (store.emailAddressVisibility == EmailAddressVisibility.everyone) {
// ... we have to also check for [PerAccountStore.emailAddressVisibility].
// See:
// * https://github.com/zulip/zulip-mobile/pull/5515#discussion_r997731727
// * https://chat.zulip.org/#narrow/stream/378-api-design/topic/email.20address.20visibility/near/1296133
return user.email;
} else {
return null;
}
}
}

@override
Widget build(BuildContext context) {
final zulipLocalizations = ZulipLocalizations.of(context);
Expand All @@ -42,6 +71,7 @@ class ProfilePage extends StatelessWidget {
return const _ProfileErrorPage();
}

final displayEmail = _getDisplayEmailFor(user, store: store);
final items = [
Center(
child: Avatar(userId: userId, size: 200, borderRadius: 200 / 8)),
Expand All @@ -50,7 +80,10 @@ class ProfilePage extends StatelessWidget {
textAlign: TextAlign.center,
style: _TextStyles.primaryFieldText
.merge(weightVariableTextStyle(context, wght: 700))),
// TODO(#291) render email field
if (displayEmail != null)
Text(displayEmail,
textAlign: TextAlign.center,
style: _TextStyles.primaryFieldText),
Text(roleToLabel(user.role, zulipLocalizations),
textAlign: TextAlign.center,
style: _TextStyles.primaryFieldText),
Expand Down
2 changes: 2 additions & 0 deletions test/example_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,7 @@ InitialSnapshot initialSnapshot({
String? zulipMergeBase,
List<String>? alertWords,
List<CustomProfileField>? customProfileFields,
EmailAddressVisibility? emailAddressVisibility,
int? serverTypingStartedExpiryPeriodMilliseconds,
int? serverTypingStoppedWaitPeriodMilliseconds,
int? serverTypingStartedWaitPeriodMilliseconds,
Expand All @@ -599,6 +600,7 @@ InitialSnapshot initialSnapshot({
zulipMergeBase: zulipMergeBase ?? recentZulipVersion,
alertWords: alertWords ?? ['klaxon'],
customProfileFields: customProfileFields ?? [],
emailAddressVisibility: EmailAddressVisibility.everyone,
serverTypingStartedExpiryPeriodMilliseconds:
serverTypingStartedExpiryPeriodMilliseconds ?? 15000,
serverTypingStoppedWaitPeriodMilliseconds:
Expand Down
4 changes: 3 additions & 1 deletion test/widgets/profile_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,14 @@ void main() {

group('ProfilePage', () {
testWidgets('page builds; profile page renders', (WidgetTester tester) async {
final user = eg.user(userId: 1, fullName: 'test user');
final user = eg.user(userId: 1, fullName: 'test user',
deliveryEmail: '[email protected]');

await setupPage(tester, users: [user], pageUserId: user.userId);

check(because: 'find user avatar', find.byType(Avatar).evaluate()).length.equals(1);
check(because: 'find user name', find.text('test user').evaluate()).isNotEmpty();
check(because: 'find user delivery email', find.text('[email protected]').evaluate()).isNotEmpty();
});

testWidgets('page builds; profile page renders with profileData', (WidgetTester tester) async {
Expand Down