Skip to content

Commit

Permalink
fix: fix user avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
brian030128 committed Jun 22, 2024
1 parent 890c274 commit 91876cd
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 12 deletions.
1 change: 1 addition & 0 deletions Dockerfile.media
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ RUN go build -o /app /usr/src/app/cmd/media_service/main.go
FROM alpine:3.19
COPY --from=build /app /app
EXPOSE 8080
EXPOSE 8081
CMD ["/app"]


4 changes: 2 additions & 2 deletions migrations/2406170339_create_image_table.up.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
CREATE TABLE tmp_file(
fileId uuid PRIMARY KEY,
file_id uuid PRIMARY KEY,
path varchar(100) NOT NULL ,
expected_usage int8 NOT NULL ,
uploader uuid references user_identity(user_id) NOT NULL,
uploaded_at timestamp NOT NULL
);

CREATE TABLE confirmed_file(
fileId uuid PRIMARY KEY,
file_id uuid PRIMARY KEY,
path varchar(100) NOT NULL ,
usage int8 NOT NULL ,
uploader uuid references user_identity(user_id) NOT NULL,
Expand Down
4 changes: 3 additions & 1 deletion migrations/2406210105_default_avatar.down.sql
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
ALTER TABLE user_identity DROP COLUMN avatar_url;
ALTER TABLE user_identity DROP COLUMN avatar_id;
DELETE FROM confirmed_file WHERE file_id = '00000000-0000-0000-0000-000000000000';
DELETE FROM user_identity WHERE user_id = '00000000-0000-0000-0000-000000000000';
19 changes: 18 additions & 1 deletion migrations/2406210105_default_avatar.up.sql
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
ALTER TABLE user_identity ADD COLUMN avatar_url VARCHAR(255) DEFAULT '/default_avatar.png' NOT NULL;
INSERT INTO user_identity(user_id, name) VALUES ('00000000-0000-0000-0000-000000000000', 'system');
INSERT INTO confirmed_file(
file_id,
path,
usage,
uploader,
uploaded_at,
confirmed_at
) VALUES (
'00000000-0000-0000-0000-000000000000',
'/default_avatar.png',
1,
'00000000-0000-0000-0000-000000000000',
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP
);

ALTER TABLE user_identity ADD COLUMN avatar_id uuid DEFAULT '00000000-0000-0000-0000-000000000000' NOT NULL;
3 changes: 2 additions & 1 deletion services/api/controllers/group/get_members.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ func (s Service) GetGroupMembers(ctx context.Context, req *monify.GetGroupMember

fileService := ctx.Value(lib.FileServiceContextKey{}).(infra.FileService)
rows, err := db.QueryContext(ctx, `
SELECT gm.user_id, gm.group_member_id, ui.name, ui.avatar_url
SELECT gm.user_id, gm.group_member_id, ui.name, cf.path
FROM group_member gm
LEFT JOIN user_identity ui on gm.user_id = ui.user_id
LEFT JOIN confirmed_file cf on ui.avatar_id = cf.file_id
WHERE group_id = $1
`, groupId)
if err != nil {
Expand Down
9 changes: 8 additions & 1 deletion services/api/controllers/user/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"google.golang.org/grpc/status"
"monify/lib"
monify "monify/protobuf/gen/go"
"monify/services/api/infra"
)

func (Service) GetUserInfo(ctx context.Context, req *monify.GetUserInfoRequest) (*monify.GetUserInfoResponse, error) {
Expand All @@ -20,13 +21,19 @@ func (Service) GetUserInfo(ctx context.Context, req *monify.GetUserInfoRequest)
logger := ctx.Value(lib.LoggerContextKey{}).(*zap.Logger)
response := monify.GetUserInfoResponse{}

err := db.QueryRowContext(ctx, "SELECT name, avatar_url FROM user_identity WHERE user_id = $1", userId).Scan(&response.Name, &response.AvatarUrl)
err := db.QueryRowContext(ctx, `
SELECT name, cf.path
FROM user_identity
LEFT JOIN confirmed_file cf on user_identity.avatar_id = cf.file_id
WHERE user_id = $1`, userId).Scan(&response.Name, &response.AvatarUrl)
if err != nil {
if err == sql.ErrNoRows {
return nil, status.Error(codes.NotFound, "Not found")
}
logger.Error("failed to get user info", zap.Error(err))
return nil, status.Error(codes.Internal, "Internal")
}
fileService := ctx.Value(lib.FileServiceContextKey{}).(infra.FileService)
response.AvatarUrl = fileService.GetUrl(response.AvatarUrl)
return &response, nil
}
11 changes: 10 additions & 1 deletion services/api/infra/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"database/sql"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
monify "monify/protobuf/gen/go"
)

type Resources struct {
Expand All @@ -24,10 +26,17 @@ func SetupResources(config Config) Resources {
panic(err)
}

grpc.NewClient("")
conn, err := grpc.NewClient("media_service:8081", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
panic(err)
}
return Resources{
DBConn: dbConn,
Logger: logger,
KafkaWriters: kafkaWriters,
FileService: FileService{
config: config,
client: monify.NewMediaServiceClient(conn),
},
}
}
2 changes: 1 addition & 1 deletion services/media/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,6 @@ func extractFileNameSuffix(fileName string) string {

func StoreTmpFile(ctx context.Context, file media.TmpFile) error {
db := ctx.Value(lib.DatabaseContextKey{}).(*sql.DB)
_, err := db.ExecContext(ctx, "INSERT INTO tmp_file (fileid, path, uploader, expected_usage, uploaded_at) VALUES ($1, $2, $3, $4, $5)", file.Id, file.Path, file.Uploader, file.ExpectedUsage, file.UploadedAt)
_, err := db.ExecContext(ctx, "INSERT INTO tmp_file (file_id, path, uploader, expected_usage, uploaded_at) VALUES ($1, $2, $3, $4, $5)", file.Id, file.Path, file.Uploader, file.ExpectedUsage, file.UploadedAt)
return err
}
8 changes: 4 additions & 4 deletions services/media/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@ type Service struct {
func (Service) ConfirmImageUsage(ctx context.Context, req *monify.ConfirmFileUsageRequest) (*monify.MEmpty, error) {
db := ctx.Value(lib.DatabaseContextKey{}).(*sql.DB)
tmpImage := media.TmpFile{}
err := db.QueryRowContext(ctx, "SELECT path, expected_usage, uploader, uploaded_at FROM tmp_file WHERE fileid = $1", req.FileId).Scan(&tmpImage.Path, &tmpImage.ExpectedUsage, &tmpImage.Uploader, &tmpImage.UploadedAt)
err := db.QueryRowContext(ctx, "SELECT path, expected_usage, uploader, uploaded_at FROM tmp_file WHERE file_id = $1", req.FileId).Scan(&tmpImage.Path, &tmpImage.ExpectedUsage, &tmpImage.Uploader, &tmpImage.UploadedAt)
if err != nil {
if err == sql.ErrNoRows {
return nil, status.Errorf(codes.NotFound, "image not found")
}
return nil, status.Errorf(codes.Internal, "error getting image: %v", err)
}

_, err = db.ExecContext(ctx, "INSERT INTO confirmed_file (fileid, path, \"usage\", uploader, uploaded_at, confirmed_at) VALUES ($1, $2, $3, $4, $5, $6)", req.FileId, tmpImage.Path, tmpImage.ExpectedUsage, tmpImage.Uploader, tmpImage.UploadedAt, time.Now())
_, err = db.ExecContext(ctx, "INSERT INTO confirmed_file (file_id, path, \"usage\", uploader, uploaded_at, confirmed_at) VALUES ($1, $2, $3, $4, $5, $6)", req.FileId, tmpImage.Path, tmpImage.ExpectedUsage, tmpImage.Uploader, tmpImage.UploadedAt, time.Now())
if err != nil {
return nil, status.Errorf(codes.Internal, "error inserting image: %v", err)
}
_, _ = db.ExecContext(ctx, "DELETE FROM tmp_file WHERE fileid = $1", req.FileId)
_, _ = db.ExecContext(ctx, "DELETE FROM tmp_file WHERE file_id = $1", req.FileId)
return &monify.MEmpty{}, nil
}

func (Service) GetFileUrl(ctx context.Context, req *monify.GetFileUrlRequest) (*monify.GetFileUrlResponse, error) {
db := ctx.Value(lib.DatabaseContextKey{}).(*sql.DB)
var path string
err := db.QueryRowContext(ctx, "SELECT path FROM confirmed_file WHERE fileid = $1", req.FileId).Scan(&path)
err := db.QueryRowContext(ctx, "SELECT path FROM confirmed_file WHERE file_id = $1", req.FileId).Scan(&path)
if err != nil {
if err == sql.ErrNoRows {
return nil, status.Errorf(codes.NotFound, "image not found")
Expand Down

0 comments on commit 91876cd

Please sign in to comment.