MCPcopy Index your code
hub / github.com/Linen-dev/linen.dev / handleInbound

Function handleInbound

packages/integration-email/src/inbound.ts:43–122  ·  view source on GitHub ↗
(reqBody: any)

Source from the content-addressed store, hash-verified

41}
42
43async function handleInbound(reqBody: any) {
44 const spam = reqBody.headerLines.find(
45 (h: any) => h.key === 'x-ses-spam-verdict'
46 );
47 if (spam && spam.line.indexOf('FAIL') > -1) {
48 // skip spam
49 return;
50 }
51 const virus = reqBody.headerLines.find(
52 (h: any) => h.key === 'x-ses-virus-verdict'
53 );
54 if (virus && virus.line.indexOf('FAIL') > -1) {
55 // skip virus
56 return;
57 }
58
59 const forwarded = reqBody.headerLines.find(
60 (h: any) => h.key === 'x-forwarded-to'
61 );
62
63 const destination: string[] = [];
64 if (forwarded && forwarded.line) {
65 const emails = extractEmail(forwarded.line);
66 if (emails) {
67 destination.push(...emails);
68 }
69 }
70 const externalId = reqBody.messageId;
71 const to = extractEmail(reqBody.to.text);
72 if (to) {
73 destination.push(...to);
74 }
75 const from = reqBody.from.text;
76 const body = cleanUpQuotedEmail(reqBody.text || reqBody.html); // TODO: parse html to text
77 const title = reqBody.subject;
78
79 // "to" must match a channel integration
80 const channel = await findChannel(destination);
81 if (!channel) {
82 console.warn('channel not found');
83 return;
84 }
85
86 // "from" must find or create a new user
87 const user = await linenSdk.findOrCreateUser({
88 accountsId: channel.accountId,
89 displayName: from,
90 externalUserId: from,
91 });
92
93 // "inReplyTo" must find or create a thread
94 const message =
95 !!reqBody.inReplyTo &&
96 (await linenSdk.findMessage({
97 externalMessageId: reqBody.inReplyTo,
98 channelId: channel.id,
99 }));
100 // if message exists, we create a new message

Callers 1

inbound.tsFile · 0.85

Calls 9

extractEmailFunction · 0.90
cleanUpQuotedEmailFunction · 0.90
findChannelFunction · 0.85
findOrCreateUserMethod · 0.80
findMessageMethod · 0.80
createNewMessageMethod · 0.80
createNewThreadMethod · 0.80
warnMethod · 0.65
findMethod · 0.45

Tested by

no test coverage detected