Edits history of script submission #23358 for ' Modify Email (ms_outlook)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Modify Email
     * Change a message's state: mark read or unread, flag, set importance, and add or remove categories (labels). Only the options you set are applied; existing categories not listed are kept. Requires Mail.ReadWrite (a connection default).
     */
    export async function main(
      auth: RT.MsOutlook,
      message_id: string,
      is_read: "keep" | "true" | "false" = "keep",
      flag_status: "keep" | "notFlagged" | "flagged" | "complete" = "keep",
      importance: "keep" | "low" | "normal" | "high" = "keep",
      add_categories: string[] | undefined,
      remove_categories: string[] | undefined,
      mailbox: string | undefined
    ) {
      const base = `https://graph.microsoft.com/v1.0/${
        mailbox != null && mailbox !== ""
          ? `users/${encodeURIComponent(mailbox)}`
          : "me"
      }`
      const url = `${base}/messages/${encodeURIComponent(message_id)}`
      const headers = {
        Authorization: `Bearer ${auth.token}`,
        "Content-Type": "application/json",
        Accept: "application/json",
      }
    
      const patch: { [key: string]: any } = {}
      if (is_read !== "keep") patch.isRead = is_read === "true"
      if (flag_status !== "keep") patch.flag = { flagStatus: flag_status }
      if (importance !== "keep") patch.importance = importance
    
      // categories is replaced as a whole, so merge with what the message has
      if (add_categories?.length || remove_categories?.length) {
        const current = await fetch(`${url}?$select=categories`, { headers })
        if (!current.ok) {
          throw new Error(`${current.status} ${await current.text()}`)
        }
        const { categories } = (await current.json()) as { categories: string[] }
        patch.categories = [
          ...new Set([...categories, ...(add_categories ?? [])]),
        ].filter((c) => !(remove_categories ?? []).includes(c))
      }
    
      const response = await fetch(url, {
        method: "PATCH",
        headers,
        body: JSON.stringify(patch),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 2 hours ago

  • bunnative
    //native
    
    /**
     * Modify Email
     * Change a message's state: mark read or unread, flag, set importance, and add or remove categories (labels). Only the options you set are applied; existing categories not listed are kept. Requires Mail.ReadWrite (a connection default).
     */
    export async function main(
      auth: RT.Outlook,
      message_id: string,
      is_read: "keep" | "true" | "false" = "keep",
      flag_status: "keep" | "notFlagged" | "flagged" | "complete" = "keep",
      importance: "keep" | "low" | "normal" | "high" = "keep",
      add_categories: string[] | undefined,
      remove_categories: string[] | undefined,
      mailbox: string | undefined
    ) {
      const base = `https://graph.microsoft.com/v1.0/${
        mailbox != null && mailbox !== ""
          ? `users/${encodeURIComponent(mailbox)}`
          : "me"
      }`
      const url = `${base}/messages/${encodeURIComponent(message_id)}`
      const headers = {
        Authorization: `Bearer ${auth.token}`,
        "Content-Type": "application/json",
        Accept: "application/json",
      }
    
      const patch: { [key: string]: any } = {}
      if (is_read !== "keep") patch.isRead = is_read === "true"
      if (flag_status !== "keep") patch.flag = { flagStatus: flag_status }
      if (importance !== "keep") patch.importance = importance
    
      // categories is replaced as a whole, so merge with what the message has
      if (add_categories?.length || remove_categories?.length) {
        const current = await fetch(`${url}?$select=categories`, { headers })
        if (!current.ok) {
          throw new Error(`${current.status} ${await current.text()}`)
        }
        const { categories } = (await current.json()) as { categories: string[] }
        patch.categories = [
          ...new Set([...categories, ...(add_categories ?? [])]),
        ].filter((c) => !(remove_categories ?? []).includes(c))
      }
    
      const response = await fetch(url, {
        method: "PATCH",
        headers,
        body: JSON.stringify(patch),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 11 hours ago

  • bunnative
    //native
    
    /**
     * Modify Email
     * Change a message's state: mark read or unread, flag, set importance, and add or remove categories (labels). Only the options you set are applied; existing categories not listed are kept. Requires Mail.ReadWrite on the connection.
     */
    export async function main(
      auth: RT.Outlook,
      message_id: string,
      is_read: "keep" | "true" | "false" = "keep",
      flag_status: "keep" | "notFlagged" | "flagged" | "complete" = "keep",
      importance: "keep" | "low" | "normal" | "high" = "keep",
      add_categories: string[] | undefined,
      remove_categories: string[] | undefined,
      mailbox: string | undefined
    ) {
      const base = `https://graph.microsoft.com/v1.0/${
        mailbox != null && mailbox !== ""
          ? `users/${encodeURIComponent(mailbox)}`
          : "me"
      }`
      const url = `${base}/messages/${encodeURIComponent(message_id)}`
      const headers = {
        Authorization: `Bearer ${auth.token}`,
        "Content-Type": "application/json",
        Accept: "application/json",
      }
    
      const patch: { [key: string]: any } = {}
      if (is_read !== "keep") patch.isRead = is_read === "true"
      if (flag_status !== "keep") patch.flag = { flagStatus: flag_status }
      if (importance !== "keep") patch.importance = importance
    
      // categories is replaced as a whole, so merge with what the message has
      if (add_categories?.length || remove_categories?.length) {
        const current = await fetch(`${url}?$select=categories`, { headers })
        if (!current.ok) {
          throw new Error(`${current.status} ${await current.text()}`)
        }
        const { categories } = (await current.json()) as { categories: string[] }
        patch.categories = [
          ...new Set([...categories, ...(add_categories ?? [])]),
        ].filter((c) => !(remove_categories ?? []).includes(c))
      }
    
      const response = await fetch(url, {
        method: "PATCH",
        headers,
        body: JSON.stringify(patch),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 11 hours ago