<commit_before>/*
 * Handles the brunt of the post-related websocket calls
 */

var $ = require('jquery'),
	common = require('../common'),
	main = require('./main'),
	posts = require('./posts'),
	state = require('./state');

// The actual object of handler functions for websocket calls
var dispatcher = main.dispatcher;

dispatcher[common.INSERT_POST] = function(msg) {
	// TODO: msg[0] is legacy. Could use a fix after we shift in the new client
	msg = msg[1];
	const isThread = !msg.op;
	if (isThread)
		state.syncs[msg.num] = 1;
	msg.editing = true;

	// Did I create this post?
	var el;
	const msgNonce = msg.nonce;
	delete msg.nonce;
	const myNonce = posts.nonce.get_nonces()[msgNonce];
	var bump = state.page.get('live');
	if (myNonce && myNonce.tab === state.page.get('tabID')) {
		// posted in this tab; transform placeholder
		state.ownPosts[msg.num] = true;
		main.oneeSama.trigger('insertOwnPost', msg);
		main.postSM.feed('alloc', msg);
		bump = false;
		// delete only after a delay so all tabs notice that it's ours
		setTimeout(posts.nonce.destroy_nonce.bind(null, msgNonce), 10000);
		// if we've already made a placeholder for this post, use it
		if (main.postForm && main.postForm.el)
			el = main.postForm.el;
	}

	// Add to my post set
	if (myNonce) {
		msg.mine = true;
		state.mine.write(msg.num, state.mine.now());
	}

	// TODO: Bump thread to top, if not saging

	new posts[isThread ? 'Section' : 'Article']({
		// Create model
		model: new posts.models[isThread ? 'Thread' : 'Post'](msg),
		id: msg.num,
		el: el
	});

	if (isThread)
		return;
	var parent = state.posts.get(msg.op);
	if (!parent)
		return;
	parent.get('replies').push(msg.num);
	parent.trigger('shiftReplies');
};

// Move thread to the archive board
dispatcher[common.MOVE_THREAD] = function(msg) {
	msg = msg[0];
	var model = new posts.ThreadModel(msg);
	main.oneeSama.links = msg.links;
	new posts.Section({
		model: model,
		id: msg.num
	});
};

dispatcher[common.INSERT_IMAGE] = function(msg) {
	var model = state.posts.get(msg[0]);
	// Did I just upload this?
	if (main.postModel && main.postModel.get('num') == msg[0]) {
		if (model)
			model.set('image', msg[1], {silent: true});
		main.postForm.insertUploaded(msg[1]);
	}
	else if (model)
		model.set('image', msg[1]);
};

dispatcher[common.UPDATE_POST] = function(msg) {
	const num = msg[0],
		links = msg[4],
		msgState = [msg[2] || 0, msg[3] || 0];
	var extra = msg[5],
		model = state.posts.get(num);

	// TODO: Add backlinks

	if (model) {
		model.set({
			body: model.get('body') + msg[1],
			state: msgState
		});
	}

	// Am I updating my own post?
	if (num in state.ownPosts) {
		if (extra)
			extra.links = links;
		else
			extra = {links: links};
		main.oneeSama.trigger('insertOwnPost', extra);
		return;
	}

	// TODO: Make this prettier
	var bq = $('#' + num + ' > blockquote');
	if (bq.length) {
		main.oneeSama.dice = extra && extra.dice;
		main.oneeSama.links = links || {};
		main.oneeSama.callback = inject;
		main.oneeSama.buffer = bq;
		main.oneeSama.state = msgState;
		main.oneeSama.fragment(msg[1]);
	}
};

// Add various additional tags inside the blockqoute
var inject = exports.inject = function(frag) {
	var $dest = this.buffer;
	for (var i = 0; i < this.state[1]; i++)
		$dest = $dest.children('del:last');
	if (this.state[0] == common.S_QUOTE)
		$dest = $dest.children('em:last');
	if (this.strong)
		$dest = $dest.children('strong:last');
	var out = null;
	if (frag.safe) {
		var m = frag.safe.match(/^<(\w+)>$/);
		if (m)
			out = document.createElement(m[1]);
		else if (/^<\/\w+>$/.test(frag.safe))
			out = '';
	}
	if (out === null) {
		if (Array.isArray(frag))
			out = $(common.flatten(frag).join(''));
		else
			out = common.escape_fragment(frag);
	}
	if (out)
		$dest.append(out);
	return out;
};

// Make the text spoilers toggle revealing on click
var touchable_spoiler_tag = exports.touchable_spoiler_tag = function(del) {
	del.html = '<del onclick="void(0)">';
};
main.oneeSama.hook('spoilerTag', touchable_spoiler_tag);

