-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathprofiles.js
More file actions
294 lines (246 loc) · 8.22 KB
/
profiles.js
File metadata and controls
294 lines (246 loc) · 8.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import { MAIN_PROFILE_ID } from '../../../constants'
import { DBProfileHandlers } from '../../../datastores/handlers/index'
import { calculateColorLuminance, getRandomColor } from '../../helpers/colors'
import { deepCopy } from '../../helpers/utils'
const state = {
profileList: [{
_id: MAIN_PROFILE_ID,
name: 'All Channels',
bgColor: '#000000',
textColor: '#FFFFFF',
subscriptions: []
}],
activeProfile: MAIN_PROFILE_ID
}
const getters = {
getProfileList: (state) => {
return state.profileList
},
getActiveProfile: (state) => {
const activeProfileId = state.activeProfile
return state.profileList.find((profile) => {
return profile._id === activeProfileId
})
},
profileById: (state) => (id) => {
return state.profileList.find(p => p._id === id)
},
getSubscribedChannelIdSet: (state) => {
// The all channels profile is always the first profile in the array
const mainProfile = state.profileList[0]
return mainProfile.subscriptions.reduce((set, channel) => set.add(channel.id), new Set())
},
}
const collator = new Intl.Collator(undefined, {
usage: 'sort',
caseFirst: 'upper',
sensitivity: 'case',
numeric: true
})
function profileSort(a, b) {
if (a._id === MAIN_PROFILE_ID) return -1
if (b._id === MAIN_PROFILE_ID) return 1
const nameA = a.name.normalize('NFC')
const nameB = b.name.normalize('NFC')
return collator.compare(nameA, nameB)
}
const actions = {
async grabAllProfiles({ rootState, commit, state }, defaultName = null) {
let profiles
try {
profiles = await DBProfileHandlers.find()
} catch (errMessage) {
console.error(errMessage)
return
}
if (!Array.isArray(profiles)) return
if (profiles.length === 0) {
// Create a default profile and persist it
const randomColor = getRandomColor().value
const textColor = calculateColorLuminance(randomColor)
const defaultProfile = {
_id: MAIN_PROFILE_ID,
name: defaultName,
bgColor: randomColor,
textColor: textColor,
subscriptions: []
}
try {
await DBProfileHandlers.create(defaultProfile)
commit('setProfileList', [defaultProfile])
} catch (errMessage) {
console.error(errMessage)
}
return
}
// We want the primary profile to always be first
// So sort with that then sort alphabetically by profile name
profiles = profiles.sort(profileSort)
if (state.profileList.length < profiles.length) {
const profile = profiles.find((profile) => {
return profile._id === rootState.settings.defaultProfile
})
if (profile) {
commit('setActiveProfile', profile._id)
}
}
commit('setProfileList', profiles)
},
async batchUpdateSubscriptionDetails({ dispatch, state }, channels) {
if (channels.length === 0) { return }
const profileList = state.profileList
for (const profile of profileList) {
const currentProfileCopy = deepCopy(profile)
let profileUpdated = false
for (const { channelThumbnailUrl, channelName, channelId } of channels) {
const channel = currentProfileCopy.subscriptions.find((channel) => {
return channel.id === channelId
}) ?? null
if (channel === null) { continue }
if (channel.name !== channelName && channelName != null) {
channel.name = channelName
profileUpdated = true
}
if (channelThumbnailUrl) {
const thumbnail = channelThumbnailUrl
// change thumbnail size if different
.replace(/=s\d*/, '=s176')
// If this is an Invidious URL, convert it to a YouTube one
.replace(/^https?:\/\/[^/]+\/ggpht/, 'https://yt3.googleusercontent.com')
if (channel.thumbnail !== thumbnail) {
channel.thumbnail = thumbnail
profileUpdated = true
}
}
}
if (profileUpdated) {
await dispatch('updateProfile', currentProfileCopy)
}
}
},
async updateSubscriptionDetails({ dispatch, state }, { channelThumbnailUrl, channelName, channelId }) {
const thumbnail = channelThumbnailUrl
// change thumbnail size if different
?.replace(/=s\d*/, '=s176')
// If this is an Invidious URL, convert it to a YouTube one
.replace(/^https?:\/\/[^/]+\/ggpht/, 'https://yt3.googleusercontent.com') ??
null
const profileList = state.profileList
for (const profile of profileList) {
const currentProfileCopy = deepCopy(profile)
const channel = currentProfileCopy.subscriptions.find((channel) => {
return channel.id === channelId
}) ?? null
if (channel === null) { continue }
let updated = false
if (channel.name !== channelName && channelName != null) {
channel.name = channelName
updated = true
}
if (channel.thumbnail !== thumbnail && thumbnail != null) {
channel.thumbnail = thumbnail
updated = true
}
if (updated) {
await dispatch('updateProfile', currentProfileCopy)
} else { // channel has not been updated, stop iterating through profiles
break
}
}
},
async createProfile({ commit }, profile) {
try {
const newProfile = await DBProfileHandlers.create(profile)
commit('addProfileToList', newProfile)
} catch (errMessage) {
console.error(errMessage)
}
},
async updateProfile({ commit }, profile) {
try {
await DBProfileHandlers.upsert(profile)
commit('upsertProfileToList', profile)
} catch (errMessage) {
console.error(errMessage)
}
},
async addChannelToProfiles({ commit }, { channel, profileIds }) {
// If this is an Invidious URL, convert it to a YouTube one
if (!channel.thumbnail.startsWith('https://yt3.googleusercontent.com/')) {
channel.thumbnail = channel.thumbnail.replace(/^https?:\/\/[^/]+\/ggpht/, 'https://yt3.googleusercontent.com')
}
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)
commit('removeProfileFromList', profileId)
} catch (errMessage) {
console.error(errMessage)
}
},
updateActiveProfile({ commit }, id) {
commit('setActiveProfile', id)
}
}
const mutations = {
setProfileList(state, profileList) {
state.profileList = profileList
},
setActiveProfile(state, activeProfile) {
state.activeProfile = activeProfile
},
addProfileToList(state, profile) {
state.profileList.push(profile)
state.profileList.sort(profileSort)
},
upsertProfileToList(state, updatedProfile) {
const i = state.profileList.findIndex((p) => {
return p._id === updatedProfile._id
})
if (i === -1) {
state.profileList.push(updatedProfile)
} else {
state.profileList.splice(i, 1, updatedProfile)
}
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
profile.subscriptions = profile.subscriptions.filter(channel => channel.id !== channelId)
}
},
removeProfileFromList(state, profileId) {
const i = state.profileList.findIndex((profile) => {
return profile._id === profileId
})
state.profileList.splice(i, 1)
}
}
export default {
state,
getters,
actions,
mutations
}