Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add last_active field to clients #3409

Merged
merged 8 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cassandra-schema.cql
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,7 @@ CREATE TABLE brig_test.clients (
cookie text,
ip inet,
label text,
last_active timestamp,
lat double,
lon double,
model text,
Expand Down
1 change: 1 addition & 0 deletions changelog.d/1-api-changes/last-active
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Client objects have gained an optional `last_active` field. Whenever a client fetches notifications via `GET /notifications`, as long as it provides a client parameter, the `last_active` field of that client is updated, and set to the current timestamp, rounded to the next multiple of a week.
4 changes: 4 additions & 0 deletions charts/gundeck/templates/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ data:
host: 0.0.0.0
port: {{ $.Values.service.internalPort }}

brig:
host: brig
port: 8080

cassandra:
endpoint:
host: {{ .cassandra.host }}
Expand Down
1 change: 1 addition & 0 deletions integration/integration.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ library
Test.AssetDownload
Test.B2B
Test.Brig
Test.Client
Test.Conversation
Test.Demo
Test.Notifications
Expand Down
12 changes: 12 additions & 0 deletions integration/test/API/Brig.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ getUser user target = do
joinHttpPath ["users", domain, uid]
submit "GET" req

getClient ::
(HasCallStack, MakesValue user, MakesValue client) =>
user ->
client ->
App Response
getClient u cli = do
c <- make cli & asString
req <-
baseRequest u Brig Versioned $
joinHttpPath ["clients", c]
submit "GET" req

data AddClient = AddClient
{ ctype :: String,
internal :: Bool,
Expand Down
34 changes: 34 additions & 0 deletions integration/test/Test/Client.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Test.Client where

import API.Brig
import API.Gundeck
import Data.Aeson
import Data.Time.Clock.POSIX
import Data.Time.Clock.System
import Data.Time.Format
import SetupHelpers
import Testlib.Prelude

testClientLastActive :: HasCallStack => App ()
testClientLastActive = do
alice <- randomUser OwnDomain def
c0 <- addClient alice def >>= getJSON 201
cid <- c0 %. "id"

-- newly created clients should not have a last_active value
tm0 <- fromMaybe Null <$> lookupField c0 "last_active"
tm0 `shouldMatch` Null

now <- systemSeconds <$> liftIO getSystemTime

-- fetching notifications updates last_active
void $ getNotifications alice cid def

c1 <- getClient alice cid >>= getJSON 200
tm1 <- c1 %. "last_active" & asString
ts1 <-
round @Double
. realToFrac
. utcTimeToPOSIXSeconds
<$> parseTimeM False defaultTimeLocale "%Y-%m-%dT%H:%M:%SZ" tm1
assertBool "last_active is earlier than expected" $ ts1 >= now
20 changes: 12 additions & 8 deletions integration/test/Testlib/RunServices.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module Testlib.RunServices where

import Control.Concurrent
import Control.Monad.Codensity (lowerCodensity)
import qualified Data.Map as Map
import System.Directory
Expand Down Expand Up @@ -114,18 +115,23 @@ main = do
cwd <- getWorkingDirectory
mbProjectRoot <- findProjectRoot cwd
cfg <- case mbProjectRoot of
Nothing -> error "Could not find project root. Please make sure you call run-services from the somewhere in wire-server."
Nothing -> error "Could not find project root. Please make sure you call run-services from somewhere in wire-server."
Just projectRoot ->
pure $ joinPath [projectRoot, "services/integration.yaml"]

genv <- createGlobalEnv cfg
env <- lowerCodensity $ mkEnv genv

args <- getArgs
let args' = case args of
(x : xs) -> x : xs
_ -> ["sleep", "10000d"]
let cp = proc "sh" (["-c", "exec \"$@\"", "--"] <> args')

let run = case args of
[] -> do
putStrLn "services started"
forever (threadDelay 1000000000)
_ -> do
let cp = proc "sh" (["-c", "exec \"$@\"", "--"] <> args)
(_, _, _, ph) <- createProcess cp
exitWith =<< waitForProcess ph

runAppWithEnv env $ do
lowerCodensity $ do
Expand All @@ -138,6 +144,4 @@ main = do
traverseConcurrentlyCodensity
(\(res, staticPorts, overrides) -> startDynamicBackend res staticPorts overrides)
[(backendA, staticPortsA, fedConfig backendB.berDomain), (backendB, staticPortsB, fedConfig backendA.berDomain)]
liftIO $ do
(_, _, _, ph) <- createProcess cp
exitWith =<< waitForProcess ph
liftIO run
10 changes: 10 additions & 0 deletions libs/wire-api/src/Wire/API/Routes/Internal/Brig.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module Wire.API.Routes.Internal.Brig
MLSAPI,
TeamsAPI,
UserAPI,
ClientAPI,
AuthAPI,
FederationRemotesAPI,
EJPDRequest,
Expand Down Expand Up @@ -386,6 +387,7 @@ type API =
:<|> GetVerificationCode
:<|> TeamsAPI
:<|> UserAPI
:<|> ClientAPI
:<|> AuthAPI
:<|> OAuthAPI
:<|> ISearchIndexAPI
Expand Down Expand Up @@ -436,6 +438,14 @@ type GetDefaultLocale =
:> "locale"
:> Get '[Servant.JSON] LocaleUpdate

type ClientAPI =
Summary "Update last_active field of a client"
:> "clients"
:> Capture "uid" UserId
:> Capture "client" ClientId
:> "activity"
:> MultiVerb1 'POST '[Servant.JSON] (RespondEmpty 200 "OK")

type AuthAPI =
Named
"legalhold-login"
Expand Down
5 changes: 4 additions & 1 deletion libs/wire-api/src/Wire/API/User/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ import qualified Data.Set as Set
import Data.Swagger hiding (Schema, ToSchema, schema)
import qualified Data.Swagger as Swagger
import qualified Data.Text.Encoding as Text.E
import Data.Time.Clock
import Data.UUID (toASCIIBytes)
import Deriving.Swagger
( CustomSwagger,
Expand Down Expand Up @@ -460,7 +461,8 @@ data Client = Client
clientLocation :: Maybe Location,
clientModel :: Maybe Text,
clientCapabilities :: ClientCapabilityList,
clientMLSPublicKeys :: MLSPublicKeys
clientMLSPublicKeys :: MLSPublicKeys,
clientLastActive :: Maybe UTCTime
}
deriving stock (Eq, Show, Generic, Ord)
deriving (Arbitrary) via (GenericUniform Client)
Expand Down Expand Up @@ -500,6 +502,7 @@ instance ToSchema Client where
<*> clientModel .= maybe_ (optField "model" schema)
<*> clientCapabilities .= (fromMaybe mempty <$> optField "capabilities" schema)
<*> clientMLSPublicKeys .= mlsPublicKeysFieldSchema
<*> clientLastActive .= maybe_ (optField "last_active" utcTimeSchema)

mlsPublicKeysFieldSchema :: ObjectSchema SwaggerDoc MLSPublicKeys
mlsPublicKeysFieldSchema = fromMaybe mempty <$> optField "mls_public_keys" mlsPublicKeysSchema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
module Test.Wire.API.Golden.Generated.Client_user where

import Data.Id (ClientId (ClientId, client))
import Data.Json.Util (readUTCTimeMillis)
import Data.Json.Util
import qualified Data.Map as Map
import Data.Misc
import Data.Set as Set
Expand All @@ -41,7 +41,8 @@ testObject_Client_user_1 =
clientLocation = Nothing,
clientModel = Just "\995802;\1081067",
clientCapabilities = ClientCapabilityList Set.empty,
clientMLSPublicKeys = mempty
clientMLSPublicKeys = mempty,
clientLastActive = Nothing
}

testObject_Client_user_2 :: Client
Expand All @@ -56,7 +57,8 @@ testObject_Client_user_2 =
clientLocation = Just (location (Latitude 0.6919026326441752) (Longitude 1.18215529547942)),
clientModel = Nothing,
clientCapabilities = ClientCapabilityList Set.empty,
clientMLSPublicKeys = mempty
clientMLSPublicKeys = mempty,
clientLastActive = Nothing
}

testObject_Client_user_3 :: Client
Expand All @@ -71,7 +73,8 @@ testObject_Client_user_3 =
clientLocation = Just (location (Latitude (-0.31865405026910076)) (Longitude 6.859482454480745e-2)),
clientModel = Nothing,
clientCapabilities = ClientCapabilityList Set.empty,
clientMLSPublicKeys = mempty
clientMLSPublicKeys = mempty,
clientLastActive = fmap fromUTCTimeMillis (readUTCTimeMillis "2023-07-04T09:35:32.000Z")
}

