diff options
Diffstat (limited to 'src/services')
| -rw-r--r-- | src/services/api/api.service.js | 32 | ||||
| -rw-r--r-- | src/services/backend_interactor_service/backend_interactor_service.js | 8 | ||||
| -rw-r--r-- | src/services/completion/completion.js | 70 | ||||
| -rw-r--r-- | src/services/timeline_fetcher/timeline_fetcher.service.js | 10 |
4 files changed, 107 insertions, 13 deletions
diff --git a/src/services/api/api.service.js b/src/services/api/api.service.js index b0e8dd87..e848d076 100644 --- a/src/services/api/api.service.js +++ b/src/services/api/api.service.js @@ -19,9 +19,11 @@ const UNFOLLOWING_URL = '/api/friendships/destroy.json' const QVITTER_USER_PREF_URL = '/api/qvitter/set_profile_pref.json' const REGISTRATION_URL = '/api/account/register.json' const AVATAR_UPDATE_URL = '/api/qvitter/update_avatar.json' +const EXTERNAL_PROFILE_URL = '/api/externalprofile/show.json' +const QVITTER_USER_TIMELINE_URL = '/api/qvitter/statuses/user_timeline.json' // const USER_URL = '/api/users/show.json' -import { each } from 'lodash' +import { each, map } from 'lodash' const oldfetch = window.fetch @@ -88,6 +90,13 @@ const authHeaders = (user) => { } } +const externalProfile = (profileUrl) => { + let url = `${EXTERNAL_PROFILE_URL}?profileurl=${profileUrl}` + return fetch(url, { + method: 'GET' + }).then((data) => data.json()) +} + const followUser = ({id, credentials}) => { let url = `${FOLLOWING_URL}?user_id=${id}` return fetch(url, { @@ -143,24 +152,34 @@ const setUserMute = ({id, credentials, muted = true}) => { }) } -const fetchTimeline = ({timeline, credentials, since = false, until = false}) => { +const fetchTimeline = ({timeline, credentials, since = false, until = false, userId = false}) => { const timelineUrls = { public: PUBLIC_TIMELINE_URL, friends: FRIENDS_TIMELINE_URL, mentions: MENTIONS_URL, - 'publicAndExternal': PUBLIC_AND_EXTERNAL_TIMELINE_URL + 'publicAndExternal': PUBLIC_AND_EXTERNAL_TIMELINE_URL, + user: QVITTER_USER_TIMELINE_URL } let url = timelineUrls[timeline] + let params = [] + if (since) { - url += `?since_id=${since}` + params.push(['since_id', since]) } if (until) { - url += `?max_id=${until}` + params.push(['max_id', until]) + } + + if (userId) { + params.push(['user_id', userId]) } + const queryString = map(params, (param) => `${param[0]}=${param[1]}`).join('&') + url += `?${queryString}` + return fetch(url, { headers: authHeaders(credentials) }).then((data) => data.json()) } @@ -253,7 +272,8 @@ const apiService = { setUserMute, fetchMutes, register, - updateAvatar + updateAvatar, + externalProfile } export default apiService diff --git a/src/services/backend_interactor_service/backend_interactor_service.js b/src/services/backend_interactor_service/backend_interactor_service.js index ceb559b6..5dbbf4b3 100644 --- a/src/services/backend_interactor_service/backend_interactor_service.js +++ b/src/services/backend_interactor_service/backend_interactor_service.js @@ -26,8 +26,8 @@ const backendInteractorService = (credentials) => { return apiService.unfollowUser({credentials, id}) } - const startFetching = ({timeline, store}) => { - return timelineFetcherService.startFetching({timeline, store, credentials}) + const startFetching = ({timeline, store, userId = false}) => { + return timelineFetcherService.startFetching({timeline, store, credentials, userId}) } const setUserMute = ({id, muted = true}) => { @@ -38,6 +38,7 @@ const backendInteractorService = (credentials) => { const register = (params) => apiService.register(params) const updateAvatar = ({params}) => apiService.updateAvatar({credentials, params}) + const externalProfile = (profileUrl) => apiService.externalProfile(profileUrl) const backendInteractorServiceInstance = { fetchStatus, @@ -51,7 +52,8 @@ const backendInteractorService = (credentials) => { setUserMute, fetchMutes, register, - updateAvatar + updateAvatar, + externalProfile } return backendInteractorServiceInstance diff --git a/src/services/completion/completion.js b/src/services/completion/completion.js new file mode 100644 index 00000000..8788d837 --- /dev/null +++ b/src/services/completion/completion.js @@ -0,0 +1,70 @@ +import { reduce, find } from 'lodash' + +export const replaceWord = (str, toReplace, replacement) => { + return str.slice(0, toReplace.start) + replacement + str.slice(toReplace.end) +} + +export const wordAtPosition = (str, pos) => { + const words = splitIntoWords(str) + const wordsWithPosition = addPositionToWords(words) + + return find(wordsWithPosition, ({start, end}) => start <= pos && end > pos) +} + +export const addPositionToWords = (words) => { + return reduce(words, (result, word) => { + const data = { + word, + start: 0, + end: word.length + } + + if (result.length > 0) { + const previous = result.pop() + + data.start += previous.end + data.end += previous.end + + result.push(previous) + } + + result.push(data) + + return result + }, []) +} + +export const splitIntoWords = (str) => { + // Split at word boundaries + const regex = /\b/ + const triggers = /[@#]+$/ + + let split = str.split(regex) + + // Add trailing @ and # to the following word. + const words = reduce(split, (result, word) => { + if (result.length > 0) { + let previous = result.pop() + const matches = previous.match(triggers) + if (matches) { + previous = previous.replace(triggers, '') + word = matches[0] + word + } + result.push(previous) + } + result.push(word) + + return result + }, []) + + return words +} + +const completion = { + wordAtPosition, + addPositionToWords, + splitIntoWords, + replaceWord +} + +export default completion diff --git a/src/services/timeline_fetcher/timeline_fetcher.service.js b/src/services/timeline_fetcher/timeline_fetcher.service.js index 24aef069..b28de9e7 100644 --- a/src/services/timeline_fetcher/timeline_fetcher.service.js +++ b/src/services/timeline_fetcher/timeline_fetcher.service.js @@ -14,7 +14,7 @@ const update = ({store, statuses, timeline, showImmediately}) => { }) } -const fetchAndUpdate = ({store, credentials, timeline = 'friends', older = false, showImmediately = false}) => { +const fetchAndUpdate = ({store, credentials, timeline = 'friends', older = false, showImmediately = false, userId = false}) => { const args = { timeline, credentials } const rootState = store.rootState || store.state const timelineData = rootState.statuses.timelines[camelCase(timeline)] @@ -25,14 +25,16 @@ const fetchAndUpdate = ({store, credentials, timeline = 'friends', older = false args['since'] = timelineData.maxId } + args['userId'] = userId + return apiService.fetchTimeline(args) .then((statuses) => update({store, statuses, timeline, showImmediately}), () => store.dispatch('setError', { value: true })) } -const startFetching = ({ timeline = 'friends', credentials, store }) => { - fetchAndUpdate({timeline, credentials, store, showImmediately: true}) - const boundFetchAndUpdate = () => fetchAndUpdate({ timeline, credentials, store }) +const startFetching = ({timeline = 'friends', credentials, store, userId = false}) => { + fetchAndUpdate({timeline, credentials, store, showImmediately: true, userId}) + const boundFetchAndUpdate = () => fetchAndUpdate({ timeline, credentials, store, userId }) return setInterval(boundFetchAndUpdate, 10000) } const timelineFetcher = { |
