Repository: syncthing/syncthing
File: lib/model/model.go (Go)

Code before:
			cfg.Folders = make([]config.FolderConfiguration, 0, len(folders))
			for _, fcfg := range folders {
				cfg.Folders = append(cfg.Folders, fcfg)
			}
			cfg.Devices = make([]config.DeviceConfiguration, len(devices))
			for _, dcfg := range devices {
				cfg.Devices = append(cfg.Devices, dcfg)
			}
		})
	}

	return nil
}

func (m *model) ccHandleFolders(folders []protocol.Folder, deviceCfg config.DeviceConfiguration, ccDeviceInfos map[string]*indexSenderStartInfo, indexSenders *indexSenderRegistry) ([]string, map[string]struct{}, error) {
	handleTime := time.Now()
	var folderDevice config.FolderDeviceConfiguration
	tempIndexFolders := make([]string, 0, len(folders))
	paused := make(map[string]struct{}, len(folders))
	seenFolders := make(map[string]struct{}, len(folders))
	updatedPending := make([]updatedPendingFolder, 0, len(folders))
	deviceID := deviceCfg.DeviceID
	for _, folder := range folders {
		seenFolders[folder.ID] = struct{}{}

		cfg, ok := m.cfg.Folder(folder.ID)
		if ok {
			folderDevice, ok = cfg.Device(deviceID)
		}
		if !ok {
			indexSenders.remove(folder.ID)
			if deviceCfg.IgnoredFolder(folder.ID) {
				l.Infof("Ignoring folder %s from device %s since we are configured to", folder.Description(), deviceID)
				continue
			}
			recvEnc := len(ccDeviceInfos[folder.ID].local.EncryptionPasswordToken) > 0
			if err := m.db.AddOrUpdatePendingFolder(folder.ID, folder.Label, deviceID, recvEnc); err != nil {
				l.Warnf("Failed to persist pending folder entry to database: %v", err)
			}
			indexSenders.addPending(cfg, ccDeviceInfos[folder.ID])
			updatedPending = append(updatedPending, updatedPendingFolder{
				FolderID:         folder.ID,
				FolderLabel:      folder.Label,
				DeviceID:         deviceID,
				ReceiveEncrypted: recvEnc,
			})
			// DEPRECATED: Only for backwards compatibility, should be removed.
			m.evLogger.Log(events.FolderRejected, map[string]string{
				"folder":      folder.ID,
				"folderLabel": folder.Label,
				"device":      deviceID.String(),

Review comment (suggestion):
`db.ObservedFolder` was meant to be an internal, non-exported type.  It's only because of gogoproto that this is even visible outside of the db package.  If you don't think that it's likely to be confused with `PendingFolder`, I'm okay with using it like this, though.

Code after:
			cfg.Folders = make([]config.FolderConfiguration, 0, len(folders))
			for _, fcfg := range folders {
				cfg.Folders = append(cfg.Folders, fcfg)
			}
			cfg.Devices = make([]config.DeviceConfiguration, len(devices))
			for _, dcfg := range devices {
				cfg.Devices = append(cfg.Devices, dcfg)
			}
		})
	}

	return nil
}

func (m *model) ccHandleFolders(folders []protocol.Folder, deviceCfg config.DeviceConfiguration, ccDeviceInfos map[string]*indexSenderStartInfo, indexSenders *indexSenderRegistry) ([]string, map[string]struct{}, error) {
	var folderDevice config.FolderDeviceConfiguration
	tempIndexFolders := make([]string, 0, len(folders))
	paused := make(map[string]struct{}, len(folders))
	seenFolders := make(map[string]struct{}, len(folders))
	updatedPending := make([]updatedPendingFolder, 0, len(folders))
	deviceID := deviceCfg.DeviceID
	expiredPending, err := m.db.PendingFoldersForDevice(deviceID)
	if err != nil {
		l.Infof("Could not get pending folders for cleanup: %v", err)
	}
	of := db.ObservedFolder{Time: time.Now().Truncate(time.Second)}
	for _, folder := range folders {
		seenFolders[folder.ID] = struct{}{}

		cfg, ok := m.cfg.Folder(folder.ID)
		if ok {
			folderDevice, ok = cfg.Device(deviceID)
		}
		if !ok {
			indexSenders.remove(folder.ID)
			if deviceCfg.IgnoredFolder(folder.ID) {
				l.Infof("Ignoring folder %s from device %s since we are configured to", folder.Description(), deviceID)
				continue
			}
			delete(expiredPending, folder.ID)
			of.Label = folder.Label
			of.ReceiveEncrypted = len(ccDeviceInfos[folder.ID].local.EncryptionPasswordToken) > 0
			if err := m.db.AddOrUpdatePendingFolder(folder.ID, of, deviceID); err != nil {
				l.Warnf("Failed to persist pending folder entry to database: %v", err)
			}
			indexSenders.addPending(cfg, ccDeviceInfos[folder.ID])
			updatedPending = append(updatedPending, updatedPendingFolder{
				FolderID:         folder.ID,
				FolderLabel:      folder.Label,
				DeviceID:         deviceID,
				ReceiveEncrypted: of.ReceiveEncrypted,