notifications now also undergo some parsing, hypothetically could use MastoAPI

notifications, maybe.
This commit is contained in:
Henry Jameson 2019-01-14 22:38:37 +03:00
parent e0fd6d12ed
commit 790fcf37d2
4 changed files with 53 additions and 32 deletions

View file

@ -250,42 +250,33 @@ const addNewNotifications = (state, { dispatch, notifications, older, visibleNot
const allStatuses = state.allStatuses
const allStatusesObject = state.allStatusesObject
each(notifications, (notification) => {
const result = mergeOrAdd(allStatuses, allStatusesObject, notification.notice)
const action = result.item
notification.action = mergeOrAdd(allStatuses, allStatusesObject, notification.action).item
notification.status = notification.status && mergeOrAdd(allStatuses, allStatusesObject, notification.status).item
// Only add a new notification if we don't have one for the same action
if (!find(state.notifications.data, (oldNotification) => oldNotification.action.id === action.id)) {
if (!state.notifications.idStore.hasOwnProperty(notification.id)) {
state.notifications.maxId = Math.max(notification.id, state.notifications.maxId)
state.notifications.minId = Math.min(notification.id, state.notifications.minId)
const fresh = !notification.is_seen
const status = notification.ntype === 'like'
? action.favorited_status
: action
const result = {
type: notification.ntype,
status,
action,
seen: !fresh
}
state.notifications.data.push(result)
state.notifications.idStore[notification.id] = result
console.log('AWOOOOOO', notification)
state.notifications.data.push(notification)
state.notifications.idStore[notification.id] = notification
if ('Notification' in window && window.Notification.permission === 'granted') {
const notifObj = {}
const action = notification.action
const title = action.user.name
const result = {}
result.icon = action.user.profile_image_url
result.body = action.text // there's a problem that it doesn't put a space before links tho
notifObj.icon = action.user.profile_image_url
notifObj.body = action.text // there's a problem that it doesn't put a space before links tho
// Shows first attached non-nsfw image, if any. Should add configuration for this somehow...
if (action.attachments && action.attachments.length > 0 && !action.nsfw &&
action.attachments[0].mimetype.startsWith('image/')) {
result.image = action.attachments[0].url
notifObj.image = action.attachments[0].url
}
if (fresh && !state.notifications.desktopNotificationSilence && visibleNotificationTypes.includes(notification.ntype)) {
let notification = new window.Notification(title, result)
if (notification.fresh && !state.notifications.desktopNotificationSilence && visibleNotificationTypes.includes(notification.ntype)) {
let notification = new window.Notification(title, notifObj)
// Chrome is known for not closing notifications automatically
// according to MDN, anyway.
setTimeout(notification.close.bind(notification), 5000)

View file

@ -68,6 +68,7 @@ export const mutations = {
},
setUserForNotification (state, notification) {
notification.action.user = state.usersObject[notification.action.user.id]
notification.from_profile = state.usersObject[notification.action.user.id]
},
setColor (state, { user: { id }, highlighted }) {
const user = state.usersObject[id]
@ -149,8 +150,8 @@ const users = {
})
},
addNewNotifications (store, { notifications }) {
const users = compact(map(notifications, 'from_profile'))
const notificationIds = compact(notifications.map(_ => String(_.id)))
const users = map(notifications, 'from_profile')
const notificationIds = notifications.map(_ => String(_.id))
store.commit('addNewUsers', users)
const notificationsObject = store.rootState.statuses.notifications.idStore

View file

@ -44,7 +44,7 @@ const SUGGESTIONS_URL = '/api/v1/suggestions'
const MASTODON_USER_FAVORITES_TIMELINE_URL = '/api/v1/favourites'
import { each, map } from 'lodash'
import { parseStatus, parseUser } from '../status_normalizer/status_normalizer.service.js'
import { parseStatus, parseUser, parseNotification } from '../status_normalizer/status_normalizer.service.js'
import 'whatwg-fetch'
const oldfetch = window.fetch
@ -347,7 +347,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))
.then((data) => data.map(isNotifications ? parseNotification : parseStatus))
}
const verifyCredentials = (user) => {

View file

@ -23,10 +23,6 @@ const qvitterStatusType = (status) => {
return 'unknown'
}
const isMastoAPI = (status) => {
return status.hasOwnProperty('account')
}
export const parseUser = (data) => {
const output = {}
const masto = data.hasOwnProperty('acct')
@ -95,6 +91,7 @@ export const parseUser = (data) => {
}
const parseAttachment = (data) => {
// TODO A little bit messy ATM but works with both APIs
return {
...data,
mimetype: data.mimetype || data.type
@ -103,11 +100,12 @@ const parseAttachment = (data) => {
export const parseStatus = (data) => {
const output = {}
const masto = isMastoAPI(data)
const masto = data.hasOwnProperty('account')
output.raw = data
console.log(masto ? 'MAMMAL' : 'OLD SHIT')
console.log(data)
if (masto) {
output.favorited = data.favourited
output.fave_num = data.favourites_count
@ -172,6 +170,37 @@ export const parseStatus = (data) => {
return output
}
export const parseNotification = (data) => {
const mastoDict = {
'favourite': 'like',
'reblog': 'repeat'
}
const masto = !data.hasOwnProperty('ntype')
const output = {}
if (masto) {
output.type = mastoDict[data.type] || data.type
output.seen = null // missing
output.status = parseStatus(data.status)
output.action = null // missing
output.from_profile = parseUser(data.account)
} else {
const parsedNotice = parseStatus(data.notice)
output.type = data.ntype
output.seen = data.is_seen
output.status = output.type === 'like'
? parseStatus(data.notice.favorited_status)
: parsedNotice
output.action = parsedNotice
output.from_profile = parseUser(data.from_profile)
}
output.created_at = new Date(data.created_at)
output.id = data.id
return output
}
const isNsfw = (status) => {
const nsfwRegex = /#nsfw/i
return (status.tags || []).includes('nsfw') || !!status.text.match(nsfwRegex)