Repository navigation
Expand file tree
/
Copy pathchat_titles.go
More file actions
129 lines (109 loc) · 2.74 KB
/
Copy pathchat_titles.go
File metadata and controls
129 lines (109 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"bytes"
"fmt"
"sort"
"strings"
"github.com/fossteams/teams-api/pkg/csa"
"golang.org/x/net/html"
)
func textMessage(input string) string {
output := ""
z := html.NewTokenizer(bytes.NewBuffer([]byte(input)))
for {
tt := z.Next()
if tt == html.ErrorToken {
break
}
switch tt {
case html.TextToken:
text := string(z.Text())
if strings.TrimSpace(text) == "" {
continue
}
output += fmt.Sprintf("\t%v\n", text)
}
if tt == html.ErrorToken {
break
}
}
return output
}
func (s *AppState) chatTitle(chat csa.Chat) string {
selfMri := ""
selfDisplayName := ""
if s.me != nil {
selfMri = s.me.Mri
selfDisplayName = s.me.DisplayName
}
return resolveChatTitle(chat, selfMri, selfDisplayName)
}
func resolveChatTitle(chat csa.Chat, selfMri, selfDisplayName string) string {
if title := strings.TrimSpace(chat.Title); title != "" && !sameDisplayName(title, selfDisplayName) {
return title
}
if subject := strings.TrimSpace(chat.MeetingInformation.Subject); subject != "" {
return subject
}
names := chatParticipantNames(chat, selfMri)
switch {
case len(names) == 0:
case len(names) <= 3:
return strings.Join(names, ", ")
default:
return fmt.Sprintf("%s +%d", strings.Join(names[:3], ", "), len(names)-3)
}
if title := strings.TrimSpace(chat.LastMessage.ImDisplayName); title != "" && !sameDisplayName(title, selfDisplayName) {
return title
}
return anonymousChatTitle(chat, selfMri)
}
func chatParticipantNames(chat csa.Chat, selfMri string) []string {
names := []string{}
seen := map[string]struct{}{}
for _, member := range chat.Members {
name := strings.TrimSpace(member.FriendlyName)
if name == "" || member.Mri == selfMri {
continue
}
key := strings.ToLower(name)
if _, ok := seen[key]; ok {
continue
}
seen[key] = struct{}{}
names = append(names, name)
}
sort.Slice(names, func(i, j int) bool {
return strings.ToLower(names[i]) < strings.ToLower(names[j])
})
return names
}
func anonymousChatTitle(chat csa.Chat, selfMri string) string {
participantCount := 0
for _, member := range chat.Members {
if member.Mri == selfMri {
continue
}
participantCount += 1
}
switch {
case strings.EqualFold(chat.ChatType, "meeting"):
return "Meeting chat"
case chat.IsOneOnOne || participantCount <= 1:
return "Direct chat"
case participantCount > 1:
return fmt.Sprintf("Group chat (%d people)", participantCount)
case len(chat.Members) > 0:
return fmt.Sprintf("Group chat (%d people)", len(chat.Members))
default:
return "Conversation"
}
}
func sameDisplayName(left, right string) bool {
left = strings.TrimSpace(left)
right = strings.TrimSpace(right)
if left == "" || right == "" {
return false
}
return strings.EqualFold(left, right)
}