@@ -14,8 +14,7 @@ import (
1414 "github.com/hmchangw/chat/pkg/natsutil"
1515)
1616
17- // sourceSubscription is the subset of a rocketchat_subscriptions doc the mapper consumes,
18- // decoded from the connector's relaxed extended JSON via bson.UnmarshalExtJSON (handles $date).
17+ // sourceSubscription is the subset of a rocketchat_subscriptions doc the mapper decodes (handles $date).
1918type sourceSubscription struct {
2019 ID string `bson:"_id"`
2120 U struct {
@@ -51,8 +50,7 @@ func (s *sourceSubscription) lastSeenMillis() int64 {
5150 return ls
5251}
5352
54- // subUpdateDescription is the connector's update delta for a subscription change event. Only the
55- // changed field keys matter for event selection, so values are decoded as opaque any.
53+ // subUpdateDescription is the connector's update delta; only changed field keys matter, values are opaque.
5654type subUpdateDescription struct {
5755 UpdatedFields map [string ]any `bson:"updatedFields" json:"updatedFields"`
5856 RemovedFields []string `bson:"removedFields" json:"removedFields"`
@@ -71,18 +69,14 @@ func subChanged(desc subUpdateDescription, field string) bool {
7169 return false
7270}
7371
74- // handleSubscription maps a rocketchat_subscriptions change event to inbox InboxEvents per
75- // spec §4.3 / §4.0. insert/replace emit member_added plus the state events that reproduce the
76- // source row; update emits the matching event(s) (open toggle drives membership). delete is
77- // un-actionable (only the source _id) → skip + metric.
72+ // handleSubscription maps a rocketchat_subscriptions change event to inbox InboxEvents (§4.3 / §4.0):
73+ // insert/replace reproduce the source row, update emits the matching event(s), delete maps by _id.
7874//
7975//nolint:gocritic // ev passed by value to mirror handle's signature; off the hot path.
8076func (h * handler ) handleSubscription (ctx context.Context , ev oplogEvent ) error {
8177 if ev .Op == "delete" {
82- // A genuine leave/remove hard-deletes the source row (distinct from the open:false hide
83- // handled as a member_removed update below). The delete event carries only the source _id,
84- // but the destination adopted that _id at member_added time, so emit a subscription_deleted
85- // keyed by it and let inbox-worker delete the row by _id.
78+ // A genuine leave hard-deletes the source row (vs the open:false hide → member_removed below).
79+ // The destination adopted this _id at member_added time, so delete-by-_id maps back to it.
8680 id , idErr := documentKeyID (ev .DocumentKey )
8781 if idErr != nil {
8882 return idErr
@@ -195,9 +189,8 @@ func (h *handler) publishSubscriptionState(ctx context.Context, ss *sourceSubscr
195189 return h .pub .Publish (ctx , h .readEvent (ss , siteID ))
196190}
197191
198- // memberAddedEvent builds the member_added InboxEvent. RoomType is classified from the
199- // subscription's t alone — a subscription can't distinguish discussion/botDM (no prid/teamId/bot
200- // visibility), so those degrade to channel/dm; roles are overridden by the role_updated below.
192+ // memberAddedEvent builds the member_added InboxEvent. RoomType is classified from t alone
193+ // (a sub can't see prid/teamId/bot, so discussion/botDM degrade to channel/dm); roles via role_updated.
201194func (h * handler ) memberAddedEvent (ss * sourceSubscription , siteID string ) model.InboxEvent {
202195 class := classifyRoom (ss .T , false , false , false , 2 )
203196 payload := mustMarshal (model.MemberAddEvent {
@@ -215,8 +208,7 @@ func (h *handler) memberAddedEvent(ss *sourceSubscription, siteID string) model.
215208}
216209
217210// subscriptionDeletedEvent builds the subscription_deleted InboxEvent for a source hard-delete,
218- // carrying the source subscription _id (adopted as the destination _id at member_added time).
219- // Routed to the local inbox-worker like every other migrated row.
211+ // carrying the source _id (adopted as the destination _id at member_added time).
220212func (h * handler ) subscriptionDeletedEvent (subID string ) model.InboxEvent {
221213 payload := mustMarshal (model.SubscriptionDeletedEvent {
222214 SubID : subID ,
@@ -302,13 +294,12 @@ func mapSubscriptionRoles(roles []string) []model.Role {
302294 return out
303295}
304296
305- // mustMarshal JSON-encodes an inner InboxEvent payload. The argument is always a fixed-shape
306- // model struct of plain scalars/slices, so json.Marshal cannot fail; a marshal error would be a
307- // programmer error, not a runtime condition, hence the panic.
297+ // mustMarshal JSON-encodes a fixed-shape model payload; json.Marshal cannot fail on these,
298+ // so an error is a programmer error and panics.
308299func mustMarshal (v any ) []byte {
309300 b , err := json .Marshal (v )
310301 if err != nil {
311- panic (fmt .Sprintf ("marshal subscription payload: %v" , err ))
302+ panic (fmt .Sprintf ("marshal inbox payload: %v" , err ))
312303 }
313304 return b
314305}
0 commit comments