always reply to a specific message; moved matrix-related utils to the linkpearl; refactoring
This commit is contained in:
8
vendor/gitlab.com/etke.cc/linkpearl/accountdata.go
generated
vendored
8
vendor/gitlab.com/etke.cc/linkpearl/accountdata.go
generated
vendored
@@ -24,7 +24,7 @@ func (l *Linkpearl) GetAccountData(name string) (map[string]string, error) {
|
||||
l.acc.Add(name, data)
|
||||
return data, nil
|
||||
}
|
||||
return data, err
|
||||
return data, UnwrapError(err)
|
||||
}
|
||||
data = l.decryptAccountData(data)
|
||||
|
||||
@@ -37,7 +37,7 @@ func (l *Linkpearl) SetAccountData(name string, data map[string]string) error {
|
||||
l.acc.Add(name, data)
|
||||
|
||||
data = l.encryptAccountData(data)
|
||||
return l.GetClient().SetAccountData(name, data)
|
||||
return UnwrapError(l.GetClient().SetAccountData(name, data))
|
||||
}
|
||||
|
||||
// GetRoomAccountData of the room (from cache and API, with encryption support)
|
||||
@@ -59,7 +59,7 @@ func (l *Linkpearl) GetRoomAccountData(roomID id.RoomID, name string) (map[strin
|
||||
l.acc.Add(key, data)
|
||||
return data, nil
|
||||
}
|
||||
return data, err
|
||||
return data, UnwrapError(err)
|
||||
}
|
||||
data = l.decryptAccountData(data)
|
||||
|
||||
@@ -73,7 +73,7 @@ func (l *Linkpearl) SetRoomAccountData(roomID id.RoomID, name string, data map[s
|
||||
l.acc.Add(key, data)
|
||||
|
||||
data = l.encryptAccountData(data)
|
||||
return l.GetClient().SetRoomAccountData(roomID, name, data)
|
||||
return UnwrapError(l.GetClient().SetRoomAccountData(roomID, name, data))
|
||||
}
|
||||
|
||||
func (l *Linkpearl) encryptAccountData(data map[string]string) map[string]string {
|
||||
|
||||
22
vendor/gitlab.com/etke.cc/linkpearl/send.go
generated
vendored
22
vendor/gitlab.com/etke.cc/linkpearl/send.go
generated
vendored
@@ -1,8 +1,6 @@
|
||||
package linkpearl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"maunium.net/go/mautrix"
|
||||
"maunium.net/go/mautrix/event"
|
||||
"maunium.net/go/mautrix/format"
|
||||
@@ -16,24 +14,22 @@ func (l *Linkpearl) Send(roomID id.RoomID, content interface{}) (id.EventID, err
|
||||
l.log.Debug().Str("roomID", roomID.String()).Any("content", content).Msg("sending event")
|
||||
resp, err := l.api.SendMessageEvent(roomID, event.EventMessage, content)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", UnwrapError(err)
|
||||
}
|
||||
return resp.EventID, nil
|
||||
}
|
||||
|
||||
// SendNotice to a room with optional thread relation
|
||||
func (l *Linkpearl) SendNotice(roomID id.RoomID, threadID id.EventID, message string, args ...interface{}) {
|
||||
content := format.RenderMarkdown(fmt.Sprintf(message, args...), true, true)
|
||||
if threadID != "" {
|
||||
content.RelatesTo = &event.RelatesTo{
|
||||
Type: event.RelThread,
|
||||
EventID: threadID,
|
||||
}
|
||||
// SendNotice to a room with optional relations, markdown supported
|
||||
func (l *Linkpearl) SendNotice(roomID id.RoomID, message string, relates ...*event.RelatesTo) {
|
||||
content := format.RenderMarkdown(message, true, true)
|
||||
content.MsgType = event.MsgNotice
|
||||
if len(relates) > 0 {
|
||||
content.RelatesTo = relates[0]
|
||||
}
|
||||
|
||||
_, err := l.Send(roomID, &content)
|
||||
if err != nil {
|
||||
l.log.Error().Err(err).Str("roomID", roomID.String()).Msg("cannot send a notice int the room")
|
||||
l.log.Error().Err(UnwrapError(err)).Str("roomID", roomID.String()).Msg("cannot send a notice int the room")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,6 +37,7 @@ func (l *Linkpearl) SendNotice(roomID id.RoomID, threadID id.EventID, message st
|
||||
func (l *Linkpearl) SendFile(roomID id.RoomID, req *mautrix.ReqUploadMedia, msgtype event.MessageType, relation *event.RelatesTo) error {
|
||||
resp, err := l.GetClient().UploadMedia(*req)
|
||||
if err != nil {
|
||||
err = UnwrapError(err)
|
||||
l.log.Error().Err(err).Str("file", req.FileName).Msg("cannot upload file")
|
||||
return err
|
||||
}
|
||||
@@ -52,6 +49,7 @@ func (l *Linkpearl) SendFile(roomID id.RoomID, req *mautrix.ReqUploadMedia, msgt
|
||||
RelatesTo: relation,
|
||||
},
|
||||
})
|
||||
err = UnwrapError(err)
|
||||
if err != nil {
|
||||
l.log.Error().Err(err).Str("file", req.FileName).Msg("cannot send uploaded file")
|
||||
}
|
||||
|
||||
84
vendor/gitlab.com/etke.cc/linkpearl/utils.go
generated
vendored
Normal file
84
vendor/gitlab.com/etke.cc/linkpearl/utils.go
generated
vendored
Normal file
@@ -0,0 +1,84 @@
|
||||
package linkpearl
|
||||
|
||||
import (
|
||||
"github.com/rs/zerolog"
|
||||
"maunium.net/go/mautrix"
|
||||
"maunium.net/go/mautrix/event"
|
||||
"maunium.net/go/mautrix/id"
|
||||
)
|
||||
|
||||
// EventParent returns parent event ID (either from thread or from reply-to relation)
|
||||
func EventParent(currentID id.EventID, content *event.MessageEventContent) id.EventID {
|
||||
if content == nil {
|
||||
return currentID
|
||||
}
|
||||
|
||||
relation := content.OptionalGetRelatesTo()
|
||||
if relation == nil {
|
||||
return currentID
|
||||
}
|
||||
|
||||
threadParent := relation.GetThreadParent()
|
||||
if threadParent != "" {
|
||||
return threadParent
|
||||
}
|
||||
|
||||
replyParent := relation.GetReplyTo()
|
||||
if replyParent != "" {
|
||||
return replyParent
|
||||
}
|
||||
|
||||
return currentID
|
||||
}
|
||||
|
||||
// EventField returns field value from raw event content
|
||||
func EventField[T any](content *event.Content, field string) T {
|
||||
var zero T
|
||||
raw := content.Raw[field]
|
||||
if raw == nil {
|
||||
return zero
|
||||
}
|
||||
|
||||
v, ok := raw.(T)
|
||||
if !ok {
|
||||
return zero
|
||||
}
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
func ParseContent(evt *event.Event, eventType event.Type, log *zerolog.Logger) {
|
||||
if evt.Content.Parsed != nil {
|
||||
return
|
||||
}
|
||||
perr := evt.Content.ParseRaw(eventType)
|
||||
if perr != nil {
|
||||
log.Error().Err(perr).Msg("cannot parse event content")
|
||||
}
|
||||
}
|
||||
|
||||
// UnwrapError tries to unwrap a error into something meaningful, like mautrix.HTTPError or mautrix.RespError
|
||||
func UnwrapError(err error) error {
|
||||
switch err.(type) {
|
||||
case nil:
|
||||
return nil
|
||||
case mautrix.HTTPError:
|
||||
return unwrapHTTPError(err)
|
||||
default:
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func unwrapHTTPError(err error) error {
|
||||
httperr, ok := err.(mautrix.HTTPError)
|
||||
if !ok {
|
||||
return err
|
||||
}
|
||||
|
||||
uwerr := httperr.Unwrap()
|
||||
if uwerr != nil {
|
||||
return uwerr
|
||||
}
|
||||
|
||||
return httperr
|
||||
}
|
||||
Reference in New Issue
Block a user