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)
|
||||
}
|
||||
}
|
||||
|
||||
+16
-14
@@ -228,24 +228,26 @@ func (p *CachedUserPermissions) ClearCanMessage() { *p &^= CachedUserCanMessage
|
||||
func (p CachedUserPermissions) CanMessage() bool { return p&CachedUserCanMessage != 0 }
|
||||
|
||||
type Hub struct {
|
||||
Mu sync.RWMutex `json:"-"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
Roles [256]*HubRole `json:"-"`
|
||||
Users map[uuid.UUID]*HubUser `json:"-"`
|
||||
Groups [256]*HubGroup `json:"-"`
|
||||
Name string `json:"name"`
|
||||
IconUrl string `json:"iconUrl"`
|
||||
BgUrl string `json:"backgroundUrl"`
|
||||
JoinRole *HubRole `json:"joinRole"`
|
||||
Id uuid.UUID `json:"id"`
|
||||
Creator uuid.UUID `json:"creator"`
|
||||
Color Rgba `json:"color"`
|
||||
UserColorAllowed bool `json:"userColorAllowed"`
|
||||
Mu sync.RWMutex `json:"-"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
Roles [256]*HubRole `json:"-"`
|
||||
Users map[uuid.UUID]*HubUser `json:"-"`
|
||||
Groups [256]*HubGroup `json:"-"`
|
||||
Channels map[uuid.UUID]*HubChannel `json:"channels"`
|
||||
Name string `json:"name"`
|
||||
IconUrl string `json:"iconUrl"`
|
||||
BgUrl string `json:"backgroundUrl"`
|
||||
JoinRole *HubRole `json:"joinRole"`
|
||||
Id uuid.UUID `json:"id"`
|
||||
Creator uuid.UUID `json:"creator"`
|
||||
Color Rgba `json:"color"`
|
||||
UserColorAllowed bool `json:"userColorAllowed"`
|
||||
}
|
||||
|
||||
func NewHub() *Hub {
|
||||
return &Hub{
|
||||
Users: make(map[uuid.UUID]*HubUser),
|
||||
Users: make(map[uuid.UUID]*HubUser),
|
||||
Channels: make(map[uuid.UUID]*HubChannel),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package wsServer
|
||||
|
||||
import (
|
||||
"context"
|
||||
json2 "encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
@@ -14,6 +15,7 @@ import (
|
||||
|
||||
"github.com/coder/websocket"
|
||||
"github.com/coder/websocket/wsjson"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func ServeWsConnection(responseWriter http.ResponseWriter, request *http.Request) {
|
||||
@@ -74,6 +76,21 @@ func WsSendMessageCloseIfTimeout(user *types.User, message any) {
|
||||
}
|
||||
}
|
||||
|
||||
func WsSendMessageToMultipleCloseIfTimeout(users *[]types.User, excludeId uuid.UUID, message any) {
|
||||
|
||||
json, err := json2.Marshal(message)
|
||||
if err != nil {
|
||||
log.Printf("json marshal error: %v", err)
|
||||
json = []byte{}
|
||||
}
|
||||
|
||||
for _, user := range *users {
|
||||
if user.Id != excludeId {
|
||||
WsSendMessageCloseIfTimeout(&user, json)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func sendToAllMessageCloseIfTimeout(message *map[string]any) {
|
||||
cache.Mu.RLock()
|
||||
users := make([]*types.User, 0, len(cache.Users))
|
||||
|
||||
Reference in New Issue
Block a user