db

package
v0.0.0-...-836670f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 18, 2023 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultEnvVar describes the default environment
	// variable used to open a connection to the database.
	DefaultEnvVar = "TLACFG"

	// the number of program thumbnails available to choose from.
	ThumbnailCount = 58

	// classesAliasPath describes the path to the collection with 3 word id => hash mapping for classes
	ClassesAliasPath = "classes_alias"
)

Variables

View Source
var EnableBetaFeatures = os.Getenv("ENABLE_BETA_FEATURES")

Functions

func DefaultData

func DefaultData() (User, []Program)

defaultData is the factory function for constructing default UserData structs and its associated Programs. Associations between said UserData and Programs are not automatically applied in the database.

Types

type Class

type Class struct {
	Thumbnail   int64    `firestore:"thumbnail" json:"thumbnail"`
	Name        string   `firestore:"name" json:"name"`
	Creator     string   `firestore:"creator" json:"creator"`
	Instructors []string `firestore:"instructors" json:"instructors"`
	Members     []string `firestore:"members" json:"members"`
	Programs    []string `firestore:"programs" json:"programs"`
	CID         string   `firestore:"CID" json:"cid"`
	WID         string   `firestore:"WID" json:"wid"`
	Description string   `firestore:"description" json:"description"`
}

Class is a struct representation of a class document. It provides functions for converting the struct to firebase-digestible types.

type Connection

type Connection struct {
	*websocket.Conn
	UID string
	// Set of UIDs which are notified when this connection has changes
	Subscriptions stringSet
}

type DB

type DB struct {
	// Primary database connection.
	*firestore.Client
}

DB implements the TLADB interface on a Firestore database.

func Open

func Open(ctx context.Context, cfg string) (*DB, error)

Open returns a pointer to a new database client based on JSON credentials given by the environment variable. Returns an error if it fails at any point.

func OpenFromJSON

func OpenFromJSON(ctx context.Context, path string) (*DB, error)

OpenFromJSON returns a pointer to a new database client based on a JSON file given by the provided path. Returns an error if it fails at any point.

func (*DB) AddClassToUser

func (d *DB) AddClassToUser(ctx context.Context, uid string, cid string) error

AddClassToUser takes a uid and a pid, and adds the pid to the user's list of programs

func (*DB) AddUserToClass

func (d *DB) AddUserToClass(ctx context.Context, uid string, cid string) error

AddUserToClass add an uid to a given class

func (*DB) CreateClass

func (d *DB) CreateClass(c echo.Context) error

CreateClass is the handler for creating a new class. It takes the UID of the creator, the name of the class, and a thumbnail id.

func (*DB) CreateCollab

func (d *DB) CreateCollab(c echo.Context) error

CreateCollab creates a collaborative session, setting up the session's websocket. Request Body:

{
   uid: UID for the user the program belongs to
   name: optional name identifier for the session, defaults to random UUID.
}

Returns status 201 created on success.

func (*DB) CreateProgram

func (d *DB) CreateProgram(ctx context.Context, p Program) (Program, error)

func (*DB) CreateUser

func (d *DB) CreateUser(ctx context.Context, u User) (User, error)

func (*DB) DeleteClass

func (d *DB) DeleteClass(ctx context.Context, cid string) error

func (*DB) DeleteUser

func (d *DB) DeleteUser(ctx context.Context, uid string) error

func (*DB) ForkProgram

func (d *DB) ForkProgram(c echo.Context) error

ForkProgram forks a program `pid` to the user `uid`.

Request Body:

{
   uid string
   pid string
}

Returns status 201 created on success.

func (*DB) GetID

func (d *DB) GetID(ctx context.Context, path string) (int64, error)

GetID returns the proper ID from the given alias.

func (*DB) GetUIDFromWID

func (d *DB) GetUIDFromWID(ctx context.Context, wid string, path string) (string, error)

GetUIDFromWID returns the UID given a WID

func (*DB) InitShards

func (d *DB) InitShards(ctx context.Context, path string) error

InitShards takes the path to the class_alias collection (path) and initializes the shard

func (*DB) JoinCollab

