Skip to content

Commit 10416b9

Browse files
committed
Fix project/user searches
1 parent c161ac5 commit 10416b9

File tree

7 files changed

+45
-14
lines changed

7 files changed

+45
-14
lines changed

package.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ description: Please see the README on GitHub at <https://github.com/gith
2020
ghc-options:
2121
- -Wall
2222
- -Werror
23-
- -Wno-name-shadowing
23+
- -Wname-shadowing
2424
- -Wno-type-defaults
2525
- -Wno-missing-pattern-synonym-signatures
2626
- -fprint-expanded-synonyms

share-api.cabal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ library
180180
BlockArguments
181181
QuasiQuotes
182182
ImportQualifiedPost
183-
ghc-options: -Wall -Werror -Wno-name-shadowing -Wno-type-defaults -Wno-missing-pattern-synonym-signatures -fprint-expanded-synonyms -fwrite-ide-info -O2 -funbox-strict-fields
183+
ghc-options: -Wall -Werror -Wname-shadowing -Wno-type-defaults -Wno-missing-pattern-synonym-signatures -fprint-expanded-synonyms -fwrite-ide-info -O2 -funbox-strict-fields
184184
build-depends:
185185
Diff
186186
, MonadRandom
@@ -319,7 +319,7 @@ executable share-api
319319
BlockArguments
320320
QuasiQuotes
321321
ImportQualifiedPost
322-
ghc-options: -Wall -Werror -Wno-name-shadowing -Wno-type-defaults -Wno-missing-pattern-synonym-signatures -fprint-expanded-synonyms -fwrite-ide-info -O2 -funbox-strict-fields -threaded -rtsopts "-with-rtsopts=-N -A32m -qn2 -T"
322+
ghc-options: -Wall -Werror -Wname-shadowing -Wno-type-defaults -Wno-missing-pattern-synonym-signatures -fprint-expanded-synonyms -fwrite-ide-info -O2 -funbox-strict-fields -threaded -rtsopts "-with-rtsopts=-N -A32m -qn2 -T"
323323
build-depends:
324324
Diff
325325
, MonadRandom

src/Share/App.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ type CloudApp = AppM ()
2020

2121
instance Logging.MonadLogger (AppM ()) where
2222
logMsg msg = do
23-
log <- asks Env.logger
23+
log' <- asks Env.logger
2424
minSeverity <- asks Env.minLogSeverity
2525
when (Logging.severity msg >= minSeverity) $ do
2626
timestamp <- asks timeCache >>= liftIO
27-
liftIO . log . Logging.logFmtFormatter timestamp $ msg
27+
liftIO . log' . Logging.logFmtFormatter timestamp $ msg
2828

2929
instance Cryptonite.MonadRandom (AppM reqCtx) where
3030
getRandomBytes =

