thedesk/app/js/tl/mix.js

217 lines
7.1 KiB
JavaScript
Raw Normal View History

2018-01-28 23:22:43 +11:00
//Integrated TL
2019-11-09 04:05:15 +11:00
async function mixtl(acct_id, tlid, type, delc, voice) {
2019-11-09 00:52:54 +11:00
localStorage.setItem('now', type)
todo('Integrated TL Loading...(Local)')
2019-11-09 04:05:15 +11:00
const domain = localStorage.getItem('domain_' + acct_id)
let startLocal = 'https://' + domain + '/api/v1/timelines/public?local=true'
let local = await getTL(startLocal, acct_id)
let startHome = 'https://' + domain + '/api/v1/timelines/home'
let home = await getTL(startHome, acct_id)
let concated = _.concat(local, home)
let uniqued = _.uniqBy(concated, 'id')
let sorted = _.orderBy(uniqued, ['id'], ['desc'])
let integrated = _.slice(sorted, 0, 19)
$('#landing_' + tlid).hide()
let mute = getFilterTypeByAcct(acct_id, 'mix')
let templete = parse(integrated, type, acct_id, tlid, '', mute, type)
localStorage.setItem('lastobj_' + tlid, integrated[0].id)
$('#timeline_' + tlid).html(templete)
additional(acct_id, tlid)
jQuery('time.timeago').timeago()
todc()
2020-09-07 20:06:57 +10:00
if(mastodonBaseWsStatus[domain] == 'cannotuse') {
2020-09-03 12:21:20 +10:00
mixre(acct_id, tlid, 'mix', mute, voice, '')
2020-09-07 20:06:57 +10:00
} else if (mastodonBaseWsStatus[domain] == 'undetected' || mastodonBaseWsStatus[domain] == 'connecting') {
2020-09-03 12:21:20 +10:00
const mbws = setInterval(function () {
2020-09-07 20:06:57 +10:00
if(mastodonBaseWsStatus[domain] == 'cannotuse') {
2020-09-03 12:21:20 +10:00
mixre(acct_id, tlid, 'mix', mute, voice, '')
clearInterval(mbws)
} else if(mastodonBaseWsStatus[domain] == 'available') {
mastodonBaseWs[domain].send(`{"type":"subscribe","stream":"public:local"}`)
clearInterval(mbws)
}
}, 1000)
} else if(mastodonBaseWsStatus[domain] == 'available') {
mastodonBaseWs[domain].send(`{"type":"subscribe","stream":"public:local"}`)
}
2019-11-09 04:05:15 +11:00
$(window).scrollTop(0)
lastId = integrated[0].id
beforeLastId = integrated[1].id
}
async function getTL(start, acct_id) {
const at = localStorage.getItem('acct_' + acct_id + '_at')
let promise = await fetch(start, {
2019-11-09 00:52:54 +11:00
method: 'GET',
2018-01-28 23:22:43 +11:00
headers: {
2019-11-09 00:52:54 +11:00
'content-type': 'application/json',
Authorization: 'Bearer ' + at
2019-10-31 02:30:26 +11:00
}
2019-11-04 03:10:06 +11:00
})
2019-11-09 04:05:15 +11:00
if (!promise.ok) {
promise.text().then(function(text) {
setLog(promise.url, promise.status, text)
2019-11-09 00:52:54 +11:00
})
2019-11-09 04:05:15 +11:00
}
return await promise.json()
2018-01-28 23:22:43 +11:00
}
//Streamingに接続
2019-11-09 04:05:15 +11:00
function mixre(acct_id, tlid, TLtype, mute, voice, mode) {
2019-11-09 00:52:54 +11:00
var domain = localStorage.getItem('domain_' + acct_id)
var at = localStorage.getItem('acct_' + acct_id + '_at')
if (localStorage.getItem('streaming_' + acct_id)) {
var wss = localStorage.getItem('streaming_' + acct_id)
2019-05-19 17:39:30 +10:00
} else {
2019-11-09 00:52:54 +11:00
var wss = 'wss://' + domain
2018-09-17 21:55:00 +10:00
}
2019-11-09 00:52:54 +11:00
var startHome = wss + '/api/v1/streaming/?stream=user&access_token=' + at
var startLocal = wss + '/api/v1/streaming/?stream=public:local&access_token=' + at
2019-11-09 04:05:15 +11:00
var wshid = wsHome.length
var wslid = wsLocal.length
wsHome[wshid] = new WebSocket(startHome)
wsLocal[wslid] = new WebSocket(startLocal)
wsHome[wshid].onopen = function(mess) {
2019-11-09 00:52:54 +11:00
localStorage.setItem('wssH_' + tlid, wshid)
console.table({
tlid: tlid,
type: 'Connect Streaming API(Integrated:Home)',
domain: domain,
message: mess
})
$('#notice_icon_' + tlid).removeClass('red-text')
}
2019-11-09 04:05:15 +11:00
wsLocal[wslid].onopen = function(mess) {
2019-11-09 00:52:54 +11:00
localStorage.setItem('wssL_' + tlid, wslid)
console.table({
tlid: tlid,
type: 'Connect Streaming API(Integrated:Local)',
domain: domain,
message: mess
})
$('#notice_icon_' + tlid).removeClass('red-text')
}
2019-11-09 04:05:15 +11:00
wsLocal[wslid].onmessage = function(mess) {
console.log('Receive Streaming API:(Integrated:Local)', mess)
integratedMessage(mess, acct_id, tlid, mute, voice)
2019-11-09 00:52:54 +11:00
}
2019-11-09 04:05:15 +11:00
wsHome[wshid].onmessage = function(mess) {
console.log(['Receive Streaming API:(Integrated:Home)', mess])
integratedMessage(mess, acct_id, tlid, mute, voice)
2019-11-09 00:52:54 +11:00
}
2019-11-09 04:05:15 +11:00
wsLocal[wslid].onerror = function(error) {
2019-11-09 00:52:54 +11:00
console.error('WebSocketLocal Error')
console.error(error)
$('#notice_icon_' + tlid).addClass('red-text')
2019-11-09 00:52:54 +11:00
if (mode == 'error') {
todo('WebSocket Error ' + error)
2019-05-19 17:39:30 +10:00
} else {
2019-11-09 00:52:54 +11:00
var errorct = localStorage.getItem('wserror_' + tlid) * 1 + 1
localStorage.setItem('wserror_' + tlid, errorct)
2019-05-19 17:39:30 +10:00
if (errorct < 3) {
2019-11-09 00:52:54 +11:00
reconnector(tlid, TLtype, acct_id, '', 'error')
2019-04-22 04:30:25 +10:00
}
2018-08-21 04:26:14 +10:00
}
2019-11-09 00:52:54 +11:00
}
2019-11-09 04:05:15 +11:00
wsLocal[wslid].onclose = function() {
2019-11-09 00:52:54 +11:00
console.warn('WebSocketLocal Closing:' + tlid)
$('#notice_icon_' + tlid).addClass('red-text')
2019-11-09 00:52:54 +11:00
if (mode == 'error') {
todo('WebSocket Closed')
2019-05-19 17:39:30 +10:00
} else {
2019-11-09 00:52:54 +11:00
var errorct = localStorage.getItem('wserror_' + tlid) * 1 + 1
localStorage.setItem('wserror_' + tlid, errorct)
2019-05-19 17:39:30 +10:00
if (errorct < 3) {
2019-11-09 00:52:54 +11:00
reconnector(tlid, TLtype, acct_id, '', 'error')
2019-04-22 04:30:25 +10:00
}
2018-08-21 04:26:14 +10:00
}
2019-11-09 00:52:54 +11:00
}
2019-11-09 04:05:15 +11:00
wsHome[wshid].onerror = function(error) {
2019-11-09 00:52:54 +11:00
console.error(['WebSocketHome Error', error])
$('#notice_icon_' + tlid).addClass('red-text')
2019-11-09 00:52:54 +11:00
if (mode == 'error') {
todo('WebSocket Error ' + error)
2019-05-19 17:39:30 +10:00
} else {
2019-11-09 00:52:54 +11:00
var errorct = localStorage.getItem('wserror_' + tlid) * 1 + 1
localStorage.setItem('wserror_' + tlid, errorct)
2019-05-19 17:39:30 +10:00
if (errorct < 3) {
2019-11-09 00:52:54 +11:00
reconnector(tlid, TLtype, acct_id, '', 'error')
2019-04-22 04:30:25 +10:00
}
2018-08-21 04:26:14 +10:00
}
2019-11-09 00:52:54 +11:00
}
2019-11-09 04:05:15 +11:00
wsHome[wshid].onclose = function() {
2019-11-09 00:52:54 +11:00
console.warn('WebSocketHome Closing:' + tlid)
$('#notice_icon_' + tlid).addClass('red-text')
2019-11-09 00:52:54 +11:00
if (mode == 'error') {
todo('WebSocket Closed')
2019-05-19 17:39:30 +10:00
} else {
2019-11-09 00:52:54 +11:00
var errorct = localStorage.getItem('wserror_' + tlid) * 1 + 1
localStorage.setItem('wserror_' + tlid, errorct)
2019-05-19 17:39:30 +10:00
if (errorct < 3) {
2019-11-09 00:52:54 +11:00
reconnector(tlid, TLtype, acct_id, '', 'error')
2019-04-22 04:30:25 +10:00
}
2018-08-21 04:26:14 +10:00
}
2019-11-09 00:52:54 +11:00
}
2018-01-28 23:22:43 +11:00
}
2019-11-09 04:05:15 +11:00
function integratedMessage(mess, acct_id, tlid, mute, voice) {
let data = JSON.parse(mess.data)
let type = data.event
let payload = data.payload
if (type == 'delete') {
$('[unique-id=' + payload + ']').hide()
$('[unique-id=' + payload + ']').remove()
} else if (type == 'update') {
let obj = JSON.parse(payload)
if (obj.id != lastId && obj.id != beforeLastId) {
lastId = obj.id
beforeLastId = obj.id
let dom = parse([obj], '', acct_id, tlid, '', mute)
if (voice) say(obj.content)
if ($('timeline_box_' + tlid + '_box .tl-box').scrollTop() === 0) {
$('#timeline_' + tlid).prepend(dom)
} else {
let pool = localStorage.getItem('pool_' + tlid)
if (pool) {
pool = dom + pool
} else {
pool = dom
}
localStorage.setItem('pool_' + tlid, pool)
}
scrollck()
additional(acct_id, tlid)
jQuery('time.timeago').timeago()
}
}
}
2018-01-28 23:22:43 +11:00
//ある程度のスクロールで発火
2019-11-09 04:05:15 +11:00
async function mixmore(tlid, type) {
2019-11-09 00:52:54 +11:00
var multi = localStorage.getItem('column')
var obj = JSON.parse(multi)
var acct_id = obj[tlid].domain
moreloading = true
todo('Integrated TL MoreLoading...(Local)')
2019-11-09 04:05:15 +11:00
const domain = localStorage.getItem('domain_' + acct_id)
const sid = $('#timeline_' + tlid + ' .cvo')
.last()
.attr('unique-id')
let startLocal = 'https://' + domain + '/api/v1/timelines/public?local=true&max_id=' + sid
let local = await getTL(startLocal, acct_id)
2019-11-16 23:29:11 +11:00
let startHome = 'https://' + domain + '/api/v1/timelines/home?max_id=' + sid
2019-11-09 04:05:15 +11:00
let home = await getTL(startHome, acct_id)
let concated = _.concat(local, home)
let uniqued = _.uniqBy(concated, 'id')
let sorted = _.orderBy(uniqued, ['id'], ['desc'])
let integrated = _.slice(sorted, 0, 19)
$('#landing_' + tlid).hide()
let mute = getFilterTypeByAcct(acct_id, 'mix')
let templete = parse(integrated, type, acct_id, tlid, '', mute, type)
localStorage.setItem('lastobj_' + tlid, integrated[0].id)
$('#timeline_' + tlid).append(templete)
additional(acct_id, tlid)
jQuery('time.timeago').timeago()
moreloading = false
todc()
2018-03-15 06:42:48 +11:00
}