<commit_before>/*
 * Copyright (C) 2015 Actor LLC. <https://actor.im>
 */

package im.actor.core.modules.messaging.dialogs;

import java.util.ArrayList;
import java.util.List;

import im.actor.core.entity.Avatar;
import im.actor.core.entity.ContentDescription;
import im.actor.core.entity.ContentType;
import im.actor.core.entity.Dialog;
import im.actor.core.entity.DialogBuilder;
import im.actor.core.entity.Group;
import im.actor.core.entity.GroupType;
import im.actor.core.entity.Message;
import im.actor.core.entity.Peer;
import im.actor.core.entity.User;
import im.actor.core.entity.content.AbsContent;
import im.actor.core.modules.ModuleContext;
import im.actor.core.modules.messaging.dialogs.entity.ChatClear;
import im.actor.core.modules.messaging.dialogs.entity.ChatDelete;
import im.actor.core.modules.messaging.dialogs.entity.CounterChanged;
import im.actor.core.modules.messaging.dialogs.entity.GroupChanged;
import im.actor.core.modules.messaging.dialogs.entity.HistoryLoaded;
import im.actor.core.modules.messaging.dialogs.entity.InMessage;
import im.actor.core.modules.messaging.dialogs.entity.MessageContentChanged;
import im.actor.core.modules.messaging.dialogs.entity.MessageDeleted;
import im.actor.core.modules.messaging.dialogs.entity.PeerReadChanged;
import im.actor.core.modules.messaging.dialogs.entity.PeerReceiveChanged;
import im.actor.core.modules.messaging.dialogs.entity.UserChanged;
import im.actor.core.modules.messaging.history.entity.DialogHistory;
import im.actor.core.modules.ModuleActor;
import im.actor.runtime.Log;
import im.actor.runtime.Runtime;
import im.actor.runtime.actors.messages.Void;
import im.actor.runtime.annotations.Verified;
import im.actor.runtime.promise.Promise;
import im.actor.runtime.storage.ListEngine;

import static im.actor.core.util.JavaUtil.equalsE;

public class DialogsActor extends ModuleActor {

    private ListEngine<Dialog> dialogs;
    private Boolean isEmpty;
    private Boolean emptyNotified;

    public DialogsActor(ModuleContext context) {
        super(context);
    }

    @Override
    public void preStart() {
        super.preStart();
        this.dialogs = context().getMessagesModule().getDialogsEngine();
        notifyState(true);
    }

    @Verified
    private Promise<Void> onMessage(Peer peer, Message message, boolean forceWrite, int counter) {
        long start = im.actor.runtime.Runtime.getCurrentTime();
        PeerDesc peerDesc = buildPeerDesc(peer);
        if (peerDesc == null) {
            Log.d("DialogsActor", "unknown peer desc");
            return Promise.success(null);
        }

        if (message == null) {
            // Ignore empty message if not forcing write
            if (!forceWrite) {
                Log.d("DialogsActor", "not force");
                return Promise.success(null);
            }

            // Else perform chat clear
            onChatClear(peer);
        } else {
            Dialog dialog = dialogs.getValue(peer.getUnuqueId());

            ContentDescription contentDescription = ContentDescription.fromContent(message.getContent());

            DialogBuilder builder = new DialogBuilder()
                    .setRid(message.getRid())
                    .setTime(message.getDate())
                    .setMessageType(contentDescription.getContentType())
                    .setText(contentDescription.getText())
                    .setRelatedUid(contentDescription.getRelatedUser())
                    .setSenderId(message.getSenderId())
                    .setDialogTitle(peerDesc.getTitle())
                    .setDialogAvatar(peerDesc.getAvatar())
                    .setIsBot(peerDesc.isBot())
                    .setIsChannel(peerDesc.isChannel());

            if (counter >= 0) {
                builder.setUnreadCount(counter);
            }

            boolean forceUpdate = false;

            if (dialog != null) {
                // Ignore old messages if no force
                if (!forceWrite && dialog.getSortDate() > message.getSortDate()) {
                    Log.d("DialogsActor", "too old");
                    return Promise.success(null);
                }

                builder.setPeer(dialog.getPeer())
                        .setSortKey(dialog.getSortDate())
                        .updateKnownReceiveDate(dialog.getKnownReceiveDate())
                        .updateKnownReadDate(dialog.getKnownReadDate());

                // Do not push up dialogs for silent messages
                if (!contentDescription.isSilent()) {
                    builder.setSortKey(message.getSortDate());
                }

            } else {
                // Do not create dialogs for silent messages
                if (contentDescription.isSilent()) {
                    Log.d("DialogsActor", "is silent in");
                    return Promise.success(null);
                }

                builder.setPeer(peer)
                        .setSortKey(message.getSortDate());

                forceUpdate = true;
            }

            addOrUpdateItem(builder.createDialog());
            notifyState(forceUpdate);
        }

        return Promise.success(null);
    }

