thedesk/app/js/tl/notification.js

319 lines
8.8 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");
2018-07-30 21:03:49 +10:00
var at = localStorage.getItem("acct_"+ acct_id + "_at");
2018-03-31 13:39:06 +11:00
if(!native){
native="yes";
}
2018-01-28 23:22:43 +11:00
var domain = localStorage.getItem("domain_" + acct_id);
2018-08-23 03:29:39 +10:00
if(localStorage.getItem("mode_" + domain)=="misskey"){
var misskey=true;
2018-07-30 21:03:49 +10:00
var start = "https://" + domain + "/api/i/notifications";
var i={
method: 'POST',
headers: {
'content-type': 'application/json',
},
body:JSON.stringify({
i:at
})
}
}else{
2018-08-23 03:29:39 +10:00
var misskey=false;
2018-07-30 21:03:49 +10:00
var start = "https://" + domain + "/api/v1/notifications";
var i={
method: 'GET',
headers: {
'content-type': 'application/json',
'Authorization': 'Bearer ' + at
},
}
}
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-17 01:39:06 +10:00
if(json[0]){
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"){
2018-05-02 14:14:03 +10:00
var ct=key;
if(key>14){
2018-03-31 13:39:06 +11:00
ct="15+";
}
var electron = require("electron");
var ipc = electron.ipcRenderer;
2018-05-26 05:00:04 +10:00
var os = electron.remote.process.platform;
var options = {
2018-07-29 17:37:54 +10:00
body: ct+lang_notf_new[lang],
2018-07-17 01:39:06 +10:00
icon: localStorage.getItem("prof_"+acct_id)
2018-05-26 05:00:04 +10:00
};
if(os=="darwin"){
var n = new Notification('TheDesk:'+domain, options);
}else{
2018-07-29 17:37:54 +10:00
ipc.send('native-notf', ['TheDesk:'+domain,ct+lang_notf_new[lang],localStorage.getItem("prof_"+acct_id)]);
2018-05-26 05:00:04 +10:00
}
2018-03-31 13:39:06 +11:00
}
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)),"notif");
}else{
var mute=[];
}
2018-02-26 02:32:10 +11:00
if(obj.type!="follow"){
2018-08-23 03:29:39 +10:00
if(misskey){
2018-07-30 21:03:49 +10:00
templete = templete+misskeyParse([obj], 'notf', acct_id, 'notf', -1, mute);
}else{
templete = templete+parse([obj], 'notf', acct_id, 'notf', -1, mute);
}
2018-02-26 02:32:10 +11:00
}else{
2018-08-23 03:29:39 +10:00
if(misskey){
2018-07-30 21:03:49 +10:00
templete = templete+misskeyUserparse([obj], 'notf', acct_id, 'notf', -1, mute);
}else{
templete = templete+userparse([obj.account], 'notf', acct_id, 'notf', -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
}
2018-08-17 03:21:40 +10:00
$("#landing_" + tlid).hide();
2018-01-28 23:22:43 +11:00
jQuery("time.timeago").timeago();
2018-07-17 01:39:06 +10:00
}
2018-01-28 23:22:43 +11:00
$("#notf-box").addClass("fetched");
todc();
});
2018-08-23 03:29:39 +10:00
if(!misskey){
2018-09-17 21:55:00 +10:00
if(localStorage.getItem("streaming_" + acct_id)){
var wss=localStorage.getItem("streaming_" + acct_id)
}else{
var wss="wss://"+domain
}
var start = wss + "/api/v1/streaming/?stream=user&access_token=" +
2018-07-30 21:03:49 +10:00
at;
}else{
var start = "wss://" + domain + "/?i=" +
2018-01-28 23:22:43 +11:00
at;
2018-07-30 21:03:49 +10:00
}
2018-01-28 23:22:43 +11:00
console.log(start);
2018-02-05 01:56:31 +11:00
var wsid = websocketNotf.length;
2018-08-17 03:21:40 +10:00
websocketNotf[acct_id] = new WebSocket(start);
2018-02-05 01:56:31 +11:00
console.log(websocketNotf);
2018-08-17 03:21:40 +10:00
websocketNotf[acct_id].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-08-17 03:21:40 +10:00
2018-01-28 23:22:43 +11:00
}
2018-08-17 03:21:40 +10:00
websocketNotf[acct_id].onmessage = function(mess) {
2018-03-14 05:31:31 +11:00
console.log("Receive Streaming API(Notf):"+acct_id);
2018-07-30 21:03:49 +10:00
var popup = localStorage.getItem("popup");
if (!popup) {
popup = 0;
}
2018-08-17 03:21:40 +10:00
console.log(domain)
2018-08-23 03:29:39 +10:00
if(misskey){
2018-08-17 03:21:40 +10:00
console.log("misskey")
2018-07-30 21:03:49 +10:00
console.log(JSON.parse(mess.data));
if (JSON.parse(mess.data).type == "notification") {
var obj = JSON.parse(mess.data).body;
console.log(obj);
if(obj.type!="follow"){
2018-08-17 03:21:40 +10:00
2018-07-30 21:03:49 +10:00
templete = misskeyParse([obj], 'notf', acct_id, 'notf', popup);
}else{
templete = misskeyUserparse([obj], 'notf', acct_id, 'notf', popup);
}
2018-08-17 03:21:40 +10:00
if(obj.type=="reaction"){
console.log("refresh")
reactRefresh(acct_id,obj.note.id)
}
2018-07-30 21:03:49 +10:00
if(!$("div[data-notfIndv=" + acct_id +"_"+obj.id+"]").length){
$("div[data-notf=" + acct_id +"]").prepend(templete);
}
jQuery("time.timeago").timeago();
2018-08-17 03:21:40 +10:00
}else if(JSON.parse(mess.data).type == "note-updated"){
var obj = JSON.parse(mess.data).body.note;
reactRefreshCore(obj)
2018-07-30 21:03:49 +10:00
}
}else{
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") {
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-05-02 14:14:03 +10:00
templete = parse([obj], 'notf', acct_id, 'notf', popup);
2018-02-26 02:32:10 +11:00
}else{
2018-05-02 14:14:03 +10:00
templete = userparse([obj], 'notf', acct_id, 'notf', popup);
2018-04-01 00:55:47 +11:00
}
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-07-30 21:03:49 +10:00
}
2018-01-28 23:22:43 +11:00
}
2018-08-17 03:21:40 +10:00
websocketNotf[acct_id].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);
2018-07-07 03:51:48 +10:00
var at = localStorage.getItem("acct_"+ acct_id + "_at");
2018-08-17 03:21:40 +10:00
2018-08-23 03:29:39 +10:00
if(localStorage.getItem("mode_" + domain)=="misskey"){
var misskey=true;
2018-07-30 21:03:49 +10:00
var start = "https://" + domain + "/api/i/notifications";
var i={
method: 'POST',
headers: {
'content-type': 'application/json',
},
body:JSON.stringify({
i:at,
untilId:sid
})
}
}else{
2018-08-23 03:29:39 +10:00
var misskey=false;
2018-07-30 21:03:49 +10:00
var start = "https://" + domain + "/api/v1/notifications"+
"max_id=" + sid;
var i={
method: 'GET',
headers: {
'content-type': 'application/json',
'Authorization': 'Bearer ' + at
},
}
}
2018-08-17 03:21:40 +10:00
fetch(start, i,
).then(function(response) {
2018-03-15 06:42:48 +11:00
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"){
2018-08-23 03:29:39 +10:00
if(misskey){
2018-08-17 03:21:40 +10:00
templete = templete+misskeyParse([obj.note], '', acct_id, tlid, -1);
2018-07-30 21:03:49 +10:00
}else{
templete = templete+parse([obj], '', acct_id, tlid, -1);
}
2018-03-15 06:42:48 +11:00
}else{
2018-08-23 03:29:39 +10:00
if(misskey){
2018-07-30 21:03:49 +10:00
templete = templete+misskeyUserparse([obj], '', acct_id, tlid, -1);
}else{
templete = templete+userparse([obj.account], '', acct_id, tlid, -1);
}
2018-03-15 06:42:48 +11:00
}
});
$("#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) {
2018-08-10 01:18:35 +10:00
if($("#notf-box_" + tlid).hasClass("column-hide")){
$("#notf-box_" + tlid).css("display","block")
$("#notf-box_" + tlid).animate({
'height': '400px'
},{
'duration': 300,
'complete': function(){
$("#notf-box_" + tlid).css("overflow-y","scroll")
$("#notf-box_" + tlid).removeClass("column-hide")
}
});
}else{
$("#notf-box_" + tlid).css("overflow-y","hidden")
$("#notf-box_" + tlid).animate({
'height': '0'
},{
'duration': 300,
'complete': function(){
$("#notf-box_" + tlid).addClass("column-hide")
$("#notf-box_" + tlid).css("display","none")
}
});
}
2018-07-28 07:25:12 +10:00
notfCanceler(acct)
}
function notfCanceler(acct){
2018-07-29 19:43:08 +10:00
$(".notf-reply_" + acct).text(0);
localStorage.removeItem("notf-reply_" + acct)
2018-07-29 19:43:08 +10:00
$(".notf-reply_" + acct).addClass("hide");
$(".notf-fav_" + acct).text(0);
localStorage.removeItem("notf-fav_" + acct)
2018-07-29 19:43:08 +10:00
$(".notf-fav_" + acct).addClass("hide");
$(".notf-bt_" + acct).text(0);
localStorage.removeItem("notf-bt_" + acct)
2018-07-29 19:43:08 +10:00
$(".notf-bt_" + acct).addClass("hide");
$(".notf-follow_" + acct).text(0);
localStorage.removeItem("notf-follow_" + acct)
2018-07-29 19:43:08 +10:00
$(".notf-follow_" + acct).addClass("hide");
2018-03-14 05:31:31 +11:00
$(".notf-icon_" + acct).removeClass("red-text");
2018-01-28 23:22:43 +11:00
}
2018-07-28 07:25:12 +10:00
function allNotfRead(){
console.log(localStorage.getItem("notf-fav_2"));
2018-07-28 07:25:12 +10:00
var multi = localStorage.getItem("multi");
if (!multi) {
var obj = [{
at: localStorage.getItem("acct_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);
}
Object.keys(obj).forEach(function(key) {
2018-07-29 19:43:08 +10:00
console.log(key);
2018-07-28 07:25:12 +10:00
notfCanceler(key)
});
}
allNotfRead()