<commit_before>###
# Add a setting
# @param {String} _id
# @param {Mixed} value
# @param {Object} setting
###
RocketChat.settings.add = (_id, value, options = {}) ->
	if not _id or not value?
		return false

	# console.log '[functions] RocketChat.settings.add -> '.green, 'arguments:', arguments

	if process?.env?[_id]?
		value = process.env[_id]
	else if Meteor.settings?[_id]?
		value = Meteor.settings[_id]

	updateSettings =
		i18nLabel: options.i18nLabel or _id

	# default description i18n key will be the setting name + "_Description"
	# (eg: LDAP_Enable -> LDAP_Enable_Description)
	updateSettings.i18nDescription = if options.i18nDescription?
		options.i18nDescription
	else
		"#{_id}_Description"
	updateSettings.type = options.type if options.type
	updateSettings.multiline = options.multiline if options.multiline
	updateSettings.group = options.group if options.group
	updateSettings.section = options.section if options.section
	updateSettings.public = options.public if options.public
	updateSettings.placeholder = options.placeholder if options.placeholder

	upsertChanges = { $setOnInsert: { value: value }, $set: updateSettings }

	if options.persistent is true
		upsertChanges.$unset = { ts: true }
	else
		upsertChanges.$set.ts = new Date

	return RocketChat.models.Settings.upsert { _id: _id }, upsertChanges


###
# Add a setting group
# @param {String} _id
###
RocketChat.settings.addGroup = (_id, options = {}) ->
	if not _id
		return false

	# console.log '[functions] RocketChat.settings.addGroup -> '.green, 'arguments:', arguments

	updateSettings =
		type: 'group'
		i18nLabel: options.i18nLabel or _id

	updateSettings.i18nDescription = if options.i18nDescription?
		options.i18nDescription
	else
		"#{_id}_Description"

	upsertChanges = { $set: updateSettings }
	if options.persistent is true
		upsertChanges.$unset = { ts: true }
	else
		upsertChanges.$set.ts = new Date

	return RocketChat.models.Settings.upsert { _id: _id }, upsertChanges


###
# Remove a setting by id
# @param {String} _id
###
RocketChat.settings.removeById = (_id) ->
	if not _id
		return false

	# console.log '[functions] RocketChat.settings.add -> '.green, 'arguments:', arguments

	return RocketChat.models.Settings.removeById _id


###
# Update a setting by id
# @param {String} _id
###
RocketChat.settings.updateById = (_id, value) ->
	RocketChat.models.Settings.updateValueById _id, value


Meteor.methods
	saveSetting: (_id, value) ->
		console.log '[method] saveSetting', _id, value
		if Meteor.userId()?
			user = Meteor.users.findOne Meteor.userId()

		unless RocketChat.authz.hasPermission(Meteor.userId(), 'edit-privileged-setting') is true
			throw new Meteor.Error 503, 'Not authorized'

		# console.log "saveSetting -> ".green, _id, value
		RocketChat.settings.updateById _id, value
		return true<commit_msg>storing settings value source information<commit_after>###
# Add a setting
# @param {String} _id
# @param {Mixed} value
# @param {Object} setting
###
RocketChat.settings.add = (_id, value, options = {}) ->
	if not _id or not value?
		return false

	# console.log '[functions] RocketChat.settings.add -> '.green, 'arguments:', arguments

	options.packageValue = value
	options.valueSource = 'packageValue'

	if process?.env?[_id]?
		value = process.env[_id]
		options.processEnvValue = value
		options.valueSource = 'processEnvValue'

	else if Meteor.settings?[_id]?
		value = Meteor.settings[_id]
		options.meteorSettingsValue = value
		options.valueSource = 'meteorSettingsValue'

	unless options.i18nLabel? options.i18nLabel = _id

	# Default description i18n key will be the setting name + "_Description" (eg: LDAP_Enable -> LDAP_Enable_Description)
	unless options.i18nDescription? options.i18nDescription = "#{_id}_Description"

	upsertChanges =
		$set: options
		$setOnInsert:
			value: value
			createdAt: new Date

	if options.persistent is true
		upsertChanges.$unset = { ts: true }
	else
		upsertChanges.$set.ts = new Date

	return RocketChat.models.Settings.upsert { _id: _id }, upsertChanges


###
# Add a setting group
# @param {String} _id
###
RocketChat.settings.addGroup = (_id, options = {}) ->
	if not _id
		return false

	# console.log '[functions] RocketChat.settings.addGroup -> '.green, 'arguments:', arguments

	setting =
		type: 'group'
		i18nLabel: options.i18nLabel or _id

	setting.i18nDescription = if options.i18nDescription?
		options.i18nDescription
	else
		"#{_id}_Description"

	upsertChanges = { $set: setting }
	if options.persistent is true
		upsertChanges.$unset = { ts: true }
	else
		upsertChanges.$set.ts = new Date

	return RocketChat.models.Settings.upsert { _id: _id }, upsertChanges


###
# Remove a setting by id
# @param {String} _id
###
RocketChat.settings.removeById = (_id) ->
	if not _id
		return false

	# console.log '[functions] RocketChat.settings.add -> '.green, 'arguments:', arguments

	return RocketChat.models.Settings.removeById _id


###
# Update a setting by id
# @param {String} _id
###
RocketChat.settings.updateById = (_id, value) ->
	if not _id
		return false

	return RocketChat.models.Settings.updateValueById _id, value


Meteor.methods
	saveSetting: (_id, value) ->
		console.log '[method] saveSetting', _id, value
		if Meteor.userId()?
			user = Meteor.users.findOne Meteor.userId()

		unless RocketChat.authz.hasPermission(Meteor.userId(), 'edit-privileged-setting') is true
			throw new Meteor.Error 503, 'Not authorized'

		# console.log "saveSetting -> ".green, _id, value
		RocketChat.settings.updateById _id, value
		return true
{'fraction_non_alphanumeric': 0.11906052393857272, 'fraction_numerical': 0.008672086720867209, 'mean_word_length': 4.427450980392157, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '5447174', 'n_tokens_mistral': 1928, 'n_tokens_neox': 1785, 'n_words': 469}