Skip to content

Commit 5b85e89

Browse files
committed
user [nfc]: Move getDisplayEmailFor method to lib/api/model/model.dart
Moved getDisplayEmailFor method which gets the user's real email address to lib/api/model/model.dart and refactored usages to remove duplication.
1 parent 3f4d272 commit 5b85e89

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

lib/api/model/model.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:json_annotation/json_annotation.dart';
22

3+
import '../../model/store.dart';
34
import 'events.dart';
45
import 'initial_snapshot.dart';
56
import 'reaction.dart';
@@ -305,6 +306,34 @@ enum UserRole{
305306
int? toJson() => apiValue;
306307
}
307308

309+
/// The given user's real email address, if known, for displaying in the UI.
310+
///
311+
/// Returns null if self-user isn't able to see [user]'s real email address.
312+
String? getDisplayEmailFor(User user, {required PerAccountStore store}) {
313+
if (store.account.zulipFeatureLevel >= 163) { // TODO(server-7)
314+
// A non-null value means self-user has access to [user]'s real email,
315+
// while a null value means it doesn't have access to the email.
316+
// Search for "delivery_email" in https://zulip.com/api/register-queue.
317+
return user.deliveryEmail;
318+
} else {
319+
if (user.deliveryEmail != null) {
320+
// A non-null value means self-user has access to [user]'s real email,
321+
// while a null value doesn't necessarily mean it doesn't have access
322+
// to the email, ....
323+
return user.deliveryEmail;
324+
} else if (store.emailAddressVisibility == EmailAddressVisibility.everyone) {
325+
// ... we have to also check for [PerAccountStore.emailAddressVisibility].
326+
// See:
327+
// * https://github.com/zulip/zulip-mobile/pull/5515#discussion_r997731727
328+
// * https://chat.zulip.org/#narrow/stream/378-api-design/topic/email.20address.20visibility/near/1296133
329+
return user.email;
330+
} else {
331+
return null;
332+
}
333+
}
334+
}
335+
336+
308337
/// As in `streams` in the initial snapshot.
309338
///
310339
/// Not called `Stream` because dart:async uses that name.

lib/widgets/profile.dart

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ import 'dart:convert';
33
import 'package:flutter/material.dart';
44
import 'package:flutter_gen/gen_l10n/zulip_localizations.dart';
55

6-
import '../api/model/initial_snapshot.dart';
76
import '../api/model/model.dart';
87
import '../model/content.dart';
98
import '../model/narrow.dart';
10-
import '../model/store.dart';
119
import 'app_bar.dart';
1210
import 'content.dart';
1311
import 'message_list.dart';
@@ -36,32 +34,6 @@ class ProfilePage extends StatelessWidget {
3634
page: ProfilePage(userId: userId));
3735
}
3836

39-
/// The given user's real email address, if known, for displaying in the UI.
40-
///
41-
/// Returns null if self-user isn't able to see [user]'s real email address.
42-
String? _getDisplayEmailFor(User user, {required PerAccountStore store}) {
43-
if (store.account.zulipFeatureLevel >= 163) { // TODO(server-7)
44-
// A non-null value means self-user has access to [user]'s real email,
45-
// while a null value means it doesn't have access to the email.
46-
// Search for "delivery_email" in https://zulip.com/api/register-queue.
47-
return user.deliveryEmail;
48-
} else {
49-
if (user.deliveryEmail != null) {
50-
// A non-null value means self-user has access to [user]'s real email,
51-
// while a null value doesn't necessarily mean it doesn't have access
52-
// to the email, ....
53-
return user.deliveryEmail;
54-
} else if (store.emailAddressVisibility == EmailAddressVisibility.everyone) {
55-
// ... we have to also check for [PerAccountStore.emailAddressVisibility].
56-
// See:
57-
// * https://github.com/zulip/zulip-mobile/pull/5515#discussion_r997731727
58-
// * https://chat.zulip.org/#narrow/stream/378-api-design/topic/email.20address.20visibility/near/1296133
59-
return user.email;
60-
} else {
61-
return null;
62-
}
63-
}
64-
}
6537

6638
@override
6739
Widget build(BuildContext context) {
@@ -72,7 +44,7 @@ class ProfilePage extends StatelessWidget {
7244
return const _ProfileErrorPage();
7345
}
7446

75-
final displayEmail = _getDisplayEmailFor(user, store: store);
47+
final displayEmail = getDisplayEmailFor(user, store: store);
7648
final items = [
7749
Center(
7850
child: Avatar(userId: userId, size: 200, borderRadius: 200 / 8)),

0 commit comments

Comments
 (0)