thedesk/app/js/tl/tl.js

558 lines
15 KiB
JavaScript
Raw Normal View History

2018-01-28 23:22:43 +11:00
//TL取得
2018-07-22 23:03:46 +10:00
function tl(type, data, acct_id, tlid, delc, voice) {
2018-01-28 23:22:43 +11:00
scrollevent();
localStorage.removeItem("morelock");
localStorage.removeItem("pool");
2018-01-31 03:43:01 +11:00
var domain = localStorage.getItem("domain_" + acct_id);
2018-04-01 07:00:27 +10:00
//タグとかの場合はカラム追加して描画
2018-01-28 23:22:43 +11:00
if (tlid == "add") {
console.log("add");
var newtab = $(".box").length;
var add = {
domain: acct_id,
2018-04-01 07:00:27 +10:00
type: type,
2018-01-28 23:22:43 +11:00
data: data
};
var multi = localStorage.getItem("column");
var obj = JSON.parse(multi);
2018-03-27 13:39:35 +11:00
localStorage.setItem("card_" + obj.length,"true");
2018-01-28 23:22:43 +11:00
obj.push(add);
console.log(obj);
var json = JSON.stringify(obj);
localStorage.setItem("column", json);
parseColumn();
return;
}
2018-03-14 05:31:31 +11:00
2018-01-28 23:22:43 +11:00
if (!type) {
var type = localStorage.getItem("now");
if (!type) {
//デフォルト
var type = "local";
}
}
2018-07-30 21:03:49 +10:00
if (type == "mix" && domain != "misskey.xyz") {
2018-01-28 23:22:43 +11:00
//Integratedなら飛ばす
2018-01-31 03:43:01 +11:00
$("#notice_" + tlid).text("Integrated TL(" + localStorage.getItem(
"user_" + acct_id) + "@" + domain + ")");
2018-02-25 02:59:53 +11:00
$("#notice_icon_" + tlid).text("merge_type");
2018-07-22 23:03:46 +10:00
mixtl(acct_id, tlid, "integrated",delc,voice);
2018-01-28 23:22:43 +11:00
return;
2018-03-27 13:39:35 +11:00
}else if (type == "plus") {
//Local+なら飛ばす
$("#notice_" + tlid).text("Local+ TL(" + localStorage.getItem(
"user_" + acct_id) + "@" + domain + ")");
$("#notice_icon_" + tlid).text("people_outline");
2018-07-22 23:03:46 +10:00
mixtl(acct_id, tlid, "plus",delc,voice);
2018-03-27 13:39:35 +11:00
return;
}else if (type == "notf") {
2018-01-28 23:22:43 +11:00
//通知なら飛ばす
2018-03-14 05:31:31 +11:00
//notf(acct_id, tlid, 'direct');
2018-03-20 15:55:25 +11:00
$("#notice_" + tlid).text(cap(type, data, acct_id) + "(" + localStorage.getItem(
2018-01-28 23:22:43 +11:00
"user_" + acct_id) + "@" + domain + ")");
2018-02-25 02:59:53 +11:00
$("#notice_icon_" + tlid).text("notifications");
2018-01-28 23:22:43 +11:00
return;
}
localStorage.setItem("now", type);
todo(cap(type) + " TL Loading...");
2018-07-07 03:51:48 +10:00
var at = localStorage.getItem("acct_"+ acct_id + "_at");
2018-03-27 13:39:35 +11:00
if(type!="noauth"){
var hdr={
'content-type': 'application/json',
'Authorization': 'Bearer ' + at
};
$("#notice_" + tlid).text(cap(type, data, acct_id) + "(" + localStorage.getItem(
"user_" + acct_id) + "@" + domain + ")");
}else{
var hdr={
'content-type': 'application/json'
};
domain=acct_id;
$("#notice_" + tlid).text("Glance TL(" + domain + ")");
}
2018-02-25 02:59:53 +11:00
$("#notice_icon_" + tlid).text(icon(type));
2018-07-30 21:03:49 +10:00
if(domain=="misskey.xyz"){
var url=misskeycom(type, data);
var start = "https://" + domain + "/api/notes/"+url;
var method="POST";
var req={};
if(type!="noauth"){
req.i=at;
2018-03-14 05:31:31 +11:00
}
2018-07-30 21:03:49 +10:00
if(type=="local-media"||type=="pub-media"){
req.mediaOnly=true;
}
if(type=="tag"){
req.tag=data;
}
if(type=="list"){
req.listId=data;
}
req.limit=20;
var i={
method: method,
headers: hdr,
body: JSON.stringify(req),
}
}else{
var url=com(type, data);
if(type=="tag"){
var tag = localStorage.getItem("tag-range");
if(tag=="local"){
url=url+"local=true";
}
}
var start = "https://" + domain + "/api/v1/timelines/" + url;
var method="GET";
var i={
method: method,
headers: hdr
};
2018-03-14 05:31:31 +11:00
}
2018-07-30 21:03:49 +10:00
2018-01-28 23:22:43 +11:00
console.log(start);
2018-07-30 21:03:49 +10:00
fetch(start, i).then(function(response) {
2018-01-28 23:22:43 +11:00
return response.json();
}).catch(function(error) {
todo(error);
console.error(error);
}).then(function(json) {
2018-07-30 21:03:49 +10:00
console.log(json);
2018-07-17 01:39:06 +10:00
if(localStorage.getItem("filter_"+ acct_id)!="undefined"){
var mute=getFilterType(JSON.parse(localStorage.getItem("filter_"+ acct_id)),type);
}else{
var mute=[];
}
2018-07-30 21:03:49 +10:00
if(domain=="misskey.xyz"){
var templete = misskeyParse(json, type, acct_id, tlid, "", mute);
}else{
var templete = parse(json, type, acct_id, tlid, "", mute);
}
2018-01-28 23:22:43 +11:00
$("#timeline_" + tlid).html(templete);
additional(acct_id, tlid);
jQuery("time.timeago").timeago();
todc();
2018-07-22 23:03:46 +10:00
reload(type, '', acct_id, tlid, data, mute, delc,voice);
2018-01-28 23:22:43 +11:00
$(window).scrollTop(0);
});
}
//Streaming接続
2018-07-22 23:03:46 +10:00
function reload(type, cc, acct_id, tlid, data, mute, delc, voice) {
2018-01-28 23:22:43 +11:00
if (!type) {
var type = localStorage.getItem("now");
}
var domain = localStorage.getItem("domain_" + acct_id);
2018-07-07 03:51:48 +10:00
var at = localStorage.getItem("acct_"+ acct_id + "_at");
2018-01-28 23:22:43 +11:00
localStorage.setItem("now", type);
2018-07-30 21:03:49 +10:00
if(domain=="misskey.xyz"){
if (type == "home") {
var start = "wss://" + domain +
"/?i=" + at;
} else if (type == "pub") {
var start = "wss://" + domain +
"/global-timeline?i=" + at;
} else if (type == "pub-media") {
var start = "wss://" + domain +
"/global-timeline?i=" + at;
} else if (type == "local") {
var start = "wss://" + domain +
"/local-timeline?i=" + at;
} else if (type == "local-media") {
var start = "wss://" + domain +
"/local-timeline?i=" + at;
} else if (type == "mix") {
var start = "wss://" + domain +
"/hybrid-timeline?i=" + at;
} else if (type == "tag") {
Materialize.toast(lang_misskeyparse_tagnostr[lang], 1000);
} else if (type == "noauth") {
var start = "wss://" + acct_id +
"/local-timeline?i=" + at;
} else if (type=="list"){
Materialize.toast(lang_misskeyparse_listnostr[lang], 1000);
}
}else{
if (type == "home") {
var start = "wss://" + domain +
"/api/v1/streaming/?stream=user&access_token=" + at;
} else if (type == "pub") {
var start = "wss://" + domain +
"/api/v1/streaming/?stream=public&access_token=" + at;
} else if (type == "pub-media") {
var start = "wss://" + domain +
"/api/v1/streaming/?stream=public:media&access_token=" + at;
} else if (type == "local") {
var start = "wss://" + domain +
"/api/v1/streaming/?stream=public:local&access_token=" + at;
} else if (type == "local-media") {
var start = "wss://" + domain +
"/api/v1/streaming/?stream=public:local:media&only_media=true&access_token=" + at;
} else if (type == "tag") {
if(type=="tag"){
var tag = localStorage.getItem("tag-range");
if(tag=="local"){
data=data+"&local=true";
}
2018-03-14 05:31:31 +11:00
}
2018-07-30 21:03:49 +10:00
var start = "wss://" + domain +
"/api/v1/streaming/?stream=hashtag&tag=" + data +"&access_token=" + at;
} else if (type == "noauth") {
var start = "wss://" + acct_id +
"/api/v1/streaming/?stream=public:local";
} else if (type=="list"){
var start = "wss://" + domain +
"/api/v1/streaming/?stream=list&list=" + data +"&access_token=" + at;
} else if (type=="dm"){
var start = "wss://" + domain +
"/api/v1/streaming/?stream=direct&access_token=" + at;
2018-03-14 05:31:31 +11:00
}
2018-03-27 13:39:35 +11:00
}
2018-07-30 21:03:49 +10:00
2018-01-28 23:22:43 +11:00
console.log(start);
var wsid = websocket.length;
websocket[wsid] = new WebSocket(start);
websocket[wsid].onopen = function(mess) {
console.log(tlid + ":Connect Streaming API:" + type);
console.log(mess);
2018-03-11 01:22:59 +11:00
$("#notice_icon_" + tlid).removeClass("red-text");
2018-01-28 23:22:43 +11:00
}
websocket[wsid].onmessage = function(mess) {
console.log(tlid + ":Receive Streaming API:");
2018-07-30 21:03:49 +10:00
console.log(JSON.parse(mess.data));
if(domain=="misskey.xyz"){
if (JSON.parse(mess.data).type == "note") {
var obj = JSON.parse(mess.data).body;
2018-07-22 23:03:46 +10:00
if(voice){
2018-08-05 02:26:34 +10:00
say(obj.text)
2018-07-30 21:03:49 +10:00
}
var templete = misskeyParse([obj], type, acct_id, tlid,"",mute);
2018-03-11 01:22:59 +11:00
var pool = localStorage.getItem("pool_" + tlid);
if (pool) {
pool = templete + pool;
} else {
pool = templete
}
localStorage.setItem("pool_" + tlid, pool);
scrollck();
jQuery("time.timeago").timeago();
2018-01-28 23:22:43 +11:00
}
2018-07-30 21:03:49 +10:00
}else{
var typeA = JSON.parse(mess.data).event;
if (typeA == "delete") {
var obj = JSON.parse(mess.data).payload;
if(delc=="true"){
$("#timeline_"+tlid+" [toot-id=" + JSON.parse(mess.data).payload + "]").addClass("emphasized");
$("#timeline_"+tlid+" [toot-id=" + JSON.parse(mess.data).payload + "]").addClass("by_delcatch");
}else{
$("[toot-id=" + JSON.parse(mess.data).payload + "]").hide();
$("[toot-id=" + JSON.parse(mess.data).payload + "]").remove();
}
} else if (typeA == "update") {
var obj = JSON.parse(JSON.parse(mess.data).payload);
console.log(obj);
if($("#timeline_" + tlid +" [toot-id=" + obj.id + "]").length < 1){
if(voice){
say(obj.content)
}
var templete = parse([obj], type, acct_id, tlid,"",mute);
var pool = localStorage.getItem("pool_" + tlid);
if (pool) {
pool = templete + pool;
} else {
pool = templete
}
localStorage.setItem("pool_" + tlid, pool);
scrollck();
additional(acct_id, tlid);
jQuery("time.timeago").timeago();
}else{
todo("二重取得発生中");
}
todc();
}else if(typeA=="filters_changed"){
filterUpdate(acct_id);
}
2018-01-28 23:22:43 +11:00
}
2018-07-30 21:03:49 +10:00
2018-01-28 23:22:43 +11:00
}
websocket[wsid].onerror = function(error) {
console.error('WebSocket Error ' + error);
};
}
//一定のスクロールで発火
function moreload(type, tlid) {
var multi = localStorage.getItem("column");
var obj = JSON.parse(multi);
var acct_id = obj[tlid].domain;
if (!type) {
2018-01-31 03:43:01 +11:00
var type = obj[tlid].type;
2018-02-05 01:56:31 +11:00
}else{
var data;
}
if(type=="tag"){
var data=obj[tlid].data;
2018-03-14 05:31:31 +11:00
var tag = localStorage.getItem("tag-range");
if(tag=="local"){
data=data+"&local=true";
}
2018-04-16 23:58:14 +10:00
}else if(type=="list"){
var data=obj[tlid].data;
2018-01-28 23:22:43 +11:00
}
2018-07-19 02:05:53 +10:00
var sid = $("#timeline_" + tlid + " .cvo").last().attr("unique-id");
2018-01-28 23:22:43 +11:00
if (localStorage.getItem("morelock") != sid) {
localStorage.setItem("morelock", sid);
2018-08-11 01:58:00 +10:00
if (type == "mix" && localStorage.getItem("domain_" + acct_id)!="misskey.xyz") {
2018-03-27 13:39:35 +11:00
mixmore(tlid,"integrated");
return;
2018-08-11 01:58:00 +10:00
}else if (type == "plus" && localStorage.getItem("domain_" + acct_id)!="misskey.xyz") {
2018-03-27 13:39:35 +11:00
mixmore(tlid,"plus");
2018-01-28 23:22:43 +11:00
return;
2018-03-15 06:42:48 +11:00
}else if (type == "notf") {
notfmore(tlid);
return;
2018-01-28 23:22:43 +11:00
}
localStorage.setItem("now", type);
todo(cap(type) + " TL MoreLoading");
2018-07-30 21:03:49 +10:00
if(type!="noauth"){
2018-08-05 15:16:12 +10:00
var at = localStorage.getItem("acct_"+ acct_id + "_at");
2018-07-30 21:03:49 +10:00
var hdr={
2018-01-28 23:22:43 +11:00
'content-type': 'application/json',
'Authorization': 'Bearer ' + at
2018-07-30 21:03:49 +10:00
};
var domain = localStorage.getItem("domain_" + acct_id);
}else{
var hdr={
'content-type': 'application/json'
};
domain=acct_id;
}
if(domain=="misskey.xyz"){
hdr={
'content-type': 'application/json'
};
var url=misskeycom(type, data);
var start = "https://" + domain + "/api/notes/"+url;
var method="POST";
var req={};
if(type!="noauth"){
req.i=at;
}
if(type=="local-media"||type=="pub-media"){
req.mediaOnly=true;
}
if(type=="tag"){
req.tag=data;
}
if(type=="list"){
req.listId=data;
}
req.untilId=sid;
req.limit=20;
var i={
method: method,
headers: hdr,
body: JSON.stringify(req),
}
}else{
var start = "https://" + domain + "/api/v1/timelines/" + com(type,data) +
"max_id=" + sid;
var method="GET";
var i={
method: method,
headers: hdr
};
}
fetch(start, i).then(function(response) {
2018-01-28 23:22:43 +11:00
return response.json();
}).catch(function(error) {
todo(error);
console.error(error);
}).then(function(json) {
2018-07-30 21:03:49 +10:00
if(domain=="misskey.xyz"){
var templete = misskeyParse(json, '', acct_id, tlid,"",mute);
}else{
var templete = parse(json, '', acct_id, tlid,"",mute);
}
2018-01-28 23:22:43 +11:00
$("#timeline_" + tlid).append(templete);
additional(acct_id, tlid);
jQuery("time.timeago").timeago();
2018-03-15 06:42:48 +11:00
localStorage.removeItem("morelock")
2018-01-28 23:22:43 +11:00
todc();
});
}
}
//WebSocket切断
function tlCloser() {
Object.keys(websocket).forEach(function(tlid) {
if (websocketOld[tlid]) {
websocketOld[tlid].close();
console.log("Close Streaming API: Old" + tlid);
}
if (websocket[0]) {
console.log(websocket[0]);
websocket[tlid].close();
console.log("Close Streaming API:" + tlid);
}
});
websocket = [];
Object.keys(websocketHome).forEach(function(tlid) {
if (websocketHome[tlid]) {
websocketHome[tlid].close();
console.log("Close Streaming API:MixHome" + tlid);
}
});
websocketHome = [];
Object.keys(websocketLocal).forEach(function(tlid) {
if (websocketLocal[tlid]) {
websocketLocal[tlid].close();
console.log("Close Streaming API:MixLocal" + tlid);
}
});
websocketLocal = [];
2018-02-05 01:56:31 +11:00
Object.keys(websocketNotf).forEach(function(tlid) {
if (websocketNotf[tlid]) {
websocketNotf[tlid].close();
console.log("Close Streaming API:Notf" + tlid);
}
});
websocketLocal = [];
2018-01-28 23:22:43 +11:00
}
//TLのタイトル
2018-03-20 15:55:25 +11:00
function cap(type, data, acct_id) {
2018-03-27 13:39:35 +11:00
//独自ロケール
var locale = localStorage.getItem("locale");
if(locale=="yes"){
var locale=false;
}
2018-01-28 23:22:43 +11:00
if (type == "home") {
2018-03-27 13:39:35 +11:00
if(localStorage.getItem("home_" + acct_id) && !locale){
2018-03-20 15:55:25 +11:00
var response=localStorage.getItem("home_" + acct_id);
}else{
var response="Home TL";
}
2018-01-28 23:22:43 +11:00
} else if (type == "local") {
2018-03-27 13:39:35 +11:00
if(localStorage.getItem("local_" + acct_id) && !locale){
2018-03-20 15:55:25 +11:00
var response=localStorage.getItem("local_" + acct_id);
}else{
var response="Local TL";
}
2018-06-12 01:44:28 +10:00
} else if (type == "local-media") {
if(localStorage.getItem("local_" + acct_id) && !locale){
2018-07-29 19:43:08 +10:00
var response=localStorage.getItem("local_" + acct_id)+"("+lang_tl_media[lang]+")";
2018-06-12 01:44:28 +10:00
}else{
var response="Local TL(Media)";
}
2018-01-28 23:22:43 +11:00
} else if (type == "pub") {
2018-03-27 13:39:35 +11:00
if(localStorage.getItem("public_" + acct_id) && !locale){
2018-03-20 15:55:25 +11:00
var response=localStorage.getItem("public_" + acct_id);
}else{
var response="Federated TL";
}
2018-06-12 01:44:28 +10:00
} else if (type == "pub-media") {
if(localStorage.getItem("public_" + acct_id) && !locale){
2018-07-29 19:43:08 +10:00
var response=localStorage.getItem("public_" + acct_id)+"("+lang_tl_media[lang]+")";
2018-06-12 01:44:28 +10:00
}else{
var response="Federated TL(Media)";
}
2018-01-28 23:22:43 +11:00
} else if (type == "tag") {
2018-03-20 15:55:25 +11:00
var response= "#" + data
2018-01-28 23:22:43 +11:00
} else if (type == "list") {
2018-04-01 07:00:27 +10:00
var ltitle=localStorage.getItem("list_"+data+"_"+acct_id);
var response= "List(" + ltitle + ")"
2018-01-28 23:22:43 +11:00
} else if (type == "notf") {
2018-03-27 13:39:35 +11:00
if(localStorage.getItem("notification_" + acct_id) && !locale){
2018-03-20 15:55:25 +11:00
var response=localStorage.getItem("notification_" + acct_id);
}else{
var response="Notification TL";
}
2018-03-27 13:39:35 +11:00
} else if (type == "noauth") {
var response= "Glance TL"
2018-05-02 14:14:03 +10:00
} else if (type == "dm") {
var response= "DM"
2018-07-29 17:37:54 +10:00
} else if (type == "mix") {
2018-08-10 01:18:35 +10:00
if(localStorage.getItem("domain_" + acct_id)=="misskey.xyz"){
var response= "Social TL"
}else{
var response= "Integrated"
}
2018-07-29 17:37:54 +10:00
} else if (type == "plus") {
var response= "Local+"
2018-01-28 23:22:43 +11:00
}
2018-03-20 15:55:25 +11:00
return response;
2018-01-28 23:22:43 +11:00
}
//TLのURL
function com(type, data) {
if (type == "home") {
return "home?"
2018-03-27 13:39:35 +11:00
} else if (type == "local" || type == "noauth") {
2018-01-28 23:22:43 +11:00
return "public?local=true&"
2018-06-12 01:44:28 +10:00
} else if (type == "local-media") {
return "public?local=true&only_media=true&"
2018-01-28 23:22:43 +11:00
} else if (type == "pub") {
return "public?"
2018-06-12 01:44:28 +10:00
} else if (type == "pub-media") {
return "public?only_media=true&"
2018-01-28 23:22:43 +11:00
} else if (type == "tag") {
return "tag/" + data + "?"
2018-05-02 14:14:03 +10:00
}else if (type == "list") {
2018-01-28 23:22:43 +11:00
return "list/" + data + "?"
2018-05-02 14:14:03 +10:00
}else if (type="dm") {
return "direct?"
2018-01-28 23:22:43 +11:00
}
}
2018-07-30 21:03:49 +10:00
function misskeycom(type, data) {
if (type == "home") {
return "timeline"
}else if (type == "mix") {
return "hybrid-timeline"
} else if (type == "local" || type == "noauth") {
return "local-timeline"
} else if (type == "local-media") {
return "local-timeline"
} else if (type == "pub") {
return "global-timeline"
} else if (type == "pub-media") {
return "global-timeline"
} else if (type == "tag") {
return "search_by_tag"
}else if (type == "list") {
return "user-list-timeline"
}
}
2018-02-25 02:59:53 +11:00
//TLのアイコン
function icon(type) {
if (type == "home") {
return "home"
2018-06-12 01:44:28 +10:00
} else if (type == "local" || type == "noauth" || type == "local-media") {
2018-02-25 02:59:53 +11:00
return "people_outline"
2018-06-12 01:44:28 +10:00
} else if (type == "pub" || type == "pub-media") {
2018-02-25 02:59:53 +11:00
return "language"
} else if (type == "tag") {
return "search"
2018-04-01 07:00:27 +10:00
} else if (type == "list") {
return "view_headline"
2018-05-02 14:14:03 +10:00
}else if (type == "list") {
2018-02-25 02:59:53 +11:00
return "subject"
2018-05-02 14:14:03 +10:00
}else if (type == "dm") {
return "mail"
2018-07-30 21:03:49 +10:00
}else if (type == "mix") {
return "share"
2018-02-25 02:59:53 +11:00
}
2018-03-27 13:39:35 +11:00
}