Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | 5x 81x 81x 81x 81x 81x 81x 3x 2x 2x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 1x 3x 3x 31x 24x 59x 3x 1x 1x 2x 1x 2x 81x | // @implements UC-NOTIF-001.1, UC-NOTIF-002.1, UC-WORKFLOW-011.1, UC-REPORT-006.1
import { defineStore } from 'pinia'
import { ref } from 'vue'
import api from '@/services/api'
type MultilingualText = string | Record<string, string>
// What a notification about one of the user's comments carries: a snapshot,
// since the comment may be gone by the time it is read. A comment on a store
// pack carries the pack in place of the entity and item.
export interface CommentNotificationData {
commentId: string
commentExcerpt: string
entityId?: string
entityName?: MultilingualText
itemId?: string
itemLabel?: MultilingualText | null
packId?: string
packName?: MultilingualText
}
// What the flow owner is told when an Email action of a workflow transition
// could not reach some of its recipients (UC-WORKFLOW-011.1)
export interface AutomationEmailSkippedData {
// The item's flow (absent from notifications older than flow URLs)
flowId?: string
entityId: string
entityName: MultilingualText
itemId: string
itemLabel: MultilingualText | null
templateName: MultilingualText
recipients: Array<{ userId: string; reason: 'not-member' | 'no-email' }>
}
// What the owner of a flow that installed a pack is told when a new version
// is out (UC-STORE-013.1): the pack page offers to update the flow's copies
export interface PackVersionPublishedData {
packId: string
packName: MultilingualText
version: number
flowId: string
flowName: string
}
// What the author of reported content is told when a platform admin acted on
// it (UC-REPORT-006.1): the statement of reasons, and where to contest it
export interface ModerationDecisionData {
targetType: 'comment' | 'conversation' | 'pack'
contentExcerpt: string | null
packName?: MultilingualText
reason: string
removed: boolean
sanction: 'warning' | 'restriction' | 'ban'
restrictedUntil: string | null
note: string | null
contactEmail: string
// What an appeal names (UC-REPORT-009.1); missing on notifications sent before appeals
targetKey?: string
decisionId?: string
}
// What a reporter is told once a platform admin decided (UC-REPORT-006.1)
export interface ReportDecidedData {
targetType: 'comment' | 'conversation' | 'pack'
contentExcerpt: string | null
packName?: MultilingualText
outcome: 'actioned' | 'dismissed'
// A dismissal may be appealed (UC-REPORT-009.1)
targetKey?: string
decisionId?: string
}
// What an appellant is told once a platform admin decided their appeal (UC-REPORT-010.1)
export interface AppealDecidedData {
role: 'author' | 'reporter'
outcome: 'upheld' | 'overturned'
targetType: 'comment' | 'conversation' | 'pack'
contentExcerpt: string | null
packName?: MultilingualText
sanctionLifted: boolean
note: string | null
}
// A platform admin answered reports filed in bad faith (UC-REPORT-011.1)
export interface ReportingRestrictionData {
note: string | null
// reporting-suspended only
suspendedUntil?: string
}
interface NotificationBase {
id: string
createdAt: string
}
export type AppNotification =
| (NotificationBase & {
// comment-rejected: the community rejected it; comment-removed: it was
// deleted, still awaiting review, because its entity stopped being public or
// stopped taking comments, or its pack stopped taking comments
type: 'comment-rejected' | 'comment-removed'
data: CommentNotificationData
})
| (NotificationBase & {
type: 'automation-email-skipped'
data: AutomationEmailSkippedData
})
| (NotificationBase & {
type: 'pack-version-published'
data: PackVersionPublishedData
})
| (NotificationBase & {
type: 'moderation-decision'
data: ModerationDecisionData
})
| (NotificationBase & {
type: 'report-decided'
data: ReportDecidedData
})
| (NotificationBase & {
type: 'appeal-decided'
data: AppealDecidedData
})
| (NotificationBase & {
type: 'reporting-warning'
data: ReportingRestrictionData
})
| (NotificationBase & {
type: 'reporting-suspended'
data: ReportingRestrictionData
})
/**
* The notifications inbox — same delivery model as Direct Messages (see
* stores/messages.ts): REST is the source of truth, useMessagesSocket.ts
* merges live pushes in, and the unread badge is re-synced from the server
* rather than counted client-side.
*/
export const useNotificationsStore = defineStore('notifications', () => {
const notifications = ref<AppNotification[]>([])
const loading = ref(false)
const nextToken = ref<string | null>(null)
// When the user last opened the inbox, as of the last fetch: what came
// after it is shown as new.
const lastReadAt = ref<string | null>(null)
const unreadTotal = ref(0)
// Set by NotificationsPage while it is mounted: a notification arriving
// then is seen at once, so it is marked read instead of counted.
const inboxOpen = ref(false)
async function fetchNotifications(reset = true) {
if (reset) {
notifications.value = []
nextToken.value = null
}
loading.value = true
try {
const params: Record<string, string> = { limit: '20' }
if (!reset && nextToken.value) params.nextToken = nextToken.value
const response = await api.get('/v1/notifications', { params })
const data = response.data
notifications.value = reset
? data.notifications
: [...notifications.value, ...data.notifications]
nextToken.value = data.nextToken
if (reset) lastReadAt.value = data.lastReadAt
} finally {
loading.value = false
}
}
async function loadMore() {
if (loading.value || !nextToken.value) return
await fetchNotifications(false)
}
async function markAllRead() {
await api.post('/v1/notifications/read')
unreadTotal.value = 0
}
async function fetchUnreadCount() {
const response = await api.get('/v1/notifications/unread-count')
unreadTotal.value = response.data.count
}
function isNew(notification: AppNotification): boolean {
return !lastReadAt.value || notification.createdAt > lastReadAt.value
}
/**
* Handles a WebSocket-pushed notification — called by useMessagesSocket.ts.
* With the inbox open, it is shown at the top and marked read; otherwise
* the badge is re-synced from the server rather than counted client-side.
* A reconnect can replay a push: a known id is not shown twice.
*/
async function receivePushedNotification(notification: AppNotification) {
if (!inboxOpen.value) {
await fetchUnreadCount()
return
}
if (!notifications.value.some((n) => n.id === notification.id)) {
notifications.value = [notification, ...notifications.value]
}
await markAllRead()
}
return {
notifications,
loading,
nextToken,
lastReadAt,
unreadTotal,
inboxOpen,
fetchNotifications,
loadMore,
markAllRead,
fetchUnreadCount,
isNew,
receivePushedNotification
}
})
|