<commit_before>import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';
import _ from 'underscore';

import { API } from '../../../../api';
import { findGuest, findRoom } from '../lib/livechat';
import { Livechat } from '../../lib/Livechat';

API.v1.addRoute('livechat/page.visited', {
	post() {
		try {
			check(this.bodyParams, {
				token: String,
				rid: String,
				pageInfo: Match.ObjectIncluding({
					change: String,
					title: String,
					location: Match.ObjectIncluding({
						href: String,
					}),
				}),
			});

			const { token, rid, pageInfo } = this.bodyParams;

			const guest = findGuest(token);
			if (!guest) {
				throw new Meteor.Error('invalid-token');
			}

			const room = findRoom(token, rid);
			if (!room) {
				throw new Meteor.Error('invalid-room');
			}

			const obj = Livechat.savePageHistory(token, rid, pageInfo);
			if (obj) {
				const page = _.pick(obj, 'msg', 'navigation');
				return API.v1.success({ page });
			}

			return API.v1.success();
		} catch (e) {
			return API.v1.failure(e);
		}
	},
});<commit_msg>[FIX] Allow storing the navigation history of unregistered Livechat visitors (#14970)<commit_after>import { Match, check } from 'meteor/check';
import _ from 'underscore';

import { API } from '../../../../api';
import { Livechat } from '../../lib/Livechat';

API.v1.addRoute('livechat/page.visited', {
	post() {
		try {
			check(this.bodyParams, {
				token: String,
				rid: Match.Maybe(String),
				pageInfo: Match.ObjectIncluding({
					change: String,
					title: String,
					location: Match.ObjectIncluding({
						href: String,
					}),
				}),
			});

			const { token, rid, pageInfo } = this.bodyParams;
			const obj = Livechat.savePageHistory(token, rid, pageInfo);
			if (obj) {
				const page = _.pick(obj, 'msg', 'navigation');
				return API.v1.success({ page });
			}

			return API.v1.success();
		} catch (e) {
			return API.v1.failure(e);
		}
	},
});
{'fraction_non_alphanumeric': 0.1743353783231084, 'fraction_numerical': 0.0066462167689161555, 'mean_word_length': 2.898406374501992, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2388530', 'n_tokens_mistral': 809, 'n_tokens_neox': 648, 'n_words': 156}