<commit_before>const ApiContext = require('../../../modules/ApiContext');
const MongoAdapter = require('../../../modules/MongoAdapter');
const $ = require('cafy').default;

/** @param {ApiContext} apiContext */
exports.get = async (apiContext) => {
	await apiContext.proceed({
		query: {},
		permissions: ['userRead']
	});
	if (apiContext.responsed) return;

	const user = await apiContext.repository.findById('users', apiContext.params.id);
	if (user == null) {
		apiContext.response(404, 'user not found');
		return;
	}

	apiContext.response(200, { user: await apiContext.usersService.serialize(user) });
};

/** @param {ApiContext} apiContext */
exports.patch = async (apiContext) => {
	await apiContext.proceed({
		body: {
			screenName: { cafy: $().string(), default: null },
			description: { cafy: $().string().range(0, 256), default: null },
			name: { cafy: $().string().range(1, 32), default: null },
			iconFileId: { cafy: $().string().pipe(i => MongoAdapter.validateId(i)), default: null }
		},
		permissions: ['userWrite']
	});
	if (apiContext.responsed) return;

	const { screenName, name, description, iconFileId } = apiContext.body;
	const data = { };

	// アイコンを設定するときは、storageRead権限を要求する
	if (iconFileId != null && !apiContext.applicationsService.hasPermission(apiContext.application, 'storageRead')) {
		apiContext.response(403, { message: 'you do not have any permissions', details: ['storageRead'] });
		return;
	}

	const user = await apiContext.repository.findById('users', apiContext.params.id);
	if (user == null) {
		apiContext.response(404, 'user as premise not found');
		return;
	}

	if (!apiContext.user._id.equals(user._id)) {
		apiContext.response(403, 'this operation is not permitted');
		return;
	}

	// screenName
	if (screenName != null) {
		if (!apiContext.usersService.validFormatScreenName(screenName)) {
			apiContext.response(400, 'screenName is invalid format');
			return;
		}
		if (!apiContext.usersService.availableScreenName(screenName)) {
			apiContext.response(400, 'screenName is invalid');
			return;
		}
		if (!await apiContext.usersService.nonDuplicatedScreenName(screenName)) {
			apiContext.response(400, 'this screenName is already exists');
			return;
		}
		data.screenName = screenName;
	}

	// name
	if (name != null) {
		data.name = name;
	}

	// description
	if (description != null) {
		data.description = description;
	}

	// iconFileId
	if (iconFileId != null) {
		const iconFile = await apiContext.repository.findById('storageFiles', iconFileId);
		if (!iconFile.creator.id.equals(apiContext.user._id)) {
			apiContext.response(400, 'icon file must be owned');
			return;
		}<commit_msg>when icon file not found<commit_after>const ApiContext = require('../../../modules/ApiContext');
const MongoAdapter = require('../../../modules/MongoAdapter');
const $ = require('cafy').default;

/** @param {ApiContext} apiContext */
exports.get = async (apiContext) => {
	await apiContext.proceed({
		query: {},
		permissions: ['userRead']
	});
	if (apiContext.responsed) return;

	const user = await apiContext.repository.findById('users', apiContext.params.id);
	if (user == null) {
		apiContext.response(404, 'user not found');
		return;
	}

	apiContext.response(200, { user: await apiContext.usersService.serialize(user) });
};

/** @param {ApiContext} apiContext */
exports.patch = async (apiContext) => {
	await apiContext.proceed({
		body: {
			screenName: { cafy: $().string(), default: null },
			description: { cafy: $().string().range(0, 256), default: null },
			name: { cafy: $().string().range(1, 32), default: null },
			iconFileId: { cafy: $().string().pipe(i => MongoAdapter.validateId(i)), default: null }
		},
		permissions: ['userWrite']
	});
	if (apiContext.responsed) return;

	const { screenName, name, description, iconFileId } = apiContext.body;
	const data = { };

	// アイコンを設定するときは、storageRead権限を要求する
	if (iconFileId != null && !apiContext.applicationsService.hasPermission(apiContext.application, 'storageRead')) {
		apiContext.response(403, { message: 'you do not have any permissions', details: ['storageRead'] });
		return;
	}

	const user = await apiContext.repository.findById('users', apiContext.params.id);
	if (user == null) {
		apiContext.response(404, 'user as premise not found');
		return;
	}

	if (!apiContext.user._id.equals(user._id)) {
		apiContext.response(403, 'this operation is not permitted');
		return;
	}

	// screenName
	if (screenName != null) {
		if (!apiContext.usersService.validFormatScreenName(screenName)) {
			apiContext.response(400, 'screenName is invalid format');
			return;
		}
		if (!apiContext.usersService.availableScreenName(screenName)) {
			apiContext.response(400, 'screenName is invalid');
			return;
		}
		if (!await apiContext.usersService.nonDuplicatedScreenName(screenName)) {
			apiContext.response(400, 'this screenName is already exists');
			return;
		}
		data.screenName = screenName;
	}

	// name
	if (name != null) {
		data.name = name;
	}

	// description
	if (description != null) {
		data.description = description;
	}

	// iconFileId
	if (iconFileId != null) {
		const iconFile = await apiContext.repository.findById('storageFiles', iconFileId);
		if (iconFile == null) {
			apiContext.response(404, 'icon file not found');
			return;
		}

		if (!iconFile.creator.id.equals(apiContext.user._id)) {
			apiContext.response(400, 'icon file must be owned');
			return;
		}
{'fraction_non_alphanumeric': 0.1616423155169225, 'fraction_numerical': 0.013131126317736268, 'mean_word_length': 4.391824526420738, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '2825223', 'n_tokens_mistral': 1914, 'n_tokens_neox': 1737, 'n_words': 385}