0
0
mirror of https://github.com/thegeeklab/wp-matrix.git synced 2024-06-02 18:39:40 +02:00

use single NewClient method

This commit is contained in:
Robert Kaussow 2024-05-10 23:41:30 +02:00
parent fb149a715f
commit d670d4b115
Signed by: xoxys
GPG Key ID: 4E692A2EAECC03C0
5 changed files with 60 additions and 47 deletions

View File

@ -27,15 +27,47 @@ type MessageOptions struct {
TemplateUnsafe bool
}
// NewClient creates a new Client instance with the provided mautrix.Client.
func NewClient(client *mautrix.Client) *Client {
return &Client{
client: client,
Message: &Message{
client: client,
Opt: MessageOptions{},
},
// NewClient creates a new Matrix client with the given parameters and joins the specified room.
// It authenticates the user if the userID and token are not provided, and returns a Client struct
// that can be used to send messages to the room.
func NewClient(ctx context.Context, url, roomID, userID, token, username, password string) (*Client, error) {
muid := id.NewUserID(EnsurePrefix("@", userID), url)
c, err := mautrix.NewClient(url, muid, token)
if err != nil {
return nil, err
}
if userID == "" || token == "" {
_, err := c.Login(
ctx,
&mautrix.ReqLogin{
Type: "m.login.password",
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: username},
Password: password,
InitialDeviceDisplayName: "Woodpecker CI",
StoreCredentials: true,
},
)
if err != nil {
return nil, fmt.Errorf("failed to authenticate user: %w", err)
}
}
joinResp, err := c.JoinRoom(ctx, EnsurePrefix("!", roomID), "", nil)
if err != nil {
return nil, fmt.Errorf("failed to join room: %w", err)
}
return &Client{
client: c,
Message: &Message{
client: c,
Opt: MessageOptions{
RoomID: joinResp.RoomID,
},
},
}, nil
}
// Send sends a message to the specified room. It sanitizes the message content
@ -50,7 +82,7 @@ func (m *Message) Send(ctx context.Context) error {
_, err := m.client.SendMessageEvent(ctx, m.Opt.RoomID, event.EventMessage, content)
if err != nil {
return fmt.Errorf("failed to send message: %w", err)
return err
}
return nil

View File

@ -74,7 +74,7 @@ type MockAPIClient_SendMessageEvent_Call struct {
// - eventType event.Type
// - contentJSON interface{}
// - extra ...mautrix.ReqSendEvent
func (_e *MockAPIClient_Expecter) SendMessageEvent(ctx interface{}, roomID interface{}, eventType interface{}, contentJSON interface{}, extra ...interface{}) *MockAPIClient_SendMessageEvent_Call {
func (_e *MockAPIClient_Expecter) SendMessageEvent(ctx, roomID, eventType, contentJSON interface{}, extra ...interface{}) *MockAPIClient_SendMessageEvent_Call {
return &MockAPIClient_SendMessageEvent_Call{Call: _e.mock.On("SendMessageEvent",
append([]interface{}{ctx, roomID, eventType, contentJSON}, extra...)...)}
}
@ -107,7 +107,8 @@ func (_c *MockAPIClient_SendMessageEvent_Call) RunAndReturn(run func(context.Con
func NewMockAPIClient(t interface {
mock.TestingT
Cleanup(func())
}) *MockAPIClient {
},
) *MockAPIClient {
mock := &MockAPIClient{}
mock.Mock.Test(t)

View File

@ -1,4 +1,4 @@
package plugin
package matrix
import "strings"

View File

@ -1,4 +1,4 @@
package plugin
package matrix
import "testing"

View File

@ -14,8 +14,6 @@ import (
"github.com/rs/zerolog/log"
"github.com/thegeeklab/wp-matrix/matrix"
"github.com/thegeeklab/wp-plugin-go/v2/template"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/id"
)
var ErrAuthSourceNotSet = errors.New("either username and password or userid and accesstoken are required")
@ -45,44 +43,26 @@ func (p *Plugin) Validate() error {
// Execute provides the implementation of the plugin.
func (p *Plugin) Execute() error {
muid := id.NewUserID(EnsurePrefix("@", p.Settings.UserID), p.Settings.Homeserver)
mc, err := mautrix.NewClient(p.Settings.Homeserver, muid, p.Settings.AccessToken)
if err != nil {
return fmt.Errorf("failed to initialize client: %w", err)
}
if p.Settings.UserID == "" || p.Settings.AccessToken == "" {
_, err := mc.Login(
p.Network.Context,
&mautrix.ReqLogin{
Type: "m.login.password",
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: p.Settings.Username},
Password: p.Settings.Password,
InitialDeviceDisplayName: "Woodpecker CI",
StoreCredentials: true,
},
)
if err != nil {
return fmt.Errorf("failed to authenticate user: %w", err)
}
}
log.Info().Msg("logged in successfully")
joinResp, err := mc.JoinRoom(p.Network.Context, EnsurePrefix("!", p.Settings.RoomID), "", nil)
if err != nil {
return fmt.Errorf("failed to join room: %w", err)
}
msg, err := p.CreateMessage()
if err != nil {
return fmt.Errorf("failed to create message: %w", err)
}
client := matrix.NewClient(mc)
client, err := matrix.NewClient(
p.Network.Context,
p.Settings.Homeserver,
p.Settings.RoomID,
p.Settings.UserID,
p.Settings.AccessToken,
p.Settings.Username,
p.Settings.Password,
)
if err != nil {
return fmt.Errorf("failed to initialize client: %w", err)
}
client.Message.Opt = matrix.MessageOptions{
RoomID: joinResp.RoomID,
RoomID: client.Message.Opt.RoomID,
Message: msg,
TemplateUnsafe: p.Settings.TemplateUnsafe,
}