diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/components/conversation/conversation.js | 9 | ||||
| -rw-r--r-- | src/components/timeline/timeline.js | 17 | ||||
| -rw-r--r-- | src/components/user_profile/user_profile.js | 6 | ||||
| -rw-r--r-- | src/components/user_profile/user_profile.vue | 1 | ||||
| -rw-r--r-- | src/modules/api.js | 10 | ||||
| -rw-r--r-- | src/modules/statuses.js | 82 | ||||
| -rw-r--r-- | src/services/api/api.service.js | 17 | ||||
| -rw-r--r-- | src/services/status_normalizer/status_normalizer.service.js | 118 | ||||
| -rw-r--r-- | src/services/timeline_fetcher/timeline_fetcher.service.js | 4 |
9 files changed, 183 insertions, 81 deletions
diff --git a/src/components/conversation/conversation.js b/src/components/conversation/conversation.js index 9d9f7bbe..7bad14a5 100644 --- a/src/components/conversation/conversation.js +++ b/src/components/conversation/conversation.js @@ -1,9 +1,8 @@ import { reduce, filter, sortBy } from 'lodash' -import { statusType } from '../../modules/statuses.js' import Status from '../status/status.vue' const sortAndFilterConversation = (conversation) => { - conversation = filter(conversation, (status) => statusType(status) !== 'retweet') + conversation = filter(conversation, (status) => status.type !== 'retweet') return sortBy(conversation, 'id') } @@ -18,10 +17,12 @@ const conversation = { 'collapsable' ], computed: { - status () { return this.statusoid }, + status () { + return this.statusoid + }, conversation () { if (!this.status) { - return false + return [] } const conversationId = this.status.statusnet_conversation_id diff --git a/src/components/timeline/timeline.js b/src/components/timeline/timeline.js index 98da8660..23d2c1e8 100644 --- a/src/components/timeline/timeline.js +++ b/src/components/timeline/timeline.js @@ -7,7 +7,6 @@ import { throttle } from 'lodash' const Timeline = { props: [ 'timeline', - 'timelineName', 'title', 'userId', 'tag', @@ -55,7 +54,7 @@ const Timeline = { timelineFetcher.fetchAndUpdate({ store, credentials, - timeline: this.timelineName, + timeline: this.timeline, showImmediately, userId: this.userId, tag: this.tag @@ -70,32 +69,32 @@ const Timeline = { destroyed () { window.removeEventListener('scroll', this.scrollLoad) if (typeof document.hidden !== 'undefined') document.removeEventListener('visibilitychange', this.handleVisibilityChange, false) - this.$store.commit('setLoading', { timeline: this.timelineName, value: false }) + this.$store.commit('setLoading', { timeline: this.timeline, value: false }) }, methods: { showNewStatuses () { if (this.timeline.flushMarker !== 0) { - this.$store.commit('clearTimeline', { timeline: this.timelineName }) - this.$store.commit('queueFlush', { timeline: this.timelineName, id: 0 }) + this.$store.commit('clearTimeline', { timeline: this.timeline }) + this.$store.commit('queueFlush', { timeline: this.timeline, id: 0 }) this.fetchOlderStatuses() } else { - this.$store.commit('showNewStatuses', { timeline: this.timelineName }) + this.$store.commit('showNewStatuses', { timeline: this.timeline }) this.paused = false } }, fetchOlderStatuses: throttle(function () { const store = this.$store const credentials = store.state.users.currentUser.credentials - store.commit('setLoading', { timeline: this.timelineName, value: true }) + store.commit('setLoading', { timeline: this.timeline, value: true }) timelineFetcher.fetchAndUpdate({ store, credentials, - timeline: this.timelineName, + timeline: this.timeline, older: true, showImmediately: true, userId: this.userId, tag: this.tag - }).then(() => store.commit('setLoading', { timeline: this.timelineName, value: false })) + }).then(() => store.commit('setLoading', { timeline: this.timeline, value: false })) }, 1000, this), scrollLoad (e) { const bodyBRect = document.body.getBoundingClientRect() diff --git a/src/components/user_profile/user_profile.js b/src/components/user_profile/user_profile.js index bde20707..245d55ca 100644 --- a/src/components/user_profile/user_profile.js +++ b/src/components/user_profile/user_profile.js @@ -1,6 +1,7 @@ import UserCardContent from '../user_card_content/user_card_content.vue' import UserCard from '../user_card/user_card.vue' import Timeline from '../timeline/timeline.vue' +import { emptyTl } from '../../modules/statuses.js' const UserProfile = { created () { @@ -13,6 +14,11 @@ const UserProfile = { destroyed () { this.$store.dispatch('stopFetching', 'user') }, + data () { + return { + favorites: emptyTl({ type: 'favorites', userId: this.userId }) + } + }, computed: { timeline () { return this.$store.state.statuses.timelines.user diff --git a/src/components/user_profile/user_profile.vue b/src/components/user_profile/user_profile.vue index 50619026..265fc65b 100644 --- a/src/components/user_profile/user_profile.vue +++ b/src/components/user_profile/user_profile.vue @@ -20,6 +20,7 @@ <i class="icon-spin3 animate-spin"></i> </div> </div> + <Timeline :label="$t('user_card.favorites')" :embedded="true" :title="$t('user_profile.favorites_title')" :timeline="favorites"/> </tab-switcher> </div> <div v-else class="panel user-profile-placeholder"> diff --git a/src/modules/api.js b/src/modules/api.js index a61340c2..b85b24be 100644 --- a/src/modules/api.js +++ b/src/modules/api.js @@ -5,7 +5,7 @@ import { Socket } from 'phoenix' const api = { state: { backendInteractor: backendInteractorService(), - fetchers: {}, + fetchers: new Map(), socket: null, chatDisabled: false, followRequests: [] @@ -15,10 +15,10 @@ const api = { state.backendInteractor = backendInteractor }, addFetcher (state, {timeline, fetcher}) { - state.fetchers[timeline] = fetcher + state.fetchers.set(timeline, fetcher) }, removeFetcher (state, {timeline}) { - delete state.fetchers[timeline] + delete state.fetchers.delete(timeline) }, setSocket (state, socket) { state.socket = socket @@ -41,13 +41,13 @@ const api = { } // Don't start fetching if we already are. - if (!store.state.fetchers[timeline]) { + if (!store.state.fetchers.has(timeline)) { const fetcher = store.state.backendInteractor.startFetching({timeline, store, userId}) store.commit('addFetcher', {timeline, fetcher}) } }, stopFetching (store, timeline) { - const fetcher = store.state.fetchers[timeline] + const fetcher = store.state.fetchers.get(timeline) window.clearInterval(fetcher) store.commit('removeFetcher', {timeline}) }, diff --git a/src/modules/statuses.js b/src/modules/statuses.js index f92239a9..97b6d2ee 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -1,8 +1,8 @@ -import { includes, remove, slice, sortBy, toInteger, each, find, flatten, maxBy, minBy, merge, last, isArray } from 'lodash' +import { remove, slice, sortBy, toInteger, each, find, flatten, maxBy, minBy, merge, last, isArray } from 'lodash' import apiService from '../services/api/api.service.js' // import parse from '../services/status_parser/status_parser.js' -const emptyTl = (userId = 0) => ({ +export const emptyTl = (tl, userId) => (Object.assign(tl, { statuses: [], statusesObject: {}, faves: [], @@ -14,9 +14,10 @@ const emptyTl = (userId = 0) => ({ loading: false, followers: [], friends: [], + userId: 0, flushMarker: 0, userId -}) +})) export const defaultState = { allStatuses: [], @@ -33,30 +34,17 @@ export const defaultState = { favorites: new Set(), error: false, timelines: { - mentions: emptyTl(), - public: emptyTl(), - user: emptyTl(), - publicAndExternal: emptyTl(), - friends: emptyTl(), - tag: emptyTl(), - dms: emptyTl() + mentions: emptyTl({ type: 'mentions' }), + public: emptyTl({ type: 'public' }), + user: emptyTl({ type: 'user' }), // TODO: switch to unregistered + publicAndExternal: emptyTl({ type: 'publicAndExternal' }), + friends: emptyTl({ type: 'friends' }), + tag: emptyTl({ type: 'tag' }), + dms: emptyTl({ type: 'dms' }) } } -const isNsfw = (status) => { - const nsfwRegex = /#nsfw/i - return includes(status.tags, 'nsfw') || !!status.text.match(nsfwRegex) -} - export const prepareStatus = (status) => { - // Parse nsfw tags - if (status.nsfw === undefined) { - status.nsfw = isNsfw(status) - if (status.retweeted_status) { - status.nsfw = status.retweeted_status.nsfw - } - } - // Set deleted flag status.deleted = false @@ -75,31 +63,6 @@ const visibleNotificationTypes = (rootState) => { ].filter(_ => _) } -export const statusType = (status) => { - if (status.is_post_verb) { - return 'status' - } - - if (status.retweeted_status) { - return 'retweet' - } - - if ((typeof status.uri === 'string' && status.uri.match(/(fave|objectType=Favourite)/)) || - (typeof status.text === 'string' && status.text.match(/favorited/))) { - return 'favorite' - } - - if (status.text.match(/deleted notice {{tag/) || status.qvitter_delete_notice) { - return 'deletion' - } - - if (status.text.match(/started following/) || status.activity_type === 'follow') { - return 'follow' - } - - return 'unknown' -} - export const findMaxId = (...args) => { return (maxBy(flatten(args), 'id') || {}).id } @@ -137,7 +100,7 @@ const addNewStatuses = (state, { statuses, showImmediately = false, timeline, us const allStatuses = state.allStatuses const allStatusesObject = state.allStatusesObject - const timelineObject = state.timelines[timeline] + const timelineObject = typeof timeline === 'object' ? timeline : state.timelines[timeline] const maxNew = statuses.length > 0 ? maxBy(statuses, 'id').id : 0 const older = timeline && maxNew < timelineObject.maxId @@ -153,13 +116,13 @@ const addNewStatuses = (state, { statuses, showImmediately = false, timeline, us return } - const addStatus = (status, showImmediately, addToTimeline = true) => { - const result = mergeOrAdd(allStatuses, allStatusesObject, status) - status = result.item + const addStatus = (data, showImmediately, addToTimeline = true) => { + const result = mergeOrAdd(allStatuses, allStatusesObject, data) + const status = result.item if (result.new) { // We are mentioned in a post - if (statusType(status) === 'status' && find(status.attentions, { id: user.id })) { + if (status.type === 'status' && find(status.attentions, { id: user.id })) { const mentions = state.timelines.mentions // Add the mention to the mentions timeline @@ -270,7 +233,7 @@ const addNewStatuses = (state, { statuses, showImmediately = false, timeline, us } each(statuses, (status) => { - const type = statusType(status) + const type = status.type const processor = processors[type] || processors['default'] processor(status) }) @@ -337,7 +300,7 @@ export const mutations = { addNewStatuses, addNewNotifications, showNewStatuses (state, { timeline }) { - const oldTimeline = (state.timelines[timeline]) + const oldTimeline = (typeof timeline === 'object' ? timeline : state.timelines[timeline]) oldTimeline.newStatusCount = 0 oldTimeline.visibleStatuses = slice(oldTimeline.statuses, 0, 50) @@ -346,7 +309,8 @@ export const mutations = { each(oldTimeline.visibleStatuses, (status) => { oldTimeline.visibleStatusesObject[status.id] = status }) }, clearTimeline (state, { timeline }) { - state.timelines[timeline] = emptyTl(state.timelines[timeline].userId) + const timelineObject = typeof timeline === 'object' ? timeline : state.timelines[timeline] + emptyTl(timelineObject, state.timelines[timeline].userId) }, setFavorited (state, { status, value }) { const newStatus = state.allStatusesObject[status.id] @@ -366,7 +330,8 @@ export const mutations = { newStatus.deleted = true }, setLoading (state, { timeline, value }) { - state.timelines[timeline].loading = value + const timelineObject = typeof timeline === 'object' ? timeline : state.timelines[timeline] + timelineObject.loading = value }, setNsfw (state, { id, nsfw }) { const newStatus = state.allStatusesObject[id] @@ -387,7 +352,8 @@ export const mutations = { }) }, queueFlush (state, { timeline, id }) { - state.timelines[timeline].flushMarker = id + const timelineObject = typeof timeline === 'object' ? timeline : state.timelines[timeline] + timelineObject.flushMarker = id } } diff --git a/src/services/api/api.service.js b/src/services/api/api.service.js index 4ee95bd1..48e5d480 100644 --- a/src/services/api/api.service.js +++ b/src/services/api/api.service.js @@ -41,7 +41,10 @@ const APPROVE_USER_URL = '/api/pleroma/friendships/approve' const DENY_USER_URL = '/api/pleroma/friendships/deny' const SUGGESTIONS_URL = '/api/v1/suggestions' +const MASTODON_USER_FAVORITES_TIMELINE_URL = '/api/v1/favourites' + import { each, map } from 'lodash' +import { parseStatus } from '../status_normalizer/status_normalizer.service.js' import 'whatwg-fetch' const oldfetch = window.fetch @@ -70,6 +73,7 @@ const updateAvatar = ({credentials, params}) => { form.append(key, value) } }) + return fetch(url, { headers: authHeaders(credentials), method: 'POST', @@ -87,6 +91,7 @@ const updateBg = ({credentials, params}) => { form.append(key, value) } }) + return fetch(url, { headers: authHeaders(credentials), method: 'POST', @@ -110,6 +115,7 @@ const updateBanner = ({credentials, params}) => { form.append(key, value) } }) + return fetch(url, { headers: authHeaders(credentials), method: 'POST', @@ -267,12 +273,14 @@ const fetchConversation = ({id, credentials}) => { let url = `${CONVERSATION_URL}/${id}.json?count=100` return fetch(url, { headers: authHeaders(credentials) }) .then((data) => data.json()) + .then((data) => data.map(parseStatus)) } const fetchStatus = ({id, credentials}) => { let url = `${STATUS_URL}/${id}.json` return fetch(url, { headers: authHeaders(credentials) }) .then((data) => data.json()) + .then((data) => parseStatus(data)) } const setUserMute = ({id, credentials, muted = true}) => { @@ -300,12 +308,14 @@ const fetchTimeline = ({timeline, credentials, since = false, until = false, use notifications: QVITTER_USER_NOTIFICATIONS_URL, 'publicAndExternal': PUBLIC_AND_EXTERNAL_TIMELINE_URL, user: QVITTER_USER_TIMELINE_URL, + favorites: MASTODON_USER_FAVORITES_TIMELINE_URL, tag: TAG_TIMELINE_URL } + const type = timeline.type || timeline + const isNotifications = type === 'notifications' + const params = [] - let url = timelineUrls[timeline] - - let params = [] + let url = timelineUrls[type] if (since) { params.push(['since_id', since]) @@ -333,6 +343,7 @@ const fetchTimeline = ({timeline, credentials, since = false, until = false, use throw new Error('Error fetching timeline') }) .then((data) => data.json()) + .then((data) => data.map(isNotifications ? _ => _ : parseStatus)) } const verifyCredentials = (user) => { diff --git a/src/services/status_normalizer/status_normalizer.service.js b/src/services/status_normalizer/status_normalizer.service.js new file mode 100644 index 00000000..ce7cd050 --- /dev/null +++ b/src/services/status_normalizer/status_normalizer.service.js @@ -0,0 +1,118 @@ +export const qvitterStatusType = (status) => { + if (status.is_post_verb) { + return 'status' + } + + if (status.retweeted_status) { + return 'retweet' + } + + if ((typeof status.uri === 'string' && status.uri.match(/(fave|objectType=Favourite)/)) || + (typeof status.text === 'string' && status.text.match(/favorited/))) { + return 'favorite' + } + + if (status.text.match(/deleted notice {{tag/) || status.qvitter_delete_notice) { + return 'deletion' + } + + if (status.text.match(/started following/) || status.activity_type === 'follow') { + return 'follow' + } + + return 'unknown' +} + +const isMastoAPI = (status) => { + return status.hasOwnProperty('account') +} + +const parseUser = (data) => { + return { + id: data.id, + screen_name: data.screen_name || data.acct + } +} + +const parseAttachment = (data) => { + return { + ...data, + mimetype: data.mimetype || data.type + } +} + +export const parseStatus = (data) => { + const output = {} + const masto = isMastoAPI(data) + output.raw = data + + console.log(masto ? 'MAMMAL' : 'OLD SHIT') + console.log(data) + if (masto) { + output.favorited = data.favourited + output.fave_num = data.favourites_count + + output.repeated = data.reblogged + output.repeat_num = data.reblogs_count + + output.type = data.reblog ? 'retweet' : 'status' + output.nsfw = data.sensitive + + output.statusnet_html = data.content + // Not exactly the same... + output.text = data.content + + output.in_reply_to_status_id = data.in_reply_to_id + output.in_reply_to_user_id = data.in_reply_to_user_id + } else { + output.favorited = data.favorited + output.fave_num = data.fave_num + + output.repeated = data.repeated + output.repeat_num = data.repeat_num + + // catchall, temporary + // Object.assign(output, data) + + output.type = qvitterStatusType(data) + + if (data.nsfw === undefined) { + output.nsfw = isNsfw(data) + if (data.retweeted_status) { + output.nsfw = data.retweeted_status.nsfw + } + } + output.statusnet_html = data.statusnet_html + output.text = data.text + + output.in_reply_to_status_id = data.in_reply_to_id + output.in_reply_to_user_id = data.in_reply_to_account_id + } + + output.id = Number(data.id) + output.visibility = data.visibility + output.created_at = new Date(data.created_at) + + output.user = parseUser(masto ? data.account : data.user) + + output.attentions = ((masto ? data.mentions : data.attentions) || []) + .map(_ => ({ + id: _.id, + following: _.following // FIXME: MastoAPI doesn't have this + })) + + output.attachments = ((masto ? data.media_attachments : data.attachments) || []) + .map(parseAttachment) + + const retweetedStatus = masto ? data.reblog : data.retweeted_status + if (retweetedStatus) { + output.retweeted_status = parseStatus(retweetedStatus) + } + + return output +} + +const isNsfw = (status) => { + const nsfwRegex = /#nsfw/i + return (status.tags || []).includes('nsfw') || !!status.text.match(nsfwRegex) +} diff --git a/src/services/timeline_fetcher/timeline_fetcher.service.js b/src/services/timeline_fetcher/timeline_fetcher.service.js index 727f6c60..126e07cf 100644 --- a/src/services/timeline_fetcher/timeline_fetcher.service.js +++ b/src/services/timeline_fetcher/timeline_fetcher.service.js @@ -3,7 +3,7 @@ import { camelCase } from 'lodash' import apiService from '../api/api.service.js' const update = ({store, statuses, timeline, showImmediately, userId}) => { - const ccTimeline = camelCase(timeline) + const ccTimeline = typeof timeline === 'object' ? timeline : camelCase(timeline) store.dispatch('setError', { value: false }) @@ -18,7 +18,7 @@ const update = ({store, statuses, timeline, showImmediately, userId}) => { 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 timelineData = typeof timeline === 'object' ? timeline : rootState.statuses.timelines[camelCase(timeline)] if (older) { args['until'] = until || timelineData.minVisibleId |
