thedesk/app/js/login/manager.js

338 lines
9.9 KiB
JavaScript
Raw Normal View History

2018-01-28 23:22:43 +11:00
//アカウントマネージャ
//最初に読むやつ
function load() {
$("#acct-list").html("");
var prof = localStorage.getItem("prof");
$(".my-prof").attr("src", prof);
var name = localStorage.getItem("name");
$("#now-name").text(name);
var user = localStorage.getItem("user");
$("#now-user").text(user);
var domain = localStorage.getItem("domain");
$(".now-domain").text(domain);
var multi = localStorage.getItem("multi");
if (!multi) {
var obj = [{
at: localStorage.getItem(localStorage.getItem("domain_0") + "_at"),
name: localStorage.getItem("name_0"),
domain: localStorage.getItem("domain_0"),
user: localStorage.getItem("user_0"),
prof: localStorage.getItem("prof_0"),
id: localStorage.getItem("user-id_0")
}];
var json = JSON.stringify(obj);
localStorage.setItem("multi", json);
} else {
var obj = JSON.parse(multi);
}
console.log(obj);
var templete;
Object.keys(obj).forEach(function(key) {
var acct = obj[key];
var list = key * 1 + 1;
2018-04-01 00:55:47 +11:00
templete = '<div id="acct_' + key + '"><div class="col s1">' + list +
'.</div><div class="col s2"><img src="' + acct.prof + '" width="40" height="40"></div><div class="text col s3">' +
2018-03-15 06:42:48 +11:00
acct.name + '&nbsp;<span class="gray">' + escapeHTML(acct.user) + '@' + acct.domain +
2018-04-01 00:55:47 +11:00
'</span></div><div class="col s2"><button class="btn waves-effect disTar" onclick="data(\'' +
2018-01-28 23:22:43 +11:00
acct.domain +
2018-04-01 00:55:47 +11:00
'\')">インスタンス情報</button></div><div class="col s2"><button class="btn waves-effect" onclick="refresh(' +
2018-01-28 23:22:43 +11:00
key +
2018-04-01 00:55:47 +11:00
')">情報更新</button></div><div class="col s2"><button class="btn waves-effect red disTar" onclick="multiDel(' +
key + ')">削除</button><br></div></div>';
2018-01-28 23:22:43 +11:00
$("#acct-list").append(templete);
});
var acctN = localStorage.getItem("acct");
if (!acctN) {
localStorage.setItem("acct", 0);
var acctN = 0;
}
2018-04-07 14:31:09 +10:00
var electron = require("electron");
var remote=electron.remote;
var platform=remote.process.platform;
if(platform=="win32"){
}else{
$("#linux").prop("checked", true);
}
2018-01-28 23:22:43 +11:00
}
//最初に読む
load();
support();
//instances.social
function data(domain) {
$("#ins-upd").text("Loading...");
$("#ins-add").text("Loading...");
$("#ins-connect").text("Loading...");
$("#ins-toot").text("Loading...");
$("#ins-sys").text("Loading...");
$("#ins-per").text("Loading...");
$("#ins-user").text("Loading...");
$("#ins-ver").text("Loading...");
2018-04-07 14:31:09 +10:00
$("#ins-name").text("Loading...");
2018-01-28 23:22:43 +11:00
$("#ins-prof").attr('src', "./img/loading.svg");
var start = "https://instances.social/api/1.0/instances/show?name=" + domain;
fetch(start, {
method: 'GET',
headers: {
'content-type': 'application/json',
'Authorization': 'Bearer tC8F6xWGWBUwGScyNevYlx62iO6fdQ4oIK0ad68Oo7ZKB8GQdGpjW9TKxBnIh8grAhvd5rw3iyP9JPamoDpeLQdz62EToPJUW99hDx8rfuJfGdjQuimZPTbIOx0woA5M'
},
}).then(function(response) {
return response.json();
}).catch(function(error) {
todo(error);
console.error(error);
}).then(function(json) {
console.log(json);
if (!json.error) {
2018-04-07 14:31:09 +10:00
$("#ins-name").text(json.name);
2018-01-28 23:22:43 +11:00
$("#ins-upd").text(date(json.checked_at, 'full'));
$("#ins-add").text(date(json.added_at, 'full'));
$("#ins-connect").text(json.connections);
$("#ins-toot").text(json.statuses);
$("#ins-sys").text(date(json.updated_at, 'full'));
$("#ins-per").text(json.uptime * 100);
$("#ins-user").text(json.users);
$("#ins-ver").text(json.version);
$("#ins-prof").attr('src', json.thumbnail);
}
});
}
//アカウントデータ 消す
function multiDel(target) {
var multi = localStorage.getItem("multi");
var obj = JSON.parse(multi);
if (confirm(obj[target]["user"] + "@" + obj[target]["domain"] + "を削除します")) {
obj.splice(target, 1);
var json = JSON.stringify(obj);
localStorage.setItem("multi", json);
2018-03-11 01:22:59 +11:00
Object.keys(obj).forEach(function(key) {
refresh(key);
});
2018-01-28 23:22:43 +11:00
load();
}
}
//サポートインスタンス
function support() {
2018-03-27 13:39:35 +11:00
var json=JSON.parse(localStorage.getItem("instance"));
2018-01-28 23:22:43 +11:00
console.log(json);
Object.keys(json).forEach(function(key) {
var instance = json[key];
if (instance == "instance") {
2018-04-01 00:55:47 +11:00
templete = '<a onclick="login(\'' + key +
'\')" class="collection-item pointer transparent">' + json[key + "_name"] + '(' + key + ')</a>';
2018-01-28 23:22:43 +11:00
$("#support").append(templete);
}
});
}
//URL指定してポップアップ
function login(url) {
var multi = localStorage.getItem("multi");
var obj = JSON.parse(multi);
var ng;
Object.keys(obj).forEach(function(key) {
var acct = obj[key];
if (acct.domain == url) {
Materialize.toast(url + "は登録できません。同一インスタンスには一つのアカウントでしかログインできません。", 5000);
ng = "true";
return;
}
});
if (ng) {
return;
}
2018-03-11 01:22:59 +11:00
if($('#linux:checked').val()=="on"){
2018-02-26 02:32:10 +11:00
var red = "urn:ietf:wg:oauth:2.0:oob"
}else{
var red = 'thedesk://manager';
}
localStorage.setItem("redirect", red);
2018-01-28 23:22:43 +11:00
var start = "https://" + url + "/api/v1/apps";
2018-04-17 03:10:35 +10:00
var httpreq = new XMLHttpRequest();
httpreq.open('POST', start, true);
httpreq.setRequestHeader('Content-Type', 'application/json');
httpreq.responseType = 'json';
httpreq.send(JSON.stringify({
scopes: 'read write follow',
client_name: "TheDesk(PC)",
redirect_uris: red,
website: "https://thedesk.top"
}));
httpreq.onreadystatechange = function() {
if (httpreq.readyState == 4) {
var json = httpreq.response;
console.log(json);
var auth = "https://" + url + "/oauth/authorize?client_id=" + json[
"client_id"] + "&client_secret=" + json["client_secret"] +
"&response_type=code&scope=read+write+follow&redirect_uri=" + red;
localStorage.setItem("domain_tmp", url);
localStorage.setItem("client_id", json["client_id"]);
localStorage.setItem("client_secret", json["client_secret"]);
$("#auth").show();
$("#add").hide();
const {
shell
} = require('electron');
shell.openExternal(auth);
var electron = require("electron");
var ipc = electron.ipcRenderer;
if ($('#linux:checked').val() == "on") {} else {
2018-02-26 02:32:10 +11:00
ipc.send('quit', 'go');
}
2018-04-17 03:10:35 +10:00
}
}
2018-01-28 23:22:43 +11:00
}
//テキストボックスにURL入れた
function instance() {
var url = $("#url").val();
login(url);
}
//コード入れてAccessTokenゲット
2018-02-18 16:43:11 +11:00
function code(code) {
2018-02-26 02:32:10 +11:00
localStorage.removeItem("redirect")
2018-02-18 16:43:11 +11:00
if(!code){
var code = $("#code").val();
}
2018-01-28 23:22:43 +11:00
var url = localStorage.getItem("domain_tmp");
localStorage.removeItem("domain_tmp");
var start = "https://" + url + "/oauth/token";
var id = localStorage.getItem("client_id");
var secret = localStorage.getItem("client_secret");
2018-04-17 03:10:35 +10:00
var httpreq = new XMLHttpRequest();
httpreq.open('POST', start, true);
httpreq.setRequestHeader('Content-Type', 'application/json');
httpreq.responseType = 'json';
httpreq.send(JSON.stringify({
grant_type: "authorization_code",
redirect_uri: "urn:ietf:wg:oauth:2.0:oob",
client_id: id,
client_secret: secret,
code: code
}));
httpreq.onreadystatechange = function() {
if (httpreq.readyState == 4) {
var json = httpreq.response;
console.log(json);
if (json["access_token"]) {
$("#auth").hide();
$("#add").show();
getdata(url, json["access_token"]);
}
2018-01-28 23:22:43 +11:00
}
2018-04-17 03:10:35 +10:00
}
2018-01-28 23:22:43 +11:00
}
//ユーザーデータ取得
function getdata(domain, at) {
var start = "https://" + domain + "/api/v1/accounts/verify_credentials";
fetch(start, {
method: 'GET',
headers: {
'content-type': 'application/json',
'Authorization': 'Bearer ' + at
},
}).then(function(response) {
return response.json();
}).catch(function(error) {
todo(error);
console.error(error);
}).then(function(json) {
console.log(json);
if (json.error) {
console.error("Error:" + json.error);
Materialize.toast("エラーが発生しました。しばらく待ってから再起動してください。Error:" + json.error,
5000);
return;
}
2018-03-31 13:39:06 +11:00
var avatar=json["avatar"];
//missingがmissingなやつ
if(avatar=="/avatars/original/missing.png"){
avatar="./img/missing.svg";
}
2018-01-28 23:22:43 +11:00
var add = {
at: at,
name: json["display_name"],
domain: domain,
user: json["acct"],
2018-03-31 13:39:06 +11:00
prof: avatar,
2018-01-28 23:22:43 +11:00
id: json["id"]
};
var multi = localStorage.getItem("multi");
var obj = JSON.parse(multi);
2018-02-17 00:08:43 +11:00
var target = obj.lengtth;
2018-01-28 23:22:43 +11:00
obj.push(add);
2018-02-17 00:08:43 +11:00
localStorage.setItem("name_" + target, json["display_name"]);
localStorage.setItem("user_" + target, json["acct"]);
localStorage.setItem("user-id_" + target, json["id"]);
2018-03-31 13:39:06 +11:00
localStorage.setItem("prof_" + target, avatar);
2018-01-28 23:22:43 +11:00
console.log(obj);
var json = JSON.stringify(obj);
localStorage.setItem("multi", json);
load();
});
}
//ユーザーデータ更新
function refresh(target) {
var multi = localStorage.getItem("multi");
var obj = JSON.parse(multi);
var start = "https://" + obj[target].domain +
"/api/v1/accounts/verify_credentials";
2018-02-17 00:08:43 +11:00
console.log(start);
2018-01-28 23:22:43 +11:00
fetch(start, {
method: 'GET',
headers: {
'content-type': 'application/json',
'Authorization': 'Bearer ' + obj[target].at
},
}).then(function(response) {
return response.json();
}).catch(function(error) {
todo(error);
console.error(error);
}).then(function(json) {
console.log(json);
if (json.error) {
console.error("Error:" + json.error);
Materialize.toast("エラーが発生しました。しばらく待ってから再起動してください。Error:" + json.error,
5000);
return;
}
2018-03-31 13:39:06 +11:00
var avatar=json["avatar"];
//missingがmissingなやつ
2018-04-07 14:31:09 +10:00
if(avatar=="/avatars/original/missing.png" || !avatar){
2018-03-31 13:39:06 +11:00
avatar="./img/missing.svg";
}
2018-01-28 23:22:43 +11:00
var ref = {
at: obj[target].at,
name: json["display_name"],
domain: obj[target].domain,
user: json["acct"],
2018-03-31 13:39:06 +11:00
prof: avatar,
2018-01-28 23:22:43 +11:00
id: json["id"]
};
2018-02-17 00:08:43 +11:00
localStorage.setItem("name_" + target, json["display_name"]);
localStorage.setItem("user_" + target, json["acct"]);
localStorage.setItem("user-id_" + target, json["id"]);
console.log("user-id_" + target+":"+json["id"])
2018-02-18 05:44:20 +11:00
console.log(localStorage.getItem("user-id_"+target));
2018-03-31 13:39:06 +11:00
localStorage.setItem("prof_" + target, avatar);
2018-01-28 23:22:43 +11:00
obj[target] = ref;
console.log(obj);
var json = JSON.stringify(obj);
localStorage.setItem("multi", json);
load();
});
}