refactor coordinators

This commit is contained in:
pa
2026-03-10 11:54:09 +09:00
parent 648fcde085
commit 2fffadfbcf
16 changed files with 1068 additions and 1257 deletions
@@ -1,43 +1,48 @@
/**
* @param {object} deps Coordinator dependencies.
* @returns {object} Friend relationship coordinator methods.
*/
export function createFriendRelationshipCoordinator(deps) {
const {
friendLog,
friendLogTable,
getCurrentUserId,
requestFriendStatus,
handleFriendStatus,
addFriendship,
deleteFriend,
database,
notificationStore,
sharedFeedStore,
favoriteStore,
uiStore,
shouldNotifyUnfriend,
nowIso
} = deps;
import { database } from '../service/database';
import { friendRequest } from '../api';
import { useAppearanceSettingsStore } from '../stores/settings/appearance';
import { useFavoriteStore } from '../stores/favorite';
import { useFriendStore } from '../stores/friend';
import { useNotificationStore } from '../stores/notification';
import { useSharedFeedStore } from '../stores/sharedFeed';
import { useUiStore } from '../stores/ui';
import { useUserStore } from '../stores/user';
/**
* Validates and applies unfriend transition side effects.
* @param {string} id User id.
*/
function runDeleteFriendshipFlow(id) {
const ctx = friendLog.get(id);
if (typeof ctx === 'undefined') {
return;
}
requestFriendStatus({
/**
* Validates and applies unfriend transition side effects.
* @param {string} id User id.
* @param {object} [options] Test seams.
* @param {function} [options.nowIso] ISO timestamp provider.
*/
export function runDeleteFriendshipFlow(
id,
{ nowIso = () => new Date().toJSON() } = {}
) {
const friendStore = useFriendStore();
const userStore = useUserStore();
const notificationStore = useNotificationStore();
const sharedFeedStore = useSharedFeedStore();
const favoriteStore = useFavoriteStore();
const uiStore = useUiStore();
const appearanceSettingsStore = useAppearanceSettingsStore();
const { friendLog, friendLogTable } = friendStore;
const ctx = friendLog.get(id);
if (typeof ctx === 'undefined') {
return;
}
friendRequest
.getFriendStatus({
userId: id,
currentUserId: getCurrentUserId()
}).then((args) => {
if (args.params.currentUserId !== getCurrentUserId()) {
currentUserId: userStore.currentUser.id
})
.then((args) => {
if (args.params.currentUserId !== userStore.currentUser.id) {
// safety check for delayed response
return;
}
handleFriendStatus(args);
friendStore.handleFriendStatus(args);
if (!args.json.isFriend && friendLog.has(id)) {
const friendLogHistory = {
created_at: nowIso(),
@@ -45,44 +50,47 @@ export function createFriendRelationshipCoordinator(deps) {
userId: id,
displayName: ctx.displayName || id
};
friendLogTable.value.data.push(friendLogHistory);
friendLogTable.data.push(friendLogHistory);
database.addFriendLogHistory(friendLogHistory);
notificationStore.queueFriendLogNoty(friendLogHistory);
sharedFeedStore.addEntry(friendLogHistory);
friendLog.delete(id);
database.deleteFriendLogCurrent(id);
favoriteStore.handleFavoriteDelete(id);
if (shouldNotifyUnfriend()) {
if (!appearanceSettingsStore.hideUnfriends) {
uiStore.notifyMenu('friend-log');
}
deleteFriend(id);
friendStore.deleteFriend(id);
}
});
}
/**
* Reconciles current friend list against local friend log.
* @param {object} ref Current user reference.
*/
function runUpdateFriendshipsFlow(ref) {
let id;
const set = new Set();
for (id of ref.friends) {
set.add(id);
addFriendship(id);
}
for (id of friendLog.keys()) {
if (id === getCurrentUserId()) {
friendLog.delete(id);
database.deleteFriendLogCurrent(id);
} else if (!set.has(id)) {
runDeleteFriendshipFlow(id);
}
}
}
return {
runDeleteFriendshipFlow,
runUpdateFriendshipsFlow
};
}
/**
* Reconciles current friend list against local friend log.
* @param {object} ref Current user reference.
* @param {object} [options] Test seams.
* @param {function} [options.nowIso] ISO timestamp provider.
*/
export function runUpdateFriendshipsFlow(
ref,
{ nowIso = () => new Date().toJSON() } = {}
) {
const friendStore = useFriendStore();
const userStore = useUserStore();
const { friendLog } = friendStore;
let id;
const set = new Set();
for (id of ref.friends) {
set.add(id);
friendStore.addFriendship(id);
}
for (id of friendLog.keys()) {
if (id === userStore.currentUser.id) {
friendLog.delete(id);
database.deleteFriendLogCurrent(id);
} else if (!set.has(id)) {
runDeleteFriendshipFlow(id, { nowIso });
}
}
}