chore: retrieved base logic files from matrix bridge

This commit is contained in:
Felipe M 2025-07-31 12:17:24 +02:00
parent b10a439a29
commit 202622f2c4
No known key found for this signature in database
GPG key ID: 52E5D65FCF99808A
5 changed files with 600 additions and 1 deletions

View file

@ -0,0 +1,79 @@
package kvstore
// KV Store key prefixes and constants
// This file centralizes all KV store key patterns used throughout the plugin
// to ensure consistency and avoid key conflicts.
const (
// CurrentKVStoreVersion is the current version requiring migrations
CurrentKVStoreVersion = 2
// KeyPrefixMatrixUser is the prefix for Matrix user ID -> Mattermost user ID mappings
KeyPrefixMatrixUser = "matrix_user_"
// KeyPrefixMattermostUser is the prefix for Mattermost user ID -> Matrix user ID mappings
KeyPrefixMattermostUser = "mattermost_user_"
// KeyPrefixChannelMapping is the prefix for Mattermost channel ID -> Matrix room mappings
KeyPrefixChannelMapping = "channel_mapping_"
// KeyPrefixRoomMapping is the prefix for Matrix room identifier -> Mattermost channel ID mappings
KeyPrefixRoomMapping = "room_mapping_"
// KeyPrefixGhostUser is the prefix for Mattermost user ID -> Matrix ghost user ID cache
KeyPrefixGhostUser = "ghost_user_"
// KeyPrefixGhostRoom is the prefix for ghost user room membership tracking
KeyPrefixGhostRoom = "ghost_room_"
// KeyPrefixMatrixEventPost is the prefix for Matrix event ID -> Mattermost post ID mappings
KeyPrefixMatrixEventPost = "matrix_event_post_"
// KeyPrefixMatrixReaction is the prefix for Matrix reaction event ID -> reaction info mappings
KeyPrefixMatrixReaction = "matrix_reaction_"
// KeyStoreVersion is the key for tracking the current KV store schema version
KeyStoreVersion = "kv_store_version"
// KeyPrefixLegacyDMMapping was the old prefix for DM mappings (migrated to channel_mapping_)
KeyPrefixLegacyDMMapping = "dm_mapping_"
// KeyPrefixLegacyMatrixDMMapping was the old prefix for Matrix DM mappings (migrated to room_mapping_)
KeyPrefixLegacyMatrixDMMapping = "matrix_dm_mapping_"
)
// Helper functions for building KV store keys
// BuildMatrixUserKey creates a key for Matrix user -> Mattermost user mapping
func BuildMatrixUserKey(matrixUserID string) string {
return KeyPrefixMatrixUser + matrixUserID
}
// BuildMattermostUserKey creates a key for Mattermost user -> Matrix user mapping
func BuildMattermostUserKey(mattermostUserID string) string {
return KeyPrefixMattermostUser + mattermostUserID
}
// BuildChannelMappingKey creates a key for channel -> room mapping
func BuildChannelMappingKey(channelID string) string {
return KeyPrefixChannelMapping + channelID
}
// BuildRoomMappingKey creates a key for room -> channel mapping
func BuildRoomMappingKey(roomIdentifier string) string {
return KeyPrefixRoomMapping + roomIdentifier
}
// BuildGhostUserKey creates a key for ghost user cache
func BuildGhostUserKey(mattermostUserID string) string {
return KeyPrefixGhostUser + mattermostUserID
}
// BuildGhostRoomKey creates a key for ghost user room membership
func BuildGhostRoomKey(mattermostUserID, roomID string) string {
return KeyPrefixGhostRoom + mattermostUserID + "_" + roomID
}
// BuildMatrixEventPostKey creates a key for Matrix event -> post mapping
func BuildMatrixEventPostKey(matrixEventID string) string {
return KeyPrefixMatrixEventPost + matrixEventID
}
// BuildMatrixReactionKey creates a key for Matrix reaction storage
func BuildMatrixReactionKey(reactionEventID string) string {
return KeyPrefixMatrixReaction + reactionEventID
}

View file

@ -1,6 +1,13 @@
// Package kvstore provides a key-value store interface for plugin data persistence.
package kvstore
// KVStore provides an interface for key-value storage operations.
type KVStore interface {
// Define your methods here. This package is used to access the KVStore pluginapi methods.
GetTemplateData(userID string) (string, error)
Get(key string) ([]byte, error)
Set(key string, value []byte) error
Delete(key string) error
ListKeys(page, perPage int) ([]string, error)
ListKeysWithPrefix(page, perPage int, prefix string) ([]string, error)
}

View file

@ -8,17 +8,19 @@ import (
// We expose our calls to the KVStore pluginapi methods through this interface for testability and stability.
// This allows us to better control which values are stored with which keys.
// Client wraps the Mattermost plugin API client for KV store operations.
type Client struct {
client *pluginapi.Client
}
// NewKVStore creates a new KVStore implementation using the provided plugin API client.
func NewKVStore(client *pluginapi.Client) KVStore {
return Client{
client: client,
}
}
// Sample method to get a key-value pair in the KV store
// GetTemplateData retrieves template data for a specific user from the KV store.
func (kv Client) GetTemplateData(userID string) (string, error) {
var templateData string
err := kv.client.KV.Get("template_key-"+userID, &templateData)
@ -27,3 +29,49 @@ func (kv Client) GetTemplateData(userID string) (string, error) {
}
return templateData, nil
}
// Get retrieves a value from the KV store by key.
func (kv Client) Get(key string) ([]byte, error) {
var data []byte
err := kv.client.KV.Get(key, &data)
if err != nil {
return nil, errors.Wrap(err, "failed to get key from KV store")
}
return data, nil
}
// Set stores a key-value pair in the KV store.
func (kv Client) Set(key string, value []byte) error {
_, appErr := kv.client.KV.Set(key, value)
if appErr != nil {
return errors.Wrap(appErr, "failed to set key in KV store")
}
return nil
}
// Delete removes a key-value pair from the KV store.
func (kv Client) Delete(key string) error {
appErr := kv.client.KV.Delete(key)
if appErr != nil {
return errors.Wrap(appErr, "failed to delete key from KV store")
}
return nil
}
// ListKeys retrieves a paginated list of keys from the KV store.
func (kv Client) ListKeys(page, perPage int) ([]string, error) {
keys, appErr := kv.client.KV.ListKeys(page, perPage)
if appErr != nil {
return nil, errors.Wrap(appErr, "failed to list keys from KV store")
}
return keys, nil
}
// ListKeysWithPrefix retrieves a paginated list of keys with a specific prefix from the KV store.
func (kv Client) ListKeysWithPrefix(page, perPage int, prefix string) ([]string, error) {
keys, appErr := kv.client.KV.ListKeys(page, perPage, pluginapi.WithPrefix(prefix))
if appErr != nil {
return nil, errors.Wrap(appErr, "failed to list keys with prefix from KV store")
}
return keys, nil
}