github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/emails/user.go (about)

     1  // Copyright 2018 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package emails
     5  
     6  import (
     7  	"github.com/keybase/client/go/contacts"
     8  	"github.com/keybase/client/go/libkb"
     9  	"github.com/keybase/client/go/protocol/keybase1"
    10  )
    11  
    12  func AddEmail(mctx libkb.MetaContext, email keybase1.EmailAddress, visibility keybase1.IdentityVisibility) error {
    13  	payload := make(libkb.JSONPayload)
    14  	payload["email"] = email
    15  	payload["visibility"] = visibility
    16  
    17  	arg := libkb.APIArg{
    18  		Endpoint:    "email/add",
    19  		JSONPayload: payload,
    20  		SessionType: libkb.APISessionTypeREQUIRED,
    21  	}
    22  
    23  	_, err := mctx.G().API.PostJSON(mctx, arg)
    24  	return err
    25  }
    26  
    27  func clearEmailsFromContactCache(mctx libkb.MetaContext, email keybase1.EmailAddress) {
    28  	cache := contacts.NewContactCacheStore(mctx.G())
    29  	cache.RemoveContactsCacheEntries(mctx, nil /* phoneNumber */, &email)
    30  	if sync := mctx.G().SyncedContactList; sync != nil {
    31  		sync.UnresolveContactsWithComponent(mctx, nil /* phoneNumber */, &email)
    32  	}
    33  }
    34  
    35  func DeleteEmail(mctx libkb.MetaContext, email keybase1.EmailAddress) error {
    36  	payload := make(libkb.JSONPayload)
    37  	payload["email"] = email
    38  
    39  	arg := libkb.APIArg{
    40  		Endpoint:    "email/delete",
    41  		JSONPayload: payload,
    42  		SessionType: libkb.APISessionTypeREQUIRED,
    43  	}
    44  
    45  	_, err := mctx.G().API.PostJSON(mctx, arg)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	// Now remove this email from contact lookup cache and from synced
    51  	// contacts.
    52  	clearEmailsFromContactCache(mctx, email)
    53  	return nil
    54  }
    55  
    56  func SetPrimaryEmail(mctx libkb.MetaContext, email keybase1.EmailAddress) error {
    57  	payload := make(libkb.JSONPayload)
    58  	payload["email"] = email
    59  
    60  	arg := libkb.APIArg{
    61  		Endpoint:    "email/set-primary",
    62  		JSONPayload: payload,
    63  		SessionType: libkb.APISessionTypeREQUIRED,
    64  	}
    65  
    66  	_, err := mctx.G().API.PostJSON(mctx, arg)
    67  	return err
    68  }
    69  
    70  func SendVerificationEmail(mctx libkb.MetaContext, email keybase1.EmailAddress) error {
    71  	payload := make(libkb.JSONPayload)
    72  	payload["email"] = email
    73  
    74  	arg := libkb.APIArg{
    75  		Endpoint:    "email/send-verify",
    76  		JSONPayload: payload,
    77  		SessionType: libkb.APISessionTypeREQUIRED,
    78  	}
    79  
    80  	_, err := mctx.G().API.PostJSON(mctx, arg)
    81  	return err
    82  }
    83  
    84  func SetVisibilityEmail(mctx libkb.MetaContext, email keybase1.EmailAddress, visibility keybase1.IdentityVisibility) error {
    85  	payload := make(libkb.JSONPayload)
    86  	payload["email"] = email
    87  	payload["visibility"] = visibility
    88  
    89  	arg := libkb.APIArg{
    90  		Endpoint:    "email/set-visibility",
    91  		JSONPayload: payload,
    92  		SessionType: libkb.APISessionTypeREQUIRED,
    93  	}
    94  
    95  	_, err := mctx.G().API.PostJSON(mctx, arg)
    96  	if err != nil {
    97  		return err
    98  	}
    99  	if visibility == keybase1.IdentityVisibility_PRIVATE {
   100  		clearEmailsFromContactCache(mctx, email)
   101  	}
   102  	return nil
   103  }
   104  
   105  func SetVisibilityAllEmail(mctx libkb.MetaContext, visibility keybase1.IdentityVisibility) error {
   106  	payload := make(libkb.JSONPayload)
   107  	payload["visibility"] = visibility
   108  	payload["all"] = true
   109  
   110  	arg := libkb.APIArg{
   111  		Endpoint:    "email/set-visibility",
   112  		JSONPayload: payload,
   113  		SessionType: libkb.APISessionTypeREQUIRED,
   114  	}
   115  
   116  	_, err := mctx.G().API.PostJSON(mctx, arg)
   117  	return err
   118  }
   119  
   120  func GetEmails(mctx libkb.MetaContext) ([]keybase1.Email, error) {
   121  	return libkb.LoadUserEmails(mctx)
   122  }