2021-07-04 20:19:04 -07:00
|
|
|
import { getWithHeaders, paramsString, DEFAULT_TIMEOUT } from '../_utils/ajax.js'
|
|
|
|
import { auth, basename } from './utils.js'
|
2018-01-18 20:25:34 -08:00
|
|
|
|
2018-02-08 22:29:29 -08:00
|
|
|
function getTimelineUrlPath (timeline) {
|
2018-01-21 20:02:32 -08:00
|
|
|
switch (timeline) {
|
|
|
|
case 'local':
|
|
|
|
case 'federated':
|
2018-01-22 21:16:27 -08:00
|
|
|
return 'timelines/public'
|
2018-01-21 20:02:32 -08:00
|
|
|
case 'home':
|
2018-01-22 21:16:27 -08:00
|
|
|
return 'timelines/home'
|
2018-02-03 18:06:02 -08:00
|
|
|
case 'notifications':
|
2019-05-04 17:58:44 -07:00
|
|
|
case 'notifications/mentions':
|
2018-02-03 18:06:02 -08:00
|
|
|
return 'notifications'
|
2018-02-07 22:49:50 -08:00
|
|
|
case 'favorites':
|
|
|
|
return 'favourites'
|
2019-04-13 11:04:39 -07:00
|
|
|
case 'direct':
|
2019-04-02 12:05:27 +02:00
|
|
|
return 'conversations'
|
2020-07-25 20:17:08 +02:00
|
|
|
case 'bookmarks':
|
|
|
|
return 'bookmarks'
|
2018-01-22 21:16:27 -08:00
|
|
|
}
|
|
|
|
if (timeline.startsWith('tag/')) {
|
|
|
|
return 'timelines/tag'
|
|
|
|
} else if (timeline.startsWith('account/')) {
|
|
|
|
return 'accounts'
|
2018-02-08 09:15:25 -08:00
|
|
|
} else if (timeline.startsWith('list/')) {
|
|
|
|
return 'timelines/list'
|
2018-01-21 20:02:32 -08:00
|
|
|
}
|
2020-07-25 20:17:08 +02:00
|
|
|
throw new Error(`Invalid timeline type: ${timeline}`)
|
2018-01-21 20:02:32 -08:00
|
|
|
}
|
|
|
|
|
2019-04-13 11:04:39 -07:00
|
|
|
export async function getTimeline (instanceName, accessToken, timeline, maxId, since, limit) {
|
2019-08-03 13:49:37 -07:00
|
|
|
const timelineUrlName = getTimelineUrlPath(timeline)
|
2018-01-22 21:16:27 -08:00
|
|
|
let url = `${basename(instanceName)}/api/v1/${timelineUrlName}`
|
2018-01-18 20:25:34 -08:00
|
|
|
|
2018-01-21 20:02:32 -08:00
|
|
|
if (timeline.startsWith('tag/')) {
|
2019-03-30 21:48:49 -07:00
|
|
|
url += '/' + timeline.split('/')[1]
|
2018-01-22 21:16:27 -08:00
|
|
|
} else if (timeline.startsWith('account/')) {
|
2019-03-30 21:48:49 -07:00
|
|
|
url += '/' + timeline.split('/')[1] + '/statuses'
|
2018-02-08 09:15:25 -08:00
|
|
|
} else if (timeline.startsWith('list/')) {
|
2019-03-30 21:48:49 -07:00
|
|
|
url += '/' + timeline.split('/')[1]
|
2018-01-21 20:02:32 -08:00
|
|
|
}
|
|
|
|
|
2019-08-03 13:49:37 -07:00
|
|
|
const params = {}
|
2018-01-18 20:25:34 -08:00
|
|
|
if (since) {
|
2019-07-22 07:43:36 -07:00
|
|
|
params.since_id = since
|
2018-01-18 20:25:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (maxId) {
|
|
|
|
params.max_id = maxId
|
|
|
|
}
|
|
|
|
|
2018-08-29 22:49:14 -07:00
|
|
|
if (limit) {
|
|
|
|
params.limit = limit
|
|
|
|
}
|
|
|
|
|
2018-01-18 23:37:43 -08:00
|
|
|
if (timeline === 'local') {
|
|
|
|
params.local = true
|
|
|
|
}
|
|
|
|
|
2019-03-30 21:48:49 -07:00
|
|
|
if (timeline.startsWith('account/')) {
|
|
|
|
if (timeline.endsWith('media')) {
|
|
|
|
params.only_media = true
|
|
|
|
} else {
|
|
|
|
params.exclude_replies = !timeline.endsWith('/with_replies')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-04 17:58:44 -07:00
|
|
|
if (timeline === 'notifications/mentions') {
|
2022-12-11 13:09:12 -08:00
|
|
|
params.exclude_types = ['follow', 'favourite', 'reblog', 'poll', 'admin.sign_up', 'update', 'follow_request', 'admin.report']
|
2019-05-04 17:58:44 -07:00
|
|
|
}
|
|
|
|
|
2018-01-18 20:25:34 -08:00
|
|
|
url += '?' + paramsString(params)
|
|
|
|
|
2019-09-20 23:17:52 -07:00
|
|
|
console.log('fetching url', url)
|
2020-06-29 21:16:22 -07:00
|
|
|
let { json: items, headers } = await getWithHeaders(url, auth(accessToken), { timeout: DEFAULT_TIMEOUT })
|
2019-04-02 12:05:27 +02:00
|
|
|
|
2019-04-13 11:04:39 -07:00
|
|
|
if (timeline === 'direct') {
|
2021-12-27 20:57:16 -08:00
|
|
|
items = items.map(item => item.last_status).filter(Boolean) // ignore falsy last_status'es
|
2019-04-13 11:04:39 -07:00
|
|
|
}
|
2020-06-13 12:07:27 +02:00
|
|
|
return { items, headers }
|
2018-02-08 22:29:29 -08:00
|
|
|
}
|