feat: optimize tools up35 (#1552)

* upgrade package and rtc convert

* upgrade package and rtc convert

* upgrade package and rtc convert
This commit is contained in:
chao
2023-12-12 10:03:40 +08:00
committed by GitHub
parent bb6462647a
commit 6b55cfd0b8
15 changed files with 1007 additions and 355 deletions
@@ -0,0 +1,86 @@
package mgo
import (
"context"
"github.com/OpenIMSDK/tools/mgoutil"
"github.com/OpenIMSDK/tools/pagination"
"github.com/openimsdk/open-im-server/v3/tools/up35/pkg/internal/rtc/mongo/table"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"time"
)
func NewMeeting(db *mongo.Database) (table.MeetingInterface, error) {
coll := db.Collection("meeting")
_, err := coll.Indexes().CreateMany(context.Background(), []mongo.IndexModel{
{
Keys: bson.D{
{Key: "room_id", Value: 1},
},
Options: options.Index().SetUnique(true),
},
{
Keys: bson.D{
{Key: "host_user_id", Value: 1},
},
},
{
Keys: bson.D{
{Key: "create_time", Value: -1},
},
},
})
if err != nil {
return nil, err
}
return &meeting{coll: coll}, nil
}
type meeting struct {
coll *mongo.Collection
}
func (x *meeting) Find(ctx context.Context, roomIDs []string) ([]*table.MeetingInfo, error) {
return mgoutil.Find[*table.MeetingInfo](ctx, x.coll, bson.M{"room_id": bson.M{"$in": roomIDs}})
}
func (x *meeting) CreateMeetingInfo(ctx context.Context, meetingInfo *table.MeetingInfo) error {
return mgoutil.InsertMany(ctx, x.coll, []*table.MeetingInfo{meetingInfo})
}
func (x *meeting) UpdateMeetingInfo(ctx context.Context, roomID string, update map[string]any) error {
if len(update) == 0 {
return nil
}
return mgoutil.UpdateOne(ctx, x.coll, bson.M{"room_id": roomID}, bson.M{"$set": update}, false)
}
func (x *meeting) GetUnCompleteMeetingIDList(ctx context.Context, roomIDs []string) ([]string, error) {
if len(roomIDs) == 0 {
return nil, nil
}
return mgoutil.Find[string](ctx, x.coll, bson.M{"room_id": bson.M{"$in": roomIDs}, "status": 0}, options.Find().SetProjection(bson.M{"_id": 0, "room_id": 1}))
}
func (x *meeting) Delete(ctx context.Context, roomIDs []string) error {
return mgoutil.DeleteMany(ctx, x.coll, bson.M{"room_id": bson.M{"$in": roomIDs}})
}
func (x *meeting) GetMeetingRecords(ctx context.Context, hostUserID string, startTime, endTime time.Time, pagination pagination.Pagination) (int64, []*table.MeetingInfo, error) {
var and []bson.M
if hostUserID != "" {
and = append(and, bson.M{"host_user_id": hostUserID})
}
if !startTime.IsZero() {
and = append(and, bson.M{"create_time": bson.M{"$gte": startTime}})
}
if !endTime.IsZero() {
and = append(and, bson.M{"create_time": bson.M{"$lte": endTime}})
}
filter := bson.M{}
if len(and) > 0 {
filter["$and"] = and
}
return mgoutil.FindPage[*table.MeetingInfo](ctx, x.coll, filter, pagination, options.Find().SetSort(bson.M{"create_time": -1}))
}
@@ -0,0 +1,76 @@
package mgo
import (
"context"
"github.com/OpenIMSDK/tools/mgoutil"
"github.com/OpenIMSDK/tools/pagination"
"github.com/OpenIMSDK/tools/utils"
"github.com/openimsdk/open-im-server/v3/tools/up35/pkg/internal/rtc/mongo/table"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"time"
)
func NewMeetingInvitation(db *mongo.Database) (table.MeetingInvitationInterface, error) {
coll := db.Collection("meeting_invitation")
_, err := coll.Indexes().CreateMany(context.Background(), []mongo.IndexModel{
{
Keys: bson.D{
{Key: "room_id", Value: 1},
{Key: "user_id", Value: 1},
},
Options: options.Index().SetUnique(true),
},
{
Keys: bson.D{
{Key: "create_time", Value: -1},
},
},
})
if err != nil {
return nil, err
}
return &meetingInvitation{coll: coll}, nil
}
type meetingInvitation struct {
coll *mongo.Collection
}
func (x *meetingInvitation) FindUserIDs(ctx context.Context, roomID string) ([]string, error) {
return mgoutil.Find[string](ctx, x.coll, bson.M{"room_id": roomID}, options.Find().SetProjection(bson.M{"_id": 0, "user_id": 1}))
}
func (x *meetingInvitation) CreateMeetingInvitationInfo(ctx context.Context, roomID string, inviteeUserIDs []string) error {
now := time.Now()
return mgoutil.InsertMany(ctx, x.coll, utils.Slice(inviteeUserIDs, func(userID string) *table.MeetingInvitationInfo {
return &table.MeetingInvitationInfo{
RoomID: roomID,
UserID: userID,
CreateTime: now,
}
}))
}
func (x *meetingInvitation) GetUserInvitedMeetingIDs(ctx context.Context, userID string) (meetingIDs []string, err error) {
fiveDaysAgo := time.Now().AddDate(0, 0, -5)
return mgoutil.Find[string](ctx, x.coll, bson.M{"user_id": userID, "create_time": bson.M{"$gte": fiveDaysAgo}}, options.Find().SetSort(bson.M{"create_time": -1}).SetProjection(bson.M{"_id": 0, "room_id": 1}))
}
func (x *meetingInvitation) Delete(ctx context.Context, roomIDs []string) error {
return mgoutil.DeleteMany(ctx, x.coll, bson.M{"room_id": bson.M{"$in": roomIDs}})
}
func (x *meetingInvitation) GetMeetingRecords(ctx context.Context, joinedUserID string, startTime, endTime time.Time, pagination pagination.Pagination) (int64, []string, error) {
var and []bson.M
and = append(and, bson.M{"user_id": joinedUserID})
if !startTime.IsZero() {
and = append(and, bson.M{"create_time": bson.M{"$gte": startTime}})
}
if !endTime.IsZero() {
and = append(and, bson.M{"create_time": bson.M{"$lte": endTime}})
}
opt := options.Find().SetSort(bson.M{"create_time": -1}).SetProjection(bson.M{"_id": 0, "room_id": 1})
return mgoutil.FindPage[string](ctx, x.coll, bson.M{"$and": and}, pagination, opt)
}
@@ -0,0 +1,32 @@
package mgo
import (
"context"
"github.com/OpenIMSDK/tools/mgoutil"
"github.com/openimsdk/open-im-server/v3/tools/up35/pkg/internal/rtc/mongo/table"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
func NewMeetingRecord(db *mongo.Database) (table.MeetingRecordInterface, error) {
coll := db.Collection("meeting_record")
_, err := coll.Indexes().CreateMany(context.Background(), []mongo.IndexModel{
{
Keys: bson.D{
{Key: "room_id", Value: 1},
},
},
})
if err != nil {
return nil, err
}
return &meetingRecord{coll: coll}, nil
}
type meetingRecord struct {
coll *mongo.Collection
}
func (x *meetingRecord) CreateMeetingVideoRecord(ctx context.Context, meetingVideoRecord *table.MeetingVideoRecord) error {
return mgoutil.InsertMany(ctx, x.coll, []*table.MeetingVideoRecord{meetingVideoRecord})
}
@@ -0,0 +1,89 @@
package mgo
import (
"context"
"github.com/OpenIMSDK/tools/mgoutil"
"github.com/OpenIMSDK/tools/pagination"
"github.com/openimsdk/open-im-server/v3/tools/up35/pkg/internal/rtc/mongo/table"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"time"
)
func NewSignal(db *mongo.Database) (table.SignalInterface, error) {
coll := db.Collection("signal")
_, err := coll.Indexes().CreateMany(context.Background(), []mongo.IndexModel{
{
Keys: bson.D{
{Key: "sid", Value: 1},
},
Options: options.Index().SetUnique(true),
},
{
Keys: bson.D{
{Key: "inviter_user_id", Value: 1},
},
},
{
Keys: bson.D{
{Key: "initiate_time", Value: -1},
},
},
})
if err != nil {
return nil, err
}
return &signal{coll: coll}, nil
}
type signal struct {
coll *mongo.Collection
}
func (x *signal) Find(ctx context.Context, sids []string) ([]*table.SignalModel, error) {
return mgoutil.Find[*table.SignalModel](ctx, x.coll, bson.M{"sid": bson.M{"$in": sids}})
}
func (x *signal) CreateSignal(ctx context.Context, signalModel *table.SignalModel) error {
return mgoutil.InsertMany(ctx, x.coll, []*table.SignalModel{signalModel})
}
func (x *signal) Update(ctx context.Context, sid string, update map[string]any) error {
if len(update) == 0 {
return nil
}
return mgoutil.UpdateOne(ctx, x.coll, bson.M{"sid": sid}, bson.M{"$set": update}, false)
}
func (x *signal) UpdateSignalFileURL(ctx context.Context, sID, fileURL string) error {
return x.Update(ctx, sID, map[string]any{"file_url": fileURL})
}
func (x *signal) UpdateSignalEndTime(ctx context.Context, sID string, endTime time.Time) error {
return x.Update(ctx, sID, map[string]any{"end_time": endTime})
}
func (x *signal) Delete(ctx context.Context, sids []string) error {
if len(sids) == 0 {
return nil
}
return mgoutil.DeleteMany(ctx, x.coll, bson.M{"sid": bson.M{"$in": sids}})
}
func (x *signal) PageSignal(ctx context.Context, sesstionType int32, sendID string, startTime, endTime time.Time, pagination pagination.Pagination) (int64, []*table.SignalModel, error) {
var and []bson.M
if !startTime.IsZero() {
and = append(and, bson.M{"initiate_time": bson.M{"$gte": startTime}})
}
if !endTime.IsZero() {
and = append(and, bson.M{"initiate_time": bson.M{"$lte": endTime}})
}
if sesstionType != 0 {
and = append(and, bson.M{"sesstion_type": sesstionType})
}
if sendID != "" {
and = append(and, bson.M{"inviter_user_id": sendID})
}
return mgoutil.FindPage[*table.SignalModel](ctx, x.coll, bson.M{"$and": and}, pagination, options.Find().SetSort(bson.M{"initiate_time": -1}))
}
@@ -0,0 +1,78 @@
package mgo
import (
"context"
"github.com/OpenIMSDK/tools/mgoutil"
"github.com/OpenIMSDK/tools/pagination"
"github.com/OpenIMSDK/tools/utils"
"github.com/openimsdk/open-im-server/v3/tools/up35/pkg/internal/rtc/mongo/table"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"time"
)
func NewSignalInvitation(db *mongo.Database) (table.SignalInvitationInterface, error) {
coll := db.Collection("signal_invitation")
_, err := coll.Indexes().CreateMany(context.Background(), []mongo.IndexModel{
{
Keys: bson.D{
{Key: "sid", Value: 1},
{Key: "user_id", Value: 1},
},
Options: options.Index().SetUnique(true),
},
{
Keys: bson.D{
{Key: "initiate_time", Value: -1},
},
},
})
if err != nil {
return nil, err
}
return &signalInvitation{coll: coll}, nil
}
type signalInvitation struct {
coll *mongo.Collection
}
func (x *signalInvitation) Find(ctx context.Context, sid string) ([]*table.SignalInvitationModel, error) {
return mgoutil.Find[*table.SignalInvitationModel](ctx, x.coll, bson.M{"sid": sid})
}
func (x *signalInvitation) CreateSignalInvitation(ctx context.Context, sid string, inviteeUserIDs []string) error {
now := time.Now()
return mgoutil.InsertMany(ctx, x.coll, utils.Slice(inviteeUserIDs, func(userID string) *table.SignalInvitationModel {
return &table.SignalInvitationModel{
UserID: userID,
SID: sid,
InitiateTime: now,
HandleTime: time.Unix(0, 0),
}
}))
}
func (x *signalInvitation) HandleSignalInvitation(ctx context.Context, sID, InviteeUserID string, status int32) error {
return mgoutil.UpdateOne(ctx, x.coll, bson.M{"sid": sID, "user_id": InviteeUserID}, bson.M{"$set": bson.M{"status": status, "handle_time": time.Now()}}, true)
}
func (x *signalInvitation) PageSID(ctx context.Context, recvID string, startTime, endTime time.Time, pagination pagination.Pagination) (int64, []string, error) {
var and []bson.M
and = append(and, bson.M{"user_id": recvID})
if !startTime.IsZero() {
and = append(and, bson.M{"initiate_time": bson.M{"$gte": startTime}})
}
if !endTime.IsZero() {
and = append(and, bson.M{"initiate_time": bson.M{"$lte": endTime}})
}
return mgoutil.FindPage[string](ctx, x.coll, bson.M{"$and": and}, pagination, options.Find().SetProjection(bson.M{"_id": 0, "sid": 1}).SetSort(bson.M{"initiate_time": -1}))
}
func (x *signalInvitation) Delete(ctx context.Context, sids []string) error {
if len(sids) == 0 {
return nil
}
return mgoutil.DeleteMany(ctx, x.coll, bson.M{"sid": bson.M{"$in": sids}})
}