Skip to content

Commit

Permalink
Make the emoji aliases more flexible
Browse files Browse the repository at this point in the history
In case we decide to touch the database.
  • Loading branch information
zapek committed Feb 29, 2024
1 parent ae7b116 commit 44ff857
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions ui/src/main/java/io/xeres/ui/support/emoji/RsEmojiAlias.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,25 @@
import java.io.IOException;
import java.util.*;

/**
* Handles shortcodes produced by Retroshare. Since they are sent directly by it in the wire,
* typos in its database should be preserved.
*/
class RsEmojiAlias
{
private static final Logger log = LoggerFactory.getLogger(RsEmojiAlias.class);

private static final String EMOTES_DATABASE = "/retroshare-emojis.json";

private final Map<String, String> aliasesMap = HashMap.newHashMap(705);
private final int longestAlias;
private Map<String, String> aliasesMap;
private int longestAlias;

private record AliasEntity(String alias, String unicode)
{
}

RsEmojiAlias(ObjectMapper objectMapper)
{
int longestAlias;

try
{
var loadedAliases = objectMapper.readValue(Objects.requireNonNull(getClass().getResourceAsStream(EMOTES_DATABASE)),
Expand All @@ -53,6 +55,8 @@ private record AliasEntity(String alias, String unicode)

log.debug("Loaded {} Retroshare emoji aliases", loadedAliases.size());

aliasesMap = HashMap.newHashMap(loadedAliases.size());

loadedAliases.forEach(aliasEntity -> aliasesMap.put(aliasEntity.alias(), aliasEntity.unicode()));
longestAlias = aliasesMap.keySet().stream()
.max(Comparator.comparingInt(String::length))
Expand All @@ -61,16 +65,26 @@ private record AliasEntity(String alias, String unicode)
catch (IOException e)
{
log.error("Couldn't load Retroshare emoji alias database", e);
aliasesMap = Map.of();
longestAlias = 0;
}
this.longestAlias = longestAlias;
}

/**
* Gets the unicode emoji for the alias.
*
* @param alias the shortcode, for example <i>wink</i>
* @return the unicode emoji
*/
public String getUnicodeForAlias(String alias)
{
return aliasesMap.get(alias);
}

/**
* Gets the longest alias in the database, for optimization purposes.
* @return the longest alias in the database
*/
public int getLongestAlias()
{
return longestAlias;
Expand Down

0 comments on commit 44ff857

Please sign in to comment.