    @Verified
    private Promise<Void> onUserChanged(User user) {
        Dialog dialog = dialogs.getValue(user.peer().getUnuqueId());
        if (dialog != null) {
            // Ignore if nothing changed
            if (dialog.getDialogTitle().equals(user.getName())
                    && equalsE(dialog.getDialogAvatar(), user.getAvatar())) {
                return Promise.success(null);
            }

            // Update dialog peer info
            Dialog updated = dialog.editPeerInfo(user.getName(), user.getAvatar());
            addOrUpdateItem(updated);
            updateSearch(updated);
        }

        return Promise.success(null);
    }

    @Verified
    private Promise<Void> onGroupChanged(Group group) {<commit_msg>fix(core): add newly crated group to search<commit_after>/*
 * Copyright (C) 2015 Actor LLC. <https://actor.im>
 */

package im.actor.core.modules.messaging.dialogs;

import java.util.ArrayList;
import java.util.List;

import im.actor.core.entity.Avatar;
import im.actor.core.entity.ContentDescription;
import im.actor.core.entity.ContentType;
import im.actor.core.entity.Dialog;
import im.actor.core.entity.DialogBuilder;
import im.actor.core.entity.Group;
import im.actor.core.entity.GroupType;
import im.actor.core.entity.Message;
import im.actor.core.entity.Peer;
import im.actor.core.entity.User;
import im.actor.core.entity.content.AbsContent;
import im.actor.core.modules.ModuleContext;
import im.actor.core.modules.messaging.dialogs.entity.ChatClear;
import im.actor.core.modules.messaging.dialogs.entity.ChatDelete;
import im.actor.core.modules.messaging.dialogs.entity.CounterChanged;
import im.actor.core.modules.messaging.dialogs.entity.GroupChanged;
import im.actor.core.modules.messaging.dialogs.entity.HistoryLoaded;
import im.actor.core.modules.messaging.dialogs.entity.InMessage;
import im.actor.core.modules.messaging.dialogs.entity.MessageContentChanged;
import im.actor.core.modules.messaging.dialogs.entity.MessageDeleted;
import im.actor.core.modules.messaging.dialogs.entity.PeerReadChanged;
import im.actor.core.modules.messaging.dialogs.entity.PeerReceiveChanged;
import im.actor.core.modules.messaging.dialogs.entity.UserChanged;
import im.actor.core.modules.messaging.history.entity.DialogHistory;
import im.actor.core.modules.ModuleActor;
import im.actor.runtime.Log;
import im.actor.runtime.Runtime;
import im.actor.runtime.actors.messages.Void;
import im.actor.runtime.annotations.Verified;
import im.actor.runtime.promise.Promise;
import im.actor.runtime.storage.ListEngine;

import static im.actor.core.util.JavaUtil.equalsE;

public class DialogsActor extends ModuleActor {

    private ListEngine<Dialog> dialogs;
    private Boolean isEmpty;
    private Boolean emptyNotified;

    public DialogsActor(ModuleContext context) {
        super(context);
    }

    @Override
    public void preStart() {
        super.preStart();
        this.dialogs = context().getMessagesModule().getDialogsEngine();
        notifyState(true);
    }

    @Verified
    private Promise<Void> onMessage(Peer peer, Message message, boolean forceWrite, int counter) {
        long start = im.actor.runtime.Runtime.getCurrentTime();
        PeerDesc peerDesc = buildPeerDesc(peer);
        if (peerDesc == null) {
            Log.d("DialogsActor", "unknown peer desc");
            return Promise.success(null);
        }

        if (message == null) {
            // Ignore empty message if not forcing write
            if (!forceWrite) {
                Log.d("DialogsActor", "not force");
                return Promise.success(null);
            }

            // Else perform chat clear
            onChatClear(peer);
        } else {
            Dialog dialog = dialogs.getValue(peer.getUnuqueId());

            ContentDescription contentDescription = ContentDescription.fromContent(message.getContent());

            DialogBuilder builder = new DialogBuilder()
                    .setRid(message.getRid())
                    .setTime(message.getDate())
                    .setMessageType(contentDescription.getContentType())
                    .setText(contentDescription.getText())
                    .setRelatedUid(contentDescription.getRelatedUser())
                    .setSenderId(message.getSenderId())
                    .setDialogTitle(peerDesc.getTitle())
                    .setDialogAvatar(peerDesc.getAvatar())
                    .setIsBot(peerDesc.isBot())
                    .setIsChannel(peerDesc.isChannel());

            if (counter >= 0) {
                builder.setUnreadCount(counter);
            }

            boolean forceUpdate = false;
            boolean needUpdateSearch = false;
            if (dialog != null) {
                // Ignore old messages if no force
                if (!forceWrite && dialog.getSortDate() > message.getSortDate()) {
                    Log.d("DialogsActor", "too old");
                    return Promise.success(null);
                }

                builder.setPeer(dialog.getPeer())
                        .setSortKey(dialog.getSortDate())
                        .updateKnownReceiveDate(dialog.getKnownReceiveDate())
                        .updateKnownReadDate(dialog.getKnownReadDate());

                // Do not push up dialogs for silent messages
                if (!contentDescription.isSilent()) {
                    builder.setSortKey(message.getSortDate());
                }

            } else {
                // Do not create dialogs for silent messages
                if (contentDescription.isSilent()) {
                    Log.d("DialogsActor", "is silent in");
                    return Promise.success(null);
                }

                builder.setPeer(peer)
                        .setSortKey(message.getSortDate());
                needUpdateSearch = true;
                forceUpdate = true;
            }

            Dialog dialog1 = builder.createDialog();
            addOrUpdateItem(dialog1);
            if (needUpdateSearch) {
                updateSearch(dialog1);
            }
            notifyState(forceUpdate);
        }

        return Promise.success(null);
    }

    @Verified
    private Promise<Void> onUserChanged(User user) {
        Dialog dialog = dialogs.getValue(user.peer().getUnuqueId());
        if (dialog != null) {
            // Ignore if nothing changed
            if (dialog.getDialogTitle().equals(user.getName())
                    && equalsE(dialog.getDialogAvatar(), user.getAvatar())) {
                return Promise.success(null);
            }

            // Update dialog peer info
            Dialog updated = dialog.editPeerInfo(user.getName(), user.getAvatar());
            addOrUpdateItem(updated);
            updateSearch(updated);
        }

        return Promise.success(null);
    }

    @Verified
    private Promise<Void> onGroupChanged(Group group) {
{'fraction_non_alphanumeric': 0.10714878089235362, 'fraction_numerical': 0.0010781224083595952, 'mean_word_length': 2.2671362774315904, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '989043', 'n_tokens_mistral': 3281, 'n_tokens_neox': 3114, 'n_words': 567}