@@ -18,6 +18,7 @@ import (
1818 "github.com/cozy/cozy-stack/model/permission"
1919 csettings "github.com/cozy/cozy-stack/model/settings"
2020 "github.com/cozy/cozy-stack/model/vfs"
21+ "github.com/cozy/cozy-stack/pkg/config/config"
2122 "github.com/cozy/cozy-stack/pkg/consts"
2223 "github.com/cozy/cozy-stack/pkg/couchdb"
2324 "github.com/cozy/cozy-stack/pkg/couchdb/mango"
@@ -89,7 +90,10 @@ func (m *Member) CreateSharingRequest(inst *instance.Instance, s *Sharing, c *Cr
8990 if err != nil {
9091 return err
9192 }
92- var list interface {} = []Credentials {{DriveToken : token }}
93+ var list interface {} = []Credentials {{
94+ DriveToken : token ,
95+ State : c .State ,
96+ }}
9397 creds = & list
9498 }
9599
@@ -468,6 +472,43 @@ func (s *Sharing) SendAnswer(inst *instance.Instance, state string) error {
468472 s .Credentials [0 ].Client = creds .Client
469473 s .Active = true
470474 s .Initial = s .NbFiles > 0
475+
476+ options := config .GetConfig ().Sharing .OptionsForContext (inst .ContextName )
477+ // Mark the sender's contact as trusted since we accepted their sharing
478+ if * options .AutoAcceptTrustedContacts && len (s .Members ) > 0 && s .Members [0 ].Email != "" {
479+ c , err := contact .FindByEmail (inst , s .Members [0 ].Email )
480+ if err != nil {
481+ // Contact doesn't exist, create it using the standardized method
482+ c , err = contact .CreateFromSharingMember (inst , s .Members [0 ].Email , s .Members [0 ].Name , s .Members [0 ].Instance )
483+ if err != nil {
484+ if couchdb .IsConflictError (err ) {
485+ c , err = contact .FindByEmail (inst , s .Members [0 ].Email )
486+ if err != nil {
487+ inst .Logger ().WithNamespace ("sharing" ).
488+ Debugf ("Contact creation conflict and retry failed: %s" , err )
489+ c = nil
490+ }
491+ } else {
492+ inst .Logger ().WithNamespace ("sharing" ).
493+ Warnf ("Could not create contact for sender: %s" , err )
494+ }
495+ } else {
496+ inst .Logger ().WithNamespace ("sharing" ).
497+ Infof ("Created contact for sender %s" , s .Members [0 ].Email )
498+ }
499+ }
500+
501+ if c != nil && ! c .IsTrusted () {
502+ if err := c .MarkAsTrusted (inst ); err != nil {
503+ inst .Logger ().WithNamespace ("sharing" ).
504+ Warnf ("Could not mark contact as trusted: %s" , err )
505+ } else {
506+ inst .Logger ().WithNamespace ("sharing" ).
507+ Infof ("Marked contact %s as trusted after accepting sharing" , s .Members [0 ].Email )
508+ }
509+ }
510+ }
511+
471512 return couchdb .UpdateDoc (inst , s )
472513}
473514
0 commit comments