Browse Source

misc: add permission for admin(#master)

master
OhYee 2 years ago
parent
commit
e77ad893ec
Signed by: OhYee
GPG Key ID: 5A9E1F63ED274FBB
  1. 82
      extensions/queue/main.go
  2. 41
      extensions/queue/type.go

82
extensions/queue/main.go

@ -72,7 +72,7 @@ func CreateAndUpdate(context register.HandleContext) (err error) { @@ -72,7 +72,7 @@ func CreateAndUpdate(context register.HandleContext) (err error) {
}
if args.ID != "" {
if err = update(args.ID, u.ID, args.Password, args.Description, args.Max); err != nil {
if err = update(args.ID, u, args.Password, args.Description, args.Max); err != nil {
res.Success = false
res.Title = "修改信息失败"
res.Content = err.Error()
@ -127,17 +127,26 @@ func create(userID primitive.ObjectID, password string, description string, max @@ -127,17 +127,26 @@ func create(userID primitive.ObjectID, password string, description string, max
return
}
func update(queueID string, userID primitive.ObjectID, password string, description string, max int8) (err error) {
func update(queueID string, u *user.TypeDB, password string, description string, max int8) (err error) {
if u == nil {
return errors.New("user info is nil")
}
queueObjID, err := primitive.ObjectIDFromHex(queueID)
if err != nil {
return
}
if _, err = mongo.Update("blotter", "queue", bson.M{
condition := bson.M{
"_id": queueObjID,
"leader": userID,
"finish_time": 0,
}, bson.M{
}
if !(u.Permission&1 == 1) {
condition["leader"] = u.ID
}
if _, err = mongo.Update("blotter", "queue", condition, bson.M{
"$set": bson.M{
"password": password,
"description": description,
@ -166,7 +175,7 @@ func Finish(context register.HandleContext) (err error) { @@ -166,7 +175,7 @@ func Finish(context register.HandleContext) (err error) {
return
}
if err = finish(u.ID, args.ID); err != nil {
if err = finish(u, args.ID); err != nil {
res.Success = false
res.Title = "排队完成失败"
res.Content = err.Error()
@ -179,15 +188,24 @@ func Finish(context register.HandleContext) (err error) { @@ -179,15 +188,24 @@ func Finish(context register.HandleContext) (err error) {
return
}
func finish(userID primitive.ObjectID, ID string) (err error) {
func finish(u *user.TypeDB, ID string) (err error) {
if u == nil {
return errors.New("user info is nil")
}
objID, err := primitive.ObjectIDFromHex(ID)
if err != nil {
return
}
_, err = mongo.Update("blotter", "queue", bson.M{
"_id": objID,
"leader": userID,
}, bson.M{
condition := bson.M{
"_id": objID,
}
if !(u.Permission&1 == 1) {
condition["leader"] = u.ID
}
_, err = mongo.Update("blotter", "queue", condition, bson.M{
"$set": bson.M{
"finish_time": time.Now().Unix(),
},
@ -240,12 +258,17 @@ func insert(userID primitive.ObjectID, ID string) (err error) { @@ -240,12 +258,17 @@ func insert(userID primitive.ObjectID, ID string) (err error) {
return
}
queues := make([]struct {
Max int64 `bson:"max"`
}, 0)
cnt, err := mongo.Find("blotter", "queue", bson.M{
"_id": queueID,
"finish_time": 0,
}, nil, nil)
if cnt == 0 {
err = errors.New("队伍不存在或已结束")
}, nil, &queues)
if err != nil || cnt == 0 {
if err == nil {
err = errors.New("队伍不存在或已结束")
}
return
}
@ -284,7 +307,7 @@ func insert(userID primitive.ObjectID, ID string) (err error) { @@ -284,7 +307,7 @@ func insert(userID primitive.ObjectID, ID string) (err error) {
},
}, nil)
if cnt == 0 {
if cnt <= queues[0].Max {
go boardcast(queueID, true)
}
@ -442,7 +465,7 @@ func Land(context register.HandleContext) (err error) { @@ -442,7 +465,7 @@ func Land(context register.HandleContext) (err error) {
return
}
if err = landAndOut(u.ID, args.QueueID, args.MemberID, "land"); err != nil {
if err = landAndOut(u, args.QueueID, args.MemberID, "land"); err != nil {
res.Success = false
res.Title = "着陆失败"
res.Content = err.Error()
@ -455,8 +478,11 @@ func Land(context register.HandleContext) (err error) { @@ -455,8 +478,11 @@ func Land(context register.HandleContext) (err error) {
return
}
func landAndOut(userObjID primitive.ObjectID, queueID string, memberID string, op string) (err error) {
func landAndOut(u *user.TypeDB, queueID string, memberID string, op string) (err error) {
defer errors.Wrapper(&err)
if u == nil {
return errors.New("user info is nil")
}
if op != "land" && op != "out" {
err = errors.New("op must be \"land\" or \"out\"")
@ -474,20 +500,30 @@ func landAndOut(userObjID primitive.ObjectID, queueID string, memberID string, o @@ -474,20 +500,30 @@ func landAndOut(userObjID primitive.ObjectID, queueID string, memberID string, o
return
}
queues := make([]struct {
Leader primitive.ObjectID `bson:"_id"`
}, 0)
cnt, err := mongo.Find("blotter", "queue", bson.M{
"_id": queueObjID,
"finish_time": 0,
}, nil, nil)
if cnt == 0 {
err = errors.New("队伍不存在或已结束")
}, nil, &queues)
if err != nil || cnt == 0 {
if err == nil {
err = errors.New("队伍不存在或已结束")
}
return
}
res, err := mongo.Update("blotter", "queue_members", bson.M{
condition := bson.M{
"_id": memberObjID,
"queue": queueObjID,
fieldName: 0,
}, bson.M{
}
if !(u.Permission&1 == 1 || u.ID == queues[0].Leader) {
condition["user"] = u.ID
}
res, err := mongo.Update("blotter", "queue_members", condition, bson.M{
"$set": bson.M{
fieldName: time.Now().Unix(),
},
@ -529,7 +565,7 @@ func Out(context register.HandleContext) (err error) { @@ -529,7 +565,7 @@ func Out(context register.HandleContext) (err error) {
return
}
if err = landAndOut(u.ID, args.QueueID, args.MemberID, "out"); err != nil {
if err = landAndOut(u, args.QueueID, args.MemberID, "out"); err != nil {
res.Success = false
res.Title = "出队失败"
res.Content = err.Error()

41
extensions/queue/type.go

@ -7,6 +7,7 @@ import ( @@ -7,6 +7,7 @@ import (
"github.com/OhYee/blotter/api/pkg/user"
"github.com/OhYee/blotter/api/pkg/variable"
"github.com/OhYee/blotter/mongo"
"github.com/OhYee/blotter/output"
"github.com/gorilla/websocket"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
@ -195,16 +196,10 @@ type boardcastType struct { @@ -195,16 +196,10 @@ type boardcastType struct {
}
func (b *boardcastType) notify(msg string) {
output.Debug("%+v", msg)
qqrobot := ""
if v, err := variable.Get("qqrobot"); err != nil {
return
} else {
v.SetString("qqrobot", &qqrobot)
}
channel := notification.Hub.Get(b.UserID)
if channel != nil {
channels := notification.Hub.Get("queue", b.UserID)
if channels != nil {
bb, err := json.Marshal(notification.Type{
Name: "notification",
Data: map[string]interface{}{
@ -212,15 +207,25 @@ func (b *boardcastType) notify(msg string) { @@ -212,15 +207,25 @@ func (b *boardcastType) notify(msg string) {
},
})
if err == nil {
channel <- notification.WritePackage{
MessageType: websocket.TextMessage,
MessageData: bb,
for _, channel := range channels {
channel.Channel <- notification.WritePackage{
MessageType: websocket.TextMessage,
MessageData: bb,
}
}
}
}
channel = notification.Hub.Get(qqrobot)
if channel != nil {
qqrobot := ""
if v, err := variable.Get("qqrobot"); err != nil {
return
} else {
v.SetString("qqrobot", &qqrobot)
}
channels = notification.Hub.Get("qqrobot_queue", qqrobot)
if channels != nil {
bb, err := json.Marshal(notification.Type{
Name: "notification",
Data: map[string]interface{}{
@ -229,9 +234,11 @@ func (b *boardcastType) notify(msg string) { @@ -229,9 +234,11 @@ func (b *boardcastType) notify(msg string) {
},
})
if err == nil {
channel <- notification.WritePackage{
MessageType: websocket.TextMessage,
MessageData: bb,
for _, channel := range channels {
channel.Channel <- notification.WritePackage{
MessageType: websocket.TextMessage,
MessageData: bb,
}
}
}
}

Loading…
Cancel
Save