func (d *DB) JoinCollab(c echo.Context) error

func (*DB) LeaveClass

func (d *DB) LeaveClass(c echo.Context) error

LeaveClass takes a UID and CID through the request body, and attempts to remove user UID from the provided class CID.

func (*DB) LoadClass

func (d *DB) LoadClass(ctx context.Context, cid string) (Class, error)

func (*DB) LoadProgram

func (d *DB) LoadProgram(ctx context.Context, pid string) (Program, error)

func (*DB) LoadUser

func (d *DB) LoadUser(ctx context.Context, uid string) (User, error)

func (*DB) MakeAlias

func (d *DB) MakeAlias(ctx context.Context, uid string, path string) (string, error)

MakeAlias takes an id (usually pid or cid), generates a 3 word id(wid), and stores it in Firebase. The generated wid is returned as a string, with words comma seperated

func (*DB) RemoveClassFromUser

func (d *DB) RemoveClassFromUser(ctx context.Context, uid string, cid string) error

RemoveClassFromUser removes a class from a given user

func (*DB) RemoveProgram

func (d *DB) RemoveProgram(ctx context.Context, pid string) error

func (*DB) RemoveUserFromClass

func (d *DB) RemoveUserFromClass(ctx context.Context, uid string, cid string) error

RemoveUserFromClass removes an uid from a given class

func (*DB) StoreClass

func (d *DB) StoreClass(ctx context.Context, c Class) error

func (*DB) StoreProgram

func (d *DB) StoreProgram(ctx context.Context, p Program) error

func (*DB) StoreUser

func (d *DB) StoreUser(ctx context.Context, u User) error

func (*DB) UpdateProgram

func (d *DB) UpdateProgram(c echo.Context) error

UpdateProgram expects an array of partial Program structs and a UID of the user they belong to. If the user pointed to by UID does not own the programs passed to update, no programs are updated.

Request Body:

{
    "uid": [REQUIRED],
    "programs": [array of partial program objects as indexed in user]
}

Returns status 200 OK on nominal request.

func (*DB) UpdateUser

func (d *DB) UpdateUser(c echo.Context) error

UpdateUser updates the doc with specified UID's fields to match those of the request body.

Request Body:

{
	   "uid": [REQUIRED]
    [User object fields]
}

Returns: Status 200 on success.

type DBContext

type DBContext struct {
	echo.Context
	TLADB
}

DBContext describes the basic echo context required by handlers.

type Message

type Message struct {
	Author string `json:"author"`
	Type   string `json:"type"`
	Target string `json:"target"`
	Body   string `json:"body"`
}

type MockDB

type MockDB struct {
	// contains filtered or unexported fields
}

func OpenMock

func OpenMock() *MockDB

Creates a new MockDB.

func (*MockDB) CreateProgram

func (d *MockDB) CreateProgram(_ context.Context, p Program) (Program, error)

func (*MockDB) CreateUser

func (d *MockDB) CreateUser(_ context.Context, u User) (User, error)

func (*MockDB) DeleteClass

func (d *MockDB) DeleteClass(_ context.Context, cid string) error

func (*MockDB) DeleteUser

func (d *MockDB) DeleteUser(_ context.Context, uid string) error

func (*MockDB) GetUIDFromWID

func (d *MockDB) GetUIDFromWID(ctx context.Context, wid string, path string) (string, error)

func (*MockDB) LoadClass

func (d *MockDB) LoadClass(_ context.Context, cid string) (c Class, err error)

func (*MockDB) LoadProgram

func (d *MockDB) LoadProgram(_ context.Context, pid string) (Program, error)

func (*MockDB) LoadUser

func (d *MockDB) LoadUser(_ context.Context, uid string) (u User, err error)

func (*MockDB) MakeAlias

func (d *MockDB) MakeAlias(ctx context.Context, uid string, path string) (string, error)

Temporary stand-ins to allow other refactors to function

func (*MockDB) RemoveProgram

func (d *MockDB) RemoveProgram(_ context.Context, pid string) error

func (*MockDB) StoreClass

func (d *MockDB) StoreClass(_ context.Context, c Class) error