testObject_Client_user_4 :: Client
Expand All @@ -86,7 +89,8 @@ testObject_Client_user_4 =
clientLocation = Just (location (Latitude 0.43019316470477537) (Longitude (-2.1994844230432533))),
clientModel = Just "",
clientCapabilities = ClientCapabilityList Set.empty,
clientMLSPublicKeys = mempty
clientMLSPublicKeys = mempty,
clientLastActive = Nothing
}

testObject_Client_user_5 :: Client
Expand All @@ -101,7 +105,8 @@ testObject_Client_user_5 =
clientLocation = Just (location (Latitude (-1.505966289957799)) (Longitude (-2.516893825541776))),
clientModel = Just "\9015o",
clientCapabilities = ClientCapabilityList Set.empty,
clientMLSPublicKeys = mempty
clientMLSPublicKeys = mempty,
clientLastActive = Nothing
}

testObject_Client_user_6 :: Client
Expand All @@ -116,7 +121,8 @@ testObject_Client_user_6 =
clientLocation = Just (location (Latitude 0.3764380360505919) (Longitude 1.3619562593325738)),
clientModel = Just "",
clientCapabilities = ClientCapabilityList Set.empty,
clientMLSPublicKeys = mempty
clientMLSPublicKeys = mempty,
clientLastActive = fmap fromUTCTimeMillis (readUTCTimeMillis "2021-09-15T22:00:21.000Z")
}

