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

feat: user uploads is now nested on user document #86

Merged
merged 2 commits into from
Sep 28, 2024
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
2 changes: 1 addition & 1 deletion db/ano-user-likes.n1ql
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ FROM
$user
UNNEST s.likes AS userLikes
) AS l
LEFT JOIN `bucket_name` f ON f.sha256 = l.sha256
LEFT JOIN `bucket_name` f ON f.sha256 = l.sha256
WHERE f.`type` = "file"
OFFSET $offset
LIMIT
Expand Down
57 changes: 30 additions & 27 deletions db/ano-user-submissions.n1ql
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
/* N1QL query to retrieve user submissions for an anonymous user. */

/* N1QL query to retrieve likes for an anonymous user. */
SELECT
{
"id": META(a).id,
"liked": false,
"date": a.timestamp,
{
"id": UUID(),
"date": l.ts,
"liked": FALSE,
"file": {
"hash": f.sha256,
"tags": f.tags,
"filename": f.submissions [0].filename,
"class": f.ml.pe.predicted_class,
"multiav": {
"value": ARRAY_COUNT(
ARRAY_FLATTEN(
ARRAY i.infected FOR i IN OBJECT_VALUES(f.multiav.last_scan) WHEN i.infected = TRUE END,
1
)
),
"count": OBJECT_LENGTH(f.multiav.last_scan)
}
"hash": f.sha256,
"tags": f.tags,
"filename": f.submissions [0].filename,
"class": f.ml.pe.predicted_class,
"multiav": {
"value": ARRAY_COUNT(
ARRAY_FLATTEN(
ARRAY i.infected FOR i IN OBJECT_VALUES(f.multiav.last_scan) WHEN i.infected = TRUE END,
1
)
),
"count": OBJECT_LENGTH(f.multiav.last_scan)
}
}
}.*
}.*
FROM
`bucket_name` a
LEFT JOIN `bucket_name` f
ON KEYS a.target
WHERE
a.`type` = 'activity'
AND LOWER(a.`username`) = $user
AND a.`kind` = "submit"
ORDER BY
a.timestamp DESC
(
SELECT
userSubmissions.*
FROM
`bucket_name` s
USE KEYS
$user
UNNEST s.submissions AS userSubmissions
) AS l
LEFT JOIN `bucket_name` f ON f.sha256 = l.sha256
WHERE f.`type` = "file"
OFFSET $offset
LIMIT
$limit
5 changes: 3 additions & 2 deletions db/user-likes.n1ql
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ FROM
$user
UNNEST s.likes AS userLikes
) AS l
LEFT JOIN `bucket_name` f ON f.sha256 = l.sha256
WHERE f.`type` = "file"OFFSET $offset
LEFT JOIN `bucket_name` f ON f.sha256 = l.sha256
WHERE f.`type` = "file"
OFFSET $offset
LIMIT
$limit
77 changes: 39 additions & 38 deletions db/user-submissions.n1ql
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,49 @@

