update naming of functions, add option to get count of unread messages of user
This commit is contained in:
@@ -21,7 +21,7 @@ import (
|
||||
)
|
||||
|
||||
func HandleDm(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -74,10 +74,14 @@ func HandleDm(response http.ResponseWriter, request *http.Request) {
|
||||
Receiver: conn.Id,
|
||||
}
|
||||
|
||||
wsServer.WsSendMessageCloseIfTimeout(target, types.WsEventMessage{
|
||||
Type: WsEventType.DirectMessage,
|
||||
Event: message,
|
||||
})
|
||||
if user.WsConn != nil {
|
||||
wsServer.WsSendMessageCloseIfTimeout(target, types.WsEventMessage{
|
||||
Type: WsEventType.DirectMessage,
|
||||
Event: message,
|
||||
})
|
||||
} else {
|
||||
cache.IncrementConnectionsUnreadMessages(user.Id, conn.Id)
|
||||
}
|
||||
|
||||
err = postgresql.ConnectionMessageSave(ctx, message)
|
||||
if err != nil {
|
||||
@@ -88,8 +92,52 @@ func HandleDm(response http.ResponseWriter, request *http.Request) {
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
||||
func HandleUserGetConnectionsUnreadMessages(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
return
|
||||
}
|
||||
|
||||
ctx := request.Context()
|
||||
user, err := getUserByToken(ctx, request.Header.Get("token"))
|
||||
if err != nil {
|
||||
http.Error(response, "invalid token", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
connectionIds, err := convertions.StringToUuidSlice(request.URL.Query().Get("connections"))
|
||||
if err != nil {
|
||||
http.Error(response, "invalid uuid format", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
result := make([]uint32, len(*connectionIds))
|
||||
|
||||
for _, connId := range *connectionIds {
|
||||
_, ok := cache.GetConnection(user, connId)
|
||||
if !ok {
|
||||
http.Error(response, "no such connection: "+connId.String(), http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
for _, connId := range *connectionIds {
|
||||
count, _ := cache.GetConnectionsUnreadMessages(user.Id, connId)
|
||||
cache.DeallocateConnectionsUnreadMessages(user.Id, connId)
|
||||
result = append(result, count)
|
||||
}
|
||||
|
||||
json, err := json2.Marshal(result)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
response.Write(json)
|
||||
}
|
||||
|
||||
func HandleUserGetConnectionMessages(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -104,7 +152,7 @@ func HandleUserGetConnectionMessages(response http.ResponseWriter, request *http
|
||||
return
|
||||
}
|
||||
|
||||
before, err := convertions.ConvertStringTimestamp(request.URL.Query().Get("before"))
|
||||
before, err := convertions.StringToTimestamp(request.URL.Query().Get("before"))
|
||||
if err != nil {
|
||||
before = time.Now()
|
||||
}
|
||||
@@ -158,7 +206,7 @@ func HandleUserGetConnectionMessages(response http.ResponseWriter, request *http
|
||||
}
|
||||
|
||||
func HandleUserNewConnection(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -167,7 +215,7 @@ func HandleUserNewConnection(response http.ResponseWriter, request *http.Request
|
||||
http.Error(response, "invalid token", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
recipientId, err := convertions.ConvertStringUuid(request.FormValue("recipient"))
|
||||
recipientId, err := convertions.StringToUuid(request.FormValue("recipient"))
|
||||
if err != nil {
|
||||
http.Error(response, "no such user", http.StatusUnauthorized)
|
||||
return
|
||||
@@ -205,7 +253,7 @@ func HandleUserNewConnection(response http.ResponseWriter, request *http.Request
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
cache.CacheAddConnection(requestor, recipient, connection)
|
||||
cache.AddConnection(requestor, recipient, connection)
|
||||
|
||||
wsServer.WsSendMessageCloseIfTimeout(recipient, types.WsEventMessage{
|
||||
Type: WsEventType.ConnectionCreated,
|
||||
@@ -217,7 +265,7 @@ func HandleUserNewConnection(response http.ResponseWriter, request *http.Request
|
||||
}
|
||||
|
||||
func HandleUserDeleteConnection(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -259,7 +307,7 @@ func HandleUserDeleteConnection(response http.ResponseWriter, request *http.Requ
|
||||
return
|
||||
}
|
||||
|
||||
cache.CacheDeleteConnection(user, user2, conn.Id)
|
||||
cache.DeleteConnection(user, user2, conn.Id)
|
||||
wsServer.WsSendMessageCloseIfTimeout(user2, types.WsEventMessage{
|
||||
Type: WsEventType.ConnectionDeleted,
|
||||
Event: conn.Id,
|
||||
@@ -270,7 +318,7 @@ func HandleUserDeleteConnection(response http.ResponseWriter, request *http.Requ
|
||||
}
|
||||
|
||||
func HandleUserElevateConnection(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -330,7 +378,7 @@ func HandleUserElevateConnection(response http.ResponseWriter, request *http.Req
|
||||
}
|
||||
|
||||
func HandleUserDeElevateConnection(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -377,7 +425,7 @@ func HandleUserDeElevateConnection(response http.ResponseWriter, request *http.R
|
||||
}
|
||||
|
||||
func HandleUserGetConnections(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
func HandleAttachmentFileUpload(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, postFile) {
|
||||
if !validCheckWithResponseOnFail(&response, request, file) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -22,9 +22,9 @@ func HandleAttachmentFileUpload(response http.ResponseWriter, request *http.Requ
|
||||
return
|
||||
}
|
||||
|
||||
request.Body = http.MaxBytesReader(response, request.Body, int64(globals.MaxPostWithFileBytes))
|
||||
request.Body = http.MaxBytesReader(response, request.Body, int64(globals.MaxRequestWithFileBytes))
|
||||
|
||||
if err = request.ParseMultipartForm(int64(globals.MaxPostBytes)); err != nil {
|
||||
if err = request.ParseMultipartForm(int64(globals.MaxRequestBytes)); err != nil {
|
||||
http.Error(response, "invalid multipart form", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
@@ -61,7 +61,7 @@ func HandleAttachmentFileUpload(response http.ResponseWriter, request *http.Requ
|
||||
}
|
||||
|
||||
func HandleGetUserAvatar(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -72,7 +72,7 @@ func HandleGetUserAvatar(response http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
targetId, err := convertions.ConvertStringUuid(request.URL.Query().Get("userid"))
|
||||
targetId, err := convertions.StringToUuid(request.URL.Query().Get("userid"))
|
||||
if err != nil {
|
||||
http.Error(response, "invalid userid", http.StatusBadRequest)
|
||||
return
|
||||
@@ -100,7 +100,7 @@ func HandleGetUserAvatar(response http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
|
||||
func HandleGetUserProfileBg(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -110,7 +110,7 @@ func HandleGetUserProfileBg(response http.ResponseWriter, request *http.Request)
|
||||
return
|
||||
}
|
||||
|
||||
targetId, err := convertions.ConvertStringUuid(request.URL.Query().Get("userid"))
|
||||
targetId, err := convertions.StringToUuid(request.URL.Query().Get("userid"))
|
||||
if err != nil {
|
||||
http.Error(response, "invalid userid", http.StatusBadRequest)
|
||||
return
|
||||
@@ -138,7 +138,7 @@ func HandleGetUserProfileBg(response http.ResponseWriter, request *http.Request)
|
||||
}
|
||||
|
||||
func HandleAttachmentFileDownload(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
)
|
||||
|
||||
func getUserById(ctx context.Context, userId uuid.UUID) (*types.User, error) {
|
||||
user, err := cache.CacheGetUserById(userId)
|
||||
user, err := cache.GetUserById(userId)
|
||||
if err != nil {
|
||||
user = &types.User{Id: userId}
|
||||
err = postgresql.GetWholeUser(ctx, user)
|
||||
@@ -35,12 +35,12 @@ func getUserByToken(ctx context.Context, token string) (*types.User, error) {
|
||||
return getUserById(ctx, userId)
|
||||
}
|
||||
func getConnectionWithResponseOnFail(response *http.ResponseWriter, request *http.Request, user *types.User) (*types.Connection, bool) {
|
||||
connectionId, err := convertions.ConvertStringUuid(request.FormValue("connectionid"))
|
||||
connectionId, err := convertions.StringToUuid(request.FormValue("connectionid"))
|
||||
if err != nil {
|
||||
http.Error(*response, "invalid connectionid", http.StatusBadRequest)
|
||||
return nil, false
|
||||
}
|
||||
conn, ok := cache.CacheGetConnection(user, connectionId)
|
||||
conn, ok := cache.GetConnection(user, connectionId)
|
||||
if !ok {
|
||||
http.Error(*response, "invalid connectionid", http.StatusBadRequest)
|
||||
return nil, false
|
||||
|
||||
@@ -11,23 +11,23 @@ import (
|
||||
type postType uint8
|
||||
|
||||
const (
|
||||
postNormal postType = iota
|
||||
postFile
|
||||
postAvatar
|
||||
postProfileBg
|
||||
normal postType = iota
|
||||
file
|
||||
avatar
|
||||
profileBg
|
||||
)
|
||||
|
||||
func validCheckWithResponseOnFail(response *http.ResponseWriter, request *http.Request, pt postType) bool {
|
||||
var maxSize int64
|
||||
switch pt {
|
||||
case postFile:
|
||||
maxSize = int64(globals.MaxPostWithFileBytes)
|
||||
case postAvatar:
|
||||
maxSize = int64(globals.MaxPostWithAvatar)
|
||||
case postProfileBg:
|
||||
maxSize = int64(globals.MaxPostWithProfileBg)
|
||||
case file:
|
||||
maxSize = int64(globals.MaxRequestWithFileBytes)
|
||||
case avatar:
|
||||
maxSize = int64(globals.MaxRequestWithAvatarBytes)
|
||||
case profileBg:
|
||||
maxSize = int64(globals.MaxRequestWithProfileBgBytes)
|
||||
default:
|
||||
maxSize = int64(globals.MaxPostBytes)
|
||||
maxSize = int64(globals.MaxRequestBytes)
|
||||
}
|
||||
|
||||
if request.ContentLength > maxSize {
|
||||
|
||||
@@ -19,7 +19,7 @@ import (
|
||||
)
|
||||
|
||||
func HandleUserNewToken(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ func HandleUserNewToken(response http.ResponseWriter, request *http.Request) {
|
||||
ctx = request.Context()
|
||||
)
|
||||
|
||||
user, err = cache.CacheGetUserByName(username)
|
||||
user, err = cache.GetUserByName(username)
|
||||
if err != nil {
|
||||
user = &types.User{Name: username}
|
||||
if err = postgresql.UserGetStandardInfoByName(ctx, user); err != nil {
|
||||
@@ -78,7 +78,7 @@ func HandleUserNewToken(response http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
|
||||
func HandleUserNew(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -119,7 +119,7 @@ func HandleUserNew(response http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
|
||||
func HandleUserDelete(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -136,12 +136,12 @@ func HandleUserDelete(response http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
cache.CacheDeleteUser(userId)
|
||||
cache.DeleteUser(userId)
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
||||
func HandleUserModProfile(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -191,7 +191,7 @@ func HandleUserModProfile(response http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
|
||||
func HandleUserModAvatar(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, postAvatar) {
|
||||
if !validCheckWithResponseOnFail(&response, request, avatar) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -202,9 +202,9 @@ func HandleUserModAvatar(response http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
request.Body = http.MaxBytesReader(response, request.Body, int64(globals.MaxPostWithAvatar))
|
||||
request.Body = http.MaxBytesReader(response, request.Body, int64(globals.MaxRequestWithAvatarBytes))
|
||||
|
||||
if err = request.ParseMultipartForm(int64(globals.MaxPostBytes)); err != nil {
|
||||
if err = request.ParseMultipartForm(int64(globals.MaxRequestBytes)); err != nil {
|
||||
http.Error(response, "invalid multipart form", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
@@ -254,7 +254,7 @@ func HandleUserModAvatar(response http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
|
||||
func HandleUserModProfileBg(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, postProfileBg) {
|
||||
if !validCheckWithResponseOnFail(&response, request, profileBg) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -265,9 +265,9 @@ func HandleUserModProfileBg(response http.ResponseWriter, request *http.Request)
|
||||
return
|
||||
}
|
||||
|
||||
request.Body = http.MaxBytesReader(response, request.Body, int64(globals.MaxPostWithProfileBg))
|
||||
request.Body = http.MaxBytesReader(response, request.Body, int64(globals.MaxRequestWithProfileBgBytes))
|
||||
|
||||
if err = request.ParseMultipartForm(int64(globals.MaxPostBytes)); err != nil {
|
||||
if err = request.ParseMultipartForm(int64(globals.MaxRequestBytes)); err != nil {
|
||||
http.Error(response, "invalid multipart form", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
@@ -317,7 +317,7 @@ func HandleUserModProfileBg(response http.ResponseWriter, request *http.Request)
|
||||
}
|
||||
|
||||
func HandleUserGetUser(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -328,7 +328,7 @@ func HandleUserGetUser(response http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
targetId, err := convertions.ConvertStringUuid(request.URL.Query().Get("targetid"))
|
||||
targetId, err := convertions.StringToUuid(request.URL.Query().Get("targetid"))
|
||||
if err != nil {
|
||||
http.Error(response, "invalid userid", http.StatusUnauthorized)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user