func (*MockDB) StoreProgram

func (d *MockDB) StoreProgram(_ context.Context, p Program) error

func (*MockDB) StoreUser

func (d *MockDB) StoreUser(_ context.Context, u User) error

type Program

type Program struct {
	Code        string `firestore:"code" json:"code"`
	DateCreated string `firestore:"dateCreated" json:"dateCreated"`
	Language    string `firestore:"language" json:"language"`
	Name        string `firestore:"name" json:"name"`
	Thumbnail   int64  `firestore:"thumbnail" json:"thumbnail"`
	UID         string `json:"uid"`
	WID         string `json:"wid"` // Optional WID of class associated with program
}

Program is a representation of a program document.

func DefaultProgram

func DefaultProgram(language string) (defaultProg Program)

defaultProgram returns a Program struct initialized to default values for a given Language. if the language does not exist, it returns nil.

func (*Program) ToFirestoreUpdate

func (p *Program) ToFirestoreUpdate() (up []firestore.Update)

ToFirestoreUpdate returns the []firestore.Update representation of this struct. Any fields that are non-zero valued are included in the update, save for the date of creation.

type Session

type Session struct {
	*sync.Mutex

	// Map UIDs to their websocket.Conn
	Conns   map[string]*Connection
	Teacher string
}

Session describes a collaborative coding environment.

func (*Session) AddConn

func (s *Session) AddConn(uid string, conn *websocket.Conn) error

func (*Session) BroadcastAll

func (s *Session) BroadcastAll(msg Message) (lastErr error)

BroadcastAll sends a message to all active connections

func (*Session) BroadcastError

func (s *Session) BroadcastError(uid string, err string) error

BroadcastError creates and sends an Error message given a string err to a given uid

func (*Session) BroadcastTo

func (s *Session) BroadcastTo(msg Message, uids ...string) (lastErr error)

BroadcastTo sends a Message msg to the connections associated with the provided uids

func (*Session) BroadcastToSet

func (s *Session) BroadcastToSet(msg Message, uids stringSet) (lastErr error)

BroadcastToSet sends a Message msg to the connections associated with the provided uids in a stringSet

func (*Session) RemoveConn

func (s *Session) RemoveConn(uid string) error

func (*Session) RequestAccess

func (s *Session) RequestAccess(uid string, msg Message) error

type Shard

type Shard struct {
	Count int64
}

Shard class Currently all it does is store the number of shards

type TLADB

type TLADB interface {
	LoadProgram(context.Context, string) (Program, error)
	StoreProgram(context.Context, Program) error
	// Rename to DeleteProgram after moving API handler out of db/program.go
	RemoveProgram(context.Context, string) error

	LoadClass(context.Context, string) (Class, error)
	StoreClass(context.Context, Class) error
	DeleteClass(context.Context, string) error

	LoadUser(context.Context, string) (User, error)
	StoreUser(context.Context, User) error
	DeleteUser(context.Context, string) error

	CreateUser(context.Context, User) (User, error)
	CreateProgram(context.Context, Program) (Program, error)

	MakeAlias(context.Context, string, string) (string, error)
	GetUIDFromWID(context.Context, string, string) (string, error)
}

TLADB describes the basic set of operations required by backend handlers. Atomicity of operations on a TLADB are implementation-dependent.

type User

type User struct {
	Classes           []string `firestore:"classes" json:"classes"`
	DisplayName       string   `firestore:"displayName" json:"displayName"`
	MostRecentProgram string   `firestore:"mostRecentProgram" json:"mostRecentProgram"`
	PhotoName         string   `firestore:"photoName" json:"photoName"`
	Programs          []string `firestore:"programs" json:"programs"`
	UID               string   `json:"uid"`
	DeveloperAcc      bool     `firestore:"developerAcc" json:"developerAcc"`
}

User is a struct representation of a user document. It provides functions for converting the struct to firebase-digestible types.

func (*User) ToFirestoreUpdate

func (u *User) ToFirestoreUpdate() []firestore.Update

ToFirestoreUpdate returns the database update representation of its UserData struct.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL