#436: integrate mastoAPI notifications

This commit is contained in:
dave 2019-03-12 17:16:57 -04:00
parent f397537642
commit cd9a7dd488
8 changed files with 135 additions and 42 deletions

View file

@ -21,6 +21,9 @@ const Notification = {
}, },
userProfileLink (user) { userProfileLink (user) {
return generateProfileLink(user.id, user.screen_name, this.$store.state.instance.restrictedNicknames) return generateProfileLink(user.id, user.screen_name, this.$store.state.instance.restrictedNicknames)
},
dismiss () {
this.$store.dispatch('dismissNotifications', { id: this.notification.id })
} }
}, },
computed: { computed: {

View file

@ -1,44 +1,57 @@
<template> <template>
<status v-if="notification.type === 'mention'" :compact="true" :statusoid="notification.status"></status> <div class="notification-item">
<div class="non-mention" :class="[userClass, { highlighted: userStyle }]" :style="[ userStyle ]"v-else> <button @click.prevent="dismiss" class="btn-dismiss">{{$t("notifications.dismiss")}}</button>
<a class='avatar-container' :href="notification.action.user.statusnet_profile_url" @click.stop.prevent.capture="toggleUserExpanded"> <status v-if="notification.type === 'mention'" :compact="true" :statusoid="notification.status"></status>
<UserAvatar :compact="true" :betterShadow="betterShadow" :src="notification.action.user.profile_image_url_original"/> <div class="non-mention" :class="[userClass, { highlighted: userStyle }]" :style="[ userStyle ]"v-else>
</a> <a class='avatar-container' :href="notification.action.user.statusnet_profile_url" @click.stop.prevent.capture="toggleUserExpanded">
<div class='notification-right'> <UserAvatar :compact="true" :betterShadow="betterShadow" :src="notification.action.user.profile_image_url_original"/>
<UserCard :user="notification.action.user" :rounded="true" :bordered="true" v-if="userExpanded"/> </a>
<span class="notification-details"> <div class='notification-right'>
<div class="name-and-action"> <UserCard :user="notification.action.user" :rounded="true" :bordered="true" v-if="userExpanded"/>
<span class="username" v-if="!!notification.action.user.name_html" :title="'@'+notification.action.user.screen_name" v-html="notification.action.user.name_html"></span> <span class="notification-details">
<span class="username" v-else :title="'@'+notification.action.user.screen_name">{{ notification.action.user.name }}</span> <div class="name-and-action">
<span v-if="notification.type === 'like'"> <span class="username" v-if="!!notification.action.user.name_html" :title="'@'+notification.action.user.screen_name" v-html="notification.action.user.name_html"></span>
<i class="fa icon-star lit"></i> <span class="username" v-else :title="'@'+notification.action.user.screen_name">{{ notification.action.user.name }}</span>
<small>{{$t('notifications.favorited_you')}}</small> <span v-if="notification.type === 'like'">
</span> <i class="fa icon-star lit"></i>
<span v-if="notification.type === 'repeat'"> <small>{{$t('notifications.favorited_you')}}</small>
<i class="fa icon-retweet lit" :title="$t('tool_tip.repeat')"></i> </span>
<small>{{$t('notifications.repeated_you')}}</small> <span v-if="notification.type === 'repeat'">
</span> <i class="fa icon-retweet lit" :title="$t('tool_tip.repeat')"></i>
<span v-if="notification.type === 'follow'"> <small>{{$t('notifications.repeated_you')}}</small>
<i class="fa icon-user-plus lit"></i> </span>
<small>{{$t('notifications.followed_you')}}</small> <span v-if="notification.type === 'follow'">
</span> <i class="fa icon-user-plus lit"></i>
</div> <small>{{$t('notifications.followed_you')}}</small>
<div class="timeago"> </span>
<router-link v-if="notification.status" :to="{ name: 'conversation', params: { id: notification.status.id } }" class="faint-link"> </div>
<timeago :since="notification.action.created_at" :auto-update="240"></timeago> <div class="timeago">
<router-link v-if="notification.status" :to="{ name: 'conversation', params: { id: notification.status.id } }" class="faint-link">
<timeago :since="notification.action.created_at" :auto-update="240"></timeago>
</router-link>
</div>
</span>
<div class="follow-text" v-if="notification.type === 'follow'">
<router-link :to="userProfileLink(notification.action.user)">
@{{notification.action.user.screen_name}}
</router-link> </router-link>
</div> </div>
</span> <template v-else>
<div class="follow-text" v-if="notification.type === 'follow'"> <status class="faint" :compact="true" :statusoid="notification.status" :noHeading="true"></status>
<router-link :to="userProfileLink(notification.action.user)"> </template>
@{{notification.action.user.screen_name}}
</router-link>
</div> </div>
<template v-else>
<status class="faint" :compact="true" :statusoid="notification.status" :noHeading="true"></status>
</template>
</div> </div>
</div> </div>
</template> </template>
<script src="./notification.js"></script> <script src="./notification.js"></script>
<style lang="scss">
@import '../../_variables.scss';
.notification-item {
display: flex;
flex: 1;
flex-direction: column;
}
</style>

View file

@ -53,7 +53,10 @@ const Notifications = {
}, },
methods: { methods: {
markAsSeen () { markAsSeen () {
this.$store.dispatch('markNotificationsAsSeen', this.visibleNotifications) this.$store.dispatch('markNotificationsAsSeen')
},
clear () {
this.$store.dispatch('clearNotifications')
}, },
fetchOlderNotifications () { fetchOlderNotifications () {
const store = this.$store const store = this.$store

View file

@ -9,7 +9,8 @@
<div @click.prevent class="loadmore-error alert error" v-if="error"> <div @click.prevent class="loadmore-error alert error" v-if="error">
{{$t('timeline.error_fetching')}} {{$t('timeline.error_fetching')}}
</div> </div>
<button v-if="unseenCount" @click.prevent="markAsSeen" class="read-button">{{$t('notifications.read')}}</button> <!-- <button v-if="unseenCount" @click.prevent="markAsSeen" class="read-button">{{$t('notifications.read')}}</button> -->
<button v-if="notifications.length" @click.prevent="clear" class="read-button">{{$t('notifications.clear')}}</button>
</div> </div>
<div class="panel-body"> <div class="panel-body">
<div v-for="notification in visibleNotifications" :key="notification.action.id" class="notification" :class='{"unseen": !notification.seen}'> <div v-for="notification in visibleNotifications" :key="notification.action.id" class="notification" :class='{"unseen": !notification.seen}'>

View file

@ -62,6 +62,8 @@
"load_older": "Load older notifications", "load_older": "Load older notifications",
"notifications": "Notifications", "notifications": "Notifications",
"read": "Read!", "read": "Read!",
"clear": "Clear!",
"dismiss": "Dismiss!",
"repeated_you": "repeated your status", "repeated_you": "repeated your status",
"no_more_notifications": "No more notifications" "no_more_notifications": "No more notifications"
}, },

View file

@ -1,4 +1,4 @@
import { remove, slice, each, find, maxBy, minBy, merge, first, last, isArray } from 'lodash' import { remove, slice, each, find, findIndex, maxBy, minBy, merge, first, last, isArray } from 'lodash'
import apiService from '../services/api/api.service.js' import apiService from '../services/api/api.service.js'
// import parse from '../services/status_parser/status_parser.js' // import parse from '../services/status_parser/status_parser.js'
@ -390,6 +390,27 @@ export const mutations = {
notification.seen = true notification.seen = true
}) })
}, },
clearNotifications (state) {
state.notifications.data = []
state.notifications.idStore = {}
state.notifications.maxId = 0
state.notifications.minId = 0
},
dismissNotifications (state, { id }) {
const { data } = state.notifications
const idx = findIndex(data, { id })
if (idx !== -1) {
const notification = data[idx]
data.splice(idx, 1)
delete state.notifications.idStore[id]
if (state.notifications.maxId === notification.id) {
state.notifications.maxId = data.length ? maxBy(data, 'id').id : 0
} else if (state.notifications.minId === notification.id) {
state.notifications.minId = data.length ? minBy(data, 'id').id : 0
}
}
},
queueFlush (state, { timeline, id }) { queueFlush (state, { timeline, id }) {
state.timelines[timeline].flushMarker = id state.timelines[timeline].flushMarker = id
} }
@ -474,6 +495,19 @@ const statuses = {
id: rootState.statuses.notifications.maxId, id: rootState.statuses.notifications.maxId,
credentials: rootState.users.currentUser.credentials credentials: rootState.users.currentUser.credentials
}) })
},
clearNotifications ({ rootState, commit }) {
commit('clearNotifications')
apiService.clearNotifications({
credentials: rootState.users.currentUser.credentials
})
},
dismissNotifications ({ rootState, commit }, { id }) {
commit('dismissNotifications', { id })
apiService.dismissNotifications({
id,
credentials: rootState.users.currentUser.credentials
})
} }
}, },
mutations mutations

