0
0
mirror of https://github.com/thegeeklab/wp-matrix.git synced 2024-09-19 15:12:47 +02:00
wp-matrix/plugin/impl.go

102 lines
2.8 KiB
Go
Raw Normal View History

2020-11-25 20:44:45 +01:00
// Copyright (c) 2020, the Drone Plugins project authors.
// Copyright (c) 2021, Robert Kaussow <mail@thegeeklab.de>
2020-11-25 20:44:45 +01:00
// Use of this source code is governed by an Apache 2.0 license that can be
// found in the LICENSE file.
package plugin
import (
"context"
"errors"
2020-11-25 20:44:45 +01:00
"fmt"
"github.com/rs/zerolog/log"
"github.com/thegeeklab/wp-plugin-go/v2/template"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/id"
2020-11-25 20:44:45 +01:00
)
var ErrAuthSourceNotSet = errors.New("either username and password or userid and accesstoken are required")
//nolint:revive
2023-08-21 11:36:07 +02:00
func (p *Plugin) run(ctx context.Context) error {
if err := p.Validate(); err != nil {
return fmt.Errorf("validation failed: %w", err)
}
if err := p.Execute(); err != nil {
return fmt.Errorf("execution failed: %w", err)
}
return nil
}
2020-11-25 20:44:45 +01:00
// Validate handles the settings validation of the plugin.
func (p *Plugin) Validate() error {
if (p.Settings.Username == "" || p.Settings.Password == "") &&
(p.Settings.UserID == "" || p.Settings.AccessToken == "") {
return ErrAuthSourceNotSet
}
2020-11-25 20:44:45 +01:00
return nil
}
// Execute provides the implementation of the plugin.
func (p *Plugin) Execute() error {
muid := id.NewUserID(EnsurePrefix("@", p.Settings.UserID), p.Settings.Homeserver)
matrix, err := mautrix.NewClient(p.Settings.Homeserver, muid, p.Settings.AccessToken)
2020-11-25 20:44:45 +01:00
if err != nil {
return fmt.Errorf("failed to initialize client: %w", err)
}
if p.Settings.UserID == "" || p.Settings.AccessToken == "" {
_, err := matrix.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,
},
)
2020-11-25 20:44:45 +01:00
if err != nil {
return fmt.Errorf("failed to authenticate user: %w", err)
}
}
log.Info().Msg("logged in successfully")
2020-11-25 20:44:45 +01:00
joinResp, err := matrix.JoinRoom(p.Network.Context, EnsurePrefix("!", p.Settings.RoomID), "", nil)
2020-11-25 20:44:45 +01:00
if err != nil {
return fmt.Errorf("failed to join room: %w", err)
}
msg, err := p.CreateMessage()
2020-11-25 20:44:45 +01:00
if err != nil {
return fmt.Errorf("failed to create message: %w", err)
2020-11-25 20:44:45 +01:00
}
client := NewMatrixClient(matrix)
client.Message.Opt = MatrixMessageOpt{
RoomID: joinResp.RoomID,
Message: msg,
TemplateUnsafe: p.Settings.TemplateUnsafe,
2020-11-25 20:44:45 +01:00
}
if err := client.Message.Send(p.Network.Context); err != nil {
return fmt.Errorf("failed to send message: %w", err)
}
log.Info().Msg("message sent successfully")
return nil
}
// CreateMessage generates a message string based on the plugin's template and metadata.
func (p *Plugin) CreateMessage() (string, error) {
return template.RenderTrim(p.Network.Context, *p.Network.Client, p.Settings.Template, p.Metadata)
2020-11-25 20:44:45 +01:00
}