2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
2021-02-28 01:18:00 +01:00
|
|
|
|
2021-06-23 11:23:03 +02:00
|
|
|
import render_confirm_deactivate_own_user from "../templates/confirm_dialog/confirm_deactivate_own_user.hbs";
|
2021-07-22 17:50:43 +02:00
|
|
|
import render_dialog_change_password from "../templates/dialog_change_password.hbs";
|
2021-02-28 01:18:00 +01:00
|
|
|
import render_settings_api_key_modal from "../templates/settings/api_key_modal.hbs";
|
|
|
|
import render_settings_custom_user_profile_field from "../templates/settings/custom_user_profile_field.hbs";
|
|
|
|
import render_settings_dev_env_email_access from "../templates/settings/dev_env_email_access.hbs";
|
|
|
|
|
|
|
|
import * as avatar from "./avatar";
|
2021-03-16 23:38:59 +01:00
|
|
|
import * as blueslip from "./blueslip";
|
2021-02-28 01:18:00 +01:00
|
|
|
import * as channel from "./channel";
|
|
|
|
import * as common from "./common";
|
2021-06-23 11:23:03 +02:00
|
|
|
import * as confirm_dialog from "./confirm_dialog";
|
2021-03-25 23:20:18 +01:00
|
|
|
import {csrf_token} from "./csrf";
|
2021-07-05 19:15:37 +02:00
|
|
|
import * as dialog_widget from "./dialog_widget";
|
2021-04-13 05:18:25 +02:00
|
|
|
import {$t_html} from "./i18n";
|
2021-02-28 01:18:00 +01:00
|
|
|
import * as overlays from "./overlays";
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
2021-02-28 01:18:00 +01:00
|
|
|
import * as people from "./people";
|
|
|
|
import * as pill_typeahead from "./pill_typeahead";
|
2021-02-28 01:19:16 +01:00
|
|
|
import * as settings_bots from "./settings_bots";
|
2021-03-21 16:46:13 +01:00
|
|
|
import * as settings_data from "./settings_data";
|
2021-02-28 01:18:00 +01:00
|
|
|
import * as settings_ui from "./settings_ui";
|
|
|
|
import * as ui_report from "./ui_report";
|
|
|
|
import * as user_pill from "./user_pill";
|
2021-06-23 20:48:49 +02:00
|
|
|
import * as user_profile from "./user_profile";
|
2021-07-28 16:00:58 +02:00
|
|
|
import {user_settings} from "./user_settings";
|
2021-02-28 01:18:00 +01:00
|
|
|
|
2021-04-23 21:27:13 +02:00
|
|
|
let password_quality; // Loaded asynchronously
|
|
|
|
|
2021-02-28 01:18:00 +01:00
|
|
|
export function update_email(new_email) {
|
2021-07-18 19:04:06 +02:00
|
|
|
const email_input = $("#change_email");
|
2017-04-06 02:28:57 +02:00
|
|
|
|
|
|
|
if (email_input) {
|
|
|
|
email_input.text(new_email);
|
|
|
|
}
|
2021-02-28 01:18:00 +01:00
|
|
|
}
|
2017-04-06 02:28:57 +02:00
|
|
|
|
2021-02-28 01:18:00 +01:00
|
|
|
export function update_full_name(new_full_name) {
|
2018-02-02 01:24:26 +01:00
|
|
|
// Arguably, this should work more like how the `update_email`
|
|
|
|
// flow works, where we update the name in the modal on open,
|
|
|
|
// rather than updating it here, but this works.
|
2021-06-17 18:42:31 +02:00
|
|
|
const full_name_input = $(".full-name-change-form input[name='full_name']");
|
2018-02-02 01:24:26 +01:00
|
|
|
if (full_name_input) {
|
|
|
|
full_name_input.val(new_full_name);
|
|
|
|
}
|
2021-02-28 01:18:00 +01:00
|
|
|
}
|
2018-02-02 01:24:26 +01:00
|
|
|
|
2021-02-28 01:18:00 +01:00
|
|
|
export function update_name_change_display() {
|
2021-03-21 16:46:13 +01:00
|
|
|
if (!settings_data.user_can_change_name()) {
|
2020-07-22 02:59:06 +02:00
|
|
|
$("#full_name").prop("disabled", true);
|
2018-03-02 21:44:14 +01:00
|
|
|
$(".change_name_tooltip").show();
|
|
|
|
} else {
|
2020-07-22 02:59:06 +02:00
|
|
|
$("#full_name").prop("disabled", false);
|
2018-03-02 21:44:14 +01:00
|
|
|
$(".change_name_tooltip").hide();
|
|
|
|
}
|
2021-02-28 01:18:00 +01:00
|
|
|
}
|
2018-03-02 21:44:14 +01:00
|
|
|
|
2021-02-28 01:18:00 +01:00
|
|
|
export function update_email_change_display() {
|
2018-03-02 21:44:14 +01:00
|
|
|
if (page_params.realm_email_changes_disabled && !page_params.is_admin) {
|
2021-06-24 15:55:14 +02:00
|
|
|
$("#change_email").prop("disabled", true);
|
2018-03-02 21:44:14 +01:00
|
|
|
$(".change_email_tooltip").show();
|
|
|
|
} else {
|
2021-06-24 15:55:14 +02:00
|
|
|
$("#change_email").prop("disabled", false);
|
2018-03-02 21:44:14 +01:00
|
|
|
$(".change_email_tooltip").hide();
|
|
|
|
}
|
2021-02-28 01:18:00 +01:00
|
|
|
}
|
2018-03-02 21:44:14 +01:00
|
|
|
|
2021-02-28 01:18:00 +01:00
|
|
|
export function update_avatar_change_display() {
|
2021-03-21 16:46:13 +01:00
|
|
|
if (!settings_data.user_can_change_avatar()) {
|
2021-06-28 13:10:46 +02:00
|
|
|
// We disable this widget by simply hiding its edit UI.
|
|
|
|
$("#user-avatar-upload-widget .image_upload_button").hide();
|
2021-06-30 11:58:46 +02:00
|
|
|
$(".user-avatar-section .settings-info-icon").show();
|
2019-04-23 04:51:04 +02:00
|
|
|
} else {
|
2021-06-28 13:10:46 +02:00
|
|
|
$("#user-avatar-upload-widget .image_upload_button").show();
|
2021-06-30 11:58:46 +02:00
|
|
|
$(".user-avatar-section .settings-info-icon").hide();
|
2019-04-23 04:51:04 +02:00
|
|
|
}
|
2021-02-28 01:18:00 +01:00
|
|
|
}
|
2019-04-23 04:51:04 +02:00
|
|
|
|
2020-04-02 09:55:16 +02:00
|
|
|
function display_avatar_upload_complete() {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#user-avatar-upload-widget .upload-spinner-background").css({visibility: "hidden"});
|
2020-07-15 21:20:36 +02:00
|
|
|
$("#user-avatar-upload-widget .image-upload-text").show();
|
2020-07-15 20:58:34 +02:00
|
|
|
$("#user-avatar-upload-widget .image-delete-button").show();
|
2020-04-02 09:55:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function display_avatar_upload_started() {
|
|
|
|
$("#user-avatar-source").hide();
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#user-avatar-upload-widget .upload-spinner-background").css({visibility: "visible"});
|
2020-07-15 21:20:36 +02:00
|
|
|
$("#user-avatar-upload-widget .image-upload-text").hide();
|
2020-07-15 20:58:34 +02:00
|
|
|
$("#user-avatar-upload-widget .image-delete-button").hide();
|
2020-04-02 09:55:16 +02:00
|
|
|
}
|
|
|
|
|
2021-04-13 05:18:25 +02:00
|
|
|
function settings_change_error(message_html, xhr) {
|
|
|
|
ui_report.error(message_html, xhr, $("#account-settings-status").expectOne());
|
2017-04-06 02:28:57 +02:00
|
|
|
}
|
|
|
|
|
2019-07-10 22:18:45 +02:00
|
|
|
function update_custom_profile_field(field, method) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let field_id;
|
2019-07-10 22:18:45 +02:00
|
|
|
if (method === channel.del) {
|
|
|
|
field_id = field;
|
|
|
|
} else {
|
|
|
|
field_id = field.id;
|
|
|
|
}
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
const spinner_element = $(
|
2021-02-03 23:23:32 +01:00
|
|
|
`.custom_user_field[data-field-id="${CSS.escape(field_id)}"] .custom-field-status`,
|
2020-07-15 00:34:28 +02:00
|
|
|
).expectOne();
|
|
|
|
settings_ui.do_settings_change(
|
|
|
|
method,
|
|
|
|
"/json/users/me/profile_data",
|
|
|
|
{data: JSON.stringify([field])},
|
|
|
|
spinner_element,
|
|
|
|
);
|
2019-07-10 22:18:45 +02:00
|
|
|
}
|
|
|
|
|
2018-06-05 12:57:02 +02:00
|
|
|
function update_user_custom_profile_fields(fields, method) {
|
|
|
|
if (method === undefined) {
|
|
|
|
blueslip.error("Undefined method in update_user_custom_profile_fields");
|
|
|
|
}
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
|
|
|
|
for (const field of fields) {
|
2019-07-10 22:18:45 +02:00
|
|
|
update_custom_profile_field(field, method);
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
2018-05-25 20:58:14 +02:00
|
|
|
}
|
|
|
|
|
2021-02-28 01:18:00 +01:00
|
|
|
export function append_custom_profile_fields(element_id, user_id) {
|
2020-02-05 14:30:59 +01:00
|
|
|
const person = people.get_by_user_id(user_id);
|
2019-04-07 17:29:53 +02:00
|
|
|
if (person.is_bot) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const all_custom_fields = page_params.custom_profile_fields;
|
|
|
|
const all_field_types = page_params.custom_profile_field_types;
|
2019-06-06 11:51:53 +02:00
|
|
|
|
2020-02-12 07:19:14 +01:00
|
|
|
const all_field_template_types = new Map([
|
|
|
|
[all_field_types.LONG_TEXT.id, "text"],
|
|
|
|
[all_field_types.SHORT_TEXT.id, "text"],
|
2021-03-24 11:41:29 +01:00
|
|
|
[all_field_types.SELECT.id, "select"],
|
2020-02-12 07:19:14 +01:00
|
|
|
[all_field_types.USER.id, "user"],
|
|
|
|
[all_field_types.DATE.id, "date"],
|
|
|
|
[all_field_types.EXTERNAL_ACCOUNT.id, "text"],
|
|
|
|
[all_field_types.URL.id, "url"],
|
|
|
|
]);
|
2018-10-24 16:32:30 +02:00
|
|
|
|
2021-01-22 22:29:08 +01:00
|
|
|
for (const field of all_custom_fields) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let field_value = people.get_custom_profile_data(user_id, field.id);
|
2021-03-24 11:41:29 +01:00
|
|
|
const is_select_field = field.type === all_field_types.SELECT.id;
|
2019-11-02 00:06:25 +01:00
|
|
|
const field_choices = [];
|
2019-06-06 11:51:53 +02:00
|
|
|
|
2018-12-31 06:41:06 +01:00
|
|
|
if (field_value === undefined || field_value === null) {
|
|
|
|
field_value = {value: "", rendered_value: ""};
|
|
|
|
}
|
2021-03-24 11:41:29 +01:00
|
|
|
if (is_select_field) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const field_choice_dict = JSON.parse(field.field_data);
|
|
|
|
for (const choice in field_choice_dict) {
|
2018-10-24 16:32:30 +02:00
|
|
|
if (choice) {
|
|
|
|
field_choices[field_choice_dict[choice].order] = {
|
|
|
|
value: choice,
|
|
|
|
text: field_choice_dict[choice].text,
|
2018-12-31 06:41:06 +01:00
|
|
|
selected: choice === field_value.value,
|
2018-10-24 16:32:30 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const html = render_settings_custom_user_profile_field({
|
2020-07-20 22:18:43 +02:00
|
|
|
field,
|
2020-02-12 07:19:14 +01:00
|
|
|
field_type: all_field_template_types.get(field.type),
|
2020-07-20 22:18:43 +02:00
|
|
|
field_value,
|
2019-06-06 11:51:53 +02:00
|
|
|
is_long_text_field: field.type === all_field_types.LONG_TEXT.id,
|
|
|
|
is_user_field: field.type === all_field_types.USER.id,
|
|
|
|
is_date_field: field.type === all_field_types.DATE.id,
|
2021-03-24 11:41:29 +01:00
|
|
|
is_select_field,
|
2020-07-20 22:18:43 +02:00
|
|
|
field_choices,
|
2018-10-24 16:32:30 +02:00
|
|
|
});
|
|
|
|
$(element_id).append(html);
|
2021-01-22 22:29:08 +01:00
|
|
|
}
|
2021-02-28 01:18:00 +01:00
|
|
|
}
|
2018-10-24 16:32:30 +02:00
|
|
|
|
2021-02-28 01:18:00 +01:00
|
|
|
export function initialize_custom_date_type_fields(element_id) {
|
2018-10-22 10:20:33 +02:00
|
|
|
$(element_id).find(".custom_user_field .datepicker").flatpickr({
|
|
|
|
altInput: true,
|
2020-06-07 06:16:34 +02:00
|
|
|
altFormat: "F j, Y",
|
|
|
|
allowInput: true,
|
|
|
|
});
|
2019-07-29 20:54:30 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
$(element_id)
|
|
|
|
.find(".custom_user_field .datepicker")
|
|
|
|
.on("mouseenter", function () {
|
|
|
|
if ($(this).val().length <= 0) {
|
|
|
|
$(this).parent().find(".remove_date").hide();
|
|
|
|
} else {
|
|
|
|
$(this).parent().find(".remove_date").show();
|
|
|
|
}
|
|
|
|
});
|
2019-07-29 20:59:33 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
$(element_id)
|
|
|
|
.find(".custom_user_field .remove_date")
|
|
|
|
.on("click", function () {
|
|
|
|
$(this).parent().find(".custom_user_field_value").val("");
|
|
|
|
});
|
2021-02-28 01:18:00 +01:00
|
|
|
}
|
2018-10-22 10:20:33 +02:00
|
|
|
|
2021-02-28 01:18:00 +01:00
|
|
|
export function initialize_custom_user_type_fields(
|
2020-07-15 00:34:28 +02:00
|
|
|
element_id,
|
|
|
|
user_id,
|
|
|
|
is_editable,
|
|
|
|
set_handler_on_update,
|
|
|
|
) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const field_types = page_params.custom_profile_field_types;
|
2020-02-03 09:42:50 +01:00
|
|
|
const user_pills = new Map();
|
2018-10-23 19:46:12 +02:00
|
|
|
|
2020-02-05 14:30:59 +01:00
|
|
|
const person = people.get_by_user_id(user_id);
|
2019-04-07 17:29:53 +02:00
|
|
|
if (person.is_bot) {
|
2020-01-16 20:37:29 +01:00
|
|
|
return user_pills;
|
2019-04-07 17:29:53 +02:00
|
|
|
}
|
|
|
|
|
2021-01-22 22:29:08 +01:00
|
|
|
for (const field of page_params.custom_profile_fields) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let field_value_raw = people.get_custom_profile_data(user_id, field.id);
|
2018-10-23 19:46:12 +02:00
|
|
|
|
2018-12-31 06:41:06 +01:00
|
|
|
if (field_value_raw) {
|
|
|
|
field_value_raw = field_value_raw.value;
|
|
|
|
}
|
|
|
|
|
2018-10-23 19:46:12 +02:00
|
|
|
// If field is not editable and field value is null, we don't expect
|
|
|
|
// pill container for that field and proceed further
|
|
|
|
if (field.type === field_types.USER.id && (field_value_raw || is_editable)) {
|
2020-07-15 00:34:28 +02:00
|
|
|
const pill_container = $(element_id)
|
2021-02-03 23:23:32 +01:00
|
|
|
.find(`.custom_user_field[data-field-id="${CSS.escape(field.id)}"] .pill-container`)
|
2020-07-15 00:34:28 +02:00
|
|
|
.expectOne();
|
2019-11-02 00:06:25 +01:00
|
|
|
const pills = user_pill.create_pills(pill_container);
|
2018-10-23 19:46:12 +02:00
|
|
|
|
|
|
|
function update_custom_user_field() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const fields = [];
|
|
|
|
const user_ids = user_pill.get_user_ids(pills);
|
2018-10-23 19:46:12 +02:00
|
|
|
if (user_ids.length < 1) {
|
|
|
|
fields.push(field.id);
|
|
|
|
update_user_custom_profile_fields(fields, channel.del);
|
|
|
|
} else {
|
|
|
|
fields.push({id: field.id, value: user_ids});
|
|
|
|
update_user_custom_profile_fields(fields, channel.patch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (field_value_raw) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const field_value = JSON.parse(field_value_raw);
|
2018-10-23 19:46:12 +02:00
|
|
|
if (field_value) {
|
2021-01-22 22:29:08 +01:00
|
|
|
for (const pill_user_id of field_value) {
|
2020-02-05 14:30:59 +01:00
|
|
|
const user = people.get_by_user_id(pill_user_id);
|
2018-10-23 19:46:12 +02:00
|
|
|
user_pill.append_user(user, pills);
|
2021-01-22 22:29:08 +01:00
|
|
|
}
|
2018-10-23 19:46:12 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-31 07:45:33 +01:00
|
|
|
|
2018-10-23 19:46:12 +02:00
|
|
|
if (is_editable) {
|
2020-07-15 01:29:15 +02:00
|
|
|
const input = pill_container.children(".input");
|
2018-10-23 19:46:12 +02:00
|
|
|
if (set_handler_on_update) {
|
2021-05-25 10:11:21 +02:00
|
|
|
const opts = {update_func: update_custom_user_field, user: true};
|
2020-07-24 18:04:15 +02:00
|
|
|
pill_typeahead.set_up(input, pills, opts);
|
2020-07-02 01:45:54 +02:00
|
|
|
pills.onPillRemove(() => {
|
2018-10-23 19:46:12 +02:00
|
|
|
update_custom_user_field();
|
|
|
|
});
|
|
|
|
} else {
|
2021-05-25 10:11:21 +02:00
|
|
|
pill_typeahead.set_up(input, pills, {user: true});
|
2018-10-23 19:46:12 +02:00
|
|
|
}
|
|
|
|
}
|
2020-01-16 20:37:29 +01:00
|
|
|
user_pills.set(field.id, pills);
|
2018-10-23 19:46:12 +02:00
|
|
|
}
|
2021-01-22 22:29:08 +01:00
|
|
|
}
|
2019-04-07 17:29:53 +02:00
|
|
|
|
2018-10-23 19:46:12 +02:00
|
|
|
return user_pills;
|
2021-02-28 01:18:00 +01:00
|
|
|
}
|
2018-10-23 19:46:12 +02:00
|
|
|
|
2021-02-28 01:18:00 +01:00
|
|
|
export function add_custom_profile_fields_to_settings() {
|
2018-06-10 07:03:22 +02:00
|
|
|
if (!overlays.settings_open()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-17 18:42:31 +02:00
|
|
|
const element_id = "#profile-settings .custom-profile-fields-form";
|
2018-10-22 10:20:33 +02:00
|
|
|
$(element_id).html("");
|
2018-06-07 11:39:12 +02:00
|
|
|
|
2021-02-28 01:18:00 +01:00
|
|
|
append_custom_profile_fields(element_id, people.my_current_user_id());
|
|
|
|
initialize_custom_user_type_fields(element_id, people.my_current_user_id(), true, true);
|
|
|
|
initialize_custom_date_type_fields(element_id);
|
|
|
|
}
|
2017-04-06 02:28:57 +02:00
|
|
|
|
2022-01-05 18:08:47 +01:00
|
|
|
export function hide_confirm_email_banner() {
|
|
|
|
if (!overlays.settings_open()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$("#account-settings-status").hide();
|
|
|
|
}
|
|
|
|
|
2021-02-28 01:18:00 +01:00
|
|
|
export function set_up() {
|
2018-02-26 20:09:07 +01:00
|
|
|
// Add custom profile fields elements to user account settings.
|
2021-02-28 01:18:00 +01:00
|
|
|
add_custom_profile_fields_to_settings();
|
2017-04-06 02:28:57 +02:00
|
|
|
$("#account-settings-status").hide();
|
2017-06-13 17:57:33 +02:00
|
|
|
|
2021-07-20 14:52:50 +02:00
|
|
|
const setup_api_key_modal = () => {
|
2020-06-05 20:34:18 +02:00
|
|
|
function request_api_key(data) {
|
2019-08-12 16:43:20 +02:00
|
|
|
channel.post({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/fetch_api_key",
|
2020-07-20 22:18:43 +02:00
|
|
|
data,
|
|
|
|
success(data) {
|
2019-08-12 16:43:20 +02:00
|
|
|
$("#get_api_key_password").val("");
|
|
|
|
$("#api_key_value").text(data.api_key);
|
|
|
|
// The display property on the error bar is set to important
|
|
|
|
// so instead of making display: none !important we just
|
|
|
|
// remove it.
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#api_key_status").remove();
|
2019-08-12 16:43:20 +02:00
|
|
|
$("#password_confirmation").hide();
|
2021-07-20 14:52:50 +02:00
|
|
|
$("#get_api_key_button").hide();
|
2019-08-12 16:43:20 +02:00
|
|
|
$("#show_api_key").show();
|
2021-07-20 14:52:50 +02:00
|
|
|
$("#api_key_buttons").show();
|
2019-08-12 16:43:20 +02:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
error(xhr) {
|
2021-04-13 05:18:25 +02:00
|
|
|
ui_report.error(
|
|
|
|
$t_html({defaultMessage: "Error"}),
|
|
|
|
xhr,
|
|
|
|
$("#api_key_status").expectOne(),
|
|
|
|
);
|
2019-08-12 16:43:20 +02:00
|
|
|
$("#show_api_key").hide();
|
|
|
|
},
|
|
|
|
});
|
2020-06-05 20:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$("#api_key_value").text("");
|
|
|
|
$("#show_api_key").hide();
|
2021-07-20 14:52:50 +02:00
|
|
|
$("#api_key_buttons").hide();
|
2021-04-05 09:53:15 +02:00
|
|
|
common.setup_password_visibility_toggle(
|
|
|
|
"#get_api_key_password",
|
|
|
|
"#get_api_key_password + .password_visibility_toggle",
|
2021-04-20 16:49:39 +02:00
|
|
|
{tippy_tooltips: true},
|
2021-04-05 09:53:15 +02:00
|
|
|
);
|
2020-06-05 20:34:18 +02:00
|
|
|
|
2021-07-20 14:52:50 +02:00
|
|
|
function do_get_api_key() {
|
|
|
|
$("#api_key_status").hide();
|
|
|
|
const data = {};
|
|
|
|
data.password = $("#get_api_key_password").val();
|
|
|
|
request_api_key(data);
|
|
|
|
}
|
|
|
|
|
2020-06-05 20:34:18 +02:00
|
|
|
if (page_params.realm_password_auth_enabled === false) {
|
|
|
|
// Skip the password prompt step, since the user doesn't have one.
|
|
|
|
request_api_key({});
|
|
|
|
} else {
|
2020-07-02 01:45:54 +02:00
|
|
|
$("#get_api_key_button").on("click", (e) => {
|
2020-06-05 20:34:18 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2021-07-20 14:52:50 +02:00
|
|
|
do_get_api_key();
|
|
|
|
});
|
|
|
|
$("#get_api_key_password").on("keydown", (e) => {
|
|
|
|
if (e.key === "Enter") {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
do_get_api_key();
|
|
|
|
}
|
2020-06-05 20:34:18 +02:00
|
|
|
});
|
|
|
|
}
|
2018-05-18 15:21:44 +02:00
|
|
|
|
2021-07-20 14:52:50 +02:00
|
|
|
$("#regenerate_api_key").on("click", (e) => {
|
2019-08-12 16:43:20 +02:00
|
|
|
channel.post({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/users/me/api_key/regenerate",
|
2020-07-20 22:18:43 +02:00
|
|
|
success(data) {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#api_key_value").text(data.api_key);
|
2019-08-12 16:43:20 +02:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
error(xhr) {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#user_api_key_error").text(JSON.parse(xhr.responseText).msg).show();
|
2019-08-12 16:43:20 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2018-05-18 15:21:44 +02:00
|
|
|
});
|
2017-06-13 17:57:33 +02:00
|
|
|
|
2019-08-12 16:43:20 +02:00
|
|
|
$("#download_zuliprc").on("click", function () {
|
2020-02-01 00:04:23 +01:00
|
|
|
const bot_object = {
|
|
|
|
user_id: people.my_current_user_id(),
|
2020-04-09 22:22:58 +02:00
|
|
|
email: page_params.delivery_email,
|
2020-02-01 00:04:23 +01:00
|
|
|
api_key: $("#api_key_value").text(),
|
|
|
|
};
|
|
|
|
const data = settings_bots.generate_zuliprc_content(bot_object);
|
2019-08-12 16:43:20 +02:00
|
|
|
$(this).attr("href", settings_bots.encode_zuliprc_as_uri(data));
|
2017-06-13 17:57:33 +02:00
|
|
|
});
|
2021-04-05 09:53:15 +02:00
|
|
|
|
2021-07-20 14:52:50 +02:00
|
|
|
$("#api_key_modal [data-micromodal-close]").on("click", () => {
|
2021-04-05 09:53:15 +02:00
|
|
|
common.reset_password_toggle_icons(
|
|
|
|
"#get_api_key_password",
|
|
|
|
"#get_api_key_password + .password_visibility_toggle",
|
|
|
|
);
|
|
|
|
});
|
2021-07-20 14:52:50 +02:00
|
|
|
};
|
2017-06-13 17:57:33 +02:00
|
|
|
|
2020-07-20 21:26:58 +02:00
|
|
|
$("#api_key_button").on("click", (e) => {
|
2021-07-20 14:52:50 +02:00
|
|
|
$("body").append(render_settings_api_key_modal());
|
2019-08-12 16:43:20 +02:00
|
|
|
setup_api_key_modal();
|
2021-07-20 14:52:50 +02:00
|
|
|
$("#api_key_status").hide();
|
|
|
|
overlays.open_modal("api_key_modal", {
|
|
|
|
autoremove: true,
|
|
|
|
micromodal: true,
|
|
|
|
micromodal_opts: {
|
|
|
|
onShow: () => {
|
|
|
|
$("#get_api_key_password").trigger("focus");
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2019-08-12 16:43:20 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2017-06-13 17:57:33 +02:00
|
|
|
});
|
2017-04-06 02:28:57 +02:00
|
|
|
|
|
|
|
function clear_password_change() {
|
|
|
|
// Clear the password boxes so that passwords don't linger in the DOM
|
|
|
|
// for an XSS attacker to find.
|
2021-04-05 09:53:15 +02:00
|
|
|
common.reset_password_toggle_icons(
|
|
|
|
"#old_password",
|
|
|
|
"#old_password + .password_visibility_toggle",
|
|
|
|
);
|
|
|
|
common.reset_password_toggle_icons(
|
|
|
|
"#new_password",
|
|
|
|
"#new_password + .password_visibility_toggle",
|
|
|
|
);
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#old_password, #new_password").val("");
|
2021-04-23 21:27:13 +02:00
|
|
|
password_quality?.("", $("#pw_strength .bar"), $("#new_password"));
|
2017-04-06 02:28:57 +02:00
|
|
|
}
|
|
|
|
|
2021-07-22 17:50:43 +02:00
|
|
|
function change_password_post_render() {
|
|
|
|
$("#change_password_modal")
|
|
|
|
.find("[data-micromodal-close]")
|
|
|
|
.on("click", () => {
|
|
|
|
clear_password_change();
|
|
|
|
});
|
|
|
|
common.setup_password_visibility_toggle(
|
|
|
|
"#old_password",
|
|
|
|
"#old_password + .password_visibility_toggle",
|
|
|
|
{tippy_tooltips: true},
|
|
|
|
);
|
|
|
|
common.setup_password_visibility_toggle(
|
|
|
|
"#new_password",
|
|
|
|
"#new_password + .password_visibility_toggle",
|
|
|
|
{tippy_tooltips: true},
|
|
|
|
);
|
|
|
|
clear_password_change();
|
|
|
|
}
|
2017-04-06 02:28:57 +02:00
|
|
|
|
2021-02-28 21:16:38 +01:00
|
|
|
$("#change_password").on("click", async (e) => {
|
2017-04-06 02:28:57 +02:00
|
|
|
e.preventDefault();
|
2017-12-24 19:00:29 +01:00
|
|
|
e.stopPropagation();
|
2021-11-29 18:42:33 +01:00
|
|
|
|
|
|
|
function validate_input(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
const old_password = $("#old_password").val();
|
|
|
|
const new_password = $("#new_password").val();
|
|
|
|
|
|
|
|
if (old_password === "") {
|
|
|
|
ui_report.error(
|
|
|
|
$t_html({defaultMessage: "Please enter your password"}),
|
|
|
|
undefined,
|
|
|
|
$("#dialog_error"),
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_password === "") {
|
|
|
|
ui_report.error(
|
|
|
|
$t_html({defaultMessage: "Please choose a new password"}),
|
|
|
|
undefined,
|
|
|
|
$("#dialog_error"),
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-22 17:50:43 +02:00
|
|
|
dialog_widget.launch({
|
|
|
|
html_heading: $t_html({defaultMessage: "Change password"}),
|
|
|
|
html_body: render_dialog_change_password(),
|
|
|
|
html_submit_button: $t_html({defaultMessage: "Change"}),
|
|
|
|
loading_spinner: true,
|
|
|
|
id: "change_password_modal",
|
|
|
|
form_id: "change_password_container",
|
|
|
|
post_render: change_password_post_render,
|
|
|
|
on_click: do_change_password,
|
2021-11-29 18:42:33 +01:00
|
|
|
validate_input,
|
2021-07-22 17:50:43 +02:00
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#pw_change_controls").show();
|
2017-04-20 08:21:31 +02:00
|
|
|
if (page_params.realm_password_auth_enabled !== false) {
|
2017-04-06 02:28:57 +02:00
|
|
|
// zxcvbn.js is pretty big, and is only needed on password
|
|
|
|
// change, so load it asynchronously.
|
2021-04-23 21:27:13 +02:00
|
|
|
password_quality = (await import("./password_quality")).password_quality;
|
2021-02-28 21:16:38 +01:00
|
|
|
$("#pw_strength .bar").removeClass("fade");
|
2017-04-06 02:28:57 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-07-22 17:50:43 +02:00
|
|
|
function do_change_password(e) {
|
2017-12-24 19:00:29 +01:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2021-07-22 17:50:43 +02:00
|
|
|
const change_password_error = $("#change_password_modal").find("#dialog_error");
|
2021-11-25 19:33:58 +01:00
|
|
|
change_password_error.hide();
|
2017-12-24 19:00:29 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const data = {
|
2020-07-15 01:29:15 +02:00
|
|
|
old_password: $("#old_password").val(),
|
|
|
|
new_password: $("#new_password").val(),
|
2017-12-24 19:00:29 +01:00
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const new_pw_field = $("#new_password");
|
2019-11-02 00:06:25 +01:00
|
|
|
const new_pw = data.new_password;
|
2020-07-15 01:29:15 +02:00
|
|
|
if (new_pw !== "") {
|
2021-04-23 21:27:13 +02:00
|
|
|
if (password_quality === undefined) {
|
|
|
|
// password_quality didn't load, for whatever reason.
|
2019-03-19 06:10:47 +01:00
|
|
|
settings_change_error(
|
2020-07-15 01:29:15 +02:00
|
|
|
"An internal error occurred; try reloading the page. " +
|
2020-07-15 00:34:28 +02:00
|
|
|
"Sorry for the trouble!",
|
|
|
|
);
|
2019-03-19 06:10:47 +01:00
|
|
|
return;
|
2021-04-23 21:27:13 +02:00
|
|
|
} else if (!password_quality(new_pw, undefined, new_pw_field)) {
|
2021-04-13 05:18:25 +02:00
|
|
|
settings_change_error($t_html({defaultMessage: "New password is too weak"}));
|
2019-03-19 06:10:47 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-05 03:01:45 +02:00
|
|
|
channel.set_password_change_in_progress(true);
|
2019-11-02 00:06:25 +01:00
|
|
|
const opts = {
|
2020-07-20 22:18:43 +02:00
|
|
|
success_continuation() {
|
2021-10-05 03:01:45 +02:00
|
|
|
channel.set_password_change_in_progress(false);
|
2021-07-22 17:50:43 +02:00
|
|
|
dialog_widget.close_modal();
|
2017-12-24 19:00:29 +01:00
|
|
|
},
|
2020-08-25 18:37:48 +02:00
|
|
|
error_continuation() {
|
2021-07-22 17:50:43 +02:00
|
|
|
dialog_widget.hide_dialog_spinner();
|
2021-10-05 03:01:45 +02:00
|
|
|
channel.set_password_change_in_progress(false);
|
2020-08-25 18:37:48 +02:00
|
|
|
},
|
2019-07-10 19:17:26 +02:00
|
|
|
error_msg_element: change_password_error,
|
2021-04-20 16:41:39 +02:00
|
|
|
failure_msg_html: null,
|
2019-07-10 19:17:26 +02:00
|
|
|
};
|
2020-07-15 00:34:28 +02:00
|
|
|
settings_ui.do_settings_change(
|
|
|
|
channel.patch,
|
|
|
|
"/json/settings",
|
|
|
|
data,
|
|
|
|
$("#account-settings-status").expectOne(),
|
|
|
|
opts,
|
|
|
|
);
|
2019-07-10 19:17:26 +02:00
|
|
|
clear_password_change();
|
2021-07-22 17:50:43 +02:00
|
|
|
}
|
2017-12-24 19:00:29 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#new_password").on("input", () => {
|
|
|
|
const field = $("#new_password");
|
2021-04-23 21:27:13 +02:00
|
|
|
password_quality?.(field.val(), $("#pw_strength .bar"), field);
|
2017-04-06 02:28:57 +02:00
|
|
|
});
|
|
|
|
|
2021-06-17 18:42:31 +02:00
|
|
|
$("#full_name").on("change", (e) => {
|
2018-01-30 08:56:15 +01:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2019-11-02 00:06:25 +01:00
|
|
|
const data = {};
|
2018-01-30 08:56:15 +01:00
|
|
|
|
2021-06-17 18:42:31 +02:00
|
|
|
data.full_name = $("#full_name").val();
|
2019-07-10 19:17:26 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
settings_ui.do_settings_change(
|
|
|
|
channel.patch,
|
|
|
|
"/json/settings",
|
|
|
|
data,
|
2021-06-17 18:42:31 +02:00
|
|
|
$(".full-name-status").expectOne(),
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2017-04-06 02:28:57 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#change_email_button").on("click", (e) => {
|
2017-04-06 02:28:57 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2020-07-15 01:29:15 +02:00
|
|
|
const change_email_error = $("#change_email_modal").find(".change_email_info").expectOne();
|
2019-11-02 00:06:25 +01:00
|
|
|
const data = {};
|
2020-07-15 01:29:15 +02:00
|
|
|
data.email = $(".email_change_container").find("input[name='email']").val();
|
2017-04-06 02:28:57 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const opts = {
|
2020-07-20 22:18:43 +02:00
|
|
|
success_continuation() {
|
2019-07-10 19:17:26 +02:00
|
|
|
if (page_params.development_environment) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const email_msg = render_settings_dev_env_email_access();
|
2020-07-15 00:34:28 +02:00
|
|
|
ui_report.success(
|
|
|
|
email_msg,
|
|
|
|
$("#dev-account-settings-status").expectOne(),
|
|
|
|
4000,
|
|
|
|
);
|
2017-04-06 02:28:57 +02:00
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
overlays.close_modal("#change_email_modal");
|
2017-04-06 02:28:57 +02:00
|
|
|
},
|
2019-07-10 19:17:26 +02:00
|
|
|
error_msg_element: change_email_error,
|
2021-04-13 05:18:25 +02:00
|
|
|
success_msg_html: $t_html(
|
|
|
|
{defaultMessage: "Check your email ({email}) to confirm the new address."},
|
|
|
|
{email: data.email},
|
|
|
|
),
|
2022-01-05 18:08:47 +01:00
|
|
|
sticky: true,
|
2019-07-10 19:17:26 +02:00
|
|
|
};
|
2020-07-15 00:34:28 +02:00
|
|
|
settings_ui.do_settings_change(
|
|
|
|
channel.patch,
|
|
|
|
"/json/settings",
|
|
|
|
data,
|
|
|
|
$("#account-settings-status").expectOne(),
|
|
|
|
opts,
|
|
|
|
);
|
2017-04-06 02:28:57 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#change_email").on("click", (e) => {
|
2017-04-06 02:28:57 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2018-02-02 16:54:26 +01:00
|
|
|
if (!page_params.realm_email_changes_disabled || page_params.is_admin) {
|
2020-07-15 01:29:15 +02:00
|
|
|
overlays.open_modal("#change_email_modal");
|
2021-07-18 19:04:06 +02:00
|
|
|
const email = $("#change_email").text().trim();
|
2020-07-15 01:29:15 +02:00
|
|
|
$(".email_change_container").find("input[name='email']").val(email);
|
2018-02-02 15:40:15 +01:00
|
|
|
}
|
2017-04-06 02:28:57 +02:00
|
|
|
});
|
|
|
|
|
2021-06-17 18:42:31 +02:00
|
|
|
$("#profile-settings").on("click", ".custom_user_field .remove_date", (e) => {
|
2018-06-09 15:28:55 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2020-07-15 01:29:15 +02:00
|
|
|
const field = $(e.target).closest(".custom_user_field").expectOne();
|
2020-10-07 09:17:30 +02:00
|
|
|
const field_id = Number.parseInt($(field).attr("data-field-id"), 10);
|
2018-06-09 15:28:55 +02:00
|
|
|
update_user_custom_profile_fields([field_id], channel.del);
|
|
|
|
});
|
|
|
|
|
2021-06-17 18:42:31 +02:00
|
|
|
$("#profile-settings").on("change", ".custom_user_field_value", function (e) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const fields = [];
|
|
|
|
const value = $(this).val();
|
2020-10-07 09:17:30 +02:00
|
|
|
const field_id = Number.parseInt(
|
2020-07-15 00:34:28 +02:00
|
|
|
$(e.target).closest(".custom_user_field").attr("data-field-id"),
|
|
|
|
10,
|
|
|
|
);
|
2018-06-05 12:57:02 +02:00
|
|
|
if (value) {
|
2020-07-20 22:18:43 +02:00
|
|
|
fields.push({id: field_id, value});
|
2018-06-05 12:57:02 +02:00
|
|
|
update_user_custom_profile_fields(fields, channel.patch);
|
|
|
|
} else {
|
|
|
|
fields.push(field_id);
|
|
|
|
update_user_custom_profile_fields(fields, channel.del);
|
|
|
|
}
|
2018-02-26 20:42:37 +01:00
|
|
|
});
|
|
|
|
|
2021-06-23 11:23:03 +02:00
|
|
|
$("#user_deactivate_account_button").on("click", (e) => {
|
|
|
|
// This click event must not get propagated to parent container otherwise the modal
|
|
|
|
// will not show up because of a call to `close_active_modal` in `settings.js`.
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2018-01-26 08:01:58 +01:00
|
|
|
|
2021-06-23 11:23:03 +02:00
|
|
|
function handle_confirm() {
|
2021-06-25 09:18:53 +02:00
|
|
|
channel.del({
|
|
|
|
url: "/json/users/me",
|
|
|
|
success() {
|
2021-07-05 19:15:37 +02:00
|
|
|
dialog_widget.hide_dialog_spinner();
|
2021-07-27 18:48:19 +02:00
|
|
|
dialog_widget.close_modal();
|
2021-06-25 09:18:53 +02:00
|
|
|
window.location.href = "/login/";
|
|
|
|
},
|
|
|
|
error(xhr) {
|
|
|
|
const error_last_owner = $t_html({
|
|
|
|
defaultMessage: "Error: Cannot deactivate the only organization owner.",
|
|
|
|
});
|
|
|
|
const error_last_user = $t_html(
|
|
|
|
{
|
|
|
|
defaultMessage:
|
|
|
|
"Error: Cannot deactivate the only user. You can deactivate the whole organization though in your <z-link>organization profile settings</z-link>.",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"z-link": (content_html) =>
|
|
|
|
`<a target="_blank" href="/#organization/organization-profile">${content_html}</a>`,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
let rendered_error_msg;
|
|
|
|
if (xhr.responseJSON.code === "CANNOT_DEACTIVATE_LAST_USER") {
|
|
|
|
if (xhr.responseJSON.is_last_owner) {
|
|
|
|
rendered_error_msg = error_last_owner;
|
|
|
|
} else {
|
|
|
|
rendered_error_msg = error_last_user;
|
2018-08-21 08:14:46 +02:00
|
|
|
}
|
2021-06-25 09:18:53 +02:00
|
|
|
}
|
2021-07-05 19:15:37 +02:00
|
|
|
dialog_widget.hide_dialog_spinner();
|
2021-07-27 18:48:19 +02:00
|
|
|
dialog_widget.close_modal();
|
2021-06-25 09:18:53 +02:00
|
|
|
$("#account-settings-status")
|
|
|
|
.addClass("alert-error")
|
|
|
|
.html(rendered_error_msg)
|
|
|
|
.show();
|
|
|
|
},
|
|
|
|
});
|
2021-06-23 11:23:03 +02:00
|
|
|
}
|
|
|
|
const html_body = render_confirm_deactivate_own_user();
|
|
|
|
confirm_dialog.launch({
|
|
|
|
html_heading: $t_html({defaultMessage: "Deactivate your account"}),
|
|
|
|
html_body,
|
|
|
|
on_click: handle_confirm,
|
|
|
|
help_link: "/help/deactivate-your-account",
|
|
|
|
loading_spinner: true,
|
|
|
|
});
|
2017-04-06 02:28:57 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#show_my_user_profile_modal").on("click", () => {
|
2018-08-25 15:27:28 +02:00
|
|
|
overlays.close_overlay("settings");
|
2020-02-05 14:30:59 +01:00
|
|
|
const user = people.get_by_user_id(people.my_current_user_id());
|
2020-07-02 01:45:54 +02:00
|
|
|
setTimeout(() => {
|
2021-06-23 20:48:49 +02:00
|
|
|
user_profile.show_user_profile(user);
|
2018-08-25 15:27:28 +02:00
|
|
|
}, 100);
|
2018-09-02 20:29:37 +02:00
|
|
|
|
|
|
|
// If user opened the "preview profile" modal from user
|
|
|
|
// settings, then closing preview profile modal should
|
|
|
|
// send them back to the settings modal.
|
2020-07-15 01:29:15 +02:00
|
|
|
$("body").one("hidden.bs.modal", "#user-profile-modal", (e) => {
|
2018-09-02 20:29:37 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
setTimeout(() => {
|
2018-09-02 20:29:37 +02:00
|
|
|
if (!overlays.settings_open()) {
|
|
|
|
overlays.open_settings();
|
|
|
|
}
|
|
|
|
}, 100);
|
|
|
|
});
|
2018-08-25 15:27:28 +02:00
|
|
|
});
|
|
|
|
|
2017-04-06 02:28:57 +02:00
|
|
|
function upload_avatar(file_input) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const form_data = new FormData();
|
2017-04-06 02:28:57 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
form_data.append("csrfmiddlewaretoken", csrf_token);
|
2020-02-08 01:53:49 +01:00
|
|
|
for (const [i, file] of Array.prototype.entries.call(file_input[0].files)) {
|
2020-07-15 01:29:15 +02:00
|
|
|
form_data.append("file-" + i, file);
|
2020-02-08 01:53:49 +01:00
|
|
|
}
|
2020-04-02 09:55:16 +02:00
|
|
|
display_avatar_upload_started();
|
2017-07-05 19:15:15 +02:00
|
|
|
channel.post({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/users/me/avatar",
|
2017-04-06 02:28:57 +02:00
|
|
|
data: form_data,
|
|
|
|
cache: false,
|
|
|
|
processData: false,
|
|
|
|
contentType: false,
|
2020-07-20 22:18:43 +02:00
|
|
|
success() {
|
2020-04-02 09:55:16 +02:00
|
|
|
display_avatar_upload_complete();
|
2020-06-15 08:28:41 +02:00
|
|
|
$("#user-avatar-upload-widget .image_file_input_error").hide();
|
2018-03-02 18:33:20 +01:00
|
|
|
$("#user-avatar-source").hide();
|
2018-10-18 10:04:43 +02:00
|
|
|
// Rest of the work is done via the user_events -> avatar_url event we will get
|
2018-03-02 18:33:20 +01:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
error(xhr) {
|
2020-04-02 09:55:16 +02:00
|
|
|
display_avatar_upload_complete();
|
2020-07-15 01:29:15 +02:00
|
|
|
if (page_params.avatar_source === "G") {
|
2018-03-02 18:33:20 +01:00
|
|
|
$("#user-avatar-source").show();
|
|
|
|
}
|
2020-06-15 08:28:41 +02:00
|
|
|
const $error = $("#user-avatar-upload-widget .image_file_input_error");
|
2019-01-13 09:11:41 +01:00
|
|
|
$error.text(JSON.parse(xhr.responseText).msg);
|
|
|
|
$error.show();
|
2017-04-06 02:28:57 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
avatar.build_user_avatar_widget(upload_avatar);
|
2021-06-30 19:46:27 +02:00
|
|
|
|
2021-07-28 16:00:58 +02:00
|
|
|
$("#user_timezone").val(user_settings.timezone);
|
2021-06-30 19:46:27 +02:00
|
|
|
|
|
|
|
$("#user_timezone").on("change", function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
const data = {timezone: this.value};
|
|
|
|
|
|
|
|
settings_ui.do_settings_change(
|
|
|
|
channel.patch,
|
2021-07-10 21:23:28 +02:00
|
|
|
"/json/settings",
|
2021-06-30 19:46:27 +02:00
|
|
|
data,
|
|
|
|
$(".timezone-setting-status").expectOne(),
|
|
|
|
);
|
|
|
|
});
|
2021-07-01 16:16:23 +02:00
|
|
|
|
2021-10-03 08:36:36 +02:00
|
|
|
$("#privacy_settings_box").on("change", "input", (e) => {
|
2021-07-01 16:16:23 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
2021-10-03 08:36:36 +02:00
|
|
|
const input_elem = $(e.currentTarget);
|
|
|
|
const setting_name = input_elem.attr("name");
|
|
|
|
const checked = input_elem.prop("checked");
|
|
|
|
|
|
|
|
const data = {[setting_name]: checked};
|
2021-07-01 16:16:23 +02:00
|
|
|
settings_ui.do_settings_change(
|
|
|
|
channel.patch,
|
2021-07-11 11:16:32 +02:00
|
|
|
"/json/settings",
|
2021-07-01 16:16:23 +02:00
|
|
|
data,
|
2021-09-17 10:20:37 +02:00
|
|
|
$("#account-settings .privacy-setting-status").expectOne(),
|
2021-07-01 16:16:23 +02:00
|
|
|
);
|
|
|
|
});
|
2021-02-28 01:18:00 +01:00
|
|
|
}
|