thedesk/app/js/tl/notification.js

493 lines
15 KiB
JavaScript
Raw Normal View History

2018-01-28 23:22:43 +11:00
//通知
//取得+Streaming接続
2019-01-26 14:24:26 +11:00
function notf(acct_id, tlid, sys){
if (sys == "direct") {
notfColumn(acct_id, tlid, sys)
} else {
notfCommon(acct_id, tlid, sys)
}
}
function notfColumn(acct_id, tlid, sys){
2018-01-28 23:22:43 +11:00
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);
2019-03-06 19:08:48 +11:00
console.log(start)
var httpreq = new XMLHttpRequest();
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";
2019-03-06 19:08:48 +11:00
httpreq.open(POST, start, true);
httpreq.setRequestHeader('Content-Type', 'application/json');
var body=JSON.stringify({
i:at
});
2018-07-30 21:03:49 +10:00
}else{
2018-08-23 03:29:39 +10:00
var misskey=false;
2019-01-26 14:24:26 +11:00
if(localStorage.getItem("exclude-"+tlid)){
var exc=localStorage.getItem("exclude-"+tlid);
}else{
var exc="";
}
var start = "https://" + domain + "/api/v1/notifications"+exc;
2019-03-06 19:08:48 +11:00
httpreq.open("GET", start, true);
httpreq.setRequestHeader('Content-Type', 'application/json');
httpreq.setRequestHeader('Authorization', 'Bearer ' + at);
var body="";
2018-07-30 21:03:49 +10:00
}
2019-03-06 19:08:48 +11:00
2019-03-08 05:19:26 +11:00
httpreq.responseType = "json";
2019-03-06 19:08:48 +11:00
httpreq.send(body);
httpreq.onreadystatechange = function() {
2019-03-08 05:19:26 +11:00
if (httpreq.readyState === 4) {
2019-03-06 19:08:48 +11:00
var json = httpreq.response;
2019-03-13 02:51:07 +11:00
var max_id = httpreq.getResponseHeader("link").match(/[?&]{1}max_id=([0-9]+)/)[1];
2019-03-06 19:08:48 +11:00
if(json[0]){
var templete="";
var lastnotf=localStorage.getItem("lastnotf_" + acct_id);
localStorage.setItem("lastnotf_" + acct_id,json[0].id);
Object.keys(json).forEach(function(key) {
var obj = json[key];
if(lastnotf==obj.id && key>0 && native=="yes"){
var ct=key;
if(key>14){
ct="15+";
}
var electron = require("electron");
var ipc = electron.ipcRenderer;
var os = electron.remote.process.platform;
var options = {
body: ct+lang_notf_new[lang],
icon: localStorage.getItem("prof_"+acct_id)
};
if(os=="darwin"){
var n = new Notification('TheDesk:'+domain, options);
}else{
ipc.send('native-notf', ['TheDesk:'+domain,ct+lang_notf_new[lang],localStorage.getItem("prof_"+acct_id)]);
}
}
if(localStorage.getItem("filter_"+ acct_id)!="undefined"){
var mute=getFilterType(JSON.parse(localStorage.getItem("filter_"+ acct_id)),"notif");
2018-05-26 05:00:04 +10:00
}else{
2019-03-06 19:08:48 +11:00
var mute=[];
2018-05-26 05:00:04 +10:00
}
2019-03-06 19:08:48 +11:00
if(obj.type!="follow"){
if(misskey){
templete = templete+misskeyParse([obj], 'notf', acct_id, 'notf', -1, mute);
}else{
templete = templete+parse([obj], 'notf', acct_id, 'notf', -1, mute);
}
}else{
if(misskey){
templete = templete+misskeyUserparse([obj], 'notf', acct_id, 'notf', -1, mute);
}else{
templete = templete+userparse([obj.account], 'notf', acct_id, 'notf', -1);
}
}
});
templete=templete+'<div class="hide notif-marker" data-maxid="'+max_id+'"></div>';
$("#timeline_" + tlid).html(templete);
$("#landing_" + tlid).hide();
jQuery("time.timeago").timeago();
2018-07-30 21:03:49 +10:00
}
2019-03-06 19:08:48 +11:00
$("#notf-box").addClass("fetched");
todc();
2019-01-26 14:24:26 +11:00
}
2019-03-06 19:08:48 +11:00
}
2019-01-26 14:24:26 +11:00
if(!misskey){
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=" +
at;
}else{
var start = "wss://" + domain + "/?i=" +
at;
}
console.log(start);
var wsid = websocketNotf.length;
websocketNotf[acct_id] = new WebSocket(start);
console.log(websocketNotf);
websocketNotf[acct_id].onopen = function(mess) {
console.log("Connect Streaming API(Notf):");
console.log(mess);
$("i[data-notf=" + acct_id +"]").removeClass("red-text");
}
websocketNotf[acct_id].onmessage = function(mess) {
console.log("Receive Streaming API(Notf):"+acct_id);
var popup = localStorage.getItem("popup");
if (!popup) {
popup = 0;
}
console.log(domain)
if(misskey){
console.log("misskey")
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"){
templete = misskeyParse([obj], 'notf', acct_id, 'notf', popup);
}else{
templete = misskeyUserparse([obj], 'notf', acct_id, 'notf', popup);
}
if(obj.type=="reaction"){
console.log("refresh")
reactRefresh(acct_id,obj.note.id)
}
if(!$("div[data-notfIndv=" + acct_id +"_"+obj.id+"]").length){
$("div[data-notf=" + acct_id +"]").prepend(templete);
}
jQuery("time.timeago").timeago();
}else if(JSON.parse(mess.data).type == "note-updated"){
var obj = JSON.parse(mess.data).body.note;
reactRefreshCore(obj)
}
}else{
var obj = JSON.parse(JSON.parse(mess.data).payload);
console.log(obj);
var type = JSON.parse(mess.data).event;
if (type == "notification") {
var templete="";
localStorage.setItem("lastnotf_" + acct_id,obj.id);
if(obj.type!="follow"){
templete = parse([obj], 'notf', acct_id, 'notf', popup);
}else{
templete = userparse([obj], 'notf', acct_id, 'notf', popup);
}
if(!$("div[data-notfIndv=" + acct_id +"_"+obj.id+"]").length){
$(".tl[data-notf=" + acct_id +"]").prepend(templete);
}
2019-02-28 04:02:23 +11:00
$(".notf-timeline[data-acct=" + acct_id +"]").prepend(templete);
2019-01-26 14:24:26 +11:00
jQuery("time.timeago").timeago();
} else if (type == "delete") {
$("[toot-id=" + obj + "]").hide();
$("[toot-id=" + obj + "]").remove();
}
}
}
websocketNotf[acct_id].onerror = function(error) {
console.error('WebSocket Error ' + error);
};
}
function notfCommon(acct_id, tlid, sys) {
todo("Notifications Loading...");
var native=localStorage.getItem("nativenotf");
var at = localStorage.getItem("acct_"+ acct_id + "_at");
if(!native){
native="yes";
}
var domain = localStorage.getItem("domain_" + acct_id);
if(localStorage.getItem("mode_" + domain)=="misskey"){
var misskey=true;
var start = "https://" + domain + "/api/i/notifications";
var i={
method: 'POST',
headers: {
'content-type': 'application/json',
},
body:JSON.stringify({
i:at
})
}
}else{
var misskey=false;
var start = "https://" + domain + "/api/v1/notifications";
var i={
method: 'GET',
headers: {
'content-type': 'application/json',
'Authorization': 'Bearer ' + at
},
2018-01-28 23:22:43 +11:00
}
2019-01-26 14:24:26 +11:00
}
fetch(start, i).then(function(response) {
console.log(response.headers.get('link'));
return response.json();
}).catch(function(error) {
todo(error);
console.error(error);
}).then(function(json) {
if(json[0]){
var templete="";
var lastnotf=localStorage.getItem("lastnotf_" + acct_id);
localStorage.setItem("lastnotf_" + acct_id,json[0].id);
Object.keys(json).forEach(function(key) {
var obj = json[key];
if(lastnotf==obj.id && key>0 && native=="yes"){
var ct=key;
if(key>14){
ct="15+";
}
var electron = require("electron");
var ipc = electron.ipcRenderer;
var os = electron.remote.process.platform;
var options = {
body: ct+lang_notf_new[lang],
icon: localStorage.getItem("prof_"+acct_id)
};
if(os=="darwin"){
var n = new Notification('TheDesk:'+domain, options);
}else{
ipc.send('native-notf', ['TheDesk:'+domain,ct+lang_notf_new[lang],localStorage.getItem("prof_"+acct_id)]);
}
}
if(localStorage.getItem("filter_"+ acct_id)!="undefined"){
var mute=getFilterType(JSON.parse(localStorage.getItem("filter_"+ acct_id)),"notif");
}else{
var mute=[];
}
if(obj.type!="follow"){
if(misskey){
templete = templete+misskeyParse([obj], 'notf', acct_id, 'notf', -1, mute);
}else{
templete = templete+parse([obj], 'notf', acct_id, 'notf', -1, mute);
}
}else{
if(misskey){
templete = templete+misskeyUserparse([obj], 'notf', acct_id, 'notf', -1, mute);
}else{
templete = templete+userparse([obj.account], 'notf', acct_id, 'notf', -1);
}
}
});
$("div[data-notf=" + acct_id +"]").html(templete);
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) {
2019-03-06 19:08:48 +11:00
console.log(moreloading);
console.log("kicked");
2018-03-15 06:42:48 +11:00
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;
}
2019-03-06 19:08:48 +11:00
var sid = $("#timeline_" + tlid + " .notif-marker").last().attr("data-maxid");
var at = localStorage.getItem("acct_"+ acct_id + "_at");
var domain = localStorage.getItem("domain_" + acct_id);
if (sid && !moreloading) {
moreloading=true;
var httpreq = new XMLHttpRequest();
if(localStorage.getItem("mode_" + domain)=="misskey"){
var misskey=true;
var start = "https://" + domain + "/api/i/notifications";
httpreq.open(POST, start, true);
httpreq.setRequestHeader('Content-Type', 'application/json');
var body=JSON.stringify({
i:at,
untilID:sid
});
}else{
var misskey=false;
if(localStorage.getItem("exclude-"+tlid)){
var exc=localStorage.getItem("exclude-"+tlid)+"&max_id="+sid;
}else{
var exc="?max_id="+sid;
}
var start = "https://" + domain + "/api/v1/notifications"+exc;
httpreq.open("GET", start, true);
httpreq.setRequestHeader('Content-Type', 'application/json');
httpreq.setRequestHeader('Authorization', 'Bearer ' + at);
var body="";
}
2019-03-08 05:19:26 +11:00
httpreq.responseType = "json";
2019-03-06 19:08:48 +11:00
httpreq.send(body);
httpreq.onreadystatechange = function() {
2019-03-08 05:19:26 +11:00
if (httpreq.readyState === 4) {
2019-03-06 19:08:48 +11:00
var json = httpreq.response;
console.log(json);
2019-03-13 02:51:07 +11:00
var max_id = httpreq.getResponseHeader("link").match(/[?&]{1}max_id=([0-9]+)/)[1];
2019-03-06 19:08:48 +11:00
if(json[0]){
var templete="";
var lastnotf=localStorage.getItem("lastnotf_" + acct_id);
localStorage.setItem("lastnotf_" + acct_id,json[0].id);
Object.keys(json).forEach(function(key) {
var obj = json[key];
if(localStorage.getItem("filter_"+ acct_id)!="undefined"){
var mute=getFilterType(JSON.parse(localStorage.getItem("filter_"+ acct_id)),"notif");
2018-07-30 21:03:49 +10:00
}else{
2019-03-06 19:08:48 +11:00
var mute=[];
2018-07-30 21:03:49 +10:00
}
2019-03-06 19:08:48 +11:00
if(obj.type!="follow"){
if(misskey){
templete = templete+misskeyParse([obj], 'notf', acct_id, 'notf', -1, mute);
}else{
templete = templete+parse([obj], 'notf', acct_id, 'notf', -1, mute);
}
2018-07-30 21:03:49 +10:00
}else{
2019-03-06 19:08:48 +11:00
if(misskey){
templete = templete+misskeyUserparse([obj], 'notf', acct_id, 'notf', -1, mute);
}else{
templete = templete+userparse([obj.account], 'notf', acct_id, 'notf', -1);
}
2018-07-30 21:03:49 +10:00
}
2019-03-06 19:08:48 +11:00
});
moreloading=false;
templete=templete+'<div class="hide notif-marker" data-maxid="'+max_id+'"></div>';
$("#timeline_" + tlid).append(templete);
$("#landing_" + tlid).hide();
jQuery("time.timeago").timeago();
2018-03-15 06:42:48 +11:00
}
2019-03-06 19:08:48 +11:00
$("#notf-box").addClass("fetched");
todc();
}
2018-03-15 06:42:48 +11:00
}
}
2019-03-06 19:08:48 +11:00
}
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");
2019-04-11 02:52:01 +10:00
if (multi) {
2018-07-28 07:25:12 +10:00
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)
});
}
2019-04-11 02:52:01 +10:00
}
allNotfRead()