// Show announces to users


'use strict';


module.exports = function (N) {

  N.wire.before('server_chain:*', async function show_announces(env) {
    if (!N.config.announces) return;

    let announces = [];

    //
    // Find announces we can show to user's group
    //
    for (let announceid of Object.keys(N.config.announces)) {
      let announce = N.config.announces[announceid];

      if (!announce.usergroup) continue;

      let usergroup;

      try {
        usergroup = await N.models.users.UserGroup.findIdByName(announce.usergroup);
      } catch (__) {
        // suppress "usergroup not found" errors
      }

      if (usergroup && env.user_info.usergroups.some(ug => String(ug) === String(usergroup))) {
        announces.push({
          id:        announceid,
          hide_days: announce.hide_days
        });
      }
    }

    //
    // Check that selected announces haven't been dismissed already
    //
    if (announces.length) {
      let hide_dates = (await N.models.users.AnnounceHideMark.findOne({
        user: env.user_info.user_id
      }))?.hide || {};

      let need_cleanup = false;

      announces = announces.filter(ann => {
        // Only show announce if either:
        //  - it cannot be hidden
        //  - user didn't hide it
        //  - user did hide it, but it reappeared again after some time
        //
        if (!ann.hide_days) return true;

        if (!hide_dates[ann.id]) return true;

        if (+hide_dates[ann.id] > Date.now()) return false;

        delete hide_dates[ann.id];
        need_cleanup = true;
        return true;
      });

      if (need_cleanup) {
        if (Object.keys(hide_dates).length > 0) {
          await N.models.users.AnnounceHideMark.updateOne(
            { user: env.user_info.user_id },
            { $set: { hide: hide_dates } }
          );
        } else {
          await N.models.users.AnnounceHideMark.deleteOne(
            { user: env.user_info.user_id }
          );
        }
      }
    }

    if (announces.length) {
      env.res.announces = (env.res.announces || []).concat(announces);
    }
  });
};

@@ -383,16 +383,39 @@
 unceid%5D;
+%0A      let show = true;
 %0A%0A      
@@ -418,17 +418,16 @@
     if (
-!
 announce
@@ -442,25 +442,18 @@
 up) 
-continue;%0A%0A
+%7B%0A 
       
+ 
 let 
@@ -474,14 +474,18 @@
     
+  
 try %7B%0A
+  
     
@@ -571,16 +571,18 @@
 ;%0A      
+  
 %7D catch 
@@ -584,24 +584,26 @@
 atch (__) %7B%0A
+  
         // s
@@ -641,27 +641,31 @@
 rrors%0A      
+  
 %7D%0A%0A
+  
       if (us
@@ -662,16 +662,17 @@
     if (
+!
 usergrou
@@ -677,11 +677,12 @@
 oup 
-&& 
+%7C%7C !
 env.
@@ -746,24 +746,257 @@
 rgroup))) %7B%0A
+          show = false;%0A        %7D%0A      %7D%0A%0A      if (announce.api_path) %7B%0A        let methods = %5B announce.api_path %5D.flat();%0A%0A        if (!methods.includes(env.method)) %7B%0A          show = false;%0A        %7D%0A      %7D%0A%0A      if (show) %7B%0A
         anno

Allow to set announces on specific apiPath