Skip to content

Commit f0e19f5

Browse files
committed
small cleanups for google importer
1 parent 0f6c405 commit f0e19f5

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

typescript/packages/common-cli/gmail.tsx

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { h } from "@commontools/html";
2-
import { recipe, handler, UI, NAME, cell, derive } from "@commontools/builder";
2+
import { cell, derive, handler, NAME, recipe, UI } from "@commontools/builder";
33
import { z } from "zod";
44

55
const Email = z.object({
@@ -28,7 +28,9 @@ type Auth = z.infer<typeof Auth>;
2828
const Recipe = z
2929
.object({
3030
settings: z.object({
31-
labels: z.string().default("INBOX").describe("comma separated list of labels"),
31+
labels: z.string().default("INBOX").describe(
32+
"comma separated list of labels",
33+
),
3234
limit: z.number().default(10).describe("number of emails to import"),
3335
}),
3436
})
@@ -91,7 +93,9 @@ const googleUpdater = handler<
9193
}
9294

9395
// Get the set of existing email IDs for efficient lookup
94-
const existingEmailIds = new Set((state.emails || []).map((email) => email.id));
96+
const existingEmailIds = new Set(
97+
(state.emails || []).map((email) => email.id),
98+
);
9599

96100
console.log("existing email ids", existingEmailIds);
97101

@@ -104,7 +108,9 @@ const googleUpdater = handler<
104108

105109
fetchEmail(state.auth.token, state.settings.limit, labels).then((emails) => {
106110
// Filter out any duplicates by ID
107-
const newEmails = emails.messages.filter((email) => !existingEmailIds.has(email.id));
111+
const newEmails = emails.messages.filter((email) =>
112+
!existingEmailIds.has(email.id)
113+
);
108114

109115
if (newEmails.length > 0) {
110116
console.log(`Adding ${newEmails.length} new emails`);
@@ -134,7 +140,9 @@ function extractEmailAddress(header: string): string {
134140

135141
// Helper function to extract header value from message headers
136142
function getHeader(headers: any[], name: string): string {
137-
const header = headers.find((h) => h.name.toLowerCase() === name.toLowerCase());
143+
const header = headers.find((h) =>
144+
h.name.toLowerCase() === name.toLowerCase()
145+
);
138146
return header ? header.value : "";
139147
}
140148

@@ -145,9 +153,11 @@ export async function fetchEmail(
145153
) {
146154
// First, get the list of message IDs from the inbox
147155
const listResponse = await fetch(
148-
`https://gmail.googleapis.com/gmail/v1/users/me/messages?labelIds=${labelIds.join(
149-
",",
150-
)}&maxResults=${maxResults}`,
156+
`https://gmail.googleapis.com/gmail/v1/users/me/messages?labelIds=${
157+
labelIds.join(
158+
",",
159+
)
160+
}&maxResults=${maxResults}`,
151161
{
152162
headers: {
153163
Authorization: `Bearer ${accessToken}`,
@@ -184,7 +194,9 @@ export async function fetchEmail(
184194

185195
// Extract plain text content if available
186196
let plainText = "";
187-
if (messageData.payload.parts && Array.isArray(messageData.payload.parts)) {
197+
if (
198+
messageData.payload.parts && Array.isArray(messageData.payload.parts)
199+
) {
188200
const textPart = messageData.payload.parts.find(
189201
(part: any) => part.mimeType === "text/plain",
190202
);
@@ -270,7 +282,12 @@ export default recipe(Recipe, ResultSchema, ({ settings }) => {
270282
<tr>
271283
<td>&nbsp;{email.date}&nbsp;</td>
272284
<td>&nbsp;{email.subject}&nbsp;</td>
273-
<td>&nbsp;{derive(email, (email) => email.labelIds.join(", "))}&nbsp;</td>
285+
<td>
286+
&nbsp;{derive(
287+
email,
288+
(email) => email.labelIds.join(", "),
289+
)}&nbsp;
290+
</td>
274291
</tr>
275292
))}
276293
</tbody>

typescript/packages/common-cli/google-importer.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function updateOnce(charm: Cell<Charm>) {
7979

8080
const { token, expiresAt } = auth.get();
8181

82-
if (token && expiresAt && expiresAt < Date.now()) {
82+
if (token && expiresAt && Date.now() > expiresAt) {
8383
refreshAuthToken(auth, charm);
8484
} else if (token) {
8585
log(charm, "calling googleUpdater in charm");
@@ -114,45 +114,45 @@ async function refreshAuthToken(auth: Cell<any>, charm: Cell<Charm>) {
114114
log(charm, "refreshed token");
115115
}
116116

117-
const notGoogleUpdaterCharm = async (charmId: string): Promise<boolean> => {
117+
const isGoogleUpdaterCharm = async (charmId: string): Promise<boolean> => {
118118
const charm = await manager.get(charmId, false);
119119
if (!charm) {
120120
log(charmId, "charm not found");
121121
return true;
122122
}
123123
const googleUpdater = charm.key("googleUpdater");
124124
const auth = charm.key("auth");
125-
return !(isStream(googleUpdater) && auth);
125+
return !!(isStream(googleUpdater) && auth);
126126
};
127127

128128
/**
129129
* Sets up watching for a charm and schedules periodic updates
130130
*/
131-
function ignoreCharm(charmId: string): Promise<boolean> {
131+
function isIgnoredCharm(charmId: string): Promise<boolean> {
132132
if (checkedCharms.has(charmId)) {
133133
return Promise.resolve(true);
134134
}
135135
checkedCharms.set(charmId, true);
136136

137-
return notGoogleUpdaterCharm(charmId);
137+
return isGoogleUpdaterCharm(charmId);
138138
}
139139

140140
async function watchCharm(charmId: string | undefined) {
141-
if (!charmId || (await ignoreCharm(charmId))) {
141+
if (!charmId || (await isIgnoredCharm(charmId))) {
142142
return;
143143
}
144-
const charm = await manager.get(charmId, true);
145-
if (!charm) {
144+
const runningCharm = await manager.get(charmId, true);
145+
if (!runningCharm) {
146146
log(charmId, "charm not found");
147147
return;
148148
}
149149

150150
// Initial update
151-
updateOnce(charm);
151+
updateOnce(runningCharm);
152152

153153
// Schedule periodic updates
154154
setInterval(() => {
155-
updateOnce(charm);
155+
updateOnce(runningCharm);
156156
}, CHECK_INTERVAL);
157157
}
158158

0 commit comments

Comments
 (0)