github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/model/custom_status.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 "fmt" 9 "io" 10 "time" 11 ) 12 13 const ( 14 UserPropsKeyCustomStatus = "customStatus" 15 16 CustomStatusTextMaxRunes = 100 17 MaxRecentCustomStatuses = 5 18 DefaultCustomStatusEmoji = "speech_balloon" 19 ) 20 21 var validCustomStatusDuration = map[string]bool{ 22 "thirty_minutes": true, 23 "one_hour": true, 24 "four_hours": true, 25 "today": true, 26 "this_week": true, 27 "date_and_time": true, 28 } 29 30 type CustomStatus struct { 31 Emoji string `json:"emoji"` 32 Text string `json:"text"` 33 Duration string `json:"duration"` 34 ExpiresAt time.Time `json:"expires_at"` 35 } 36 37 func (cs *CustomStatus) PreSave() { 38 if cs.Emoji == "" { 39 cs.Emoji = DefaultCustomStatusEmoji 40 } 41 42 if cs.Duration == "" && !cs.ExpiresAt.Before(time.Now()) { 43 cs.Duration = "date_and_time" 44 } 45 runes := []rune(cs.Text) 46 if len(runes) > CustomStatusTextMaxRunes { 47 cs.Text = string(runes[:CustomStatusTextMaxRunes]) 48 } 49 } 50 51 func (cs *CustomStatus) ToJson() string { 52 csCopy := *cs 53 b, _ := json.Marshal(csCopy) 54 return string(b) 55 } 56 57 func (cs *CustomStatus) AreDurationAndExpirationTimeValid() bool { 58 if cs.Duration == "" && (cs.ExpiresAt.IsZero() || !cs.ExpiresAt.Before(time.Now())) { 59 return true 60 } 61 62 if validCustomStatusDuration[cs.Duration] && !cs.ExpiresAt.Before(time.Now()) { 63 return true 64 } 65 66 return false 67 } 68 69 func CustomStatusFromJson(data io.Reader) *CustomStatus { 70 var cs *CustomStatus 71 _ = json.NewDecoder(data).Decode(&cs) 72 return cs 73 } 74 75 func RuneToHexadecimalString(r rune) string { 76 return fmt.Sprintf("%04x", r) 77 } 78 79 type RecentCustomStatuses []CustomStatus 80 81 func (rcs *RecentCustomStatuses) Contains(cs *CustomStatus) bool { 82 var csJSON = cs.ToJson() 83 84 // status is empty 85 if cs == nil || csJSON == "" || (cs.Emoji == "" && cs.Text == "") { 86 return false 87 } 88 89 for _, status := range *rcs { 90 if status.ToJson() == csJSON { 91 return true 92 } 93 } 94 95 return false 96 } 97 98 func (rcs *RecentCustomStatuses) Add(cs *CustomStatus) *RecentCustomStatuses { 99 newRCS := (*rcs)[:0] 100 101 // if same `text` exists in existing recent custom statuses, modify existing status 102 for _, status := range *rcs { 103 if status.Text != cs.Text { 104 newRCS = append(newRCS, status) 105 } 106 } 107 newRCS = append(RecentCustomStatuses{*cs}, newRCS...) 108 if len(newRCS) > MaxRecentCustomStatuses { 109 newRCS = newRCS[:MaxRecentCustomStatuses] 110 } 111 return &newRCS 112 } 113 114 func (rcs *RecentCustomStatuses) Remove(cs *CustomStatus) *RecentCustomStatuses { 115 var csJSON = cs.ToJson() 116 if csJSON == "" || (cs.Emoji == "" && cs.Text == "") { 117 return rcs 118 } 119 120 newRCS := (*rcs)[:0] 121 for _, status := range *rcs { 122 if status.ToJson() != csJSON { 123 newRCS = append(newRCS, status) 124 } 125 } 126 127 return &newRCS 128 } 129 130 func (rcs *RecentCustomStatuses) ToJson() string { 131 rcsCopy := *rcs 132 b, _ := json.Marshal(rcsCopy) 133 return string(b) 134 } 135 136 func RecentCustomStatusesFromJson(data io.Reader) *RecentCustomStatuses { 137 var rcs *RecentCustomStatuses 138 _ = json.NewDecoder(data).Decode(&rcs) 139 return rcs 140 }