dispatcher[common.FINISH_POST] = function(msg) {
	const num = msg[0];
	delete state.ownPosts[num];<commit_msg>Reveal spoiler tags on click<commit_after>/*
 * Handles the brunt of the post-related websocket calls
 */

var $ = require('jquery'),
	common = require('../common'),
	main = require('./main'),
	posts = require('./posts'),
	state = require('./state');

// The actual object of handler functions for websocket calls
var dispatcher = main.dispatcher;

dispatcher[common.INSERT_POST] = function(msg) {
	// TODO: msg[0] is legacy. Could use a fix after we shift in the new client
	msg = msg[1];
	const isThread = !msg.op;
	if (isThread)
		state.syncs[msg.num] = 1;
	msg.editing = true;

	// Did I create this post?
	var el;
	const msgNonce = msg.nonce;
	delete msg.nonce;
	const myNonce = posts.nonce.get_nonces()[msgNonce];
	var bump = state.page.get('live');
	if (myNonce && myNonce.tab === state.page.get('tabID')) {
		// posted in this tab; transform placeholder
		state.ownPosts[msg.num] = true;
		main.oneeSama.trigger('insertOwnPost', msg);
		main.postSM.feed('alloc', msg);
		bump = false;
		// delete only after a delay so all tabs notice that it's ours
		setTimeout(posts.nonce.destroy_nonce.bind(null, msgNonce), 10000);
		// if we've already made a placeholder for this post, use it
		if (main.postForm && main.postForm.el)
			el = main.postForm.el;
	}

	// Add to my post set
	if (myNonce) {
		msg.mine = true;
		state.mine.write(msg.num, state.mine.now());
	}

	// TODO: Bump thread to top, if not saging

	new posts[isThread ? 'Section' : 'Article']({
		// Create model
		model: new posts.models[isThread ? 'Thread' : 'Post'](msg),
		id: msg.num,
		el: el
	});

	if (isThread)
		return;
	var parent = state.posts.get(msg.op);
	if (!parent)
		return;
	parent.get('replies').push(msg.num);
	parent.trigger('shiftReplies');
};

// Move thread to the archive board
dispatcher[common.MOVE_THREAD] = function(msg) {
	msg = msg[0];
	var model = new posts.ThreadModel(msg);
	main.oneeSama.links = msg.links;
	new posts.Section({
		model: model,
		id: msg.num
	});
};

dispatcher[common.INSERT_IMAGE] = function(msg) {
	var model = state.posts.get(msg[0]);
	// Did I just upload this?
	if (main.postModel && main.postModel.get('num') == msg[0]) {
		if (model)
			model.set('image', msg[1], {silent: true});
		main.postForm.insertUploaded(msg[1]);
	}
	else if (model)
		model.set('image', msg[1]);
};

dispatcher[common.UPDATE_POST] = function(msg) {
	const num = msg[0],
		links = msg[4],
		msgState = [msg[2] || 0, msg[3] || 0];
	var extra = msg[5],
		model = state.posts.get(num);

	// TODO: Add backlinks

	if (model) {
		model.set({
			body: model.get('body') + msg[1],
			state: msgState
		});
	}

	// Am I updating my own post?
	if (num in state.ownPosts) {
		if (extra)
			extra.links = links;
		else
			extra = {links: links};
		main.oneeSama.trigger('insertOwnPost', extra);
		return;
	}

	// TODO: Make this prettier
	var bq = $('#' + num + ' > blockquote');
	if (bq.length) {
		main.oneeSama.dice = extra && extra.dice;
		main.oneeSama.links = links || {};
		main.oneeSama.callback = inject;
		main.oneeSama.buffer = bq;
		main.oneeSama.state = msgState;
		main.oneeSama.fragment(msg[1]);
	}
};

// Add various additional tags inside the blockqoute
var inject = exports.inject = function(frag) {
	var $dest = this.buffer;
	for (var i = 0; i < this.state[1]; i++)
		$dest = $dest.children('del:last');
	if (this.state[0] == common.S_QUOTE)
		$dest = $dest.children('em:last');
	if (this.strong)
		$dest = $dest.children('strong:last');
	var out = null;
	if (frag.safe) {
		var m = frag.safe.match(/^<(\w+)>$/);
		if (m)
			out = document.createElement(m[1]);
		else if (/^<\/\w+>$/.test(frag.safe))
			out = '';
	}
	if (out === null) {
		if (Array.isArray(frag))
			out = $(common.flatten(frag).join(''));
		else
			out = common.escape_fragment(frag);
	}
	if (out)
		$dest.append(out);
	return out;
};

// Make the text spoilers toggle revealing on click
main.$doc.on('click', 'del', function (event) {
	if (!event.spoilt) {
		event.spoilt = true;
		$(event.target).toggleClass('reveal');
	}
});
// For mobile
var touchable_spoiler_tag = exports.touchable_spoiler_tag = function(del) {
	del.html = '<del onclick="void(0)">';
};
main.oneeSama.hook('spoilerTag', touchable_spoiler_tag);

dispatcher[common.FINISH_POST] = function(msg) {
	const num = msg[0];
	delete state.ownPosts[num];
{'fraction_non_alphanumeric': 0.17589073634204275, 'fraction_numerical': 0.006888361045130641, 'mean_word_length': 3.6939799331103678, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '5140591', 'n_tokens_mistral': 3333, 'n_tokens_neox': 3145, 'n_words': 768}