UNPKG

1.9 kBJavaScriptView Raw
1const computed = require('mutant/computed');
2const Value = require('mutant/value');
3const userViewingGame = require('./userViewingGame')();
4
5module.exports = (userGamesUpdateWatcher, getSituationObs, myIdent) => {
6 const activityObs = userGamesUpdateWatcher.getRingBufferGameMsgsForPlayer(myIdent, getSituationObs, ['chess_game_end'], 10);
7
8 function getRecentActivity() {
9 return activityObs;
10 }
11
12 const lastSeenObs = Value();
13
14 return {
15 getRecentActivityForUserGames: () => getRecentActivity(),
16 unseenNotifications: () => {
17 const recentActivity = getRecentActivity();
18
19 const unseen = computed([recentActivity, lastSeenObs], (activityMessages) => {
20 const lastSeenStr = localStorage.getItem('ssb_chess_last_seen_notification');
21 const lastSeen = lastSeenStr ? parseFloat(lastSeenStr) : 0;
22
23 return activityMessages.filter(entry => entry.msg.timestamp > lastSeen);
24 });
25
26 return computed([unseen], (unseenMessages) => {
27 // If the user is already viewing the game, don't up the count.
28 const currentGame = userViewingGame.getCurrentGame();
29 const msgIndex = unseenMessages.findIndex(
30 entry => entry.msg.value.content.root === currentGame,
31 );
32
33 if (currentGame && (msgIndex !== -1)) {
34 unseenMessages.splice(msgIndex, 1);
35
36 // If it's the only one that was in the list, make sure the user won't
37 // see it when they open the app again
38 if (unseenMessages.length === 0) {
39 localStorage.setItem('ssb_chess_last_seen_notification', Date.now());
40 }
41 }
42
43 return unseenMessages;
44 });
45 },
46 setLastseenMessage: (timeStamp) => {
47 localStorage.setItem('ssb_chess_last_seen_notification', timeStamp);
48
49 // Reset the count to 0 if we're already viewing the page.
50 lastSeenObs.set(timeStamp);
51 },
52
53 };
54};