show unread mail count in topbar
[browser-rts.git] / src / api.ts
index 66381943a40e55598146862be56975ebd26dbbad..5b1edc657a5969056141dc3190231d429106e519 100644 (file)
@@ -100,6 +100,8 @@ server.get<{params: { cityId: string }}, string>('/city/:cityId', async req => {
 server.get<{}, string>('/poll/overview', async req => {
        const account = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
        const city = await cityRepo.getUsersCity(account.id);
+  const unreadMail = await mailRepo.countUnread(account.id);
+
 
   const usage = {
     foodUsagePerTick: await cityRepo.foodUsagePerTick(city),
@@ -111,13 +113,14 @@ server.get<{}, string>('/poll/overview', async req => {
        return renderKingomOverview({
     ...city,
     ...usage
-  }, account) + topbar({...city, ...usage});
+  }, account) + topbar({...city, ...usage}, unreadMail);
 });
 
 server.get<{}, string>('/poll/construction', async req => {
        const account = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
        const city = await cityRepo.getUsersCity(account.id);
        const buildings = await cityRepo.buildingRepository.list();
+  const unreadMail = await mailRepo.countUnread(account.id);
 
        const buildQueues = await cityRepo.getBuildQueues(account.id);
   const usage = {
@@ -126,12 +129,13 @@ server.get<{}, string>('/poll/construction', async req => {
     energyUsagePerTick: await cityRepo.energyUsagePerTick(city),
     energyProductionPerTick: await cityRepo.energyProductionPerTick(city)
   }
-       return renderLandDevelopment(city, buildings, buildQueues) + topbar({...city, ...usage});
+       return renderLandDevelopment(city, buildings, buildQueues) + topbar({...city, ...usage}, unreadMail);
 });
 
 server.get<{}, string>('/poll/unit-training', async req => {
        const account = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
        const city = await cityRepo.getUsersCity(account.id);
+  const unreadMail = await mailRepo.countUnread(account.id);
 
        const unitTrainingQueues = await cityRepo.getUnitTrainingQueues(account.id);
        const units = await cityRepo.unitRepository.list();
@@ -145,12 +149,13 @@ server.get<{}, string>('/poll/unit-training', async req => {
        return renderUnitTraining(city, units, unitTrainingQueues) + topbar({
     ...city,
     ...usage
-  });
+  }, unreadMail);
 });
 
 server.post<{body: {sector: string}}, string>('/poll/map', async req => {
        const account = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
        const city = await cityRepo.getUsersCity(account.id);
+  const unreadMail = await mailRepo.countUnread(account.id);
 
   let sector = city.sector_id;
   if(req.body.sector) {
@@ -172,12 +177,13 @@ server.post<{body: {sector: string}}, string>('/poll/map', async req => {
        return renderOverworldMap(await cityRepo.findAllInSector(sector), city, sector) + topbar({
     ...city,
     ...usage
-  });
+  }, unreadMail);
 });
 
 server.get<{}, string>('/poll/mailroom', async req => {
        const account = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
        const city = await cityRepo.getUsersCity(account.id);
+  const unreadMail = await mailRepo.countUnread(account.id);
 
   const usage = {
     foodUsagePerTick: await cityRepo.foodUsagePerTick(city),
@@ -189,7 +195,7 @@ server.get<{}, string>('/poll/mailroom', async req => {
        return renderMailroom(await mailRepo.listReceivedMessages(account.id)) + topbar({
     ...city,
     ...usage
-  });
+  }, unreadMail);
 });
 
 
@@ -336,24 +342,29 @@ server.post<{
                });
        }, 'reload-outgoing-attacks');
 
-server.get<void, string>('/messages', async req => {
-       const acct = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
-       const msgs = await mailRepo.listReceivedMessages(acct.id);
-
-       return JSON.stringify(msgs);
-});
-
 server.get<{params: {id: string}}, string>('/messages/:id', async req => {
        const acct = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
+  const city = await cityRepo.getUsersCity(acct.id);
        const msg = await mailRepo.getMessage(req.params.id, acct.id);
 
        if(!msg) {
                throw new NotFoundError('No such message', ERROR_CODE.DUPLICATE_CACHE_KEY);
        }
 
+  const usage = {
+    foodUsagePerTick: await cityRepo.foodUsagePerTick(city),
+    foodProductionPerTick: await cityRepo.foodProductionPerTick(city),
+    energyUsagePerTick: await cityRepo.energyUsagePerTick(city),
+    energyProductionPerTick: await cityRepo.energyProductionPerTick(city)
+  }
+
        await mailRepo.markAsRead(msg.id, msg.to_account);
+  const unreadMail = await mailRepo.countUnread(acct.id);
 
-       return renderMessage(msg);
+       return renderMailroom(await mailRepo.listReceivedMessages(acct.id), msg) + topbar({
+    ...city,
+    ...usage
+  }, unreadMail);
 });
 
 server.get<void, string>('/attacks/outgoing', async req => {