2018-03-03 23:15:50 +01:00
|
|
|
|
import { updateInstanceInfo, updateVerifyCredentialsForInstance } from '../../_actions/instances'
|
|
|
|
|
import { updateLists } from '../../_actions/lists'
|
|
|
|
|
import { createStream } from '../../_actions/streaming'
|
2018-03-25 21:24:38 +02:00
|
|
|
|
import { updateCustomEmojiForInstance } from '../../_actions/emoji'
|
2018-04-03 06:14:12 +02:00
|
|
|
|
import { addStatusesOrNotifications } from '../../_actions/addStatusOrNotification'
|
|
|
|
|
import { getTimeline } from '../../_api/timelines'
|
2018-08-30 07:49:14 +02:00
|
|
|
|
import { TIMELINE_BATCH_SIZE } from '../../_static/timelines'
|
2018-02-11 22:46:57 +01:00
|
|
|
|
|
|
|
|
|
export function instanceObservers (store) {
|
2018-02-15 18:02:46 +01:00
|
|
|
|
// stream to watch for home timeline updates and notifications
|
|
|
|
|
let currentInstanceStream
|
|
|
|
|
|
|
|
|
|
store.observe('currentInstance', async (currentInstance) => {
|
|
|
|
|
if (!process.browser) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (currentInstanceStream) {
|
|
|
|
|
currentInstanceStream.close()
|
|
|
|
|
currentInstanceStream = null
|
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
|
window.currentInstanceStream = null
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-11 22:46:57 +01:00
|
|
|
|
if (!currentInstance) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
updateVerifyCredentialsForInstance(currentInstance)
|
|
|
|
|
updateInstanceInfo(currentInstance)
|
2018-03-25 21:24:38 +02:00
|
|
|
|
updateCustomEmojiForInstance(currentInstance)
|
2018-02-11 22:46:57 +01:00
|
|
|
|
updateLists()
|
2018-02-15 18:02:46 +01:00
|
|
|
|
|
|
|
|
|
await updateInstanceInfo(currentInstance)
|
2018-04-03 06:14:12 +02:00
|
|
|
|
|
|
|
|
|
let currentInstanceIsUnchanged = () => {
|
2018-04-19 18:37:05 +02:00
|
|
|
|
let { currentInstance: newCurrentInstance } = store.get()
|
|
|
|
|
return newCurrentInstance === currentInstance
|
2018-04-03 06:14:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!currentInstanceIsUnchanged()) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-19 18:37:05 +02:00
|
|
|
|
let { currentInstanceInfo } = store.get()
|
|
|
|
|
if (!currentInstanceInfo) {
|
2018-02-15 18:02:46 +01:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-03 06:14:12 +02:00
|
|
|
|
let homeTimelineItemIds = store.getForTimeline(currentInstance,
|
|
|
|
|
'home', 'timelineItemIds')
|
|
|
|
|
let firstHomeTimelineItemId = homeTimelineItemIds && homeTimelineItemIds[0]
|
|
|
|
|
let notificationItemIds = store.getForTimeline(currentInstance,
|
|
|
|
|
'notifications', 'timelineItemIds')
|
|
|
|
|
let firstNotificationTimelineItemId = notificationItemIds && notificationItemIds[0]
|
|
|
|
|
|
|
|
|
|
let onOpenStream = async () => {
|
|
|
|
|
if (!currentInstanceIsUnchanged()) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// fill in the "streaming gap" – i.e. fetch the most recent 20 items so that there isn't
|
|
|
|
|
// a big gap in the timeline if you haven't looked at it in awhile
|
|
|
|
|
async function fillGap (timelineName, firstTimelineItemId) {
|
|
|
|
|
if (!firstTimelineItemId) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
let newTimelineItems = await getTimeline(currentInstance, accessToken,
|
2018-08-30 07:49:14 +02:00
|
|
|
|
timelineName, null, firstTimelineItemId, TIMELINE_BATCH_SIZE)
|
2018-04-03 06:14:12 +02:00
|
|
|
|
if (newTimelineItems.length) {
|
|
|
|
|
addStatusesOrNotifications(currentInstance, timelineName, newTimelineItems)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
|
fillGap('home', firstHomeTimelineItemId),
|
|
|
|
|
fillGap('notifications', firstNotificationTimelineItemId)
|
|
|
|
|
])
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-19 18:37:05 +02:00
|
|
|
|
let { accessToken } = store.get()
|
|
|
|
|
let streamingApi = currentInstanceInfo.urls.streaming_api
|
2018-04-03 06:14:12 +02:00
|
|
|
|
currentInstanceStream = createStream(streamingApi,
|
|
|
|
|
currentInstance, accessToken, 'home', onOpenStream)
|
2018-02-15 18:02:46 +01:00
|
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
|
window.currentInstanceStream = currentInstanceStream
|
|
|
|
|
}
|
2018-02-11 22:46:57 +01:00
|
|
|
|
})
|
2018-02-11 23:11:03 +01:00
|
|
|
|
}
|