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

View File

@@ -3,20 +3,22 @@ import { defineStore } from 'pinia';
import { toast } from 'vue-sonner';
import { useI18n } from 'vue-i18n';
import {
convertFileUrlToImageUrl,
createDefaultGroupRef,
hasGroupPermission,
replaceBioSymbols,
sanitizeEntityJson
} from '../shared/utils';
import {
groupRequest,
instanceRequest,
userRequest,
worldRequest
} from '../api';
import { patchGroupFromEvent } from '../query';
import {
convertFileUrlToImageUrl,
hasGroupPermission,
replaceBioSymbols
} from '../shared/utils';
import { database } from '../service/database';
import { groupDialogFilterOptions } from '../shared/constants/';
import { patchGroupFromEvent } from '../query';
import { useGameStore } from './game';
import { useInstanceStore } from './instance';
import { useModalStore } from './modal';
@@ -128,6 +130,11 @@ export const useGroupStore = defineStore('Group', () => {
{ flush: 'sync' }
);
/**
*
* @param groupId
* @param options
*/
function showGroupDialog(groupId, options = {}) {
if (!groupId) {
return;
@@ -232,6 +239,11 @@ export const useGroupStore = defineStore('Group', () => {
);
}
/**
*
* @param ref
* @param message
*/
function groupChange(ref, message) {
if (!currentUserGroupsInit.value) {
return;
@@ -260,6 +272,9 @@ export const useGroupStore = defineStore('Group', () => {
workerTimers.setTimeout(saveCurrentUserGroups, 100);
}
/**
*
*/
function saveCurrentUserGroups() {
if (!currentUserGroupsInit.value) {
return;
@@ -284,10 +299,10 @@ export const useGroupStore = defineStore('Group', () => {
/**
*
* @param {object} ref
* @param {array} oldRoles
* @param {array} newRoles
* @param {array} oldRoleIds
* @param {array} newRoleIds
* @param {Array} oldRoles
* @param {Array} newRoles
* @param {Array} oldRoleIds
* @param {Array} newRoleIds
*/
function groupRoleChange(ref, oldRoles, newRoles, oldRoleIds, newRoleIds) {
// check for removed/added roleIds
@@ -401,7 +416,7 @@ export const useGroupStore = defineStore('Group', () => {
/**
*
* @param {{ groupId: string }} params
* @return { Promise<{posts: any, params}> }
* @returns { Promise<{posts: any, params}> }
*/
async function getAllGroupPosts(params) {
const n = 100;
@@ -442,6 +457,10 @@ export const useGroupStore = defineStore('Group', () => {
return returnArgs;
}
/**
*
* @param groupId
*/
function getGroupDialogGroup(groupId) {
const D = groupDialog.value;
D.isGetGroupDialogGroupLoading = false;
@@ -497,32 +516,38 @@ export const useGroupStore = defineStore('Group', () => {
});
}
});
groupRequest.getCachedGroupCalendar(groupId).then((args) => {
if (groupDialog.value.id === args.params.groupId) {
D.calendar = args.json.results;
for (const event of D.calendar) {
applyGroupEvent(event);
// fetch again for isFollowing
groupRequest
.getCachedGroupCalendarEvent({
groupId,
eventId: event.id
})
.then((args) => {
Object.assign(
event,
applyGroupEvent(args.json)
);
});
groupRequest
.getCachedGroupCalendar(groupId)
.then((args) => {
if (groupDialog.value.id === args.params.groupId) {
D.calendar = args.json.results;
for (const event of D.calendar) {
applyGroupEvent(event);
// fetch again for isFollowing
groupRequest
.getCachedGroupCalendarEvent({
groupId,
eventId: event.id
})
.then((args) => {
Object.assign(
event,
applyGroupEvent(args.json)
);
});
}
}
}
});
});
}
nextTick(() => (D.isGetGroupDialogGroupLoading = false));
return args;
});
}
/**
*
* @param event
*/
function applyGroupEvent(event) {
return {
userInterest: {
@@ -536,6 +561,9 @@ export const useGroupStore = defineStore('Group', () => {
};
}
/**
*
*/
async function updateInGameGroupOrder() {
inGameGroupOrder.value = [];
try {
@@ -551,6 +579,11 @@ export const useGroupStore = defineStore('Group', () => {
}
}
/**
*
* @param a
* @param b
*/
function sortGroupInstancesByInGame(a, b) {
const aIndex = inGameGroupOrder.value.indexOf(a?.group?.id);
const bIndex = inGameGroupOrder.value.indexOf(b?.group?.id);
@@ -566,6 +599,10 @@ export const useGroupStore = defineStore('Group', () => {
return aIndex - bIndex;
}
/**
*
* @param groupId
*/
function leaveGroup(groupId) {
groupRequest
.leaveGroup({
@@ -590,6 +627,10 @@ export const useGroupStore = defineStore('Group', () => {
});
}
/**
*
* @param groupId
*/
function leaveGroupPrompt(groupId) {
modalStore
.confirm({
@@ -603,6 +644,9 @@ export const useGroupStore = defineStore('Group', () => {
.catch(() => {});
}
/**
*
*/
function updateGroupPostSearch() {
const D = groupDialog.value;
const search = D.postsSearch.toLowerCase();
@@ -620,6 +664,11 @@ export const useGroupStore = defineStore('Group', () => {
});
}
/**
*
* @param groupId
* @param visibility
*/
function setGroupVisibility(groupId, visibility) {
return groupRequest
.setGroupMemberProps(userStore.currentUser.id, groupId, {
@@ -632,6 +681,11 @@ export const useGroupStore = defineStore('Group', () => {
});
}
/**
*
* @param groupId
* @param subscribe
*/
function setGroupSubscription(groupId, subscribe) {
return groupRequest
.setGroupMemberProps(userStore.currentUser.id, groupId, {
@@ -651,73 +705,9 @@ export const useGroupStore = defineStore('Group', () => {
*/
function applyGroup(json) {
let ref = cachedGroups.get(json.id);
if (json.rules) {
json.rules = replaceBioSymbols(json.rules);
}
if (json.name) {
json.name = replaceBioSymbols(json.name);
}
if (json.description) {
json.description = replaceBioSymbols(json.description);
}
sanitizeEntityJson(json, ['rules', 'name', 'description']);
if (typeof ref === 'undefined') {
ref = {
id: '',
name: '',
shortCode: '',
description: '',
bannerId: '',
bannerUrl: '',
createdAt: '',
discriminator: '',
galleries: [],
iconId: '',
iconUrl: '',
isVerified: false,
joinState: '',
languages: [],
links: [],
memberCount: 0,
memberCountSyncedAt: '',
membershipStatus: '',
onlineMemberCount: 0,
ownerId: '',
privacy: '',
rules: null,
tags: [],
// in group
initialRoleIds: [],
myMember: {
bannedAt: null,
groupId: '',
has2FA: false,
id: '',
isRepresenting: false,
isSubscribedToAnnouncements: false,
joinedAt: '',
managerNotes: '',
membershipStatus: '',
permissions: [],
roleIds: [],
userId: '',
visibility: '',
_created_at: '',
_id: '',
_updated_at: ''
},
updatedAt: '',
// includeRoles: true
roles: [],
// group list
$memberId: '',
groupId: '',
isRepresenting: false,
memberVisibility: false,
mutualGroup: false,
// VRCX
$languages: [],
...json
};
ref = createDefaultGroupRef(json);
cachedGroups.set(ref.id, ref);
} else {
if (currentUserGroups.has(ref.id)) {
@@ -796,6 +786,10 @@ export const useGroupStore = defineStore('Group', () => {
return ref;
}
/**
*
* @param args
*/
function handleGroupRepresented(args) {
const D = userStore.userDialog;
const json = args.json;
@@ -819,6 +813,10 @@ export const useGroupStore = defineStore('Group', () => {
applyGroup(json);
}
/**
*
* @param args
*/
function handleGroupList(args) {
for (const json of args.json) {
json.$memberId = json.id;
@@ -827,6 +825,10 @@ export const useGroupStore = defineStore('Group', () => {
}
}
/**
*
* @param args
*/
function handleGroupMemberProps(args) {
if (args.userId === userStore.currentUser.id) {
const json = args.json;
@@ -873,6 +875,10 @@ export const useGroupStore = defineStore('Group', () => {
}
}
/**
*
* @param args
*/
function handleGroupPermissions(args) {
if (args.params.userId !== userStore.currentUser.id) {
return;
@@ -919,10 +925,18 @@ export const useGroupStore = defineStore('Group', () => {
updateGroupPostSearch();
}
/**
*
* @param args
*/
function handleGroupMember(args) {
args.ref = applyGroupMember(args.json);
}
/**
*
* @param args
*/
async function handleGroupUserInstances(args) {
groupInstances.value = [];
for (const json of args.json.instances) {
@@ -990,6 +1004,10 @@ export const useGroupStore = defineStore('Group', () => {
return json;
}
/**
*
* @param ref
*/
function applyGroupLanguage(ref) {
ref.$languages = [];
const { languages } = ref;
@@ -1008,6 +1026,11 @@ export const useGroupStore = defineStore('Group', () => {
}
}
/**
*
* @param userId
* @param groups
*/
async function loadCurrentUserGroups(userId, groups) {
const savedGroups = JSON.parse(
await configRepository.getString(
@@ -1063,6 +1086,9 @@ export const useGroupStore = defineStore('Group', () => {
getCurrentUserGroups();
}
/**
*
*/
async function getCurrentUserGroups() {
const args = await groupRequest.getGroups({
userId: userStore.currentUser.id
@@ -1082,6 +1108,9 @@ export const useGroupStore = defineStore('Group', () => {
saveCurrentUserGroups();
}
/**
*
*/
function getCurrentUserRepresentedGroup() {
return groupRequest
.getRepresentedGroup({
@@ -1093,6 +1122,9 @@ export const useGroupStore = defineStore('Group', () => {
});
}
/**
*
*/
async function initUserGroups() {
updateInGameGroupOrder();
loadCurrentUserGroups(
@@ -1101,6 +1133,10 @@ export const useGroupStore = defineStore('Group', () => {
);
}
/**
*
* @param userId
*/
function showModerateGroupDialog(userId) {
const D = moderateGroupDialog.value;
D.userId = userId;
@@ -1108,6 +1144,11 @@ export const useGroupStore = defineStore('Group', () => {
D.visible = true;
}
/**
*
* @param groupId
* @param userId
*/
function showGroupMemberModerationDialog(groupId, userId = '') {
const D = groupMemberModeration.value;
D.id = groupId;