github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/model/sharing/api_sharing.go (about) 1 package sharing 2 3 import ( 4 "net/http" 5 6 "github.com/cozy/cozy-stack/pkg/consts" 7 "github.com/cozy/cozy-stack/pkg/couchdb" 8 "github.com/cozy/cozy-stack/pkg/jsonapi" 9 "github.com/labstack/echo/v4" 10 ) 11 12 // InfoByDocTypeData returns the sharings info as data array in the JSON-API format 13 func InfoByDocTypeData(c echo.Context, statusCode int, sharings []*APISharing) error { 14 data := make([]jsonapi.Object, len(sharings)) 15 for i, s := range sharings { 16 data[i] = s 17 } 18 return jsonapi.DataList(c, http.StatusOK, data, nil) 19 } 20 21 // APISharing is used to serialize a Sharing to JSON-API 22 type APISharing struct { 23 *Sharing 24 // XXX Hide the credentials 25 Credentials *interface{} `json:"credentials,omitempty"` 26 SharedDocs []couchdb.DocReference `json:"-"` 27 } 28 29 // Included is part of jsonapi.Object interface 30 func (s *APISharing) Included() []jsonapi.Object { return nil } 31 32 // Relationships is part of jsonapi.Object interface 33 func (s *APISharing) Relationships() jsonapi.RelationshipMap { 34 return jsonapi.RelationshipMap{ 35 "shared_docs": jsonapi.Relationship{ 36 Data: s.SharedDocs, 37 }, 38 } 39 } 40 41 // Links is part of jsonapi.Object interface 42 func (s *APISharing) Links() *jsonapi.LinksList { 43 return &jsonapi.LinksList{Self: "/sharings/" + s.SID} 44 } 45 46 // Clone is part of the couchdb.Doc interface 47 func (s *APISharing) Clone() couchdb.Doc { 48 panic("APISharing should not be cloned") 49 } 50 51 var _ jsonapi.Object = (*APISharing)(nil) 52 53 // APICredentials is used to serialize credentials to JSON-API. It is used for 54 // Cozy to Cozy exchange of the credentials, after a recipient has accepted a 55 // sharing. 56 type APICredentials struct { 57 *Credentials 58 PublicName string `json:"public_name,omitempty"` 59 CID string `json:"_id,omitempty"` 60 Bitwarden *APIBitwarden `json:"bitwarden,omitempty"` 61 } 62 63 // APIBitwarden is used to exchange information when the sharing has a rule for 64 // bitwarden organizations. It allows to share documents with end to end 65 // encryption. 66 type APIBitwarden struct { 67 UserID string `json:"user_id"` 68 PublicKey string `json:"public_key"` 69 } 70 71 // ID returns the sharing qualified identifier 72 func (c *APICredentials) ID() string { return c.CID } 73 74 // Rev returns the sharing revision 75 func (c *APICredentials) Rev() string { return "" } 76 77 // DocType returns the sharing document type 78 func (c *APICredentials) DocType() string { return consts.SharingsAnswer } 79 80 // SetID changes the sharing qualified identifier 81 func (c *APICredentials) SetID(id string) { c.CID = id } 82 83 // SetRev changes the sharing revision 84 func (c *APICredentials) SetRev(rev string) {} 85 86 // Clone is part of jsonapi.Object interface 87 func (c *APICredentials) Clone() couchdb.Doc { 88 panic("APICredentials must not be cloned") 89 } 90 91 // Included is part of jsonapi.Object interface 92 func (c *APICredentials) Included() []jsonapi.Object { return nil } 93 94 // Relationships is part of jsonapi.Object interface 95 func (c *APICredentials) Relationships() jsonapi.RelationshipMap { return nil } 96 97 // Links is part of jsonapi.Object interface 98 func (c *APICredentials) Links() *jsonapi.LinksList { return nil } 99 100 var _ jsonapi.Object = (*APICredentials)(nil) 101 102 // APIMoved is used when a Cozy has been moved to a new address to inform the 103 // other members of the sharing of this new URL. 104 type APIMoved struct { 105 SharingID string `json:"id"` 106 NewInstance string `json:"new_instance"` 107 AccessToken string `json:"access_token,omitempty"` 108 RefreshToken string `json:"refresh_token,omitempty"` 109 } 110 111 // ID returns the sharing qualified identifier 112 func (m *APIMoved) ID() string { return m.SharingID } 113 114 // Rev returns the sharing revision 115 func (m *APIMoved) Rev() string { return "" } 116 117 // DocType returns the sharing document type 118 func (m *APIMoved) DocType() string { return consts.SharingsMoved } 119 120 // SetID changes the sharing qualified identifier 121 func (m *APIMoved) SetID(id string) { m.SharingID = id } 122 123 // SetRev changes the sharing revision 124 func (m *APIMoved) SetRev(rev string) {} 125 126 // Clone is part of jsonapi.Object interface 127 func (m *APIMoved) Clone() couchdb.Doc { 128 panic("APIMoved must not be cloned") 129 } 130 131 // Included is part of jsonapi.Object interface 132 func (m *APIMoved) Included() []jsonapi.Object { return nil } 133 134 // Relationships is part of jsonapi.Object interface 135 func (m *APIMoved) Relationships() jsonapi.RelationshipMap { return nil } 136 137 // Links is part of jsonapi.Object interface 138 func (m *APIMoved) Links() *jsonapi.LinksList { return nil } 139 140 var _ jsonapi.Object = (*APIMoved)(nil)