Merge pull request #123 from T-vK/master

Add support for importing old FreeTube subscription.db files
This commit is contained in:
Preston 2020-09-12 11:03:59 -04:00 committed by GitHub
commit cd0c8a064e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 52 additions and 4 deletions

View File

@ -125,9 +125,17 @@ export default Vue.extend({
let textDecode = new TextDecoder('utf-8').decode(data) let textDecode = new TextDecoder('utf-8').decode(data)
textDecode = textDecode.split('\n') textDecode = textDecode.split('\n')
textDecode.pop() textDecode.pop()
textDecode = textDecode.map(data => JSON.parse(data))
textDecode.forEach((data) => { let importingOldFormat = false
const profileData = JSON.parse(data) const firstEntry = textDecode[0]
if (firstEntry.channelId && firstEntry.channelName && firstEntry.channelThumbnail && firstEntry._id && firstEntry.profile) {
// Old FreeTube subscriptions format detected, so convert it to the new one:
textDecode = await this.convertOldFreeTubeFormatToNew(textDecode)
importingOldFormat = true
}
textDecode.forEach((profileData) => {
// We would technically already be done by the time the data is parsed, // We would technically already be done by the time the data is parsed,
// however we want to limit the possibility of malicious data being sent // however we want to limit the possibility of malicious data being sent
// to the app, so we'll only grab the data we need here. // to the app, so we'll only grab the data we need here.
@ -159,7 +167,18 @@ export default Vue.extend({
message: message message: message
}) })
} else { } else {
this.updateProfile(profileObject) if (importingOldFormat && profileObject.name === 'All Channels') {
const primaryProfile = JSON.parse(JSON.stringify(this.profileList[0]))
// filter out subscriptions that already exist before concatenating
profileObject.subscriptions = profileObject.subscriptions.filter(newSub => {
const existingSub = primaryProfile.subscriptions.find(existingSub => existingSub.id === newSub.id)
return !existingSub // return false if sub already exists in default profile
})
primaryProfile.subscriptions = primaryProfile.subscriptions.concat(profileObject.subscriptions)
this.updateProfile(primaryProfile)
} else {
this.updateProfile(profileObject)
}
} }
}) })
@ -707,6 +726,33 @@ export default Vue.extend({
}) })
}, },
async convertOldFreeTubeFormatToNew(oldData) {
const convertedData = []
for (const channel of oldData) {
for (const profile of channel.profile) {
let index = convertedData.findIndex(p => p.name === profile.value)
if (index === -1) { // profile doesn't exist yet
const randomBgColor = await this.getRandomColor()
const contrastyTextColor = await this.calculateColorLuminance(randomBgColor)
convertedData.push({
name: profile.value,
bgColor: randomBgColor,
textColor: contrastyTextColor,
subscriptions: [],
_id: channel._id
})
index = convertedData.length - 1
}
convertedData[index].subscriptions.push({
id: channel.channelId,
name: channel.channelName,
thumbnail: channel.channelThumbnail
})
}
}
return convertedData
},
getChannelInfoInvidious: function (channelId) { getChannelInfoInvidious: function (channelId) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const subscriptionsPayload = { const subscriptionsPayload = {
@ -772,7 +818,9 @@ export default Vue.extend({
'updateProfile', 'updateProfile',
'updateShowProgressBar', 'updateShowProgressBar',
'updateHistory', 'updateHistory',
'showToast' 'showToast',
'getRandomColor',
'calculateColorLuminance'
]), ]),
...mapMutations([ ...mapMutations([