akkoma-fe/src/services/backend_interactor_service/backend_interactor_service.js

49 lines
1.9 KiB
JavaScript
Raw Normal View History

2019-11-24 16:50:28 +00:00
import apiService, { getMastodonSocketURI, handleMastoWS } from '../api/api.service.js'
import timelineFetcherService from '../timeline_fetcher/timeline_fetcher.service.js'
2019-04-03 16:04:46 +00:00
import notificationsFetcher from '../notifications_fetcher/notifications_fetcher.service.js'
import followRequestFetcher from '../../services/follow_request_fetcher/follow_request_fetcher.service'
const backendInteractorService = credentials => ({
startFetchingTimeline ({ timeline, store, userId = false, tag }) {
2019-04-04 16:03:56 +00:00
return timelineFetcherService.startFetching({ timeline, store, credentials, userId, tag })
},
2019-04-04 16:03:56 +00:00
startFetchingNotifications ({ store }) {
2019-04-04 16:03:56 +00:00
return notificationsFetcher.startFetching({ store, credentials })
},
fetchAndUpdateNotifications ({ store }) {
return notificationsFetcher.fetchAndUpdate({ store, credentials })
},
startFetchingFollowRequest ({ store }) {
return followRequestFetcher.startFetching({ store, credentials })
},
2019-05-08 03:36:35 +00:00
2019-11-24 16:50:28 +00:00
startUserSocket ({ store, onMessage }) {
const serv = store.rootState.instance.server.replace('http', 'ws')
2019-11-24 16:50:28 +00:00
const url = serv + getMastodonSocketURI({ credentials, stream: 'user' })
const socket = new WebSocket(url)
console.debug('Socket created:', socket)
2019-11-24 16:50:28 +00:00
if (socket) {
socket.addEventListener('open', (wsEvent) => console.debug('MastoAPI User WebSocket connection established'))
2019-11-24 16:50:28 +00:00
socket.addEventListener('message', (wsEvent) => onMessage(handleMastoWS(wsEvent)))
socket.addEventListener('error', (error) => console.error('MastoApi User WebSocket Error:', error))
return socket
2019-11-24 16:50:28 +00:00
} else {
throw new Error('failed to connect to socket')
}
},
...Object.entries(apiService).reduce((acc, [key, func]) => {
return {
...acc,
[key]: (args) => func({ credentials, ...args })
}
}, {}),
verifyCredentials: apiService.verifyCredentials
})
export default backendInteractorService