github.com/decred/politeia@v1.4.0/politeiawww/legacy/user/cms.go (about)

     1  package user
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/google/uuid"
     7  )
     8  
     9  const (
    10  	CMSPluginVersion               = "1"
    11  	CMSPluginID                    = "cms"
    12  	CmdNewCMSUser                  = "newcmsuser"
    13  	CmdCMSUsersByDomain            = "cmsusersbydomain"
    14  	CmdCMSUsersByContractorType    = "cmsusersbycontractortype"
    15  	CmdUpdateCMSUser               = "updatecmsuser"
    16  	CmdCMSUserByID                 = "cmsuserbyid"
    17  	CmdCMSUserSubContractors       = "cmsusersubcontractors"
    18  	CmdCMSUsersByProposalToken     = "cmsusersbyproposaltoken"
    19  	CmdNewCMSUserCodeStats         = "newcmsusercodestats"
    20  	CmdUpdateCMSUserCodeStats      = "updatecmsusercodestats"
    21  	CmdCMSCodeStatsByUserMonthYear = "cmscodestatsbyusermonthyear"
    22  )
    23  
    24  // CMSUser represents a CMS user. It contains the standard politeiawww user
    25  // fields as well as CMS specific user fields.
    26  type CMSUser struct {
    27  	User
    28  	Domain             int         `json:"domain"` // Contractor domain
    29  	GitHubName         string      `json:"githubname"`
    30  	MatrixName         string      `json:"matrixname"`
    31  	ContractorType     int         `json:"contractortype"`
    32  	ContractorName     string      `json:"contractorname"`
    33  	ContractorLocation string      `json:"contractorlocation"`
    34  	ContractorContact  string      `json:"contractorcontact"`
    35  	SupervisorUserIDs  []uuid.UUID `json:"supervisoruserids"`
    36  	ProposalsOwned     []string    `json:"proposalsowned"`
    37  }
    38  
    39  // EncodeCMSUser encodes a CMSUser into a JSON byte slice.
    40  func EncodeCMSUser(u CMSUser) ([]byte, error) {
    41  	return json.Marshal(u)
    42  }
    43  
    44  // DecodeCMSUser decodes a JSON byte slice into a CMSUser.
    45  func DecodeCMSUser(payload []byte) (*CMSUser, error) {
    46  	var u CMSUser
    47  
    48  	err := json.Unmarshal(payload, &u)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  
    53  	return &u, nil
    54  }
    55  
    56  // NewCMSUser creates a new CMS user record in the user database.
    57  type NewCMSUser struct {
    58  	Email                     string `json:"email"`
    59  	Username                  string `json:"username"`
    60  	NewUserVerificationToken  []byte `json:"newuserverificationtoken"`
    61  	NewUserVerificationExpiry int64  `json:"newuserverificationtokenexiry"`
    62  	ContractorType            int    `json:"contractortype"`
    63  }
    64  
    65  // EncodeNewCMSUser encodes a NewCMSUser into a JSON byte slice.
    66  func EncodeNewCMSUser(u NewCMSUser) ([]byte, error) {
    67  	return json.Marshal(u)
    68  }
    69  
    70  // DecodeNewCMSUser decodes JSON byte slice into a NewCMSUser.
    71  func DecodeNewCMSUser(b []byte) (*NewCMSUser, error) {
    72  	var u NewCMSUser
    73  
    74  	err := json.Unmarshal(b, &u)
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  
    79  	return &u, nil
    80  }
    81  
    82  // NewCMSUserReply is the reply to the NewCMSUser command.
    83  type NewCMSUserReply struct{}
    84  
    85  // EncodeNewCMSUserReply encodes a NewCMSUserReply into a JSON byte slice.
    86  func EncodeNewCMSUserReply(u NewCMSUserReply) ([]byte, error) {
    87  	return json.Marshal(u)
    88  }
    89  
    90  // DecodeNewCMSUserReply decodes JSON byte slice into a NewCMSUserReply.
    91  func DecodeNewCMSUserReply(b []byte) (*NewCMSUserReply, error) {
    92  	var reply NewCMSUserReply
    93  
    94  	err := json.Unmarshal(b, &reply)
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  
    99  	return &reply, nil
   100  }
   101  
   102  // CMSUsersByDomain returns all CMS users within the provided domain.
   103  type CMSUsersByDomain struct {
   104  	Domain int `json:"domain"` // Contractor domain
   105  }
   106  
   107  // EncodeCMSUsersByDomain encodes a CMSUsersByDomain into a JSON byte slice.
   108  func EncodeCMSUsersByDomain(u CMSUsersByDomain) ([]byte, error) {
   109  	return json.Marshal(u)
   110  }
   111  
   112  // DecodeCMSUsersByDomain decodes JSON byte slice into a CMSUsersByDomain.
   113  func DecodeCMSUsersByDomain(b []byte) (*CMSUsersByDomain, error) {
   114  	var u CMSUsersByDomain
   115  
   116  	err := json.Unmarshal(b, &u)
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  
   121  	return &u, nil
   122  }
   123  
   124  // CMSUsersByDomainReply is the reply to the CMSUsersByDomain command.
   125  type CMSUsersByDomainReply struct {
   126  	Users []CMSUser `json:"users"`
   127  }
   128  
   129  // EncodeCMSUsersByDomainReply encodes a CMSUsersByDomainReply into a JSON
   130  // byte slice.
   131  func EncodeCMSUsersByDomainReply(u CMSUsersByDomainReply) ([]byte, error) {
   132  	return json.Marshal(u)
   133  }
   134  
   135  // DecodeCMSUsersByDomainReply decodes JSON byte slice into a
   136  // CMSUsersByDomainReply.
   137  func DecodeCMSUsersByDomainReply(b []byte) (*CMSUsersByDomainReply, error) {
   138  	var reply CMSUsersByDomainReply
   139  
   140  	err := json.Unmarshal(b, &reply)
   141  	if err != nil {
   142  		return nil, err
   143  	}
   144  
   145  	return &reply, nil
   146  }
   147  
   148  // CMSUsersByContractorType returns all CMS users within the provided
   149  // contractor type.
   150  type CMSUsersByContractorType struct {
   151  	ContractorType int `json:"contractortype"` // Contractor type
   152  }
   153  
   154  // EncodeCMSUsersByContractorType encodes a CMSUsersByContractorType into a
   155  // JSON byte slice.
   156  func EncodeCMSUsersByContractorType(u CMSUsersByContractorType) ([]byte, error) {
   157  	return json.Marshal(u)
   158  }
   159  
   160  // DecodeCMSUsersByContractorType decodes JSON byte slice into a
   161  // CMSUsersByContractorType.
   162  func DecodeCMSUsersByContractorType(b []byte) (*CMSUsersByContractorType, error) {
   163  	var u CMSUsersByContractorType
   164  
   165  	err := json.Unmarshal(b, &u)
   166  	if err != nil {
   167  		return nil, err
   168  	}
   169  
   170  	return &u, nil
   171  }
   172  
   173  // CMSUsersByContractorTypeReply is the reply to the CMSUsersByContractorType
   174  // command.
   175  type CMSUsersByContractorTypeReply struct {
   176  	Users []CMSUser `json:"users"`
   177  }
   178  
   179  // EncodeCMSUsersByContractorTypeReply encodes a CMSUsersByContractorTypeReply
   180  // into a JSON
   181  // byte slice.
   182  func EncodeCMSUsersByContractorTypeReply(u CMSUsersByContractorTypeReply) ([]byte, error) {
   183  	return json.Marshal(u)
   184  }
   185  
   186  // DecodeCMSUsersByContractorTypeReply decodes JSON byte slice into a
   187  // CMSUsersByContractorTypeReply.
   188  func DecodeCMSUsersByContractorTypeReply(b []byte) (*CMSUsersByContractorTypeReply, error) {
   189  	var reply CMSUsersByContractorTypeReply
   190  
   191  	err := json.Unmarshal(b, &reply)
   192  	if err != nil {
   193  		return nil, err
   194  	}
   195  
   196  	return &reply, nil
   197  }
   198  
   199  // UpdateCMSUser creates a new CMS user record in the user database.
   200  type UpdateCMSUser struct {
   201  	ID                 uuid.UUID   `json:"id"`
   202  	Domain             int         `json:"domain"` // Contractor domain
   203  	GitHubName         string      `json:"githubname"`
   204  	MatrixName         string      `json:"matrixname"`
   205  	ContractorType     int         `json:"contractortype"`
   206  	ContractorName     string      `json:"contractorname"`
   207  	ContractorLocation string      `json:"contractorlocation"`
   208  	ContractorContact  string      `json:"contractorcontact"`
   209  	SupervisorUserIDs  []uuid.UUID `json:"supervisoruserids"`
   210  	ProposalsOwned     []string    `json:"proposalsowned"`
   211  }
   212  
   213  // EncodeUpdateCMSUser encodes a UpdateCMSUser into a JSON byte slice.
   214  func EncodeUpdateCMSUser(u UpdateCMSUser) ([]byte, error) {
   215  	return json.Marshal(u)
   216  }
   217  
   218  // DecodeUpdateCMSUser decodes JSON byte slice into a UpdateCMSUser.
   219  func DecodeUpdateCMSUser(b []byte) (*UpdateCMSUser, error) {
   220  	var u UpdateCMSUser
   221  
   222  	err := json.Unmarshal(b, &u)
   223  	if err != nil {
   224  		return nil, err
   225  	}
   226  
   227  	return &u, nil
   228  }
   229  
   230  // UpdateCMSUserReply is the reply to the UpdateCMSUser command.
   231  type UpdateCMSUserReply struct{}
   232  
   233  // EncodeUpdateCMSUserReply encodes a UpdateCMSUserReply into a JSON byte slice.
   234  func EncodeUpdateCMSUserReply(u UpdateCMSUserReply) ([]byte, error) {
   235  	return json.Marshal(u)
   236  }
   237  
   238  // DecodeUpdateCMSUserReply decodes JSON byte slice into a UpdateCMSUserReply.
   239  func DecodeUpdateCMSUserReply(b []byte) (*UpdateCMSUserReply, error) {
   240  	var reply UpdateCMSUserReply
   241  
   242  	err := json.Unmarshal(b, &reply)
   243  	if err != nil {
   244  		return nil, err
   245  	}
   246  
   247  	return &reply, nil
   248  }
   249  
   250  // CMSUserByID returns CMS User with the matching user ID.
   251  type CMSUserByID struct {
   252  	ID string `json:"id"` // Contractor user id
   253  }
   254  
   255  // EncodeCMSUserByID encodes a CMSUserByID into a JSON byte slice.
   256  func EncodeCMSUserByID(u CMSUserByID) ([]byte, error) {
   257  	return json.Marshal(u)
   258  }
   259  
   260  // DecodeCMSUserByID decodes JSON byte slice into a CMSUserByID.
   261  func DecodeCMSUserByID(b []byte) (*CMSUserByID, error) {
   262  	var u CMSUserByID
   263  
   264  	err := json.Unmarshal(b, &u)
   265  	if err != nil {
   266  		return nil, err
   267  	}
   268  
   269  	return &u, nil
   270  }
   271  
   272  // CMSUserByIDReply is the reply to the CMSUserByID command.
   273  type CMSUserByIDReply struct {
   274  	User *CMSUser `json:"user"`
   275  }
   276  
   277  // EncodeCMSUserByIDReply encodes a CMSUserByIDReply into a JSON
   278  // byte slice.
   279  func EncodeCMSUserByIDReply(u CMSUserByIDReply) ([]byte, error) {
   280  	return json.Marshal(u)
   281  }
   282  
   283  // DecodeCMSUserByIDReply decodes JSON byte slice into a
   284  // CMSUserByIDReply.
   285  func DecodeCMSUserByIDReply(b []byte) (*CMSUserByIDReply, error) {
   286  	var reply CMSUserByIDReply
   287  
   288  	err := json.Unmarshal(b, &reply)
   289  	if err != nil {
   290  		return nil, err
   291  	}
   292  
   293  	return &reply, nil
   294  }
   295  
   296  // CMSUserSubContractors retrieves all users that are currently have the
   297  // given ID as their SupervisorID
   298  type CMSUserSubContractors struct {
   299  	ID string `json:"id"` // Contractor user id
   300  }
   301  
   302  // EncodeCMSUserSubContractors encodes a CMSUserSubContractors into a JSON byte
   303  // slice.
   304  func EncodeCMSUserSubContractors(u CMSUserSubContractors) ([]byte, error) {
   305  	return json.Marshal(u)
   306  }
   307  
   308  // DecodeCMSUserSubContractors decodes JSON byte slice into a
   309  // CMSUserSubContractors.
   310  func DecodeCMSUserSubContractors(b []byte) (*CMSUserSubContractors, error) {
   311  	var u CMSUserSubContractors
   312  
   313  	err := json.Unmarshal(b, &u)
   314  	if err != nil {
   315  		return nil, err
   316  	}
   317  
   318  	return &u, nil
   319  }
   320  
   321  // CMSUserSubContractorsReply is the reply to the CMSUserSubContractors
   322  // command.
   323  type CMSUserSubContractorsReply struct {
   324  	Users []CMSUser `json:"users"`
   325  }
   326  
   327  // EncodeCMSUserSubContractorsReply encodes a CMSUserSubContractorsReply into a
   328  // JSON byte slice.
   329  func EncodeCMSUserSubContractorsReply(u CMSUserSubContractorsReply) ([]byte, error) {
   330  	return json.Marshal(u)
   331  }
   332  
   333  // DecodeCMSUserSubContractorsReply decodes JSON byte slice into a
   334  // CMSUserSubContractorsReply.
   335  func DecodeCMSUserSubContractorsReply(b []byte) (*CMSUserSubContractorsReply, error) {
   336  	var reply CMSUserSubContractorsReply
   337  
   338  	err := json.Unmarshal(b, &reply)
   339  	if err != nil {
   340  		return nil, err
   341  	}
   342  
   343  	return &reply, nil
   344  }
   345  
   346  // CMSUsersByProposalToken returns all CMS users within the provided
   347  // proposal token.
   348  type CMSUsersByProposalToken struct {
   349  	Token string `json:"token"` // Proposal token
   350  }
   351  
   352  // EncodeCMSUsersByProposalToken encodes a CMSUsersByProposalToken into a
   353  // JSON byte slice.
   354  func EncodeCMSUsersByProposalToken(u CMSUsersByProposalToken) ([]byte, error) {
   355  	return json.Marshal(u)
   356  }
   357  
   358  // DecodeCMSUsersByProposalToken decodes JSON byte slice into a
   359  // CMSUsersByProposalToken.
   360  func DecodeCMSUsersByProposalToken(b []byte) (*CMSUsersByProposalToken, error) {
   361  	var u CMSUsersByProposalToken
   362  
   363  	err := json.Unmarshal(b, &u)
   364  	if err != nil {
   365  		return nil, err
   366  	}
   367  
   368  	return &u, nil
   369  }
   370  
   371  // CMSUsersByProposalTokenReply is the reply to the CMSUsersByProposalToken
   372  // command.
   373  type CMSUsersByProposalTokenReply struct {
   374  	Users []CMSUser `json:"users"`
   375  }
   376  
   377  // EncodeCMSUsersByProposalTokenReply encodes a CMSUsersByProposalTokenReply
   378  // into a JSON
   379  // byte slice.
   380  func EncodeCMSUsersByProposalTokenReply(u CMSUsersByProposalTokenReply) ([]byte, error) {
   381  	return json.Marshal(u)
   382  }
   383  
   384  // DecodeCMSUsersByProposalTokenReply decodes JSON byte slice into a
   385  // CMSUsersByProposalTokenReply.
   386  func DecodeCMSUsersByProposalTokenReply(b []byte) (*CMSUsersByProposalTokenReply, error) {
   387  	var reply CMSUsersByProposalTokenReply
   388  
   389  	err := json.Unmarshal(b, &reply)
   390  	if err != nil {
   391  		return nil, err
   392  	}
   393  
   394  	return &reply, nil
   395  }
   396  
   397  // CodeStats is contains the all the information about a given user's code
   398  // work underneath a given repository over a certain month/year.
   399  type CodeStats struct {
   400  	ID               string   // UserID + GithubName + Month + Year
   401  	Repository       string   // Repository
   402  	GitHubName       string   // GithubName
   403  	Month            int      // Month of code stats
   404  	Year             int      // Year of code stats
   405  	PRs              []string // All PR URLs
   406  	Reviews          []string // All Reviewed PR URLS
   407  	Commits          []string // All of the commit URLS
   408  	MergedAdditions  int64    // Total merged code additions
   409  	MergedDeletions  int64    // Total merged code deletions
   410  	UpdatedAdditions int64    // Total updated code additions
   411  	UpdatedDeletions int64    // Total updated code deletions
   412  	ReviewAdditions  int64    // Total reviewed code additions
   413  	ReviewDeletions  int64    // Total reviewed code deletions
   414  	CommitAdditions  int64    // Total committed code additions
   415  	CommitDeletions  int64    // Total committed code deletions
   416  }
   417  
   418  // EncodeCodeStats encodes a CodeStats into a JSON byte slice.
   419  func EncodeCodeStats(cs CodeStats) ([]byte, error) {
   420  	return json.Marshal(cs)
   421  }
   422  
   423  // DecodeCodeStats decodes JSON byte slice into a CodeStats.
   424  func DecodeCodeStats(b []byte) (*CodeStats, error) {
   425  	var cs CodeStats
   426  
   427  	err := json.Unmarshal(b, &cs)
   428  	if err != nil {
   429  		return nil, err
   430  	}
   431  
   432  	return &cs, nil
   433  }
   434  
   435  // NewCMSCodeStats creates a new CMS code stats record in the user database.
   436  type NewCMSCodeStats struct {
   437  	UserCodeStats []CodeStats `json:"usercodestats"`
   438  }
   439  
   440  // EncodeNewCMSCodeStats encodes a NewCMSCodeStats into a JSON byte slice.
   441  func EncodeNewCMSCodeStats(u NewCMSCodeStats) ([]byte, error) {
   442  	return json.Marshal(u)
   443  }
   444  
   445  // DecodeNewCMSCodeStats decodes JSON byte slice into a NewCMSCodeStats.
   446  func DecodeNewCMSCodeStats(b []byte) (*NewCMSCodeStats, error) {
   447  	var u NewCMSCodeStats
   448  
   449  	err := json.Unmarshal(b, &u)
   450  	if err != nil {
   451  		return nil, err
   452  	}
   453  
   454  	return &u, nil
   455  }
   456  
   457  // NewCMSCodeStatsReply replies a to a NewCMSCodeStats request
   458  type NewCMSCodeStatsReply struct{}
   459  
   460  // EncodeNewCMSCodeStatsReply encodes a NewCMSCodeStatsReply into a JSON byte slice.
   461  func EncodeNewCMSCodeStatsReply(u NewCMSCodeStatsReply) ([]byte, error) {
   462  	return json.Marshal(u)
   463  }
   464  
   465  // DecodeNewCMSCodeStatsReply decodes JSON byte slice into a NewCMSCodeStatsReply.
   466  func DecodeNewCMSCodeStatsReply(b []byte) (*NewCMSCodeStatsReply, error) {
   467  	var u NewCMSCodeStatsReply
   468  
   469  	err := json.Unmarshal(b, &u)
   470  	if err != nil {
   471  		return nil, err
   472  	}
   473  
   474  	return &u, nil
   475  }
   476  
   477  // UpdateCMSCodeStats updates a CMS code stats record in the user database.
   478  type UpdateCMSCodeStats struct {
   479  	UserCodeStats []CodeStats `json:"usercodestats"`
   480  }
   481  
   482  // EncodeUpdateCMSCodeStats encodes a UpdateCMSCodeStats into a JSON byte slice.
   483  func EncodeUpdateCMSCodeStats(u UpdateCMSCodeStats) ([]byte, error) {
   484  	return json.Marshal(u)
   485  }
   486  
   487  // DecodeUpdateCMSCodeStats decodes JSON byte slice into a UpdateCMSCodeStats.
   488  func DecodeUpdateCMSCodeStats(b []byte) (*UpdateCMSCodeStats, error) {
   489  	var u UpdateCMSCodeStats
   490  
   491  	err := json.Unmarshal(b, &u)
   492  	if err != nil {
   493  		return nil, err
   494  	}
   495  
   496  	return &u, nil
   497  }
   498  
   499  // UpdateCMSCodeStatsReply replies a to a UpdateCMSCodeStats request
   500  type UpdateCMSCodeStatsReply struct{}
   501  
   502  // EncodeUpdateCMSCodeStatsReply encodes a UpdateCMSCodeStatsReply into a JSON byte slice.
   503  func EncodeUpdateCMSCodeStatsReply(u UpdateCMSCodeStatsReply) ([]byte, error) {
   504  	return json.Marshal(u)
   505  }
   506  
   507  // DecodeUpdateCMSCodeStatsReply decodes JSON byte slice into a UpdateCMSCodeStatsReply.
   508  func DecodeUpdateCMSCodeStatsReply(b []byte) (*UpdateCMSCodeStatsReply, error) {
   509  	var u UpdateCMSCodeStatsReply
   510  
   511  	err := json.Unmarshal(b, &u)
   512  	if err != nil {
   513  		return nil, err
   514  	}
   515  
   516  	return &u, nil
   517  }
   518  
   519  // CMSCodeStatsByUserMonthYear fetches CMS code stats based on requested
   520  // githubname, month and year
   521  type CMSCodeStatsByUserMonthYear struct {
   522  	GithubName string `json:"githubname"`
   523  	Month      int    `json:"month"`
   524  	Year       int    `json:"year"`
   525  }
   526  
   527  // EncodeCMSCodeStatsByUserMonthYear encodes a CMSCodeStatsByUserMonthYear into a JSON byte slice.
   528  func EncodeCMSCodeStatsByUserMonthYear(u CMSCodeStatsByUserMonthYear) ([]byte, error) {
   529  	return json.Marshal(u)
   530  }
   531  
   532  // DecodeCMSCodeStatsByUserMonthYear decodes JSON byte slice into a CMSCodeStatsByUserMonthYear.
   533  func DecodeCMSCodeStatsByUserMonthYear(b []byte) (*CMSCodeStatsByUserMonthYear, error) {
   534  	var u CMSCodeStatsByUserMonthYear
   535  
   536  	err := json.Unmarshal(b, &u)
   537  	if err != nil {
   538  		return nil, err
   539  	}
   540  
   541  	return &u, nil
   542  }
   543  
   544  // CMSCodeStatsByUserMonthYearReply replies CMS code stats based on requested
   545  // githubname, month and year
   546  type CMSCodeStatsByUserMonthYearReply struct {
   547  	UserCodeStats []CodeStats `json:"usercodestats"`
   548  }
   549  
   550  // EncodeCMSCodeStatsByUserMonthYearReply encodes a CMSCodeStatsByUserMonthYearReply into a JSON byte slice.
   551  func EncodeCMSCodeStatsByUserMonthYearReply(u CMSCodeStatsByUserMonthYearReply) ([]byte, error) {
   552  	return json.Marshal(u)
   553  }
   554  
   555  // DecodeCMSCodeStatsByUserMonthYearReply decodes JSON byte slice into a CMSCodeStatsByUserMonthYearReply.
   556  func DecodeCMSCodeStatsByUserMonthYearReply(b []byte) (*CMSCodeStatsByUserMonthYearReply, error) {
   557  	var u CMSCodeStatsByUserMonthYearReply
   558  
   559  	err := json.Unmarshal(b, &u)
   560  	if err != nil {
   561  		return nil, err
   562  	}
   563  
   564  	return &u, nil
   565  }