Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid cloning all profiles when subscribing and unsubscribing #5289

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ const DBActions = {
UPDATE_PLAYLIST: 'db-action-history-update-playlist',
},

PROFILES: {
ADD_CHANNEL: 'db-action-profiles-add-channel',
REMOVE_CHANNEL: 'db-action-profiles-remove-channel'
},

PLAYLISTS: {
UPSERT_VIDEO: 'db-action-playlists-upsert-video-by-playlist-name',
UPSERT_VIDEOS: 'db-action-playlists-upsert-videos-by-playlist-name',
Expand All @@ -77,6 +82,11 @@ const SyncEvents = {
UPDATE_PLAYLIST: 'sync-history-update-playlist',
},

PROFILES: {
ADD_CHANNEL: 'sync-profiles-add-channel',
REMOVE_CHANNEL: 'sync-profiles-remove-channel'
},

PLAYLISTS: {
UPSERT_VIDEO: 'sync-playlists-upsert-video',
DELETE_VIDEO: 'sync-playlists-delete-video',
Expand Down
30 changes: 30 additions & 0 deletions src/datastores/handlers/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,36 @@ class Profiles {
return db.profiles.updateAsync({ _id: profile._id }, profile, { upsert: true })
}

static addChannelToProfiles(channel, profileIds) {
if (profileIds.length === 1) {
return db.profiles.updateAsync(
{ _id: profileIds[0] },
{ $push: { subscriptions: channel } }
)
} else {
return db.profiles.updateAsync(
{ _id: { $in: profileIds } },
{ $push: { subscriptions: channel } },
{ multi: true }
)
}
}

static removeChannelFromProfiles(channelId, profileIds) {
if (profileIds.length === 1) {
return db.profiles.updateAsync(
{ _id: profileIds[0] },
{ $pull: { subscriptions: { id: channelId } } }
)
} else {
return db.profiles.updateAsync(
{ _id: { $in: profileIds } },
{ $pull: { subscriptions: { id: channelId } } },
{ multi: true }
)
}
}

static delete(id) {
return db.profiles.removeAsync({ _id: id })
}
Expand Down
20 changes: 20 additions & 0 deletions src/datastores/handlers/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,26 @@ class Profiles {
)
}

static addChannelToProfiles(channel, profileIds) {
return ipcRenderer.invoke(
IpcChannels.DB_PROFILES,
{
action: DBActions.PROFILES.ADD_CHANNEL,
data: { channel, profileIds }
}
)
}

static removeChannelFromProfiles(channelId, profileIds) {
return ipcRenderer.invoke(
IpcChannels.DB_PROFILES,
{
action: DBActions.PROFILES.REMOVE_CHANNEL,
data: { channelId, profileIds }
}
)
}

static delete(id) {
return ipcRenderer.invoke(
IpcChannels.DB_PROFILES,
Expand Down
8 changes: 8 additions & 0 deletions src/datastores/handlers/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ class Profiles {
return baseHandlers.profiles.upsert(profile)
}

static addChannelToProfiles(channel, profileIds) {
return baseHandlers.profiles.addChannelToProfiles(channel, profileIds)
}

static removeChannelFromProfiles(channelId, profileIds) {
return baseHandlers.profiles.removeChannelFromProfiles(channelId, profileIds)
}

static delete(id) {
return baseHandlers.profiles.delete(id)
}
Expand Down
18 changes: 18 additions & 0 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,24 @@ function runApp() {
)
return null

case DBActions.PROFILES.ADD_CHANNEL:
await baseHandlers.profiles.addChannelToProfiles(data.channel, data.profileIds)
syncOtherWindows(
IpcChannels.SYNC_PROFILES,
event,
{ event: SyncEvents.PROFILES.ADD_CHANNEL, data }
)
return null

case DBActions.PROFILES.REMOVE_CHANNEL:
await baseHandlers.profiles.removeChannelFromProfiles(data.channelId, data.profileIds)
syncOtherWindows(
IpcChannels.SYNC_PROFILES,
event,
{ event: SyncEvents.PROFILES.REMOVE_CHANNEL, data }
)
return null

case DBActions.GENERAL.DELETE:
await baseHandlers.profiles.delete(data)
syncOtherWindows(
Expand Down
88 changes: 36 additions & 52 deletions src/renderer/components/ft-subscribe-button/ft-subscribe-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import FtButton from '../../components/ft-button/ft-button.vue'
import FtPrompt from '../../components/ft-prompt/ft-prompt.vue'

import { MAIN_PROFILE_ID } from '../../../constants'
import { deepCopy, showToast } from '../../helpers/utils'
import { showToast } from '../../helpers/utils'
import { getFirstCharacter } from '../../helpers/strings'

export default defineComponent({
Expand Down Expand Up @@ -118,31 +118,27 @@ export default defineComponent({
this.handleUnsubscription(profile)
}
} else {
const currentProfile = deepCopy(profile)
const subscription = {
id: this.channelId,
name: this.channelName,
thumbnail: this.channelThumbnail
}
currentProfile.subscriptions.push(subscription)

this.updateProfile(currentProfile)
showToast(this.$t('Channel.Added channel to your subscriptions'))
const profileIds = [profile._id]

if (profile._id !== MAIN_PROFILE_ID) {
const primaryProfile = deepCopy(this.profileList.find(prof => {
const primaryProfile = this.profileList.find(prof => {
return prof._id === MAIN_PROFILE_ID
}))

const index = primaryProfile.subscriptions.findIndex((channel) => {
return channel.id === this.channelId
})

if (index === -1) {
primaryProfile.subscriptions.push(subscription)
this.updateProfile(primaryProfile)
if (!this.isProfileSubscribed(primaryProfile)) {
profileIds.push(MAIN_PROFILE_ID)
}
}

const channel = {
id: this.channelId,
name: this.channelName,
thumbnail: this.channelThumbnail
}

this.addChannelToProfiles({ channel, profileIds })

showToast(this.$t('Channel.Added channel to your subscriptions'))
}

if (this.isProfileDropdownEnabled && this.openDropdownOnSubscribe && !this.isProfileDropdownOpen) {
Expand All @@ -169,30 +165,34 @@ export default defineComponent({
},

handleUnsubscription: function (profile) {
const currentProfile = deepCopy(profile)
currentProfile.subscriptions = currentProfile.subscriptions.filter((channel) => {
return channel.id !== this.channelId
})

this.updateProfile(currentProfile)
showToast(this.$t('Channel.Channel has been removed from your subscriptions'))
const profileIds = [profile._id]

if (profile._id === MAIN_PROFILE_ID) {
// Check if a subscription exists in a different profile.
// Remove from there as well.
let duplicateSubscriptions = 0

this.profileList.forEach((profileInList) => {
if (profileInList._id === MAIN_PROFILE_ID) {
return
}
duplicateSubscriptions += this.unsubscribe(profileInList, this.channelId)

if (this.isProfileSubscribed(profileInList)) {
profileIds.push(profileInList._id)
}
})
}

if (duplicateSubscriptions > 0) {
const message = this.$t('Channel.Removed subscription from {count} other channel(s)', { count: duplicateSubscriptions })
showToast(message)
}
this.removeChannelFromProfiles({
channelId: this.channelId,
profileIds
})

showToast(this.$t('Channel.Channel has been removed from your subscriptions'))

if (profile._id === MAIN_PROFILE_ID && profileIds.length > 1) {
showToast(this.$t('Channel.Removed subscription from {count} other channel(s)', {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (non-blocking, minor): Can do as a separate PR, as this is a pre-existing "issue", but we might as well pluralize the label instead of doing (s)

count: profileIds.length - 1
}))
}
},

Expand All @@ -207,30 +207,14 @@ export default defineComponent({
},

isProfileSubscribed: function (profile) {
return this.subscriptionInfoForProfile(profile) !== null
},

unsubscribe: function(profile, channelId) {
const parsedProfile = deepCopy(profile)
const index = parsedProfile.subscriptions.findIndex((channel) => {
return channel.id === channelId
return profile.subscriptions.some((channel) => {
return channel.id === this.channelId
})

if (index !== -1) {
// use filter instead of splice in case the subscription appears multiple times
// https://github.com/FreeTubeApp/FreeTube/pull/3468#discussion_r1179290877
parsedProfile.subscriptions = parsedProfile.subscriptions.filter((x) => {
return x.id !== channelId
})

this.updateProfile(parsedProfile)
return 1
}
return 0
},

...mapActions([
'updateProfile'
'addChannelToProfiles',
'removeChannelFromProfiles'
])
}
})
34 changes: 34 additions & 0 deletions src/renderer/store/modules/profiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,24 @@ const actions = {
}
},

async addChannelToProfiles({ commit }, { channel, profileIds }) {
try {
await DBProfileHandlers.addChannelToProfiles(channel, profileIds)
commit('addChannelToProfiles', { channel, profileIds })
} catch (errMessage) {
console.error(errMessage)
}
},

async removeChannelFromProfiles({ commit }, { channelId, profileIds }) {
try {
await DBProfileHandlers.removeChannelFromProfiles(channelId, profileIds)
commit('removeChannelFromProfiles', { channelId, profileIds })
} catch (errMessage) {
console.error(errMessage)
}
},

async removeProfile({ commit }, profileId) {
try {
await DBProfileHandlers.delete(profileId)
Expand Down Expand Up @@ -214,6 +232,22 @@ const mutations = {
state.profileList.sort(profileSort)
},

addChannelToProfiles(state, { channel, profileIds }) {
for (const id of profileIds) {
state.profileList.find(profile => profile._id === id).subscriptions.push(channel)
}
},

removeChannelFromProfiles(state, { channelId, profileIds }) {
for (const id of profileIds) {
const profile = state.profileList.find(profile => profile._id === id)

// use filter instead of splice in case the subscription appears multiple times
// https://github.com/FreeTubeApp/FreeTube/pull/3468#discussion_r1179290877
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question (non-blocking, minor): Doesn't have to be considered for this PR, but wouldn't a user know that they're subscribed to a channel multiple times? That + the amount of time since this bug has been fixed (unsure exactly when) means maybe this is safe to make a splice now.

profile.subscriptions = profile.subscriptions.filter(channel => channel.id !== channelId)
}
},

removeProfileFromList(state, profileId) {
const i = state.profileList.findIndex((profile) => {
return profile._id === profileId
Expand Down
8 changes: 8 additions & 0 deletions src/renderer/store/modules/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,14 @@ const customActions = {
commit('upsertProfileToList', data)
break

case SyncEvents.PROFILES.ADD_CHANNEL:
commit('addChannelToProfiles', data)
break

case SyncEvents.PROFILES.REMOVE_CHANNEL:
commit('removeChannelFromProfiles', data)
break

case SyncEvents.GENERAL.DELETE:
commit('removeProfileFromList', data)
break
Expand Down