testObject_Client_user_7 :: Client
Expand All @@ -131,7 +137,8 @@ testObject_Client_user_7 =
clientLocation = Nothing,
clientModel = Just "",
clientCapabilities = ClientCapabilityList Set.empty,
clientMLSPublicKeys = mempty
clientMLSPublicKeys = mempty,
clientLastActive = Nothing
}

testObject_Client_user_8 :: Client
Expand All @@ -146,7 +153,8 @@ testObject_Client_user_8 =
clientLocation = Just (location (Latitude 0.8626148594727595) (Longitude (-1.971023301844283))),
clientModel = Just "\1113929",
clientCapabilities = ClientCapabilityList Set.empty,
clientMLSPublicKeys = mempty
clientMLSPublicKeys = mempty,
clientLastActive = Nothing
}

testObject_Client_user_9 :: Client
Expand All @@ -161,7 +169,8 @@ testObject_Client_user_9 =
clientLocation = Just (location (Latitude (-0.3086524641730466)) (Longitude 1.72690152811777)),
clientModel = Just "\13056m",
clientCapabilities = ClientCapabilityList Set.empty,
clientMLSPublicKeys = mempty
clientMLSPublicKeys = mempty,
clientLastActive = Nothing
}

testObject_Client_user_10 :: Client
Expand All @@ -176,7 +185,8 @@ testObject_Client_user_10 =
clientLocation = Just (location (Latitude (-2.6734377548386075)) (Longitude (-1.40544074714727))),
clientModel = Just "\CAN",
clientCapabilities = ClientCapabilityList Set.empty,
clientMLSPublicKeys = Map.fromList [(Ed25519, "ZmFrZSBwdWJsaWMga2V5")]
clientMLSPublicKeys = Map.fromList [(Ed25519, "ZmFrZSBwdWJsaWMga2V5")],
clientLastActive = Nothing
}

testObject_Client_user_11 :: Client
Expand All @@ -191,7 +201,8 @@ testObject_Client_user_11 =
clientLocation = Just (location (Latitude 0.44311730892815937) (Longitude 0.6936233843789369)),
clientModel = Just "ML",
clientCapabilities = ClientCapabilityList Set.empty,
clientMLSPublicKeys = mempty
clientMLSPublicKeys = mempty,
clientLastActive = Nothing
}

