code.vegaprotocol.io/vega@v0.79.0/core/types/team.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package types
    17  
    18  import (
    19  	"fmt"
    20  	"time"
    21  
    22  	vgrand "code.vegaprotocol.io/vega/libs/rand"
    23  )
    24  
    25  const teamIDLength = 64
    26  
    27  type TeamID string
    28  
    29  func (t TeamID) IsNoTeam() bool {
    30  	return len(string(t)) <= 0
    31  }
    32  
    33  type Team struct {
    34  	ID TeamID
    35  
    36  	Referrer *Membership
    37  	Referees []*Membership
    38  
    39  	Name      string
    40  	TeamURL   string
    41  	AvatarURL string
    42  	CreatedAt time.Time
    43  
    44  	Closed    bool
    45  	AllowList []PartyID
    46  }
    47  
    48  type Membership struct {
    49  	PartyID        PartyID
    50  	JoinedAt       time.Time
    51  	StartedAtEpoch uint64
    52  }
    53  
    54  func (t *Team) RemoveReferee(refereeToRemove PartyID) {
    55  	refereeIndex := 0
    56  	for i, referee := range t.Referees {
    57  		if referee.PartyID == refereeToRemove {
    58  			refereeIndex = i
    59  			break
    60  		}
    61  	}
    62  
    63  	lastIndex := len(t.Referees) - 1
    64  	if refereeIndex < lastIndex {
    65  		copy(t.Referees[refereeIndex:], t.Referees[refereeIndex+1:])
    66  	}
    67  	t.Referees[lastIndex] = nil
    68  	t.Referees = t.Referees[:lastIndex]
    69  }
    70  
    71  func (t *Team) EnsureCanJoin(party PartyID) error {
    72  	if !t.Closed {
    73  		return nil
    74  	}
    75  
    76  	if len(t.AllowList) == 0 {
    77  		return ErrTeamIsClosed(t.ID)
    78  	}
    79  
    80  	for _, allowedParty := range t.AllowList {
    81  		if allowedParty == party {
    82  			return nil
    83  		}
    84  	}
    85  
    86  	return ErrRefereeNotAllowedToJoinTeam(t.ID)
    87  }
    88  
    89  func NewTeamID() TeamID {
    90  	return TeamID(vgrand.RandomStr(teamIDLength))
    91  }
    92  
    93  func ErrTeamIsClosed(id TeamID) error {
    94  	return fmt.Errorf("team %q is closed", id)
    95  }
    96  
    97  func ErrRefereeNotAllowedToJoinTeam(id TeamID) error {
    98  	return fmt.Errorf("party is not allowed to join team %q", id)
    99  }