From 7052bc6b8efeddfd8523891764c3cbc6fcebf8d8 Mon Sep 17 00:00:00 2001
From: Eike Kettner <eike.kettner@posteo.de>
Date: Sat, 28 Nov 2020 01:36:59 +0100
Subject: [PATCH] Add cc and bcc to item mail

---
 .../scala/docspell/backend/ops/OMail.scala    |  4 ++
 .../src/main/resources/docspell-openapi.yml   | 10 ++++
 .../restserver/routes/MailSendRoutes.scala    |  4 +-
 modules/webapp/src/main/elm/Comp/ItemMail.elm | 56 ++++++++++++++++++-
 4 files changed, 72 insertions(+), 2 deletions(-)

diff --git a/modules/backend/src/main/scala/docspell/backend/ops/OMail.scala b/modules/backend/src/main/scala/docspell/backend/ops/OMail.scala
index f761570c..5efc5221 100644
--- a/modules/backend/src/main/scala/docspell/backend/ops/OMail.scala
+++ b/modules/backend/src/main/scala/docspell/backend/ops/OMail.scala
@@ -68,6 +68,8 @@ object OMail {
       item: Ident,
       subject: String,
       recipients: List[MailAddress],
+      cc: List[MailAddress],
+      bcc: List[MailAddress],
       body: String,
       attach: AttachSelection
   )
