Skip to content

Commit 286024c

Browse files
committed
api [nfc]: Move tryParseEmojiCodeToUnicode to API subtree; document
This describes the semantics of part of Zulip's API.
1 parent 15bd955 commit 286024c

File tree

3 files changed

+30
-22
lines changed

3 files changed

+30
-22
lines changed

lib/api/model/model.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,35 @@ enum UserTopicVisibilityPolicy {
507507
int? toJson() => apiValue;
508508
}
509509

510+
/// Convert a Unicode emoji's Zulip "emoji code" into the
511+
/// actual Unicode code points.
512+
///
513+
/// The argument corresponds to [Reaction.emojiCode] when [Reaction.emojiType]
514+
/// is [ReactionType.unicodeEmoji]. For docs, see:
515+
/// https://zulip.com/api/add-reaction#parameter-reaction_type
516+
///
517+
/// In addition to reactions, these appear in Zulip content HTML;
518+
/// see [UnicodeEmojiNode.emojiUnicode].
519+
String? tryParseEmojiCodeToUnicode(String emojiCode) {
520+
// Ported from: https://github.com/zulip/zulip-mobile/blob/c979530d6804db33310ed7d14a4ac62017432944/src/emoji/data.js#L108-L112
521+
// which refers to a comment in the server implementation:
522+
// https://github.com/zulip/zulip/blob/63c9296d5339517450f79f176dc02d77b08020c8/zerver/models.py#L3235-L3242
523+
// In addition to what's in the doc linked above, that comment adds:
524+
//
525+
// > For examples, see "non_qualified" or "unified" in the following data,
526+
// > with "non_qualified" taking precedence when both present:
527+
// > https://raw.githubusercontent.com/iamcal/emoji-data/a8174c74675355c8c6a9564516b2e961fe7257ef/emoji_pretty.json
528+
// > [link fixed to permalink; original comment says "master" for the commit]
529+
try {
530+
return String.fromCharCodes(emojiCode.split('-')
531+
.map((hex) => int.parse(hex, radix: 16)));
532+
} on FormatException { // thrown by `int.parse`
533+
return null;
534+
} on ArgumentError { // thrown by `String.fromCharCodes`
535+
return null;
536+
}
537+
}
538+
510539
/// As in the get-messages response.
511540
///
512541
/// https://zulip.com/api/get-messages#response

lib/model/content.dart

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:flutter/foundation.dart';
33
import 'package:html/dom.dart' as dom;
44
import 'package:html/parser.dart';
55

6+
import '../api/model/model.dart';
67
import 'code_block.dart';
78

89
/// A node in a parse tree for Zulip message-style content.
@@ -716,27 +717,6 @@ class GlobalTimeNode extends InlineContentNode {
716717

717718
////////////////////////////////////////////////////////////////
718719
719-
// Ported from https://github.com/zulip/zulip-mobile/blob/c979530d6804db33310ed7d14a4ac62017432944/src/emoji/data.js#L108-L112
720-
//
721-
// Which was in turn ported from https://github.com/zulip/zulip/blob/63c9296d5339517450f79f176dc02d77b08020c8/zerver/models.py#L3235-L3242
722-
// and that describes the encoding as follows:
723-
//
724-
// > * For Unicode emoji, [emoji_code is] a dash-separated hex encoding of
725-
// > the sequence of Unicode codepoints that define this emoji in the
726-
// > Unicode specification. For examples, see "non_qualified" or
727-
// > "unified" in the following data, with "non_qualified" taking
728-
// > precedence when both present:
729-
// > https://raw.githubusercontent.com/iamcal/emoji-data/master/emoji_pretty.json
730-
String? tryParseEmojiCodeToUnicode(String code) {
731-
try {
732-
return String.fromCharCodes(code.split('-').map((hex) => int.parse(hex, radix: 16)));
733-
} on FormatException { // thrown by `int.parse`
734-
return null;
735-
} on ArgumentError { // thrown by `String.fromCharCodes`
736-
return null;
737-
}
738-
}
739-
740720
/// What sort of nodes a [_ZulipContentParser] is currently expecting to find.
741721
enum _ParserContext {
742722
/// The parser is currently looking for block nodes.

lib/widgets/emoji_reaction.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import 'package:flutter/material.dart';
33

44
import '../api/model/model.dart';
55
import '../api/route/messages.dart';
6-
import '../model/content.dart';
76
import 'content.dart';
87
import 'store.dart';
98
import 'text.dart';

0 commit comments

Comments
 (0)