Skip to content

Commit

Permalink
profile: Display user email
Browse files Browse the repository at this point in the history
Fixes: zulip#291
  • Loading branch information
sm-sayedi committed Aug 6, 2024
1 parent f3faf90 commit 7f8d368
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
39 changes: 38 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,37 @@ 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.
///
/// **Note:** Starting from Zulip version 7.0 (FL 163), there's a change in
/// the API about how self-user can access [user]'s real email address.
/// Search for "delivery_email" in https://zulip.com/api/register-queue.
// TODO(server-7, FL >= 163): remove
String? _getDisplayEmailFor(User user, {required PerAccountStore store}) {
if (store.account.zulipFeatureLevel >= 163) {
// A non-null value means [selfUser] has access to [user]'s real email,
// while a null value means it doesn't have access to the email.
return user.deliveryEmail;
} else {
if (user.deliveryEmail != null) {
// A non-null value means [selfUser] 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 +75,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 +84,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
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: 'testuser@example.com');

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('testuser@example.com').evaluate()).isNotEmpty();
});

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

0 comments on commit 7f8d368

Please sign in to comment.