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

package im.actor.core.modules.messaging;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;

import im.actor.core.api.ApiJsonMessage;
import im.actor.core.api.ApiMessage;
import im.actor.core.api.ApiOutPeer;
import im.actor.core.api.ApiPeer;
import im.actor.core.api.ApiPeerType;
import im.actor.core.api.ApiTextMessage;
import im.actor.core.api.base.SeqUpdate;
import im.actor.core.api.rpc.RequestArchiveChat;
import im.actor.core.api.rpc.RequestClearChat;
import im.actor.core.api.rpc.RequestDeleteChat;
import im.actor.core.api.rpc.RequestFavouriteDialog;
import im.actor.core.api.rpc.RequestMessageRemoveReaction;
import im.actor.core.api.rpc.RequestMessageSetReaction;
import im.actor.core.api.rpc.RequestUnfavouriteDialog;
import im.actor.core.api.rpc.RequestUpdateMessage;
import im.actor.core.api.rpc.ResponseDialogsOrder;
import im.actor.core.api.rpc.ResponseLoadArchived;
import im.actor.core.api.rpc.ResponseReactionsResponse;
import im.actor.core.api.rpc.ResponseSeq;
import im.actor.core.api.rpc.ResponseSeqDate;
import im.actor.core.api.updates.UpdateChatClear;
import im.actor.core.api.updates.UpdateChatDelete;
import im.actor.core.api.updates.UpdateChatGroupsChanged;
import im.actor.core.api.updates.UpdateMessageContentChanged;
import im.actor.core.api.updates.UpdateReactionsUpdate;
import im.actor.core.entity.ConversationState;
import im.actor.core.entity.Dialog;
import im.actor.core.entity.DialogSpec;
import im.actor.core.entity.Group;
import im.actor.core.entity.GroupMember;
import im.actor.core.entity.Message;
import im.actor.core.entity.Peer;
import im.actor.core.entity.PeerType;
import im.actor.core.entity.User;
import im.actor.core.entity.content.FastThumb;
import im.actor.core.entity.content.JsonContent;
import im.actor.core.entity.content.TextContent;
import im.actor.core.entity.Sticker;
import im.actor.core.events.PeerChatPreload;
import im.actor.core.modules.AbsModule;
import im.actor.core.modules.ModuleContext;
import im.actor.core.events.AppVisibleChanged;
import im.actor.core.events.PeerChatClosed;
import im.actor.core.events.PeerChatOpened;
import im.actor.core.modules.messaging.actors.ArchivedDialogsActor;
import im.actor.core.modules.messaging.actors.ConversationActor;
import im.actor.core.modules.messaging.actors.ConversationHistoryActor;
import im.actor.core.modules.messaging.actors.CursorReaderActor;
import im.actor.core.modules.messaging.actors.CursorReceiverActor;
import im.actor.core.modules.messaging.actors.DialogsActor;
import im.actor.core.modules.messaging.actors.DialogsHistoryActor;
import im.actor.core.modules.messaging.actors.ActiveDialogsActor;
import im.actor.core.modules.messaging.actors.MessageDeleteActor;
import im.actor.core.modules.messaging.actors.OwnReadActor;
import im.actor.core.modules.messaging.actors.SenderActor;
import im.actor.core.modules.sequence.internal.ChangeContent;
import im.actor.core.network.RpcCallback;
import im.actor.core.network.RpcException;
import im.actor.core.network.RpcInternalException;
import im.actor.core.viewmodel.Command;
import im.actor.core.viewmodel.CommandCallback;
import im.actor.core.viewmodel.ConversationVM;
import im.actor.core.viewmodel.DialogGroupsVM;
import im.actor.core.viewmodel.DialogSpecVM;
import im.actor.runtime.Storage;
import im.actor.runtime.actors.ActorCreator;
import im.actor.runtime.actors.ActorRef;
import im.actor.runtime.actors.Props;
import im.actor.runtime.bser.BserCreator;
import im.actor.runtime.eventbus.BusSubscriber;
import im.actor.runtime.eventbus.Event;
import im.actor.runtime.files.FileSystemReference;
import im.actor.runtime.mvvm.MVVMCollection;
import im.actor.runtime.storage.ListEngine;
import im.actor.runtime.storage.SyncKeyValue;

import static im.actor.runtime.actors.ActorSystem.system;

public class MessagesModule extends AbsModule implements BusSubscriber {

    private static final String DIALOGS_KEY_VERSION = "_1";

    private ListEngine<Dialog> dialogs;

