discourse/app/assets/javascripts/admin/addon/components/ip-lookup.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

118 lines
3.3 KiB
JavaScript
Raw Normal View History

import AdminUser from "admin/models/admin-user";
import Component from "@ember/component";
import EmberObject from "@ember/object";
import I18n from "I18n";
2016-07-01 01:55:44 +08:00
import { ajax } from "discourse/lib/ajax";
import bootbox from "bootbox";
import copyText from "discourse/lib/copy-text";
import discourseComputed from "discourse-common/utils/decorators";
import discourseLater from "discourse-common/lib/later";
export default Component.extend({
classNames: ["ip-lookup"],
@discourseComputed("other_accounts.length", "totalOthersWithSameIP")
2019-01-22 22:09:04 +08:00
otherAccountsToDelete(otherAccountsLength, totalOthersWithSameIP) {
// can only delete up to 50 accounts at a time
2019-01-22 22:09:04 +08:00
const total = Math.min(50, totalOthersWithSameIP || 0);
const visible = Math.min(50, otherAccountsLength || 0);
return Math.max(visible, total);
2019-01-22 22:09:04 +08:00
},
actions: {
2019-01-22 22:09:04 +08:00
lookup() {
this.set("show", true);
if (!this.location) {
ajax("/admin/users/ip-info", {
data: { ip: this.ip },
}).then((location) =>
this.set("location", EmberObject.create(location))
2019-01-22 22:09:04 +08:00
);
}
if (!this.other_accounts) {
2014-11-18 01:17:24 +08:00
this.set("otherAccountsLoading", true);
2019-01-22 22:09:04 +08:00
const data = {
ip: this.ip,
exclude: this.userId,
order: "trust_level DESC",
};
2019-01-22 22:09:04 +08:00
ajax("/admin/users/total-others-with-same-ip", {
data,
}).then((result) => this.set("totalOthersWithSameIP", result.total));
2019-01-22 22:09:04 +08:00
AdminUser.findAll("active", data).then((users) => {
this.setProperties({
other_accounts: users,
2014-11-18 01:17:24 +08:00
otherAccountsLoading: false,
});
});
}
},
2019-01-22 22:09:04 +08:00
hide() {
this.set("show", false);
},
2019-01-22 22:09:04 +08:00
copy() {
let text = `IP: ${this.ip}\n`;
const location = this.location;
if (location) {
if (location.hostname) {
text += `${I18n.t("ip_lookup.hostname")}: ${location.hostname}\n`;
}
text += I18n.t("ip_lookup.location");
if (location.location) {
text += `: ${location.location}\n`;
} else {
text += `: ${I18n.t("ip_lookup.location_not_found")}\n`;
}
if (location.organization) {
text += I18n.t("ip_lookup.organisation");
text += `: ${location.organization}\n`;
}
}
2019-01-22 22:09:04 +08:00
const $copyRange = $('<p id="copy-range"></p>');
$copyRange.html(text.trim().replace(/\n/g, "<br>"));
$(document.body).append($copyRange);
if (copyText(text, $copyRange[0])) {
this.set("copied", true);
discourseLater(() => this.set("copied", false), 2000);
}
2019-01-22 22:09:04 +08:00
$copyRange.remove();
},
2019-01-22 22:09:04 +08:00
deleteOtherAccounts() {
bootbox.confirm(
I18n.t("ip_lookup.confirm_delete_other_accounts"),
I18n.t("no_value"),
I18n.t("yes_value"),
2019-01-22 22:09:04 +08:00
(confirmed) => {
if (confirmed) {
2019-01-22 22:09:04 +08:00
this.setProperties({
other_accounts: null,
otherAccountsLoading: true,
totalOthersWithSameIP: null,
});
2016-07-01 01:55:44 +08:00
ajax("/admin/users/delete-others-with-same-ip.json", {
type: "DELETE",
data: {
ip: this.ip,
exclude: this.userId,
order: "trust_level DESC",
},
2019-01-22 22:09:04 +08:00
}).then(() => this.send("lookup"));
2018-06-15 23:03:24 +08:00
}
}
);
},
},
});