@@ -230,6 +232,8 @@ object OMail {
             val fields: Seq[Trans[F]] = Seq(
               From(sett.mailFrom),
               Tos(m.recipients),
+              Ccs(m.cc),
+              Bccs(m.bcc),
               XMailer.emil,
               Subject(m.subject),
               TextBody[F](m.body)
diff --git a/modules/restapi/src/main/resources/docspell-openapi.yml b/modules/restapi/src/main/resources/docspell-openapi.yml
index 4d0cbb04..f28ccd07 100644
--- a/modules/restapi/src/main/resources/docspell-openapi.yml
+++ b/modules/restapi/src/main/resources/docspell-openapi.yml
@@ -4012,6 +4012,8 @@ components:
         is ignored, if `addAllAttachments` is set to `true`.
       required:
         - recipients
+        - cc
+        - bcc
         - subject
         - body
         - addAllAttachments
@@ -4021,6 +4023,14 @@ components:
           type: array
           items:
             type: string
+        cc:
+          type: array
+          items:
+            type: string
+        bcc:
+          type: array
+          items:
+            type: string
         subject:
           type: string
         body:
diff --git a/modules/restserver/src/main/scala/docspell/restserver/routes/MailSendRoutes.scala b/modules/restserver/src/main/scala/docspell/restserver/routes/MailSendRoutes.scala
index 0dd67cc9..1c6a40c7 100644
--- a/modules/restserver/src/main/scala/docspell/restserver/routes/MailSendRoutes.scala
+++ b/modules/restserver/src/main/scala/docspell/restserver/routes/MailSendRoutes.scala
@@ -39,11 +39,13 @@ object MailSendRoutes {
   def convertIn(item: Ident, s: SimpleMail): Either[String, ItemMail] =
     for {
       rec     <- s.recipients.traverse(MailAddress.parse)
+      cc      <- s.cc.traverse(MailAddress.parse)
+      bcc     <- s.bcc.traverse(MailAddress.parse)
       fileIds <- s.attachmentIds.traverse(Ident.fromString)
       sel =
         if (s.addAllAttachments) AttachSelection.All
         else AttachSelection.Selected(fileIds)
-    } yield ItemMail(item, s.subject, rec, s.body, sel)
+    } yield ItemMail(item, s.subject, rec, cc, bcc, s.body, sel)
 
   def convertOut(res: SendResult): BasicResult =
     res match {
diff --git a/modules/webapp/src/main/elm/Comp/ItemMail.elm b/modules/webapp/src/main/elm/Comp/ItemMail.elm
index 83f5ccd8..fb62176d 100644
--- a/modules/webapp/src/main/elm/Comp/ItemMail.elm
+++ b/modules/webapp/src/main/elm/Comp/ItemMail.elm
@@ -28,6 +28,10 @@ type alias Model =
     , subject : String
     , recipients : List String
     , recipientsModel : Comp.EmailInput.Model
+    , ccRecipients : List String
+    , ccRecipientsModel : Comp.EmailInput.Model
+    , bccRecipients : List String
+    , bccRecipientsModel : Comp.EmailInput.Model
     , body : String
     , attachAll : Bool
     , formError : Maybe String
@@ -37,6 +41,8 @@ type alias Model =
 type Msg
     = SetSubject String
     | RecipientMsg Comp.EmailInput.Msg
+    | CCRecipientMsg Comp.EmailInput.Msg
+    | BCCRecipientMsg Comp.EmailInput.Msg
     | SetBody String
     | ConnMsg (Comp.Dropdown.Msg String)
     | ConnResp (Result Http.Error EmailSettingsList)
@@ -67,6 +73,10 @@ emptyModel =
     , subject = ""
     , recipients = []
     , recipientsModel = Comp.EmailInput.init
+    , ccRecipients = []
+    , ccRecipientsModel = Comp.EmailInput.init
+    , bccRecipients = []
+    , bccRecipientsModel = Comp.EmailInput.init
     , body = ""
     , attachAll = True
     , formError = Nothing
@@ -83,6 +93,8 @@ clear model =
     { model
         | subject = ""
         , recipients = []
+        , ccRecipients = []
+        , bccRecipients = []
         , body = ""
     }
 
@@ -103,6 +115,26 @@ update flags msg model =
             , FormNone
             )
 
+        CCRecipientMsg m ->
+            let
+                ( em, ec, rec ) =
+                    Comp.EmailInput.update flags model.ccRecipients m model.ccRecipientsModel
+            in
+            ( { model | ccRecipients = rec, ccRecipientsModel = em }
+            , Cmd.map CCRecipientMsg ec
+            , FormNone
+            )
+
+        BCCRecipientMsg m ->
+            let
+                ( em, ec, rec ) =
+                    Comp.EmailInput.update flags model.bccRecipients m model.bccRecipientsModel
+            in
+            ( { model | bccRecipients = rec, bccRecipientsModel = em }
+            , Cmd.map BCCRecipientMsg ec
+            , FormNone
+            )
+
         SetBody str ->
             ( { model | body = str }, Cmd.none, FormNone )
 
@@ -153,8 +185,18 @@ update flags msg model =
             case ( model.formError, Comp.Dropdown.getSelected model.connectionModel ) of
                 ( Nothing, conn :: [] ) ->
                     let
+                        emptyMail =
+                            Api.Model.SimpleMail.empty
+
                         sm =
-                            SimpleMail model.recipients model.subject model.body model.attachAll []
+                            { emptyMail
+                                | recipients = model.recipients
+                                , cc = model.ccRecipients
+                                , bcc = model.bccRecipients
+                                , subject = model.subject
+                                , body = model.body
+                                , addAllAttachments = model.attachAll
+                            }
                     in
                     ( model, Cmd.none, FormSend { conn = conn, mail = sm } )
 
@@ -195,6 +237,18 @@ view settings model =
                 ]
             , Html.map RecipientMsg (Comp.EmailInput.view model.recipients model.recipientsModel)
             ]
+        , div [ class "field" ]
+            [ label []
+                [ text "CC(s)"
+                ]
+            , Html.map CCRecipientMsg (Comp.EmailInput.view model.ccRecipients model.ccRecipientsModel)
+            ]
+        , div [ class "field" ]
+            [ label []
+                [ text "BCC(s)"
+                ]
+            , Html.map BCCRecipientMsg (Comp.EmailInput.view model.bccRecipients model.bccRecipientsModel)
+            ]
         , div [ class "field" ]
             [ label [] [ text "Subject" ]
             , input