Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix: consistent unauthed behavior on public-link surfaces (#5133)

gbirmane72a00afc1d6
diff --git a/apps/web/src/features/block-chat/definition.ts b/apps/web/src/features/block-chat/definition.ts
index db2597d5e0..1520a20471 100644
--- a/apps/web/src/features/block-chat/definition.ts
+++ b/apps/web/src/features/block-chat/definition.ts
@@ -1,9 +1,14 @@
-import { defineBlock, type ExtractLoadType, LoadErrors } from '@core/block';
+import {
+ defineBlock,
+ type ExtractLoadType,
+ LoadErrors,
+ loadResult,
+} from '@core/block';
import { Model } from '@core/component/AI/constant/model';
-import { cognitionApiServiceClient } from '@service-cognition/client';
+import { fetchAndCacheChat } from '@queries/cognition/chat-data';
import type { Entity } from '@service-cognition/generated/schemas/entity';
import type { DocumentMetadata } from '@service-storage/generated/schemas/documentMetadata';
-import { ok } from 'neverthrow';
+import { err, ok } from 'neverthrow';
import BlockChat from './component/Block';
export const DEFAULT_CHAT_NAME = 'New Chat';
@@ -20,13 +25,8 @@ export const definition = defineBlock({
if (source.type === 'dss') {
// Fetch the chat from dcs
const chatId = source.id;
- const res = await cognitionApiServiceClient.getChat({ chat_id: chatId });
- if (
- res.isErr() &&
- res.error.some((error) => error.code === 'UNAUTHORIZED')
- )
- return LoadErrors.INVALID;
- if (res.isErr()) return LoadErrors.MISSING;
+ const res = await loadResult(fetchAndCacheChat(chatId));
+ if (res.isErr()) return err(res.error);
const chat = res.value;
if (intent === 'preload') {
diff --git a/apps/web/src/features/property/side-panel/properties/EntityPropertiesSection.tsx b/apps/web/src/features/property/side-panel/properties/EntityPropertiesSection.tsx
index fc68d8dcb3..796f41eb72 100644
--- a/apps/web/src/features/property/side-panel/properties/EntityPropertiesSection.tsx
+++ b/apps/web/src/features/property/side-panel/properties/EntityPropertiesSection.tsx
@@ -1,4 +1,5 @@
import { SidePanel } from '@components/app/side-panel/SidePanel';
+import { useIsAuthenticated } from '@core/auth';
import type { BlockAlias, BlockName } from '@core/block';
import { PopupPreview } from '@core/component/DocumentPreview';
import { HoverCard } from '@core/component/HoverCard';
@@ -84,12 +85,13 @@ const TAGGABLE_ENTITY_TYPES: ReadonlySet<EntityType> = new Set<EntityType>([
export function EntityTagsSection(props: EntityTagsSectionProps) {
const tagsQuery = useTagsQuery();
+ const isAuthenticated = useIsAuthenticated();
return (
<Show when={TAGGABLE_ENTITY_TYPES.has(props.entityType)}>
<SidePanel.Section id="tags" title="Tags" defaultOpen order={props.order}>
<Show
- when={!tagsQuery.isError}
+ when={isAuthenticated() !== false && !tagsQuery.isError}
fallback={
<span class="text-xs text-ink-extra-muted">Tags unavailable</span>
}
diff --git a/apps/web/src/lib/queries/channel/channel-messages.ts b/apps/web/src/lib/queries/channel/channel-messages.ts
index 0d430ca277..523100301e 100644
--- a/apps/web/src/lib/queries/channel/channel-messages.ts
+++ b/apps/web/src/lib/queries/channel/channel-messages.ts
@@ -1,4 +1,8 @@
-import { ThrownResultError, throwOnErr } from '@core/util/result';
+import {
+ ThrownResultError,
+ thrownResultErrorHasCode,
+ throwOnErr,
+} from '@core/util/result';
import {
type ApiChannelMessage,
type ApiResolvedChannelMessage,
@@ -128,6 +132,12 @@ export function channelMessagesQueryOptions(
if (loadAroundMessageId && isMissingChannelMessageError(error)) {
return false;
}
+ if (
+ thrownResultErrorHasCode(error, 'UNAUTHORIZED') ||
+ thrownResultErrorHasCode(error, 'FORBIDDEN')
+ ) {
+ return false;
+ }
return failureCount < 1;
},
};
diff --git a/apps/web/src/lib/queries/cognition/chat-data.ts b/apps/web/src/lib/queries/cognition/chat-data.ts
index f38c360df4..8130d9fc7a 100644
--- a/apps/web/src/lib/queries/cognition/chat-data.ts
+++ b/apps/web/src/lib/queries/cognition/chat-data.ts
@@ -1,26 +1,43 @@
+import { catchToResult, throwOnErr } from '@core/util/result';
import { cognitionApiServiceClient } from '@service-cognition/client';
-import type { ChatResponse } from '@service-cognition/generated/schemas';
import { useQuery } from '@tanstack/solid-query';
import type { Accessor } from 'solid-js';
+import { queryClient } from '../client';
import { chatDataQueryKey } from './keys';
const STALE_TIME = 60 * 1000;
const GC_TIME = 10 * 60 * 1000;
-async function fetchChatData(chatId: string): Promise<ChatResponse> {
- const result = await cognitionApiServiceClient.getChat({ chat_id: chatId });
- if (result.isErr()) {
- throw new Error('Failed to fetch chat');
- }
- return result.value.chat;
+function chatQueryOptions(chatId: string) {
+ return {
+ queryKey: chatDataQueryKey(chatId),
+ queryFn: async () =>
+ await throwOnErr(
+ async () => await cognitionApiServiceClient.getChat({ chat_id: chatId })
+ ),
+ staleTime: STALE_TIME,
+ gcTime: GC_TIME,
+ };
+}
+
+/**
+ * Fetch a chat fresh from the service through the query cache, so mounted
+ * chat queries reuse the response instead of refetching.
+ */
+export async function fetchAndCacheChat(chatId: string) {
+ return await catchToResult(
+ async () =>
+ await queryClient.fetchQuery({
+ ...chatQueryOptions(chatId),
+ staleTime: 0,
+ })
+ );
}
export function useChatDataQuery(chatId: Accessor<string>) {
return useQuery(() => ({
- queryKey: chatDataQueryKey(chatId()),
- queryFn: () => fetchChatData(chatId()),
- staleTime: STALE_TIME,
- gcTime: GC_TIME,
+ ...chatQueryOptions(chatId()),
+ select: (data) => data.chat,
enabled: !!chatId(),
}));
}
diff --git a/apps/web/src/lib/queries/properties/tags.ts b/apps/web/src/lib/queries/properties/tags.ts
index 651f7d41e9..276e90164f 100644
--- a/apps/web/src/lib/queries/properties/tags.ts
+++ b/apps/web/src/lib/queries/properties/tags.ts
@@ -23,7 +23,7 @@ export function useTagsQuery() {
queryFn: async () =>
await throwOnErr(async () => await propertiesServiceClient.listTags()),
staleTime: 1000 * 60 * 5,
- enabled: isAuthenticated(),
+ enabled: isAuthenticated() === true,
placeholderData: EMPTY_TAG_SETS,
}));
}