Skip to content

Commit

Permalink
feat: refaction action follow/unfollow (#89)
Browse files Browse the repository at this point in the history
* fixed followers get query

* init slices in user entity

* implement action follow / unfollow
  • Loading branch information
ayoubfaouzi authored Sep 29, 2024
1 parent 2f2c683 commit 22d900d
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 44 deletions.
7 changes: 5 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}/cmd",
// "program": "${workspaceFolder}/cmd/migrator",
"env": {
"SFW_WEBAPIS_DEPLOYMENT_KIND": "local"
},
"dlvFlags": ["--check-go-version=false"],
},
"dlvFlags": [
"--check-go-version=false"
],
}
]
}
9 changes: 2 additions & 7 deletions db/action-follow.n1ql
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,5 @@

UPDATE `bucket_name`
USE KEYS LOWER($user)
SET `following` = ARRAY_PREPEND({"username": $username, "ts": $ts}, `following`)
WHERE NOT ANY item IN `following` SATISFIES item.username = $username END;

UPDATE `bucket_name`
USE KEYS LOWER($username)
SET `followers` = ARRAY_PREPEND({"username": $user, "ts": $ts}, `followers`)
WHERE NOT ANY item IN `followers` SATISFIES item.username = $user END;
SET `DYNAMIC_FIELD` = ARRAY_PREPEND({"username": $username, "ts": $ts}, `DYNAMIC_FIELD`)
WHERE NOT ANY item IN `DYNAMIC_FIELD` SATISFIES item.username = $username END;
10 changes: 2 additions & 8 deletions db/action-unfollow.n1ql
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

UPDATE `bucket_name`
USE KEYS LOWER($user)
SET `following` = ARRAY item FOR item IN `following`
SET `DYNAMIC_FIELD` = ARRAY item FOR item IN `DYNAMIC_FIELD`
WHEN item.username != $username END
WHERE ANY item IN `following` SATISFIES item.username = $username END;

UPDATE `bucket_name`
USE KEYS LOWER($username)
SET `followers` = ARRAY item FOR item IN `followers`
WHEN item.username != $user END
WHERE ANY item IN `followers` SATISFIES item.username = $user END;
WHERE ANY item IN `DYNAMIC_FIELD` SATISFIES item.username = $username END;
20 changes: 7 additions & 13 deletions db/ano-user-followers.n1ql
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
/* N1QL query to retrieve user followers for an anonymous user. */

SELECT RAW {
"id": META(p).id,
"username": p.`username`,
"member_since": p.`member_since`,
"follow": false
"id": META(p).id,
"username": p.`username`,
"member_since": p.`member_since`,
"follow": FALSE
}
FROM
`bucket_name` p
USE KEYS [(
SELECT
RAW u.`followers`
FROM
`bucket_name` u
USE KEYS $user
)[0]]
FROM `bucket_name` p USE KEYS [(
SELECT RAW ARRAY LOWER(u.username) FOR u IN s.`followers` END
FROM `bucket_name` s USE KEYS $user )[0]]
OFFSET $offset
LIMIT
$limit
20 changes: 8 additions & 12 deletions db/user-followers.n1ql
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@ SELECT RAW {
"id": META(p).id,
"username": p.`username`,
"member_since": p.`member_since`,
"follow": ARRAY_BINARY_SEARCH(ARRAY_SORT((
SELECT
RAW `following`
FROM
`bucket_name`
USE KEYS $loggedInUser
) [0]), META(p).id) >= 0
"follow": ARRAY_BINARY_SEARCH(ARRAY_SORT((
SELECT RAW ARRAY LOWER(u.username) FOR u IN n.`following` END
FROM `bucket_name` n USE KEYS $loggedInUser ) [0]),
META(p).id) >= 0
}
FROM
`bucket_name` p
USE KEYS [(
SELECT RAW u.`followers` FROM `bucket_name` u USE KEYS $user)[0]]
FROM `bucket_name` p USE KEYS [(
SELECT RAW ARRAY LOWER(u.username) FOR u IN s.`followers` END
FROM `bucket_name` s USE KEYS $user )[0]]
OFFSET $offset
LIMIT
$limit
$limit
27 changes: 25 additions & 2 deletions internal/user/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,19 @@ func (r repository) Follow(ctx context.Context, username string, userFollow enti
params["user"] = username

query := r.db.N1QLQuery[dbcontext.ActionFollow]
return r.db.Query(ctx, query, params, &results)
query = strings.ReplaceAll(query, "DYNAMIC_FIELD", "following")
err := r.db.Query(ctx, query, params, &results)
if err != nil {
return err
}

// As we can't chain two N1QL queries, we execute them sequentially.
query = r.db.N1QLQuery[dbcontext.ActionFollow]
query = strings.ReplaceAll(query, "DYNAMIC_FIELD", "followers")
params["username"] = username
params["user"] = userFollow.Username
return r.db.Query(ctx, query, params, &results)

}

func (r repository) Unfollow(ctx context.Context, username, targetUsername string) error {
Expand All @@ -460,5 +472,16 @@ func (r repository) Unfollow(ctx context.Context, username, targetUsername strin
params["user"] = username

query := r.db.N1QLQuery[dbcontext.ActionUnfollow]
return r.db.Query(ctx, query, params, &results)
query = strings.ReplaceAll(query, "DYNAMIC_FIELD", "following")
err := r.db.Query(ctx, query, params, &results)
if err != nil {
return err
}

// As we can't chain two N1QL queries, we execute them sequentially.
query = r.db.N1QLQuery[dbcontext.ActionUnfollow]
query = strings.ReplaceAll(query, "DYNAMIC_FIELD", "followers")
params["username"] = username
params["user"] = targetUsername
return r.db.Query(ctx, query, params, &results)
}
4 changes: 4 additions & 0 deletions internal/user/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ func (s service) Create(ctx context.Context, req CreateUserRequest) (
Email: strings.ToLower(req.Email),
MemberSince: now.Unix(),
LastSeen: now.Unix(),
Following: []entity.UserFollows{},
Followers: []entity.UserFollows{},
Submissions: []entity.UserSubmission{},
Likes: []entity.UserLike{},
})
if err != nil {
return User{}, err
Expand Down

0 comments on commit 22d900d

Please sign in to comment.