Move message parsing to parser subpackage

This commit is contained in:
Tulir Asokan 2018-04-14 11:50:18 +03:00
parent 782ba0657a
commit f5530ff99c
4 changed files with 13 additions and 12 deletions

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package messages
package parser
import (
"fmt"

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package messages
package parser
// TagWithMeta is an open HTML tag with some metadata (e.g. list index, a href value).
type TagWithMeta struct {

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package messages
package parser
import (
"fmt"
@ -24,12 +24,13 @@ import (
"maunium.net/go/gomuks/debug"
"maunium.net/go/gomuks/interface"
"maunium.net/go/gomuks/matrix/rooms"
"maunium.net/go/gomuks/ui/messages"
"maunium.net/go/gomuks/ui/messages/tstring"
"maunium.net/go/gomuks/ui/widget"
"maunium.net/go/tcell"
)
func ParseEvent(gmx ifc.Gomuks, room *rooms.Room, evt *gomatrix.Event) UIMessage {
func ParseEvent(gmx ifc.Gomuks, room *rooms.Room, evt *gomatrix.Event) messages.UIMessage {
member := room.GetMember(evt.Sender)
if member != nil {
evt.Sender = member.DisplayName
@ -51,7 +52,7 @@ func unixToTime(unix int64) time.Time {
return timestamp
}
func ParseMessage(gmx ifc.Gomuks, room *rooms.Room, evt *gomatrix.Event) UIMessage {
func ParseMessage(gmx ifc.Gomuks, room *rooms.Room, evt *gomatrix.Event) messages.UIMessage {
msgtype, _ := evt.Content["msgtype"].(string)
ts := unixToTime(evt.Timestamp)
switch msgtype {
@ -59,10 +60,10 @@ func ParseMessage(gmx ifc.Gomuks, room *rooms.Room, evt *gomatrix.Event) UIMessa
format, hasFormat := evt.Content["format"].(string)
if hasFormat && format == "org.matrix.custom.html" {
text := ParseHTMLMessage(room, evt)
return NewExpandedTextMessage(evt.ID, evt.Sender, msgtype, text, ts)
return messages.NewExpandedTextMessage(evt.ID, evt.Sender, msgtype, text, ts)
} else {
text, _ := evt.Content["body"].(string)
return NewTextMessage(evt.ID, evt.Sender, msgtype, text, ts)
return messages.NewTextMessage(evt.ID, evt.Sender, msgtype, text, ts)
}
case "m.image":
url, _ := evt.Content["url"].(string)
@ -70,7 +71,7 @@ func ParseMessage(gmx ifc.Gomuks, room *rooms.Room, evt *gomatrix.Event) UIMessa
if err != nil {
debug.Printf("Failed to download %s: %v", url, err)
}
return NewImageMessage(gmx, evt.ID, evt.Sender, msgtype, hs, id, data, ts)
return messages.NewImageMessage(gmx, evt.ID, evt.Sender, msgtype, hs, id, data, ts)
}
return nil
}
@ -120,8 +121,8 @@ func getMembershipEventContent(evt *gomatrix.Event) (sender string, text tstring
return
}
func ParseMembershipEvent(evt *gomatrix.Event) UIMessage {
func ParseMembershipEvent(evt *gomatrix.Event) messages.UIMessage {
sender, text := getMembershipEventContent(evt)
ts := unixToTime(evt.Timestamp)
return NewExpandedTextMessage(evt.ID, sender, "m.room.membership", text, ts)
return messages.NewExpandedTextMessage(evt.ID, sender, "m.room.membership", text, ts)
}

View File

@ -31,7 +31,7 @@ import (
"maunium.net/go/gomuks/lib/notification"
"maunium.net/go/gomuks/matrix/pushrules"
"maunium.net/go/gomuks/matrix/rooms"
"maunium.net/go/gomuks/ui/messages"
"maunium.net/go/gomuks/ui/messages/parser"
"maunium.net/go/gomuks/ui/widget"
"maunium.net/go/tcell"
"maunium.net/go/tview"
@ -465,5 +465,5 @@ func (view *MainView) LoadHistory(room string, initial bool) {
}
func (view *MainView) ParseEvent(roomView ifc.RoomView, evt *gomatrix.Event) ifc.Message {
return messages.ParseEvent(view.gmx, roomView.MxRoom(), evt)
return parser.ParseEvent(view.gmx, roomView.MxRoom(), evt)
}