Search in sources :

Example 1 with StringMatch

use of im.actor.core.util.StringMatch in project actor-platform by actorapp.

the class MentionsModule method findMentions.

public List<MentionFilterResult> findMentions(int gid, String query) {
    query = query.trim().toLowerCase();
    ArrayList<MentionFilterResult> results = new ArrayList<MentionFilterResult>();
    final Group group = groups().getValue(gid);
    GroupMember[] members = group.getMembers().toArray(new GroupMember[group.getMembers().size()]);
    Arrays.sort(members, (a, b) -> {
        User ua = users().getValue(a.getUid());
        User ub = users().getValue(b.getUid());
        return ua.getName().compareToIgnoreCase(ub.getName());
    });
    for (GroupMember member : members) {
        if (member.getUid() == myUid()) {
            continue;
        }
        User user = users().getValue(member.getUid());
        boolean isNick = user.getNick() != null;
        String mention;
        String secondName;
        if (isNick) {
            mention = user.getNick();
            secondName = user.getName();
        } else {
            if (user.getLocalName() != null) {
                mention = user.getServerName();
                secondName = user.getLocalName();
            } else {
                mention = user.getName();
                secondName = null;
            }
        }
        if (query.length() == 0) {
            results.add(new MentionFilterResult(user.getUid(), user.getAvatar(), isNick ? "@" + mention : mention, new ArrayList<>(), secondName, new ArrayList<>(), isNick));
        } else {
            List<StringMatch> mentionMatches = StringMatcher.findMatches(mention, query);
            if (secondName != null) {
                List<StringMatch> secondNameMatches = StringMatcher.findMatches(secondName, query);
                if (mentionMatches.size() > 0 || secondNameMatches.size() > 0) {
                    if (isNick) {
                        List<StringMatch> nickMatches = new ArrayList<>();
                        for (StringMatch m : mentionMatches) {
                            nickMatches.add(new StringMatch(m.getStart() + 1, m.getLength()));
                        }
                        mentionMatches = nickMatches;
                    }
                    results.add(new MentionFilterResult(user.getUid(), user.getAvatar(), isNick ? "@" + mention : mention, mentionMatches, secondName, secondNameMatches, isNick));
                }
            } else {
                if (mentionMatches.size() > 0) {
                    results.add(new MentionFilterResult(user.getUid(), user.getAvatar(), mention, mentionMatches, null, null, false));
                }
            }
        }
    }
    if (results.size() > SEARCH_LIMIT) {
        results.clear();
    }
    return results;
}
Also used : Group(im.actor.core.entity.Group) GroupMember(im.actor.core.entity.GroupMember) User(im.actor.core.entity.User) MentionFilterResult(im.actor.core.entity.MentionFilterResult) ArrayList(java.util.ArrayList) StringMatch(im.actor.core.util.StringMatch)

Example 2 with StringMatch

use of im.actor.core.util.StringMatch in project actor-platform by actorapp.

the class JsMentionFilterResult method create.

public static JsMentionFilterResult create(MentionFilterResult res) {
    JsMessenger messenger = JsMessenger.getInstance();
    JsPeerInfo peerInfo = messenger.buildPeerInfo(Peer.user(res.getUid()));
    JsArray<JsStringMatch> mentionMatches = JsArray.createArray().cast();
    JsArray<JsStringMatch> secondMatches = JsArray.createArray().cast();
    if (res.getMentionMatches() != null) {
        for (StringMatch match : res.getMentionMatches()) {
            mentionMatches.push(JsStringMatch.create(match));
        }
    }
    if (res.getOriginalMatches() != null) {
        for (StringMatch match : res.getOriginalMatches()) {
            secondMatches.push(JsStringMatch.create(match));
        }
    }
    return create(peerInfo, res.getMentionString(), mentionMatches, res.getOriginalString(), secondMatches, res.isNickname());
}
Also used : JsMessenger(im.actor.core.js.JsMessenger) StringMatch(im.actor.core.util.StringMatch)

Aggregations

StringMatch (im.actor.core.util.StringMatch)2 Group (im.actor.core.entity.Group)1 GroupMember (im.actor.core.entity.GroupMember)1 MentionFilterResult (im.actor.core.entity.MentionFilterResult)1 User (im.actor.core.entity.User)1 JsMessenger (im.actor.core.js.JsMessenger)1 ArrayList (java.util.ArrayList)1