Skip to content

Commit

Permalink
feat: introduce a new endpoint for UI metadata (#92)
Browse files Browse the repository at this point in the history
* feat: introduce a new endpoint for UI meta

* rename ext n1ql -> sql for code highlighting

* format all sql files

* tidy
  • Loading branch information
ayoubfaouzi authored Oct 11, 2024
1 parent e390411 commit e82aa96
Show file tree
Hide file tree
Showing 55 changed files with 713 additions and 518 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
# Dependency directories (remove the comment below to include it)
# vendor/

# npm
node_modules/
package.json
package-lock.json

# Saferwall ignore list.
main
cmd/__debug_bin
private.env
private.env
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@ repos:
files: \.(yaml|yml)$
types: [file, yaml]
entry: yamllint -c .yamllint.yaml
# - repo: https://github.com/sql-formatter-org/sql-formatter
# rev: v15.4.3
# hooks:
# - id: sql-formatter
# files: \.(sql|n1ql)$
# entry: sql-formatter --fix
# language: node
# types: [sql]
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,9 @@ generate/doc: ## Generate OpenAPI spec.

old='"{}":' && new="- {}:" \
&& sed -i "s|$$old|$$new|g" ${ROOT_DIR}/docs/docs.go

format/sql: ## Format N1QL files.
# npm install sql-formatter
@for file in ./db/*.sql; do \
npx sql-formatter $$file -l n1ql --fix; \
done
6 changes: 0 additions & 6 deletions db/action-follow.n1ql

This file was deleted.

10 changes: 10 additions & 0 deletions db/action-follow.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* N1QL query to prepend a username to the user's following. */
UPDATE `bucket_name`
USE KEYS LOWER($user)
SET
`DYNAMIC_FIELD` = ARRAY_PREPEND(
{"username": $username, "ts": $ts},
`DYNAMIC_FIELD`
)
WHERE
NOT ANY item IN `DYNAMIC_FIELD` SATISFIES item.username = $username END;
6 changes: 0 additions & 6 deletions db/action-like.n1ql

This file was deleted.

7 changes: 7 additions & 0 deletions db/action-like.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* N1QL query to prepend a liked file from a user's likes. */
UPDATE `bucket_name`
USE KEYS $user
SET
likes = ARRAY_PREPEND({"sha256": $sha256, "ts": $ts}, likes)
WHERE
NOT ANY item IN likes SATISFIES item.sha256 = $sha256 END;
7 changes: 0 additions & 7 deletions db/action-unfollow.n1ql

This file was deleted.

7 changes: 7 additions & 0 deletions db/action-unfollow.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* N1QL query to remove a user from a user's following. */
UPDATE `bucket_name`
USE KEYS LOWER($user)
SET
`DYNAMIC_FIELD` = ARRAY item FOR item IN `DYNAMIC_FIELD` WHEN item.username != $username END
WHERE
ANY item IN `DYNAMIC_FIELD` SATISFIES item.username = $username END;
7 changes: 0 additions & 7 deletions db/action-unlike.n1ql

This file was deleted.

7 changes: 7 additions & 0 deletions db/action-unlike.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* N1QL query to remove a liked file from a user's likes. */
UPDATE `bucket_name`
USE KEYS $user
SET
likes = ARRAY like_item FOR like_item IN likes WHEN like_item.sha256 != $sha256 END
WHERE
ANY like_item IN likes SATISFIES like_item.sha256 = $sha256 END;
60 changes: 0 additions & 60 deletions db/ano-user-activities.n1ql

This file was deleted.

64 changes: 64 additions & 0 deletions db/ano-user-activities.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* N1QL query to retrieve activities for an anonymous user. */
WITH
activity_data AS (
SELECT
activity.kind,
META(activity).id AS activity_id,
activity.username,
activity.timestamp,
activity.target
FROM
`bucket_name` AS activity
WHERE
activity.type = 'activity'
AND activity.src = "web"
AND activity.kind != "comment"
ORDER BY
activity.timestamp DESC
OFFSET
$offset
LIMIT
$limit
)
SELECT
{
"type": activity.kind,
"id": activity.activity_id,
"author": {
"username": activity.username,
"member_since": (
SELECT
RAW u.member_since
FROM
`bucket_name` AS u
USE KEYS LOWER(activity.username)
) [0]
},
"follow": false,
"date": activity.timestamp
}.*,
(
CASE
WHEN activity.kind = "follow" THEN {"target": activity.target}
ELSE {
"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)
}
}
}
END
).*
FROM
activity_data AS activity
LEFT JOIN `bucket_name` AS f ON KEYS activity.target;
34 changes: 0 additions & 34 deletions db/ano-user-comments.n1ql

This file was deleted.

35 changes: 35 additions & 0 deletions db/ano-user-comments.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* N1QL query to retrieve user's comments for an anonymous user. */
SELECT
{
"id": META(c).id,
"comment": c.body,
"liked": false,
"date": c.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)
}
}
}.*
FROM
`bucket_name` c
LEFT JOIN `bucket_name` f ON KEYS c.sha256
WHERE
c.`type` = 'comment'
AND c.`username` = $user
ORDER BY
c.timestamp DESC
OFFSET
$offset
LIMIT
$limit
14 changes: 0 additions & 14 deletions db/ano-user-followers.n1ql

This file was deleted.

23 changes: 23 additions & 0 deletions db/ano-user-followers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* 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
}
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
13 changes: 0 additions & 13 deletions db/ano-user-following.n1ql

This file was deleted.

23 changes: 23 additions & 0 deletions db/ano-user-following.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* N1QL query to retrieve users' following for an anonymous user. */
SELECT
RAW {
"id": META(p).id,
"username": p.`username`,
"member_since": p.`member_since`,
"follow": FALSE
}
FROM
`bucket_name` p
USE KEYS [
(
SELECT
RAW ARRAY LOWER(u.username) FOR u IN s.`following` END
FROM
`bucket_name` s
USE KEYS $user
) [0]
]
OFFSET
$offset
LIMIT
$limit
Loading

0 comments on commit e82aa96

Please sign in to comment.