@@ -2,8 +2,11 @@ package sharing
22
33import (
44 "encoding/json"
5+ "net/url"
6+ "strings"
57 "time"
68
9+ "github.com/cozy/cozy-stack/pkg/apps"
710 "github.com/cozy/cozy-stack/pkg/consts"
811 "github.com/cozy/cozy-stack/pkg/contacts"
912 "github.com/cozy/cozy-stack/pkg/couchdb"
@@ -466,4 +469,49 @@ func GetSharingsByDocType(inst *instance.Instance, docType string) (map[string]*
466469 return sharings , nil
467470}
468471
472+ func findIntentForRedirect (inst * instance.Instance , app * apps.WebappManifest , doctype string ) (* apps.Intent , string ) {
473+ action := "SHARING"
474+ if app != nil {
475+ if intent := app .FindIntent (action , doctype ); intent != nil {
476+ return intent , app .Slug ()
477+ }
478+ }
479+ var mans []apps.WebappManifest
480+ err := couchdb .GetAllDocs (inst , consts .Apps , & couchdb.AllDocsRequest {}, & mans )
481+ if err != nil {
482+ return nil , ""
483+ }
484+ for _ , man := range mans {
485+ if intent := man .FindIntent (action , doctype ); intent != nil {
486+ return intent , man .Slug ()
487+ }
488+ }
489+ return nil , ""
490+ }
491+
492+ // RedirectAfterAuthorizeURL returns the URL for the redirection after a user
493+ // has authorized a sharing.
494+ func (s * Sharing ) RedirectAfterAuthorizeURL (inst * instance.Instance ) * url.URL {
495+ doctype := s .Rules [0 ].DocType
496+ app , _ := apps .GetWebappBySlug (inst , s .AppSlug )
497+
498+ if intent , slug := findIntentForRedirect (inst , app , doctype ); intent != nil {
499+ u := inst .SubDomain (slug )
500+ parts := strings .SplitN (intent .Href , "#" , 2 )
501+ if len (parts [0 ]) > 0 {
502+ u .Path = parts [0 ]
503+ }
504+ if len (parts ) == 2 && len (parts [1 ]) > 0 {
505+ u .Fragment = parts [1 ]
506+ }
507+ u .RawQuery = "sharing=" + s .SID
508+ return u
509+ }
510+
511+ if app == nil {
512+ return inst .DefaultRedirection ()
513+ }
514+ return inst .SubDomain (app .Slug ())
515+ }
516+
469517var _ couchdb.Doc = & Sharing {}
0 commit comments