1919package model
2020
2121import (
22- "database/sql"
2322 "strconv"
2423 "time"
2524
@@ -29,7 +28,7 @@ import (
2928 resourcespb "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1"
3029 link "github.com/cs3org/go-cs3apis/cs3/sharing/link/v1beta1"
3130 provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
32- typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
31+ types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
3332 conversions "github.com/cs3org/reva/v3/pkg/cbox/utils"
3433
3534 "gorm.io/datatypes"
@@ -95,37 +94,31 @@ type ShareID struct {
9594 ID uint `gorm:"primarykey"`
9695}
9796
98- // This is the base model for all share types. We cannot use gorm.Model, because we want our ID
99- // to be a foreign key to ShareID, but we incorporate the date fields from gorm. This struct
100- // does not declare any unique indexes as it is embedded by others.
101- type BaseModel struct {
97+ // This is the base model for all share types and embeds parts of gorm.Model. We can't use it, because
98+ // we want our ID to be a foreign key to ShareID, but we incorporate the date fields from GORM.
99+ // The commented-out fields would logically belong here, but we define them in each specific type
100+ // to control the unique indexes to be enforced in the corresponding tables.
101+ type ProtoShare struct {
102102 // Id has to be called Id and not ID, otherwise the foreign key will not work
103103 // ID is a special field in GORM, which it uses as the default Primary Key
104104 Id uint `gorm:"primaryKey;not null;autoIncrement:false"`
105105 ShareId ShareID `gorm:"foreignKey:Id;references:ID;constraint:OnDelete:CASCADE"` //;references:ID
106106 CreatedAt time.Time
107107 UpdatedAt time.Time
108- DeletedAt gorm.DeletedAt `gorm:"index"`
109- }
110-
111- // ProtoShare contains fields that are common between PublicLinks and Shares.
112- // We also define some indexes for performance reasons.
113- type ProtoShare struct {
114- BaseModel
108+ //DeletedAt gorm.DeletedAt `gorm:"index"`
109+ //Inode string `gorm:"size:32;index"`
110+ //Instance string `gorm:"size:32;index"`
115111 UIDOwner string `gorm:"size:64"`
116112 UIDInitiator string `gorm:"size:64;index"`
117113 ItemType ItemType `gorm:"size:16;index"` // file | folder | reference | symlink
118114 InitialPath string
119- Inode string `gorm:"size:32;index"`
120- Instance string `gorm:"size:32;index"`
121115 Permissions uint8
122116 Orphan bool
123117 Expiration datatypes.NullTime `gorm:"index"`
124118}
125119
126120// Share is a regular share between users or groups. The unique index ensures that there
127- // can only be one share per (inode, instance, permissions, recipient) tuple, unless the share is deleted:
128- // for that, we redeclare `DeletedAt`, `Inode`, and `Instance` for GORM to define the unique index.
121+ // can only be one share per (inode, instance, permissions, recipient) tuple, unless the share is deleted.
129122type Share struct {
130123 ProtoShare
131124 DeletedAt gorm.DeletedAt `gorm:"uniqueIndex:u_share"`
@@ -139,7 +132,10 @@ type Share struct {
139132// PublicLink is a public link share. We only enforce a unique constraint on the token.
140133type PublicLink struct {
141134 ProtoShare
142- Token string `gorm:"uniqueIndex:u_link_token;size:32"` // Current tokens are only 16 chars long, but old tokens used to be 32 characters
135+ DeletedAt gorm.DeletedAt `gorm:"index"`
136+ Inode string `gorm:"size:32"`
137+ Instance string `gorm:"size:32"`
138+ Token string `gorm:"uniqueIndex:u_link_token;size:32"` // Current tokens are only 16 chars long, but old tokens used to be 32 characters
143139 Quicklink bool
144140 NotifyUploads bool
145141 NotifyUploadsExtraRecipients string
@@ -159,21 +155,21 @@ type ShareState struct {
159155}
160156
161157// OcmShare represents an OCM share for a remote user. The unique index ensures that there
162- // can only be one share per (inode, instance, shareWith, owner) tuple, unless the share is deleted:
163- // for that, we redeclare `DeletedAt`, `Inode`, and `Instance` as in Share. In addition, tokens must be unique.
158+ // can only be one share per (inode, instance, recipient) tuple, unless the share is deleted.
159+ // In addition, tokens must be unique.
160+ // TODO(lopresti) see if we can consolidate Owner and Initiator with UIDOwner and UIDInitiator in ProtoShare
164161type OcmShare struct {
165- BaseModel
162+ ProtoShare
166163 DeletedAt gorm.DeletedAt `gorm:"uniqueIndex:u_ocmshare"`
167164 Inode string `gorm:"size:64;not null;uniqueIndex:u_ocmshare"`
168165 Instance string `gorm:"size:64;not null;uniqueIndex:u_ocmshare"`
169166 Token string `gorm:"size:255;not null;uniqueIndex:u_ocmshare_token"`
170167 Name string `gorm:"type:text;not null"`
171168 ShareWith string `gorm:"size:255;not null;uniqueIndex:u_ocmshare"`
172- Owner string `gorm:"size:255;not null;uniqueIndex:u_ocmshare "`
169+ Owner string `gorm:"size:255;not null"`
173170 Initiator string `gorm:"type:text;not null"`
174171 Ctime uint64 `gorm:"not null"`
175172 Mtime uint64 `gorm:"not null"`
176- Expiration sql.NullInt64 `gorm:"default:null"`
177173 RecipientType OcmShareType `gorm:"not null"`
178174 Protocols []OcmShareProtocol `gorm:"constraint:OnDelete:CASCADE;"`
179175}
@@ -189,18 +185,18 @@ type OcmShareProtocol struct {
189185// OcmReceivedShare represents an OCM share received from a remote user.
190186type OcmReceivedShare struct {
191187 gorm.Model
192- RemoteShareID string `gorm:"index:i_ocmrecshare_remoteshareid;not null"`
193- Name string `gorm:"size:255;not null"`
194- ItemType ItemType `gorm:"size:16;not null"`
195- ShareWith string `gorm:"size:255;not null"`
196- Owner string `gorm:"index:i_ocmrecshare_owner;size:255;not null"`
197- Initiator string `gorm:"index:i_ocmrecshare_initiator;size:255;not null"`
198- Ctime uint64 `gorm:"not null"`
199- Mtime uint64 `gorm:"not null"`
200- Expiration sql. NullInt64 `gorm:"default:null "`
201- RecipientType OcmShareType `gorm:"index:i_ocmrecshare_type;not null"`
202- State OcmShareState `gorm:"index:i_ocmrecshare_state;not null"`
203- Alias string `gorm:"size:64"`
188+ RemoteShareID string `gorm:"index:i_ocmrecshare_remoteshareid;not null"`
189+ Name string `gorm:"size:255;not null"`
190+ ItemType ItemType `gorm:"size:16;not null"`
191+ ShareWith string `gorm:"size:255;not null"`
192+ Owner string `gorm:"index:i_ocmrecshare_owner;size:255;not null"`
193+ Initiator string `gorm:"index:i_ocmrecshare_initiator;size:255;not null"`
194+ Ctime uint64 `gorm:"not null"`
195+ Mtime uint64 `gorm:"not null"`
196+ Expiration datatypes. NullTime `gorm:"index "`
197+ RecipientType OcmShareType `gorm:"index:i_ocmrecshare_type;not null"`
198+ State OcmShareState `gorm:"index:i_ocmrecshare_state;not null"`
199+ Alias string `gorm:"size:64"`
204200 Hidden bool
205201}
206202
@@ -219,10 +215,10 @@ type OcmReceivedShareProtocol struct {
219215}
220216
221217func (s * Share ) AsCS3Share (granteeType userpb.UserType ) * collaboration.Share {
222- creationTs := & typespb .Timestamp {
218+ creationTs := & types .Timestamp {
223219 Seconds : uint64 (s .CreatedAt .Unix ()),
224220 }
225- updateTs := & typespb .Timestamp {
221+ updateTs := & types .Timestamp {
226222 Seconds : uint64 (s .UpdatedAt .Unix ()),
227223 }
228224 share := & collaboration.Share {
@@ -244,7 +240,7 @@ func (s *Share) AsCS3Share(granteeType userpb.UserType) *collaboration.Share {
244240 }
245241
246242 if s .Expiration .Valid {
247- share .Expiration = & typespb .Timestamp {
243+ share .Expiration = & types .Timestamp {
248244 Seconds : uint64 (s .Expiration .V .Unix ()),
249245 }
250246 }
@@ -271,16 +267,16 @@ func (s *Share) AsCS3ReceivedShare(state *ShareState, granteeType userpb.UserTyp
271267}
272268
273269func (p * PublicLink ) AsCS3PublicShare () * link.PublicShare {
274- ts := & typespb .Timestamp {
270+ ts := & types .Timestamp {
275271 Seconds : uint64 (p .CreatedAt .Unix ()),
276272 }
277273
278- var expires * typespb .Timestamp
274+ var expires * types .Timestamp
279275 if p .Expiration .Valid {
280276 exp , err := p .Expiration .Value ()
281277 if err == nil {
282278 expiration := exp .(time.Time )
283- expires = & typespb .Timestamp {
279+ expires = & types .Timestamp {
284280 Seconds : uint64 (expiration .Unix ()),
285281 }
286282 }
0 commit comments