add message in hub
This commit is contained in:
+19
-19
@@ -90,22 +90,22 @@ func getHubUserIfValidWithResponseOnFail(ctx context.Context, response http.Resp
|
||||
return user, hubUser, hub, nil
|
||||
}
|
||||
|
||||
//func getHubChannelIfValidWithResponseOnFail(ctx context.Context, response http.ResponseWriter, hub *types.Hub, hubUser *types.HubUser, channelId string) (
|
||||
// *types.HubChannel, error) {
|
||||
// channelUuid, err := convertions.StringToUuid(channelId)
|
||||
// if err != nil {
|
||||
// http.Error(response, "invalid channelid", http.StatusBadRequest)
|
||||
// return nil, errors.New("invalid channelid")
|
||||
// }
|
||||
// channel, ok := hub.Channels[channelUuid]
|
||||
// if !ok {
|
||||
// http.Error(response, "invalid channelid", http.StatusBadRequest)
|
||||
// return nil, errors.New("invalid channelid")
|
||||
// }
|
||||
//
|
||||
// if !haveHubUserPermissionsOnChannel(types.CachedUserCanView, hubUser, channel) {
|
||||
// return nil, errors.New("invalid channelid")
|
||||
// }
|
||||
//
|
||||
// return channel, nil
|
||||
//}
|
||||
func getHubChannelIfValidWithResponseOnFail(ctx context.Context, response http.ResponseWriter, hub *types.Hub, hubUser *types.HubUser, channelId string) (
|
||||
*types.HubChannel, error) {
|
||||
channelUuid, err := convertions.StringToUuid(channelId)
|
||||
if err != nil {
|
||||
http.Error(response, "invalid channelid", http.StatusBadRequest)
|
||||
return nil, errors.New("invalid channelid")
|
||||
}
|
||||
channel, ok := hub.Channels[channelUuid]
|
||||
if !ok {
|
||||
http.Error(response, "invalid channelid", http.StatusBadRequest)
|
||||
return nil, errors.New("invalid channelid")
|
||||
}
|
||||
|
||||
if !haveHubUserPermissionsOnChannel(types.CachedUserCanView, hubUser, channel) {
|
||||
return nil, errors.New("invalid channelid")
|
||||
}
|
||||
|
||||
return channel, nil
|
||||
}
|
||||
|
||||
@@ -2,10 +2,12 @@ package httpRequest
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go-socket/packages/cache"
|
||||
"go-socket/packages/types"
|
||||
"go-socket/packages/wsServer"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
@@ -40,12 +42,12 @@ func addHubUserToPermissionCache(hub *types.Hub, user *types.HubUser) {
|
||||
|
||||
if roles.HasSameId(channel.RolesCanView) {
|
||||
perms |= types.CachedUserCanView
|
||||
}
|
||||
if roles.HasSameId(channel.RolesCanReadHistory) {
|
||||
perms |= types.CachedUserCanReadHistory
|
||||
}
|
||||
if roles.HasSameId(channel.RolesCanMessage) {
|
||||
perms |= types.CachedUserCanMessage
|
||||
if roles.HasSameId(channel.RolesCanReadHistory) {
|
||||
perms |= types.CachedUserCanReadHistory
|
||||
}
|
||||
if roles.HasSameId(channel.RolesCanMessage) {
|
||||
perms |= types.CachedUserCanMessage
|
||||
}
|
||||
}
|
||||
channel.Mu.Lock()
|
||||
channel.UsersCachedPermissions[userId] = perms
|
||||
@@ -127,6 +129,7 @@ func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
|
||||
channel.RolesCanReadHistory.Add(rootGroup.Id)
|
||||
channel.RolesCanReadHistory.Add(memberRole.Id)
|
||||
channel.UsersCachedPermissions[creator.OriginalId] = types.CachedUserPermissionsAll
|
||||
hub.Channels[channel.Id] = channel
|
||||
rootGroup.Channels[channel.Id] = channel
|
||||
|
||||
cache.SaveHub(hub)
|
||||
@@ -156,15 +159,55 @@ func HandleHubJoin(response http.ResponseWriter, request *http.Request) {
|
||||
addHubUserToPermissionCache(hub, hubUser)
|
||||
}
|
||||
|
||||
// func HandleHubMessage(response http.ResponseWriter, request *http.Request) {
|
||||
// if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
// return
|
||||
// }
|
||||
// ctx := request.Context()
|
||||
// user, hubUser, hub, err := getHubUserIfValidWithResponseOnFail(ctx, response, request.Header.Get("token"), request.FormValue("hubid"))
|
||||
// if err != nil {
|
||||
// return
|
||||
// }
|
||||
//
|
||||
//
|
||||
// }
|
||||
func HandleHubMessage(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
user, hubUser, hub, err := getHubUserIfValidWithResponseOnFail(ctx, response, request.Header.Get("token"), request.FormValue("hubid"))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
channel, err := getHubChannelIfValidWithResponseOnFail(ctx, response, hub, hubUser, request.Header.Get("channelid"))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
msgContent := request.FormValue("msgContent")
|
||||
attachedFile := request.FormValue("attachedFile")
|
||||
|
||||
if msgContent == "" && attachedFile == "" {
|
||||
http.Error(response, "empty msgContent", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if attachedFile != "" && !strings.HasPrefix(attachedFile, channel.Id.String()+"/") {
|
||||
http.Error(response, "invalid attachedFile", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
channel.Mu.RLock()
|
||||
perms := channel.UsersCachedPermissions
|
||||
channel.Mu.RUnlock()
|
||||
|
||||
msg := &types.Message{
|
||||
Id: uuid.New(),
|
||||
AttachedFile: attachedFile,
|
||||
Content: msgContent,
|
||||
Sender: user.Id,
|
||||
Receiver: channel.Id,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
for permId, perm := range perms {
|
||||
if !perm.CanReadHistory() {
|
||||
continue
|
||||
}
|
||||
|
||||
target, err := cache.GetUserById(permId)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
wsServer.WsSendMessageCloseIfTimeout(target, msg)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user