model

package
v0.0.0-...-f944d32 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2021 License: MIT Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const (
	VoteLike   = 1
	VoteUnlike = -1
	VoteNone   = 0
)

Variables

View Source
var (
	ProjMutexMap = util.NewMutexMap()
)
View Source
var Resp400 = Response{
	Success: false,
	Code:    400,
	Msg:     "bad request",
}

Resp400 is for 'Bad Request'

View Source
var Resp403 = Response{
	Success: false,
	Code:    403,
	Msg:     "forbidden",
}

Resp403 is for 'Forbidden'

View Source
var Resp404 = Response{
	Success: false,
	Code:    404,
	Msg:     "not found",
}

Resp404 is for 'Not Found'

View Source
var RespInvalidToken = Response{
	Success: false,
	Code:    401,
	Msg:     "invalid token",
}

RespInvalidToken when token is invalid

Functions

func CheckPass

func CheckPass(user *User, pass string) bool

CheckPass checks whether a password is correct

func GenMultiTrnFilesFromSrcFiles

func GenMultiTrnFilesFromSrcFiles(srcRegs []string, trnReg string, ignores []string, proj *Project) ([]string, map[string][]string)

func GenSalt

func GenSalt() []byte

GenSalt gens a random 64-byte salt

func GenTrnFilesFromSrcFiles

func GenTrnFilesFromSrcFiles(src []string, trn string, lang *Language, proj *Project) (res []string)

func HashSalt

func HashSalt(pass string, salt []byte) []byte

HashSalt calcs H(pass+salt)

func Init

func Init() error

Init initialize the model

func NewVote

func NewVote(user_id int64, tran_id int64, vote int)

func ProcessTXT

func ProcessTXT(data []byte) ([]string, []int)

func ProcessXML

func ProcessXML(cfg *ProjCfg, data []byte) ([]string, []int)

func ProjExisted

func ProjExisted(uri string) bool

func SetAllFilesInvalid

func SetAllFilesInvalid(proj_id int64)

func SetAllSentsInvalid

func SetAllSentsInvalid(file_id int64)

func SetUserProjRole

func SetUserProjRole(user_id int64, proj_id int64, role Role)

func TxnVote

func TxnVote(user_id int64, tran_id int64, vote int) (likes int)

func UpdateProjCfgPullStatus

func UpdateProjCfgPullStatus(cfg *ProjCfg, stat int)

UpdateProjCfgPullStatus update a project cfg pull status

func UpdateProjCfgPushStatus

func UpdateProjCfgPushStatus(cfg *ProjCfg, stat int)

UpdateProjCfgPushStatus update a project cfg push status

func UpdateVote

func UpdateVote(user_id int64, tran_id int64, vote int)

Types

type CreateProjRequest

type CreateProjRequest struct {
	Name     string   `json:"name"`
	URI      string   `json:"uri"`
	Desc     string   `json:"desc"`
	GitURL   string   `json:"git_url"`
	SrcLangs []string `json:"src_langs"`
	TrnLangs []string `json:"trn_langs"`
	Type     int      `json:"type"`
	Token    string   `json:"token"`
}

type ExtraCfg

type ExtraCfg struct {
	XML XMLCfg `json:"xml"`
}

type Language

type Language struct {
	Code  string `json:"code"`
	Code2 string `json:"code2"`
	ISO   string `json:"iso"`
	Name  string `json:"name"`
}

Language 语言

func GetLangByCode

func GetLangByCode(code string) (lang Language, ok bool)

GetLangByCode gets a language by language code

func ListLangs

func ListLangs() []Language

func ParseLangsFromCodes

func ParseLangsFromCodes(codes []string) ([]Language, bool)

ParseLangsFromCodes extract []Lang from []string like ["en", "zh-CN"]

func ParseLangsFromStr

func ParseLangsFromStr(s string) ([]Language, bool)

ParseLangsFromStr extract []Lang from string like "eng|zho"

type LoginRequest

