DEV: refactoring ip-lookup (#6923)

This commit is contained in:
Joffrey JAFFEUX 2019-01-22 15:09:04 +01:00 committed by GitHub
parent 80d42b4ea2
commit ea8373351b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 53 deletions

View File

@ -1,3 +1,4 @@
import { default as computed } from "ember-addons/ember-computed-decorators";
import { ajax } from "discourse/lib/ajax";
import AdminUser from "admin/models/admin-user";
import copyText from "discourse/lib/copy-text";
@ -5,43 +6,39 @@ import copyText from "discourse/lib/copy-text";
export default Ember.Component.extend({
classNames: ["ip-lookup"],
otherAccountsToDelete: function() {
@computed("other_accounts.length", "totalOthersWithSameIP")
otherAccountsToDelete(otherAccountsLength, totalOthersWithSameIP) {
// can only delete up to 50 accounts at a time
var total = Math.min(50, this.get("totalOthersWithSameIP") || 0);
var visible = Math.min(50, this.get("other_accounts.length") || 0);
const total = Math.min(50, totalOthersWithSameIP || 0);
const visible = Math.min(50, otherAccountsLength || 0);
return Math.max(visible, total);
}.property("other_accounts", "totalOthersWithSameIP"),
},
actions: {
lookup: function() {
var self = this;
lookup() {
this.set("show", true);
if (!this.get("location")) {
ajax("/admin/users/ip-info", {
data: { ip: this.get("ip") }
}).then(function(location) {
self.set("location", Ember.Object.create(location));
});
ajax("/admin/users/ip-info", { data: { ip: this.get("ip") } }).then(
location => this.set("location", Ember.Object.create(location))
);
}
if (!this.get("other_accounts")) {
this.set("otherAccountsLoading", true);
var data = {
const data = {
ip: this.get("ip"),
exclude: this.get("userId"),
order: "trust_level DESC"
};
ajax("/admin/users/total-others-with-same-ip", { data }).then(function(
result
) {
self.set("totalOthersWithSameIP", result.total);
});
ajax("/admin/users/total-others-with-same-ip", { data }).then(result =>
this.set("totalOthersWithSameIP", result.total)
);
AdminUser.findAll("active", data).then(function(users) {
self.setProperties({
AdminUser.findAll("active", data).then(users => {
this.setProperties({
other_accounts: users,
otherAccountsLoading: false
});
@ -49,11 +46,11 @@ export default Ember.Component.extend({
}
},
hide: function() {
hide() {
this.set("show", false);
},
copy: function() {
copy() {
let text = `IP: ${this.get("ip")}\n`;
const location = this.get("location");
if (location) {
@ -73,25 +70,25 @@ export default Ember.Component.extend({
text += `: ${location.organization}\n`;
}
}
const copyRange = $('<p id="copy-range"></p>');
copyRange.html(text.trim().replace(/\n/g, "<br>"));
$(document.body).append(copyRange);
if (copyText(text, copyRange[0])) {
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);
Ember.run.later(() => this.set("copied", false), 2000);
}
copyRange.remove();
$copyRange.remove();
},
deleteOtherAccounts: function() {
var self = this;
deleteOtherAccounts() {
bootbox.confirm(
I18n.t("ip_lookup.confirm_delete_other_accounts"),
I18n.t("no_value"),
I18n.t("yes_value"),
function(confirmed) {
confirmed => {
if (confirmed) {
self.setProperties({
this.setProperties({
other_accounts: null,
otherAccountsLoading: true,
totalOthersWithSameIP: null
@ -100,13 +97,11 @@ export default Ember.Component.extend({
ajax("/admin/users/delete-others-with-same-ip.json", {
type: "DELETE",
data: {
ip: self.get("ip"),
exclude: self.get("userId"),
ip: this.get("ip"),
exclude: this.get("userId"),
order: "trust_level DESC"
}
}).then(function() {
self.send("lookup");
});
}).then(() => this.send("lookup"));
}
}
);

View File

@ -1,36 +1,44 @@
{{#if ip}}
<button class="btn btn-default" {{action "lookup"}}>
{{d-icon "globe"}}{{i18n 'admin.user.ip_lookup'}}
{{d-icon "globe"}}
{{i18n "admin.user.ip_lookup"}}
</button>
{{/if}}
{{#if show}}
<div class="location-box">
<a class="close pull-right" {{action "hide"}}>{{d-icon "times"}}</a>
{{#if copied}}
<a class="btn btn-default btn-hover pull-right">{{d-icon "copy"}} {{i18n "ip_lookup.copied"}}</a>
<a class="btn btn-default btn-hover pull-right">
{{d-icon "copy"}}
{{i18n "ip_lookup.copied"}}
</a>
{{else}}
<a class="btn btn-default pull-right no-text" {{action "copy"}}>{{d-icon "copy"}}</a>
<a class="btn btn-default pull-right no-text" {{action "copy"}}>
{{d-icon "copy"}}
</a>
{{/if}}
<h4>{{i18n 'ip_lookup.title'}}</h4>
<p class='powered-by'>{{{i18n 'ip_lookup.powered_by'}}}</p>
<h4>{{i18n "ip_lookup.title"}}</h4>
<p class='powered-by'>{{{i18n "ip_lookup.powered_by"}}}</p>
<dl>
{{#if location}}
{{#if location.hostname}}
<dt>{{i18n 'ip_lookup.hostname'}}</dt>
<dt>{{i18n "ip_lookup.hostname"}}</dt>
<dd>{{location.hostname}}</dd>
{{/if}}
<dt>{{i18n 'ip_lookup.location'}}</dt>
<dt>{{i18n "ip_lookup.location"}}</dt>
<dd>
{{#if location.location}}
<a href="https://maps.google.com/maps?q={{unbound location.latitude}},{{unbound location.longitude}}" target="_blank">{{location.location}}</a>
<a href="https://maps.google.com/maps?q={{unbound location.latitude}},{{unbound location.longitude}}" target="_blank">
{{location.location}}
</a>
{{else}}
{{i18n 'ip_lookup.location_not_found'}}
{{i18n "ip_lookup.location_not_found"}}
{{/if}}
</dd>
{{#if location.organization}}
<dt>{{i18n 'ip_lookup.organisation'}}</dt>
<dt>{{i18n "ip_lookup.organisation"}}</dt>
<dd>{{location.organization}}</dd>
{{/if}}
{{else}}
@ -38,31 +46,39 @@
{{/if}}
<dt>
{{i18n 'ip_lookup.other_accounts'}}
{{i18n "ip_lookup.other_accounts"}}
<strong>{{totalOthersWithSameIP}}</strong>
{{#if other_accounts.length}}
<button class="btn btn-danger pull-right" {{action "deleteOtherAccounts"}}>
{{d-icon "warning"}}{{i18n 'ip_lookup.delete_other_accounts' count=otherAccountsToDelete}}
{{d-icon "warning"}}
{{i18n "ip_lookup.delete_other_accounts" count=otherAccountsToDelete}}
</button>
{{/if}}
</dt>
{{#conditional-loading-spinner size="small" condition=otherAccountsLoading}}
{{#if other_accounts.length}}
<dd class="other-accounts">
<table class="table table-condensed table-hover">
<thead>
<tr>
<th>{{i18n 'ip_lookup.username'}}</th>
<th>{{i18n 'ip_lookup.trust_level'}}</th>
<th>{{i18n 'ip_lookup.read_time'}}</th>
<th>{{i18n 'ip_lookup.topics_entered'}}</th>
<th>{{i18n 'ip_lookup.post_count'}}</th>
<th>{{i18n "ip_lookup.username"}}</th>
<th>{{i18n "ip_lookup.trust_level"}}</th>
<th>{{i18n "ip_lookup.read_time"}}</th>
<th>{{i18n "ip_lookup.topics_entered"}}</th>
<th>{{i18n "ip_lookup.post_count"}}</th>
</tr>
</thead>
<tbody>
{{#each other_accounts as |a|}}
<tr>
<td>{{#link-to "adminUser" a}}{{avatar a usernamePath="user.username" imageSize="small"}}&nbsp;{{a.username}}{{/link-to}}</td>
<td>
{{#link-to "adminUser" a}}
{{avatar a usernamePath="user.username" imageSize="small"}}
&nbsp;
<span>{{a.username}}</span>
{{/link-to}}
</td>
<td>{{a.trustLevel.id}}</td>
<td>{{a.time_read}}</td>
<td>{{a.topics_entered}}</td>