Live video on the AT Protocol
at eli/multistream-fixes 208 lines 7.9 kB view raw
1package model 2 3import ( 4 "context" 5 "fmt" 6 "os" 7 "path/filepath" 8 "time" 9 10 comatproto "github.com/bluesky-social/indigo/api/atproto" 11 "github.com/bluesky-social/indigo/api/bsky" 12 "github.com/bluesky-social/indigo/atproto/syntax" 13 "gorm.io/driver/sqlite" 14 "gorm.io/gorm" 15 "gorm.io/plugin/prometheus" 16 "stream.place/streamplace/pkg/config" 17 "stream.place/streamplace/pkg/log" 18 "stream.place/streamplace/pkg/streamplace" 19) 20 21type DBModel struct { 22 DB *gorm.DB 23} 24 25type Model interface { 26 CreatePlayerEvent(event PlayerEventAPI) error 27 ListPlayerEvents(playerID string) ([]PlayerEvent, error) 28 PlayerReport(playerID string) (map[string]any, error) 29 ClearPlayerEvents() error 30 31 CreateSegment(segment *Segment) error 32 MostRecentSegments() ([]Segment, error) 33 LatestSegmentForUser(user string) (*Segment, error) 34 LatestSegmentsForUser(user string, limit int, before *time.Time, after *time.Time) ([]Segment, error) 35 FilterLiveRepoDIDs(repoDIDs []string) ([]string, error) 36 CreateThumbnail(thumb *Thumbnail) error 37 LatestThumbnailForUser(user string) (*Thumbnail, error) 38 GetSegment(id string) (*Segment, error) 39 GetExpiredSegments(ctx context.Context) ([]Segment, error) 40 DeleteSegment(ctx context.Context, id string) error 41 StartSegmentCleaner(ctx context.Context) error 42 SegmentCleaner(ctx context.Context) error 43 44 GetIdentity(id string) (*Identity, error) 45 UpdateIdentity(ident *Identity) error 46 47 GetRepo(did string) (*Repo, error) 48 GetRepoByHandle(handle string) (*Repo, error) 49 GetRepoByHandleOrDID(arg string) (*Repo, error) 50 GetRepoBySigningKey(signingKey string) (*Repo, error) 51 GetAllRepos() ([]Repo, error) 52 SearchReposByHandle(query string, limit int) ([]Repo, error) 53 UpdateRepo(repo *Repo) error 54 55 UpdateSigningKey(key *SigningKey) error 56 GetSigningKey(ctx context.Context, did, repoDID string) (*SigningKey, error) 57 GetSigningKeyByRKey(ctx context.Context, rkey string) (*SigningKey, error) 58 GetSigningKeysForRepo(repoDID string) ([]SigningKey, error) 59 60 CreateFollow(ctx context.Context, userDID, rev string, follow *bsky.GraphFollow) error 61 GetUserFollowing(ctx context.Context, userDID string) ([]Follow, error) 62 GetUserFollowers(ctx context.Context, userDID string) ([]Follow, error) 63 GetUserFollowingUser(ctx context.Context, userDID, subjectDID string) (*Follow, error) 64 DeleteFollow(ctx context.Context, userDID, rev string) error 65 66 CreateFeedPost(ctx context.Context, post *FeedPost) error 67 ListFeedPosts() ([]FeedPost, error) 68 ListFeedPostsByType(feedType string, limit int, after int64) ([]FeedPost, error) 69 GetFeedPost(uri string) (*FeedPost, error) 70 GetReplies(repoDID string) ([]*bsky.FeedDefs_PostView, error) 71 72 CreateLivestream(ctx context.Context, ls *Livestream) error 73 GetLatestLivestreamForRepo(repoDID string) (*Livestream, error) 74 GetLivestreamByPostURI(postURI string) (*Livestream, error) 75 GetLatestLivestreams(limit int, before *time.Time) ([]Livestream, error) 76 77 CreateTeleport(ctx context.Context, tp *Teleport) error 78 GetLatestTeleportForRepo(repoDID string) (*Teleport, error) 79 GetActiveTeleportsForRepo(repoDID string) ([]Teleport, error) 80 GetActiveTeleportsToRepo(targetDID string) ([]Teleport, error) 81 GetTeleportByURI(uri string) (*Teleport, error) 82 DeleteTeleport(ctx context.Context, uri string) error 83 DenyTeleport(ctx context.Context, uri string) error 84 85 CreateBlock(ctx context.Context, block *Block) error 86 GetBlock(ctx context.Context, rkey string) (*Block, error) 87 GetUserBlock(ctx context.Context, userDID, subjectDID string) (*Block, error) 88 DeleteBlock(ctx context.Context, rkey string) error 89 90 CreateChatMessage(ctx context.Context, message *ChatMessage) error 91 MostRecentChatMessages(repoDID string) ([]*streamplace.ChatDefs_MessageView, error) 92 GetChatMessage(uri string) (*ChatMessage, error) 93 DeleteChatMessage(ctx context.Context, uri string, deletedAt *time.Time) error 94 95 CreateGate(ctx context.Context, gate *Gate) error 96 DeleteGate(ctx context.Context, rkey string) error 97 GetGate(ctx context.Context, rkey string) (*Gate, error) 98 GetUserGates(ctx context.Context, userDID string) ([]*Gate, error) 99 100 CreateChatProfile(ctx context.Context, profile *ChatProfile) error 101 GetChatProfile(ctx context.Context, repoDID string) (*ChatProfile, error) 102 103 UpdateServerSettings(ctx context.Context, settings *ServerSettings) error 104 GetServerSettings(ctx context.Context, server string, repoDID string) (*ServerSettings, error) 105 DeleteServerSettings(ctx context.Context, server string, repoDID string) error 106 107 CreateLabeler(did string) (*Labeler, error) 108 GetLabeler(did string) (*Labeler, error) 109 UpdateLabelerCursor(did string, cursor int64) error 110 111 CreateLabel(label *Label) error 112 GetActiveLabels(uri string) ([]*comatproto.LabelDefs_Label, error) 113 114 UpdateBroadcastOrigin(ctx context.Context, origin *streamplace.BroadcastOrigin, aturi syntax.ATURI) error 115 GetRecentBroadcastOrigins(ctx context.Context) ([]*streamplace.BroadcastDefs_BroadcastOriginView, error) 116 117 CreateMetadataConfiguration(ctx context.Context, metadata *MetadataConfiguration) error 118 GetMetadataConfiguration(ctx context.Context, repoDID string) (*MetadataConfiguration, error) 119 DeleteMetadataConfiguration(ctx context.Context, repoDID string) error 120 121 CreateModerationDelegation(ctx context.Context, rec *streamplace.ModerationPermission, aturi syntax.ATURI) error 122 DeleteModerationDelegation(ctx context.Context, rkey string) error 123 GetModerationDelegation(ctx context.Context, streamerDID, moderatorDID string) (*streamplace.ModerationDefs_PermissionView, error) 124 GetModerationDelegations(ctx context.Context, streamerDID, moderatorDID string) ([]*streamplace.ModerationDefs_PermissionView, error) 125 GetModeratorDelegations(ctx context.Context, moderatorDID string) ([]*streamplace.ModerationDefs_PermissionView, error) 126 GetStreamerModerators(ctx context.Context, streamerDID string) ([]*streamplace.ModerationDefs_PermissionView, error) 127 128 GetRecommendation(userDID string) (*Recommendation, error) 129 UpsertRecommendation(rec *Recommendation) error 130} 131 132var DBRevision = 2 133 134func MakeDB(dbURL string) (Model, error) { 135 sqliteSuffix := dbURL 136 if dbURL != ":memory:" { 137 // Ensure dbURL exists as a directory on the filesystem 138 if err := os.MkdirAll(dbURL, os.ModePerm); err != nil { 139 return nil, fmt.Errorf("error creating database directory: %w", err) 140 } 141 dbPath := filepath.Join(dbURL, fmt.Sprintf("index_%d.sqlite", DBRevision)) 142 sqliteSuffix = dbPath 143 // if this isn't ":memory:", ensure that directory exists (eg, if db 144 // file is being initialized) 145 if err := os.MkdirAll(filepath.Dir(sqliteSuffix), os.ModePerm); err != nil { 146 return nil, fmt.Errorf("error creating database path: %w", err) 147 } 148 } 149 log.Log(context.Background(), "starting database", "dbURL", sqliteSuffix) 150 dial := sqlite.Open(sqliteSuffix) 151 152 db, err := gorm.Open(dial, &gorm.Config{ 153 SkipDefaultTransaction: true, 154 TranslateError: true, 155 Logger: config.GormLogger, 156 }) 157 if err != nil { 158 return nil, fmt.Errorf("error starting database: %w", err) 159 } 160 err = db.Exec("PRAGMA journal_mode=WAL;").Error 161 if err != nil { 162 return nil, fmt.Errorf("error setting journal mode: %w", err) 163 } 164 165 err = db.Use(prometheus.New(prometheus.Config{ 166 DBName: "index", 167 RefreshInterval: 10, 168 StartServer: false, 169 })) 170 if err != nil { 171 return nil, fmt.Errorf("error using prometheus plugin: %w", err) 172 } 173 174 sqlDB, err := db.DB() 175 if err != nil { 176 return nil, fmt.Errorf("error getting database: %w", err) 177 } 178 sqlDB.SetMaxOpenConns(1) 179 for _, model := range []any{ 180 PlayerEvent{}, 181 Segment{}, 182 Thumbnail{}, 183 Identity{}, 184 Repo{}, 185 SigningKey{}, 186 Follow{}, 187 FeedPost{}, 188 Livestream{}, 189 Block{}, 190 ChatMessage{}, 191 ChatProfile{}, 192 Gate{}, 193 ServerSettings{}, 194 Labeler{}, 195 Label{}, 196 BroadcastOrigin{}, 197 MetadataConfiguration{}, 198 Teleport{}, 199 ModerationDelegation{}, 200 Recommendation{}, 201 } { 202 err = db.AutoMigrate(model) 203 if err != nil { 204 return nil, err 205 } 206 } 207 return &DBModel{DB: db}, nil 208}