Skip to content

Fetch server emoji data (about Unicode emoji) #976

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 7 commits into from
Oct 10, 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
16 changes: 14 additions & 2 deletions lib/api/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,24 @@ class ApiConnection {
bool _isOpen = true;

Future<T> send<T>(String routeName, T Function(Map<String, dynamic>) fromJson,
http.BaseRequest request, {String? overrideUserAgent}) async {
http.BaseRequest request, {
bool useAuth = true,
String? overrideUserAgent,
}) async {
assert(_isOpen);

assert(debugLog("${request.method} ${request.url}"));

addAuth(request);
if (useAuth) {
if (request.url.origin != realmUrl.origin) {
// No caller should get here with a URL whose origin isn't the realm's.
// If this does happen, it's important not to proceed, because we'd be
// sending the user's auth credentials.
throw StateError("ApiConnection.send called with useAuth on off-realm URL");
}
addAuth(request);
}

if (overrideUserAgent != null) {
request.headers['User-Agent'] = overrideUserAgent;
} else {
Expand Down
3 changes: 3 additions & 0 deletions lib/api/model/initial_snapshot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class InitialSnapshot {

final int maxFileUploadSizeMib;

final Uri? serverEmojiDataUrl; // TODO(server-6)

@JsonKey(readValue: _readUsersIsActiveFallbackTrue)
final List<User> realmUsers;
@JsonKey(readValue: _readUsersIsActiveFallbackFalse)
Expand Down Expand Up @@ -117,6 +119,7 @@ class InitialSnapshot {
required this.userTopics,
required this.realmDefaultExternalAccounts,
required this.maxFileUploadSizeMib,
required this.serverEmojiDataUrl,
required this.realmUsers,
required this.realmNonActiveUsers,
required this.crossRealmBots,
Expand Down
4 changes: 4 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.

48 changes: 48 additions & 0 deletions lib/api/route/realm.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:http/http.dart' as http;
import 'package:json_annotation/json_annotation.dart';

import '../core.dart';
import '../model/initial_snapshot.dart';

part 'realm.g.dart';

Expand Down Expand Up @@ -94,3 +96,49 @@ class ExternalAuthenticationMethod {

Map<String, dynamic> toJson() => _$ExternalAuthenticationMethodToJson(this);
}

/// Fetch data from the URL described by [InitialSnapshot.serverEmojiDataUrl].
///
/// This request is unauthenticated, and the URL need not be on the realm.
/// The given [ApiConnection] is used for providing a `User-Agent` header
/// and for handling errors.
///
/// For docs, search for "server_emoji"
/// in <https://zulip.com/api/register-queue>.
Future<ServerEmojiData> fetchServerEmojiData(ApiConnection connection, {
required Uri emojiDataUrl,
}) {
// TODO(#950): cache server responses on fetchServerEmojiData

// This nontraditional endpoint doesn't conform to all the usual Zulip API
// protocols: https://zulip.com/api/rest-error-handling
// notably the `{ code, msg, result }` format for errors.
// So in the case of an error, the generic Zulip API error-handling logic
// in [ApiConnection.send] will throw [MalformedServerResponseException]
// in some cases where "malformed" isn't quite the right description.
// We'll just tolerate that.
// If it really mattered, we could refactor [ApiConnection] to accommodate.
//
// Similarly, there's no `"result": "success"` in the response.
// Fortunately none of our code looks for that in the first place.
return connection.send('fetchServerEmojiData', ServerEmojiData.fromJson,
useAuth: false,
http.Request('GET', emojiDataUrl));
}

/// The server's data describing its list of Unicode emoji
/// and its names for them.
///
/// For docs, search for "server_emoji"
/// in <https://zulip.com/api/register-queue>.
@JsonSerializable(fieldRename: FieldRename.snake)
class ServerEmojiData {
final Map<String, List<String>> codeToNames;

ServerEmojiData({required this.codeToNames});

factory ServerEmojiData.fromJson(Map<String, dynamic> json) =>
_$ServerEmojiDataFromJson(json);

Map<String, dynamic> toJson() => _$ServerEmojiDataToJson(this);
}
13 changes: 13 additions & 0 deletions lib/api/route/realm.g.dart

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

24 changes: 23 additions & 1 deletion lib/model/emoji.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import '../api/model/events.dart';
import '../api/model/initial_snapshot.dart';
import '../api/model/model.dart';
import '../api/route/realm.dart';

/// An emoji, described by how to display it in the UI.
sealed class EmojiDisplay {
Expand Down Expand Up @@ -61,6 +62,12 @@ mixin EmojiStore {
required String emojiCode,
required String emojiName,
});

// TODO cut debugServerEmojiData once we can query for lists of emoji;
// have tests make those queries end-to-end
Map<String, List<String>>? get debugServerEmojiData;

void setServerEmojiData(ServerEmojiData data);
}

/// The implementation of [EmojiStore] that does the work.
Expand All @@ -72,7 +79,7 @@ class EmojiStoreImpl with EmojiStore {
EmojiStoreImpl({
required this.realmUrl,
required this.realmEmoji,
});
}) : _serverEmojiData = null; // TODO(#974) maybe start from a hard-coded baseline

/// The same as [PerAccountStore.realmUrl].
final Uri realmUrl;
Expand Down Expand Up @@ -131,6 +138,21 @@ class EmojiStoreImpl with EmojiStore {
);
}

@override
Map<String, List<String>>? get debugServerEmojiData => _serverEmojiData;

/// The server's list of Unicode emoji and names for them,
/// from [ServerEmojiData].
///
/// This is null until [UpdateMachine.fetchEmojiData] finishes
/// retrieving the data.
Map<String, List<String>>? _serverEmojiData;

@override
void setServerEmojiData(ServerEmojiData data) {
_serverEmojiData = data.codeToNames;
}

void handleRealmEmojiUpdateEvent(RealmEmojiUpdateEvent event) {
realmEmoji = event.realmEmoji;
}
Expand Down
115 changes: 94 additions & 21 deletions lib/model/store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import '../api/model/model.dart';
import '../api/route/events.dart';
import '../api/route/messages.dart';
import '../api/backoff.dart';
import '../api/route/realm.dart';
import '../log.dart';
import '../notifications/receive.dart';
import 'autocomplete.dart';
Expand Down Expand Up @@ -345,6 +346,15 @@ class PerAccountStore extends ChangeNotifier with EmojiStore, ChannelStore, Mess
emojiType: emojiType, emojiCode: emojiCode, emojiName: emojiName);
}

