thedesk/app/js/tl/notification.js

437 lines
18 KiB
JavaScript
Raw Normal View History

2018-01-28 23:22:43 +11:00
//通知
//取得+Streaming接続
2019-05-19 17:39:30 +10:00
function notf(acct_id, tlid, sys) {
2021-06-02 16:59:36 +10:00
if (sys == 'direct') {
notfColumn(acct_id, tlid, sys)
} else {
notfCommon(acct_id, tlid, sys)
}
2019-01-26 14:24:26 +11:00
}
2021-06-02 16:59:36 +10:00
2019-05-19 17:39:30 +10:00
function notfColumn(acct_id, tlid, sys) {
2021-06-02 16:59:36 +10:00
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)
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,
})
} else {
var misskey = false
if (localStorage.getItem('exclude-' + tlid)) {
var exc = localStorage.getItem('exclude-' + tlid)
} else {
var exc = ''
}
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-05-19 17:39:30 +10:00
2021-06-02 16:59:36 +10:00
httpreq.responseType = 'json'
httpreq.send(body)
2022-12-06 01:12:33 +11:00
httpreq.onreadystatechange = function () {
2021-06-02 16:59:36 +10:00
if (httpreq.readyState === 4) {
var json = httpreq.response
if (this.status !== 200) {
$('#landing_' + tlid).append(`<div>${this.status}</div><div>${escapeHTML(this.response)}`)
setLog(start, this.status, this.response)
}
var max_id = httpreq.getResponseHeader('link')
if (max_id) {
max_id = max_id.match(/[?&]{1}max_id=([0-9]+)/)[1]
}
if (json[0]) {
var templete = ''
var lastnotf = localStorage.getItem('lastnotf_' + acct_id)
localStorage.setItem('lastnotf_' + acct_id, json[0].id)
2022-12-06 01:12:33 +11:00
Object.keys(json).forEach(function (key) {
2021-06-02 16:59:36 +10:00
var obj = json[key]
if (lastnotf == obj.id && key > 0 && native == 'yes') {
var ct = key
if (key > 14) {
ct = '15+'
}
var os = localStorage.getItem('platform')
var options = {
body: ct + lang.lang_notf_new,
icon: localStorage.getItem('prof_' + acct_id),
}
var n = new Notification('TheDesk:' + domain, options)
}
var mute = getFilterTypeByAcct(acct_id, 'notif')
2022-12-06 01:12:33 +11:00
//Pleromaにはmoveというtypeがあるらしい。何が互換APIじゃ
if (obj.type !== 'follow' && obj.type !== 'move' && obj.type !== 'request' && obj.type !== 'admin.sign_up') {
2021-06-02 16:59:36 +10:00
if (misskey) {
templete = templete + misskeyParse([obj], 'notf', acct_id, tlid, -1, mute)
} else {
templete = templete + parse([obj], 'notf', acct_id, tlid, -1, mute)
}
} else if (obj.type == 'follow_request') {
templete = templete + userparse([obj.account], 'request', acct_id, tlid, -1)
} else {
if (misskey) {
templete = templete + misskeyUserparse([obj], 'notf', acct_id, tlid, -1, mute)
} else {
templete = templete + userparse([obj.account], obj.type, acct_id, tlid, -1)
}
}
})
templete = templete + '<div class="hide notif-marker" data-maxid="' + max_id + '"></div>'
$('#timeline_' + tlid).html(templete)
2022-12-06 01:12:33 +11:00
// $('#landing_' + tlid).hide()
2021-06-02 16:59:36 +10:00
jQuery('time.timeago').timeago()
}
$('#notf-box').addClass('fetched')
todc()
2022-12-06 01:12:33 +11:00
//Markers
2021-06-02 16:59:36 +10:00
var markers = localStorage.getItem('markers')
if (markers == 'yes') {
markers = true
} else {
markers = false
}
if (markers) {
getMarker(tlid, 'notf', acct_id)
}
}
}
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
}
2019-01-26 14:24:26 +11:00
}
2021-06-02 16:59:36 +10:00
function notfCommon(acct_id, tlid, sys, stream) {
2021-06-02 16:59:36 +10:00
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,
},
}
}
if (stream == 'only') {
notfWS(misskey, acct_id, tlid, domain, at)
return false
}
fetch(start, i)
2022-12-06 01:12:33 +11:00
.then(function (response) {
2021-06-02 16:59:36 +10:00
console.log('header to get param:' + response.headers.get('link'))
if (!response.ok) {
2022-12-06 01:12:33 +11:00
response.text().then(function (text) {
2021-06-02 16:59:36 +10:00
console.log('notf error', 'div[data-notf=' + acct_id + '] .landing')
$('div[data-notf=' + acct_id + '] .landing').append(`<div>${response.status}</div><div>${escapeHTML(text)}`)
setLog(response.url, response.status, text)
})
}
return response.json()
})
2022-12-06 01:12:33 +11:00
.catch(function (error) {
2021-06-02 16:59:36 +10:00
todo(error)
setLog(start, 'JSON', error)
console.error(error)
})
2022-12-06 01:12:33 +11:00
.then(function (json) {
2021-06-02 16:59:36 +10:00
if (json[0]) {
var templete = ''
var lastnotf = localStorage.getItem('lastnotf_' + acct_id)
localStorage.setItem('lastnotf_' + acct_id, json[0].id)
2022-12-06 01:12:33 +11:00
Object.keys(json).forEach(function (key) {
2021-06-02 16:59:36 +10:00
var obj = json[key]
if (lastnotf == obj.id && key > 0 && native == 'yes') {
var ct = key
if (key > 14) {
ct = '15+'
}
var os = localStorage.getItem('platform')
var options = {
body: ct + lang.lang_notf_new,
icon: localStorage.getItem('prof_' + acct_id),
}
var n = new Notification('TheDesk:' + domain, options)
}
var mute = getFilterTypeByAcct(acct_id, 'notif')
2022-12-06 01:12:33 +11:00
//Pleromaにはmoveというtypeがあるらしい。何が互換APIじゃ
if (obj.type !== 'follow' && obj.type !== 'move' && obj.type !== 'request' && obj.type !== 'admin.sign_up') {
2021-06-02 16:59:36 +10:00
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], obj.type, acct_id, 'notf', -1)
}
}
})
$('div[data-notf=' + acct_id + ']').html(templete)
2022-12-06 01:12:33 +11:00
// $('#landing_' + tlid).hide()
2021-06-02 16:59:36 +10:00
jQuery('time.timeago').timeago()
}
$('#notf-box').addClass('fetched')
todc()
if (stream != 'no') notfWS(misskey, acct_id, tlid, domain, at)
})
2019-05-11 02:31:15 +10:00
}
2021-06-02 16:59:36 +10:00
2019-05-19 17:39:30 +10:00
function notfWS(misskey, acct_id, tlid, domain, at) {
2021-06-02 16:59:36 +10:00
if (mastodonBaseWsStatus[domain] == 'available') return false
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
2018-01-28 23:22:43 +11:00
2021-06-02 16:59:36 +10:00
var wsid = websocketNotf.length
websocketNotf[acct_id] = new WebSocket(start)
2022-12-06 01:12:33 +11:00
websocketNotf[acct_id].onopen = function (mess) {
2021-06-02 16:59:36 +10:00
console.table({
acct_id: acct_id,
type: 'Connect Streaming API(Notf)',
domain: domain,
message: [mess],
})
$('i[data-notf=' + acct_id + ']').removeClass('red-text')
}
2022-12-06 01:12:33 +11:00
websocketNotf[acct_id].onmessage = function (mess) {
2021-06-02 16:59:36 +10:00
$('#landing_' + tlid).hide()
2022-12-06 01:12:33 +11:00
//console.log(["Receive Streaming API(Notf):" + acct_id + "(" + domain + ")", JSON.parse(JSON.parse(mess.data).payload)]);
2021-06-02 16:59:36 +10:00
var popup = localStorage.getItem('popup')
if (!popup) {
popup = 0
}
var obj = JSON.parse(JSON.parse(mess.data).payload)
var type = JSON.parse(mess.data).event
if (type == 'notification') {
var templete = ''
localStorage.setItem('lastnotf_' + acct_id, obj.id)
if (!$('#unread_' + tlid + ' .material-icons').hasClass('teal-text')) {
//markers show中はダメ
2022-12-06 01:12:33 +11:00
if (obj.type !== 'follow' && obj.type !== 'move' && obj.type !== 'request' && obj.type !== 'admin.sign_up') {
2021-06-02 16:59:36 +10:00
templete = parse([obj], 'notf', acct_id, 'notf', popup)
} else if (obj.type == 'follow_request') {
templete = userparse([obj.account], 'request', acct_id, 'notf', -1)
} else {
templete = userparse([obj], obj.type, acct_id, 'notf', popup)
}
if (!$('div[data-notfIndv=' + acct_id + '_' + obj.id + ']').length) {
$('div[data-notf=' + acct_id + ']').prepend(templete)
$('div[data-const=notf_' + acct_id + ']').prepend(templete)
}
jQuery('time.timeago').timeago()
}
} else if (type == 'delete') {
$('[toot-id=' + obj + ']').hide()
$('[toot-id=' + obj + ']').remove()
}
}
2022-12-06 01:12:33 +11:00
websocketNotf[acct_id].onerror = function (error) {
2021-06-02 16:59:36 +10:00
console.error('WebSocket Error ', error)
errorct++
console.log(errorct)
if (errorct < 3) {
notfWS(misskey, acct_id, tlid, domain, at)
}
}
2022-12-06 01:12:33 +11:00
websocketNotf[acct_id].onclose = function (error) {
2021-06-02 16:59:36 +10:00
console.error('WebSocket Close ', error)
errorct++
console.log(errorct)
if (errorct < 3) {
notfWS(misskey, acct_id, tlid, domain, at)
}
}
}
2019-05-08 01:48:59 +10:00
}
2018-03-15 06:42:48 +11:00
//一定のスクロールで発火
function notfmore(tlid) {
2021-06-02 16:59:36 +10:00
console.log({ status: 'kicked', status: moreloading })
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 + ' .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-05-19 17:39:30 +10:00
2021-06-02 16:59:36 +10:00
httpreq.responseType = 'json'
httpreq.send(body)
2022-12-06 01:12:33 +11:00
httpreq.onreadystatechange = function () {
2021-06-02 16:59:36 +10:00
if (httpreq.readyState === 4) {
var json = httpreq.response
if (this.status !== 200) {
setLog(start, this.status, this.response)
}
console.log(['More notifications on ' + tlid, json])
var max_id = httpreq.getResponseHeader('link').match(/[?&]{1}max_id=([0-9]+)/)[1]
if (json[0]) {
var templete = ''
var lastnotf = localStorage.getItem('lastnotf_' + acct_id)
localStorage.setItem('lastnotf_' + acct_id, json[0].id)
2022-12-06 01:12:33 +11:00
Object.keys(json).forEach(function (key) {
2021-06-02 16:59:36 +10:00
var obj = json[key]
var mute = getFilterTypeByAcct(acct_id, 'notif')
2022-12-06 01:12:33 +11:00
if (obj.type !== 'follow' && obj.type !== 'move' && obj.type !== 'request' && obj.type !== 'admin.sign_up') {
2021-06-02 16:59:36 +10:00
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], obj.type, acct_id, 'notf', -1)
}
}
})
moreloading = false
templete = templete + '<div class="hide notif-marker" data-maxid="' + max_id + '"></div>'
$('#timeline_' + tlid).append(templete)
2022-12-06 01:12:33 +11:00
// $('#landing_' + tlid).hide()
2021-06-02 16:59:36 +10:00
jQuery('time.timeago').timeago()
}
$('#notf-box').addClass('fetched')
todc()
}
}
}
2018-03-15 06:42:48 +11:00
}
2018-01-28 23:22:43 +11:00
//通知トグルボタン
function notfToggle(acct, tlid) {
2021-06-02 16:59:36 +10:00
if ($('#notf-box_' + tlid).hasClass('column-hide')) {
$('#notf-box_' + tlid).css('display', 'block')
$('#notf-box_' + tlid).animate({
height: '400px',
}, {
duration: 300,
2022-12-06 01:12:33 +11:00
complete: function () {
2021-06-02 16:59:36 +10:00
$('#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,
2022-12-06 01:12:33 +11:00
complete: function () {
2021-06-02 16:59:36 +10:00
$('#notf-box_' + tlid).addClass('column-hide')
$('#notf-box_' + tlid).css('display', 'none')
},
})
}
notfCanceler(acct)
2018-07-28 07:25:12 +10:00
}
2021-06-02 16:59:36 +10:00
2019-05-19 17:39:30 +10:00
function notfCanceler(acct) {
2021-06-02 16:59:36 +10:00
$('.notf-reply_' + acct).text(0)
localStorage.removeItem('notf-reply_' + acct)
$('.notf-reply_' + acct).addClass('hide')
$('.notf-fav_' + acct).text(0)
localStorage.removeItem('notf-fav_' + acct)
$('.notf-fav_' + acct).addClass('hide')
$('.notf-bt_' + acct).text(0)
localStorage.removeItem('notf-bt_' + acct)
$('.notf-bt_' + acct).addClass('hide')
$('.notf-follow_' + acct).text(0)
localStorage.removeItem('notf-follow_' + acct)
$('.notf-follow_' + acct).addClass('hide')
$('.notf-icon_' + acct).removeClass('red-text')
var id = $('#announce_' + acct + ' .announcement')
.first()
.attr('data-id')
$('.notf-announ_' + acct + '_ct').text('')
$(`.boxIn[data-acct=${acct}] .notice-box`).removeClass('has-notf')
if (id) {
localStorage.setItem('announ_' + acct, id)
}
2018-01-28 23:22:43 +11:00
}
2021-06-02 16:59:36 +10:00
2019-05-19 17:39:30 +10:00
function allNotfRead() {
2021-06-02 16:59:36 +10:00
var multi = localStorage.getItem('multi')
if (multi) {
var obj = JSON.parse(multi)
2022-12-06 01:12:33 +11:00
Object.keys(obj).forEach(function (key) {
2021-06-02 16:59:36 +10:00
notfCanceler(key)
})
}
2019-04-11 02:52:01 +10:00
}
2021-06-02 16:59:36 +10:00
allNotfRead()