浏览代码

Use create key to deduplicate group creates

Doesn't work if there are multiple Matrix users, but better than nothing
Tulir Asokan 2 年之前
父节点
当前提交
e3174a7bd7
共有 2 个文件被更改,包括 11 次插入1 次删除
  1. 5 1
      commands.go
  2. 6 0
      user.go

+ 5 - 1
commands.go

@@ -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)
 }

+ 6 - 0
user.go

@@ -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)