Explorar el Código

create middleware

Shun Miyazawa hace 4 años
padre
commit
cad6e28596
Se han modificado 2 ficheros con 20 adiciones y 2 borrados
  1. 18 0
      packages/app/src/stores/middlewares/user.ts
  2. 2 2
      packages/app/src/stores/page.tsx

+ 18 - 0
packages/app/src/stores/middlewares/user.ts

@@ -0,0 +1,18 @@
+import { apiv3Put } from '~/client/util/apiv3-client';
+
+export const checkAndUpdateImageUrlCached = (useSWRNext) => {
+  return (key, fetcher, config) => {
+    const swrNext = useSWRNext(key, fetcher, config);
+    if (swrNext.data != null) {
+      const likerIds = swrNext.data?.likerIds != null ? swrNext.data.likerIds : [];
+      const seenUserIds = swrNext.data?.seenUserIds != null ? swrNext.data.seenUserIds : [];
+      const distinctUserIds = Array.from(new Set([...likerIds, ...seenUserIds]));
+
+      if (distinctUserIds.length > 0) {
+        apiv3Put('/users/update.imageUrlCache', { userIds: distinctUserIds });
+      }
+    }
+    return swrNext;
+
+  };
+};

+ 2 - 2
packages/app/src/stores/page.tsx

@@ -1,6 +1,5 @@
 import useSWR, { SWRResponse } from 'swr';
 
-import { Types } from 'mongoose';
 import { apiv3Get } from '~/client/util/apiv3-client';
 import { HasObjectId } from '~/interfaces/has-object-id';
 
@@ -8,6 +7,7 @@ import { IPage, IPageInfo } from '~/interfaces/page';
 import { IPagingResult } from '~/interfaces/paging-result';
 
 import { useIsGuestUser } from './context';
+import { checkAndUpdateImageUrlCached } from './middlewares/user';
 
 
 export const useSWRxPageByPath = (path: string, initialData?: IPage): SWRResponse<IPage & HasObjectId, Error> => {
@@ -63,11 +63,11 @@ export const useSWRxSubscriptionStatus = <Data, Error>(pageId: string): SWRRespo
   );
 };
 
-// TODO: 85860
 // eslint-disable-next-line @typescript-eslint/no-unused-vars
 export const useSWRxPageInfo = <Data, Error>(pageId: string): SWRResponse<IPageInfo, Error> => {
   return useSWR(
     ['/page/info', pageId],
     (endpoint, pageId) => apiv3Get(endpoint, { pageId }).then(response => response.data),
+    { use: [checkAndUpdateImageUrlCached] },
   );
 };