github.com/status-im/status-go@v1.1.0/protocol/communities/request_to_join.go (about)

     1  package communities
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"time"
     7  
     8  	"github.com/status-im/status-go/eth-node/types"
     9  	multiaccountscommon "github.com/status-im/status-go/multiaccounts/common"
    10  	"github.com/status-im/status-go/protocol/protobuf"
    11  )
    12  
    13  type RequestToJoinState uint
    14  
    15  const (
    16  	RequestToJoinStatePending RequestToJoinState = iota + 1
    17  	RequestToJoinStateDeclined
    18  	RequestToJoinStateAccepted
    19  	RequestToJoinStateCanceled
    20  	RequestToJoinStateAcceptedPending
    21  	RequestToJoinStateDeclinedPending
    22  	RequestToJoinStateAwaitingAddresses
    23  )
    24  
    25  type RequestToJoin struct {
    26  	ID                   types.HexBytes                         `json:"id"`
    27  	PublicKey            string                                 `json:"publicKey"`
    28  	Clock                uint64                                 `json:"clock"`
    29  	ENSName              string                                 `json:"ensName,omitempty"`
    30  	ChatID               string                                 `json:"chatId"`
    31  	CommunityID          types.HexBytes                         `json:"communityId"`
    32  	State                RequestToJoinState                     `json:"state"`
    33  	Our                  bool                                   `json:"our"`
    34  	Deleted              bool                                   `json:"deleted"`
    35  	RevealedAccounts     []*protobuf.RevealedAccount            `json:"revealedAccounts,omitempty"`
    36  	CustomizationColor   multiaccountscommon.CustomizationColor `json:"customizationColor,omitempty"`
    37  	ShareFutureAddresses bool                                   `json:"shareFutureAddresses"`
    38  }
    39  
    40  func (r *RequestToJoin) CalculateID() {
    41  	r.ID = CalculateRequestID(r.PublicKey, r.CommunityID)
    42  }
    43  
    44  func (r *RequestToJoin) ToCommunityRequestToJoinProtobuf() *protobuf.CommunityRequestToJoin {
    45  	return &protobuf.CommunityRequestToJoin{
    46  		Clock:              r.Clock,
    47  		EnsName:            r.ENSName,
    48  		CommunityId:        r.CommunityID,
    49  		RevealedAccounts:   r.RevealedAccounts,
    50  		CustomizationColor: multiaccountscommon.ColorToIDFallbackToBlue(r.CustomizationColor),
    51  	}
    52  }
    53  
    54  func (r *RequestToJoin) ToSyncProtobuf() *protobuf.SyncCommunityRequestsToJoin {
    55  	return &protobuf.SyncCommunityRequestsToJoin{
    56  		Id:                   r.ID,
    57  		PublicKey:            r.PublicKey,
    58  		Clock:                r.Clock,
    59  		EnsName:              r.ENSName,
    60  		ChatId:               r.ChatID,
    61  		CommunityId:          r.CommunityID,
    62  		State:                uint64(r.State),
    63  		RevealedAccounts:     r.RevealedAccounts,
    64  		CustomizationColor:   multiaccountscommon.ColorToIDFallbackToBlue(r.CustomizationColor),
    65  		ShareFutureAddresses: r.ShareFutureAddresses,
    66  	}
    67  }
    68  
    69  func (r *RequestToJoin) InitFromSyncProtobuf(proto *protobuf.SyncCommunityRequestsToJoin) {
    70  	r.ID = proto.Id
    71  	r.PublicKey = proto.PublicKey
    72  	r.Clock = proto.Clock
    73  	r.ENSName = proto.EnsName
    74  	r.ChatID = proto.ChatId
    75  	r.CommunityID = proto.CommunityId
    76  	r.State = RequestToJoinState(proto.State)
    77  	r.RevealedAccounts = proto.RevealedAccounts
    78  	r.CustomizationColor = multiaccountscommon.IDToColorFallbackToBlue(proto.CustomizationColor)
    79  	r.ShareFutureAddresses = proto.ShareFutureAddresses
    80  }
    81  
    82  func (r *RequestToJoin) Empty() bool {
    83  	return len(r.ID)+len(r.PublicKey)+int(r.Clock)+len(r.ENSName)+len(r.ChatID)+len(r.CommunityID)+int(r.State)+len(r.CustomizationColor) == 0
    84  }
    85  
    86  func (r *RequestToJoin) ShouldRetainDeclined(clock uint64) (bool, error) {
    87  	if r.State != RequestToJoinStateDeclined {
    88  		return false, nil
    89  	}
    90  
    91  	declineExpiryClock, err := AddTimeoutToRequestToJoinClock(r.Clock)
    92  	if err != nil {
    93  		return false, err
    94  	}
    95  
    96  	return clock < declineExpiryClock, nil
    97  }
    98  
    99  func AddTimeoutToRequestToJoinClock(clock uint64) (uint64, error) {
   100  	requestToJoinClock, err := strconv.ParseInt(fmt.Sprint(clock), 10, 64)
   101  	if err != nil {
   102  		return 0, err
   103  	}
   104  
   105  	// Adding 7 days to the request clock
   106  	requestTimeOutClock := uint64(time.Unix(requestToJoinClock, 0).AddDate(0, 0, 7).Unix())
   107  
   108  	return requestTimeOutClock, nil
   109  }