    private ActorRef dialogsActor;
    private ActorRef dialogsHistoryActor;
    private ActorRef archivedDialogsActor;
    private ActorRef dialogsGroupedActor;
    private ActorRef ownReadActor;
    private ActorRef plainReadActor;
    private ActorRef plainReceiverActor;
    private ActorRef sendMessageActor;
    private ActorRef deletionsActor;

    private MVVMCollection<ConversationState, ConversationVM> conversationStates;

    private final HashMap<Peer, ListEngine<Message>> conversationEngines = new HashMap<>();
    private final HashMap<Peer, ListEngine<Message>> conversationDocsEngines = new HashMap<>();
    private final HashMap<String, ListEngine> customConversationEngines = new HashMap<>();
    private final HashMap<Peer, ActorRef> conversationActors = new HashMap<>();
    private final HashMap<Peer, ActorRef> conversationHistoryActors = new HashMap<>();
    private final HashMap<Peer, ConversationVM> conversationVMS = new HashMap<>();

    private final SyncKeyValue cursorStorage;

    private final MVVMCollection<DialogSpec, DialogSpecVM> dialogDescKeyValue;

    private final DialogGroupsVM dialogGroups = new DialogGroupsVM();

    public MessagesModule(final ModuleContext context) {
        super(context);
        this.conversationStates = Storage.createKeyValue(STORAGE_CHAT_STATES,
                ConversationVM.CREATOR,
                ConversationState.CREATOR,
                ConversationState.DEFAULT_CREATOR);
        this.dialogDescKeyValue = Storage.createKeyValue(STORAGE_DIALOGS_DESC, DialogSpecVM.CREATOR, DialogSpec.CREATOR);
        this.cursorStorage = new SyncKeyValue(Storage.createKeyValue(STORAGE_CURSOR));
        this.dialogs = Storage.createList(STORAGE_DIALOGS + DIALOGS_KEY_VERSION, Dialog.CREATOR);
    }