View file

@ -29,7 +29,6 @@ const BANNER_UPDATE_URL = '/api/account/update_profile_banner.json'
const PROFILE_UPDATE_URL = '/api/account/update_profile.json' const PROFILE_UPDATE_URL = '/api/account/update_profile.json'
const EXTERNAL_PROFILE_URL = '/api/externalprofile/show.json' const EXTERNAL_PROFILE_URL = '/api/externalprofile/show.json'
const QVITTER_USER_TIMELINE_URL = '/api/qvitter/statuses/user_timeline.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 QVITTER_USER_NOTIFICATIONS_READ_URL = '/api/qvitter/statuses/notifications/read.json'
const BLOCKING_URL = '/api/blocks/create.json' const BLOCKING_URL = '/api/blocks/create.json'
const UNBLOCKING_URL = '/api/blocks/destroy.json' const UNBLOCKING_URL = '/api/blocks/destroy.json'
@ -43,6 +42,9 @@ const DENY_USER_URL = '/api/pleroma/friendships/deny'
const SUGGESTIONS_URL = '/api/v1/suggestions' const SUGGESTIONS_URL = '/api/v1/suggestions'
const MASTODON_USER_FAVORITES_TIMELINE_URL = '/api/v1/favourites' const MASTODON_USER_FAVORITES_TIMELINE_URL = '/api/v1/favourites'
const MASTODON_USER_NOTIFICATIONS_URL = '/api/v1/notifications'
const MASTODON_USER_NOTIFICATIONS_CLEAR_URL = '/api/v1/notifications/clear'
const MASTODON_USER_NOTIFICATIONS_DISMISS_URL = '/api/v1/notifications/dismiss'
import { each, map } from 'lodash' import { each, map } from 'lodash'
import { parseStatus, parseUser, parseNotification } from '../entity_normalizer/entity_normalizer.service.js' import { parseStatus, parseUser, parseNotification } from '../entity_normalizer/entity_normalizer.service.js'
@ -345,7 +347,7 @@ const fetchTimeline = ({timeline, credentials, since = false, until = false, use
friends: FRIENDS_TIMELINE_URL, friends: FRIENDS_TIMELINE_URL,
mentions: MENTIONS_URL, mentions: MENTIONS_URL,
dms: DM_TIMELINE_URL, dms: DM_TIMELINE_URL,
notifications: QVITTER_USER_NOTIFICATIONS_URL, notifications: MASTODON_USER_NOTIFICATIONS_URL,
'publicAndExternal': PUBLIC_AND_EXTERNAL_TIMELINE_URL, 'publicAndExternal': PUBLIC_AND_EXTERNAL_TIMELINE_URL,
user: QVITTER_USER_TIMELINE_URL, user: QVITTER_USER_TIMELINE_URL,
media: QVITTER_USER_TIMELINE_URL, media: QVITTER_USER_TIMELINE_URL,
@ -575,6 +577,25 @@ const markNotificationsAsSeen = ({id, credentials}) => {
}).then((data) => data.json()) }).then((data) => data.json())
} }
const clearNotifications = ({ credentials }) => {
return fetch(MASTODON_USER_NOTIFICATIONS_CLEAR_URL, {
headers: authHeaders(credentials),
method: 'POST'
}).then((data) => data.json())
}
const dismissNotifications = ({ id, credentials }) => {
const body = new FormData()
body.append('id', id)
return fetch(MASTODON_USER_NOTIFICATIONS_DISMISS_URL, {
body,
headers: authHeaders(credentials),
method: 'POST'
}).then((data) => data.json())
}
const apiService = { const apiService = {
verifyCredentials, verifyCredentials,
fetchTimeline, fetchTimeline,
@ -615,7 +636,9 @@ const apiService = {
approveUser, approveUser,
denyUser, denyUser,
suggestions, suggestions,
markNotificationsAsSeen markNotificationsAsSeen,
clearNotifications,
dismissNotifications
} }
export default apiService export default apiService

View file

@ -249,6 +249,18 @@ export const parseStatus = (data) => {
return output return output
} }
export const parseFollow = (data) => {
const output = {}
output.id = String(data.id)
output.visibility = true
output.created_at = new Date(data.created_at)
// Converting to string, the right way.
output.user = parseUser(data.account)
return output
}
export const parseNotification = (data) => { export const parseNotification = (data) => {
const mastoDict = { const mastoDict = {
'favourite': 'like', 'favourite': 'like',
@ -260,7 +272,9 @@ export const parseNotification = (data) => {
if (masto) { if (masto) {
output.type = mastoDict[data.type] || data.type output.type = mastoDict[data.type] || data.type
output.seen = null // missing output.seen = null // missing
output.status = parseStatus(data.status) output.status = output.type === 'follow'
? parseFollow(data)
: parseStatus(data.status)
output.action = output.status // not sure output.action = output.status // not sure
output.from_profile = parseUser(data.account) output.from_profile = parseUser(data.account)
} else { } else {