Read a channel message and return its file-reference attachments.
( ref: ChannelMessageRef, token: string, )
| 101 | |
| 102 | /** Read a channel message and return its file-reference attachments. */ |
| 103 | async function getMessageFileRefs( |
| 104 | ref: ChannelMessageRef, |
| 105 | token: string, |
| 106 | ): Promise<GraphFileRef[]> { |
| 107 | const channel = encodeURIComponent(ref.channelId); |
| 108 | const url = |
| 109 | ref.rootId && ref.rootId !== ref.messageId |
| 110 | ? `${GRAPH}/teams/${ref.teamId}/channels/${channel}/messages/${ref.rootId}/replies/${ref.messageId}` |
| 111 | : `${GRAPH}/teams/${ref.teamId}/channels/${channel}/messages/${ref.messageId}`; |
| 112 | const res = await fetch(url, { |
| 113 | headers: { Authorization: `Bearer ${token}` }, |
| 114 | }); |
| 115 | if (!res.ok) { |
| 116 | throw new Error( |
| 117 | `read channel message failed (HTTP ${res.status}): ${(await res.text()).slice(0, 200)}`, |
| 118 | ); |
| 119 | } |
| 120 | const msg = (await res.json()) as { |
| 121 | attachments?: Array<{ |
| 122 | contentType?: string; |
| 123 | contentUrl?: string; |
| 124 | name?: string; |
| 125 | }>; |
| 126 | }; |
| 127 | return (msg.attachments ?? []) |
| 128 | .filter( |
| 129 | (a): a is { contentType: string; contentUrl: string; name?: string } => |
| 130 | a.contentType === "reference" && typeof a.contentUrl === "string", |
| 131 | ) |
| 132 | .map((a) => ({ name: a.name ?? "file", contentUrl: a.contentUrl })); |
| 133 | } |
| 134 | |
| 135 | /** Download a SharePoint file by its sharing URL via the Graph `/shares` API. */ |
| 136 | async function downloadSharedFile( |
no test coverage detected
searching dependent graphs…