aboutsummaryrefslogtreecommitdiff
path: root/telegram/utils.go
diff options
context:
space:
mode:
Diffstat (limited to 'telegram/utils.go')
-rw-r--r--telegram/utils.go91
1 files changed, 75 insertions, 16 deletions
diff --git a/telegram/utils.go b/telegram/utils.go
index cab450f..addeee8 100644
--- a/telegram/utils.go
+++ b/telegram/utils.go
@@ -306,21 +306,19 @@ func (c *Client) ProcessStatusUpdate(chatID int64, status string, show string, o
// JoinMUC saves MUC join fact and sends initialization data
func (c *Client) JoinMUC(chatId int64, resource string, limit int32) {
// save the nickname in this MUC, also as a marker of join
- c.locks.mucResourcesLock.Lock()
- oldMap, ok := c.mucResources[chatId]
+ c.locks.mucCacheLock.Lock()
+ mucState, ok := c.mucCache[chatId]
+ if !ok || mucState == nil {
+ mucState = NewMUCState()
+ c.mucCache[chatId] = mucState
+ }
+ _, ok = mucState.Resources[resource]
if ok {
- _, ok := oldMap[resource]
- if ok {
- // already joined, initializing anyway
- } else {
- oldMap[resource] = true
- }
+ // already joined, initializing anyway
} else {
- newMap := make(map[string]bool)
- newMap[resource] = true
- c.mucResources[chatId] = newMap
+ mucState.Resources[resource] = true
}
- c.locks.mucResourcesLock.Unlock()
+ c.locks.mucCacheLock.Unlock()
c.sendMUCStatuses(chatId)
@@ -332,14 +330,27 @@ func (c *Client) JoinMUC(chatId int64, resource string, limit int32) {
c.sendMUCSubject(chatId, resource)
}
+func (c *Client) getFullName(user *client.User) string {
+ fullName := user.FirstName
+ if user.LastName != "" {
+ fullName = fullName + " " + user.LastName
+ }
+ return fullName
+}
+
func (c *Client) sendMUCStatuses(chatID int64) {
+ c.locks.mucCacheLock.Lock()
+ defer c.locks.mucCacheLock.Unlock()
+ mucState, ok := c.mucCache[chatID]
+ if !ok || mucState == nil {
+ mucState = NewMUCState()
+ c.mucCache[chatID] = mucState
+ }
+
sChatId := strconv.FormatInt(chatID, 10)
myNickname := "me"
if c.me != nil {
- myNickname := c.me.FirstName
- if c.me.LastName != "" {
- myNickname = myNickname + " " + c.me.LastName
- }
+ myNickname = c.getFullName(c.me)
}
myAffiliation := "member"
@@ -364,6 +375,11 @@ func (c *Client) sendMUCStatuses(chatID int64) {
nickname := c.GetMUCNickname(senderId)
affiliation := c.memberStatusToAffiliation(member.Status)
+ mucState.Members[senderId] = &MUCMember{
+ Nickname: nickname,
+ Affiliation: affiliation,
+ }
+
if c.me != nil && senderId == c.me.Id {
myNickname = nickname
myAffiliation = affiliation
@@ -419,6 +435,49 @@ func (c *Client) GetMUCNickname(chatID int64) string {
return c.formatContact(chatID)
}
+func (c *Client) updateMUCsNickname(memberID int64, newNickname string) {
+ c.locks.mucCacheLock.Lock()
+ defer c.locks.mucCacheLock.Unlock()
+
+ for mucId, state := range c.mucCache {
+ oldMember, ok := state.Members[memberID]
+ if ok {
+ state.Members[memberID] = &MUCMember{
+ Nickname: newNickname,
+ Affiliation: oldMember.Affiliation,
+ }
+
+ sMucId := strconv.FormatInt(mucId, 10)
+ unavailableStatusCodes := []uint16{303, 210}
+ availableStatusCodes := []uint16{100, 210}
+ if c.me != nil && memberID == c.me.Id {
+ unavailableStatusCodes = append(unavailableStatusCodes, 110)
+ availableStatusCodes = append(availableStatusCodes, 110)
+ }
+ gateway.SendPresence(
+ c.xmpp,
+ c.jid,
+ gateway.SPType("unavailable"),
+ gateway.SPFrom(sMucId),
+ gateway.SPResource(oldMember.Nickname),
+ gateway.SPImmed(true),
+ gateway.SPMUCAffiliation(oldMember.Affiliation),
+ gateway.SPMUCNick(newNickname),
+ gateway.SPMUCStatusCodes(unavailableStatusCodes),
+ )
+ gateway.SendPresence(
+ c.xmpp,
+ c.jid,
+ gateway.SPFrom(sMucId),
+ gateway.SPResource(newNickname),
+ gateway.SPImmed(true),
+ gateway.SPMUCAffiliation(oldMember.Affiliation),
+ gateway.SPMUCStatusCodes(availableStatusCodes),
+ )
+ }
+ }
+}
+
func (c *Client) formatContact(chatID int64) string {
if chatID == 0 {
return ""