type LoginRequest struct {
	Email    string `json:"email"`
	Password string `json:"password"`
}

type PostTranRequest

type PostTranRequest struct {
	FileID  int64  `json:"file_id"`
	Content string `json:"content"`
}

type ProjCfg

type ProjCfg struct {
	ID           int64      `json:"id" gorm:"primaryKey;autoIncrement"`
	Status       int        `json:"status" gorm:"type:tinyint;"`
	SrcBr        string     `json:"src_branch" gorm:"type:varchar(32);notNull"`
	TrnBr        string     `json:"trn_branch" gorm:"type:varchar(32);notNull"`
	PullGap      uint16     `json:"pull_gap" gorm:"index;notNull"`
	PushGap      uint16     `json:"push_gap" gorm:"index;notNull"`
	PullStatus   int        `json:"pull_status" gorm:"notNull"`
	PushStatus   int        `json:"push_status" gorm:"notNull"`
	LastPullAt   *time.Time `json:"last_pull_at"`
	LastPushAt   *time.Time `json:"last_push_at"`
	SrcRegs      []string   `json:"src_files" gorm:"-"`
	SrcRegsBytes []byte     `json:"-" gorm:"column:src_regs"`
	TrnReg       string     `json:"trn_file" gorm:"column:trn_reg"`
	IgnRegs      []string   `json:"ign_regs" gorm:"-"`
	IgnRegsBytes []byte     `json:"-" gorm:"column:ign_regs"`
	Extra        ExtraCfg   `json:"extra" gorm:"-"`
	ExtraBytes   []byte     `json:"-" gorm:"column:extra"`
}

ProjCfg means project config

func GetProjCfgByID

func GetProjCfgByID(id int64) *ProjCfg

GetProjCfgByID get a project config by config id

func ListSyncProjCfg

func ListSyncProjCfg() []ProjCfg

ListSyncProjCfg list all project cfg that should be sync

func NewProjCfg

func NewProjCfg(cfg *ProjCfg) (*ProjCfg, error)

NewProjCfg creates a new cfg for project

func (*ProjCfg) AfterFind

func (cfg *ProjCfg) AfterFind(tx *gorm.DB) error

func (*ProjCfg) Process

func (cfg *ProjCfg) Process()

func (*ProjCfg) TableName

func (*ProjCfg) TableName() string

TableName return table name

func (*ProjCfg) TxnProcess

func (cfg *ProjCfg) TxnProcess(proj *Project, files []string)

func (*ProjCfg) UpdateProjCfg

func (cfg *ProjCfg) UpdateProjCfg(req *UpdateProjCfgRequest) error

func (*ProjCfg) UpdateStatus

func (cfg *ProjCfg) UpdateStatus(stat int)

SSOT <== DB

func (*ProjCfg) Write

func (cfg *ProjCfg) Write()

Write writes project config to DB

type ProjFile

type ProjFile struct {
	ID      int64  `json:"id" gorm:"primaryKey"`
	ProjID  int64  `json:"proj_id" gorm:"index"`
	Path    string `json:"path" gorm:"index"`
	Valid   bool   `json:"-" gorm:"index"`
	SentCnt int    `json:"sent_cnt" gorm:""`
	WordCnt int    `json:"word_cnt" gorm:""`
	Content string `json:"-" gorm:"type:text"`
}

func GetProjFileByID

func GetProjFileByID(proj_id int64, file_id int64) *ProjFile

func GetProjFileByPath

func GetProjFileByPath(proj_id int64, file string) *ProjFile

func ListProjFiles

func ListProjFiles(proj_id int64) []ProjFile

func MustGetValidProjFile

func MustGetValidProjFile(id int64, file string) *ProjFile

func NewProjFile

func NewProjFile(file *ProjFile) (*ProjFile, error)

NewProjFile creates a new file for project

func (*ProjFile) AbsPath

func (file *ProjFile) AbsPath() string

func (*ProjFile) ReadContent

func (file *ProjFile) ReadContent() []byte

