<commit_before>package com.kylemsguy.tcasmobile.backend;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MessageManager {
    private static final String MESSAGES_URL = SessionManager.BASE_URL + "messages/";
    private static final String COMPOSE_URL = MESSAGES_URL + "compose/";

    private static final String RECIPIENTS_KEY = "to";
    private static final String TITLE_KEY = "title";
    private static final String CONTENT_KEY = "body";
    private static final String USER_NAME = "You";

    private SessionManager sm;

    public MessageManager(SessionManager sm) {
        this.sm = sm;
    }

    /**
     * Gets the threads on a specified page.
     * Temporary until a proper API is available
     *
     * @param page   Message page
     * @param folder Message folder. If null then inbox
     * @return List of MessageThreads
     */
    public List<MessageThread> getThreads(int page, String folder) throws Exception {
        String urlPage;
        if (folder == null || folder.isEmpty()) {
            urlPage = MESSAGES_URL + "page" + page + "/";
        } else {
            urlPage = MESSAGES_URL + "folder/" + folder + "/" + page + "/";
        }

        String html = sm.getPageContent(urlPage);

        if (html.matches("<!DOCTYPE html>.*This folder is currently empty\\..*"))
            return null; // folder is empty

        List<MessageThread> threads = new ArrayList<>();

        Document dom = Jsoup.parse(html);

        // TODO use an HTML class
        // TEMPORARY!!!!!! ONLY UNTIL PROPER API IS AVAILABLE
        Elements messages = dom.getElementsByAttributeValueMatching("style", "background-color:(#fff|#eee);");
        for (Element e : messages) {
            if (e.tagName().equals("tr")) {
                // needed parameters for creating a Message object
                int id;
                String title;
                List<String> users = new ArrayList<>();
                String lastMessage;
                double timeReceivedOffset;

                // Find the title, and id from the second column of list
                Element titleElement = e.child(1).child(0).child(0);
                if (!titleElement.tagName().equals("a"))
                    continue; // wrong tag
                String[] splitMessageUrl = titleElement.attr("href").split("/");
                id = Integer.parseInt(splitMessageUrl[splitMessageUrl.length - 1]);
                title = titleElement.text();

                // Find the summary of the last message received from the second column of list
                Element messageElement = e.child(1).child(1);
                if (!(messageElement.tagName().equals("div") && messageElement.attr("style").equals("font-size:11px; color:#888;")))
                    continue; // what the heck?
                lastMessage = messageElement.text();

                // find the time message was sent
                Element offsetElement = e.child(3);
                Pattern pattern = Pattern.compile("(\\d+(\\.\\d+)?) days? ago");
                Matcher matcher = pattern.matcher(offsetElement.text());
                if (!matcher.find())
                    continue; // something is wrong... can't find something that should be there...
                timeReceivedOffset = Double.parseDouble(matcher.group(1));

                // Finally get users involved
                Elements usersElements = e.child(4).children();
                for (Element userElement : usersElements) {
                    if (userElement.tagName().equals("a") && userElement.attr("href").startsWith("/users/")) {
                        users.add(userElement.text());
                        break;
                    }
                }

                // now check if the only recipient is ???
                if (users.isEmpty())
                    users.add("???");

                // got all the data we need! time to make MessageThread!
                MessageThread thread = new MessageThread(id, title, users, lastMessage, timeReceivedOffset);
                threads.add(thread);
            }
        }

        return threads;
    }


    /**
     * Creates a new message thread<commit_msg>Fixed getting users in MessageThread<commit_after>package com.kylemsguy.tcasmobile.backend;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MessageManager {
    private static final String MESSAGES_URL = SessionManager.BASE_URL + "messages/";
    private static final String COMPOSE_URL = MESSAGES_URL + "compose/";

    private static final String RECIPIENTS_KEY = "to";
    private static final String TITLE_KEY = "title";
    private static final String CONTENT_KEY = "body";
    private static final String USER_NAME = "You";

    private SessionManager sm;

    public MessageManager(SessionManager sm) {
        this.sm = sm;
    }

    /**
     * Gets the threads on a specified page.
     * Temporary until a proper API is available
     *
     * @param page   Message page
     * @param folder Message folder. If null then inbox
     * @return List of MessageThreads
     */
    public List<MessageThread> getThreads(int page, String folder) throws Exception {
        String urlPage;
        if (folder == null || folder.isEmpty()) {
            urlPage = MESSAGES_URL + "page" + page + "/";
        } else {
            urlPage = MESSAGES_URL + "folder/" + folder + "/" + page + "/";
        }

        String html = sm.getPageContent(urlPage);

        if (html.matches("<!DOCTYPE html>.*This folder is currently empty\\..*"))
            return null; // folder is empty

        List<MessageThread> threads = new ArrayList<>();

        Document dom = Jsoup.parse(html);

        // TODO use an HTML class
        // TEMPORARY!!!!!! ONLY UNTIL PROPER API IS AVAILABLE
        Elements messages = dom.getElementsByAttributeValueMatching("style", "background-color:(#fff|#eee);");
        for (Element e : messages) {
            if (e.tagName().equals("tr")) {
                // needed parameters for creating a Message object
                int id;
                String title;
                List<String> users = new ArrayList<>();
                String lastMessage;
                double timeReceivedOffset;

                // Find the title, and id from the second column of list
                Element titleElement = e.child(1).child(0).child(0);
                if (!titleElement.tagName().equals("a"))
                    continue; // wrong tag
                String[] splitMessageUrl = titleElement.attr("href").split("/");
                id = Integer.parseInt(splitMessageUrl[splitMessageUrl.length - 1]);
                title = titleElement.text();

                // Find the summary of the last message received from the second column of list
                Element messageElement = e.child(1).child(1);
                if (!(messageElement.tagName().equals("div") && messageElement.attr("style").equals("font-size:11px; color:#888;")))
                    continue; // what the heck?
                lastMessage = messageElement.text();

                // find the time message was sent
                Element offsetElement = e.child(3);
                Pattern pattern = Pattern.compile("(\\d+(\\.\\d+)?) days? ago");
                Matcher matcher = pattern.matcher(offsetElement.text());
                if (!matcher.find())
                    continue; // something is wrong... can't find something that should be there...
                timeReceivedOffset = Double.parseDouble(matcher.group(1));

                // Finally get users involved
                Elements usersElements = e.child(4).children();
                for (Element userElement : usersElements) {
                    if (userElement.tagName().equals("a") && userElement.attr("href").startsWith("/users/")) {
                        users.add(userElement.text());
                    }
                }

                // now check if the only recipient is ???
                if (users.isEmpty())
                    users.add("???");

                // got all the data we need! time to make MessageThread!
                MessageThread thread = new MessageThread(id, title, users, lastMessage, timeReceivedOffset);
                threads.add(thread);
            }
        }

        return threads;
    }


    /**
     * Creates a new message thread
{'fraction_non_alphanumeric': 0.11124803943535738, 'fraction_numerical': 0.003136903428187318, 'mean_word_length': 1.9876171352074967, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, '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': '3873628', 'n_tokens_mistral': 2349, 'n_tokens_neox': 2185, 'n_words': 654}