From b555d617e4fc1b2ea3fef7e56ffcfde983583418 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Sun, 2 Dec 2018 13:36:11 +0300 Subject: [PATCH] removes hacks from notifications storage, adds api call to let server update is_seen attribute --- src/components/notifications/notifications.js | 2 +- src/main.js | 1 - src/modules/statuses.js | 17 ++++++++++------- src/services/api/api.service.js | 16 +++++++++++++++- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/components/notifications/notifications.js b/src/components/notifications/notifications.js index 58956f98..945ffd1f 100644 --- a/src/components/notifications/notifications.js +++ b/src/components/notifications/notifications.js @@ -52,7 +52,7 @@ const Notifications = { }, methods: { markAsSeen () { - this.$store.commit('markNotificationsAsSeen', this.visibleNotifications) + this.$store.dispatch('markNotificationsAsSeen', this.visibleNotifications) }, fetchOlderNotifications () { const store = this.$store diff --git a/src/main.js b/src/main.js index 9f094ada..378fe95c 100644 --- a/src/main.js +++ b/src/main.js @@ -47,7 +47,6 @@ const persistedStateOptions = { paths: [ 'config', 'users.lastLoginName', - 'statuses.notifications.maxSavedId', 'oauth' ] } diff --git a/src/modules/statuses.js b/src/modules/statuses.js index 2c3d2550..24be847d 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -1,5 +1,4 @@ import { includes, remove, slice, sortBy, toInteger, each, find, flatten, maxBy, minBy, merge, last, isArray } from 'lodash' -import { set } from 'vue' import apiService from '../services/api/api.service.js' // import parse from '../services/status_parser/status_parser.js' @@ -26,7 +25,6 @@ export const defaultState = { notifications: { desktopNotificationSilence: true, maxId: 0, - maxSavedId: 0, minId: Number.POSITIVE_INFINITY, data: [], error: false, @@ -297,7 +295,7 @@ const addNewNotifications = (state, { dispatch, notifications, older, visibleNot state.notifications.maxId = Math.max(notification.id, state.notifications.maxId) state.notifications.minId = Math.min(notification.id, state.notifications.minId) - const fresh = !older && !notification.is_seen && notification.id > state.notifications.maxSavedId + const fresh = !notification.is_seen const status = notification.ntype === 'like' ? find(allStatuses, { id: action.in_reply_to_status_id }) : action @@ -306,7 +304,6 @@ const addNewNotifications = (state, { dispatch, notifications, older, visibleNot type: notification.ntype, status, action, - // Always assume older notifications as seen seen: !fresh } @@ -404,9 +401,8 @@ export const mutations = { addFollowers (state, { followers }) { state.timelines['user'].followers = followers }, - markNotificationsAsSeen (state, notifications) { - set(state.notifications, 'maxSavedId', state.notifications.maxId) - each(notifications, (notification) => { + markNotificationsAsSeen (state) { + each(state.notifications.data, (notification) => { notification.seen = true }) }, @@ -484,6 +480,13 @@ const statuses = { }, queueFlush ({ rootState, commit }, { timeline, id }) { commit('queueFlush', { timeline, id }) + }, + markNotificationsAsSeen ({ rootState, commit }) { + commit('markNotificationsAsSeen') + apiService.markNotificationsAsSeen({ + id: rootState.statuses.notifications.maxId, + credentials: rootState.users.currentUser.credentials + }) } }, mutations diff --git a/src/services/api/api.service.js b/src/services/api/api.service.js index 714c0fc7..ae876b7f 100644 --- a/src/services/api/api.service.js +++ b/src/services/api/api.service.js @@ -29,6 +29,7 @@ const PROFILE_UPDATE_URL = '/api/account/update_profile.json' const EXTERNAL_PROFILE_URL = '/api/externalprofile/show.json' const QVITTER_USER_TIMELINE_URL = '/api/qvitter/statuses/user_timeline.json' const QVITTER_USER_NOTIFICATIONS_URL = '/api/qvitter/statuses/notifications.json' +const QVITTER_USER_NOTIFICATIONS_READ_URL = '/api/qvitter/statuses/notifications/read.json' const BLOCKING_URL = '/api/blocks/create.json' const UNBLOCKING_URL = '/api/blocks/destroy.json' const USER_URL = '/api/users/show.json' @@ -460,6 +461,18 @@ const suggestions = ({credentials}) => { }).then((data) => data.json()) } +const markNotificationsAsSeen = ({id, credentials}) => { + const body = new FormData() + + body.append('latest_id', id) + + return fetch(QVITTER_USER_NOTIFICATIONS_READ_URL, { + body, + headers: authHeaders(credentials), + method: 'POST' + }).then((data) => data.json()) +} + const apiService = { verifyCredentials, fetchTimeline, @@ -494,7 +507,8 @@ const apiService = { fetchFollowRequests, approveUser, denyUser, - suggestions + suggestions, + markNotificationsAsSeen } export default apiService