<commit_before>package com.github.alebabai.tg2vk.service.impl;

import com.github.alebabai.tg2vk.domain.ChatSettings;
import com.github.alebabai.tg2vk.repository.UserRepository;
import com.github.alebabai.tg2vk.service.LinkerService;
import com.github.alebabai.tg2vk.service.TelegramService;
import com.github.alebabai.tg2vk.service.TemplateRenderer;
import com.github.alebabai.tg2vk.util.Tg2vkMapperUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.pengrad.telegrambot.model.request.ParseMode;
import com.pengrad.telegrambot.request.*;
import com.vk.api.sdk.objects.audio.AudioFull;
import com.vk.api.sdk.objects.docs.Doc;
import com.vk.api.sdk.objects.messages.Message;
import com.vk.api.sdk.objects.messages.MessageAttachment;
import com.vk.api.sdk.objects.users.User;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.data.util.ReflectionUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

@Service
public class LinkerServiceImpl implements LinkerService {

    private static final Logger LOGGER = LoggerFactory.getLogger(LinkerServiceImpl.class);
    private static final String PRIVATE_MESSAGE_TEMPLATE = "telegram/private_message.html";
    private static final String GROUP_MESSAGE_TEMPLATE = "telegram/group_message.html";

    private final UserRepository userRepository;
    private final TelegramService tgService;
    private final TemplateRenderer templateRenderer;
    private final MessageSourceAccessor messages;
    private final OkHttpClient httpClient;
    private final Gson gson;

    @Autowired
    public LinkerServiceImpl(UserRepository userRepository,
                             TelegramService tgService,
                             TemplateRenderer templateRenderer,
                             MessageSource messageSource) {
        this.userRepository = userRepository;
        this.tgService = tgService;
        this.templateRenderer = templateRenderer;
        this.messages = new MessageSourceAccessor(messageSource);
        this.httpClient = new OkHttpClient.Builder().build();
        this.gson = new GsonBuilder().create();
    }

    @Override
    public BiConsumer<User, Message> getVkMessageHandler(Integer userId) {
        return (profile, message) -> {
            try {
                final Integer vkChatId = Tg2vkMapperUtils.getVkChatId(message);
                Optional.ofNullable(userRepository.findOne(userId))
                        .map(user -> Optional.of(user.getChatsSettings().stream()
                                .filter(chatSettings -> Objects.equals(chatSettings.getVkChatId(), vkChatId))
                                .collect(toList()))
                                .orElseGet(() -> Collections.singletonList(new ChatSettings()
                                        .setTgChatId(user.getTgId())
                                        .setVkChatId(vkChatId)
                                        .setStarted(true))))
                        .filter(chatSettings -> chatSettings.stream().allMatch(ChatSettings::isStarted))
                        .map(chatSettings -> chatSettings.stream().map(ChatSettings::getTgChatId))
                        .ifPresent(chatSettingsStream -> chatSettingsStream.forEach(getMainHandler(message, profile)));
            } catch (Exception e) {
                LOGGER.error("Error during vk message handling: ", e);
            }
        };
    }<commit_msg>refactor(chats.linker): add default case if no chats found<commit_after>package com.github.alebabai.tg2vk.service.impl;

import com.github.alebabai.tg2vk.domain.ChatSettings;
import com.github.alebabai.tg2vk.repository.UserRepository;
import com.github.alebabai.tg2vk.service.LinkerService;
import com.github.alebabai.tg2vk.service.TelegramService;
import com.github.alebabai.tg2vk.service.TemplateRenderer;
import com.github.alebabai.tg2vk.util.Tg2vkMapperUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.pengrad.telegrambot.model.request.ParseMode;
import com.pengrad.telegrambot.request.*;
import com.vk.api.sdk.objects.audio.AudioFull;
import com.vk.api.sdk.objects.docs.Doc;
import com.vk.api.sdk.objects.messages.Message;
import com.vk.api.sdk.objects.messages.MessageAttachment;
import com.vk.api.sdk.objects.users.User;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.data.util.ReflectionUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

@Service
public class LinkerServiceImpl implements LinkerService {

    private static final Logger LOGGER = LoggerFactory.getLogger(LinkerServiceImpl.class);
    private static final String PRIVATE_MESSAGE_TEMPLATE = "telegram/private_message.html";
    private static final String GROUP_MESSAGE_TEMPLATE = "telegram/group_message.html";

    private final UserRepository userRepository;
    private final TelegramService tgService;
    private final TemplateRenderer templateRenderer;
    private final MessageSourceAccessor messages;
    private final OkHttpClient httpClient;
    private final Gson gson;

    @Autowired
    public LinkerServiceImpl(UserRepository userRepository,
                             TelegramService tgService,
                             TemplateRenderer templateRenderer,
                             MessageSource messageSource) {
        this.userRepository = userRepository;
        this.tgService = tgService;
        this.templateRenderer = templateRenderer;
        this.messages = new MessageSourceAccessor(messageSource);
        this.httpClient = new OkHttpClient.Builder().build();
        this.gson = new GsonBuilder().create();
    }

    @Override
    public BiConsumer<User, Message> getVkMessageHandler(Integer userId) {
        return (profile, message) -> {
            try {
                final Integer vkChatId = Tg2vkMapperUtils.getVkChatId(message);
                Optional.ofNullable(userRepository.findOne(userId))
                        .map(user -> Optional.of(user.getChatsSettings().stream()
                                .filter(chatSettings -> Objects.equals(chatSettings.getVkChatId(), vkChatId))
                                .collect(toList()))
                                .filter(chatSettings -> !chatSettings.isEmpty())
                                .orElseGet(() -> Collections.singletonList(new ChatSettings()
                                        .setTgChatId(user.getTgId())
                                        .setVkChatId(vkChatId)
                                        .setStarted(true))))
                        .filter(chatSettings -> chatSettings.stream().allMatch(ChatSettings::isStarted))
                        .map(chatSettings -> chatSettings.stream().map(ChatSettings::getTgChatId))
                        .ifPresent(chatSettingsStream -> chatSettingsStream.forEach(getMainHandler(message, profile)));
            } catch (Exception e) {
                LOGGER.error("Error during vk message handling: ", e);
            }
        };
    }
{'fraction_non_alphanumeric': 0.092929044930027, 'fraction_numerical': 0.003682789098944267, 'mean_word_length': 2.941461054668602, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1921063', 'n_tokens_mistral': 2223, 'n_tokens_neox': 2146, 'n_words': 379}