slight refactor of timeline fetching (#485)
This commit is contained in:
parent
8d2e0636d6
commit
1423a6b14b
|
@ -2,7 +2,7 @@ import { store } from '../_store/store'
|
||||||
import { getTimeline } from '../_api/timelines'
|
import { getTimeline } from '../_api/timelines'
|
||||||
import { toast } from '../_utils/toast'
|
import { toast } from '../_utils/toast'
|
||||||
import { mark, stop } from '../_utils/marks'
|
import { mark, stop } from '../_utils/marks'
|
||||||
import { mergeArrays } from '../_utils/arrays'
|
import { concat, mergeArrays } from '../_utils/arrays'
|
||||||
import { byItemIds } from '../_utils/sorting'
|
import { byItemIds } from '../_utils/sorting'
|
||||||
import isEqual from 'lodash-es/isEqual'
|
import isEqual from 'lodash-es/isEqual'
|
||||||
import {
|
import {
|
||||||
|
@ -11,9 +11,22 @@ import {
|
||||||
import {
|
import {
|
||||||
getTimeline as getTimelineFromDatabase
|
getTimeline as getTimelineFromDatabase
|
||||||
} from '../_database/timelines/pagination'
|
} from '../_database/timelines/pagination'
|
||||||
|
import { getStatus, getStatusContext } from '../_api/statuses'
|
||||||
|
|
||||||
const FETCH_LIMIT = 20
|
const FETCH_LIMIT = 20
|
||||||
|
|
||||||
|
async function fetchTimelineItemsFromNetwork (instanceName, accessToken, timelineName, lastTimelineItemId) {
|
||||||
|
if (timelineName.startsWith('status/')) { // special case - this is a list of descendents and ancestors
|
||||||
|
let statusId = timelineName.split('/').slice(-1)[0]
|
||||||
|
let statusRequest = getStatus(instanceName, accessToken, statusId)
|
||||||
|
let contextRequest = getStatusContext(instanceName, accessToken, statusId)
|
||||||
|
let [ status, context ] = await Promise.all([statusRequest, contextRequest])
|
||||||
|
return concat(context.ancestors, status, context.descendants)
|
||||||
|
} else { // normal timeline
|
||||||
|
return getTimeline(instanceName, accessToken, timelineName, lastTimelineItemId, FETCH_LIMIT)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function fetchTimelineItems (instanceName, accessToken, timelineName, lastTimelineItemId, online) {
|
async function fetchTimelineItems (instanceName, accessToken, timelineName, lastTimelineItemId, online) {
|
||||||
mark('fetchTimelineItems')
|
mark('fetchTimelineItems')
|
||||||
let items
|
let items
|
||||||
|
@ -23,7 +36,7 @@ async function fetchTimelineItems (instanceName, accessToken, timelineName, last
|
||||||
stale = true
|
stale = true
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
items = await getTimeline(instanceName, accessToken, timelineName, lastTimelineItemId, FETCH_LIMIT)
|
items = await fetchTimelineItemsFromNetwork(instanceName, accessToken, timelineName, lastTimelineItemId)
|
||||||
/* no await */ insertTimelineItemsInDatabase(instanceName, timelineName, items)
|
/* no await */ insertTimelineItemsInDatabase(instanceName, timelineName, items)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e)
|
console.error(e)
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { auth, basename } from './utils'
|
import { auth, basename } from './utils'
|
||||||
import { post, WRITE_TIMEOUT } from '../_utils/ajax'
|
import { DEFAULT_TIMEOUT, get, post, WRITE_TIMEOUT } from '../_utils/ajax'
|
||||||
|
|
||||||
export async function postStatus (instanceName, accessToken, text, inReplyToId, mediaIds,
|
export async function postStatus (instanceName, accessToken, text, inReplyToId, mediaIds,
|
||||||
sensitive, spoilerText, visibility) {
|
sensitive, spoilerText, visibility) {
|
||||||
|
@ -23,3 +23,13 @@ export async function postStatus (instanceName, accessToken, text, inReplyToId,
|
||||||
|
|
||||||
return post(url, body, auth(accessToken), {timeout: WRITE_TIMEOUT})
|
return post(url, body, auth(accessToken), {timeout: WRITE_TIMEOUT})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getStatusContext (instanceName, accessToken, statusId) {
|
||||||
|
let url = `${basename(instanceName)}/api/v1/statuses/${statusId}/context`
|
||||||
|
return get(url, auth(accessToken), {timeout: DEFAULT_TIMEOUT})
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getStatus (instanceName, accessToken, statusId) {
|
||||||
|
let url = `${basename(instanceName)}/api/v1/statuses/${statusId}`
|
||||||
|
return get(url, auth(accessToken), {timeout: DEFAULT_TIMEOUT})
|
||||||
|
}
|
||||||
|
|
|
@ -15,8 +15,6 @@ function getTimelineUrlPath (timeline) {
|
||||||
}
|
}
|
||||||
if (timeline.startsWith('tag/')) {
|
if (timeline.startsWith('tag/')) {
|
||||||
return 'timelines/tag'
|
return 'timelines/tag'
|
||||||
} else if (timeline.startsWith('status/')) {
|
|
||||||
return 'statuses'
|
|
||||||
} else if (timeline.startsWith('account/')) {
|
} else if (timeline.startsWith('account/')) {
|
||||||
return 'accounts'
|
return 'accounts'
|
||||||
} else if (timeline.startsWith('list/')) {
|
} else if (timeline.startsWith('list/')) {
|
||||||
|
@ -30,8 +28,6 @@ export function getTimeline (instanceName, accessToken, timeline, maxId, since)
|
||||||
|
|
||||||
if (timeline.startsWith('tag/')) {
|
if (timeline.startsWith('tag/')) {
|
||||||
url += '/' + timeline.split('/').slice(-1)[0]
|
url += '/' + timeline.split('/').slice(-1)[0]
|
||||||
} else if (timeline.startsWith('status/')) {
|
|
||||||
url += '/' + timeline.split('/').slice(-1)[0] + '/context'
|
|
||||||
} else if (timeline.startsWith('account/')) {
|
} else if (timeline.startsWith('account/')) {
|
||||||
url += '/' + timeline.split('/').slice(-1)[0] + '/statuses'
|
url += '/' + timeline.split('/').slice(-1)[0] + '/statuses'
|
||||||
} else if (timeline.startsWith('list/')) {
|
} else if (timeline.startsWith('list/')) {
|
||||||
|
@ -53,16 +49,5 @@ export function getTimeline (instanceName, accessToken, timeline, maxId, since)
|
||||||
|
|
||||||
url += '?' + paramsString(params)
|
url += '?' + paramsString(params)
|
||||||
|
|
||||||
if (timeline.startsWith('status/')) {
|
|
||||||
// special case - this is a list of descendents and ancestors
|
|
||||||
let statusUrl = `${basename(instanceName)}/api/v1/statuses/${timeline.split('/').slice(-1)[0]}`
|
|
||||||
return Promise.all([
|
|
||||||
get(url, auth(accessToken), {timeout: DEFAULT_TIMEOUT}),
|
|
||||||
get(statusUrl, auth(accessToken), {timeout: DEFAULT_TIMEOUT})
|
|
||||||
]).then(res => {
|
|
||||||
return [].concat(res[0].ancestors).concat([res[1]]).concat(res[0].descendants)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return get(url, auth(accessToken), {timeout: DEFAULT_TIMEOUT})
|
return get(url, auth(accessToken), {timeout: DEFAULT_TIMEOUT})
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,3 +31,16 @@ export function mergeArrays (leftArray, rightArray) {
|
||||||
}
|
}
|
||||||
return merged
|
return merged
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function concat () {
|
||||||
|
let res = []
|
||||||
|
for (let i = 0, len = arguments.length; i < len; i++) {
|
||||||
|
let arg = arguments[i]
|
||||||
|
if (Array.isArray(arg)) {
|
||||||
|
res = res.concat(arguments[i])
|
||||||
|
} else {
|
||||||
|
res.push(arguments[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue