|
| 1 | +import 'package:http/http.dart' as http; |
1 | 2 | import 'package:json_annotation/json_annotation.dart';
|
2 | 3 |
|
3 | 4 | import '../core.dart';
|
| 5 | +import '../model/initial_snapshot.dart'; |
4 | 6 |
|
5 | 7 | part 'realm.g.dart';
|
6 | 8 |
|
@@ -94,3 +96,49 @@ class ExternalAuthenticationMethod {
|
94 | 96 |
|
95 | 97 | Map<String, dynamic> toJson() => _$ExternalAuthenticationMethodToJson(this);
|
96 | 98 | }
|
| 99 | + |
| 100 | +/// Fetch data from the URL described by [InitialSnapshot.serverEmojiDataUrl]. |
| 101 | +/// |
| 102 | +/// This request is unauthenticated, and the URL need not be on the realm. |
| 103 | +/// The given [ApiConnection] is used for providing a `User-Agent` header |
| 104 | +/// and for handling errors. |
| 105 | +/// |
| 106 | +/// For docs, search for "server_emoji" |
| 107 | +/// in <https://zulip.com/api/register-queue>. |
| 108 | +Future<ServerEmojiData> fetchServerEmojiData(ApiConnection connection, { |
| 109 | + required Uri emojiDataUrl, |
| 110 | +}) { |
| 111 | + // TODO(#950): cache server responses on fetchServerEmojiData |
| 112 | + |
| 113 | + // This nontraditional endpoint doesn't conform to all the usual Zulip API |
| 114 | + // protocols: https://zulip.com/api/rest-error-handling |
| 115 | + // notably the `{ code, msg, result }` format for errors. |
| 116 | + // So in the case of an error, the generic Zulip API error-handling logic |
| 117 | + // in [ApiConnection.send] will throw [MalformedServerResponseException] |
| 118 | + // in some cases where "malformed" isn't quite the right description. |
| 119 | + // We'll just tolerate that. |
| 120 | + // If it really mattered, we could refactor [ApiConnection] to accommodate. |
| 121 | + // |
| 122 | + // Similarly, there's no `"result": "success"` in the response. |
| 123 | + // Fortunately none of our code looks for that in the first place. |
| 124 | + return connection.send('fetchServerEmojiData', ServerEmojiData.fromJson, |
| 125 | + useAuth: false, |
| 126 | + http.Request('GET', emojiDataUrl)); |
| 127 | +} |
| 128 | + |
| 129 | +/// The server's data describing its list of Unicode emoji |
| 130 | +/// and its names for them. |
| 131 | +/// |
| 132 | +/// For docs, search for "server_emoji" |
| 133 | +/// in <https://zulip.com/api/register-queue>. |
| 134 | +@JsonSerializable(fieldRename: FieldRename.snake) |
| 135 | +class ServerEmojiData { |
| 136 | + final Map<String, List<String>> codeToNames; |
| 137 | + |
| 138 | + ServerEmojiData({required this.codeToNames}); |
| 139 | + |
| 140 | + factory ServerEmojiData.fromJson(Map<String, dynamic> json) => |
| 141 | + _$ServerEmojiDataFromJson(json); |
| 142 | + |
| 143 | + Map<String, dynamic> toJson() => _$ServerEmojiDataToJson(this); |
| 144 | +} |
0 commit comments