SELECT
{
"id": META(a).id,
"liked": ARRAY_BINARY_SEARCH(
ARRAY_SORT(
(
SELECT RAW
n.`target`
FROM
`bucket_name` n
WHERE n.`type` = "activity"
AND n.`kind` = "submit"
AND n.`username` = $loggedInUser
"id": UUID(),
"date": l.ts,
"liked": ARRAY_BINARY_SEARCH(
ARRAY_SORT(
(
SELECT RAW
ARRAY like_item.sha256 FOR like_item IN u.`likes` END AS sha256_array
FROM
`bucket_name` u
USE KEYS
$loggedInUser
)[0]
),
f.sha256
) >= 0,
"file": {
"hash": f.sha256,
"tags": f.tags,
"filename": f.submissions [0].filename,
"class": f.ml.pe.predicted_class,
"multiav": {
"value": ARRAY_COUNT(
ARRAY_FLATTEN(
ARRAY i.infected FOR i IN OBJECT_VALUES(f.multiav.last_scan) WHEN i.infected = TRUE END,
1
)
),
f.sha256
) >= 0,
"date": a.timestamp,
"file": {
"hash": f.sha256,
"tags": f.tags,
"filename": f.submissions [0].filename,
"class": f.ml.pe.predicted_class,
"multiav": {
"value": ARRAY_COUNT(
ARRAY_FLATTEN(
ARRAY i.infected
FOR i IN OBJECT_VALUES(f.multiav.last_scan)
WHEN i.infected = TRUE END, 1
)
),
"count": OBJECT_LENGTH(f.multiav.last_scan)
}
"count": OBJECT_LENGTH(f.multiav.last_scan)
}
}.*
}
}.*
FROM
`bucket_name` a
LEFT JOIN `bucket_name` f ON KEYS a.target
WHERE
a.`type` = 'activity'
AND LOWER(a.`username`) = $user
AND a.`kind` = "submit"
ORDER BY
a.timestamp DESC
(
SELECT
userSubmissions.*
FROM
`bucket_name` s
USE KEYS
$user
UNNEST s.submissions AS userSubmissions
) AS l
LEFT JOIN `bucket_name` f ON f.sha256 = l.sha256
WHERE f.`type` = "file"
OFFSET $offset
LIMIT
$limit
44 changes: 25 additions & 19 deletions internal/entity/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,33 @@ type UserLike struct {
Timestamp int64 `json:"ts"`
}

// UserSubmissions represents file uploads by a user.
type UserSubmission struct {
SHA256 string `json:"sha256"`
Timestamp int64 `json:"ts"`
}

// User represents a user.
type User struct {
Type string `json:"type"`
Email string `json:"email,omitempty"`
Username string `json:"username"`
Password string `json:"password,omitempty"`
FullName string `json:"name"`
Location string `json:"location"`
URL string `json:"url"`
Bio string `json:"bio"`
Confirmed bool `json:"confirmed"`
MemberSince int64 `json:"member_since"`
LastSeen int64 `json:"last_seen"`
Admin bool `json:"admin"`
Following []string `json:"following"`
FollowingCount int `json:"following_count"`
Followers []string `json:"followers"`
FollowersCount int `json:"followers_count"`
Likes []UserLike `json:"likes"`
SubmissionsCount int `json:"submissions_count"`
CommentsCount int `json:"comments_count"`
Type string `json:"type"`
Email string `json:"email,omitempty"`
Username string `json:"username"`
Password string `json:"password,omitempty"`
FullName string `json:"name"`
Location string `json:"location"`
URL string `json:"url"`
Bio string `json:"bio"`
Confirmed bool `json:"confirmed"`
MemberSince int64 `json:"member_since"`
LastSeen int64 `json:"last_seen"`
Admin bool `json:"admin"`
Following []string `json:"following"`
FollowingCount int `json:"following_count"`
Followers []string `json:"followers"`
FollowersCount int `json:"followers_count"`
Likes []UserLike `json:"likes"`
Submissions []UserSubmission `json:"submissions"`
CommentsCount int `json:"comments_count"`
}

// UserPrivate represent a user with sensitive fields included.
Expand Down
6 changes: 0 additions & 6 deletions internal/file/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,6 @@ func (s service) Create(ctx context.Context, req CreateFileRequest) (
return File{}, err
}

// Update submissions count on user object.
err = s.Patch(ctx, user.ID(), "submissions_count", user.SubmissionsCount+1)
if err != nil {
return File{}, err
}

return s.Get(ctx, sha256, nil)

} else {
Expand Down
13 changes: 12 additions & 1 deletion internal/user/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,18 @@

// Always hide the password.
user.Password = ""
return c.JSON(http.StatusOK, user)

// Create a new struct dynamically including LikesCount
response := struct {
User
LikesCount int `json:"likes_count"`
SubmissionsCount int `json:"submissions_count"`
}{
User: user,
LikesCount: len(user.Likes),
SubmissionsCount: len(user.Submissions),
}
return c.JSON(http.StatusOK, response)
}

// @Summary Create a new user
Expand Down Expand Up @@ -164,7 +175,7 @@
return err
}

go r.mailer.Send(body.String(),

Check failure on line 178 in internal/user/api.go

View workflow job for this annotation

GitHub Actions / Build & Test Go Package (1.21.x, ubuntu-latest)

Error return value of `r.mailer.Send` is not checked (errcheck)
confirmAccountTpl.Subject, confirmAccountTpl.From, user.Email)

return c.JSON(http.StatusCreated, user)
Expand Down
2 changes: 1 addition & 1 deletion internal/user/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ func (s service) CountSubmissions(ctx context.Context, id string) (int, error) {
if err != nil {
return 0, err
}
return user.SubmissionsCount, err
return len(user.Submissions), err
}

func (s service) Follow(ctx context.Context, id string) error {
Expand Down
Loading