diff --git a/cmd/webapi/cors.go b/cmd/webapi/cors.go
index 9bcec05..737e4f4 100644
--- a/cmd/webapi/cors.go
+++ b/cmd/webapi/cors.go
@@ -1,8 +1,9 @@
package main
import (
- "github.com/gorilla/handlers"
"net/http"
+
+ "github.com/gorilla/handlers"
)
// applyCORSHandler applies a CORS policy to the router. CORS stands for Cross-Origin Resource Sharing: it's a security
@@ -11,8 +12,10 @@ import (
func applyCORSHandler(h http.Handler) http.Handler {
return handlers.CORS(
handlers.AllowedHeaders([]string{
- "x-example-header",
+ "Content-Type",
+ "Authorization",
}),
+ handlers.AllowCredentials(),
handlers.AllowedMethods([]string{"GET", "POST", "OPTIONS", "DELETE", "PUT"}),
handlers.AllowedOrigins([]string{"*"}),
)(h)
diff --git a/cmd/webapi/data/photos/5befc587-dd9a-4d8e-a617-d4a462eed596/1.jpg b/cmd/webapi/data/photos/5befc587-dd9a-4d8e-a617-d4a462eed596/1.jpg
new file mode 100644
index 0000000..74be9da
Binary files /dev/null and b/cmd/webapi/data/photos/5befc587-dd9a-4d8e-a617-d4a462eed596/1.jpg differ
diff --git a/cmd/webapi/data/photos/5befc587-dd9a-4d8e-a617-d4a462eed596/2.jpg b/cmd/webapi/data/photos/5befc587-dd9a-4d8e-a617-d4a462eed596/2.jpg
new file mode 100644
index 0000000..d0b1ae4
Binary files /dev/null and b/cmd/webapi/data/photos/5befc587-dd9a-4d8e-a617-d4a462eed596/2.jpg differ
diff --git a/cmd/webapi/load-configuration.go b/cmd/webapi/load-configuration.go
index cfd086f..93c91c9 100644
--- a/cmd/webapi/load-configuration.go
+++ b/cmd/webapi/load-configuration.go
@@ -29,7 +29,7 @@ type WebAPIConfiguration struct {
Filename string `conf:"default:./wasaphoto.db"`
}
Data struct {
- Path string `conf:"default:/tmp/wasaphoto"`
+ Path string `conf:"default:./data"`
}
}
diff --git a/cmd/webapi/register-web-ui-stub.go b/cmd/webapi/register-web-ui-stub.go
index a418998..78ded76 100644
--- a/cmd/webapi/register-web-ui-stub.go
+++ b/cmd/webapi/register-web-ui-stub.go
@@ -1,4 +1,4 @@
-// go:build !webui
+//go:build !webui
package main
diff --git a/cmd/webapi/wasaphoto.db b/cmd/webapi/wasaphoto.db
index 965ceb7..7880ef6 100644
Binary files a/cmd/webapi/wasaphoto.db and b/cmd/webapi/wasaphoto.db differ
diff --git a/doc/api.yaml b/doc/api.yaml
index 5f30402..8011072 100644
--- a/doc/api.yaml
+++ b/doc/api.yaml
@@ -112,9 +112,9 @@ paths:
$ref: "#/components/schemas/uid_name"
example:
- user_id: "123e4567-e89b-12d3-a456-426655440000"
- username: "Maria"
+ name: "Maria"
- user_id: "123e4567-e89b-12d3-a456-426655440001"
- username: "Filippo"
+ name: "Filippo"
'404':
description: The user does not exist.
content:
@@ -152,9 +152,9 @@ paths:
$ref: "#/components/schemas/uid_name"
example:
- user_id: "123e4567-e89b-12d3-a456-426655440000"
- username: "Maria"
+ name: "Maria"
- user_id: "123e4567-e89b-12d3-a456-426655440001"
- username: "Filippo"
+ name: "Filippo"
'404':
description: The user does not exist.
content:
@@ -360,9 +360,9 @@ paths:
$ref: "#/components/schemas/uid_name"
example:
- user_id: "123e4567-e89b-12d3-a456-426655440000"
- username: "Maria"
+ name: "Maria"
- user_id: "123e4567-e89b-12d3-a456-426655440001"
- username: "Filippo"
+ name: "Filippo"
'404':
description: The user or the photo does not exist.
content:
@@ -493,9 +493,9 @@ paths:
$ref: "#/components/schemas/uid_name"
example:
- user_id: "123e4567-e89b-12d3-a456-426655440000"
- username: "Maria"
+ name: "Maria"
- user_id: "123e4567-e89b-12d3-a456-426655440001"
- username: "Filippo"
+ name: "Filippo"
'404':
description: No user found.
content:
@@ -544,7 +544,7 @@ paths:
schema:
$ref: "#/components/schemas/user_profile"
example:
- username: "Maria"
+ name: "Maria"
followers: 25
following: 32
followed: true
@@ -881,7 +881,7 @@ paths:
$ref: "#/components/schemas/photo_stream"
example:
- user_id: "1a2b3c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d"
- username: "Federicus"
+ name: "Federicus"
photo_id: 157
upload_time: "2020-11-20T12:00:00Z"
likes: 93
@@ -982,7 +982,7 @@ components:
type: object
description: The profile of the user.
properties:
- username:
+ name:
$ref: "#/components/schemas/name"
followers:
$ref: "#/components/schemas/followers_n"
@@ -1024,7 +1024,7 @@ components:
properties:
user_id:
$ref: "#/components/schemas/uid"
- username:
+ name:
$ref: "#/components/schemas/name"
photo_id:
$ref: "#/components/schemas/photo_id"
diff --git a/open-npm.sh b/open-npm.sh
index 4a1be9f..6da674c 100755
--- a/open-npm.sh
+++ b/open-npm.sh
@@ -1,3 +1,3 @@
#!/usr/bin/env sh
-docker run -it --rm -v "$(pwd):/src" -u "$(id -u):$(id -g)" --network host --workdir /src/webui node:lts /bin/bash
+docker run -it --rm -v "$(pwd):/src" -u "$(id -u):$(id -g)" -p 4173:4173 -p 5173:5173 --workdir /src/webui node:lts /bin/bash
diff --git a/service/api/get-stream.go b/service/api/get-stream.go
index e0fcecd..0c7b320 100644
--- a/service/api/get-stream.go
+++ b/service/api/get-stream.go
@@ -32,7 +32,7 @@ func (rt *_router) GetUserStream(w http.ResponseWriter, r *http.Request, ps http
stream, err := rt.db.GetUserStream(uid, start_index, limit)
if err != nil {
- helpers.SendInternalError(err, "Database error: GetUserProfile", w, rt.baseLogger)
+ helpers.SendInternalError(err, "Database error: GetUserStream", w, rt.baseLogger)
return
}
diff --git a/service/database/db-stream.go b/service/database/db-stream.go
index 48644c4..9e22538 100644
--- a/service/database/db-stream.go
+++ b/service/database/db-stream.go
@@ -8,7 +8,7 @@ import (
func (db *appdbimpl) GetUserStream(uid string, start_index int, limit int) (*[]structures.Photo, error) {
// Get photos from the database
- rows, err := db.c.Query(`SELECT "p"."user", "p"."id", "p"."date",
+ rows, err := db.c.Query(`SELECT "p"."user", "u"."name", "p"."id", "p"."date",
(
SELECT COUNT(*) AS "likes" FROM "likes" AS "l"
WHERE "l"."photo_id" = "p"."id"
@@ -16,9 +16,17 @@ func (db *appdbimpl) GetUserStream(uid string, start_index int, limit int) (*[]s
(
SELECT COUNT(*) AS "comments" FROM "comments" AS "c"
WHERE "c"."photo" = "p"."id"
+ ),
+ (
+ SELECT EXISTS (
+ SELECT * FROM "likes" AS "l"
+ WHERE "l"."photo_id" = "p"."id"
+ AND "l"."user" = ?
+ )
)
- FROM "photos" AS "p"
- WHERE "p"."user" IN (
+ FROM "photos" AS "p", "users" AS "u"
+ WHERE "p"."user" = "u"."uid"
+ AND "p"."user" IN (
SELECT "followed" FROM "follows" WHERE "follower" = ?
)
AND "p"."user" NOT IN (
@@ -26,7 +34,7 @@ func (db *appdbimpl) GetUserStream(uid string, start_index int, limit int) (*[]s
)
ORDER BY "p"."date" DESC
LIMIT ?
- OFFSET ?`, uid, uid, limit, start_index)
+ OFFSET ?`, uid, uid, uid, limit, start_index)
if err != nil {
// Return the error
return nil, err
@@ -39,7 +47,7 @@ func (db *appdbimpl) GetUserStream(uid string, start_index int, limit int) (*[]s
for rows.Next() {
// If there is a next row, we create an instance of Photo and add it to the slice
var photo structures.Photo
- err = rows.Scan(&photo.UID, &photo.ID, &photo.Date, &photo.Likes, &photo.Comments)
+ err = rows.Scan(&photo.UID, &photo.Username, &photo.ID, &photo.Date, &photo.Likes, &photo.Comments, &photo.Liked)
if err != nil {
// Return the error
return nil, err
diff --git a/service/structures/api-structures.go b/service/structures/api-structures.go
index b1f08b4..74a923c 100644
--- a/service/structures/api-structures.go
+++ b/service/structures/api-structures.go
@@ -23,6 +23,7 @@ type Comment struct {
type Photo struct {
UID string `json:"user_id"`
+ Username string `json:"name"`
ID int64 `json:"photo_id"`
Likes int64 `json:"likes"`
Comments int64 `json:"comments"`
diff --git a/webui/index.html b/webui/index.html
index e999568..e3e9658 100644
--- a/webui/index.html
+++ b/webui/index.html
@@ -12,5 +12,6 @@
+