github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/model/guest_invite.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package model 5 6 import ( 7 "encoding/json" 8 "io" 9 "net/http" 10 ) 11 12 type GuestsInvite struct { 13 Emails []string `json:"emails"` 14 Channels []string `json:"channels"` 15 Message string `json:"message"` 16 } 17 18 // IsValid validates the user and returns an error if it isn't configured 19 // correctly. 20 func (i *GuestsInvite) IsValid() *AppError { 21 if len(i.Emails) == 0 { 22 return NewAppError("GuestsInvite.IsValid", "model.guest.is_valid.emails.app_error", nil, "", http.StatusBadRequest) 23 } 24 25 for _, email := range i.Emails { 26 if len(email) > USER_EMAIL_MAX_LENGTH || email == "" || !IsValidEmail(email) { 27 return NewAppError("GuestsInvite.IsValid", "model.guest.is_valid.email.app_error", nil, "email="+email, http.StatusBadRequest) 28 } 29 } 30 31 if len(i.Channels) == 0 { 32 return NewAppError("GuestsInvite.IsValid", "model.guest.is_valid.channels.app_error", nil, "", http.StatusBadRequest) 33 } 34 35 for _, channel := range i.Channels { 36 if len(channel) != 26 { 37 return NewAppError("GuestsInvite.IsValid", "model.guest.is_valid.channel.app_error", nil, "channel="+channel, http.StatusBadRequest) 38 } 39 } 40 return nil 41 } 42 43 // GuestsInviteFromJson will decode the input and return a GuestsInvite 44 func GuestsInviteFromJson(data io.Reader) *GuestsInvite { 45 var i *GuestsInvite 46 json.NewDecoder(data).Decode(&i) 47 return i 48 } 49 50 func (i *GuestsInvite) ToJson() string { 51 b, _ := json.Marshal(i) 52 return string(b) 53 }