Skip to content

Commit

Permalink
emoji: Exclude deactivated realm emoji from autocomplete
Browse files Browse the repository at this point in the history
Fixes zulip#1113.
  • Loading branch information
gnprice committed Dec 9, 2024
1 parent e564806 commit f09aab1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
17 changes: 12 additions & 5 deletions lib/model/emoji.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,16 @@ class EmojiStoreImpl with EmojiStore {
/// including deactivated emoji not available for new uses.
///
/// These are the emoji that can have [ReactionType.realmEmoji].
// TODO(#1113) limit to active realm emoji where appropriate
///
/// For emoji available to be newly used, see [activeRealmEmoji].
Map<String, RealmEmojiItem> allRealmEmoji;

/// The realm's custom emoji that are available for new uses
/// in messages and reactions.
Iterable<RealmEmojiItem> get activeRealmEmoji {
return allRealmEmoji.values.where((emoji) => !emoji.deactivated);
}

/// The realm-relative URL of the unique "Zulip extra emoji", :zulip:.
static const kZulipEmojiUrl = '/static/generated/emoji/images/emoji/unicode/zulip.png';

Expand Down Expand Up @@ -218,7 +225,7 @@ class EmojiStoreImpl with EmojiStore {
final results = <EmojiCandidate>[];

final namesOverridden = {
for (final emoji in allRealmEmoji.values) emoji.name,
for (final emoji in activeRealmEmoji) emoji.name,
'zulip',
};
// TODO(log) if _serverEmojiData missing
Expand All @@ -242,16 +249,16 @@ class EmojiStoreImpl with EmojiStore {
aliases: aliases));
}

for (final entry in allRealmEmoji.entries) {
final emojiName = entry.value.name;
for (final emoji in activeRealmEmoji) {
final emojiName = emoji.name;
if (emojiName == 'zulip') {
// TODO does 'zulip' really override realm emoji?
// (This is copied from zulip-mobile's behavior.)
continue;
}
results.add(_emojiCandidateFor(
emojiType: ReactionType.realmEmoji,
emojiCode: entry.key, emojiName: emojiName,
emojiCode: emoji.emojiCode, emojiName: emojiName,
aliases: null));
}

Expand Down
37 changes: 37 additions & 0 deletions test/model/emoji_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,31 @@ void main() {
return store;
}

test('realm emoji included only when active', () {
final store = prepare(realmEmoji: {
'1': eg.realmEmojiItem(emojiCode: '1', emojiName: 'abc', deactivated: true),
'2': eg.realmEmojiItem(emojiCode: '2', emojiName: 'abcd'),
});
check(store.allEmojiCandidates()).deepEquals([
isRealmCandidate(emojiCode: '2', emojiName: 'abcd'),
isZulipCandidate(),
]);
});

test('realm emoji tolerate name collisions', () {
final store = prepare(realmEmoji: {
'1': eg.realmEmojiItem(emojiCode: '1', emojiName: 'test', deactivated: true),
'2': eg.realmEmojiItem(emojiCode: '2', emojiName: 'try', deactivated: true),
'3': eg.realmEmojiItem(emojiCode: '3', emojiName: 'try', deactivated: true),
'4': eg.realmEmojiItem(emojiCode: '4', emojiName: 'try'),
'5': eg.realmEmojiItem(emojiCode: '5', emojiName: 'test', deactivated: true),
});
check(store.allEmojiCandidates()).deepEquals([
isRealmCandidate(emojiCode: '4', emojiName: 'try'),
isZulipCandidate(),
]);
});

test('realm emoji overrides Unicode emoji', () {
final store = prepare(realmEmoji: {
'1': eg.realmEmojiItem(emojiCode: '1', emojiName: 'smiley'),
Expand All @@ -137,6 +162,18 @@ void main() {
]);
});

test('deactivated realm emoji cause no override of Unicode emoji', () {
final store = prepare(realmEmoji: {
'1': eg.realmEmojiItem(emojiCode: '1', emojiName: 'ant', deactivated: true),
}, unicodeEmoji: {
'1f41c': ['ant'],
});
check(store.allEmojiCandidates()).deepEquals([
isUnicodeCandidate('1f41c', ['ant']),
isZulipCandidate(),
]);
});

test('Unicode emoji with overridden aliases survives with remaining names', () {
final store = prepare(realmEmoji: {
'1': eg.realmEmojiItem(emojiCode: '1', emojiName: 'tangerine'),
Expand Down

0 comments on commit f09aab1

Please sign in to comment.