Send captions in a different message and improve other things
This commit is contained in:
		| @@ -80,7 +80,11 @@ func (user *User) newWhatsAppFormatMaps() (map[*regexp.Regexp]string, map[*regex | |||||||
| 		mentionRegex: func(str string) string { | 		mentionRegex: func(str string) string { | ||||||
| 			jid := str[1:] + whatsappExt.NewUserSuffix | 			jid := str[1:] + whatsappExt.NewUserSuffix | ||||||
| 			puppet := user.GetPuppetByJID(jid) | 			puppet := user.GetPuppetByJID(jid) | ||||||
| 			return fmt.Sprintf(`<a href="https://matrix.to/#/%s">%s</a>`, puppet.MXID, puppet.Displayname) | 			mxid := puppet.MXID | ||||||
|  | 			if jid == user.JID() { | ||||||
|  | 				mxid = user.ID | ||||||
|  | 			} | ||||||
|  | 			return fmt.Sprintf(`<a href="https://matrix.to/#/%s">%s</a>`, mxid, puppet.Displayname) | ||||||
| 		}, | 		}, | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										36
									
								
								portal.go
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								portal.go
									
									
									
									
									
								
							| @@ -497,23 +497,22 @@ func (portal *Portal) HandleMediaMessage(download func() ([]byte, error), thumbn | |||||||
| 		portal.log.Errorln("Failed to upload media:", err) | 		portal.log.Errorln("Failed to upload media:", err) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	if len(caption) == 0 { |  | ||||||
| 		caption = info.Id | 	fileName := info.Id | ||||||
| 	exts, _ := mime.ExtensionsByType(mimeType) | 	exts, _ := mime.ExtensionsByType(mimeType) | ||||||
| 	if exts != nil && len(exts) > 0 { | 	if exts != nil && len(exts) > 0 { | ||||||
| 			caption += exts[0] | 		fileName += exts[0] | ||||||
| 		} |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	content := gomatrix.Content{ | 	content := &gomatrix.Content{ | ||||||
| 		Body: caption, | 		Body: fileName, | ||||||
| 		URL:  uploaded.ContentURI, | 		URL:  uploaded.ContentURI, | ||||||
| 		Info: &gomatrix.FileInfo{ | 		Info: &gomatrix.FileInfo{ | ||||||
| 			Size:     len(data), | 			Size:     len(data), | ||||||
| 			MimeType: mimeType, | 			MimeType: mimeType, | ||||||
| 		}, | 		}, | ||||||
| 	} | 	} | ||||||
| 	portal.SetReply(&content, info) | 	portal.SetReply(content, info) | ||||||
|  |  | ||||||
| 	if thumbnail != nil { | 	if thumbnail != nil { | ||||||
| 		thumbnailMime := http.DetectContentType(thumbnail) | 		thumbnailMime := http.DetectContentType(thumbnail) | ||||||
| @@ -545,11 +544,32 @@ func (portal *Portal) HandleMediaMessage(download func() ([]byte, error), thumbn | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	intent.UserTyping(portal.MXID, false, 0) | 	intent.UserTyping(portal.MXID, false, 0) | ||||||
| 	resp, err := intent.SendMassagedMessageEvent(portal.MXID, gomatrix.EventMessage, content, int64(info.Timestamp*1000)) | 	ts := int64(info.Timestamp * 1000) | ||||||
|  | 	resp, err := intent.SendMassagedMessageEvent(portal.MXID, gomatrix.EventMessage, content, ts) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		portal.log.Errorfln("Failed to handle message %s: %v", info.Id, err) | 		portal.log.Errorfln("Failed to handle message %s: %v", info.Id, err) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	if len(caption) > 0 { | ||||||
|  | 		captionContent := &gomatrix.Content{ | ||||||
|  | 			Body:    caption, | ||||||
|  | 			MsgType: gomatrix.MsgNotice, | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		htmlBody := portal.ParseWhatsAppFormat(captionContent.Body) | ||||||
|  | 		if htmlBody != captionContent.Body { | ||||||
|  | 			captionContent.FormattedBody = htmlBody | ||||||
|  | 			captionContent.Format = gomatrix.FormatHTML | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		_, err := intent.SendMassagedMessageEvent(portal.MXID, gomatrix.EventMessage, captionContent, ts) | ||||||
|  | 		if err != nil { | ||||||
|  | 			portal.log.Warnfln("Failed to handle caption of message %s: %v", info.Id, err) | ||||||
|  | 		} | ||||||
|  | 		// TODO store caption mxid? | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	portal.MarkHandled(info.Id, resp.EventID) | 	portal.MarkHandled(info.Id, resp.EventID) | ||||||
| 	portal.log.Debugln("Handled message", info.Id, "->", resp.EventID) | 	portal.log.Debugln("Handled message", info.Id, "->", resp.EventID) | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										9
									
								
								user.go
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								user.go
									
									
									
									
									
								
							| @@ -40,6 +40,7 @@ type User struct { | |||||||
|  |  | ||||||
| 	Admin       bool | 	Admin       bool | ||||||
| 	Whitelisted bool | 	Whitelisted bool | ||||||
|  | 	jid         string | ||||||
|  |  | ||||||
| 	portalsByMXID map[types.MatrixRoomID]*Portal | 	portalsByMXID map[types.MatrixRoomID]*Portal | ||||||
| 	portalsByJID  map[types.WhatsAppID]*Portal | 	portalsByJID  map[types.WhatsAppID]*Portal | ||||||
| @@ -199,7 +200,13 @@ func (user *User) Login(roomID types.MatrixRoomID) { | |||||||
| } | } | ||||||
|  |  | ||||||
| func (user *User) JID() string { | func (user *User) JID() string { | ||||||
| 	return strings.Replace(user.Conn.Info.Wid, whatsappExt.OldUserSuffix, whatsappExt.NewUserSuffix, 1) | 	if user.Conn == nil { | ||||||
|  | 		return "" | ||||||
|  | 	} | ||||||
|  | 	if len(user.jid) == 0 { | ||||||
|  | 		user.jid = strings.Replace(user.Conn.Info.Wid, whatsappExt.OldUserSuffix, whatsappExt.NewUserSuffix, 1) | ||||||
|  | 	} | ||||||
|  | 	return user.jid | ||||||
| } | } | ||||||
|  |  | ||||||
| func (user *User) Sync() { | func (user *User) Sync() { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user