mirror of
https://github.com/Unkn0wnCat/matrix-veles.git
synced 2025-04-30 02:36:52 +02:00
bot: Add linking command
This commit is contained in:
parent
4f587820d1
commit
01fc6a3a1f
2 changed files with 53 additions and 0 deletions
|
@ -19,7 +19,9 @@ package bot
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"github.com/Unkn0wnCat/matrix-veles/internal/db"
|
||||||
"github.com/Unkn0wnCat/matrix-veles/internal/tracer"
|
"github.com/Unkn0wnCat/matrix-veles/internal/tracer"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
"go.opentelemetry.io/otel/attribute"
|
"go.opentelemetry.io/otel/attribute"
|
||||||
"log"
|
"log"
|
||||||
"maunium.net/go/mautrix"
|
"maunium.net/go/mautrix"
|
||||||
|
@ -61,11 +63,46 @@ func handleCommand(command string, sender id.UserID, id id.RoomID, client *mautr
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(command, "link ") {
|
||||||
|
commandLink(sender, id, client, strings.TrimPrefix(command, "link "))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// No match :( - display help
|
// No match :( - display help
|
||||||
commandHelp(sender, id, client)
|
commandHelp(sender, id, client)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func commandLink(userId id.UserID, id id.RoomID, client *mautrix.Client, linkId string) {
|
||||||
|
linkId = strings.Trim(linkId, " \n\r")
|
||||||
|
|
||||||
|
linkIdP, err := primitive.ObjectIDFromHex(linkId)
|
||||||
|
if err != nil {
|
||||||
|
_, _ = client.SendNotice(id, "Invalid Link ID")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
user, err := db.GetUserByID(linkIdP)
|
||||||
|
if err != nil {
|
||||||
|
_, _ = client.SendNotice(id, "Invalid Link ID")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = user.ValidateMXID(userId.String())
|
||||||
|
if err != nil {
|
||||||
|
_, _ = client.SendNotice(id, "No matching link request pending")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = db.SaveUser(user)
|
||||||
|
if err != nil {
|
||||||
|
_, _ = client.SendNotice(id, "Database error")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, _ = client.SendNotice(id, "Successfully linked")
|
||||||
|
}
|
||||||
|
|
||||||
func commandHelp(_ id.UserID, id id.RoomID, client *mautrix.Client) {
|
func commandHelp(_ id.UserID, id id.RoomID, client *mautrix.Client) {
|
||||||
// TODO: Improve help message
|
// TODO: Improve help message
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,10 @@ package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"errors"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DBUser struct {
|
type DBUser struct {
|
||||||
|
@ -20,6 +22,20 @@ type DBUser struct {
|
||||||
Password *string `bson:"-" json:"-"` // Password may never be sent out!
|
Password *string `bson:"-" json:"-"` // Password may never be sent out!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (usr *DBUser) ValidateMXID(mxid string) error {
|
||||||
|
for i, pendingMxid := range usr.PendingMatrixLinks {
|
||||||
|
if strings.EqualFold(*pendingMxid, mxid) {
|
||||||
|
usr.PendingMatrixLinks = append(usr.PendingMatrixLinks[:i], usr.PendingMatrixLinks[i+1:]...)
|
||||||
|
|
||||||
|
usr.MatrixLinks = append(usr.MatrixLinks, &mxid)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return errors.New("not pending")
|
||||||
|
}
|
||||||
|
|
||||||
func (usr *DBUser) HashPassword() error {
|
func (usr *DBUser) HashPassword() error {
|
||||||
if usr.Password == nil {
|
if usr.Password == nil {
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Add table
Reference in a new issue