Add !post command to force-publish next queued story
Typed in a configured channel room, !post pops the head of that channel's queue and sends immediately, bypassing min-interval, burst cap, and daily cap. Canonical-URL dedup still applies. Empty queue gets a threaded reply.
This commit is contained in:
@@ -29,6 +29,10 @@ import (
|
||||
// ReactionHandler is called when a reaction is received on a post.
|
||||
type ReactionHandler func(roomID id.RoomID, eventID id.EventID, targetEventID id.EventID, emoji string, userID id.UserID)
|
||||
|
||||
// MessageHandler is called when a text message is received in a configured room.
|
||||
// body is the plain-text content; channel is the configured channel name for the room.
|
||||
type MessageHandler func(channel string, roomID id.RoomID, eventID id.EventID, sender id.UserID, body string)
|
||||
|
||||
// deviceInfo holds persisted device credentials.
|
||||
type deviceInfo struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
@@ -47,6 +51,7 @@ type Client struct {
|
||||
|
||||
mu sync.RWMutex
|
||||
onReact ReactionHandler
|
||||
onMessage MessageHandler
|
||||
cancelSync context.CancelFunc
|
||||
}
|
||||
|
||||
@@ -217,6 +222,23 @@ func (c *Client) SetReactionHandler(fn ReactionHandler) {
|
||||
c.onReact = fn
|
||||
}
|
||||
|
||||
// SetMessageHandler registers a callback for text messages in configured channel rooms.
|
||||
func (c *Client) SetMessageHandler(fn MessageHandler) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
c.onMessage = fn
|
||||
}
|
||||
|
||||
// ChannelForRoom returns the channel name configured for the given room, if any.
|
||||
func (c *Client) ChannelForRoom(roomID id.RoomID) (string, bool) {
|
||||
for name, rid := range c.channels {
|
||||
if rid == roomID {
|
||||
return name, true
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// Start begins the Matrix sync loop in the background.
|
||||
func (c *Client) Start(ctx context.Context) {
|
||||
syncer := c.mx.Syncer.(*mautrix.DefaultSyncer)
|
||||
@@ -247,6 +269,27 @@ func (c *Client) Start(ctx context.Context) {
|
||||
}
|
||||
})
|
||||
|
||||
// Listen for text messages in configured channel rooms
|
||||
syncer.OnEventType(event.EventMessage, func(ctx context.Context, evt *event.Event) {
|
||||
if evt.Sender == c.userID {
|
||||
return
|
||||
}
|
||||
channel, ok := c.ChannelForRoom(evt.RoomID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
msg := evt.Content.AsMessage()
|
||||
if msg == nil || msg.MsgType != event.MsgText {
|
||||
return
|
||||
}
|
||||
c.mu.RLock()
|
||||
handler := c.onMessage
|
||||
c.mu.RUnlock()
|
||||
if handler != nil {
|
||||
handler(channel, evt.RoomID, evt.ID, evt.Sender, msg.Body)
|
||||
}
|
||||
})
|
||||
|
||||
// Auto-join on invite
|
||||
syncer.OnEventType(event.StateMember, func(ctx context.Context, evt *event.Event) {
|
||||
if evt.GetStateKey() == string(c.userID) && evt.Content.AsMember().Membership == event.MembershipInvite {
|
||||
|
||||
Reference in New Issue
Block a user