Optimization
This commit is contained in:
		| @@ -15,7 +15,7 @@ import User from '../../models/user'; | |||||||
|  */ |  */ | ||||||
| module.exports = async (params, user, _, isSecure) => new Promise(async (res, rej) => { | module.exports = async (params, user, _, isSecure) => new Promise(async (res, rej) => { | ||||||
| 	// Get 'home' parameter | 	// Get 'home' parameter | ||||||
| 	const [home, homeErr] = $(params.home).array().each( | 	const [home, homeErr] = $(params.home).optional.array().each( | ||||||
| 		$().strict.object() | 		$().strict.object() | ||||||
| 			.have('name', $().string()) | 			.have('name', $().string()) | ||||||
| 			.have('id', $().string()) | 			.have('id', $().string()) | ||||||
| @@ -23,12 +23,38 @@ module.exports = async (params, user, _, isSecure) => new Promise(async (res, re | |||||||
| 			.have('data', $().object())).$; | 			.have('data', $().object())).$; | ||||||
| 	if (homeErr) return rej('invalid home param'); | 	if (homeErr) return rej('invalid home param'); | ||||||
|  |  | ||||||
|  | 	// Get 'id' parameter | ||||||
|  | 	const [id, idErr] = $(params.id).optional.string().$; | ||||||
|  | 	if (idErr) return rej('invalid id param'); | ||||||
|  |  | ||||||
|  | 	// Get 'data' parameter | ||||||
|  | 	const [data, dataErr] = $(params.data).optional.object().$; | ||||||
|  | 	if (dataErr) return rej('invalid data param'); | ||||||
|  |  | ||||||
|  | 	if (home) { | ||||||
| 		await User.update(user._id, { | 		await User.update(user._id, { | ||||||
| 			$set: { | 			$set: { | ||||||
| 				'client_settings.home': home | 				'client_settings.home': home | ||||||
| 			} | 			} | ||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
| 	// Send response |  | ||||||
| 		res(); | 		res(); | ||||||
|  | 	} else { | ||||||
|  | 		if (id == null && data == null) return rej('you need to set id and data params if home param unset'); | ||||||
|  |  | ||||||
|  | 		const _home = user.client_settings.home; | ||||||
|  | 		const widget = _home.find(w => w.id == id); | ||||||
|  |  | ||||||
|  | 		if (widget == null) return rej('widget not found'); | ||||||
|  |  | ||||||
|  | 		widget.data = data; | ||||||
|  |  | ||||||
|  | 		await User.update(user._id, { | ||||||
|  | 			$set: { | ||||||
|  | 				'client_settings.home': _home | ||||||
|  | 			} | ||||||
|  | 		}); | ||||||
|  |  | ||||||
|  | 		res(); | ||||||
|  | 	} | ||||||
| }); | }); | ||||||
|   | |||||||
| @@ -73,10 +73,13 @@ | |||||||
| 			if (this.view == 2) this.view = 0; | 			if (this.view == 2) this.view = 0; | ||||||
|  |  | ||||||
| 			// Save view state | 			// Save view state | ||||||
| 			this.I.client_settings.home.filter(w => w.id == this.opts.id)[0].data.view = this.view; |  | ||||||
| 			this.api('i/update_home', { | 			this.api('i/update_home', { | ||||||
| 				home: this.I.client_settings.home | 				id: this.opts.id, | ||||||
|  | 				data: { | ||||||
|  | 					view: this.view | ||||||
|  | 				} | ||||||
| 			}).then(() => { | 			}).then(() => { | ||||||
|  | 				this.I.client_settings.home.find(w => w.id == this.opts.id).data.view = this.view; | ||||||
| 				this.I.update(); | 				this.I.update(); | ||||||
| 			}); | 			}); | ||||||
| 		}; | 		}; | ||||||
|   | |||||||
| @@ -83,10 +83,13 @@ | |||||||
| 			this.zap(); | 			this.zap(); | ||||||
|  |  | ||||||
| 			// Save state | 			// Save state | ||||||
| 			this.I.client_settings.home.filter(w => w.id == this.opts.id)[0].data.channel = this.channelId; |  | ||||||
| 			this.api('i/update_home', { | 			this.api('i/update_home', { | ||||||
| 				home: this.I.client_settings.home | 				id: this.opts.id, | ||||||
|  | 				data: { | ||||||
|  | 					channel: this.channelId | ||||||
|  | 				} | ||||||
| 			}).then(() => { | 			}).then(() => { | ||||||
|  | 				this.I.client_settings.home.find(w => w.id == this.opts.id).data.channel = this.channelId; | ||||||
| 				this.I.update(); | 				this.I.update(); | ||||||
| 			}); | 			}); | ||||||
| 		}; | 		}; | ||||||
|   | |||||||
| @@ -81,10 +81,13 @@ | |||||||
| 			if (this.view == 6) this.view = 0; | 			if (this.view == 6) this.view = 0; | ||||||
|  |  | ||||||
| 			// Save view state | 			// Save view state | ||||||
| 			this.I.client_settings.home.filter(w => w.id == this.opts.id)[0].data.view = this.view; |  | ||||||
| 			this.api('i/update_home', { | 			this.api('i/update_home', { | ||||||
| 				home: this.I.client_settings.home | 				id: this.opts.id, | ||||||
|  | 				data: { | ||||||
|  | 					view: this.view | ||||||
|  | 				} | ||||||
| 			}).then(() => { | 			}).then(() => { | ||||||
|  | 				this.I.client_settings.home.find(w => w.id == this.opts.id).data.view = this.view; | ||||||
| 				this.I.update(); | 				this.I.update(); | ||||||
| 			}); | 			}); | ||||||
| 		}; | 		}; | ||||||
|   | |||||||
| @@ -88,14 +88,7 @@ | |||||||
| 			if (this.size == 2) this.size = 0; | 			if (this.size == 2) this.size = 0; | ||||||
|  |  | ||||||
| 			this.applySize(); | 			this.applySize(); | ||||||
|  | 			this.save(); | ||||||
| 			// Save state |  | ||||||
| 			this.I.client_settings.home.filter(w => w.id == this.opts.id)[0].data.size = this.size; |  | ||||||
| 			this.api('i/update_home', { |  | ||||||
| 				home: this.I.client_settings.home |  | ||||||
| 			}).then(() => { |  | ||||||
| 				this.I.update(); |  | ||||||
| 			}); |  | ||||||
| 		}; | 		}; | ||||||
|  |  | ||||||
| 		this.change = () => { | 		this.change = () => { | ||||||
| @@ -147,14 +140,23 @@ | |||||||
| 			i.one('selected', folder => { | 			i.one('selected', folder => { | ||||||
| 				this.folder = folder ? folder.id : null; | 				this.folder = folder ? folder.id : null; | ||||||
| 				this.fetch(); | 				this.fetch(); | ||||||
|  | 				this.save(); | ||||||
| 				// Save state |  | ||||||
| 				this.I.client_settings.home.filter(w => w.id == this.opts.id)[0].data.folder = this.folder; |  | ||||||
| 				this.api('i/update_home', { |  | ||||||
| 					home: this.I.client_settings.home |  | ||||||
| 				}).then(() => { |  | ||||||
| 					this.I.update(); |  | ||||||
| 			}); | 			}); | ||||||
|  | 		}; | ||||||
|  |  | ||||||
|  | 		this.save = () => { | ||||||
|  | 			// Save state | ||||||
|  | 			this.api('i/update_home', { | ||||||
|  | 				id: this.opts.id, | ||||||
|  | 				data: { | ||||||
|  | 					folder: this.folder, | ||||||
|  | 					size: this.size | ||||||
|  | 				} | ||||||
|  | 			}).then(() => { | ||||||
|  | 				const w = this.I.client_settings.home.find(w => w.id == this.opts.id); | ||||||
|  | 				w.data.folder = this.folder; | ||||||
|  | 				w.data.size = this.size; | ||||||
|  | 				this.I.update(); | ||||||
| 			}); | 			}); | ||||||
| 		}; | 		}; | ||||||
| 	</script> | 	</script> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 syuilo
					syuilo