@override
Map<String, List<String>>? get debugServerEmojiData => _emoji.debugServerEmojiData;

@override
void setServerEmojiData(ServerEmojiData data) {
_emoji.setServerEmojiData(data);
notifyListeners();
}

EmojiStoreImpl _emoji;

////////////////////////////////
Expand Down Expand Up @@ -746,9 +756,15 @@ class UpdateMachine {
final updateMachine = UpdateMachine.fromInitialSnapshot(
store: store, initialSnapshot: initialSnapshot);
updateMachine.poll();
if (initialSnapshot.serverEmojiDataUrl != null) {
// TODO(server-6): If the server is ancient, just skip trying to have
// a list of its emoji. (The old servers that don't provide
// serverEmojiDataUrl are already unsupported at time of writing.)
unawaited(updateMachine.fetchEmojiData(initialSnapshot.serverEmojiDataUrl!));
}
// TODO do registerNotificationToken before registerQueue:
// https://github.com/zulip/zulip-flutter/pull/325#discussion_r1365982807
updateMachine.registerNotificationToken();
unawaited(updateMachine.registerNotificationToken());
return updateMachine;
}

Expand All @@ -772,6 +788,43 @@ class UpdateMachine {
}
}

/// Fetch emoji data from the server, and update the store with the result.
///
/// This functions a lot like [registerQueue] and the surrounding logic
/// in [load] above, but it's unusual in that we've separated it out.
/// Effectively it's data that *would have* been in the [registerQueue]
/// response, except that we pulled it out to its own endpoint as part of
/// a caching strategy, because the data changes infrequently.
///
/// Conveniently (a) this deferred fetch doesn't cause any fetch/event race,
/// because this data doesn't get updated by events anyway (it can change
/// only on a server restart); and (b) we don't need this data for displaying
/// messages or anything else, only for certain UIs like the emoji picker,
/// so it's fine that we go without it for a while.
Future<void> fetchEmojiData(Uri serverEmojiDataUrl) async {
if (!debugEnableFetchEmojiData) return;
BackoffMachine? backoffMachine;
ServerEmojiData data;
while (true) {
try {
data = await fetchServerEmojiData(store.connection,
emojiDataUrl: serverEmojiDataUrl);
assert(debugLog('Got emoji data: ${data.codeToNames.length} emoji'));
break;
} catch (e) {
assert(debugLog('Error fetching emoji data: $e\n' // TODO(log)
'Backing off, then will retry…'));
// The emoji data is a lot less urgent than the initial fetch,
// or even the event-queue poll request. So wait longer.
backoffMachine ??= BackoffMachine(firstBound: const Duration(seconds: 2),
maxBound: const Duration(minutes: 2));
await backoffMachine.wait();
}
}

store.setServerEmojiData(data);
}

Completer<void>? _debugLoopSignal;

/// In debug mode, causes the polling loop to pause before the next
Expand Down Expand Up @@ -865,26 +918,6 @@ class UpdateMachine {
}
}

/// In debug mode, controls whether [registerNotificationToken] should
/// have its normal effect.
///
/// Outside of debug mode, this is always true and the setter has no effect.
static bool get debugEnableRegisterNotificationToken {
bool result = true;
assert(() {
result = _debugEnableRegisterNotificationToken;
return true;
}());
return result;
}
static bool _debugEnableRegisterNotificationToken = true;
static set debugEnableRegisterNotificationToken(bool value) {
assert(() {
_debugEnableRegisterNotificationToken = value;
return true;
}());
}

/// Send this client's notification token to the server, now and if it changes.
///
/// TODO The returned future isn't especially meaningful (it may or may not
Expand All @@ -910,6 +943,46 @@ class UpdateMachine {
NotificationService.instance.token.removeListener(_registerNotificationToken);
}

/// In debug mode, controls whether [fetchEmojiData] should
/// have its normal effect.
///
/// Outside of debug mode, this is always true and the setter has no effect.
static bool get debugEnableFetchEmojiData {
bool result = true;
assert(() {
result = _debugEnableFetchEmojiData;
return true;
}());
return result;
}
static bool _debugEnableFetchEmojiData = true;
static set debugEnableFetchEmojiData(bool value) {
assert(() {
_debugEnableFetchEmojiData = value;
return true;
}());
}

/// In debug mode, controls whether [registerNotificationToken] should
/// have its normal effect.
///
/// Outside of debug mode, this is always true and the setter has no effect.
static bool get debugEnableRegisterNotificationToken {
bool result = true;
assert(() {
result = _debugEnableRegisterNotificationToken;
return true;
}());
return result;
}
static bool _debugEnableRegisterNotificationToken = true;
static set debugEnableRegisterNotificationToken(bool value) {
assert(() {
_debugEnableRegisterNotificationToken = value;
return true;
}());
}

@override
String toString() => '${objectRuntimeType(this, 'UpdateMachine')}#${shortHash(this)}';
}
Loading