github.com/grokify/go-ringcentral-client@v0.3.31/office/v1/util/glip.go (about)

     1  package clientutil
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"regexp"
     7  	"strings"
     8  
     9  	rc "github.com/grokify/go-ringcentral-client/office/v1/client"
    10  )
    11  
    12  var rxMultiSpace = regexp.MustCompile(`\\s+`)
    13  
    14  type GlipApiUtil struct {
    15  	ApiClient *rc.APIClient
    16  }
    17  
    18  func AtMention(personId string) string {
    19  	return fmt.Sprintf("![:Person](%v)", personId)
    20  }
    21  
    22  func (apiUtil *GlipApiUtil) GlipGroupMemberCount(groupId string) (int64, error) {
    23  	if apiUtil.ApiClient == nil {
    24  		return int64(-1), fmt.Errorf("GlipApiUtil is missing RingCentral ApiClient")
    25  	}
    26  	groupId = strings.ToLower(strings.TrimSpace(groupId))
    27  	grp, resp, err := apiUtil.ApiClient.GlipApi.LoadGroup(context.Background(), groupId)
    28  	if err != nil {
    29  		return int64(-1), err
    30  	} else if resp.StatusCode >= 300 {
    31  		return int64(-1), fmt.Errorf("Glip API Response Code [%v]", resp.StatusCode)
    32  	}
    33  	return int64(len(grp.Members)), nil
    34  }
    35  
    36  type GlipInfoAtMentionOrGroupOfTwoInfo struct {
    37  	PersonId       string
    38  	AtMentions     []rc.GlipMentionsInfo
    39  	PersonName     string
    40  	FuzzyAtMention bool
    41  	TextRaw        string
    42  	GroupId        string
    43  }
    44  
    45  func (apiUtil *GlipApiUtil) AtMentionedOrGroupOfTwoFuzzy(info GlipInfoAtMentionOrGroupOfTwoInfo) (bool, error) {
    46  	if IsAtMentioned(info.PersonId, info.AtMentions) ||
    47  		IsAtMentionedFuzzy(info.PersonName, info.TextRaw) {
    48  		return true, nil
    49  	}
    50  	count, err := apiUtil.GlipGroupMemberCount(info.GroupId)
    51  	if err != nil || count != int64(2) {
    52  		return false, err
    53  	}
    54  	return true, nil
    55  }
    56  
    57  func IsAtMentionedFuzzy(personName, textRaw string) bool {
    58  	personName = strings.ToLower(strings.TrimSpace(personName))
    59  	rx, err := regexp.Compile(`(\A|\W)@` + personName + `\b`)
    60  	if err != nil {
    61  		return false
    62  	}
    63  	str := rx.FindString(strings.ToLower(textRaw))
    64  	return len(str) > 0
    65  }
    66  
    67  func IsAtMentionedGlipdown(personId, textRaw string) bool {
    68  	personIdMarkdownLc := strings.ToLower(AtMention(personId))
    69  	return strings.Contains(strings.ToLower(textRaw), personIdMarkdownLc)
    70  }
    71  
    72  func PrefixAtMentionUnlessMentioned(personId, text string) string {
    73  	personId = strings.TrimSpace(personId)
    74  	if len(personId) > 0 && !IsAtMentionedGlipdown(personId, text) {
    75  		return AtMention(personId) + " " + text
    76  	}
    77  	return text
    78  }
    79  
    80  // DirectMessage means a group of 2 or a team of 2
    81  func (apiUtil *GlipApiUtil) AtMentionedOrGroupOfTwo(userId, groupId string, mentions []rc.GlipMentionsInfo) (bool, error) {
    82  	if IsAtMentioned(userId, mentions) {
    83  		return true, nil
    84  	}
    85  
    86  	count, err := apiUtil.GlipGroupMemberCount(groupId)
    87  	if err != nil {
    88  		return false, err
    89  	}
    90  	if count == int64(2) {
    91  		return true, nil
    92  	}
    93  	return false, nil
    94  }
    95  
    96  func IsAtMentioned(userId string, mentions []rc.GlipMentionsInfo) bool {
    97  	for _, mention := range mentions {
    98  		if userId == mention.Id {
    99  			return true
   100  		}
   101  	}
   102  	return false
   103  }
   104  
   105  func GlipCreatePostIsEmpty(post rc.GlipCreatePost) bool {
   106  	if len(strings.TrimSpace(post.Text)) == 0 && len(post.Attachments) == 0 {
   107  		return false
   108  	}
   109  	return true
   110  }
   111  
   112  func StripAtMentionAll(id, personName, text string) string {
   113  	noAtMention := StripAtMention(id, text)
   114  	return StripAtMentionFuzzy(personName, noAtMention)
   115  }
   116  
   117  func StripAtMention(id, text string) string {
   118  	rx := regexp.MustCompile(fmt.Sprintf("!\\[:Person\\]\\(%v\\)", id))
   119  	noAtMention := rx.ReplaceAllString(text, " ")
   120  	return strings.TrimSpace(rxMultiSpace.ReplaceAllString(noAtMention, " "))
   121  }
   122  
   123  func StripAtMentionFuzzy(personName, text string) string {
   124  	rx, err := regexp.Compile(`(?i)(\A|\W)@` + personName + `\b`)
   125  	if err != nil {
   126  		return text
   127  	}
   128  	noAtMention := rx.ReplaceAllString(text, " ")
   129  	return strings.TrimSpace(rxMultiSpace.ReplaceAllString(noAtMention, " "))
   130  }