{-# LANGUAGE StrictData #-}

-- This file is part of the Wire Server implementation.
--
-- Copyright (C) 2022 Wire Swiss GmbH <opensource@wire.com>
--
-- This program is free software: you can redistribute it and/or modify it under
-- the terms of the GNU Affero General Public License as published by the Free
-- Software Foundation, either version 3 of the License, or (at your option) any
-- later version.
--
-- This program is distributed in the hope that it will be useful, but WITHOUT
-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-- FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
-- details.
--
-- You should have received a copy of the GNU Affero General Public License along
-- with this program. If not, see <https://www.gnu.org/licenses/>.

-- | This module is a work-around for the fact that we do not send notifications to all users
-- of large teams any more.  For greeting bots to hear from new users, the 'MemberJoin' event
-- is stored in a queue that every member of a team can pull, in a similar (but not identical)
-- way as gundeck's @/notifications@ end-point, galley has a @/teams/notifications@ end-point
-- where these events can be pulled.
--
-- This module is a clone of "Gundeck.Notification".
--
-- This could have been added to gundeck, but we didn't.  Some motivation: (1) It is a *team*
-- event queue; teams live in galley, and only galley triggers the team events to be stored in
-- the teams.  (2) The team event queue differs from the other queues in that it is not a
-- fallback for websockets / push notifications, but the only source of the events; so a big
-- part of gundeck isn't really needed.  (3) Fewer RPCs, less code.
--
-- FUTUREWORK: this is a work-around because it only solves *some* problems with team events.
-- We should really use a scalable message queue instead.
module Galley.API.Teams.Notifications
  ( getTeamNotifications,
    pushTeamEvent,
  )
where

import Data.Id
import Data.Json.Util (toJSONObject)
import Data.List1 qualified as List1
import Data.Range (Range)
import Galley.Data.TeamNotifications qualified as DataTeamQueue
import Galley.Effects
import Galley.Effects.BrigAccess as Intra
import Galley.Effects.TeamNotificationStore qualified as E
import Imports
import Polysemy
import Wire.API.Error
import Wire.API.Error.Galley
import Wire.API.Event.Team (Event)
import Wire.API.Internal.Notification
import Wire.API.User

getTeamNotifications ::
  ( Member BrigAccess r,
    Member (ErrorS 'TeamNotFound) r,
    Member TeamNotificationStore r
  ) =>
  UserId ->
  Maybe NotificationId ->
  Range 1 10000 Int32 ->
  Sem r QueuedNotificationList
getTeamNotifications :: forall (r :: EffectRow).
(Member BrigAccess r, Member (ErrorS 'TeamNotFound) r,
 Member TeamNotificationStore r) =>
UserId
-> Maybe NotificationId
-> Range 1 10000 Int32
-> Sem r QueuedNotificationList
getTeamNotifications UserId
zusr Maybe NotificationId
since Range 1 10000 Int32
size = do
  TeamId
tid <- (forall {k} (e :: k) (r :: EffectRow) a.
Member (ErrorS e) r =>
Maybe a -> Sem r a
forall (e :: GalleyError) (r :: EffectRow) a.
Member (ErrorS e) r =>
Maybe a -> Sem r a
noteS @'TeamNotFound =<<) (Sem r (Maybe TeamId) -> Sem r TeamId)
-> Sem r (Maybe TeamId) -> Sem r TeamId
forall a b. (a -> b) -> a -> b
$ (User -> Maybe TeamId
userTeam (User -> Maybe TeamId)
-> (UserAccount -> User) -> UserAccount -> Maybe TeamId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UserAccount -> User
accountUser =<<) (Maybe UserAccount -> Maybe TeamId)
-> Sem r (Maybe UserAccount) -> Sem r (Maybe TeamId)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> UserId -> Sem r (Maybe UserAccount)
forall (r :: EffectRow).
Member BrigAccess r =>
UserId -> Sem r (Maybe UserAccount)
Intra.getUser UserId
zusr
  ResultPage
page <- TeamId
-> Maybe NotificationId -> Range 1 10000 Int32 -> Sem r ResultPage
forall (r :: EffectRow).
Member TeamNotificationStore r =>
TeamId
-> Maybe NotificationId -> Range 1 10000 Int32 -> Sem r ResultPage
E.getTeamNotifications TeamId
tid Maybe NotificationId
since Range 1 10000 Int32
size
  QueuedNotificationList -> Sem r QueuedNotificationList
forall a. a -> Sem r a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (QueuedNotificationList -> Sem r QueuedNotificationList)
-> QueuedNotificationList -> Sem r QueuedNotificationList
forall a b. (a -> b) -> a -> b
$
    [QueuedNotification]
-> Bool -> Maybe UTCTime -> QueuedNotificationList
queuedNotificationList
      (Seq QueuedNotification -> [QueuedNotification]
forall a. Seq a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (ResultPage -> Seq QueuedNotification
DataTeamQueue.resultSeq ResultPage
page))
      (ResultPage -> Bool
DataTeamQueue.resultHasMore ResultPage
page)
      Maybe UTCTime
forall a. Maybe a
Nothing

pushTeamEvent :: (Member TeamNotificationStore r) => TeamId -> Event -> Sem r ()
pushTeamEvent :: forall (r :: EffectRow).
Member TeamNotificationStore r =>
TeamId -> Event -> Sem r ()
pushTeamEvent TeamId
tid Event
evt = do
  NotificationId
nid <- Sem r NotificationId
forall (r :: EffectRow).
Member TeamNotificationStore r =>
Sem r NotificationId
E.mkNotificationId
  TeamId -> NotificationId -> List1 Object -> Sem r ()
forall (r :: EffectRow).
Member TeamNotificationStore r =>
TeamId -> NotificationId -> List1 Object -> Sem r ()
E.createTeamNotification TeamId
tid NotificationId
nid (Object -> List1 Object
forall a. a -> List1 a
List1.singleton (Object -> List1 Object) -> Object -> List1 Object
forall a b. (a -> b) -> a -> b
$ Event -> Object
forall a. ToJSONObject a => a -> Object
toJSONObject Event
evt)