Use create key to deduplicate group creates

Doesn't work if there are multiple Matrix users, but better than nothing
This commit is contained in:
Tulir Asokan 2023-05-10 22:41:09 +03:00
parent e86c494f0f
commit e3174a7bd7
2 changed files with 11 additions and 1 deletions

View file

@ -382,8 +382,11 @@ func fnCreate(ce *WrappedCommandEvent) {
}
// TODO check m.space.parent to create rooms directly in communities
ce.Log.Infofln("Creating group for %s with name %s and participants %+v", ce.RoomID, roomNameEvent.Name, participants)
messageID := whatsmeow.GenerateMessageID()
ce.Log.Infofln("Creating group for %s with name %s and participants %+v (create key: %s)", ce.RoomID, roomNameEvent.Name, participants, messageID)
ce.User.createKeyDedup = messageID
resp, err := ce.User.Client.CreateGroup(whatsmeow.ReqCreateGroup{
CreateKey: messageID,
Name: roomNameEvent.Name,
Participants: participants,
GroupParent: types.GroupParent{
@ -420,6 +423,7 @@ func fnCreate(ce *WrappedCommandEvent) {
portal.Update(nil)
portal.UpdateBridgeInfo()
ce.User.createKeyDedup = ""
ce.Reply("Successfully created WhatsApp group %s", portal.Key.JID)
}

View file

@ -94,6 +94,8 @@ type User struct {
resyncQueue map[types.JID]resyncQueueItem
resyncQueueLock sync.Mutex
nextResync time.Time
createKeyDedup string
}
type resyncQueueItem struct {
@ -1299,6 +1301,10 @@ func (user *User) markUnread(portal *Portal, unread bool) {
func (user *User) handleGroupCreate(evt *events.JoinedGroup) {
portal := user.GetPortalByJID(evt.JID)
if len(portal.MXID) == 0 {
if evt.CreateKey == user.createKeyDedup {
user.log.Debugfln("Ignoring group create event with key %s", evt.CreateKey)
return
}
err := portal.CreateMatrixRoom(user, &evt.GroupInfo, true, true)
if err != nil {
user.log.Errorln("Failed to create Matrix room after join notification: %v", err)