refactor store

This commit is contained in:
pa
2026-03-06 22:42:43 +09:00
parent e665b3815d
commit 8ddedb2d2d
29 changed files with 3269 additions and 888 deletions
+125 -84
View File
@@ -7,14 +7,18 @@ import Noty from 'noty';
import dayjs from 'dayjs';
import {
applyBoopLegacyHandling,
checkCanInvite,
createDefaultNotificationRef,
createDefaultNotificationV2Ref,
escapeTag,
executeWithBackoff,
findUserByDisplayName,
getUserMemo,
parseLocation,
parseNotificationDetails,
removeFromArray,
replaceBioSymbols
sanitizeNotificationJson
} from '../../shared/utils';
import {
friendRequest,
@@ -163,6 +167,9 @@ export const useNotificationStore = defineStore('Notification', () => {
{ flush: 'sync' }
);
/**
*
*/
async function init() {
notificationTable.value.filters[0].value = JSON.parse(
await configRepository.getString(
@@ -174,6 +181,10 @@ export const useNotificationStore = defineStore('Notification', () => {
init();
/**
*
* @param args
*/
function handleNotification(args) {
args.ref = applyNotification(args.json);
const { ref } = args;
@@ -234,6 +245,10 @@ export const useNotificationStore = defineStore('Notification', () => {
D.incomingRequest = true;
}
/**
*
* @param notificationId
*/
function handleNotificationHide(notificationId) {
const ref = notificationTable.value.data.find(
(n) => n.id === notificationId
@@ -259,6 +274,10 @@ export const useNotificationStore = defineStore('Notification', () => {
});
}
/**
*
* @param args
*/
function handlePipelineNotification(args) {
const ref = args.json;
if (
@@ -373,6 +392,10 @@ export const useNotificationStore = defineStore('Notification', () => {
});
}
/**
*
* @param notificationId
*/
function handleNotificationSee(notificationId) {
removeFromArray(unseenNotifications.value, notificationId);
if (unseenNotifications.value.length === 0) {
@@ -392,6 +415,9 @@ export const useNotificationStore = defineStore('Notification', () => {
let seeProcessing = false;
const SEE_CONCURRENCY = 2;
/**
*
*/
async function processSeeQueue() {
if (seeProcessing) return;
seeProcessing = true;
@@ -443,7 +469,7 @@ export const useNotificationStore = defineStore('Notification', () => {
/**
* Queue a notification to be marked as seen.
* @param {string} notificationId
* @param {number} [version=1]
* @param {number} [version]
*/
function queueMarkAsSeen(notificationId, version = 1) {
if (seenIds.has(notificationId)) return;
@@ -452,6 +478,10 @@ export const useNotificationStore = defineStore('Notification', () => {
processSeeQueue();
}
/**
*
* @param args
*/
function handleNotificationAccept(args) {
let ref;
const array = notificationTable.value.data;
@@ -490,6 +520,10 @@ export const useNotificationStore = defineStore('Notification', () => {
D.isFriend = true;
}
/**
*
* @param args
*/
function handleNotificationExpire(args) {
const { ref } = args;
const D = userStore.userDialog;
@@ -509,10 +543,7 @@ export const useNotificationStore = defineStore('Notification', () => {
* @returns {object}
*/
function applyNotification(data) {
const json = { ...data };
if (json.message) {
json.message = replaceBioSymbols(json.message);
}
const json = sanitizeNotificationJson({ ...data });
let ref;
const array = notificationTable.value.data;
for (let i = array.length - 1; i >= 0; i--) {
@@ -521,102 +552,37 @@ export const useNotificationStore = defineStore('Notification', () => {
break;
}
}
// delete any null in json
for (const key in json) {
if (json[key] === null) {
delete json[key];
}
}
if (typeof ref === 'undefined') {
ref = {
id: '',
senderUserId: '',
senderUsername: '',
type: '',
message: '',
details: {},
seen: false,
created_at: '',
// VRCX
$isExpired: false,
//
...json
};
ref = createDefaultNotificationRef(json);
} else {
Object.assign(ref, json);
ref.$isExpired = false;
}
if (ref.details !== Object(ref.details)) {
let details = {};
if (ref.details !== '{}') {
try {
const object = JSON.parse(ref.details);
if (object === Object(object)) {
details = object;
}
} catch (err) {
console.log(err);
}
}
ref.details = details;
}
ref.details = parseNotificationDetails(ref.details);
return ref;
}
/**
*
* @param data
*/
function applyNotificationV2(data) {
const json = { ...data };
// delete any null in json
for (const key in json) {
if (json[key] === null || typeof json[key] === 'undefined') {
delete json[key];
}
}
if (json.message) {
json.message = replaceBioSymbols(json.message);
}
if (json.title) {
json.title = replaceBioSymbols(json.title);
}
const json = sanitizeNotificationJson({ ...data });
let ref = notificationTable.value.data.find((n) => n.id === json.id);
if (typeof ref === 'undefined') {
ref = {
id: '',
createdAt: '',
updatedAt: '',
expiresAt: '',
type: '',
link: '',
linkText: '',
message: '',
title: '',
imageUrl: '',
seen: false,
senderUserId: '',
senderUsername: '',
data: {},
responses: [],
details: {},
version: 2,
...json
};
ref = createDefaultNotificationV2Ref(json);
} else {
Object.assign(ref, json);
}
ref.created_at = ref.createdAt; // for table
// legacy handling of boops
if (ref.type === 'boop' && ref.title) {
ref.message = ref.title;
ref.title = '';
if (ref.details?.emojiId?.startsWith('default_')) {
ref.imageUrl = ref.details.emojiId;
ref.message += ` ${ref.details.emojiId.replace('default_', '')}`;
} else {
ref.imageUrl = `${AppDebug.endpointDomain}/file/${ref.details.emojiId}/${ref.details.emojiVersion}`;
}
}
applyBoopLegacyHandling(ref, AppDebug.endpointDomain);
return ref;
}
/**
*
* @param args
*/
function handleNotificationV2(args) {
const ref = applyNotificationV2(args.json);
if (ref.seen) {
@@ -645,6 +611,10 @@ export const useNotificationStore = defineStore('Notification', () => {
sharedFeedStore.addEntry(ref);
}
/**
*
* @param args
*/
function handleNotificationV2Update(args) {
const notificationId = args.params.notificationId;
const json = { ...args.json };
@@ -663,6 +633,10 @@ export const useNotificationStore = defineStore('Notification', () => {
}
}
/**
*
* @param notificationId
*/
function handleNotificationV2Hide(notificationId) {
database.expireNotificationV2(notificationId);
const ref = notificationTable.value.data.find(
@@ -674,6 +648,9 @@ export const useNotificationStore = defineStore('Notification', () => {
}
}
/**
*
*/
function expireFriendRequestNotifications() {
const array = notificationTable.value.data;
for (let i = array.length - 1; i >= 0; i--) {
@@ -813,6 +790,10 @@ export const useNotificationStore = defineStore('Notification', () => {
}
}
/**
*
* @param noty
*/
function playNoty(noty) {
if (
userStore.currentUser.status === 'busy' ||
@@ -1014,6 +995,10 @@ export const useNotificationStore = defineStore('Notification', () => {
return '';
}
/**
*
* @param gamelog
*/
function queueGameLogNoty(gamelog) {
const noty = structuredClone(gamelog);
let bias;
@@ -1090,6 +1075,10 @@ export const useNotificationStore = defineStore('Notification', () => {
}
}
/**
*
* @param feed
*/
function queueFeedNoty(feed) {
const noty = { ...feed };
if (noty.type === 'Avatar') {
@@ -1116,6 +1105,10 @@ export const useNotificationStore = defineStore('Notification', () => {
}
}
/**
*
* @param noty
*/
function queueFriendLogNoty(noty) {
if (noty.type === 'FriendRequest') {
return;
@@ -1133,6 +1126,10 @@ export const useNotificationStore = defineStore('Notification', () => {
}
}
/**
*
* @param noty
*/
function queueModerationNoty(noty) {
noty.isFriend = false;
noty.isFavorite = false;
@@ -1146,6 +1143,9 @@ export const useNotificationStore = defineStore('Notification', () => {
}
}
/**
*
*/
async function initNotifications() {
notificationInitStatus.value = false;
let tableData = await database.getNotificationsV2();
@@ -1161,6 +1161,9 @@ export const useNotificationStore = defineStore('Notification', () => {
refreshNotifications();
}
/**
*
*/
function testNotification() {
playNoty({
type: 'Event',
@@ -1169,6 +1172,10 @@ export const useNotificationStore = defineStore('Notification', () => {
});
}
/**
*
* @param row
*/
function acceptFriendRequestNotification(row) {
modalStore
.confirm({
@@ -1193,6 +1200,10 @@ export const useNotificationStore = defineStore('Notification', () => {
.catch(() => {});
}
/**
*
* @param row
*/
async function hideNotification(row) {
if (row.type === 'ignoredFriendRequest') {
await friendRequest.deleteHiddenFriendRequest(
@@ -1211,6 +1222,10 @@ export const useNotificationStore = defineStore('Notification', () => {
}
}
/**
*
* @param row
*/
function hideNotificationPrompt(row) {
modalStore
.confirm({
@@ -1223,6 +1238,10 @@ export const useNotificationStore = defineStore('Notification', () => {
.catch(() => {});
}
/**
*
* @param row
*/
function acceptRequestInvite(row) {
modalStore
.confirm({
@@ -1268,6 +1287,12 @@ export const useNotificationStore = defineStore('Notification', () => {
.catch(() => {});
}
/**
*
* @param notificationId
* @param responses
* @param responseType
*/
function sendNotificationResponse(notificationId, responses, responseType) {
if (!Array.isArray(responses) || responses.length === 0) return;
let responseData = '';
@@ -1295,6 +1320,10 @@ export const useNotificationStore = defineStore('Notification', () => {
});
}
/**
*
* @param row
*/
function deleteNotificationLog(row) {
const idx = notificationTable.value.data.findIndex(
(e) => e.id === row.id
@@ -1314,6 +1343,10 @@ export const useNotificationStore = defineStore('Notification', () => {
}
}
/**
*
* @param row
*/
function deleteNotificationLogPrompt(row) {
modalStore
.confirm({
@@ -1327,6 +1360,10 @@ export const useNotificationStore = defineStore('Notification', () => {
.catch(() => {});
}
/**
*
* @param notification
*/
function isNotificationExpired(notification) {
if (notification.$isExpired !== undefined) {
return notification.$isExpired;
@@ -1338,6 +1375,10 @@ export const useNotificationStore = defineStore('Notification', () => {
return expiresAt.isValid() && dayjs().isSameOrAfter(expiresAt);
}
/**
*
* @param link
*/
function openNotificationLink(link) {
if (!link) {
return;