src/Share/Postgres/Queries.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import Share.Web.Share.Releases.Types (ReleaseStatusFilter (..), StatusUpdate (.
3939
import Unison.Util.List qualified as Utils
4040
import Unison.Util.Monoid (intercalateMap)
4141

42-
userByUserId :: PG.QueryM m => UserId -> m (Maybe User)
42+
userByUserId :: (PG.QueryM m) => UserId -> m (Maybe User)
4343
userByUserId uid = do
4444
PG.query1Row
4545
[PG.sql|
@@ -260,8 +260,8 @@ searchUsersByNameOrHandlePrefix (Query prefix) (Limit limit) = do
260260
--
261261
-- The PG.queryListRows accepts strings as web search queries, see
262262
-- https://www.postgresql.org/docs/current/textsearch-controls.html
263-
searchProjectsByUserQuery :: Maybe UserId -> Query -> Limit -> PG.Transaction e [(Project, UserHandle)]
264-
searchProjectsByUserQuery caller (Query query) limit = do
263+
searchProjects :: Maybe UserId -> Maybe UserId -> Query -> Limit -> PG.Transaction e [(Project, UserHandle)]
264+
searchProjects caller userIdFilter (Query query) limit = do
265265
let prefixQuery =
266266
query
267267
-- Remove any chars with special meaning for tsqueries.
@@ -280,6 +280,7 @@ searchProjectsByUserQuery caller (Query query) limit = do
280280
JOIN users AS owner ON p.owner_user_id = owner.id
281281
WHERE (webquery @@ p.project_text_document OR prefixquery @@ p.project_text_document)
282282
AND (NOT p.private OR (#{caller} IS NOT NULL AND EXISTS (SELECT FROM accessible_private_projects WHERE user_id = #{caller} AND project_id = p.id)))
283+
AND (#{userIdFilter} IS NULL OR p.owner_user_id = #{userIdFilter})
283284
ORDER BY (ts_rank_cd(p.project_text_document, webquery), ts_rank_cd(p.project_text_document, prefixquery)) DESC
284285
LIMIT #{limit}
285286
|]
@@ -698,7 +699,7 @@ createBranch !_nlReceipt projectId branchName contributorId causalId mergeTarget
698699
|]
699700

700701
createRelease ::
701-
PG.QueryM m =>
702+
(PG.QueryM m) =>
702703
NameLookupReceipt ->
703704
ProjectId ->
704705
ReleaseVersion ->

src/Share/Web/Share/Impl.hs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66

77
module Share.Web.Share.Impl where
88

9+
import Data.Text qualified as Text
10+
import Servant
911
import Share.Codebase qualified as Codebase
1012
import Share.Codebase.Types qualified as Codebase
11-
import Share.IDs (TourId, UserHandle)
13+
import Share.IDs (TourId, UserHandle (..))
1214
import Share.IDs qualified as IDs
1315
import Share.JWT qualified as JWT
1416
import Share.OAuth.Session
@@ -21,6 +23,7 @@ import Share.Postgres.Users.Queries qualified as UsersQ
2123
import Share.Prelude
2224
import Share.Project (Project (..))
2325
import Share.User (User (..))
26+
import Share.User qualified as User
2427
import Share.UserProfile (UserProfile (..))
2528
import Share.Utils.API
2629
import Share.Utils.Caching
@@ -35,7 +38,6 @@ import Share.Web.Share.CodeBrowsing.API (CodeBrowseAPI)
3538
import Share.Web.Share.Contributions.Impl qualified as Contributions
3639
import Share.Web.Share.Projects.Impl qualified as Projects
3740
import Share.Web.Share.Types
38-
import Servant
3941
import Unison.Codebase.Path qualified as Path
4042
import Unison.HashQualified qualified as HQ
4143
import Unison.Name (Name)
@@ -332,13 +334,23 @@ getUserReadmeEndpoint (AuthN.MaybeAuthedUserID callerUserId) userHandle = do
332334
-- all private users in the PG query itself.
333335
searchEndpoint :: Maybe Session -> Query -> Maybe Limit -> WebApp [SearchResult]
334336
searchEndpoint _caller (Query "") _limit = pure []
335-
searchEndpoint (MaybeAuthedUserID callerUserId) query (fromMaybe (Limit 20) -> limit) = do
337+
searchEndpoint (MaybeAuthedUserID callerUserId) (Query query) (fromMaybe (Limit 20) -> limit) = do
338+
(userQuery :: Query, (projectUserFilter :: Maybe UserId, projectQuery :: Query)) <-
339+
fromMaybe query (Text.stripPrefix "@" query)
340+
& Text.splitOn "/"
341+
& \case
342+
(userQuery : projectQueryText : _rest) -> do
343+
mayUserId <- PG.runTransaction $ fmap User.user_id <$> Q.userByHandle (UserHandle userQuery)
344+
pure (Query query, (mayUserId, Query projectQueryText))
345+
[projectOrUserQuery] -> pure (Query projectOrUserQuery, (Nothing, Query projectOrUserQuery))
346+
-- This is impossible
347+
[] -> pure (Query query, (Nothing, Query query))
336348
-- We don't have a great way to order users and projects together, so we just limit to a max
337349
-- of 5 users (who match the query as a prefix), then return the rest of the results from
338350
-- projects.
339351
(users, projects) <- PG.runTransaction $ do
340-
users <- Q.searchUsersByNameOrHandlePrefix query (Limit 5)
341-
projects <- Q.searchProjectsByUserQuery callerUserId query limit
352+
users <- Q.searchUsersByNameOrHandlePrefix userQuery (Limit 5)
353+
projects <- Q.searchProjects callerUserId projectUserFilter projectQuery limit
342354
pure (users, projects)
343355
let userResults =
344356
users
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"body": [
3+
{
4+
"projectRef": "@test/publictestproject",
5+
"summary": "test project summary",
6+
"tag": "Project",
7+
"visibility": "public"
8+
}
9+
],
10+
"status": [
11+
{
12+
"status_code": 200
13+
}
14+
]
15+
}

transcripts/share-apis/projects-flow/run.zsh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ fetch "$transcript_user" GET project-catalog-get '/catalog'
7878
# Should find projects we have access to (e.g. Unison's private project), but none that we don't.
7979
fetch "$transcript_user" GET project-search '/search?query=test'
8080

81+
# Should filter project search by user if provided a full valid handle:
82+
fetch "$transcript_user" GET project-search-with-user '/search?query=@test/public'
83+
8184
# Transcript user should not find 'test' user's private project
8285
fetch "$transcript_user" GET project-search-inaccessible '/search?query=privatetestproject'
8386

0 commit comments

Comments
 (0)