testObject_Client_user_12 :: Client
Expand All @@ -206,7 +217,8 @@ testObject_Client_user_12 =
clientLocation = Just (location (Latitude (-2.502416826395783)) (Longitude 1.4712334862249388)),
clientModel = Just "",
clientCapabilities = ClientCapabilityList Set.empty,
clientMLSPublicKeys = mempty
clientMLSPublicKeys = mempty,
clientLastActive = Nothing
}

testObject_Client_user_13 :: Client
Expand All @@ -221,7 +233,8 @@ testObject_Client_user_13 =
clientLocation = Just (location (Latitude (-2.3798205243177692)) (Longitude (-2.619240132398651))),
clientModel = Just "\ETB\68772",
clientCapabilities = ClientCapabilityList Set.empty,
clientMLSPublicKeys = mempty
clientMLSPublicKeys = mempty,
clientLastActive = Nothing
}

testObject_Client_user_14 :: Client
Expand All @@ -236,7 +249,8 @@ testObject_Client_user_14 =
clientLocation = Just (location (Latitude 2.459582010332432) (Longitude (-1.2286910026214775))),
clientModel = Just "\1052175\r\917608",
clientCapabilities = ClientCapabilityList Set.empty,
clientMLSPublicKeys = mempty
clientMLSPublicKeys = mempty,
clientLastActive = Nothing
}

testObject_Client_user_15 :: Client
Expand All @@ -251,7 +265,8 @@ testObject_Client_user_15 =
clientLocation = Nothing,
clientModel = Just "zAI",
clientCapabilities = ClientCapabilityList Set.empty,
clientMLSPublicKeys = mempty
clientMLSPublicKeys = mempty,
clientLastActive = Nothing
}

testObject_Client_user_16 :: Client
Expand All @@ -266,7 +281,8 @@ testObject_Client_user_16 =
clientLocation = Nothing,
clientModel = Just "",
clientCapabilities = ClientCapabilityList Set.empty,
clientMLSPublicKeys = mempty
clientMLSPublicKeys = mempty,
clientLastActive = Nothing
}

testObject_Client_user_17 :: Client
Expand All @@ -281,7 +297,8 @@ testObject_Client_user_17 =
clientLocation = Just (location (Latitude (-1.6915872714820337)) (Longitude 2.1128949838723656)),
clientModel = Just "",
clientCapabilities = ClientCapabilityList Set.empty,
clientMLSPublicKeys = mempty
clientMLSPublicKeys = mempty,
clientLastActive = Nothing
}

testObject_Client_user_18 :: Client
Expand All @@ -296,7 +313,8 @@ testObject_Client_user_18 =
clientLocation = Just (location (Latitude (-1.2949675488134762)) (Longitude 0.43717421775412324)),
clientModel = Just "\DEL\1071737",
clientCapabilities = ClientCapabilityList Set.empty,
clientMLSPublicKeys = mempty
clientMLSPublicKeys = mempty,
clientLastActive = Nothing
}

testObject_Client_user_19 :: Client
Expand All @@ -311,7 +329,8 @@ testObject_Client_user_19 =
clientLocation = Just (location (Latitude (-1.4630309786758076)) (Longitude (-0.5295690632216867))),
clientModel = Just "",
clientCapabilities = ClientCapabilityList Set.empty,
clientMLSPublicKeys = mempty
clientMLSPublicKeys = mempty,
clientLastActive = Nothing
}

testObject_Client_user_20 :: Client
Expand All @@ -326,5 +345,6 @@ testObject_Client_user_20 =
clientLocation = Just (location (Latitude 2.8672347564452996) (Longitude (-0.9990390825956594))),
clientModel = Nothing,
clientCapabilities = ClientCapabilityList (Set.fromList [ClientSupportsLegalholdImplicitConsent]),
clientMLSPublicKeys = mempty
clientMLSPublicKeys = mempty,
clientLastActive = Nothing
}
1 change: 1 addition & 0 deletions libs/wire-api/test/golden/testObject_Client_user_3.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"cookie": "",
"id": "1",
"label": "pi",
"last_active": "2023-07-04T09:35:32Z",
"location": {
"lat": -0.31865405026910076,
"lon": 6.859482454480745e-2
Expand Down
Loading