add new event types
This commit is contained in:
@@ -2,10 +2,15 @@ package httpRequest
|
||||
|
||||
import (
|
||||
json2 "encoding/json"
|
||||
"maps"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"go-socket/packages/Enums/WsEventType"
|
||||
"go-socket/packages/postgresql"
|
||||
"go-socket/packages/types"
|
||||
"net/http"
|
||||
"strings"
|
||||
"go-socket/packages/wsServer"
|
||||
|
||||
"go-socket/packages/config"
|
||||
"go-socket/packages/convertions"
|
||||
@@ -100,21 +105,39 @@ func HandleSetUserAvatar(response http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if user.AvatarUrl != "" {
|
||||
if err = minio.Delete(ctx, user.AvatarUrl); err != nil {
|
||||
if user.AvatarType != "" {
|
||||
if err = minio.Delete(ctx, user.AvatarType); err != nil {
|
||||
minio.Delete(ctx, key)
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
user.AvatarUrl = key
|
||||
user.AvatarType = key
|
||||
err = postgresql.UserUpdateProfile(ctx, user, &types.UserProfileUpdateList{Avatar: true})
|
||||
if err != nil {
|
||||
http.Error(response, "failed to update user avatar", http.StatusInternalServerError)
|
||||
minio.Delete(ctx, user.AvatarUrl)
|
||||
minio.Delete(ctx, user.AvatarType)
|
||||
return
|
||||
}
|
||||
|
||||
user.Mu.RLock()
|
||||
connections := slices.Collect(maps.Values(user.Connections))
|
||||
user.Mu.RUnlock()
|
||||
|
||||
for _, conn := range connections {
|
||||
targetId := conn.GetSecondUser(user.Id)
|
||||
target, err := getUserById(ctx, targetId)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
wsServer.WsSendMessageCloseIfTimeout(target, types.WsEventMessage{
|
||||
Type: WsEventType.UserAvatarChange,
|
||||
Event: &map[string]any{
|
||||
"userId": user.Id,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
response.WriteHeader(http.StatusCreated)
|
||||
}
|
||||
|
||||
@@ -142,12 +165,12 @@ func HandleGetUserAvatar(response http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if target.AvatarUrl == "" {
|
||||
if target.AvatarType == "" {
|
||||
http.Error(response, "user have no avatar", http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
url, meta, err := minio.GetDownloadUrlAndMetadata(ctx, target.AvatarUrl)
|
||||
url, meta, err := minio.GetDownloadUrlAndMetadata(ctx, target.AvatarType)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
@@ -203,21 +226,38 @@ func HandleSetUserProfileBg(response http.ResponseWriter, request *http.Request)
|
||||
return
|
||||
}
|
||||
|
||||
if user.ProfileBgUrl != "" {
|
||||
if err = minio.Delete(ctx, user.ProfileBgUrl); err != nil {
|
||||
if user.ProfileBgType != "" {
|
||||
if err = minio.Delete(ctx, user.ProfileBgType); err != nil {
|
||||
minio.Delete(ctx, key)
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
user.ProfileBgUrl = key
|
||||
user.ProfileBgType = key
|
||||
err = postgresql.UserUpdateProfile(ctx, user, &types.UserProfileUpdateList{ProfileBg: true})
|
||||
if err != nil {
|
||||
http.Error(response, "failed to update user profile background", http.StatusInternalServerError)
|
||||
minio.Delete(ctx, user.ProfileBgUrl)
|
||||
minio.Delete(ctx, user.ProfileBgType)
|
||||
return
|
||||
}
|
||||
|
||||
user.Mu.RLock()
|
||||
connections := slices.Collect(maps.Values(user.Connections))
|
||||
user.Mu.RUnlock()
|
||||
|
||||
for _, conn := range connections {
|
||||
target, err := getUserById(ctx, conn.GetSecondUser(user.Id))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
wsServer.WsSendMessageCloseIfTimeout(target, types.WsEventMessage{
|
||||
Type: WsEventType.UserProfileBgChange,
|
||||
Event: &map[string]any{
|
||||
"userId": user.Id,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
response.WriteHeader(http.StatusCreated)
|
||||
}
|
||||
|
||||
@@ -245,12 +285,12 @@ func HandleGetUserProfileBg(response http.ResponseWriter, request *http.Request)
|
||||
return
|
||||
}
|
||||
|
||||
if target.ProfileBgUrl == "" {
|
||||
if target.ProfileBgType == "" {
|
||||
http.Error(response, "user have no profile background", http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
url, meta, err := minio.GetDownloadUrlAndMetadata(ctx, target.ProfileBgUrl)
|
||||
url, meta, err := minio.GetDownloadUrlAndMetadata(ctx, target.ProfileBgType)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user