func (*ProjFile) TableName

func (*ProjFile) TableName() string

TableName return table name

func (*ProjFile) TxnProcess

func (file *ProjFile) TxnProcess(wg *sync.WaitGroup, tx *gorm.DB, cfg *ProjCfg)

func (*ProjFile) Write

func (file *ProjFile) Write()

type ProjRole

type ProjRole struct {
	ProjID    int64     `json:"proj_id" gorm:"primaryKey"`
	UserID    int64     `json:"user_id" gorm:"primaryKey"`
	Role      Role      `json:"role" gorm:"type:tinyint"`
	CreatedAt time.Time ``
	UpdatedAt time.Time ``
}

func GetUserProjRole

func GetUserProjRole(user_id int64, proj_id int64) *ProjRole

func (*ProjRole) Create

func (role *ProjRole) Create() error

Create creates new project

func (*ProjRole) TableName

func (*ProjRole) TableName() string

TableName return table name

func (*ProjRole) Write

func (role *ProjRole) Write()

Write writes project to DB

type Project

type Project struct {
	ID                 int64      `json:"id" gorm:"primaryKey;autoIncrement"`
	URI                string     `json:"uri" gorm:"type:varchar(32);uniqueIndex;notNull"`
	Name               string     `json:"name" gorm:"type:varchar(32);notNull"`
	OwnerID            int64      `json:"owner_id" gorm:"index;notNull"`
	Token              string     `json:"-" gorm:"type:varchar(128)"`
	Type               int        `json:"type" gorm:"type:tinyint;index;notNull"`
	Status             int        `json:"status" gorm:"type:tinyint;notNull"`
	Desc               string     `json:"desc" gorm:"type:varchar(256)"`
	GitURL             string     `json:"git_url" gorm:"type:varchar(256)"`
	Path               string     `json:"-" gorm:"type:varchar(256)"`
	SrcLangs           string     `json:"-" gorm:"type:varchar(128)"`
	TrnLangs           string     `json:"-" gorm:"type:varchar(128)"`
	SourceLanguages    []Language `json:"src_langs" gorm:"-"`
	TranslateLanguages []Language `json:"trn_langs" gorm:"-"`
	ErrMsg             string     `json:"error_message"`
	PublicView         bool       `json:"public_view"`
	PublicContribute   bool       `json:"public_contribute"`
	CreatedAt          time.Time  `json:"created_at"`
	UpdatedAt          time.Time  `json:"updated_at"`
}

Project means project model

func GetProjByID

func GetProjByID(id int64) *Project

GetProjByID get project by id

func GetProjByURI

func GetProjByURI(uri string) *Project

GetProjByURI get project by name

func ListUninitProj

func ListUninitProj() []Project

ListUninitProj list all uninitialized projects

func ListUserProj

func ListUserProj(user_id int64) []Project

ListUserProj list all projects from a user

func (*Project) Create

func (proj *Project) Create() error

Create creates new project

func (*Project) FillLangs

func (proj *Project) FillLangs()

func (*Project) Init

func (proj *Project) Init()

Init init a new project

func (*Project) InitFail

func (proj *Project) InitFail(err error)

func (*Project) InitSucc

func (proj *Project) InitSucc()

func (*Project) TableName

func (*Project) TableName() string

TableName return table name

func (*Project) UpdateStatus

func (proj *Project) UpdateStatus(stat int)

func (*Project) Write

func (proj *Project) Write()

Write writes project to DB

type RegisterRequest

type RegisterRequest struct {
	Name     string `json:"name"`
	Email    string `json:"email"`
	Password string `json:"password"`
}

type RepoInfo

type RepoInfo struct {
	ID        int64  `json:"id"`
	OwnerName string `json:"owner_name"`
	Name      string `json:"name"`
	URL       string `json:"url"`
}

type Response

type Response struct {
	Success bool                   `json:"success"`
	Data    map[string]interface{} `json:"data"`
	Code    int                    `json:"code"`
	Msg     string                 `json:"message"`
}

