You've already forked AstralRinth
forked from didirus/AstralRinth
Threads and more! (#1232)
* Begin UI for threads and moderation overhaul * Hide close button on non-report threads * Fix review age coloring * Add project count * Remove action buttons from queue page and add queued date to project page * Hook up to actual data * Remove unused icon * Get up to 1000 projects in queue * prettier * more prettier * Changed all the things * lint * rebuild * Add omorphia * Workaround formatjs bug in ThreadSummary.vue * Fix notifications page on prod * Fix a few notifications and threads bugs * lockfile * Fix duplicate button styles * more fixes and polishing * More fixes * Remove legacy pages * More bugfixes * Add some error catching for reports and notifications * More error handling * fix lint * Add inbox links * Remove loading component and rename member header * Rely on threads always existing * Handle if project update notifs are not grouped * oops * Fix chips on notifications page * Import ModalModeration * finish threads --------- Co-authored-by: triphora <emma@modrinth.com> Co-authored-by: Jai A <jaiagr+gpg@pm.me>
This commit is contained in:
191
helpers/notifications.js
Normal file
191
helpers/notifications.js
Normal file
@@ -0,0 +1,191 @@
|
||||
import { useNuxtApp } from '#app'
|
||||
import { userReadNotifications } from '~/composables/user.js'
|
||||
|
||||
async function getBulk(type, ids) {
|
||||
const auth = (await useAuth()).value
|
||||
if (ids.length === 0) {
|
||||
return []
|
||||
}
|
||||
|
||||
const url = `${type}?ids=${JSON.stringify([...new Set(ids)])}`
|
||||
const { data: bulkFetch } = await useAsyncData(url, () => useBaseFetch(url, auth.headers))
|
||||
return bulkFetch.value
|
||||
}
|
||||
|
||||
export async function fetchNotifications() {
|
||||
try {
|
||||
const auth = (await useAuth()).value
|
||||
const { data: notifications } = await useAsyncData(`user/${auth.user.id}/notifications`, () =>
|
||||
useBaseFetch(`user/${auth.user.id}/notifications`, auth.headers)
|
||||
)
|
||||
|
||||
const projectIds = []
|
||||
const reportIds = []
|
||||
const threadIds = []
|
||||
const userIds = []
|
||||
const versionIds = []
|
||||
|
||||
for (const notification of notifications.value) {
|
||||
if (notification.body) {
|
||||
if (notification.body.project_id) {
|
||||
projectIds.push(notification.body.project_id)
|
||||
}
|
||||
if (notification.body.version_id) {
|
||||
versionIds.push(notification.body.version_id)
|
||||
}
|
||||
if (notification.body.report_id) {
|
||||
reportIds.push(notification.body.report_id)
|
||||
}
|
||||
if (notification.body.thread_id) {
|
||||
threadIds.push(notification.body.thread_id)
|
||||
}
|
||||
if (notification.body.invited_by) {
|
||||
userIds.push(notification.body.invited_by)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const reports = await getBulk('reports', reportIds)
|
||||
|
||||
for (const report of reports) {
|
||||
if (report.item_type === 'project') {
|
||||
projectIds.push(report.item_id)
|
||||
} else if (report.item_type === 'user') {
|
||||
userIds.push(report.item_id)
|
||||
} else if (report.item_type === 'version') {
|
||||
versionIds.push(report.item_id)
|
||||
}
|
||||
}
|
||||
|
||||
const versions = await getBulk('versions', versionIds)
|
||||
|
||||
for (const version of versions) {
|
||||
projectIds.push(version.project_id)
|
||||
}
|
||||
|
||||
const projects = await getBulk('projects', projectIds)
|
||||
const threads = await getBulk('threads', threadIds)
|
||||
const users = await getBulk('users', userIds)
|
||||
|
||||
for (const notification of notifications.value) {
|
||||
notification.extra_data = {}
|
||||
if (notification.body) {
|
||||
if (notification.body.project_id) {
|
||||
notification.extra_data.project = projects.find(
|
||||
(x) => x.id === notification.body.project_id
|
||||
)
|
||||
}
|
||||
if (notification.body.report_id) {
|
||||
notification.extra_data.report = reports.find((x) => x.id === notification.body.report_id)
|
||||
|
||||
const type = notification.extra_data.report.item_type
|
||||
if (type === 'project') {
|
||||
notification.extra_data.project = projects.find(
|
||||
(x) => x.id === notification.extra_data.report.item_id
|
||||
)
|
||||
} else if (type === 'user') {
|
||||
notification.extra_data.user = users.find(
|
||||
(x) => x.id === notification.extra_data.report.item_id
|
||||
)
|
||||
} else if (type === 'version') {
|
||||
notification.extra_data.version = versions.find(
|
||||
(x) => x.id === notification.extra_data.report.item_id
|
||||
)
|
||||
notification.extra_data.project = projects.find(
|
||||
(x) => x.id === notification.extra_data.version.project_id
|
||||
)
|
||||
}
|
||||
}
|
||||
if (notification.body.thread_id) {
|
||||
notification.extra_data.thread = threads.find((x) => x.id === notification.body.thread_id)
|
||||
}
|
||||
if (notification.body.invited_by) {
|
||||
notification.extra_data.invited_by = users.find(
|
||||
(x) => x.id === notification.body.invited_by
|
||||
)
|
||||
}
|
||||
if (notification.body.version_id) {
|
||||
notification.extra_data.version = versions.find(
|
||||
(x) => x.id === notification.body.version_id
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return notifications.value
|
||||
} catch (error) {
|
||||
const app = useNuxtApp()
|
||||
app.$notify({
|
||||
group: 'main',
|
||||
title: 'Error loading notifications',
|
||||
text: error.data ? error.data.description : error,
|
||||
type: 'error',
|
||||
})
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
export function groupNotifications(notifications, includeRead = false) {
|
||||
const grouped = []
|
||||
for (const notification of notifications.filter((notif) => includeRead || !notif.read)) {
|
||||
// Group notifications of the same thread or project id
|
||||
if (notification.body) {
|
||||
const index = grouped.findIndex(
|
||||
(notif) =>
|
||||
(notif.body.thread_id === notification.body.thread_id ||
|
||||
notif.body.project_id === notification.body.project_id) &&
|
||||
notification.read === notif.read
|
||||
)
|
||||
const notif = grouped[index]
|
||||
if (
|
||||
notif &&
|
||||
(notification.body.type === 'moderator_message' ||
|
||||
notification.body.type === 'project_update')
|
||||
) {
|
||||
let groupedNotifs = notif.grouped_notifs
|
||||
if (!groupedNotifs) {
|
||||
groupedNotifs = []
|
||||
}
|
||||
groupedNotifs.push(notification)
|
||||
grouped[index].grouped_notifs = groupedNotifs
|
||||
} else {
|
||||
grouped.push(notification)
|
||||
}
|
||||
} else {
|
||||
grouped.push(notification)
|
||||
}
|
||||
}
|
||||
|
||||
return grouped
|
||||
}
|
||||
|
||||
export async function markAsRead(ids) {
|
||||
try {
|
||||
const auth = (await useAuth()).value
|
||||
await useBaseFetch(`notifications?ids=${JSON.stringify([...new Set(ids)])}`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
Authorization: auth.token,
|
||||
},
|
||||
})
|
||||
await userReadNotifications(ids)
|
||||
return (notifications) => {
|
||||
const newNotifs = notifications
|
||||
newNotifs.forEach((notif) => {
|
||||
if (ids.includes(notif.id)) {
|
||||
notif.read = true
|
||||
}
|
||||
})
|
||||
return newNotifs
|
||||
}
|
||||
} catch (err) {
|
||||
const app = useNuxtApp()
|
||||
app.$notify({
|
||||
group: 'main',
|
||||
title: 'Error marking notification as read',
|
||||
text: err.data ? err.data.description : err,
|
||||
type: 'error',
|
||||
})
|
||||
return () => {}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user