Reduce nesting in ReShare Files
continuous-integration/drone/push Build is pending Details
continuous-integration/drone/pr Build is passing Details

This commit is contained in:
Sarah Jamie Lewis 2022-07-05 20:29:07 -07:00
parent eb5a60bbb6
commit fa3358cb89
1 changed files with 45 additions and 35 deletions

View File

@ -93,45 +93,55 @@ func (f *Functionality) DownloadFile(profile peer.CwtchPeer, conversationID int,
// if the time limit has not expired
func (f *Functionality) ReShareFiles(profile peer.CwtchPeer) error {
keys, err := profile.GetScopedZonedAttributes(attr.LocalScope, attr.FilesharingZone)
if err == nil {
for _, key := range keys {
// only look at timestamp keys
if strings.HasSuffix(key, ".ts") {
_, zonedpath := attr.ParseScope(key)
_, keypath := attr.ParseZone(zonedpath)
keyparts := strings.Split(keypath, ".")
if len(keyparts) == 3 && keyparts[2] == "ts" {
// fetch the timestamp key
filekey := strings.Join(keyparts[:2], ".")
timestampString, tsExists := profile.GetScopedZonedAttribute(attr.LocalScope, attr.FilesharingZone, fmt.Sprintf("%s.ts", filekey))
if tsExists {
// assert this is an actual timestamp
timestamp, err := strconv.Atoi(timestampString)
if err == nil {
dateShared := time.Unix(int64(timestamp), 0)
// If fewer than 30 days have passed since we originally shared this file,
// then attempt to share this file again...
// TODO: In the future this would be the point to change the timestamp and reshare the file...
if time.Since(dateShared) < time.Hour*24*30 {
manifest, manifestExists := profile.GetScopedZonedAttribute(attr.ConversationScope, attr.FilesharingZone, fmt.Sprintf("%s.manifest", filekey))
if manifestExists {
// everything is in order, so reshare this file with the engine
profile.ShareFile(filekey, manifest)
}
} else {
log.Debugf("ignored expired file share for %v", filekey)
}
} else {
log.Errorf("filekey attribute contains an incorrect timestamp: %v: %v", key, timestampString)
}
} else {
log.Errorf("could not find expected timestamp for %v", filekey)
}
if err != nil {
return err
}
for _, key := range keys {
// only look at timestamp keys
// this is an arbitrary choice
if strings.HasSuffix(key, ".ts") {
_, zonedpath := attr.ParseScope(key)
_, keypath := attr.ParseZone(zonedpath)
keyparts := strings.Split(keypath, ".")
// assert that the key is well-formed
if len(keyparts) == 3 && keyparts[2] == "ts" {
// fetch the timestamp key
filekey := strings.Join(keyparts[:2], ".")
timestampString, tsExists := profile.GetScopedZonedAttribute(attr.LocalScope, attr.FilesharingZone, fmt.Sprintf("%s.ts", filekey))
// assert that the timestamp actually exists
if !tsExists {
log.Errorf("could not find expected timestamp for %v", filekey)
continue
}
// assert this is an actual timestamp
timestamp, err := strconv.Atoi(timestampString)
if err != nil {
log.Errorf("error parsing timestamp for %v: %v", filekey, err)
continue
}
dateShared := time.Unix(int64(timestamp), 0)
if time.Since(dateShared) > time.Hour*24*30 {
log.Debugf("ignored expired file share for %v", filekey)
continue
}
// If fewer than 30 days have passed since we originally shared this file,
// then attempt to share this file again...
// TODO: In the future this would be the point to change the timestamp and reshare the file...
manifest, manifestExists := profile.GetScopedZonedAttribute(attr.ConversationScope, attr.FilesharingZone, fmt.Sprintf("%s.manifest", filekey))
if manifestExists {
// everything is in order, so reshare this file with the engine
profile.ShareFile(filekey, manifest)
}
}
}
}
return err
return nil
}
// ShareFile given a profile and a conversation handle, sets up a file sharing process to share the file