Response is HTTP JSON result

type Role

type Role int8
const (
	//RoleAdmin can view, submit, vote and commit translations. Can also manage project settings, including adding collaborators
	RoleAdmin Role = iota
	//RoleCommitter can view, submit, vote and commit translations
	RoleCommitter
	//RoleContributor can view, submit and vote translations
	RoleContributor
	//RoleViewer can view translations
	RoleViewer
	RoleNone Role = Role(127)
)

type Sentence

type Sentence struct {
	ID      int64  `json:"id" gorm:"primaryKey;autoIncrement"`
	ProjID  int64  `json:"proj_id" gorm:"index"`
	FileID  int64  `json:"file_id" gorm:"index"`
	Offset  int    `json:"offset" gorm:"index"`
	Valid   bool   `json:"-" gorm:"index"`
	Locked  bool   `json:"locked" gorm:""`
	MD5     string `json:"-" gorm:"index;type:char(32)"`
	Content string `json:"content" gorm:"type:text"`
}

func GetSentByID

func GetSentByID(sent_id int64) *Sentence

func ListValidSents

func ListValidSents(file_id int64) []Sentence

func ListValidSentsOrderByOffset

func ListValidSentsOrderByOffset(file_id int64) []Sentence

func NewSentence

func NewSentence(sent *Sentence) (*Sentence, error)

NewSentence creates a new file for project

func (*Sentence) TableName

func (*Sentence) TableName() string

TableName return table name

func (*Sentence) TopTran

func (sent *Sentence) TopTran(lang_code string) *Translation

func (*Sentence) Write

func (sent *Sentence) Write()

type Token

type Token struct {
	ID             int64     `json:"id" gorm:"primaryKey;autoIncrement"`
	Valid          bool      `json:"-"`
	Source         int       `json:"-" gorm:"type:varchar(16);index"`
	OwnerID        int64     `json:"-" gorm:"index"`
	OwnerName      string    `json:"owner_name" gorm:"type:varchar(32)"`
	OwnerAvatarURL string    `json:"avatar_url" gorm:"type:varchar(128)"`
	AccessToken    string    `json:"-" gorm:"type:varchar(128)"`
	Scope          string    `json:"-" gorm:"type:varchar(8);index"`
	CreatedAt      time.Time `json:"created_at"`
}

Token 存储Github等来源的Token

func GetTokenByID

func GetTokenByID(id int64) *Token

GetTokenByID gets a token by id

func GetValidTokensByOwnerID

func GetValidTokensByOwnerID(oid int64, src int) []Token

GetValidTokensByOwnerID get a token by owner id

func NewToken

func NewToken(tk *Token) (*Token, error)

NewToken create a new token

func (*Token) TableName

func (*Token) TableName() string

TableName return table name

type TranRes

type TranRes struct {
	ID       int64  `json:"id"`
	UserID   int64  `json:"user_id"`
	UserName string `json:"user_name"`
	Vote     int    `json:"vote"`
	ProjID   int64  `json:"proj_id"`
	FileID   int64  `json:"file_id"`
	SentID   int64  `json:"sent_id"`
	LangCode string `json:"lang_code"`
	Likes    int64  `json:"likes"`
	Content  string `json:"content"`
}

func ListSentTrans

func ListSentTrans(lang_code string, sent_id int64) []TranRes

type Translation

type Translation struct {
	ID       int64   `gorm:"primaryKey;autoIncrement"`
	UserID   int64   `gorm:"index"`
	ProjID   int64   `gorm:""`
	FileID   int64   `gorm:"index"`
	SentID   int64   `gorm:"index"`
	Likes    int64   `gorm:""`
	Unlikes  int64   `json:"-" gorm:""`
	Score    float64 `json:"-" gorm:"index"`
	LangCode string  `gorm:"index;type:varchar(8)"`
	Pinned   bool    `json:"pinned" gorm:"index"`
	Content  string  `gorm:"type:text"`
}

