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

package im.actor.model.modules.messages;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import im.actor.model.api.DocumentEx;
import im.actor.model.api.DocumentExPhoto;
import im.actor.model.api.DocumentExVideo;
import im.actor.model.api.DocumentMessage;
import im.actor.model.api.OutPeer;
import im.actor.model.api.TextMessage;
import im.actor.model.api.base.SeqUpdate;
import im.actor.model.api.rpc.RequestSendMessage;
import im.actor.model.api.rpc.ResponseSeqDate;
import im.actor.model.api.updates.UpdateMessageSent;
import im.actor.model.droidkit.actors.Environment;
import im.actor.model.entity.FileReference;
import im.actor.model.entity.Message;
import im.actor.model.entity.MessageState;
import im.actor.model.entity.Peer;
import im.actor.model.entity.content.AbsContent;
import im.actor.model.entity.content.DocumentContent;
import im.actor.model.entity.content.FastThumb;
import im.actor.model.entity.content.FileLocalSource;
import im.actor.model.entity.content.FileRemoteSource;
import im.actor.model.entity.content.PhotoContent;
import im.actor.model.entity.content.TextContent;
import im.actor.model.entity.content.VideoContent;
import im.actor.model.modules.Modules;
import im.actor.model.modules.file.UploadManager;
import im.actor.model.modules.messages.entity.PendingMessage;
import im.actor.model.modules.messages.entity.PendingMessagesStorage;
import im.actor.model.modules.utils.ModuleActor;
import im.actor.model.modules.utils.RandomUtils;
import im.actor.model.network.RpcCallback;
import im.actor.model.network.RpcException;

public class SenderActor extends ModuleActor {

    private static final String PREFERENCES = "sender_pending";

    private PendingMessagesStorage pendingMessages;

    public SenderActor(Modules messenger) {
        super(messenger);
    }

