瀏覽代碼

Reimplement open command

Tulir Asokan 3 年之前
父節點
當前提交
345244fedb
共有 3 個文件被更改,包括 33 次插入30 次删除
  1. 30 28
      commands.go
  2. 2 1
      portal.go
  3. 1 1
      user.go

+ 30 - 28
commands.go

@@ -854,7 +854,6 @@ func (handler *CommandHandler) CommandList(ce *CommandEvent) {
 			return
 		}
 		result = formatContacts(ce.User.bridge, contactList)
-
 	} else {
 		groupList, err := ce.User.Client.GetJoinedGroups()
 		if err != nil {
@@ -873,7 +872,7 @@ func (handler *CommandHandler) CommandList(ce *CommandEvent) {
 		if pages == 1 {
 			ce.Reply("There is only 1 page of %s", strings.ToLower(typeName))
 		} else {
-			ce.Reply("There are only %d pages of %s", pages, strings.ToLower(typeName))
+			ce.Reply("There are %d pages of %s", pages, strings.ToLower(typeName))
 		}
 		return
 	}
@@ -892,33 +891,36 @@ func (handler *CommandHandler) CommandOpen(ce *CommandEvent) {
 		ce.Reply("**Usage:** `open <group JID>`")
 		return
 	}
-	ce.Reply("Not yet implemented")
 
-	// TODO reimplement
-	//user := ce.User
-	//jid := ce.Args[0]
-	//if strings.HasSuffix(jid, whatsapp.NewUserSuffix) {
-	//	ce.Reply("That looks like a user JID. Did you mean `pm %s`?", jid[:len(jid)-len(whatsapp.NewUserSuffix)])
-	//	return
-	//}
-	//
-	//user.Conn.Store.ContactsLock.RLock()
-	//contact, ok := user.Conn.Store.Contacts[jid]
-	//user.Conn.Store.ContactsLock.RUnlock()
-	//if !ok {
-	//	ce.Reply("Group JID not found in contacts. Try syncing contacts with `sync` first.")
-	//	return
-	//}
-	//handler.log.Debugln("Importing", jid, "for", user)
-	//portal := user.bridge.GetPortalByJID(database.GroupPortalKey(jid))
-	//if len(portal.MXID) > 0 {
-	//	portal.Sync(user, contact)
-	//	ce.Reply("Portal room synced.")
-	//} else {
-	//	portal.Sync(user, contact)
-	//	ce.Reply("Portal room created.")
-	//}
-	//_, _ = portal.MainIntent().InviteUser(portal.MXID, &mautrix.ReqInviteUser{UserID: user.MXID})
+	var jid types.JID
+	if strings.ContainsRune(ce.Args[0], '@') {
+		jid, _ = types.ParseJID(ce.Args[0])
+	} else {
+		jid = types.NewJID(ce.Args[0], types.GroupServer)
+	}
+	if jid.Server != types.GroupServer || !strings.ContainsRune(jid.User, '-') {
+		ce.Reply("That does not look like a group JID")
+		return
+	}
+
+	info, err := ce.User.Client.GetGroupInfo(jid)
+	if err != nil {
+		ce.Reply("Failed to get group info: %v", err)
+		return
+	}
+	handler.log.Debugln("Importing", jid, "for", ce.User.MXID)
+	portal := ce.User.GetPortalByJID(info.JID)
+	if len(portal.MXID) > 0 {
+		portal.UpdateMatrixRoom(ce.User, info)
+		ce.Reply("Portal room synced.")
+	} else {
+		err = portal.CreateMatrixRoom(ce.User, info)
+		if err != nil {
+			ce.Reply("Failed to create room: %v", err)
+		} else {
+			ce.Reply("Portal room created.")
+		}
+	}
 }
 
 const cmdPMHelp = `pm <_international phone number_> - Open a private chat with the given phone number.`

+ 2 - 1
portal.go

@@ -764,7 +764,7 @@ func (portal *Portal) ensureUserInvited(user *User) (ok bool) {
 	return
 }
 
-func (portal *Portal) Sync(user *User, groupInfo *types.GroupInfo) bool {
+func (portal *Portal) UpdateMatrixRoom(user *User, groupInfo *types.GroupInfo) bool {
 	portal.log.Infoln("Syncing portal for", user.MXID)
 
 	if len(portal.MXID) == 0 {
@@ -773,6 +773,7 @@ func (portal *Portal) Sync(user *User, groupInfo *types.GroupInfo) bool {
 			portal.log.Errorln("Failed to create portal room:", err)
 			return false
 		}
+		return true
 	} else {
 		portal.ensureUserInvited(user)
 	}

+ 1 - 1
user.go

@@ -769,7 +769,7 @@ func (user *User) handleGroupCreate(evt *events.JoinedGroup) {
 			user.log.Errorln("Failed to create Matrix room after join notification: %v", err)
 		}
 	} else {
-		portal.Sync(user, &evt.GroupInfo)
+		portal.UpdateMatrixRoom(user, &evt.GroupInfo)
 	}
 }