    public void run() {
        this.dialogsActor = system().actorOf(Props.create(new ActorCreator() {
            @Override
            public DialogsActor create() {
                return new DialogsActor(context());
            }
        }), "actor/dialogs");
        this.dialogsHistoryActor = system().actorOf(Props.create(new ActorCreator() {
            @Override
            public DialogsHistoryActor create() {<commit_msg>ref(core): Remove unsued conversation vm cache<commit_after>/*
 * Copyright (C) 2015 Actor LLC. <https://actor.im>
 */

package im.actor.core.modules.messaging;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;

import im.actor.core.api.ApiJsonMessage;
import im.actor.core.api.ApiMessage;
import im.actor.core.api.ApiOutPeer;
import im.actor.core.api.ApiPeer;
import im.actor.core.api.ApiPeerType;
import im.actor.core.api.ApiTextMessage;
import im.actor.core.api.base.SeqUpdate;
import im.actor.core.api.rpc.RequestArchiveChat;
import im.actor.core.api.rpc.RequestClearChat;
import im.actor.core.api.rpc.RequestDeleteChat;
import im.actor.core.api.rpc.RequestFavouriteDialog;
import im.actor.core.api.rpc.RequestMessageRemoveReaction;
import im.actor.core.api.rpc.RequestMessageSetReaction;
import im.actor.core.api.rpc.RequestUnfavouriteDialog;
import im.actor.core.api.rpc.RequestUpdateMessage;
import im.actor.core.api.rpc.ResponseDialogsOrder;
import im.actor.core.api.rpc.ResponseLoadArchived;
import im.actor.core.api.rpc.ResponseReactionsResponse;
import im.actor.core.api.rpc.ResponseSeq;
import im.actor.core.api.rpc.ResponseSeqDate;
import im.actor.core.api.updates.UpdateChatClear;
import im.actor.core.api.updates.UpdateChatDelete;
import im.actor.core.api.updates.UpdateChatGroupsChanged;
import im.actor.core.api.updates.UpdateMessageContentChanged;
import im.actor.core.api.updates.UpdateReactionsUpdate;
import im.actor.core.entity.ConversationState;
import im.actor.core.entity.Dialog;
import im.actor.core.entity.DialogSpec;
import im.actor.core.entity.Group;
import im.actor.core.entity.GroupMember;
import im.actor.core.entity.Message;
import im.actor.core.entity.Peer;
import im.actor.core.entity.PeerType;
import im.actor.core.entity.User;
import im.actor.core.entity.content.FastThumb;
import im.actor.core.entity.content.JsonContent;
import im.actor.core.entity.content.TextContent;
import im.actor.core.entity.Sticker;
import im.actor.core.events.PeerChatPreload;
import im.actor.core.modules.AbsModule;
import im.actor.core.modules.ModuleContext;
import im.actor.core.events.AppVisibleChanged;
import im.actor.core.events.PeerChatClosed;
import im.actor.core.events.PeerChatOpened;
import im.actor.core.modules.messaging.actors.ArchivedDialogsActor;
import im.actor.core.modules.messaging.actors.ConversationActor;
import im.actor.core.modules.messaging.actors.ConversationHistoryActor;
import im.actor.core.modules.messaging.actors.CursorReaderActor;
import im.actor.core.modules.messaging.actors.CursorReceiverActor;
import im.actor.core.modules.messaging.actors.DialogsActor;
import im.actor.core.modules.messaging.actors.DialogsHistoryActor;
import im.actor.core.modules.messaging.actors.ActiveDialogsActor;
import im.actor.core.modules.messaging.actors.MessageDeleteActor;
import im.actor.core.modules.messaging.actors.OwnReadActor;
import im.actor.core.modules.messaging.actors.SenderActor;
import im.actor.core.modules.sequence.internal.ChangeContent;
import im.actor.core.network.RpcCallback;
import im.actor.core.network.RpcException;
import im.actor.core.network.RpcInternalException;
import im.actor.core.viewmodel.Command;
import im.actor.core.viewmodel.CommandCallback;
import im.actor.core.viewmodel.ConversationVM;
import im.actor.core.viewmodel.DialogGroupsVM;
import im.actor.core.viewmodel.DialogSpecVM;
import im.actor.runtime.Storage;
import im.actor.runtime.actors.ActorCreator;
import im.actor.runtime.actors.ActorRef;
import im.actor.runtime.actors.Props;
import im.actor.runtime.bser.BserCreator;
import im.actor.runtime.eventbus.BusSubscriber;
import im.actor.runtime.eventbus.Event;
import im.actor.runtime.files.FileSystemReference;
import im.actor.runtime.mvvm.MVVMCollection;
import im.actor.runtime.storage.ListEngine;
import im.actor.runtime.storage.SyncKeyValue;

import static im.actor.runtime.actors.ActorSystem.system;

public class MessagesModule extends AbsModule implements BusSubscriber {

    private static final String DIALOGS_KEY_VERSION = "_1";

    private ListEngine<Dialog> dialogs;

    private ActorRef dialogsActor;
    private ActorRef dialogsHistoryActor;
    private ActorRef archivedDialogsActor;
    private ActorRef dialogsGroupedActor;
    private ActorRef ownReadActor;
    private ActorRef plainReadActor;
    private ActorRef plainReceiverActor;
    private ActorRef sendMessageActor;
    private ActorRef deletionsActor;

    private MVVMCollection<ConversationState, ConversationVM> conversationStates;

    private final HashMap<Peer, ListEngine<Message>> conversationEngines = new HashMap<>();
    private final HashMap<Peer, ListEngine<Message>> conversationDocsEngines = new HashMap<>();
    private final HashMap<String, ListEngine> customConversationEngines = new HashMap<>();
    private final HashMap<Peer, ActorRef> conversationActors = new HashMap<>();
    private final HashMap<Peer, ActorRef> conversationHistoryActors = new HashMap<>();

    private final SyncKeyValue cursorStorage;

    private final MVVMCollection<DialogSpec, DialogSpecVM> dialogDescKeyValue;

    private final DialogGroupsVM dialogGroups = new DialogGroupsVM();

    public MessagesModule(final ModuleContext context) {
        super(context);
        this.conversationStates = Storage.createKeyValue(STORAGE_CHAT_STATES,
                ConversationVM.CREATOR,
                ConversationState.CREATOR,
                ConversationState.DEFAULT_CREATOR);
        this.dialogDescKeyValue = Storage.createKeyValue(STORAGE_DIALOGS_DESC, DialogSpecVM.CREATOR, DialogSpec.CREATOR);
        this.cursorStorage = new SyncKeyValue(Storage.createKeyValue(STORAGE_CURSOR));
        this.dialogs = Storage.createList(STORAGE_DIALOGS + DIALOGS_KEY_VERSION, Dialog.CREATOR);
    }

    public void run() {
        this.dialogsActor = system().actorOf(Props.create(new ActorCreator() {
            @Override
            public DialogsActor create() {
                return new DialogsActor(context());
            }
        }), "actor/dialogs");
        this.dialogsHistoryActor = system().actorOf(Props.create(new ActorCreator() {
            @Override
            public DialogsHistoryActor create() {
{'fraction_non_alphanumeric': 0.10184595798854233, 'fraction_numerical': 0.000795671546785487, 'mean_word_length': 5.0896317829457365, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, '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': '6725926', 'n_tokens_mistral': 3817, 'n_tokens_neox': 3680, 'n_words': 630}