audon/schema.go

140 wiersze
3.6 KiB
Go
Czysty Zwykły widok Historia

2022-12-03 03:20:49 +00:00
package main
import (
"context"
"time"
mastodon "github.com/mattn/go-mastodon"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type (
SessionData struct {
MastodonConfig *mastodon.Config
AuthCode string
AudonID string
}
AudonUser struct {
2022-12-04 05:19:41 +00:00
AudonID string `bson:"audon_id" json:"audon_id" validate:"alphanum"`
2022-12-04 17:52:44 +00:00
RemoteID string `bson:"remote_id" json:"remote_id" validate:"printascii"`
2022-12-04 05:19:41 +00:00
RemoteURL string `bson:"remote_url" json:"remote_url" validate:"url"`
2022-12-03 03:20:49 +00:00
CreatedAt time.Time `bson:"created_at" json:"created_at"`
}
Room struct {
2022-12-04 17:52:44 +00:00
RoomID string `bson:"room_id" json:"room_id" validate:"required,printascii"`
Title string `bson:"title" json:"title" validate:"required,printascii|multibyte"`
Description string `bson:"description" json:"description" validate:"printascii|multibyte"`
2022-12-03 03:20:49 +00:00
Host *AudonUser `bson:"host" json:"host"`
CoHost []*AudonUser `bson:"cohost" json:"cohost"`
2022-12-04 17:52:44 +00:00
FollowingOnly bool `bson:"following_only" json:"following_only"`
FollowerOnly bool `bson:"follower_only" json:"follower_only"`
MutualOnly bool `bson:"mutual_only" json:"mutual_only"`
2022-12-05 01:11:44 +00:00
Kicked []*AudonUser `bson:"kicked" json:"kicked"`
2022-12-04 05:19:41 +00:00
ScheduledAt time.Time `bson:"scheduled_at" json:"scheduled_at"`
2022-12-05 01:11:44 +00:00
EndedAt time.Time `bson:"ended_at" json:"ended_at"`
2022-12-04 05:19:41 +00:00
CreatedAt time.Time `bson:"created_at" json:"created_at"`
2022-12-03 03:20:49 +00:00
}
)
const (
COLLECTION_USER = "user"
COLLECTION_ROOM = "room"
)
2022-12-05 01:11:44 +00:00
func (a *AudonUser) Equal(u *AudonUser) bool {
if a == nil {
return false
}
return a.AudonID == u.AudonID || (a.RemoteID == u.RemoteID && a.RemoteURL == u.RemoteURL)
}
func (r *Room) IsCoHost(u *AudonUser) bool {
if r == nil {
return false
}
for _, cohost := range r.CoHost {
if cohost.Equal(u) {
return true
}
}
return false
}
func (r *Room) IsHost(u *AudonUser) bool {
return r != nil && r.Host.Equal(u)
}
2022-12-03 03:20:49 +00:00
func createIndexes(ctx context.Context) error {
userColl := mainDB.Collection(COLLECTION_USER)
userIndexes, err := userColl.Indexes().ListSpecifications(ctx)
2022-12-04 17:52:44 +00:00
if err != nil {
return err
}
2022-12-03 03:20:49 +00:00
if len(userIndexes) < 3 {
_, err := userColl.Indexes().CreateMany(ctx, []mongo.IndexModel{
{
Keys: bson.D{{Key: "audon_id", Value: 1}},
2022-12-04 17:52:44 +00:00
Options: options.Index().SetUnique(true),
2022-12-03 03:20:49 +00:00
},
{
Keys: bson.D{
{Key: "remote_url", Value: 1},
{Key: "remote_id", Value: 1},
},
},
})
if err != nil {
return err
}
}
2022-12-04 17:52:44 +00:00
roomColl := mainDB.Collection(COLLECTION_ROOM)
roomIndexes, err := roomColl.Indexes().ListSpecifications(ctx)
if err != nil {
return err
}
if len(roomIndexes) < 3 {
_, err := roomColl.Indexes().CreateMany(ctx, []mongo.IndexModel{
{
Keys: bson.D{{Key: "room_id", Value: 1}},
Options: options.Index().SetUnique(true),
},
{
Keys: bson.D{{Key: "host.audon_id", Value: 1}},
},
})
if err != nil {
return err
}
}
return nil
2022-12-03 03:20:49 +00:00
}
func findUserByRemote(ctx context.Context, remoteID, remoteURL string) (*AudonUser, error) {
var result AudonUser
coll := mainDB.Collection(COLLECTION_USER)
if err := coll.FindOne(ctx, bson.D{{Key: "remote_url", Value: remoteURL}, {Key: "remote_id", Value: remoteID}}).Decode(&result); err != nil {
return nil, err
}
return &result, nil
}
func findUserByID(ctx context.Context, audonID string) (*AudonUser, error) {
var result AudonUser
coll := mainDB.Collection(COLLECTION_USER)
2022-12-04 05:19:41 +00:00
if err := coll.FindOne(ctx, bson.D{{Key: "audon_id", Value: audonID}}).Decode(&result); err != nil {
2022-12-03 03:20:49 +00:00
return nil, err
}
return &result, nil
}