<commit_before>import userUtils from "../lib/user-utils";
import objUtils from "../lib/obj-utils";
import rangeOps from "../lib/range-ops";

const state = {
	nav: {
		mode: "loading",
		room: null,
		textRange: {
			time: null,
			after: 0,
			before: 20
		},
		threadRange: {
			time: null,
			after: 0,
			before: 20
		}
	},
	session: "",
	user: "",
	notes: [],
	texts: {},
	threads: {},
	entities: {},
	context: {},
	app: {
		listeningRooms: [],
		nearByRooms: [],
		connectionStatus: "connecting"
	},
	indexes: {
		threadsById: {},
		textsById: {},
		roomUsers: {},
		userRooms: {}
	}
};

function Store(objs) {
	// Handle situation where called without "new" keyword
	if ((this instanceof Store) === false) {
		throw new Error("Must be initialized before use");
	}

	// Throw error if not given an array as argument
	if (!Array.isArray(objs)) {
		throw new Error("Invalid array " + objs);
	}

	this._objs = objs;
}

Store.prototype.get = function(...args) {
	let value, arr;

	for (let i = this._objs.length, l = 0; i > l; i--) {
		arr = args.slice(0);

		arr.unshift(this._objs[i - 1]);

		value = objUtils.get.apply(null, arr);

		if (typeof value !== "undefined") {
			return value;
		}
	}
};

Store.prototype.with = function(obj) {
	const objs = this._objs.slice(0);

	objs.push(obj);

	return new Store(objs);
};

Store.prototype.getEntity = function(id) {
	return this.get("entities", id);
};

Store.prototype.getUser = function(id) {
	const userObj = this.getEntity(id || this.get("user"));

	if (typeof userObj === "object") {
		if (userObj.type === "user") {
			return userObj;
		}
	} else {
		return userObj;
	}
};

Store.prototype.getRoom = function(id) {
	const roomObj = this.getEntity(id || this.get("nav", "room"));

	if (typeof roomObj === "object") {
		if (roomObj.type === "room") {
			return roomObj;
		}
	} else {
		return roomObj;
	}
};

Store.prototype.getTexts = function(roomId, threadId, time, range) {
	const req = { time: time || null };
	const key = roomId + (threadId ? "_" + threadId : "");
	const texts = this.get("texts");

	if (range < 0) {
		req.before = range * -1;
	} else {
		req.after = range;
	}

	if (!(texts && texts[key])) {
		if (this.get("app", "connectionStatus") !== "online") {
			return [ "failed" ];
		} else {
			return [ "missing" ];
		}
	}

	return rangeOps.getItems(texts[key], req, "time");
};

Store.prototype.getThreads = function(roomId, time, range) {
	const req = { startTime: time || null };
	const threads = this.get("threads");

	if (range < 0) {
		req.before = range * -1;
	} else {
		req.after = range;
	}

	if (!(threads && threads[roomId])) {
		if (this.get("app", "connectionStatus") !== "online") {
			return [ "failed" ];
		} else {
			return [ "missing" ];
		}
	}

	return rangeOps.getItems(threads[roomId], req, "startTime");
};

Store.prototype.getThreadById = function(threadId) {
	return this.get("indexes", "threadsById", threadId);
};<commit_msg>Don't return hidden/abusive threads/texts from store<commit_after>import userUtils from "../lib/user-utils";
import objUtils from "../lib/obj-utils";
import rangeOps from "../lib/range-ops";

const state = {
	nav: {
		mode: "loading",
		room: null,
		textRange: {
			time: null,
			after: 0,
			before: 20
		},
		threadRange: {
			time: null,
			after: 0,
			before: 20
		}
	},
	session: "",
	user: "",
	notes: [],
	texts: {},
	threads: {},
	entities: {},
	context: {},
	app: {
		listeningRooms: [],
		nearByRooms: [],
		connectionStatus: "connecting"
	},
	indexes: {
		threadsById: {},
		textsById: {},
		roomUsers: {},
		userRooms: {}
	}
};

function Store(objs) {
	// Handle situation where called without "new" keyword
	if ((this instanceof Store) === false) {
		throw new Error("Must be initialized before use");
	}

	// Throw error if not given an array as argument
	if (!Array.isArray(objs)) {
		throw new Error("Invalid array " + objs);
	}

	this._objs = objs;
}

Store.prototype.get = function(...args) {
	let value, arr;

	for (let i = this._objs.length, l = 0; i > l; i--) {
		arr = args.slice(0);

		arr.unshift(this._objs[i - 1]);

		value = objUtils.get.apply(null, arr);

		if (typeof value !== "undefined") {
			return value;
		}
	}
};

Store.prototype.with = function(obj) {
	const objs = this._objs.slice(0);

	objs.push(obj);

	return new Store(objs);
};

Store.prototype.getEntity = function(id) {
	return this.get("entities", id);
};

Store.prototype.getUser = function(id) {
	const userObj = this.getEntity(id || this.get("user"));

	if (typeof userObj === "object") {
		if (userObj.type === "user") {
			return userObj;
		}
	} else {
		return userObj;
	}
};

Store.prototype.getRoom = function(id) {
	const roomObj = this.getEntity(id || this.get("nav", "room"));

	if (typeof roomObj === "object") {
		if (roomObj.type === "room") {
			return roomObj;
		}
	} else {
		return roomObj;
	}
};

Store.prototype.getTexts = function(roomId, threadId, time, range) {
	const req = { time: time || null };
	const key = roomId + (threadId ? "_" + threadId : "");
	const texts = this.get("texts");

	if (range < 0) {
		req.before = range * -1;
	} else {
		req.after = range;
	}

	if (!(texts && texts[key])) {
		if (this.get("app", "connectionStatus") !== "online") {
			return [ "failed" ];
		} else {
			return [ "missing" ];
		}
	}

	let data = rangeOps.getItems(texts[key], req, "time");

	if (!this.isUserAdmin()) {
		data = data.filter(text => {
			const { tags } = text;

			if (Array.isArray(tags)) {
				if (tags.includes("hidden") || tags.includes("abusive")) {
					return false;
				}
			}

			return true;
		});
	}

	return data;
};

Store.prototype.getThreads = function(roomId, time, range) {
	const req = { startTime: time || null };
	const threads = this.get("threads");

	if (range < 0) {
		req.before = range * -1;
	} else {
		req.after = range;
	}

	if (!(threads && threads[roomId])) {
		if (this.get("app", "connectionStatus") !== "online") {
			return [ "failed" ];
		} else {
			return [ "missing" ];
		}
	}

	let data = rangeOps.getItems(threads[roomId], req, "startTime");

	if (!this.isUserAdmin()) {
		data = data.filter(thread => {
			const { tags } = thread;

			if (Array.isArray(tags)) {
				if (tags.includes("thread-hidden") || tags.includes("hidden") || tags.includes("abusive")) {
					return false;
				}
			}

			return true;
		});
	}

	return data;
};

Store.prototype.getThreadById = function(threadId) {
	return this.get("indexes", "threadsById", threadId);
};
{'fraction_non_alphanumeric': 0.18770377270610153, 'fraction_numerical': 0.004347151063499456, 'mean_word_length': 3.3119143239625166, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '468831', 'n_tokens_mistral': 2545, 'n_tokens_neox': 2329, 'n_words': 550}