thedesk/app/js/tl/mix.js

237 lines
7.6 KiB
JavaScript
Raw Normal View History

2018-01-28 23:22:43 +11:00
//Integrated TL
2018-03-27 13:39:35 +11:00
function mixtl(acct_id, tlid, type) {
console.log(type);
2018-01-28 23:22:43 +11:00
localStorage.removeItem("morelock")
localStorage.setItem("now", type);
todo("Integrated TL Loading...(Local)");
2018-03-15 06:42:48 +11:00
//まずLocal
var domain = localStorage.getItem("domain_" + acct_id);
2018-07-07 03:51:48 +10:00
var at = localStorage.getItem("acct_"+ acct_id + "_at");
2018-03-15 06:42:48 +11:00
var start = "https://" + domain + "/api/v1/timelines/public?local=true";
2018-01-28 23:22:43 +11:00
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);
2018-03-15 06:42:48 +11:00
}).then(function(jsonL) {
var start = "https://" + domain + "/api/v1/timelines/home";
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(jsonH) {
var homearr=[];
var timeline = jsonL.concat(jsonH);
timeline.sort(function(a,b){
2018-06-18 00:26:45 +10:00
if(date(a.created_at,"unix")>=date(b.created_at,"unix")) return -1;
2018-03-15 06:42:48 +11:00
if(date(a.created_at,"unix")<date(b.created_at,"unix")) return 1;
return 0;
2018-03-27 13:39:35 +11:00
});
if(type=="integrated"){
timeline.splice(20);
}
2018-03-15 06:42:48 +11:00
var templete="";
Object.keys(timeline).forEach(function(key) {
var pkey=key*1+1;
2018-03-27 13:39:35 +11:00
if(pkey<timeline.length){
2018-03-15 06:42:48 +11:00
if(date(timeline[key].created_at,"unix")!=date(timeline[pkey].created_at,"unix")){
2018-03-27 13:39:35 +11:00
if(type=="integrated"){
templete = templete+parse([timeline[key]], '', acct_id, tlid);
}else if(type=="plus"){
if(timeline[key].account.acct==timeline[key].account.username){
templete = templete+parse([timeline[key]], '', acct_id, tlid);
}
}
2018-03-15 06:42:48 +11:00
}
}
2018-01-28 23:22:43 +11:00
2018-03-15 06:42:48 +11:00
});
$("#timeline_" + tlid).html(templete);
2018-03-27 13:39:35 +11:00
mixre(acct_id, tlid, type);
2018-01-28 23:22:43 +11:00
additional(acct_id, tlid);
jQuery("time.timeago").timeago();
2018-03-15 06:42:48 +11:00
todc();
});
});
2018-01-28 23:22:43 +11:00
}
2018-03-15 06:42:48 +11:00
2018-01-28 23:22:43 +11:00
//Streamingに接続
2018-03-27 13:39:35 +11:00
function mixre(acct_id, tlid, TLtype) {
2018-01-28 23:22:43 +11:00
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
var startHome = "wss://" + domain +
"/api/v1/streaming/?stream=user&access_token=" + at;
var startLocal = "wss://" + domain +
"/api/v1/streaming/?stream=public:local&access_token=" + at;
var wshid = websocketHome.length;
var wslid = websocketLocal.length;
websocketHome[wshid] = new WebSocket(startHome);
websocketLocal[wslid] = new WebSocket(startLocal);
websocketHome[wshid].onopen = function(mess) {
2018-06-18 00:26:45 +10:00
console.log("Connect Streaming API(Integrated:Home)");
2018-03-11 01:22:59 +11:00
$("#notice_icon_" + tlid).removeClass("red-text");
2018-01-28 23:22:43 +11:00
}
websocketLocal[wslid].onopen = function(mess) {
2018-06-18 00:26:45 +10:00
console.log("Connect Streaming API(Integrated:Local)");
2018-03-11 01:22:59 +11:00
$("#notice_icon_" + tlid).removeClass("red-text");
2018-01-28 23:22:43 +11:00
}
websocketLocal[wslid].onmessage = function(mess) {
2018-06-18 00:26:45 +10:00
console.log("Receive Streaming API:(Integrated:Local)");
2018-01-28 23:22:43 +11:00
var obj = JSON.parse(JSON.parse(mess.data).payload);
console.log(obj);
var type = JSON.parse(mess.data).event;
if (type == "delete") {
2018-02-25 19:41:34 +11:00
$("[toot-id=" + JSON.parse(mess.data).payload + "]").hide();
$("[toot-id=" + JSON.parse(mess.data).payload + "]").remove();
2018-01-28 23:22:43 +11:00
} else if (type == "update") {
2018-02-26 00:37:04 +11:00
var templete = parse([obj], '', acct_id, tlid);
2018-06-18 00:26:45 +10:00
if (!$("#timeline_"+tlid+" [toot-id="+obj.id+"]").length) {
2018-01-28 23:22:43 +11:00
var pool = localStorage.getItem("pool_" + tlid);
2018-06-18 00:26:45 +10:00
if (pool && templete) {
2018-01-28 23:22:43 +11:00
pool = templete + pool;
2018-06-18 00:26:45 +10:00
} else if (templete) {
2018-01-28 23:22:43 +11:00
pool = templete
2018-06-18 00:26:45 +10:00
}else{
pool="";
2018-01-28 23:22:43 +11:00
}
localStorage.setItem("pool_" + tlid, pool);
scrollck();
additional(acct_id, tlid);
jQuery("time.timeago").timeago();
todc();
2018-03-27 13:39:35 +11:00
}}
2018-01-28 23:22:43 +11:00
}
websocketHome[wshid].onmessage = function(mess) {
2018-06-18 00:26:45 +10:00
console.log("Receive Streaming API:(Integrated:Home)");
2018-01-28 23:22:43 +11:00
var obj = JSON.parse(JSON.parse(mess.data).payload);
console.log(obj);
var type = JSON.parse(mess.data).event;
if (type == "delete") {
2018-02-25 19:41:34 +11:00
$("[toot-id=" + JSON.parse(mess.data).payload + "]").hide();
$("[toot-id=" + JSON.parse(mess.data).payload + "]").remove();
2018-01-28 23:22:43 +11:00
} else if (type == "update") {
2018-03-27 13:39:35 +11:00
if(TLtype=="integrated"){
var templete = parse([obj], '', acct_id, tlid);
}else if(TLtype=="plus"){
if(obj.account.acct==obj.account.username){
var templete = parse([obj], '', acct_id, tlid);
}else{
var templete="";
}
}
2018-06-18 00:26:45 +10:00
if (!$("#timeline_"+tlid+" [toot-id="+obj.id+"]").length) {
2018-02-25 19:41:34 +11:00
var pool = localStorage.getItem("pool_" + tlid);
2018-06-18 00:26:45 +10:00
if (pool && templete) {
2018-02-25 19:41:34 +11:00
pool = templete + pool;
2018-06-18 00:26:45 +10:00
} else if (templete) {
2018-02-25 19:41:34 +11:00
pool = templete
2018-06-18 00:26:45 +10:00
}else{
pool="";
2018-01-28 23:22:43 +11:00
}
2018-02-25 19:41:34 +11:00
localStorage.setItem("pool_" + tlid, pool);
scrollck();
2018-01-28 23:22:43 +11:00
additional(acct_id, tlid);
jQuery("time.timeago").timeago();
}
}
}
websocketLocal[wslid].onerror = function(error) {
console.error('WebSocket Error ' + error);
};
websocketHome[wshid].onerror = function(error) {
console.error('WebSocket Error ' + error);
};
}
//ある程度のスクロールで発火
2018-03-27 13:39:35 +11:00
function mixmore(tlid,type) {
2018-01-28 23:22:43 +11:00
var multi = localStorage.getItem("column");
var obj = JSON.parse(multi);
var acct_id = obj[tlid].domain;
todo("Integrated TL MoreLoading...(Local)");
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
var sid = $("#timeline_" + tlid + " .cvo").last().attr("toot-id");
2018-03-15 06:42:48 +11:00
var start = "https://" + domain + "/api/v1/timelines/public?local=true&max_id="+sid;
2018-01-28 23:22:43 +11:00
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);
2018-03-15 06:42:48 +11:00
}).then(function(jsonL) {
var start = "https://" + domain + "/api/v1/timelines/home?max_id="+sid;
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(jsonH) {
var homearr=[];
var timeline = jsonL.concat(jsonH);
timeline.sort(function(a,b){
if(date(a.created_at,"unix")>date(b.created_at,"unix")) return -1;
if(date(a.created_at,"unix")<date(b.created_at,"unix")) return 1;
return 0;
});
timeline.splice(20);
var templete="";
Object.keys(timeline).forEach(function(key) {
var pkey=key*1+1;
if(pkey<20){
if(date(timeline[key].created_at,"unix")!=date(timeline[pkey].created_at,"unix")){
2018-03-27 13:39:35 +11:00
if(type=="integrated"){
templete = templete+parse([timeline[key]], '', acct_id, tlid);
}else if(type=="plus"){
if(timeline[key].account.acct==timeline[key].account.username){
templete = templete+parse([timeline[key]], '', acct_id, tlid);
}
}
2018-03-15 06:42:48 +11:00
}
}
2018-01-28 23:22:43 +11:00
2018-03-15 06:42:48 +11:00
});
$("#timeline_" + tlid).append(templete);
mixre(acct_id, tlid);
2018-01-28 23:22:43 +11:00
additional(acct_id, tlid);
jQuery("time.timeago").timeago();
todc();
2018-03-15 06:42:48 +11:00
});
2018-01-28 23:22:43 +11:00
});
2018-03-15 06:42:48 +11:00
2018-01-28 23:22:43 +11:00
2018-03-15 06:42:48 +11:00
}