func GetTran

func GetTran(sent_id int64, user_id int64, lang_code string) *Translation

func GetTranByID

func GetTranByID(id int64) *Translation

func (*Translation) Delete

func (tran *Translation) Delete()

func (*Translation) Pin

func (tran *Translation) Pin()

func (*Translation) TableName

func (*Translation) TableName() string

func (*Translation) Unpin

func (tran *Translation) Unpin()

func (*Translation) Write

func (tran *Translation) Write()

type UpdateProfileRequest

type UpdateProfileRequest struct {
	Name string `json:"name"`
	Bio  string `json:"bio"`
}

func (*UpdateProfileRequest) Map

func (req *UpdateProfileRequest) Map() map[string]interface{}

func (*UpdateProfileRequest) Valid

func (req *UpdateProfileRequest) Valid() bool

type UpdateProjCfgRequest

type UpdateProjCfgRequest struct {
	SrcBr        string   `json:"src_br"`
	TrnBr        string   `json:"trn_br"`
	PullGap      uint16   `json:"pull_gap"`
	PushGap      uint16   `json:"push_gap"`
	SrcRegs      []string `json:"src_files"`
	SrcRegsBytes []byte   `json:"-"`
	TrnReg       string   `json:"trn_file"`
	IgnRegs      []string `json:"ign_files"`
	IgnRegsBytes []byte   `json:"-"`
	Extra        ExtraCfg `json:"extra"`
	ExtraBytes   []byte   `json:"-"`
}

func (*UpdateProjCfgRequest) Map

func (req *UpdateProjCfgRequest) Map() map[string]interface{}

func (*UpdateProjCfgRequest) Valid

func (req *UpdateProjCfgRequest) Valid() bool

type User

type User struct {
	ID              int64     `json:"id" gorm:"primaryKey;autoIncrement"`
	Email           string    `json:"email" gorm:"type:varchar(256);uniqueIndex"`
	Name            string    `json:"name" gorm:"type:varchar(32);index;notNull"`
	Bio             string    `json:"bio" gorm:"type:varchar(256)"`
	AvatarURL       string    `json:"avatar_url" gorm:"type:varchar(256)"`
	GithubID        int64     `json:"github_id" gorm:"index"`
	GithubRepoToken string    `json:"-" gorm:"type:varchar(64)"`
	IsActive        bool      `json:"is_active"`
	NoPassword      bool      `json:"-"`
	CreatedAt       time.Time `json:"created_at"`
	UpdatedAt       time.Time `json:"updated_at"`
	Password        []byte    `json:"-" gorm:"type:binary(64)"`
	Salt            []byte    `json:"-" gorm:"type:binary(64)"`
}

User means user

func GetUserByEmail

func GetUserByEmail(email string) *User

GetUserByEmail gets a user by email

func GetUserByGithubID

func GetUserByGithubID(ghid int64) *User

GetUserByGithubID gets a user by github id

func GetUserByID

func GetUserByID(id int64) *User

GetUserByID gets a user by id

func NewUserFromGithub

func NewUserFromGithub(ext *goth.User) *User

NewUserFromGithub creates a new user from OAuth

func (*User) Create

func (user *User) Create() error

Create creates new user

func (*User) TableName

func (*User) TableName() string

TableName returns table name

func (*User) UpdateProfile

func (user *User) UpdateProfile(mp map[string]interface{}) error

func (*User) Write

func (user *User) Write()

Write writes user to DB

type Voting

type Voting struct {
	UserID int64 `gorm:"primaryKey"`
	TranID int64 `gorm:"primaryKey"`
	Vote   int   `gorm:"vote"`
}

func GetVote

func GetVote(user_id int64, tran_id int64) *Voting

func (*Voting) TableName

func (*Voting) TableName() string

TableName returns table name

type XMLCfg

type XMLCfg struct {
	IgnoredTags []string `json:"ignored_tags"`
}

Jump to

Keyboard shortcuts

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