    @Override
    public void preStart() {
        pendingMessages = new PendingMessagesStorage();
        byte[] p = preferences().getBytes(PREFERENCES);
        if (p != null) {
            try {
                pendingMessages = PendingMessagesStorage.fromBytes(p);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        boolean isChanged = false;
        for (PendingMessage pending : pendingMessages.getPendingMessages().toArray(new PendingMessage[0])) {
            if (pending.getContent() instanceof TextContent) {
                performSendContent(pending.getPeer(), pending.getRid(), pending.getContent());
            } else if (pending.getContent() instanceof DocumentContent) {
                DocumentContent documentContent = (DocumentContent) pending.getContent();
                if (documentContent.getSource() instanceof FileLocalSource) {
                    if (config().getFileSystemProvider() != null &&
                            config().getFileSystemProvider().isFsPersistent()) {
                        performUploadFile(pending.getRid(),
                                ((FileLocalSource) documentContent.getSource()).getFileDescriptor(),
                                ((FileLocalSource) documentContent.getSource()).getFileName());
                    } else {
                        List<Long> rids = new ArrayList<Long>();
                        rids.add(pending.getRid());
                        getConversationActor(pending.getPeer()).send(new ConversationActor.MessagesDeleted(rids));
                        pendingMessages.getPendingMessages().remove(pending);
                        isChanged = true;
                    }
                } else {
                    performSendContent(pending.getPeer(), pending.getRid(),
                            pending.getContent());
                }
            }
        }

        if (isChanged) {
            savePending();
        }
    }

    // Sending text

    public void doSendText(Peer peer, String text) {
        long rid = RandomUtils.nextRid();
        long date = Environment.getCurrentTime();

        Message message = new Message(rid, date, date, myUid(), MessageState.PENDING, new TextContent(text));
        getConversationActor(peer).send(message);

        pendingMessages.getPendingMessages().add(new PendingMessage(peer, rid, new TextContent(text)));

        performSendContent(peer, rid, new TextContent(text));
    }

    // Sending documents

    public void doSendDocument(Peer peer, String fileName, String mimeType, int fileSize,
                               FastThumb fastThumb, String descriptor) {
        long rid = RandomUtils.nextRid();
        long date = Environment.getCurrentTime();
        DocumentContent documentContent = new DocumentContent(
                new FileLocalSource(fileName, fileSize, descriptor),
                mimeType, fileName, fastThumb);
        Message message = new Message(rid, date, date, myUid(), MessageState.PENDING, documentContent);
        getConversationActor(peer).send(message);

        pendingMessages.getPendingMessages().add(new PendingMessage(peer, rid, documentContent));
        savePending();

        performUploadFile(rid, descriptor, fileName);
    }

    public void doSendPhoto(Peer peer, FastThumb fastThumb, String descriptor, String fileName,
                            int fileSize, int w, int h) {
        long rid = RandomUtils.nextRid();
        long date = Environment.getCurrentTime();<commit_msg>fix(common): Fix resending of messages<commit_after>/*
 * Copyright (C) 2015 Actor LLC. <https://actor.im>
 */

package im.actor.model.modules.messages;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import im.actor.model.api.DocumentEx;
import im.actor.model.api.DocumentExPhoto;
import im.actor.model.api.DocumentExVideo;
import im.actor.model.api.DocumentMessage;
import im.actor.model.api.OutPeer;
import im.actor.model.api.TextMessage;
import im.actor.model.api.base.SeqUpdate;
import im.actor.model.api.rpc.RequestSendMessage;
import im.actor.model.api.rpc.ResponseSeqDate;
import im.actor.model.api.updates.UpdateMessageSent;
import im.actor.model.droidkit.actors.Environment;
import im.actor.model.entity.FileReference;
import im.actor.model.entity.Message;
import im.actor.model.entity.MessageState;
import im.actor.model.entity.Peer;
import im.actor.model.entity.content.AbsContent;
import im.actor.model.entity.content.DocumentContent;
import im.actor.model.entity.content.FastThumb;
import im.actor.model.entity.content.FileLocalSource;
import im.actor.model.entity.content.FileRemoteSource;
import im.actor.model.entity.content.PhotoContent;
import im.actor.model.entity.content.TextContent;
import im.actor.model.entity.content.VideoContent;
import im.actor.model.modules.Modules;
import im.actor.model.modules.file.UploadManager;
import im.actor.model.modules.messages.entity.PendingMessage;
import im.actor.model.modules.messages.entity.PendingMessagesStorage;
import im.actor.model.modules.utils.ModuleActor;
import im.actor.model.modules.utils.RandomUtils;
import im.actor.model.network.RpcCallback;
import im.actor.model.network.RpcException;

public class SenderActor extends ModuleActor {

    private static final String PREFERENCES = "sender_pending";

    private PendingMessagesStorage pendingMessages;

    public SenderActor(Modules messenger) {
        super(messenger);
    }

    @Override
    public void preStart() {
        pendingMessages = new PendingMessagesStorage();
        byte[] p = preferences().getBytes(PREFERENCES);
        if (p != null) {
            try {
                pendingMessages = PendingMessagesStorage.fromBytes(p);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        boolean isChanged = false;
        for (PendingMessage pending : pendingMessages.getPendingMessages().toArray(new PendingMessage[0])) {
            if (pending.getContent() instanceof TextContent) {
                performSendContent(pending.getPeer(), pending.getRid(), pending.getContent());
            } else if (pending.getContent() instanceof DocumentContent) {
                DocumentContent documentContent = (DocumentContent) pending.getContent();
                if (documentContent.getSource() instanceof FileLocalSource) {
                    if (config().getFileSystemProvider() != null &&
                            config().getFileSystemProvider().isFsPersistent()) {
                        performUploadFile(pending.getRid(),
                                ((FileLocalSource) documentContent.getSource()).getFileDescriptor(),
                                ((FileLocalSource) documentContent.getSource()).getFileName());
                    } else {
                        List<Long> rids = new ArrayList<Long>();
                        rids.add(pending.getRid());
                        getConversationActor(pending.getPeer()).send(new ConversationActor.MessagesDeleted(rids));
                        pendingMessages.getPendingMessages().remove(pending);
                        isChanged = true;
                    }
                } else {
                    performSendContent(pending.getPeer(), pending.getRid(),
                            pending.getContent());
                }
            }
        }

        if (isChanged) {
            savePending();
        }
    }

    // Sending text

    public void doSendText(Peer peer, String text) {
        long rid = RandomUtils.nextRid();
        long date = Environment.getCurrentTime();

        Message message = new Message(rid, date, date, myUid(), MessageState.PENDING, new TextContent(text));
        getConversationActor(peer).send(message);

        pendingMessages.getPendingMessages().add(new PendingMessage(peer, rid, new TextContent(text)));
        savePending();

        performSendContent(peer, rid, new TextContent(text));
    }

    // Sending documents

    public void doSendDocument(Peer peer, String fileName, String mimeType, int fileSize,
                               FastThumb fastThumb, String descriptor) {
        long rid = RandomUtils.nextRid();
        long date = Environment.getCurrentTime();
        DocumentContent documentContent = new DocumentContent(
                new FileLocalSource(fileName, fileSize, descriptor),
                mimeType, fileName, fastThumb);
        Message message = new Message(rid, date, date, myUid(), MessageState.PENDING, documentContent);
        getConversationActor(peer).send(message);

        pendingMessages.getPendingMessages().add(new PendingMessage(peer, rid, documentContent));
        savePending();

        performUploadFile(rid, descriptor, fileName);
    }

    public void doSendPhoto(Peer peer, FastThumb fastThumb, String descriptor, String fileName,
                            int fileSize, int w, int h) {
        long rid = RandomUtils.nextRid();
        long date = Environment.getCurrentTime();
{'fraction_non_alphanumeric': 0.10473405227563516, 'fraction_numerical': 0.0009139097057210748, 'mean_word_length': 2.73481228668942, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '3754818', 'n_tokens_mistral': 2858, 'n_tokens_neox': 2808, 'n_words': 558}