Skip to content

Commit

Permalink
Strongly typed asset keys (#2162)
Browse files Browse the repository at this point in the history
* use assetkey type in imageasset

* fix build integration tests

* moved cql instance for assetkey

* changelog

* tweak changelog.

* asset key cql instance handle invalid keys in db

* comment

* use Icon/AssetKey in team and user profile (wire-api changes only)

* Cassandra Icon instance

* fix integration test

* fix galley integration tests

* fix spar integration tests

* format

Co-authored-by: Matthias Fischmann <mf@zerobuzz.net>
  • Loading branch information
battermann and fisx committed Mar 4, 2022
1 parent f02329b commit ae730d2
Show file tree
Hide file tree
Showing 181 changed files with 654 additions and 708 deletions.
1 change: 1 addition & 0 deletions changelog.d/5-internal/pr-2162
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Asset keys are now internally validated.
6 changes: 5 additions & 1 deletion libs/wire-api/src/Wire/API/Asset.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module Wire.API.Asset
-- * AssetKey
AssetKey (..),
assetKeyToText,
nilAssetKey,

-- * AssetToken
AssetToken (..),
Expand Down Expand Up @@ -176,7 +177,7 @@ assetKeyToText = T.decodeUtf8 . toByteString'

instance ToSchema AssetKey where
schema =
(T.decodeUtf8 . toByteString')
assetKeyToText
.= parsedText "AssetKey" (runParser parser . T.encodeUtf8)
& doc' . S.schema . S.example ?~ toJSON ("3-1-47de4580-ae51-4650-acbb-d10c028cb0ac" :: Text)

Expand All @@ -186,6 +187,9 @@ instance S.ToParamSchema AssetKey where
instance FromHttpApiData AssetKey where
parseUrlPiece = first T.pack . runParser parser . T.encodeUtf8

nilAssetKey :: AssetKey
nilAssetKey = AssetKeyV3 (Id UUID.nil) AssetVolatile

--------------------------------------------------------------------------------
-- AssetToken

Expand Down
29 changes: 15 additions & 14 deletions libs/wire-api/src/Wire/API/Team.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module Wire.API.Team
teamIconKey,
teamBinding,
TeamBinding (..),
Icon (..),

-- * TeamList
TeamList (..),
Expand Down Expand Up @@ -95,15 +96,15 @@ data Team = Team
{ _teamId :: TeamId,
_teamCreator :: UserId,
_teamName :: Text,
_teamIcon :: Text,
_teamIcon :: Icon,
_teamIconKey :: Maybe Text,
_teamBinding :: TeamBinding
}
deriving stock (Eq, Show, Generic)
deriving (Arbitrary) via (GenericUniform Team)
deriving (ToJSON, FromJSON, S.ToSchema) via (Schema Team)

newTeam :: TeamId -> UserId -> Text -> Text -> TeamBinding -> Team
newTeam :: TeamId -> UserId -> Text -> Icon -> TeamBinding -> Team
newTeam tid uid nme ico = Team tid uid nme ico Nothing

modelTeam :: Doc.Model
Expand Down Expand Up @@ -230,14 +231,14 @@ modelNewNonBindingTeam = Doc.defineModel "newNonBindingTeam" $ do

data NewTeam a = NewTeam
{ _newTeamName :: Range 1 256 Text,
_newTeamIcon :: Range 1 256 Text,
_newTeamIcon :: Icon,
_newTeamIconKey :: Maybe (Range 1 256 Text),
_newTeamMembers :: Maybe a
}
deriving stock (Eq, Show, Generic)
deriving (Arbitrary) via (GenericUniform (NewTeam a))

newNewTeam :: Range 1 256 Text -> Range 1 256 Text -> NewTeam a
newNewTeam :: Range 1 256 Text -> Icon -> NewTeam a
newNewTeam nme ico = NewTeam nme ico Nothing Nothing

newTeamObjectSchema :: ValueSchema SwaggerDoc a -> ObjectSchema SwaggerDoc (NewTeam a)
Expand All @@ -251,30 +252,30 @@ newTeamObjectSchema sch =
--------------------------------------------------------------------------------
-- TeamUpdateData

data IconUpdate = IconUpdate AssetKey | DefaultIcon
data Icon = Icon AssetKey | DefaultIcon
deriving stock (Eq, Show, Generic)
deriving (Arbitrary) via (GenericUniform IconUpdate)
deriving (ToJSON, FromJSON, S.ToSchema) via Schema IconUpdate
deriving (Arbitrary) via (GenericUniform Icon)
deriving (ToJSON, FromJSON, S.ToSchema) via Schema Icon

instance FromByteString IconUpdate where
instance FromByteString Icon where
parser =
choice
[ IconUpdate <$> (parser :: Atto.Parser AssetKey),
[ Icon <$> (parser :: Atto.Parser AssetKey),
DefaultIcon <$ Atto.string "default"
]

instance ToByteString IconUpdate where
builder (IconUpdate key) = builder key
instance ToByteString Icon where
builder (Icon key) = builder key
builder DefaultIcon = "default"

instance ToSchema IconUpdate where
instance ToSchema Icon where
schema =
(T.decodeUtf8 . toByteString')
.= parsedText "IconUpdate" (runParser parser . T.encodeUtf8)
.= parsedText "Icon" (runParser parser . T.encodeUtf8)

data TeamUpdateData = TeamUpdateData
{ _nameUpdate :: Maybe (Range 1 256 Text),
_iconUpdate :: Maybe IconUpdate,
_iconUpdate :: Maybe Icon,
_iconKeyUpdate :: Maybe (Range 1 256 Text)
}
deriving stock (Eq, Show, Generic)
Expand Down
3 changes: 2 additions & 1 deletion libs/wire-api/src/Wire/API/User/Profile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import qualified Data.Swagger.Build.Api as Doc
import qualified Data.Text as Text
import Imports
import Wire.API.Arbitrary (Arbitrary (arbitrary), GenericUniform (..))
import Wire.API.Asset (AssetKey (..))
import Wire.API.User.Orphans ()

--------------------------------------------------------------------------------
Expand Down Expand Up @@ -106,7 +107,7 @@ defaultAccentId = ColourId 0

-- Note: Intended to be turned into a sum type to add further asset types.
data Asset = ImageAsset
{ assetKey :: Text,
{ assetKey :: AssetKey,
assetSize :: Maybe AssetSize
}
deriving stock (Eq, Show, Generic)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import Data.Id (BotId (BotId), ClientId (ClientId, client), Id (Id))
import Data.Qualified
import qualified Data.UUID as UUID (fromString)
import Imports (Maybe (Just, Nothing), fromJust, read, (.))
import Wire.API.Asset
import Wire.API.Conversation
import Wire.API.Conversation.Bot
import Wire.API.Conversation.Typing
Expand All @@ -41,7 +42,14 @@ testObject_AddBotResponse_user_1 =
"\77844\129468A\1061088\30365\142096\40918\USc\DC3~0g\ENQr\v\29872\f\154305\1077132u\175940.\1018427v\v-/\bi\bJ\ETXE3\ESC8\53613\1073036\&0@\14466\51733;\27113\SYN\153289\b&\ae]\1042471H\1024555k7\EMJ\1083646[;\140668;J^`0,B\STX\95353N.@Z\v\ENQ\r\19858|'w-\b\157432V\STX \GSW|N\1072850\&3=\22550K245\DC1\142803\168718\7168\147365\ETX"
},
rsAddBotColour = ColourId {fromColourId = -3},
rsAddBotAssets = [ImageAsset "7" Nothing, ImageAsset "" (Just AssetPreview)],
rsAddBotAssets =
[ ImageAsset
(AssetKeyV3 (Id (fromJust (UUID.fromString "5cd81cc4-c643-4e9c-849c-c596a88c27fd"))) AssetExpiring)
Nothing,
ImageAsset
(AssetKeyV3 (Id (fromJust (UUID.fromString "034efa97-f628-450e-b212-009801b1470b"))) AssetExpiring)
(Just AssetPreview)
],
rsAddBotEvent =
Event
(Qualified (Id (fromJust (UUID.fromString "00000001-0000-0000-0000-000200000003"))) (Domain "faraway.example.com"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@
module Test.Wire.API.Golden.Generated.BindingNewTeamUser_user where

import Data.Currency (Alpha (XUA))
import Data.Id (Id (..))
import Data.Range (unsafeRange)
import Imports (Maybe (Just, Nothing))
import Data.UUID as UUID
import Imports (Maybe (Just, Nothing), fromJust)
import Wire.API.Asset
import Wire.API.Team
( BindingNewTeam (BindingNewTeam),
Icon (..),
NewTeam
( NewTeam,
_newTeamIcon,
Expand All @@ -41,9 +45,7 @@ testObject_BindingNewTeamUser_user_1 =
{ _newTeamName =
unsafeRange
"\fe\ENQ\1011760zm\166331\&6+)g;5\989956Z\8196\&41\DC1\n\STX\ETX%|\NULM\996272S=`I\59956UK1\1003466]X\r\SUBa\EM!\74407+\ETXepRw\ACK\ENQ#\127835\1061771\1036174\1018930UX\66821]>i&r\137805\1055913Z\1070413\&6\DC4\DC4\1024114\1058863\1044802\ESC\SYNa4\NUL\1059602\1015948\123628\tLZ\ACKw$=\SYNu\ETXE1\63200C'\ENQ\151764\47003\134542$\100516\1112326\&9;#\1044763\1015439&\ESC\1026916k/\tu\\pk\NUL\STX\1083510)\FS/Lni]Q\NUL\SIZ|=\DC1V]]\FS5\156475U6>(\17233'\CAN\179678%'I1-D\"\1098303\n\78699\npkHY#\NUL\1014868u]\1078674\147414\STX\USj'\993967'\CAN\1042144&\35396E\37802=\135058Da\STX\v\1100351=\1083565V#\993183\RS\FSN#`uny\1003178\1094898\&53#\DEL/|,+\243pW\44721i4j",
_newTeamIcon =
unsafeRange
"Coq\52427\v\182208\&7\SYN\\N\134130\8419h3 \30278;X\STX\a\a$|D\NUL\SOHh'\62853\&3-m7\1078900\SOp\22214`\1093812\&6QF\CAN\SOH9\1062958\ETB\15747FP;lm\1075533\173111\134845\22570n:\rf\1044997\\:\35041\GS\GS\26754\EM\22764i\991235\ETXjj}\1010340~\989348{; \119085\1006542\SUBL&%2\170880;@\\2`gA\984195\&0\162341\&2\163058h\FSuF\DC4\17376\ESC\GS\SO\vYnKy?v\129546H\fcLdBy\170730\&4I\1108995i\1017125\ETBc6f\v\SOH\DC3\1018708ce\1083597\SOs3L&",
_newTeamIcon = DefaultIcon,
_newTeamIconKey =
Just
( unsafeRange
Expand All @@ -64,7 +66,7 @@ testObject_BindingNewTeamUser_user_2 =
{ _newTeamName =
unsafeRange
"G\EOT\DC47\1030077bCy\83226&5\"\96437B$\STX\DC2QJb_\15727\1104659Y \156055\1044397Y\1004994g\v\991186xkJUi\1028168.=-\1054839\&2\1113630U\ESC]\SUB\1091929\DLE}R\157290\DC1\1111740\1096562+R/\1083774\170894p(M\ENQ5Fw<\144133E\1005699R\DLE44\1060383\SO%@FPG\986135JJ\vE\GSz\RS_\tb]0t_Ax}\rt\1057458h\DC3O\ACK\991050`\1038022vm-?$!)~\152722bh\RS\1011653\1007510\&0x \1092001\1078327+)A&mRfL\1109449\ENQ\1049319>K@\US\1006511\ab\vPDWG,\1062888/J~)%7?aRr\989765\&4*^\1035118K*\996771\EM\"\SO\987994\186383l\n\tE\136474\1037228\NAK\a\n\78251c?\\\ENQj\"\ESCpe\98450\NUL=\EM>J",
_newTeamIcon = unsafeRange "\SUB4\NAKF",
_newTeamIcon = Icon (AssetKeyV3 (Id (fromJust (UUID.fromString "55b9ad19-315c-4bda-8c0f-5d7b0e143008"))) AssetEternal),
_newTeamIconKey =
Just
( unsafeRange
Expand Down
Loading

0 comments on commit ae730d2

Please sign in to comment.