<commit_before>import rndstr from 'rndstr';
import $ from 'cafy';
import define from '../../define';
import { Apps } from '../../../../models';
import { genId } from '../../../../misc/gen-id';

export const meta = {
	tags: ['app'],

	requireCredential: false,

	params: {
		name: {
			validator: $.str
		},

		description: {
			validator: $.str
		},

		permission: {
			validator: $.arr($.str).unique()
		},

		// TODO: Check it is valid url
		callbackUrl: {
			validator: $.optional.nullable.str,
			default: null as any
		},
	}
};

export default define(meta, async (ps, user) => {
	// Generate secret
	const secret = rndstr('a-zA-Z0-9', 32);

	// Create account
	const app = await Apps.save({
		id: genId(),
		createdAt: new Date(),
		userId: user && user.id,
		name: ps.name,
		description: ps.description,
		permission: ps.permission,
		callbackUrl: ps.callbackUrl,
		secret: secret
	});

	return await Apps.pack(app, null, {
		detail: true,
		includeSecret: true
	});
});<commit_msg>refactor<commit_after>import rndstr from 'rndstr';
import $ from 'cafy';
import define from '../../define';
import { Apps } from '../../../../models';
import { genId } from '../../../../misc/gen-id';

export const meta = {
	tags: ['app'],

	requireCredential: false,

	params: {
		name: {
			validator: $.str
		},

		description: {
			validator: $.str
		},

		permission: {
			validator: $.arr($.str).unique()
		},

		// TODO: Check it is valid url
		callbackUrl: {
			validator: $.optional.nullable.str,
			default: null as any
		},
	}
};

export default define(meta, async (ps, user) => {
	// Generate secret
	const secret = rndstr('a-zA-Z0-9', 32);

	// Create account
	const app = await Apps.save({
		id: genId(),
		createdAt: new Date(),
		userId: user ? user.id : null,
		name: ps.name,
		description: ps.description,
		permission: ps.permission,
		callbackUrl: ps.callbackUrl,
		secret: secret
	});

	return await Apps.pack(app, null, {
		detail: true,
		includeSecret: true
	});
});
{'fraction_non_alphanumeric': 0.18292066700353715, 'fraction_numerical': 0.00404244567963618, 'mean_word_length': 2.9207920792079207, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2874171', 'n_tokens_mistral': 748, 'n_tokens_neox': 679, 'n_words': 187}