Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F85803587
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
10 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/modules/api.js b/src/modules/api.js
index ee11853012..e3d4632571 100644
--- a/src/modules/api.js
+++ b/src/modules/api.js
@@ -1,70 +1,70 @@
-import TimelineFetcher from '../services/timeline_fetcher/timeline_fetcher_two.service.js'
+import TimelineFetcher from '../services/timeline_fetcher/timeline_fetcher.service.js'
import NotificationsFetcher from '../services/notifications_fetcher/notifications_fetcher.service.js'
import ApiUtils from '../services/new_api/utils.js'
import parseLinkHeader from 'parse-link-header'
const Api = {
state: {
fetchers: {}
},
mutations: {
setFetcher (state, { fetcher, timelineName }) {
state.fetchers[timelineName] = fetcher
}
},
actions: {
async startFetchingNotifications ({ state, rootState, commit, dispatch }) {
if (state.fetchers['user_notifications']) {
return
}
const fetcher = await NotificationsFetcher.create({ state: rootState, commit, dispatch })
commit('setFetcher', { fetcher, timelineName: 'user_notifications' })
},
async startFetchingTimeline ({ state, rootState, commit, dispatch }, options) {
if (state.fetchers[options.timelineName]) {
return
}
const fetcher = await TimelineFetcher.create({ state: rootState, commit, dispatch }, options)
commit('setFetcher', { fetcher, timelineName: options.timelineName })
},
// TODO: DRY up.
async fetchPrevForTimeline ({ commit, rootState, dispatch }, { timelineName }) {
const store = { commit, state: rootState, dispatch }
const data = await ApiUtils.request({
store,
fullUrl: store.state.statuses.timelines[timelineName].links.prev.url
})
const statuses = await data.json()
const links = parseLinkHeader(data.headers.get('link'))
store.dispatch('addNewStatusesToTimeline', { statuses, timelineName })
// We don't always get new link headers.
if (links && links.prev) {
store.commit('setPrevLinkForTimeline', { link: links.prev, timelineName })
}
},
async fetchNextForTimeline ({ commit, rootState, dispatch }, { timelineName }) {
const store = { commit, state: rootState, dispatch }
const data = await ApiUtils.request({
store,
fullUrl: store.state.statuses.timelines[timelineName].links.next.url
})
const statuses = await data.json()
const links = parseLinkHeader(data.headers.get('link'))
store.dispatch('addNewStatusesToTimeline', { statuses, timelineName })
store.dispatch('showOldStatusesForTimeline', { timelineName, statuses })
// We don't always get new link headers.
if (links && links.next) {
store.commit('setNextLinkForTimeline', { link: links.next, timelineName })
}
},
stopFetchingTimeline ({ state, commit }, { timelineName }) {
const fetcher = state.fetchers[timelineName]
clearInterval(fetcher)
commit('setFetcher', { fetcher: false, timelineName })
}
}
}
export default Api
diff --git a/src/services/timeline_fetcher/timeline_fetcher.service.js b/src/services/timeline_fetcher/timeline_fetcher.service.js
index 64f8f46848..4a664d1e19 100644
--- a/src/services/timeline_fetcher/timeline_fetcher.service.js
+++ b/src/services/timeline_fetcher/timeline_fetcher.service.js
@@ -1,58 +1,89 @@
-import { camelCase } from 'lodash'
+import Timelines from '../new_api/timelines.js'
+import Users from '../new_api/users.js'
-import apiService from '../api/api.service.js'
+const streamTimeline = ({ timelineName, store }) => {
+ const rootState = store.rootState || store.state
+ const timelines = {
+ publicAndExternal: 'public',
+ public: 'public:local',
+ home: 'user'
+ }
-const update = ({store, statuses, timeline, showImmediately, userId}) => {
- const ccTimeline = camelCase(timeline)
+ let url = `${rootState.instance.server}/api/v1/streaming?stream=${timelines[timelineName]}`.replace('http', 'ws')
+ let status = ''
- store.dispatch('setError', { value: false })
+ if (rootState.oauth.token) {
+ url = `${url}&access_token=${rootState.oauth.token}`
+ }
- store.dispatch('addNewStatuses', {
- timeline: ccTimeline,
- userId,
- statuses,
- showImmediately
- })
-}
+ let socket = null
-const fetchAndUpdate = ({store, credentials, timeline = 'friends', older = false, showImmediately = false, userId = false, tag = false, until}) => {
- const args = { timeline, credentials }
- const rootState = store.rootState || store.state
- const timelineData = rootState.statuses.timelines[camelCase(timeline)]
+ const stop = () => socket.close()
+ const connect = () => {
+ status = ''
+ socket = new window.WebSocket(url)
+ socket.addEventListener('message', (event) => {
+ if (event.data === '') { return }
- if (older) {
- args['until'] = until || timelineData.minVisibleId
- } else {
- args['since'] = timelineData.maxId
+ const data = JSON.parse(event.data)
+ console.log(data.event)
+ if (data.event === 'update') {
+ const status = JSON.parse(data.payload)
+ store.dispatch('addNewStatusesToTimeline', { statuses: [status], timelineName })
+ }
+ if (data.event === 'notification') {
+ const notification = JSON.parse(data.payload)
+ store.dispatch('addNewNotificationsTwo', { notifications: [notification] })
+ }
+ })
+ socket.onopen = () => {
+ status = 'connected'
+ }
+ socket.onclose = () => {
+ status = 'closed'
+ }
}
+ const getStatus = () => (status)
- args['userId'] = userId
- args['tag'] = tag
+ return {
+ connect,
+ stop,
+ getStatus
+ }
+}
- const numStatusesBeforeFetch = timelineData.statuses.length
+const TimelineFetcher = {
+ async create (store, { type, params = {}, timelineName }) {
+ // Initial fetch, directly
+ let tf
+ switch (type) {
+ case 'public':
+ tf = Timelines.public
+ break
+ case 'user':
+ tf = Users.statuses
+ break
+ case 'home':
+ tf = Timelines.home
+ break
+ }
+ const { statuses, links } = await tf({ store, params })
+ store.dispatch('addNewStatusesToTimeline', { statuses, timelineName, showImmediately: true })
+ store.commit('setPrevLinkForTimeline', { link: links.prev, timelineName })
+ store.commit('setNextLinkForTimeline', { link: links.next, timelineName })
- return apiService.fetchTimeline(args)
- .then((statuses) => {
- if (!older && statuses.length >= 20 && !timelineData.loading && numStatusesBeforeFetch > 0) {
- store.dispatch('queueFlush', { timeline: timeline, id: timelineData.maxId })
- }
- update({store, statuses, timeline, showImmediately, userId})
- return statuses
- }, () => store.dispatch('setError', { value: true }))
-}
+ // Streaming, reactivate later
+ const socket = streamTimeline({ store, timelineName })
+ console.log(socket)
+ socket.connect()
-const startFetching = ({timeline = 'friends', credentials, store, userId = false, tag = false}) => {
- const rootState = store.rootState || store.state
- const timelineData = rootState.statuses.timelines[camelCase(timeline)]
- const showImmediately = timelineData.visibleStatuses.length === 0
- timelineData.userId = userId
- fetchAndUpdate({timeline, credentials, store, showImmediately, userId, tag})
- const boundFetchAndUpdate = () => fetchAndUpdate({ timeline, credentials, store, userId, tag })
- return setInterval(boundFetchAndUpdate, 10000)
-}
-const timelineFetcher = {
- fetchAndUpdate,
- startFetching
+ // Next fetch uses the link headers
+ const fetcher = async () => {
+ store.dispatch('fetchPrevForTimeline', { timelineName })
+ }
+
+ return setInterval(fetcher, 10000)
+ }
}
-export default timelineFetcher
+export default TimelineFetcher
diff --git a/src/services/timeline_fetcher/timeline_fetcher_two.service.js b/src/services/timeline_fetcher/timeline_fetcher_two.service.js
deleted file mode 100644
index 4a664d1e19..0000000000
--- a/src/services/timeline_fetcher/timeline_fetcher_two.service.js
+++ /dev/null
@@ -1,89 +0,0 @@
-import Timelines from '../new_api/timelines.js'
-import Users from '../new_api/users.js'
-
-const streamTimeline = ({ timelineName, store }) => {
- const rootState = store.rootState || store.state
- const timelines = {
- publicAndExternal: 'public',
- public: 'public:local',
- home: 'user'
- }
-
- let url = `${rootState.instance.server}/api/v1/streaming?stream=${timelines[timelineName]}`.replace('http', 'ws')
- let status = ''
-
- if (rootState.oauth.token) {
- url = `${url}&access_token=${rootState.oauth.token}`
- }
-
- let socket = null
-
- const stop = () => socket.close()
- const connect = () => {
- status = ''
- socket = new window.WebSocket(url)
- socket.addEventListener('message', (event) => {
- if (event.data === '') { return }
-
- const data = JSON.parse(event.data)
- console.log(data.event)
- if (data.event === 'update') {
- const status = JSON.parse(data.payload)
- store.dispatch('addNewStatusesToTimeline', { statuses: [status], timelineName })
- }
- if (data.event === 'notification') {
- const notification = JSON.parse(data.payload)
- store.dispatch('addNewNotificationsTwo', { notifications: [notification] })
- }
- })
- socket.onopen = () => {
- status = 'connected'
- }
- socket.onclose = () => {
- status = 'closed'
- }
- }
- const getStatus = () => (status)
-
- return {
- connect,
- stop,
- getStatus
- }
-}
-
-const TimelineFetcher = {
- async create (store, { type, params = {}, timelineName }) {
- // Initial fetch, directly
- let tf
- switch (type) {
- case 'public':
- tf = Timelines.public
- break
- case 'user':
- tf = Users.statuses
- break
- case 'home':
- tf = Timelines.home
- break
- }
- const { statuses, links } = await tf({ store, params })
- store.dispatch('addNewStatusesToTimeline', { statuses, timelineName, showImmediately: true })
- store.commit('setPrevLinkForTimeline', { link: links.prev, timelineName })
- store.commit('setNextLinkForTimeline', { link: links.next, timelineName })
-
- // Streaming, reactivate later
- const socket = streamTimeline({ store, timelineName })
- console.log(socket)
- socket.connect()
-
- // Next fetch uses the link headers
- const fetcher = async () => {
- store.dispatch('fetchPrevForTimeline', { timelineName })
- }
-
- return setInterval(fetcher, 10000)
- }
-}
-
-export default TimelineFetcher
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Fri, Oct 9, 9:22 AM (1 d, 18 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1784628
Default Alt Text
(10 KB)
Attached To
Mode
rPUFE pleroma-fe-upstream
Attached
Detach File
Event Timeline
Log In to Comment