Sfoglia il codice sorgente

Added real-time station updating on homepage.

KrisVos130 8 anni fa
parent
commit
3d502e8232
2 ha cambiato i file con 40 aggiunte e 2 eliminazioni
  1. 21 0
      backend/logic/stations.js
  2. 19 2
      frontend/components/pages/Home.vue

+ 21 - 0
backend/logic/stations.js

@@ -356,12 +356,33 @@ module.exports = {
 							if (station.currentSong !== null && station.currentSong._id !== undefined) {
 								station.currentSong.skipVotes = 0;
 							}
+							//TODO Pub/Sub this
 							utils.emitToRoom(`station.${station._id}`, "event:songs.next", {
 								currentSong: station.currentSong,
 								startedAt: station.startedAt,
 								paused: station.paused,
 								timePaused: 0
 							});
+
+							if (station.privacy === 'public') utils.emitToRoom('home', "event:station.nextSong", station._id, station.currentSong);
+							else {
+								let sockets = utils.getRoomSockets('home');
+								for (let socketId in sockets) {
+									let socket = sockets[socketId];
+									let session = sockets[socketId].session;
+									if (session.sessionId) {
+										cache.hget('sessions', session.sessionId, (err, session) => {
+											if (!err && session) {
+												db.models.user.findOne({_id: session.userId}, (err, user) => {
+													if (user.role === 'admin') socket.emit("event:station.nextSong", station._id, station.currentSong);
+													else if (station.type === "community" && station.owner === session.userId) socket.emit("event:station.nextSong", station._id, station.currentSong);
+												});
+											}
+										});
+									}
+								}
+							}
+
 							if (station.currentSong !== null && station.currentSong._id !== undefined) {
 								utils.socketsJoinSongRoom(utils.getRoomSockets(`station.${station._id}`), `song.${station.currentSong._id}`);
 								if (!station.paused) {

+ 19 - 2
frontend/components/pages/Home.vue

@@ -26,7 +26,7 @@
 						</div>
 					</div>
 				</div>
-				<a @click="this.$dispatch('joinStation', station._id)" href='#' class='absolute-a'></a>
+				<a @click="this.$dispatch('joinStation', station._id)" href='#' class='absolute-a' v-link="{ path: '/official/' + station._id }"></a>
 			</div>
 		</div>
 		<div class="group">
@@ -54,7 +54,7 @@
 						</div>
 					</div>
 				</div>
-				<a @click="this.$dispatch('joinStation', station._id)" href='#' class='absolute-a'></a>
+				<a @click="this.$dispatch('joinStation', station._id)" href='#' class='absolute-a' v-link="{ path: '/community/' + station._id }"></a>
 			</div>
 		</div>
 		<main-footer></main-footer>
@@ -95,6 +95,23 @@
 						if (station.currentSong && !station.currentSong.thumbnail) station.currentSong.thumbnail = "/assets/notes-transparent.png";
 						_this.stations[station.type].push(station);
 					});
+					_this.socket.on('event:station.nextSong', (stationId, newSong) => {
+						_this.stations.official.forEach((station) => {
+							if (station._id === stationId) {
+								if (!newSong) newSong = { thumbnail: '/assets/notes-transparent.png' };
+								if (newSong && !newSong.thumbnail) newSong.thumbnail = "/assets/notes-transparent.png";
+								station.currentSong = newSong;
+							}
+						});
+
+						_this.stations.community.forEach((station) => {
+							if (station._id === stationId) {
+								if (!newSong) newSong = { thumbnail: '/assets/notes-transparent.png' };
+								if (newSong && !newSong.thumbnail) newSong.thumbnail = "/assets/notes-transparent.png";
+								station.currentSong = newSong;
+							}
+						});
+					});
 				});
 			});
 		},