Przeglądaj źródła

Respect fixed subscription notices during recipient selection

DESKTOP-HN5QP3V\Administrator 3 tygodni temu
rodzic
commit
e123bcf24a

+ 1 - 1
app/service/subscribe.go

@@ -126,7 +126,7 @@ func (this *SubscribeSendService) Send() error {
 	var list = make([]*subscribe.Subscribe, 0)
 	// 更具deviceId 和 module 获取 需要推送的用户openid 列表
 	// data 应该是个json对象
-	filter := bson.M{"settings": row.ID, "open_id": bson.M{"$exists": true, "$ne": ""}}
+	filter := sender.SubscribePushRecipientFilter(row)
 
 	if len(this.Data) > 0 {
 		filter["device_id"] = bson.M{"$in": this.Data}

+ 11 - 1
pkg/sender/subscribe.go

@@ -78,7 +78,8 @@ func (this *SubscribeSender) safeSend(send *SubscribeSend) {
 	openIDs := send.OpenIds
 	if send.DeviceId != "" {
 		subscribeOne := new(subscribe.Subscribe)
-		filter := bson.M{"device_id": send.DeviceId, "open_id": bson.M{"$exists": true, "$ne": ""}, "settings": row.ID}
+		filter := SubscribePushRecipientFilter(row)
+		filter["device_id"] = send.DeviceId
 		if err := mgm.Coll(&subscribe.Subscribe{}).First(filter, subscribeOne); err != nil {
 			logger.Info("[订阅推送] 查询订阅记录失败, module=%d, device_id=%s, err=%v", send.Module, send.DeviceId, err)
 			return
@@ -92,6 +93,15 @@ func (this *SubscribeSender) safeSend(send *SubscribeSend) {
 	}
 }
 
+// SubscribePushRecipientFilter applies the same authorization rule to immediate and queued sends.
+func SubscribePushRecipientFilter(row SubscribePushRow) bson.M {
+	filter := bson.M{"open_id": bson.M{"$exists": true, "$ne": ""}}
+	if SubscribePushRequiresSetting(row) {
+		filter["settings"] = row.ID
+	}
+	return filter
+}
+
 func (this *SubscribeSender) sendWechatSubscribe(openId string, module int) {
 	configured, ok := subscribePushMessage(module)
 	if !ok {

+ 11 - 4
pkg/sender/subscribe_push_config.go

@@ -8,10 +8,12 @@ import (
 
 // SubscribePushRow is the GM SubscribePush table entry needed by the sender.
 type SubscribePushRow struct {
-	ID     int    `json:"id"`
-	Title1 string `json:"title1"`
-	Desc   string `json:"desc"`
-	IsOpen int    `json:"is_open"`
+	ID            int    `json:"id"`
+	Title1        string `json:"title1"`
+	Desc          string `json:"desc"`
+	IsOpen        int    `json:"is_open"`
+	IsSet         int    `json:"is_set"`
+	SetInitialize int    `json:"set_initialize"`
 }
 
 type subscribeMessage struct {
@@ -53,6 +55,11 @@ func SubscribePushActive(id int) bool {
 	return ok && row.IsOpen != 0 && strings.TrimSpace(row.Title1) != "" && strings.TrimSpace(row.Desc) != ""
 }
 
+// SubscribePushRequiresSetting reports whether the player must have this ID in settings.
+func SubscribePushRequiresSetting(row SubscribePushRow) bool {
+	return !(row.IsOpen == 1 && row.IsSet == 0 && row.SetInitialize == 1)
+}
+
 func SubscribePushConfig(id int) (SubscribePushRow, bool) {
 	value := subscribePushRows.Load()
 	if value == nil {

+ 31 - 1
pkg/sender/subscribe_push_config_test.go

@@ -62,7 +62,7 @@ func TestLoadSubscribePushFromGM(t *testing.T) {
 			return
 		}
 		_ = json.NewEncoder(w).Encode(map[string]any{
-			"101": map[string]any{"id": 101, "title1": "配置标题", "desc": "配置文案", "is_open": 1, "is_set": 0},
+			"101": map[string]any{"id": 101, "title1": "配置标题", "desc": "配置文案", "is_open": 1, "is_set": 0, "set_initialize": 1},
 			"102": map[string]any{"id": 102, "is_open": 0},
 			"103": map[string]any{"id": 103, "is_open": 0},
 			"104": map[string]any{"id": 104, "is_open": 0},
@@ -79,4 +79,34 @@ func TestLoadSubscribePushFromGM(t *testing.T) {
 	if !ok || got.Title != "配置标题" || got.Description != "配置文案" {
 		t.Fatalf("loaded message = %+v, enabled = %v", got, ok)
 	}
+	row, ok := SubscribePushConfig(101)
+	if !ok || row.IsSet != 0 || row.SetInitialize != 1 || SubscribePushRequiresSetting(row) {
+		t.Fatalf("GM authorization fields not loaded: %+v", row)
+	}
+}
+
+func TestSubscribePushRecipientFilter(t *testing.T) {
+	cases := []struct {
+		name        string
+		row         SubscribePushRow
+		wantSetting bool
+	}{
+		{"fixed-on", SubscribePushRow{ID: 109, IsOpen: 1, IsSet: 0, SetInitialize: 1}, false},
+		{"not-initialized", SubscribePushRow{ID: 108, IsOpen: 1, IsSet: 0, SetInitialize: 0}, true},
+		{"player-setting", SubscribePushRow{ID: 101, IsOpen: 1, IsSet: 1, SetInitialize: 1}, true},
+		{"disabled", SubscribePushRow{ID: 110, IsOpen: 0, IsSet: 0, SetInitialize: 1}, true},
+		{"other-nonzero-open", SubscribePushRow{ID: 111, IsOpen: 2, IsSet: 0, SetInitialize: 1}, true},
+	}
+	for _, tc := range cases {
+		t.Run(tc.name, func(t *testing.T) {
+			filter := SubscribePushRecipientFilter(tc.row)
+			setting, hasSetting := filter["settings"]
+			if hasSetting != tc.wantSetting || (hasSetting && setting != tc.row.ID) {
+				t.Fatalf("unexpected settings filter: %v", filter)
+			}
+			if filter["open_id"] == nil {
+				t.Fatalf("missing open_id condition: %v", filter)
+			}
+		})
+	}
 }