<commit_before>/* global Module */

/* Magic Mirror
 * Module: MMM-Todoist
 *
 * By <NAME>
 *
 * MIT Licensed.
 */

Module.register("MMM-Todoist", {

	defaults: {
		maximumEntries: 10,
		projects: ["inbox"],
		updateInterval: 10 * 60 * 1000, // every 10 minutes,
		fade: true,
		fadePoint: 0.25,
		sortType: "todoist",
		showProject: true,
		projectColors: ["#95ef63", "#ff8581", "#ffc471", "#f9ec75", "#a8c8e4", "#d2b8a3", "#e2a8e4", "#cccccc", "#fb886e",
			"#ffcc00", "#74e8d3", "#3bd5fb", "#dc4fad", "#ac193d", "#d24726", "#82ba00", "#03b3b2", "#008299",
			"#5db2ff", "#0072c6", "#000000", "#777777"
		], //These colors come from Todoist and their order matters if you want the colors to match your Todoist project colors.

		//This has been designed to use the Todoist Sync API.
		apiVersion: "v7",
		apiBase: "https://todoist.com/api",
		todoistEndpoint: "sync",
		todoistResourceType: "[\"items\", \"projects\"]"
	},

	// Define required scripts.
	getStyles: function() {
		return ["MMM-Todoist.css"];
	},
	getTranslations: function() {
		return {
			en: "translations/en.json",
			de: "translations/de.json"
		};
	},

	start: function() {
		var self = this;
		Log.info("Starting module: " + this.name);

		if (this.config.accessToken === "") {
			Log.error("MMM-Todoist: AccessToken not set!");
			return;
		}

		//Support legacy properties
		if (this.config.lists !== undefined) {
			if (this.config.lists.length > 0) {
				this.config.projects = this.config.lists;
			}
		}

		this.sendSocketNotification("FETCH_TODOIST", this.config);

		setInterval(function () {
			self.sendSocketNotification("FETCH_TODOIST", self.config);
		}, this.config.updateInterval);
	},


	// Override socket notification handler.
	socketNotificationReceived: function(notification, payload) {
		if (notification === "TASKS") {
			this.filterTodoistData(payload);
			this.updateDom(1000);
		}else if (notification === "FETCH_ERROR") {
			Log.error("Todoist Error. Could not fetch todos: " + payload.error);
		}
	},

	filterTodoistData: function(tasks){
		var self = this;
		var items = [];

		if (tasks != undefined) {
			if (tasks.items != undefined) {

				//Filter the Todos by the Projects specified in the Config
				tasks.items.forEach(function(item) {
					self.config.projects.forEach(function(project) {
						if (item.project_id == project) {
							items.push(item);
						}
					});
				});

				//Used for ordering by date
				items.forEach(function(item) {
					if (item.due_date_utc === null) {
						item.due_date_utc = "Fri 31 Dec 2100 23:59:59 +0000";
					}
					//Not used right now
					item.ISOString = new Date(item.due_date_utc.substring(4, 15).concat(item.due_date_utc.substring(15, 23))).toISOString();
				});


				//***** Sorting code if you want to add new methods. */
				switch (self.config.sortType) {
				case "todoist":
					sorteditems = self.sortByTodoist(items);
					break;
				case "dueDateAsc":
					sorteditems = self.sortByDueDateAsc(items);
					break;
				case "dueDateDesc":
					sorteditems = self.sortByDueDateDesc(items);
					break;
				default:
					sorteditems = self.sortByTodoist(items);
					break;
				}

				//Slice by max Entries
				items = items.slice(0, this.config.maximumEntries);

				this.tasks =  {"items" : items, "projects" : tasks.projects};
			}
		}
	},
	sortByTodoist: function(itemstoSort) {
		itemstoSort.sort(function(a, b) {<commit_msg>Fixes #23, Set all_day = true when no due date is set<commit_after>/* global Module */

/* Magic Mirror
 * Module: MMM-Todoist
 *
 * By <NAME>
 *
 * MIT Licensed.
 */

Module.register("MMM-Todoist", {

	defaults: {
		maximumEntries: 10,
		projects: ["inbox"],
		updateInterval: 10 * 60 * 1000, // every 10 minutes,
		fade: true,
		fadePoint: 0.25,
		sortType: "todoist",
		showProject: true,
		projectColors: ["#95ef63", "#ff8581", "#ffc471", "#f9ec75", "#a8c8e4", "#d2b8a3", "#e2a8e4", "#cccccc", "#fb886e",
			"#ffcc00", "#74e8d3", "#3bd5fb", "#dc4fad", "#ac193d", "#d24726", "#82ba00", "#03b3b2", "#008299",
			"#5db2ff", "#0072c6", "#000000", "#777777"
		], //These colors come from Todoist and their order matters if you want the colors to match your Todoist project colors.

		//This has been designed to use the Todoist Sync API.
		apiVersion: "v7",
		apiBase: "https://todoist.com/api",
		todoistEndpoint: "sync",
		todoistResourceType: "[\"items\", \"projects\"]"
	},

	// Define required scripts.
	getStyles: function() {
		return ["MMM-Todoist.css"];
	},
	getTranslations: function() {
		return {
			en: "translations/en.json",
			de: "translations/de.json"
		};
	},

	start: function() {
		var self = this;
		Log.info("Starting module: " + this.name);

		if (this.config.accessToken === "") {
			Log.error("MMM-Todoist: AccessToken not set!");
			return;
		}

		//Support legacy properties
		if (this.config.lists !== undefined) {
			if (this.config.lists.length > 0) {
				this.config.projects = this.config.lists;
			}
		}

		this.sendSocketNotification("FETCH_TODOIST", this.config);

		setInterval(function () {
			self.sendSocketNotification("FETCH_TODOIST", self.config);
		}, this.config.updateInterval);
	},


	// Override socket notification handler.
	socketNotificationReceived: function(notification, payload) {
		if (notification === "TASKS") {
			this.filterTodoistData(payload);
			this.updateDom(1000);
		}else if (notification === "FETCH_ERROR") {
			Log.error("Todoist Error. Could not fetch todos: " + payload.error);
		}
	},

	filterTodoistData: function(tasks){
		var self = this;
		var items = [];

		if (tasks != undefined) {
			if (tasks.items != undefined) {

				//Filter the Todos by the Projects specified in the Config
				tasks.items.forEach(function(item) {
					self.config.projects.forEach(function(project) {
						if (item.project_id == project) {
							items.push(item);
						}
					});
				});

				//Used for ordering by date
				items.forEach(function(item) {
					if (item.due_date_utc === null) {
						item.due_date_utc = "Fri 31 Dec 2100 23:59:59 +0000";
						item.all_day = true;
					}
					//Not used right now
					item.ISOString = new Date(item.due_date_utc.substring(4, 15).concat(item.due_date_utc.substring(15, 23))).toISOString();
				});


				//***** Sorting code if you want to add new methods. */
				switch (self.config.sortType) {
				case "todoist":
					sorteditems = self.sortByTodoist(items);
					break;
				case "dueDateAsc":
					sorteditems = self.sortByDueDateAsc(items);
					break;
				case "dueDateDesc":
					sorteditems = self.sortByDueDateDesc(items);
					break;
				default:
					sorteditems = self.sortByTodoist(items);
					break;
				}

				//Slice by max Entries
				items = items.slice(0, this.config.maximumEntries);

				this.tasks =  {"items" : items, "projects" : tasks.projects};
			}
		}
	},
	sortByTodoist: function(itemstoSort) {
		itemstoSort.sort(function(a, b) {
{'fraction_non_alphanumeric': 0.1598951507208388, 'fraction_numerical': 0.03553225571574196, 'mean_word_length': 3.3689567430025447, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 7, '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': '710822', 'n_tokens_mistral': 2973, 'n_tokens_neox': 2386, 'n_words': 538}