Pinafore-Web-Client-Frontend/routes/_api/timelines.js

69 lines
1.9 KiB
JavaScript
Raw Permalink Normal View History

2018-02-24 03:23:36 +01:00
import { getWithTimeout, paramsString } from '../_utils/ajax'
2018-02-24 23:49:28 +01:00
import { auth, basename } from './utils'
2018-01-19 05:25:34 +01:00
2018-02-09 07:29:29 +01:00
function getTimelineUrlPath (timeline) {
2018-01-22 05:02:32 +01:00
switch (timeline) {
case 'local':
case 'federated':
2018-01-23 06:16:27 +01:00
return 'timelines/public'
2018-01-22 05:02:32 +01:00
case 'home':
2018-01-23 06:16:27 +01:00
return 'timelines/home'
case 'notifications':
return 'notifications'
case 'favorites':
return 'favourites'
2018-01-23 06:16:27 +01:00
}
if (timeline.startsWith('tag/')) {
return 'timelines/tag'
2018-01-29 00:44:33 +01:00
} else if (timeline.startsWith('status/')) {
return 'statuses'
2018-01-23 06:16:27 +01:00
} else if (timeline.startsWith('account/')) {
return 'accounts'
2018-02-08 18:15:25 +01:00
} else if (timeline.startsWith('list/')) {
return 'timelines/list'
2018-01-22 05:02:32 +01:00
}
}
2018-02-09 07:29:29 +01:00
export function getTimeline (instanceName, accessToken, timeline, maxId, since) {
2018-01-23 06:16:27 +01:00
let timelineUrlName = getTimelineUrlPath(timeline)
let url = `${basename(instanceName)}/api/v1/${timelineUrlName}`
2018-01-19 05:25:34 +01:00
2018-01-22 05:02:32 +01:00
if (timeline.startsWith('tag/')) {
url += '/' + timeline.split('/').slice(-1)[0]
2018-01-29 00:44:33 +01:00
} else if (timeline.startsWith('status/')) {
url += '/' + timeline.split('/').slice(-1)[0] + '/context'
2018-01-23 06:16:27 +01:00
} else if (timeline.startsWith('account/')) {
2018-02-08 18:15:25 +01:00
url += '/' + timeline.split('/').slice(-1)[0] + '/statuses'
} else if (timeline.startsWith('list/')) {
url += '/' + timeline.split('/').slice(-1)[0]
2018-01-22 05:02:32 +01:00
}
2018-01-19 05:25:34 +01:00
let params = {}
if (since) {
params.since = since
}
if (maxId) {
params.max_id = maxId
}
2018-01-19 08:37:43 +01:00
if (timeline === 'local') {
params.local = true
}
2018-01-19 05:25:34 +01:00
url += '?' + paramsString(params)
2018-01-29 00:44:33 +01:00
if (timeline.startsWith('status/')) {
// special case - this is a list of descendents and ancestors
2018-04-09 18:29:15 +02:00
let statusUrl = `${basename(instanceName)}/api/v1/statuses/${timeline.split('/').slice(-1)[0]}`
2018-01-29 00:44:33 +01:00
return Promise.all([
2018-02-24 23:49:28 +01:00
getWithTimeout(url, auth(accessToken)),
getWithTimeout(statusUrl, auth(accessToken))
2018-01-29 00:44:33 +01:00
]).then(res => {
return [].concat(res[0].ancestors).concat([res[1]]).concat(res[0].descendants)
})
}
2018-02-24 23:49:28 +01:00
return getWithTimeout(url, auth(accessToken))
2018-02-09 07:29:29 +01:00
}