database: migrate disappearing timer to standard location (#121)

This commit is contained in:
Tulir Asokan
2025-09-12 17:35:09 +02:00
committed by GitHub
parent 93f55497f4
commit 0051042555
6 changed files with 29 additions and 33 deletions
+2 -2
View File
@@ -57,7 +57,7 @@ func (tg *TelegramConnector) GetCapabilities() *bridgev2.NetworkGeneralCapabilit
}
func (tg *TelegramConnector) GetBridgeInfoVersion() (info, capabilities int) {
return 1, 3
return 1, 4
}
// TODO get these from getConfig instead of hardcoding?
@@ -207,7 +207,7 @@ func makeTimerList() []jsontime.Milliseconds {
var telegramTimers = makeTimerList()
func (t *TelegramClient) GetCapabilities(ctx context.Context, portal *bridgev2.Portal) *event.RoomFeatures {
baseID := "fi.mau.telegram.capabilities.2025_08_26"
baseID := "fi.mau.telegram.capabilities.2025_09_11"
feat := &event.RoomFeatures{
Formatting: formattingCaps,
File: fileCaps,
-1
View File
@@ -49,7 +49,6 @@ type GhostMetadata struct {
type PortalMetadata struct {
IsSuperGroup bool `json:"is_supergroup,omitempty"`
ReadUpTo int `json:"read_up_to,omitempty"`
MessagesTTL int `json:"messages_ttl,omitempty"`
AllowedReactions []string `json:"allowed_reactions"`
}
+1 -1
View File
@@ -1,4 +1,4 @@
-- v0 -> v2: Latest revision
-- v0 -> v3 (compatible with v2+): Latest revision
CREATE TABLE telegram_user_state (
user_id BIGINT NOT NULL PRIMARY KEY,
@@ -0,0 +1,15 @@
-- v3 (compatible with v2+): Migrate disappearing timer to standard column
-- only: postgres
UPDATE portal SET disappear_type='after_send', disappear_timer=(metadata->>'messages_ttl')::BIGINT * 1_000_000_000;
-- only: sqlite
UPDATE portal SET disappear_type='after_send', disappear_timer=CAST(metadata->>'$.messages_ttl' AS INTEGER) * 1_000_000_000;
-- The above migration sets disappear type/timer for all portals, so clear out the ones that don't have a TTL
UPDATE portal SET disappear_type=NULL, disappear_timer=NULL WHERE disappear_timer=0;
-- Finally, reset the timer set ca
-- only: postgres
UPDATE portal SET cap_state=jsonb_delete(cap_state, 'flags') WHERE disappear_timer IS NOT NULL;
-- only: sqlite
UPDATE portal SET cap_state=json_remove(cap_state, '$.flags') WHERE disappear_timer IS NOT NULL;
+9 -24
View File
@@ -356,31 +356,16 @@ func (t *TelegramClient) onUpdateNewMessage(ctx context.Context, entities tg.Ent
return err
}
case *tg.MessageActionSetMessagesTTL:
res := t.main.Bridge.QueueRemoteEvent(t.userLogin, &simplevent.ChatResync{
EventMeta: eventMeta.WithType(bridgev2.RemoteEventChatResync),
ChatInfo: &bridgev2.ChatInfo{
ExtraUpdates: func(ctx context.Context, p *bridgev2.Portal) bool {
updated := p.Portal.Metadata.(*PortalMetadata).MessagesTTL != action.Period
p.Portal.Metadata.(*PortalMetadata).MessagesTTL = action.Period
return updated
},
},
})
if err := resultToError(res); err != nil {
return err
setting := database.DisappearingSetting{
Type: event.DisappearingTypeAfterSend,
Timer: time.Duration(action.Period) * time.Second,
}
// Send a notice about the TTL change
content := bridgev2.DisappearingMessageNotice(time.Duration(action.Period)*time.Second, false)
res = t.main.Bridge.QueueRemoteEvent(t.userLogin, &simplevent.Message[any]{
EventMeta: eventMeta.WithType(bridgev2.RemoteEventMessage),
ID: ids.GetMessageIDFromMessage(msg),
ConvertMessageFunc: func(ctx context.Context, portal *bridgev2.Portal, intent bridgev2.MatrixAPI, data any) (*bridgev2.ConvertedMessage, error) {
return &bridgev2.ConvertedMessage{
Parts: []*bridgev2.ConvertedMessagePart{
{Type: event.EventMessage, Content: content},
},
}, nil
res := t.main.Bridge.QueueRemoteEvent(t.userLogin, &simplevent.ChatInfoChange{
EventMeta: eventMeta.WithType(bridgev2.RemoteEventChatInfoChange),
ChatInfoChange: &bridgev2.ChatInfoChange{
ChatInfo: &bridgev2.ChatInfo{
Disappear: &setting,
},
},
})
if err := resultToError(res); err != nil {
+2 -5
View File
@@ -278,11 +278,8 @@ func (c *TelegramClient) convertToMatrix(ctx context.Context, portal *bridgev2.P
Type: event.DisappearingTypeAfterSend,
Timer: time.Duration(ttl) * time.Second,
}
} else if portal.Metadata.(*PortalMetadata).MessagesTTL > 0 {
cm.Disappear = database.DisappearingSetting{
Type: event.DisappearingTypeAfterSend,
Timer: time.Duration(ttl) * time.Second,
}
} else {
cm.Disappear = portal.Disappear
}
}