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

View File

@ -1,36 +1,44 @@
{{#if ip}} {{#if ip}}
<button class="btn btn-default" {{action "lookup"}}> <button class="btn btn-default" {{action "lookup"}}>
{{d-icon "globe"}}{{i18n 'admin.user.ip_lookup'}} {{d-icon "globe"}}
{{i18n "admin.user.ip_lookup"}}
</button> </button>
{{/if}} {{/if}}
{{#if show}} {{#if show}}
<div class="location-box"> <div class="location-box">
<a class="close pull-right" {{action "hide"}}>{{d-icon "times"}}</a> <a class="close pull-right" {{action "hide"}}>{{d-icon "times"}}</a>
{{#if copied}} {{#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}} {{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}} {{/if}}
<h4>{{i18n 'ip_lookup.title'}}</h4> <h4>{{i18n "ip_lookup.title"}}</h4>
<p class='powered-by'>{{{i18n 'ip_lookup.powered_by'}}}</p> <p class='powered-by'>{{{i18n "ip_lookup.powered_by"}}}</p>
<dl> <dl>
{{#if location}} {{#if location}}
{{#if location.hostname}} {{#if location.hostname}}
<dt>{{i18n 'ip_lookup.hostname'}}</dt> <dt>{{i18n "ip_lookup.hostname"}}</dt>
<dd>{{location.hostname}}</dd> <dd>{{location.hostname}}</dd>
{{/if}} {{/if}}
<dt>{{i18n 'ip_lookup.location'}}</dt> <dt>{{i18n "ip_lookup.location"}}</dt>
<dd> <dd>
{{#if location.location}} {{#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}} {{else}}
{{i18n 'ip_lookup.location_not_found'}} {{i18n "ip_lookup.location_not_found"}}
{{/if}} {{/if}}
</dd> </dd>
{{#if location.organization}} {{#if location.organization}}
<dt>{{i18n 'ip_lookup.organisation'}}</dt> <dt>{{i18n "ip_lookup.organisation"}}</dt>
<dd>{{location.organization}}</dd> <dd>{{location.organization}}</dd>
{{/if}} {{/if}}
{{else}} {{else}}
@ -38,31 +46,39 @@
{{/if}} {{/if}}
<dt> <dt>
{{i18n 'ip_lookup.other_accounts'}} {{i18n "ip_lookup.other_accounts"}}
<strong>{{totalOthersWithSameIP}}</strong> <strong>{{totalOthersWithSameIP}}</strong>
{{#if other_accounts.length}} {{#if other_accounts.length}}
<button class="btn btn-danger pull-right" {{action "deleteOtherAccounts"}}> <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> </button>
{{/if}} {{/if}}
</dt> </dt>
{{#conditional-loading-spinner size="small" condition=otherAccountsLoading}} {{#conditional-loading-spinner size="small" condition=otherAccountsLoading}}
{{#if other_accounts.length}} {{#if other_accounts.length}}
<dd class="other-accounts"> <dd class="other-accounts">
<table class="table table-condensed table-hover"> <table class="table table-condensed table-hover">
<thead> <thead>
<tr> <tr>
<th>{{i18n 'ip_lookup.username'}}</th> <th>{{i18n "ip_lookup.username"}}</th>
<th>{{i18n 'ip_lookup.trust_level'}}</th> <th>{{i18n "ip_lookup.trust_level"}}</th>
<th>{{i18n 'ip_lookup.read_time'}}</th> <th>{{i18n "ip_lookup.read_time"}}</th>
<th>{{i18n 'ip_lookup.topics_entered'}}</th> <th>{{i18n "ip_lookup.topics_entered"}}</th>
<th>{{i18n 'ip_lookup.post_count'}}</th> <th>{{i18n "ip_lookup.post_count"}}</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{{#each other_accounts as |a|}} {{#each other_accounts as |a|}}
<tr> <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.trustLevel.id}}</td>
<td>{{a.time_read}}</td> <td>{{a.time_read}}</td>
<td>{{a.topics_entered}}</td> <td>{{a.topics_entered}}</td>