fix: sync harden channel reads and suggested approvals
This commit is contained in:
parent
037ce017d4
commit
209a0d279b
14 changed files with 467 additions and 26 deletions
|
|
@ -41,6 +41,23 @@ func TestSetPaidMessagesPriceCreatesAndReusesMonoforum(t *testing.T) {
|
|||
if mono.TopMessageID == 0 || mono.Pts == 0 {
|
||||
t.Fatalf("monoforum top/pts = %d/%d, want service top message", mono.TopMessageID, mono.Pts)
|
||||
}
|
||||
for _, userID := range []int64{1, 42} {
|
||||
read, err := store.ReadChannelHistory(ctx, domain.ReadChannelHistoryRequest{
|
||||
UserID: userID, ChannelID: monoID, MaxID: mono.TopMessageID, Date: 1_700_000_901,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("synthetic monoforum read for %d: %v", userID, err)
|
||||
}
|
||||
if !read.ReadOnly || read.Changed || read.MaxID != mono.TopMessageID {
|
||||
t.Fatalf("synthetic monoforum read for %d = %+v, want read-only no-op at %d", userID, read, mono.TopMessageID)
|
||||
}
|
||||
if _, exists := store.members[monoID][userID]; exists {
|
||||
t.Fatalf("synthetic monoforum read persisted member for %d", userID)
|
||||
}
|
||||
if _, exists := store.dialogs[userID][monoID]; exists {
|
||||
t.Fatalf("synthetic monoforum read persisted dialog for %d", userID)
|
||||
}
|
||||
}
|
||||
dialogs, err := store.ListChannelDialogs(ctx, 1, domain.DialogFilter{Limit: 100})
|
||||
if err != nil {
|
||||
t.Fatalf("list dialogs after enable: %v", err)
|
||||
|
|
|
|||
|
|
@ -252,7 +252,7 @@ func (s *ChannelStore) ReadChannelMentions(_ context.Context, req domain.ReadCha
|
|||
func (s *ChannelStore) ReadChannelHistory(_ context.Context, req domain.ReadChannelHistoryRequest) (domain.ReadChannelHistoryResult, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
channel, err := s.channelForMemberLocked(req.UserID, req.ChannelID)
|
||||
channel, _, readOnly, err := s.channelForViewerLocked(req.UserID, req.ChannelID)
|
||||
if err != nil {
|
||||
return domain.ReadChannelHistoryResult{}, err
|
||||
}
|
||||
|
|
@ -260,6 +260,15 @@ func (s *ChannelStore) ReadChannelHistory(_ context.Context, req domain.ReadChan
|
|||
if maxID <= 0 || maxID > channel.TopMessageID {
|
||||
maxID = channel.TopMessageID
|
||||
}
|
||||
if readOnly {
|
||||
return domain.ReadChannelHistoryResult{
|
||||
ChannelID: req.ChannelID,
|
||||
MaxID: maxID,
|
||||
ReadOnly: true,
|
||||
Pts: channel.Pts,
|
||||
Forum: channel.Forum,
|
||||
}, nil
|
||||
}
|
||||
member := s.members[req.ChannelID][req.UserID]
|
||||
previous := member.ReadInboxMaxID
|
||||
changed := maxID > member.ReadInboxMaxID
|
||||
|
|
|
|||
|
|
@ -85,8 +85,12 @@ func (s *ChannelStore) toggleSuggestedPostApprovalLocked(req domain.ToggleSugges
|
|||
if req.ScheduleDate > 0 {
|
||||
scheduleDate = req.ScheduleDate
|
||||
}
|
||||
if !req.Reject && scheduleDate > 0 && (scheduleDate < req.Date+5*60 || scheduleDate > req.Date+31*24*60*60) {
|
||||
return domain.ToggleSuggestedPostApprovalResult{}, domain.ErrSuggestedPostInvalid
|
||||
if !req.Reject {
|
||||
effectiveDate, scheduleErr := domain.EffectiveSuggestedPostPublishDate(scheduleDate, req.Date)
|
||||
if scheduleErr != nil {
|
||||
return domain.ToggleSuggestedPostApprovalResult{}, scheduleErr
|
||||
}
|
||||
scheduleDate = effectiveDate
|
||||
}
|
||||
recipients := s.monoforumRecipientsLocked(parent.ID, original.SavedPeer.ID)
|
||||
base := domain.ToggleSuggestedPostApprovalResult{
|
||||
|
|
@ -136,13 +140,6 @@ func (s *ChannelStore) toggleSuggestedPostApprovalLocked(req domain.ToggleSugges
|
|||
original.SuggestedPost.Accepted = true
|
||||
original.SuggestedPost.Rejected = false
|
||||
effectivePublishDate := scheduleDate
|
||||
if effectivePublishDate == 0 {
|
||||
// TDesktop deliberately omits schedule_date for "Publish Now", but
|
||||
// renders the approval service action as an absolute date. Persist one
|
||||
// effective publication timestamp across the edited suggestion, action
|
||||
// and approval record instead of leaking an accepted zero date.
|
||||
effectivePublishDate = req.Date
|
||||
}
|
||||
original.SuggestedPost.ScheduleDate = effectivePublishDate
|
||||
original.Pts = s.nextChannelPtsLocked(mono.ID)
|
||||
s.messages[mono.ID][idx] = cloneChannelMessage(original)
|
||||
|
|
|
|||
|
|
@ -179,6 +179,89 @@ func TestSuggestedPostLowBalanceRetryScheduleAndRoleMatrix(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestSuggestedPostApprovalAcceptsDelayedScheduleAndKeepsPTSIdempotent(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, parent, mono, subscriber := newSuggestedPostMemoryFixture(t)
|
||||
|
||||
const now = 1_700_010_000
|
||||
near, err := store.SendMonoforumMessage(ctx, domain.SendMonoforumMessageRequest{
|
||||
MonoforumID: mono.ID, SenderUserID: subscriber.ID, SavedPeer: subscriber,
|
||||
RandomID: 71, Message: "near schedule", SuggestedPost: &domain.SuggestedPost{}, Date: now - 10,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
nearDate := now + 2*60
|
||||
accepted, err := store.ToggleSuggestedPostApproval(ctx, domain.ToggleSuggestedPostApprovalRequest{
|
||||
UserID: 1, MonoforumID: mono.ID, MessageID: near.Message.ID,
|
||||
ScheduleDate: nearDate, Date: now,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("accept schedule below former five-minute gate: %v", err)
|
||||
}
|
||||
if accepted.State != domain.SuggestedPostStateScheduled || accepted.Published != nil ||
|
||||
accepted.OriginalMessage.SuggestedPost.ScheduleDate != nearDate ||
|
||||
accepted.ServiceMessage.Action == nil ||
|
||||
accepted.ServiceMessage.Action.SuggestedPostScheduleDate != nearDate {
|
||||
t.Fatalf("near schedule approval = %+v, want scheduled at %d", accepted, nearDate)
|
||||
}
|
||||
monoPts, parentPts := store.channels[mono.ID].Pts, store.channels[parent.ID].Pts
|
||||
monoEvents, parentEvents := len(store.events[mono.ID]), len(store.events[parent.ID])
|
||||
replay, err := store.ToggleSuggestedPostApproval(ctx, domain.ToggleSuggestedPostApprovalRequest{
|
||||
UserID: 1, MonoforumID: mono.ID, MessageID: near.Message.ID,
|
||||
ScheduleDate: nearDate, Date: now + 10*60,
|
||||
})
|
||||
if err != nil || !replay.Duplicate {
|
||||
t.Fatalf("late duplicate approval = %+v err=%v", replay, err)
|
||||
}
|
||||
if store.channels[mono.ID].Pts != monoPts || store.channels[parent.ID].Pts != parentPts ||
|
||||
len(store.events[mono.ID]) != monoEvents || len(store.events[parent.ID]) != parentEvents {
|
||||
t.Fatal("late duplicate approval advanced PTS or appended an event")
|
||||
}
|
||||
|
||||
due, err := store.SendMonoforumMessage(ctx, domain.SendMonoforumMessageRequest{
|
||||
MonoforumID: mono.ID, SenderUserID: subscriber.ID, SavedPeer: subscriber,
|
||||
RandomID: 72, Message: "already due", SuggestedPost: &domain.SuggestedPost{}, Date: now + 20,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
approvedAt := now + 30
|
||||
dueResult, err := store.ToggleSuggestedPostApproval(ctx, domain.ToggleSuggestedPostApprovalRequest{
|
||||
UserID: 1, MonoforumID: mono.ID, MessageID: due.Message.ID,
|
||||
ScheduleDate: now - 1, Date: approvedAt,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("approve already-due schedule: %v", err)
|
||||
}
|
||||
if dueResult.State != domain.SuggestedPostStateCompleted || dueResult.Published == nil ||
|
||||
dueResult.OriginalMessage.SuggestedPost.ScheduleDate != approvedAt ||
|
||||
dueResult.ServiceMessage.Action == nil ||
|
||||
dueResult.ServiceMessage.Action.SuggestedPostScheduleDate != approvedAt {
|
||||
t.Fatalf("due schedule approval = %+v, want immediate publish at %d", dueResult, approvedAt)
|
||||
}
|
||||
|
||||
far, err := store.SendMonoforumMessage(ctx, domain.SendMonoforumMessageRequest{
|
||||
MonoforumID: mono.ID, SenderUserID: subscriber.ID, SavedPeer: subscriber,
|
||||
RandomID: 73, Message: "too far", SuggestedPost: &domain.SuggestedPost{}, Date: now + 40,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
monoPts, parentPts = store.channels[mono.ID].Pts, store.channels[parent.ID].Pts
|
||||
monoEvents, parentEvents = len(store.events[mono.ID]), len(store.events[parent.ID])
|
||||
if _, err := store.ToggleSuggestedPostApproval(ctx, domain.ToggleSuggestedPostApprovalRequest{
|
||||
UserID: 1, MonoforumID: mono.ID, MessageID: far.Message.ID,
|
||||
ScheduleDate: approvedAt + domain.MaxSuggestedPostScheduleDelay + 1, Date: approvedAt,
|
||||
}); !errors.Is(err, domain.ErrSuggestedPostInvalid) {
|
||||
t.Fatalf("far schedule err=%v, want suggested post invalid", err)
|
||||
}
|
||||
if store.channels[mono.ID].Pts != monoPts || store.channels[parent.ID].Pts != parentPts ||
|
||||
len(store.events[mono.ID]) != monoEvents || len(store.events[parent.ID]) != parentEvents {
|
||||
t.Fatal("far schedule rejection advanced PTS or appended an event")
|
||||
}
|
||||
}
|
||||
|
||||
func TestChannelAuthoredSuggestedPostAcceptedBySubscriber(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, _, mono, subscriber := newSuggestedPostMemoryFixture(t)
|
||||
|
|
|
|||
|
|
@ -756,7 +756,7 @@ WHERE (
|
|||
}
|
||||
|
||||
func (s *ChannelStore) readChannelHistoryOnce(ctx context.Context, req domain.ReadChannelHistoryRequest) (domain.ReadChannelHistoryResult, error) {
|
||||
channel, _, err := s.getChannelForMember(ctx, s.db, req.UserID, req.ChannelID)
|
||||
channel, _, readOnly, err := s.getChannelForViewer(ctx, s.db, req.UserID, req.ChannelID)
|
||||
if err != nil {
|
||||
return domain.ReadChannelHistoryResult{}, err
|
||||
}
|
||||
|
|
@ -764,6 +764,15 @@ func (s *ChannelStore) readChannelHistoryOnce(ctx context.Context, req domain.Re
|
|||
if maxID <= 0 || maxID > channel.TopMessageID {
|
||||
maxID = channel.TopMessageID
|
||||
}
|
||||
if readOnly {
|
||||
return domain.ReadChannelHistoryResult{
|
||||
ChannelID: req.ChannelID,
|
||||
MaxID: maxID,
|
||||
ReadOnly: true,
|
||||
Pts: channel.Pts,
|
||||
Forum: channel.Forum,
|
||||
}, nil
|
||||
}
|
||||
previous, unreadMark, err := s.channelReadHistoryState(ctx, req.ChannelID, req.UserID)
|
||||
if err != nil {
|
||||
return domain.ReadChannelHistoryResult{}, fmt.Errorf("read channel member state: %w", err)
|
||||
|
|
|
|||
|
|
@ -71,6 +71,28 @@ func TestChannelStoreEnablingDirectMessagesCreatesMonoforum(t *testing.T) {
|
|||
if mfTop == 0 || mfPts == 0 {
|
||||
t.Fatalf("monoforum top/pts = %d/%d, want paid-messages service top", mfTop, mfPts)
|
||||
}
|
||||
read, err := channels.ReadChannelHistory(ctx, domain.ReadChannelHistoryRequest{
|
||||
UserID: owner.ID, ChannelID: monoID, MaxID: mfTop, Date: 1700000901,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("read synthetic monoforum history: %v", err)
|
||||
}
|
||||
if !read.ReadOnly || read.Changed || read.MaxID != mfTop {
|
||||
t.Fatalf("synthetic monoforum read = %+v, want read-only no-op at %d", read, mfTop)
|
||||
}
|
||||
var memberExists, dialogExists bool
|
||||
if err := pool.QueryRow(ctx, `
|
||||
SELECT EXISTS (
|
||||
SELECT 1 FROM channel_members WHERE channel_id=$1 AND user_id=$2
|
||||
),
|
||||
EXISTS (
|
||||
SELECT 1 FROM channel_dialogs WHERE channel_id=$1 AND user_id=$2
|
||||
)`, monoID, owner.ID).Scan(&memberExists, &dialogExists); err != nil {
|
||||
t.Fatalf("check synthetic monoforum read footprint: %v", err)
|
||||
}
|
||||
if memberExists || dialogExists {
|
||||
t.Fatalf("synthetic monoforum read persisted member/dialog = %v/%v", memberExists, dialogExists)
|
||||
}
|
||||
// 同批下发母广播频道(TDesktop 据此 resolve linked_monoforum_id 并派生 MonoforumAdmin
|
||||
// 渲染 Direct-Messages 容器):GetChannelDialogs([mono]) 的 chats[] 必须同时带 mono 与母频道。
|
||||
coDelivery, err := channels.GetChannelDialogs(ctx, owner.ID, []int64{monoID})
|
||||
|
|
|
|||
|
|
@ -75,6 +75,15 @@ func TestPublicChannelAndMegagroupPreviewPostgres(t *testing.T) {
|
|||
if !found || history.Self.Status != domain.ChannelMemberLeft {
|
||||
t.Fatalf("preview history = %+v self=%+v", history.Messages, history.Self)
|
||||
}
|
||||
read, err := channels.ReadChannelHistory(ctx, domain.ReadChannelHistoryRequest{
|
||||
UserID: viewer.ID, ChannelID: public.ID, MaxID: sent.Message.ID, Date: 1700009411 + i,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("read public preview history: %v", err)
|
||||
}
|
||||
if !read.ReadOnly || read.Changed || read.MaxID != sent.Message.ID {
|
||||
t.Fatalf("public preview read = %+v, want read-only no-op at %d", read, sent.Message.ID)
|
||||
}
|
||||
audience, err := channels.FilterChannelMessageAudienceIDs(ctx, public.ID, []int64{viewer.ID, owner.ID, viewer.ID})
|
||||
if err != nil {
|
||||
t.Fatalf("filter public message audience: %v", err)
|
||||
|
|
@ -111,14 +120,16 @@ func TestPublicChannelAndMegagroupPreviewPostgres(t *testing.T) {
|
|||
previewDialog.Pts != sent.Event.Pts {
|
||||
t.Fatalf("public preview bootstrap dialog = %+v", previewDialog)
|
||||
}
|
||||
var memberExists bool
|
||||
var memberExists, dialogExists bool
|
||||
if err := pool.QueryRow(ctx, `SELECT EXISTS (
|
||||
SELECT 1 FROM channel_members WHERE channel_id = $1 AND user_id = $2
|
||||
)`, public.ID, viewer.ID).Scan(&memberExists); err != nil {
|
||||
), EXISTS (
|
||||
SELECT 1 FROM channel_dialogs WHERE channel_id = $1 AND user_id = $2
|
||||
)`, public.ID, viewer.ID).Scan(&memberExists, &dialogExists); err != nil {
|
||||
t.Fatalf("check preview member row: %v", err)
|
||||
}
|
||||
if memberExists {
|
||||
t.Fatal("public preview persisted a channel member row")
|
||||
if memberExists || dialogExists {
|
||||
t.Fatalf("public preview persisted member/dialog = %v/%v", memberExists, dialogExists)
|
||||
}
|
||||
if _, err := channels.JoinChannel(ctx, public.ID, viewer.ID, 1700009420+i); err != nil {
|
||||
t.Fatalf("join public peer: %v", err)
|
||||
|
|
|
|||
|
|
@ -118,8 +118,12 @@ func (s *ChannelStore) ToggleSuggestedPostApproval(ctx context.Context, req doma
|
|||
if req.ScheduleDate > 0 {
|
||||
scheduleDate = req.ScheduleDate
|
||||
}
|
||||
if !req.Reject && scheduleDate > 0 && (scheduleDate < req.Date+5*60 || scheduleDate > req.Date+31*24*60*60) {
|
||||
return domain.ToggleSuggestedPostApprovalResult{}, domain.ErrSuggestedPostInvalid
|
||||
if !req.Reject {
|
||||
effectiveDate, scheduleErr := domain.EffectiveSuggestedPostPublishDate(scheduleDate, req.Date)
|
||||
if scheduleErr != nil {
|
||||
return domain.ToggleSuggestedPostApprovalResult{}, scheduleErr
|
||||
}
|
||||
scheduleDate = effectiveDate
|
||||
}
|
||||
recipients, err := monoforumManagerRecipientsTx(ctx, tx, parent.ID, original.SavedPeer.ID)
|
||||
if err != nil {
|
||||
|
|
@ -173,11 +177,6 @@ func (s *ChannelStore) ToggleSuggestedPostApproval(ctx context.Context, req doma
|
|||
}
|
||||
} else {
|
||||
effectivePublishDate := scheduleDate
|
||||
if effectivePublishDate == 0 {
|
||||
// TDesktop's "Publish Now" request has no schedule_date flag, but
|
||||
// its approval service renderer always expects an absolute date.
|
||||
effectivePublishDate = req.Date
|
||||
}
|
||||
original.SuggestedPost.Accepted, original.SuggestedPost.Rejected, original.SuggestedPost.ScheduleDate = true, false, effectivePublishDate
|
||||
original, result.OriginalEvent, err = s.persistSuggestedPostEditTx(ctx, tx, original, req.UserID, req.Date)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -133,3 +133,122 @@ func TestSuggestedPostLifecyclePostgres(t *testing.T) {
|
|||
t.Fatalf("late settlement balance/channel=%d/%d, want 90/8", debit, channelBalance)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSuggestedPostApprovalAcceptsDelayedSchedulePostgres(t *testing.T) {
|
||||
pool := testPool(t)
|
||||
ctx := context.Background()
|
||||
suffix := randomSuffix(t)
|
||||
users := NewUserStore(pool)
|
||||
owner, err := users.Create(ctx, domain.User{AccessHash: 211, Phone: "+1889" + suffix + "01", FirstName: "DelayedOwner"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
subscriber, err := users.Create(ctx, domain.User{AccessHash: 212, Phone: "+1889" + suffix + "02", FirstName: "DelayedSubscriber"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
channels := NewChannelStore(pool)
|
||||
created, err := channels.CreateChannel(ctx, domain.CreateChannelRequest{
|
||||
CreatorUserID: owner.ID, Title: "Delayed Suggested " + suffix, Broadcast: true, Date: 1_700_020_000,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
enabled, err := channels.SetPaidMessagesPrice(ctx, owner.ID, created.Channel.ID, 0, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
monoID := enabled.Channel.LinkedMonoforumID
|
||||
t.Cleanup(func() {
|
||||
_, _ = pool.Exec(ctx, `DELETE FROM suggested_post_approvals WHERE monoforum_id=$1`, monoID)
|
||||
_, _ = pool.Exec(ctx, `DELETE FROM channels WHERE id=ANY($1::bigint[])`, []int64{monoID, created.Channel.ID})
|
||||
_, _ = pool.Exec(ctx, `DELETE FROM users WHERE id=ANY($1::bigint[])`, []int64{owner.ID, subscriber.ID})
|
||||
})
|
||||
saved := domain.Peer{Type: domain.PeerTypeUser, ID: subscriber.ID}
|
||||
|
||||
const now = 1_700_020_100
|
||||
near, err := channels.SendMonoforumMessage(ctx, domain.SendMonoforumMessageRequest{
|
||||
MonoforumID: monoID, SenderUserID: subscriber.ID, SavedPeer: saved,
|
||||
RandomID: 81, Message: "postgres near schedule", SuggestedPost: &domain.SuggestedPost{}, Date: now - 10,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
nearDate := now + 2*60
|
||||
accepted, err := channels.ToggleSuggestedPostApproval(ctx, domain.ToggleSuggestedPostApprovalRequest{
|
||||
UserID: owner.ID, MonoforumID: monoID, MessageID: near.Message.ID,
|
||||
ScheduleDate: nearDate, Date: now,
|
||||
})
|
||||
if err != nil || accepted.State != domain.SuggestedPostStateScheduled || accepted.Published != nil {
|
||||
t.Fatalf("near schedule approval=%+v err=%v", accepted, err)
|
||||
}
|
||||
var monoPts, parentPts, monoEvents, parentEvents int
|
||||
if err := pool.QueryRow(ctx, `SELECT pts FROM channels WHERE id=$1`, monoID).Scan(&monoPts); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := pool.QueryRow(ctx, `SELECT pts FROM channels WHERE id=$1`, created.Channel.ID).Scan(&parentPts); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := pool.QueryRow(ctx, `SELECT count(*)::int FROM channel_update_events WHERE channel_id=$1`, monoID).Scan(&monoEvents); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := pool.QueryRow(ctx, `SELECT count(*)::int FROM channel_update_events WHERE channel_id=$1`, created.Channel.ID).Scan(&parentEvents); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
replay, err := channels.ToggleSuggestedPostApproval(ctx, domain.ToggleSuggestedPostApprovalRequest{
|
||||
UserID: owner.ID, MonoforumID: monoID, MessageID: near.Message.ID,
|
||||
ScheduleDate: nearDate, Date: now + 10*60,
|
||||
})
|
||||
if err != nil || !replay.Duplicate {
|
||||
t.Fatalf("late replay=%+v err=%v", replay, err)
|
||||
}
|
||||
var gotMonoPts, gotParentPts, gotMonoEvents, gotParentEvents int
|
||||
if err := pool.QueryRow(ctx, `SELECT pts FROM channels WHERE id=$1`, monoID).Scan(&gotMonoPts); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := pool.QueryRow(ctx, `SELECT pts FROM channels WHERE id=$1`, created.Channel.ID).Scan(&gotParentPts); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := pool.QueryRow(ctx, `SELECT count(*)::int FROM channel_update_events WHERE channel_id=$1`, monoID).Scan(&gotMonoEvents); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := pool.QueryRow(ctx, `SELECT count(*)::int FROM channel_update_events WHERE channel_id=$1`, created.Channel.ID).Scan(&gotParentEvents); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if gotMonoPts != monoPts || gotParentPts != parentPts || gotMonoEvents != monoEvents || gotParentEvents != parentEvents {
|
||||
t.Fatalf("late replay changed pts/events mono=%d/%d→%d/%d parent=%d/%d→%d/%d",
|
||||
monoPts, monoEvents, gotMonoPts, gotMonoEvents, parentPts, parentEvents, gotParentPts, gotParentEvents)
|
||||
}
|
||||
|
||||
due, err := channels.SendMonoforumMessage(ctx, domain.SendMonoforumMessageRequest{
|
||||
MonoforumID: monoID, SenderUserID: subscriber.ID, SavedPeer: saved,
|
||||
RandomID: 82, Message: "postgres due schedule", SuggestedPost: &domain.SuggestedPost{}, Date: now + 20,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
approvedAt := now + 30
|
||||
dueResult, err := channels.ToggleSuggestedPostApproval(ctx, domain.ToggleSuggestedPostApprovalRequest{
|
||||
UserID: owner.ID, MonoforumID: monoID, MessageID: due.Message.ID,
|
||||
ScheduleDate: now - 1, Date: approvedAt,
|
||||
})
|
||||
if err != nil || dueResult.State != domain.SuggestedPostStateCompleted || dueResult.Published == nil {
|
||||
t.Fatalf("due approval=%+v err=%v", dueResult, err)
|
||||
}
|
||||
if dueResult.OriginalMessage.SuggestedPost.ScheduleDate != approvedAt ||
|
||||
dueResult.ServiceMessage.Action == nil ||
|
||||
dueResult.ServiceMessage.Action.SuggestedPostScheduleDate != approvedAt {
|
||||
t.Fatalf("due effective dates original/action=%d/%+v, want %d",
|
||||
dueResult.OriginalMessage.SuggestedPost.ScheduleDate, dueResult.ServiceMessage.Action, approvedAt)
|
||||
}
|
||||
var persistedDate int
|
||||
if err := pool.QueryRow(ctx, `
|
||||
SELECT schedule_date
|
||||
FROM suggested_post_approvals
|
||||
WHERE monoforum_id=$1 AND suggestion_message_id=$2`, monoID, due.Message.ID).Scan(&persistedDate); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if persistedDate != approvedAt {
|
||||
t.Fatalf("persisted due schedule=%d, want %d", persistedDate, approvedAt)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue