FreeTube/src/renderer/views/Channel/Channel.js

812 lines
25 KiB
JavaScript
Raw Normal View History

2020-02-16 19:30:00 +01:00
import Vue from 'vue'
import { mapActions } from 'vuex'
2020-02-16 19:30:00 +01:00
import FtCard from '../../components/ft-card/ft-card.vue'
import FtButton from '../../components/ft-button/ft-button.vue'
import FtInput from '../../components/ft-input/ft-input.vue'
import FtSelect from '../../components/ft-select/ft-select.vue'
import FtFlexBox from '../../components/ft-flex-box/ft-flex-box.vue'
import FtChannelBubble from '../../components/ft-channel-bubble/ft-channel-bubble.vue'
import FtLoader from '../../components/ft-loader/ft-loader.vue'
import FtElementList from '../../components/ft-element-list/ft-element-list.vue'
import FtAgeRestricted from '../../components/ft-age-restricted/ft-age-restricted.vue'
2020-02-16 19:30:00 +01:00
2020-06-24 04:40:34 +02:00
import ytch from 'yt-channel-info'
import autolinker from 'autolinker'
Store Revamp / Full database synchronization across windows (#1833) * History: Refactor history module * Profiles: Refactor profiles module * IPC: Move channel ids to their own file and make them constants * IPC: Replace single sync channel for one channel per sync type * Everywhere: Replace default profile id magic strings with constant ref * Profiles: Refactor `activeProfile` property from store This commit makes it so that `activeProfile`'s getter returns the entire profile, while the related update function only needs the profile id (instead of the previously used array index) to change the currently active profile. This change was made due to inconsistency regarding the active profile when creating new profiles. If a new profile coincidentally landed in the current active profile's array index after sorting, the app would mistakenly change to it without any action from the user apart from the profile's creation. Turning the profile id into the selector instead solves this issue. * Revert "Store: Implement history synchronization between windows" This reverts commit 99b61e617873412eb393d8f4dfccd8f8c172021f. This is necessary for an upcoming improved implementation of the history synchronization. * History: Remove unused mutation * Everywhere: Create abstract database handlers The project now utilizes abstract handlers to fetch, modify or otherwise manipulate data from the database. This facilitates 3 aspects of the app, in addition of making them future proof: - Switching database libraries is now trivial Since most of the app utilizes the abstract handlers, it's incredibly easily to change to a different DB library. Hypothetically, all that would need to be done is to simply replace the the file containing the base handlers, while the rest of the app would go unchanged. - Syncing logic between Electron and web is now properly separated There are now two distinct DB handling APIs: the Electron one and the web one. The app doesn't need to manually choose the API, because it's detected which platform is being utilized on import. - All Electron windows now share the same database instance This provides a single source of truth, improving consistency regarding data manipulation and windows synchronization. As a sidenote, syncing implementation has been left as is (web unimplemented; Electron only syncs settings, remaining datastore syncing will be implemented in the upcoming commits). * Electron/History: Implement history synchronization * Profiles: Implement suplementary profile creation logic * ft-profile-edit: Small fix on profile name missing display * Electron/Profiles: Implement profile synchronization * Electron/Playlists: Implement playlist synchronization
2021-12-15 19:42:24 +01:00
import { MAIN_PROFILE_ID } from '../../../constants'
import i18n from '../../i18n/index'
2020-06-24 04:40:34 +02:00
2020-02-16 19:30:00 +01:00
export default Vue.extend({
name: 'Search',
components: {
'ft-card': FtCard,
'ft-button': FtButton,
'ft-input': FtInput,
'ft-select': FtSelect,
'ft-flex-box': FtFlexBox,
'ft-channel-bubble': FtChannelBubble,
'ft-loader': FtLoader,
'ft-element-list': FtElementList,
'ft-age-restricted': FtAgeRestricted
2020-02-16 19:30:00 +01:00
},
data: function () {
return {
isLoading: false,
isElementListLoading: false,
currentTab: 'videos',
id: '',
idType: 0,
2020-02-16 19:30:00 +01:00
channelName: '',
bannerUrl: '',
thumbnailUrl: '',
subCount: 0,
latestVideosPage: 2,
searchPage: 2,
2020-06-24 04:40:34 +02:00
videoContinuationString: '',
2020-02-16 19:30:00 +01:00
playlistContinuationString: '',
2020-06-24 04:40:34 +02:00
searchContinuationString: '',
2020-02-16 19:30:00 +01:00
channelDescription: '',
videoSortBy: 'newest',
playlistSortBy: 'last',
lastSearchQuery: '',
relatedChannels: [],
latestVideos: [],
latestPlaylists: [],
searchResults: [],
shownElementList: [],
2020-06-24 04:40:34 +02:00
apiUsed: '',
isFamilyFriendly: false,
errorMessage: '',
2020-02-16 19:30:00 +01:00
videoSelectValues: [
'newest',
'oldest',
'popular'
],
playlistSelectValues: [
'last',
2021-03-04 22:51:56 +01:00
'newest'
2020-02-16 19:30:00 +01:00
]
}
},
computed: {
2020-06-24 04:40:34 +02:00
backendPreference: function () {
return this.$store.getters.getBackendPreference
},
backendFallback: function () {
return this.$store.getters.getBackendFallback
},
hideUnsubscribeButton: function() {
return this.$store.getters.getHideUnsubscribeButton
},
showFamilyFriendlyOnly: function() {
return this.$store.getters.getShowFamilyFriendlyOnly
},
currentInvidiousInstance: function () {
return this.$store.getters.getCurrentInvidiousInstance
},
2020-02-16 19:30:00 +01:00
sessionSearchHistory: function () {
return this.$store.getters.getSessionSearchHistory
},
profileList: function () {
return this.$store.getters.getProfileList
},
activeProfile: function () {
return this.$store.getters.getActiveProfile
},
subscriptionInfo: function () {
return this.activeProfile.subscriptions.find((channel) => {
return channel.id === this.id
}) ?? null
},
isSubscribed: function () {
return this.subscriptionInfo !== null
},
subscribedText: function () {
if (this.isSubscribed) {
return this.$t('Channel.Unsubscribe').toUpperCase()
} else {
return this.$t('Channel.Subscribe').toUpperCase()
}
},
videoSelectNames: function () {
return [
this.$t('Channel.Videos.Sort Types.Newest'),
this.$t('Channel.Videos.Sort Types.Oldest'),
this.$t('Channel.Videos.Sort Types.Most Popular')
]
},
playlistSelectNames: function () {
return [
this.$t('Channel.Playlists.Sort Types.Last Video Added'),
2021-03-04 22:51:56 +01:00
this.$t('Channel.Playlists.Sort Types.Newest')
]
},
currentLocale: function () {
return i18n.locale.replace('_', '-')
},
2020-02-16 19:30:00 +01:00
formattedSubCount: function () {
if (this.hideChannelSubscriptions) {
return null
}
return Intl.NumberFormat(this.currentLocale).format(this.subCount)
},
showFetchMoreButton: function () {
switch (this.currentTab) {
case 'videos':
if (this.apiUsed === 'invidious' || (this.videoContinuationString !== '' && this.videoContinuationString !== null)) {
return true
}
break
case 'playlists':
if (this.playlistContinuationString !== '' && this.playlistContinuationString !== null) {
return true
}
break
case 'search':
if (this.searchContinuationString !== '' && this.searchContinuationString !== null) {
return true
}
break
}
return false
},
hideChannelSubscriptions: function () {
return this.$store.getters.getHideChannelSubscriptions
2020-02-16 19:30:00 +01:00
}
},
watch: {
2020-09-03 03:06:49 +02:00
$route() {
// react to route changes...
this.originalId = this.$route.params.id
2020-09-03 03:06:49 +02:00
this.id = this.$route.params.id
this.idType = this.$route.query.idType ? Number(this.$route.query.idType) : 0
this.currentTab = this.$route.params.currentTab ?? 'videos'
this.latestVideosPage = 2
this.searchPage = 2
this.relatedChannels = []
this.latestVideos = []
this.latestPlaylists = []
this.searchResults = []
this.shownElementList = []
this.apiUsed = ''
2020-09-03 03:06:49 +02:00
this.isLoading = true
if (!process.env.IS_ELECTRON || this.backendPreference === 'invidious') {
this.getChannelInfoInvidious()
this.getPlaylistsInvidious()
2020-09-03 03:06:49 +02:00
} else {
this.getChannelInfoLocal()
this.getChannelVideosLocal()
this.getPlaylistsLocal()
2020-09-03 03:06:49 +02:00
}
},
2020-02-16 19:30:00 +01:00
videoSortBy () {
this.isElementListLoading = true
this.latestVideos = []
2020-06-24 04:40:34 +02:00
switch (this.apiUsed) {
case 'local':
this.getChannelVideosLocal()
break
case 'invidious':
this.latestVideosPage = 1
this.channelInvidiousNextPage()
break
default:
this.getChannelVideosLocal()
}
2020-02-16 19:30:00 +01:00
},
playlistSortBy () {
this.isElementListLoading = true
this.latestPlaylists = []
this.playlistContinuationString = ''
2020-06-24 04:40:34 +02:00
switch (this.apiUsed) {
case 'local':
this.getPlaylistsLocal()
break
case 'invidious':
this.channelInvidiousNextPage()
break
default:
this.getPlaylistsLocal()
}
2020-02-16 19:30:00 +01:00
}
},
mounted: function () {
this.originalId = this.$route.params.id
2020-02-16 19:30:00 +01:00
this.id = this.$route.params.id
this.idType = this.$route.query.idType ? Number(this.$route.query.idType) : 0
this.currentTab = this.$route.params.currentTab ?? 'videos'
2020-06-24 04:40:34 +02:00
this.isLoading = true
2020-02-16 19:30:00 +01:00
if (!process.env.IS_ELECTRON || this.backendPreference === 'invidious') {
this.getChannelInfoInvidious()
this.getPlaylistsInvidious()
2020-06-24 04:40:34 +02:00
} else {
this.getChannelInfoLocal()
this.getChannelVideosLocal()
this.getPlaylistsLocal()
2020-06-24 04:40:34 +02:00
}
2020-02-16 19:30:00 +01:00
},
methods: {
goToChannel: function (id) {
this.$router.push({ path: `/channel/${id}` })
},
2020-06-24 04:40:34 +02:00
getChannelInfoLocal: function () {
this.apiUsed = 'local'
const expectedId = this.originalId
ytch.getChannelInfo({ channelId: this.id, channelIdType: this.idType }).then((response) => {
if (response.alertMessage) {
this.setErrorMessage(response.alertMessage)
return
}
this.errorMessage = ''
if (expectedId !== this.originalId) {
return
}
const channelId = response.authorId
const channelName = response.author
const channelThumbnailUrl = response.authorThumbnails[2].url
this.id = channelId
// set the id type to 1 so that searching and sorting work
this.idType = 1
this.channelName = channelName
this.isFamilyFriendly = response.isFamilyFriendly
document.title = `${this.channelName} - ${process.env.PRODUCT_NAME}`
if (this.hideChannelSubscriptions || response.subscriberCount === 0) {
this.subCount = null
} else {
this.subCount = response.subscriberCount.toFixed(0)
}
this.thumbnailUrl = channelThumbnailUrl
this.updateSubscriptionDetails({ channelThumbnailUrl, channelName, channelId })
this.channelDescription = autolinker.link(response.description)
this.relatedChannels = response.relatedChannels.items
this.relatedChannels.forEach(relatedChannel => {
relatedChannel.thumbnail.map(thumbnail => {
if (!thumbnail.url.includes('https')) {
thumbnail.url = `https:${thumbnail.url}`
}
return thumbnail
})
relatedChannel.authorThumbnails = relatedChannel.thumbnail
})
if (response.authorBanners !== null) {
const bannerUrl = response.authorBanners[response.authorBanners.length - 1].url
if (!bannerUrl.includes('https')) {
this.bannerUrl = `https://${bannerUrl}`
} else {
this.bannerUrl = bannerUrl
}
} else {
this.bannerUrl = null
}
2020-06-24 04:40:34 +02:00
this.isLoading = false
}).catch((err) => {
console.error(err)
const errorMessage = this.$t('Local API Error (Click to copy)')
this.showToast({
message: `${errorMessage}: ${err}`,
time: 10000,
action: () => {
this.copyToClipboard({ content: err })
}
})
if (this.backendPreference === 'local' && this.backendFallback) {
this.showToast({
message: this.$t('Falling back to Invidious API')
})
this.getChannelInfoInvidious()
} else {
this.isLoading = false
}
2020-06-24 04:40:34 +02:00
})
},
getChannelVideosLocal: function () {
this.isElementListLoading = true
const expectedId = this.originalId
ytch.getChannelVideos({ channelId: this.id, channelIdType: this.idType, sortBy: this.videoSortBy }).then((response) => {
if (expectedId !== this.originalId) {
return
}
2020-06-24 04:40:34 +02:00
this.latestVideos = response.items
this.videoContinuationString = response.continuation
this.isElementListLoading = false
}).catch((err) => {
console.error(err)
const errorMessage = this.$t('Local API Error (Click to copy)')
this.showToast({
message: `${errorMessage}: ${err}`,
time: 10000,
action: () => {
this.copyToClipboard({ content: err })
}
})
if (this.backendPreference === 'local' && this.backendFallback) {
this.showToast({
message: this.$t('Falling back to Invidious API')
})
this.getChannelInfoInvidious()
} else {
this.isLoading = false
}
2020-06-24 04:40:34 +02:00
})
},
channelLocalNextPage: function () {
ytch.getChannelVideosMore({ continuation: this.videoContinuationString }).then((response) => {
2020-06-24 04:40:34 +02:00
this.latestVideos = this.latestVideos.concat(response.items)
this.videoContinuationString = response.continuation
}).catch((err) => {
console.error(err)
const errorMessage = this.$t('Local API Error (Click to copy)')
this.showToast({
message: `${errorMessage}: ${err}`,
time: 10000,
action: () => {
this.copyToClipboard({ content: err })
}
})
2020-06-24 04:40:34 +02:00
})
},
getChannelInfoInvidious: function () {
2020-02-16 19:30:00 +01:00
this.isLoading = true
this.apiUsed = 'invidious'
2020-02-16 19:30:00 +01:00
const expectedId = this.originalId
this.invidiousGetChannelInfo(this.id).then((response) => {
if (expectedId !== this.originalId) {
return
}
const channelName = response.author
const channelId = response.authorId
this.channelName = channelName
document.title = `${this.channelName} - ${process.env.PRODUCT_NAME}`
this.id = channelId
this.isFamilyFriendly = response.isFamilyFriendly
if (this.hideChannelSubscriptions) {
this.subCount = null
} else {
this.subCount = response.subCount
}
const thumbnail = response.authorThumbnails[3].url
this.thumbnailUrl = thumbnail.replace('https://yt3.ggpht.com', `${this.currentInvidiousInstance}/ggpht/`)
this.updateSubscriptionDetails({ channelThumbnailUrl: thumbnail, channelName: channelName, channelId: channelId })
this.channelDescription = autolinker.link(response.description)
this.relatedChannels = response.relatedChannels.map((channel) => {
channel.authorThumbnails[channel.authorThumbnails.length - 1].url = channel.authorThumbnails[channel.authorThumbnails.length - 1].url.replace('https://yt3.ggpht.com', `${this.currentInvidiousInstance}/ggpht/`)
channel.channelId = channel.authorId
return channel
})
2020-02-16 19:30:00 +01:00
this.latestVideos = response.latestVideos
if (response.authorBanners instanceof Array && response.authorBanners.length > 0) {
this.bannerUrl = response.authorBanners[0].url.replace('https://yt3.ggpht.com', `${this.currentInvidiousInstance}/ggpht/`)
} else {
this.bannerUrl = null
}
this.errorMessage = ''
2020-02-16 19:30:00 +01:00
this.isLoading = false
}).catch((err) => {
this.setErrorMessage(err.responseJSON.error)
console.error(err)
const errorMessage = this.$t('Invidious API Error (Click to copy)')
this.showToast({
message: `${errorMessage}: ${err.responseJSON.error}`,
time: 10000,
action: () => {
this.copyToClipboard({ content: err.responseJSON.error })
}
})
2020-02-16 19:30:00 +01:00
this.isLoading = false
})
},
2020-06-24 04:40:34 +02:00
channelInvidiousNextPage: function () {
2020-02-16 19:30:00 +01:00
const payload = {
resource: 'channels/videos',
id: this.id,
params: {
sort_by: this.videoSortBy,
page: this.latestVideosPage
}
}
this.invidiousAPICall(payload).then((response) => {
2020-02-16 19:30:00 +01:00
this.latestVideos = this.latestVideos.concat(response)
this.latestVideosPage++
this.isElementListLoading = false
}).catch((err) => {
console.error(err)
const errorMessage = this.$t('Local API Error (Click to copy)')
this.showToast({
message: `${errorMessage}: ${err}`,
time: 10000,
action: () => {
this.copyToClipboard({ content: err })
}
})
2020-02-16 19:30:00 +01:00
})
},
2020-06-24 04:40:34 +02:00
getPlaylistsLocal: function () {
const expectedId = this.originalId
ytch.getChannelPlaylistInfo({ channelId: this.id, channelIdType: this.idType, sortBy: this.playlistSortBy }).then((response) => {
if (expectedId !== this.originalId) {
return
}
this.latestPlaylists = response.items.map((item) => {
item.proxyThumbnail = false
return item
})
2020-06-24 04:40:34 +02:00
this.playlistContinuationString = response.continuation
this.isElementListLoading = false
}).catch((err) => {
console.error(err)
const errorMessage = this.$t('Local API Error (Click to copy)')
this.showToast({
message: `${errorMessage}: ${err}`,
time: 10000,
action: () => {
this.copyToClipboard({ content: err })
}
})
if (this.backendPreference === 'local' && this.backendFallback) {
this.showToast({
message: this.$t('Falling back to Invidious API')
})
this.getPlaylistsInvidious()
} else {
this.isLoading = false
}
2020-06-24 04:40:34 +02:00
})
},
getPlaylistsLocalMore: function () {
ytch.getChannelPlaylistsMore({ continuation: this.playlistContinuationString }).then((response) => {
2020-06-24 04:40:34 +02:00
this.latestPlaylists = this.latestPlaylists.concat(response.items)
this.playlistContinuationString = response.continuation
}).catch((err) => {
console.error(err)
const errorMessage = this.$t('Local API Error (Click to copy)')
this.showToast({
message: `${errorMessage}: ${err}`,
time: 10000,
action: () => {
this.copyToClipboard({ content: err })
}
})
2020-06-24 04:40:34 +02:00
})
},
getPlaylistsInvidious: function () {
const payload = {
resource: 'channels/playlists',
id: this.id,
params: {
sort_by: this.playlistSortBy
}
}
this.invidiousAPICall(payload).then((response) => {
this.playlistContinuationString = response.continuation
this.latestPlaylists = response.playlists
this.isElementListLoading = false
}).catch((err) => {
console.error(err)
const errorMessage = this.$t('Invidious API Error (Click to copy)')
this.showToast({
message: `${errorMessage}: ${err.responseJSON.error}`,
time: 10000,
action: () => {
this.copyToClipboard({ content: err.responseJSON.error })
}
})
if (this.backendPreference === 'invidious' && this.backendFallback) {
this.showToast({
message: this.$t('Falling back to Local API')
})
this.getPlaylistsLocal()
} else {
this.isLoading = false
}
})
},
getPlaylistsInvidiousMore: function () {
2020-02-16 19:30:00 +01:00
if (this.playlistContinuationString === null) {
console.warn('There are no more playlists available for this channel')
2020-02-16 19:30:00 +01:00
return
}
const payload = {
resource: 'channels/playlists',
id: this.id,
params: {
sort_by: this.playlistSortBy
2020-02-16 19:30:00 +01:00
}
}
if (this.playlistContinuationString) {
payload.params.continuation = this.playlistContinuationString
}
this.invidiousAPICall(payload).then((response) => {
2020-02-16 19:30:00 +01:00
this.playlistContinuationString = response.continuation
this.latestPlaylists = this.latestPlaylists.concat(response.playlists)
this.isElementListLoading = false
}).catch((err) => {
console.error(err)
const errorMessage = this.$t('Invidious API Error (Click to copy)')
this.showToast({
message: `${errorMessage}: ${err.responseJSON.error}`,
time: 10000,
action: () => {
this.copyToClipboard({ content: err.responseJSON.error })
}
})
if (this.backendPreference === 'invidious' && this.backendFallback) {
this.showToast({
message: this.$t('Falling back to Local API')
})
this.getPlaylistsLocal()
} else {
this.isLoading = false
}
2020-02-16 19:30:00 +01:00
})
},
handleSubscription: function () {
Store Revamp / Full database synchronization across windows (#1833) * History: Refactor history module * Profiles: Refactor profiles module * IPC: Move channel ids to their own file and make them constants * IPC: Replace single sync channel for one channel per sync type * Everywhere: Replace default profile id magic strings with constant ref * Profiles: Refactor `activeProfile` property from store This commit makes it so that `activeProfile`'s getter returns the entire profile, while the related update function only needs the profile id (instead of the previously used array index) to change the currently active profile. This change was made due to inconsistency regarding the active profile when creating new profiles. If a new profile coincidentally landed in the current active profile's array index after sorting, the app would mistakenly change to it without any action from the user apart from the profile's creation. Turning the profile id into the selector instead solves this issue. * Revert "Store: Implement history synchronization between windows" This reverts commit 99b61e617873412eb393d8f4dfccd8f8c172021f. This is necessary for an upcoming improved implementation of the history synchronization. * History: Remove unused mutation * Everywhere: Create abstract database handlers The project now utilizes abstract handlers to fetch, modify or otherwise manipulate data from the database. This facilitates 3 aspects of the app, in addition of making them future proof: - Switching database libraries is now trivial Since most of the app utilizes the abstract handlers, it's incredibly easily to change to a different DB library. Hypothetically, all that would need to be done is to simply replace the the file containing the base handlers, while the rest of the app would go unchanged. - Syncing logic between Electron and web is now properly separated There are now two distinct DB handling APIs: the Electron one and the web one. The app doesn't need to manually choose the API, because it's detected which platform is being utilized on import. - All Electron windows now share the same database instance This provides a single source of truth, improving consistency regarding data manipulation and windows synchronization. As a sidenote, syncing implementation has been left as is (web unimplemented; Electron only syncs settings, remaining datastore syncing will be implemented in the upcoming commits). * Electron/History: Implement history synchronization * Profiles: Implement suplementary profile creation logic * ft-profile-edit: Small fix on profile name missing display * Electron/Profiles: Implement profile synchronization * Electron/Playlists: Implement playlist synchronization
2021-12-15 19:42:24 +01:00
const currentProfile = JSON.parse(JSON.stringify(this.activeProfile))
const primaryProfile = JSON.parse(JSON.stringify(this.profileList[0]))
if (this.isSubscribed) {
currentProfile.subscriptions = currentProfile.subscriptions.filter((channel) => {
return channel.id !== this.id
})
this.updateProfile(currentProfile)
this.showToast({
2020-09-16 14:51:24 +02:00
message: this.$t('Channel.Channel has been removed from your subscriptions')
})
if (this.activeProfile._id === MAIN_PROFILE_ID) {
// Check if a subscription exists in a different profile.
// Remove from there as well.
let duplicateSubscriptions = 0
this.profileList.forEach((profile) => {
Store Revamp / Full database synchronization across windows (#1833) * History: Refactor history module * Profiles: Refactor profiles module * IPC: Move channel ids to their own file and make them constants * IPC: Replace single sync channel for one channel per sync type * Everywhere: Replace default profile id magic strings with constant ref * Profiles: Refactor `activeProfile` property from store This commit makes it so that `activeProfile`'s getter returns the entire profile, while the related update function only needs the profile id (instead of the previously used array index) to change the currently active profile. This change was made due to inconsistency regarding the active profile when creating new profiles. If a new profile coincidentally landed in the current active profile's array index after sorting, the app would mistakenly change to it without any action from the user apart from the profile's creation. Turning the profile id into the selector instead solves this issue. * Revert "Store: Implement history synchronization between windows" This reverts commit 99b61e617873412eb393d8f4dfccd8f8c172021f. This is necessary for an upcoming improved implementation of the history synchronization. * History: Remove unused mutation * Everywhere: Create abstract database handlers The project now utilizes abstract handlers to fetch, modify or otherwise manipulate data from the database. This facilitates 3 aspects of the app, in addition of making them future proof: - Switching database libraries is now trivial Since most of the app utilizes the abstract handlers, it's incredibly easily to change to a different DB library. Hypothetically, all that would need to be done is to simply replace the the file containing the base handlers, while the rest of the app would go unchanged. - Syncing logic between Electron and web is now properly separated There are now two distinct DB handling APIs: the Electron one and the web one. The app doesn't need to manually choose the API, because it's detected which platform is being utilized on import. - All Electron windows now share the same database instance This provides a single source of truth, improving consistency regarding data manipulation and windows synchronization. As a sidenote, syncing implementation has been left as is (web unimplemented; Electron only syncs settings, remaining datastore syncing will be implemented in the upcoming commits). * Electron/History: Implement history synchronization * Profiles: Implement suplementary profile creation logic * ft-profile-edit: Small fix on profile name missing display * Electron/Profiles: Implement profile synchronization * Electron/Playlists: Implement playlist synchronization
2021-12-15 19:42:24 +01:00
if (profile._id === MAIN_PROFILE_ID) {
return
}
const parsedProfile = JSON.parse(JSON.stringify(profile))
const index = parsedProfile.subscriptions.findIndex((channel) => {
return channel.id === this.id
})
if (index !== -1) {
duplicateSubscriptions++
parsedProfile.subscriptions = parsedProfile.subscriptions.filter((x) => {
2020-09-16 14:51:24 +02:00
return x.id !== this.id
})
this.updateProfile(parsedProfile)
}
})
if (duplicateSubscriptions > 0) {
this.showToast({
message: this.$t('Channel.Removed subscription from {count} other channel(s)', { count: duplicateSubscriptions })
})
}
}
} else {
const subscription = {
id: this.id,
name: this.channelName,
thumbnail: this.thumbnailUrl
}
currentProfile.subscriptions.push(subscription)
this.updateProfile(currentProfile)
this.showToast({
2020-09-16 14:51:24 +02:00
message: this.$t('Channel.Added channel to your subscriptions')
})
if (this.activeProfile._id !== MAIN_PROFILE_ID) {
const index = primaryProfile.subscriptions.findIndex((channel) => {
return channel.id === this.id
})
if (index === -1) {
primaryProfile.subscriptions.push(subscription)
this.updateProfile(primaryProfile)
}
}
}
2020-02-16 19:30:00 +01:00
},
setErrorMessage: function (errorMessage) {
this.isLoading = false
this.errorMessage = errorMessage
this.id = this.subscriptionInfo.id
this.channelName = this.subscriptionInfo.name
this.thumbnailUrl = this.subscriptionInfo.thumbnail
this.bannerUrl = null
this.subCount = null
},
2020-02-16 19:30:00 +01:00
handleFetchMore: function () {
switch (this.currentTab) {
case 'videos':
2020-06-24 04:40:34 +02:00
switch (this.apiUsed) {
case 'local':
this.channelLocalNextPage()
break
case 'invidious':
this.channelInvidiousNextPage()
break
}
2020-02-16 19:30:00 +01:00
break
case 'playlists':
2020-06-24 04:40:34 +02:00
switch (this.apiUsed) {
case 'local':
this.getPlaylistsLocalMore()
break
case 'invidious':
this.getPlaylistsInvidiousMore()
2020-06-24 04:40:34 +02:00
break
}
2020-02-16 19:30:00 +01:00
break
case 'search':
2020-06-24 04:40:34 +02:00
switch (this.apiUsed) {
case 'local':
this.searchChannelLocal()
break
case 'invidious':
this.searchChannelInvidious()
break
}
2020-02-16 19:30:00 +01:00
break
}
},
changeTab: function (tab) {
this.currentTab = tab
},
newSearch: function (query) {
this.lastSearchQuery = query
2020-06-24 04:40:34 +02:00
this.searchContinuationString = ''
2020-02-16 19:30:00 +01:00
this.isElementListLoading = true
this.searchPage = 1
this.searchResults = []
this.changeTab('search')
2020-06-24 04:40:34 +02:00
switch (this.apiUsed) {
case 'local':
this.searchChannelLocal()
break
case 'invidious':
this.searchChannelInvidious()
break
}
},
searchChannelLocal: function () {
if (this.searchContinuationString === '') {
ytch.searchChannel({ channelId: this.id, channelIdType: this.idType, query: this.lastSearchQuery }).then((response) => {
2020-06-24 04:40:34 +02:00
this.searchResults = response.items
this.isElementListLoading = false
this.searchContinuationString = response.continuation
}).catch((err) => {
console.error(err)
const errorMessage = this.$t('Local API Error (Click to copy)')
this.showToast({
message: `${errorMessage}: ${err}`,
time: 10000,
action: () => {
this.copyToClipboard({ content: err })
}
})
if (this.backendPreference === 'local' && this.backendFallback) {
this.showToast({
message: this.$t('Falling back to Invidious API')
})
this.searchChannelInvidious()
} else {
this.isLoading = false
}
2020-06-24 04:40:34 +02:00
})
} else {
ytch.searchChannelMore({ continuation: this.searchContinuationString }).then((response) => {
2020-06-24 04:40:34 +02:00
this.searchResults = this.searchResults.concat(response.items)
this.isElementListLoading = false
this.searchContinuationString = response.continuation
}).catch((err) => {
console.error(err)
const errorMessage = this.$t('Local API Error (Click to copy)')
this.showToast({
message: `${errorMessage}: ${err}`,
time: 10000,
action: () => {
this.copyToClipboard({ content: err })
}
})
2020-06-24 04:40:34 +02:00
})
}
2020-02-16 19:30:00 +01:00
},
2020-06-24 04:40:34 +02:00
searchChannelInvidious: function () {
2020-02-16 19:30:00 +01:00
const payload = {
resource: 'channels/search',
id: this.id,
params: {
q: this.lastSearchQuery,
page: this.searchPage
}
}
this.invidiousAPICall(payload).then((response) => {
2020-02-16 19:30:00 +01:00
this.searchResults = this.searchResults.concat(response)
this.isElementListLoading = false
this.searchPage++
}).catch((err) => {
console.error(err)
const errorMessage = this.$t('Invidious API Error (Click to copy)')
this.showToast({
message: `${errorMessage}: ${err}`,
time: 10000,
action: () => {
this.copyToClipboard({ content: err })
}
})
if (this.backendPreference === 'invidious' && this.backendFallback) {
this.showToast({
message: this.$t('Falling back to Local API')
})
this.searchChannelLocal()
} else {
this.isLoading = false
}
2020-02-16 19:30:00 +01:00
})
},
...mapActions([
'showToast',
'updateProfile',
'invidiousGetChannelInfo',
'invidiousAPICall',
'updateSubscriptionDetails',
'copyToClipboard'
])
2020-02-16 19:30:00 +01:00
}
})