thedesk/app/js/tl/notification.js

158 lines
4.5 KiB
JavaScript
Raw Normal View History

2018-01-28 23:22:43 +11:00
//通知
//取得+Streaming接続
function notf(acct_id, tlid, sys) {
todo("Notifications Loading...");
2018-03-31 13:39:06 +11:00
var native=localStorage.getItem("nativenotf");
if(!native){
native="yes";
}
2018-01-28 23:22:43 +11:00
var domain = localStorage.getItem("domain_" + acct_id);
var at = localStorage.getItem(domain + "_at");
var start = "https://" + domain + "/api/v1/notifications";
fetch(start, {
method: 'GET',
headers: {
'content-type': 'application/json',
'Authorization': 'Bearer ' + at
},
//body: JSON.stringify({})
}).then(function(response) {
return response.json();
}).catch(function(error) {
todo(error);
console.error(error);
}).then(function(json) {
2018-02-26 02:32:10 +11:00
var templete="";
2018-03-31 13:39:06 +11:00
var lastnotf=localStorage.getItem("lastnotf_" + acct_id);
localStorage.setItem("lastnotf_" + acct_id,json[0].id);
2018-02-26 02:32:10 +11:00
Object.keys(json).forEach(function(key) {
var obj = json[key];
2018-03-31 13:39:06 +11:00
if(lastnotf==obj.id && key>0 && native=="yes"){
var ct=key+1;
if(key==14){
ct="15+";
}
var electron = require("electron");
var ipc = electron.ipcRenderer;
ipc.send('native-notf', ['TheDesk:'+domain,ct+"件の新しい通知",localStorage.getItem("prof_"+acct_id)]);
}
2018-02-26 02:32:10 +11:00
if(obj.type!="follow"){
2018-03-20 15:55:25 +11:00
templete = templete+parse([obj], 'notf', acct_id, tlid, -1);
2018-02-26 02:32:10 +11:00
}else{
2018-03-20 15:55:25 +11:00
templete = templete+userparse([obj.account], 'notf', acct_id, tlid, -1);
2018-02-26 02:32:10 +11:00
}
});
2018-03-14 05:31:31 +11:00
2018-01-28 23:22:43 +11:00
if (sys == "direct") {
2018-02-26 00:37:04 +11:00
$("#timeline_" + tlid).html(templete);
2018-01-28 23:22:43 +11:00
} else {
2018-03-14 05:31:31 +11:00
$("div[data-notf=" + acct_id +"]").html(templete);
2018-01-28 23:22:43 +11:00
}
jQuery("time.timeago").timeago();
$("#notf-box").addClass("fetched");
todc();
});
var start = "wss://" + domain + "/api/v1/streaming/?stream=user&access_token=" +
at;
console.log(start);
2018-02-05 01:56:31 +11:00
var wsid = websocketNotf.length;
websocketNotf[wsid] = new WebSocket(start);
console.log(websocketNotf);
websocketNotf[wsid].onopen = function(mess) {
2018-03-14 05:31:31 +11:00
console.log("Connect Streaming API(Notf):");
2018-01-28 23:22:43 +11:00
console.log(mess);
2018-03-14 05:31:31 +11:00
$("i[data-notf=" + acct_id +"]").removeClass("red-text");
2018-01-28 23:22:43 +11:00
}
2018-02-05 01:56:31 +11:00
websocketNotf[wsid].onmessage = function(mess) {
2018-03-14 05:31:31 +11:00
console.log("Receive Streaming API(Notf):"+acct_id);
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 == "notification") {
var popup = localStorage.getItem("popup");
if (!popup) {
popup = 0;
}
2018-03-11 01:22:59 +11:00
var templete="";
2018-03-31 13:39:06 +11:00
localStorage.setItem("lastnotf_" + acct_id,obj.id);
2018-03-11 01:22:59 +11:00
if(obj.type!="follow"){
2018-04-01 00:55:47 +11:00
templete = parse([obj], 'notf', acct_id, tlid, popup);
2018-02-26 02:32:10 +11:00
}else{
2018-04-01 00:55:47 +11:00
templete = userparse([obj], 'notf', acct_id, tlid, popup);
}
if(!$("div[data-notfIndv=" + acct_id +"_"+obj.id+"]").length){
$("div[data-notf=" + acct_id +"]").prepend(templete);
2018-02-26 02:32:10 +11:00
}
2018-01-28 23:22:43 +11:00
jQuery("time.timeago").timeago();
2018-02-25 02:59:53 +11:00
} else if (type == "delete") {
$("[toot-id=" + obj + "]").hide();
$("[toot-id=" + obj + "]").remove();
2018-01-28 23:22:43 +11:00
}
}
2018-02-05 01:56:31 +11:00
websocketNotf[wsid].onerror = function(error) {
2018-01-28 23:22:43 +11:00
console.error('WebSocket Error ' + error);
};
}
2018-03-15 06:42:48 +11:00
//一定のスクロールで発火
function notfmore(tlid) {
var multi = localStorage.getItem("column");
var obj = JSON.parse(multi);
var acct_id = obj[tlid].domain;
if (!type) {
var type = obj[tlid].type;
}else{
var data;
}
var sid = $("#timeline_" + tlid + " .cvo").last().attr("toot-id");
console.log(sid);
if (localStorage.getItem("morelock") != sid) {
localStorage.setItem("morelock", sid);
localStorage.setItem("now", type);
todo("Notfication TL MoreLoading");
var domain = localStorage.getItem("domain_" + acct_id);
var at = localStorage.getItem(domain + "_at");
var start = "https://" + domain + "/api/v1/notifications"+
"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(json) {
var templete="";
Object.keys(json).forEach(function(key) {
var obj = json[key];
if(obj.type!="follow"){
templete = templete+parse([obj], '', acct_id, tlid, -1);
}else{
templete = templete+userparse([obj.account], '', acct_id, tlid, -1);
}
});
$("#timeline_" + tlid).append(templete);
additional(acct_id, tlid);
jQuery("time.timeago").timeago();
localStorage.removeItem("morelock")
todc();
});
}
}
2018-01-28 23:22:43 +11:00
//通知トグルボタン
function notfToggle(acct, tlid) {
$("#notf-box_" + tlid).toggleClass("hide");
2018-02-25 02:59:53 +11:00
$("#notf-box_" + tlid).toggleClass("show");
2018-03-14 05:31:31 +11:00
$(".notf-icon_" + acct).removeClass("red-text");
2018-01-28 23:22:43 +11:00
}