github.com/jancarloviray/community@v0.41.1-0.20170124221257-33a66c87cf2f/core/api/entity/objects.go (about)

     1  // Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
     2  //
     3  // This software (Documize Community Edition) is licensed under
     4  // GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
     5  //
     6  // You can operate outside the AGPL restrictions by purchasing
     7  // Documize Enterprise Edition and obtaining a commercial license
     8  // by contacting <sales@documize.com>.
     9  //
    10  // https://documize.com
    11  
    12  // Package entity provides types that mirror database tables.
    13  package entity
    14  
    15  import (
    16  	"fmt"
    17  	"strings"
    18  	"time"
    19  )
    20  
    21  // BaseEntity contains the database fields used in every table.
    22  type BaseEntity struct {
    23  	ID      uint64    `json:"-"`
    24  	RefID   string    `json:"id"`
    25  	Created time.Time `json:"created"`
    26  	Revised time.Time `json:"revised"`
    27  }
    28  
    29  // BaseEntityObfuscated is a mirror of BaseEntity,
    30  // but with the fields invisible to JSON.
    31  type BaseEntityObfuscated struct {
    32  	ID      uint64    `json:"-"`
    33  	RefID   string    `json:"-"`
    34  	Created time.Time `json:"-"`
    35  	Revised time.Time `json:"-"`
    36  }
    37  
    38  // User defines a login.
    39  type User struct {
    40  	BaseEntity
    41  	Firstname string    `json:"firstname"`
    42  	Lastname  string    `json:"lastname"`
    43  	Email     string    `json:"email"`
    44  	Initials  string    `json:"initials"`
    45  	Active    bool      `json:"active"`
    46  	Editor    bool      `json:"editor"`
    47  	Admin     bool      `json:"admin"`
    48  	Global    bool      `json:"global"`
    49  	Password  string    `json:"-"`
    50  	Salt      string    `json:"-"`
    51  	Reset     string    `json:"-"`
    52  	Accounts  []Account `json:"accounts"`
    53  }
    54  
    55  // ProtectSecrets blanks sensitive data.
    56  func (user *User) ProtectSecrets() {
    57  	user.Password = ""
    58  	user.Salt = ""
    59  	user.Reset = ""
    60  }
    61  
    62  // Fullname returns Firstname + Lastname.
    63  func (user *User) Fullname() string {
    64  	return fmt.Sprintf("%s %s", user.Firstname, user.Lastname)
    65  }
    66  
    67  // Organization defines a company that uses this app.
    68  type Organization struct {
    69  	BaseEntity
    70  	Company              string `json:"-"`
    71  	Title                string `json:"title"`
    72  	Message              string `json:"message"`
    73  	URL                  string `json:"url"`
    74  	Domain               string `json:"domain"`
    75  	Email                string `json:"email"`
    76  	AllowAnonymousAccess bool   `json:"allowAnonymousAccess"`
    77  	Serial               string `json:"-"`
    78  	Active               bool   `json:"-"`
    79  }
    80  
    81  // Account links a User to an Organization.
    82  type Account struct {
    83  	BaseEntity
    84  	Admin   bool   `json:"admin"`
    85  	Editor  bool   `json:"editor"`
    86  	UserID  string `json:"userId"`
    87  	OrgID   string `json:"orgId"`
    88  	Company string `json:"company"`
    89  	Title   string `json:"title"`
    90  	Message string `json:"message"`
    91  	Domain  string `json:"domain"`
    92  }
    93  
    94  // Label defines a container for documents.
    95  type Label struct {
    96  	BaseEntity
    97  	Name   string     `json:"name"`
    98  	OrgID  string     `json:"orgId"`
    99  	UserID string     `json:"userId"`
   100  	Type   FolderType `json:"folderType"`
   101  }
   102  
   103  // FolderType determines folder visibility.
   104  type FolderType int
   105  
   106  const (
   107  	// FolderTypePublic can be seen by anyone
   108  	FolderTypePublic FolderType = 1
   109  
   110  	// FolderTypePrivate can only be seen by the person who owns it
   111  	FolderTypePrivate FolderType = 2
   112  
   113  	// FolderTypeRestricted can be seen by selected users
   114  	FolderTypeRestricted FolderType = 3
   115  )
   116  
   117  // IsPublic means the folder can be seen by anyone.
   118  func (l *Label) IsPublic() bool {
   119  	return l.Type == FolderTypePublic
   120  }
   121  
   122  // IsPrivate means the folder can only be seen by the person who owns it.
   123  func (l *Label) IsPrivate() bool {
   124  	return l.Type == FolderTypePrivate
   125  }
   126  
   127  // IsRestricted means the folder can be seen by selected users.
   128  func (l *Label) IsRestricted() bool {
   129  	return l.Type == FolderTypeRestricted
   130  }
   131  
   132  // LabelRole determines user permissions for a folder.
   133  type LabelRole struct {
   134  	BaseEntityObfuscated
   135  	OrgID   string `json:"-"`
   136  	LabelID string `json:"folderId"`
   137  	UserID  string `json:"userId"`
   138  	CanView bool   `json:"canView"`
   139  	CanEdit bool   `json:"canEdit"`
   140  }
   141  
   142  // Document represents a document.
   143  type Document struct {
   144  	BaseEntity
   145  	OrgID    string `json:"orgId"`
   146  	LabelID  string `json:"folderId"`
   147  	UserID   string `json:"userId"`
   148  	Job      string `json:"job"`
   149  	Location string `json:"location"`
   150  	Title    string `json:"name"`
   151  	Excerpt  string `json:"excerpt"`
   152  	Slug     string `json:"-"`
   153  	Tags     string `json:"tags"`
   154  	Template bool   `json:"template"`
   155  	Layout   string `json:"layout"`
   156  }
   157  
   158  // SetDefaults ensures on blanks and cleans.
   159  func (d *Document) SetDefaults() {
   160  	d.Title = strings.TrimSpace(d.Title)
   161  
   162  	if len(d.Title) == 0 {
   163  		d.Title = "Document"
   164  	}
   165  
   166  	d.Layout = "doc"
   167  }
   168  
   169  // Attachment represents an attachment to a document.
   170  type Attachment struct {
   171  	BaseEntity
   172  	OrgID      string `json:"orgId"`
   173  	DocumentID string `json:"documentId"`
   174  	Job        string `json:"job"`
   175  	FileID     string `json:"fileId"`
   176  	Filename   string `json:"filename"`
   177  	Data       []byte `json:"-"`
   178  	Extension  string `json:"extension"`
   179  }
   180  
   181  // Page represents a section within a document.
   182  type Page struct {
   183  	BaseEntity
   184  	OrgID       string  `json:"orgId"`
   185  	DocumentID  string  `json:"documentId"`
   186  	UserID      string  `json:"userId"`
   187  	ContentType string  `json:"contentType"`
   188  	PageType    string  `json:"pageType"`
   189  	BlockID     string  `json:"blockId"`
   190  	Level       uint64  `json:"level"`
   191  	Sequence    float64 `json:"sequence"`
   192  	Title       string  `json:"title"`
   193  	Body        string  `json:"body"`
   194  	Revisions   uint64  `json:"revisions"`
   195  }
   196  
   197  // SetDefaults ensures no blank values.
   198  func (p *Page) SetDefaults() {
   199  	if len(p.ContentType) == 0 {
   200  		p.ContentType = "wysiwyg"
   201  	}
   202  
   203  	if p.IsTabType() {
   204  		p.Sequence = 0
   205  	}
   206  
   207  	p.Title = strings.TrimSpace(p.Title)
   208  }
   209  
   210  // IsSectionType tells us that page is "words"
   211  func (p *Page) IsSectionType() bool {
   212  	return p.PageType == "section"
   213  }
   214  
   215  // IsTabType tells us that page is "SaaS data embed"
   216  func (p *Page) IsTabType() bool {
   217  	return p.PageType == "tab"
   218  }
   219  
   220  // PageMeta holds raw page data that is used to
   221  // render the actual page data.
   222  type PageMeta struct {
   223  	ID             uint64    `json:"id"`
   224  	Created        time.Time `json:"created"`
   225  	Revised        time.Time `json:"revised"`
   226  	OrgID          string    `json:"orgId"`
   227  	UserID         string    `json:"userId"`
   228  	DocumentID     string    `json:"documentId"`
   229  	PageID         string    `json:"pageId"`
   230  	RawBody        string    `json:"rawBody"`        // a blob of data
   231  	Config         string    `json:"config"`         // JSON based custom config for this type
   232  	ExternalSource bool      `json:"externalSource"` // true indicates data sourced externally
   233  }
   234  
   235  // SetDefaults ensures no blank values.
   236  func (p *PageMeta) SetDefaults() {
   237  	if len(p.Config) == 0 {
   238  		p.Config = "{}"
   239  	}
   240  }
   241  
   242  // Revision holds the previous version of a Page.
   243  type Revision struct {
   244  	BaseEntity
   245  	OrgID       string `json:"orgId"`
   246  	DocumentID  string `json:"documentId"`
   247  	PageID      string `json:"pageId"`
   248  	OwnerID     string `json:"ownerId"`
   249  	UserID      string `json:"userId"`
   250  	ContentType string `json:"contentType"`
   251  	PageType    string `json:"pageType"`
   252  	Title       string `json:"title"`
   253  	Body        string `json:"body"`
   254  	RawBody     string `json:"rawBody"`
   255  	Config      string `json:"config"`
   256  	Email       string `json:"email"`
   257  	Firstname   string `json:"firstname"`
   258  	Lastname    string `json:"lastname"`
   259  	Initials    string `json:"initials"`
   260  	Revisions   int    `json:"revisions"`
   261  }
   262  
   263  // Block represents a section that has been published as a reusable content block.
   264  type Block struct {
   265  	BaseEntity
   266  	OrgID          string `json:"orgId"`
   267  	LabelID        string `json:"folderId"`
   268  	UserID         string `json:"userId"`
   269  	ContentType    string `json:"contentType"`
   270  	PageType       string `json:"pageType"`
   271  	Title          string `json:"title"`
   272  	Body           string `json:"body"`
   273  	Excerpt        string `json:"excerpt"`
   274  	RawBody        string `json:"rawBody"`        // a blob of data
   275  	Config         string `json:"config"`         // JSON based custom config for this type
   276  	ExternalSource bool   `json:"externalSource"` // true indicates data sourced externally
   277  	Used           uint64 `json:"used"`
   278  	Firstname      string `json:"firstname"`
   279  	Lastname       string `json:"lastname"`
   280  }
   281  
   282  // DocumentMeta details who viewed the document.
   283  type DocumentMeta struct {
   284  	Viewers []DocumentMetaViewer `json:"viewers"`
   285  	Editors []DocumentMetaEditor `json:"editors"`
   286  }
   287  
   288  // DocumentMetaViewer contains the "view" metatdata content.
   289  type DocumentMetaViewer struct {
   290  	UserID    string    `json:"userId"`
   291  	Created   time.Time `json:"created"`
   292  	Firstname string    `json:"firstname"`
   293  	Lastname  string    `json:"lastname"`
   294  }
   295  
   296  // DocumentMetaEditor contains the "edit" metatdata content.
   297  type DocumentMetaEditor struct {
   298  	PageID    string    `json:"pageId"`
   299  	UserID    string    `json:"userId"`
   300  	Action    string    `json:"action"`
   301  	Created   time.Time `json:"created"`
   302  	Firstname string    `json:"firstname"`
   303  	Lastname  string    `json:"lastname"`
   304  }
   305  
   306  // Search holds raw search results.
   307  type Search struct {
   308  	ID            string    `json:"id"`
   309  	Created       time.Time `json:"created"`
   310  	Revised       time.Time `json:"revised"`
   311  	OrgID         string
   312  	DocumentID    string
   313  	Level         uint64
   314  	Sequence      float64
   315  	DocumentTitle string
   316  	Slug          string
   317  	PageTitle     string
   318  	Body          string
   319  }
   320  
   321  // DocumentSearch represents 'presentable' search results.
   322  type DocumentSearch struct {
   323  	ID              string `json:"id"`
   324  	DocumentID      string `json:"documentId"`
   325  	DocumentTitle   string `json:"documentTitle"`
   326  	DocumentSlug    string `json:"documentSlug"`
   327  	DocumentExcerpt string `json:"documentExcerpt"`
   328  	Tags            string `json:"documentTags"`
   329  	PageTitle       string `json:"pageTitle"`
   330  	LabelID         string `json:"folderId"`
   331  	LabelName       string `json:"folderName"`
   332  	FolderSlug      string `json:"folderSlug"`
   333  }
   334  
   335  // SiteMeta holds information associated with an Organization.
   336  type SiteMeta struct {
   337  	OrgID                string `json:"orgId"`
   338  	Title                string `json:"title"`
   339  	Message              string `json:"message"`
   340  	URL                  string `json:"url"`
   341  	AllowAnonymousAccess bool   `json:"allowAnonymousAccess"`
   342  	Version              string `json:"version"`
   343  }
   344  
   345  // Template is used to create a new document.
   346  // Template can consist of content, attachments and
   347  // have associated meta data indentifying author, version
   348  // contact details and more.
   349  type Template struct {
   350  	ID          string       `json:"id"`
   351  	Title       string       `json:"title"`
   352  	Description string       `json:"description"`
   353  	Author      string       `json:"author"`
   354  	Type        TemplateType `json:"type"`
   355  	Dated       time.Time    `json:"dated"`
   356  }
   357  
   358  // TemplateType determines who can see a template.
   359  type TemplateType int
   360  
   361  const (
   362  	// TemplateTypePublic means anyone can see the template.
   363  	TemplateTypePublic TemplateType = 1
   364  	// TemplateTypePrivate means only the owner can see the template.
   365  	TemplateTypePrivate TemplateType = 2
   366  	// TemplateTypeRestricted means selected users can see the template.
   367  	TemplateTypeRestricted TemplateType = 3
   368  )
   369  
   370  // IsPublic means anyone can see the template.
   371  func (t *Template) IsPublic() bool {
   372  	return t.Type == TemplateTypePublic
   373  }
   374  
   375  // IsPrivate means only the owner can see the template.
   376  func (t *Template) IsPrivate() bool {
   377  	return t.Type == TemplateTypePrivate
   378  }
   379  
   380  // IsRestricted means selected users can see the template.
   381  func (t *Template) IsRestricted() bool {
   382  	return t.Type == TemplateTypeRestricted
   383  }
   384  
   385  // FolderVisibility details who can see a particular folder
   386  type FolderVisibility struct {
   387  	Name      string `json:"name"`
   388  	LabelID   string `json:"folderId"`
   389  	Type      int    `json:"folderType"`
   390  	UserID    string `json:"userId"`
   391  	Firstname string `json:"firstname"`
   392  	Lastname  string `json:"lastname"`
   393  	Email     string `json:"email"`
   394  }
   395  
   396  // SitemapDocument details a document that can be exposed via Sitemap.
   397  type SitemapDocument struct {
   398  	DocumentID string
   399  	Document   string
   400  	FolderID   string
   401  	Folder     string
   402  	Revised    time.Time
   403  }
   404  
   405  // Link defines a reference between a section and another document/section/attachment.
   406  type Link struct {
   407  	BaseEntity
   408  	OrgID            string `json:"orgId"`
   409  	FolderID         string `json:"folderId"`
   410  	UserID           string `json:"userId"`
   411  	LinkType         string `json:"linkType"`
   412  	SourceDocumentID string `json:"sourceDocumentId"`
   413  	SourcePageID     string `json:"sourcePageId"`
   414  	TargetDocumentID string `json:"targetDocumentId"`
   415  	TargetID         string `json:"targetId"`
   416  	Orphan           bool   `json:"orphan"`
   417  }
   418  
   419  // LinkCandidate defines a potential link to a document/section/attachment.
   420  type LinkCandidate struct {
   421  	RefID      string `json:"id"`
   422  	LinkType   string `json:"linkType"`
   423  	FolderID   string `json:"folderId"`
   424  	DocumentID string `json:"documentId"`
   425  	TargetID   string `json:"targetId"`
   426  	Title      string `json:"title"`   // what we label the link
   427  	Context    string `json:"context"` // additional context (e.g. excerpt, parent, file extension)
   428  }
   429  
   430  // Pin defines a saved link to a document or space
   431  type Pin struct {
   432  	BaseEntity
   433  	OrgID      string `json:"orgId"`
   434  	UserID     string `json:"userId"`
   435  	FolderID   string `json:"folderId"`
   436  	DocumentID string `json:"documentId"`
   437  	Pin        string `json:"pin"`
   438  	Sequence   int    `json:"sequence"`
   439  }