Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions apps/mail/actions/mail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,44 @@ export const modifyLabels = async ({
throw error;
}
};

export const toggleStar = async ({ ids }: { ids: string[] }) => {
try {
const driver = await getActiveDriver();
const { threadIds } = driver.normalizeIds(ids);

if (!threadIds.length) {
return { success: false, error: 'No thread IDs provided' };
}

const threadResults = await Promise.allSettled(
threadIds.map(id => driver.get(id))
);

let allStarred = true;
let anyValid = false;

for (const result of threadResults) {
if (result.status === 'fulfilled' && result.value?.[0]) {
anyValid = true;
if (!result.value[0].tags?.includes('STARRED')) {
allStarred = false;
break;
}
}
}

const shouldStar = !anyValid || !allStarred;

await driver.modifyLabels(threadIds, {
addLabels: shouldStar ? ['STARRED'] : [],
removeLabels: shouldStar ? [] : ['STARRED'],
});

return { success: true };
} catch (error) {
if (FatalErrors.includes((error as Error).message)) await deleteActiveConnection();
console.error('Error toggling star:', error);
throw error;
}
};
38 changes: 28 additions & 10 deletions apps/mail/app/api/driver/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ export const driver = async (config: IConfig): Promise<MailManager> => {
return { ...res.data, threads } as any;
},
get: async (id: string): Promise<ParsedMessage[]> => {
console.log(id);
console.log('Fetching thread:', id);
const res = await gmail.users.threads.get({ userId: 'me', id, format: 'full' });
if (!res.data.messages) return [];

Expand Down Expand Up @@ -459,15 +459,33 @@ export const driver = async (config: IConfig): Promise<MailManager> => {
);
return { threadIds };
},
async modifyLabels(id: string[], options: { addLabels: string[]; removeLabels: string[] }) {
await gmail.users.messages.batchModify({
userId: 'me',
requestBody: {
ids: id,
addLabelIds: options.addLabels,
removeLabelIds: options.removeLabels,
},
});
async modifyLabels(threadIds: string[], options: { addLabels: string[]; removeLabels: string[] }) {
const threadResults = await Promise.allSettled(
threadIds.map(threadId =>
gmail.users.threads.get({
userId: 'me',
id: threadId,
format: 'minimal'
})
)
);

const messageIds = threadResults
.filter((result): result is PromiseFulfilledResult<any> => result.status === 'fulfilled')
.flatMap(result => result.value.data.messages || [])
.map(msg => msg.id)
.filter((id): id is string => !!id);

if (messageIds.length > 0) {
await gmail.users.messages.batchModify({
userId: 'me',
requestBody: {
ids: messageIds,
addLabelIds: options.addLabels,
removeLabelIds: options.removeLabels,
},
});
}
},
getDraft: async (draftId: string) => {
try {
Expand Down
Loading