Skip to content

Commit 0b43512

Browse files
committed
Rerun transcripts
1 parent 73242d5 commit 0b43512

9 files changed

Lines changed: 121 additions & 52 deletions

File tree

src/Share/Postgres/Users/Queries.hs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module Share.Postgres.Users.Queries
1515
userByGithubUserId,
1616
userByHandle,
1717
createFromGithubUser,
18+
joinOrgIdsToUserIdsOf,
1819
NewOrPreExisting (..),
1920
getNewOrPreExisting,
2021
isNew,
@@ -274,7 +275,7 @@ findOrCreateGithubUser authZReceipt ghu@(GithubUser _login githubUserId _avatarU
274275
Nothing -> do
275276
New <$> createFromGithubUser authZReceipt ghu primaryEmail userHandle
276277

277-
searchUsersByNameOrHandlePrefix :: Query -> Limit -> PG.Transaction e [(UserLike UserId Void OrgId)]
278+
searchUsersByNameOrHandlePrefix :: Query -> Limit -> PG.Transaction e [(UserLike UserId team OrgId)]
278279
searchUsersByNameOrHandlePrefix (Query prefix) (Limit limit) = do
279280
let q = likeEscape prefix <> "%"
280281
PG.queryListRows @(UserId, Maybe OrgId)
@@ -291,6 +292,27 @@ searchUsersByNameOrHandlePrefix (Query prefix) (Limit limit) = do
291292
Just orgId -> UnifiedOrg orgId
292293
Nothing -> UnifiedUser userId
293294

295+
joinOrgIdsToUserIdsOf :: Traversal s t UserId (UserId, Maybe OrgId) -> s -> PG.Transaction e t
296+
joinOrgIdsToUserIdsOf trav s = do
297+
s
298+
& unsafePartsOf trav %%~ \userIds -> do
299+
let usersTable = zip [0 :: Int32 ..] userIds
300+
PG.queryListRows @(UserId, Maybe OrgId)
301+
[PG.sql|
302+
WITH values(ord, user_id) AS (
303+
SELECT * FROM ^{PG.toTable usersTable}
304+
)
305+
SELECT u.id, org.id
306+
FROM values
307+
LEFT JOIN orgs org ON org.user_id = values.user_id
308+
JOIN users u ON u.id = values.user_id
309+
ORDER BY ord
310+
|]
311+
<&> fmap \(userId, mayOrgId) ->
312+
if length userIds /= length userIds
313+
then error "joinOrgIdsToUserIdsOf: Missing user ids."
314+
else (userId, mayOrgId)
315+
294316
data UserCreationError
295317
= UserHandleTaken UserHandle
296318
| -- A given user handle isn't valid according to Share.
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
1-
module Share.Web.Share.DisplayInfo.Queries (userLikeDisplayInfoOf) where
1+
module Share.Web.Share.DisplayInfo.Queries (userLikeDisplayInfoOf, unifiedDisplayInfoForUserOf) where
22

33
import Control.Lens
4+
import Share.IDs
45
import Share.Postgres (Transaction)
6+
import Share.Postgres.Users.Queries qualified as UserQ
57
import Share.Postgres.Users.Queries qualified as UsersQ
68
import Share.Web.Share.DisplayInfo.Types
79
import Share.Web.Share.Orgs.Queries qualified as OrgsQ
810
import Share.Web.Share.Teams.Queries qualified as TeamsQ
911

10-
userLikeDisplayInfoOf :: s -> Traversal s t UserLikeIds UnifiedDisplayInfo -> Transaction e t
11-
userLikeDisplayInfoOf s trav = do
12+
userLikeDisplayInfoOf :: Traversal s t UserLikeIds UnifiedDisplayInfo -> s -> Transaction e t
13+
userLikeDisplayInfoOf trav s = do
1214
s & unsafePartsOf trav \userLikeIds -> do
1315
withUsers <- userLikeIds & UsersQ.userDisplayInfoOf (traversed . unifiedUser_)
1416
withTeams <- withUsers & TeamsQ.teamDisplayInfoOf (traversed . unifiedTeam_)
1517
withOrgs <- withTeams & OrgsQ.orgDisplayInfoOf (traversed . unifiedOrg_)
1618
pure withOrgs
19+
20+
unifiedDisplayInfoForUserOf :: Traversal s t UserId UnifiedDisplayInfo -> s -> Transaction e t
21+
unifiedDisplayInfoForUserOf trav s = do
22+
s & unsafePartsOf trav \userLikeIds -> do
23+
userLikes <-
24+
UserQ.joinOrgIdsToUserIdsOf traversed userLikeIds
25+
<&> fmap \case
26+
(_userId, Just orgId) -> UnifiedOrg orgId
27+
(userId, Nothing) -> UnifiedUser userId
28+
userLikes & userLikeDisplayInfoOf traversed

src/Share/Web/Share/Impl.hs

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import Share.Utils.API
3838
import Share.Utils.Caching
3939
import Share.Utils.Logging qualified as Logging
4040
import Share.Utils.Servant.Cookies qualified as Cookies
41-
import Share.Utils.URI (URIParam (..))
4241
import Share.Web.App
4342
import Share.Web.Authentication qualified as AuthN
4443
import Share.Web.Authorization qualified as AuthZ
@@ -48,7 +47,7 @@ import Share.Web.Share.Branches.Impl qualified as Branches
4847
import Share.Web.Share.CodeBrowsing.API (CodeBrowseAPI)
4948
import Share.Web.Share.Contributions.Impl qualified as Contributions
5049
import Share.Web.Share.DefinitionSearch qualified as DefinitionSearch
51-
import Share.Web.Share.DisplayInfo.Types (OrgDisplayInfo (..), UserDisplayInfo (..))
50+
import Share.Web.Share.DisplayInfo.Queries qualified as DisplayInfoQ
5251
import Share.Web.Share.Orgs.Queries qualified as OrgQ
5352
import Share.Web.Share.Orgs.Types (Org (..))
5453
import Share.Web.Share.Projects.Impl qualified as Projects
@@ -364,17 +363,12 @@ searchEndpoint (MaybeAuthedUserID callerUserId) (Query query) (fromMaybe (Limit
364363
-- We don't have a great way to order users and projects together, so we just limit to a max
365364
-- of 5 users (who match the query as a prefix), then return the rest of the results from
366365
-- projects.
367-
(users, projects) <- PG.runTransaction $ do
366+
(userLikes, projects) <- PG.runTransaction $ do
368367
userLikes <- UserQ.searchUsersByNameOrHandlePrefix userQuery (Limit 5)
368+
userLikesWithInfo <- DisplayInfoQ.userLikeDisplayInfoOf traversed userLikes
369369
projects <- Q.searchProjects callerUserId projectUserFilter projectQuery limit
370-
userResultsWithOrgInfo <- OrgQ.orgsByIdsOf (traversed . _2 . _Just) users
371-
pure (userResultsWithOrgInfo, projects)
372-
let userResults =
373-
users
374-
<&> \(User {user_name = name, avatar_url = avatarUrl, handle, user_id = userId}, mayOrgInfo) ->
375-
case mayOrgInfo of
376-
Just (Org {orgId, isCommercial}) -> SearchResultOrg (OrgDisplayInfo {user = UserDisplayInfo {handle, name, avatarUrl = unpackURI <$> avatarUrl, userId}, orgId, isCommercial})
377-
Nothing -> SearchResultUserLike (UserDisplayInfo {handle, name, avatarUrl = unpackURI <$> avatarUrl, userId})
370+
pure (userLikesWithInfo, projects)
371+
let userResults = SearchResultUserLike <$> userLikes
378372
let projectResults =
379373
projects
380374
<&> \(Project {slug, summary, visibility}, ownerHandle) ->
@@ -487,23 +481,23 @@ searchDefinitionsEndpoint callerUserId (Query query) mayLimit userFilter project
487481
accountInfoEndpoint :: Session -> WebApp UserAccountInfo
488482
accountInfoEndpoint Session {sessionUserId} = do
489483
User {user_name, avatar_url, user_email, handle, user_id} <- PGO.expectUserById sessionUserId
490-
(completedTours, organizationMemberships, isSuperadmin) <- PG.runTransaction $ do
491-
tours <- Q.getCompletedToursForUser user_id
492-
memberships <- Q.organizationMemberships user_id
484+
PG.runTransaction $ do
485+
completedTours <- Q.getCompletedToursForUser user_id
486+
organizationMemberships <- Q.organizationMemberships user_id
493487
isSuperadmin <- AuthZQ.isSuperadmin user_id
494-
pure (tours, memberships, isSuperadmin)
495-
pure $
496-
UserAccountInfo
497-
{ handle = handle,
498-
name = user_name,
499-
avatarUrl = avatar_url,
500-
userId = user_id,
501-
primaryEmail = user_email,
502-
completedTours,
503-
organizationMemberships,
504-
isSuperadmin,
505-
displayInfo
506-
}
488+
displayInfo <- DisplayInfoQ.unifiedDisplayInfoForUserOf id user_id
489+
pure $
490+
UserAccountInfo
491+
{ handle = handle,
492+
name = user_name,
493+
avatarUrl = avatar_url,
494+
userId = user_id,
495+
primaryEmail = user_email,
496+
completedTours,
497+
organizationMemberships,
498+
isSuperadmin,
499+
displayInfo
500+
}
507501

508502
completeToursEndpoint :: Session -> NonEmpty TourId -> WebApp NoContent
509503
completeToursEndpoint Session {sessionUserId} flows = do

transcripts/share-apis/code-browse/account.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
"body": {
33
"avatarUrl": "https://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?f=y&d=retro",
44
"completedTours": [],
5+
"displayInfo": {
6+
"info": {
7+
"avatarUrl": "https://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?f=y&d=retro",
8+
"handle": "transcripts",
9+
"name": "Transcript User",
10+
"userId": "U-<UUID>"
11+
},
12+
"kind": "user"
13+
},
514
"handle": "transcripts",
615
"isSuperadmin": false,
716
"name": "Transcript User",

transcripts/share-apis/code-browse/search.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
{
22
"body": [
33
{
4-
"avatarUrl": null,
5-
"handle": "test",
6-
"name": null,
7-
"tag": "User",
8-
"userId": "U-<UUID>"
4+
"displayInfo": {
5+
"info": {
6+
"avatarUrl": null,
7+
"handle": "test",
8+
"name": null,
9+
"userId": "U-<UUID>"
10+
},
11+
"kind": "user"
12+
},
13+
"tag": "UserLike"
914
},
1015
{
1116
"projectRef": "@test/publictestproject",

transcripts/share-apis/projects-flow/project-search.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
{
22
"body": [
33
{
4-
"avatarUrl": null,
5-
"handle": "test",
6-
"name": null,
7-
"tag": "User",
8-
"userId": "U-<UUID>"
4+
"displayInfo": {
5+
"info": {
6+
"avatarUrl": null,
7+
"handle": "test",
8+
"name": null,
9+
"userId": "U-<UUID>"
10+
},
11+
"kind": "user"
12+
},
13+
"tag": "UserLike"
914
},
1015
{
1116
"projectRef": "@test/publictestproject",

transcripts/share-apis/search/omni-search-orgs.json

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
{
22
"body": [
33
{
4-
"avatarUrl": "https://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?f=y&d=retro",
5-
"handle": "unison",
6-
"name": "Unison Org",
7-
"orgId": "ORG-<UUID>",
8-
"tag": "Org",
9-
"userId": "U-<UUID>"
4+
"displayInfo": {
5+
"info": {
6+
"isCommercial": false,
7+
"orgId": "ORG-<UUID>",
8+
"user": {
9+
"avatarUrl": "https://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?f=y&d=retro",
10+
"handle": "unison",
11+
"name": "Unison Org",
12+
"userId": "U-<UUID>"
13+
}
14+
},
15+
"kind": "org"
16+
},
17+
"tag": "UserLike"
1018
},
1119
{
1220
"projectRef": "@unison/privateorgproject",

transcripts/share-apis/search/omni-search-users.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
{
22
"body": [
33
{
4-
"avatarUrl": null,
5-
"handle": "test",
6-
"name": null,
7-
"tag": "User",
8-
"userId": "U-<UUID>"
4+
"displayInfo": {
5+
"info": {
6+
"avatarUrl": null,
7+
"handle": "test",
8+
"name": null,
9+
"userId": "U-<UUID>"
10+
},
11+
"kind": "user"
12+
},
13+
"tag": "UserLike"
914
},
1015
{
1116
"projectRef": "@test/publictestproject",

transcripts/share-apis/user-creation/new-user-profile.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
"body": {
33
"avatarUrl": "https://avatars.githubusercontent.com/u/0?v=4",
44
"completedTours": [],
5+
"displayInfo": {
6+
"info": {
7+
"avatarUrl": "https://avatars.githubusercontent.com/u/0?v=4",
8+
"handle": "localgithubuser",
9+
"name": "Local Github User",
10+
"userId": "U-<UUID>"
11+
},
12+
"kind": "user"
13+
},
514
"handle": "localgithubuser",
615
"isSuperadmin": false,
716
"name": "Local Github User",

0 commit comments

Comments
 (0)