Push2User Suitable for two types of conversations, one is SingleChatType and the other is NotificationChatType.
(ctx context.Context, userIDs []string, msg *sdkws.MsgData)
| 151 | |
| 152 | // Push2User Suitable for two types of conversations, one is SingleChatType and the other is NotificationChatType. |
| 153 | func (c *ConsumerHandler) Push2User(ctx context.Context, userIDs []string, msg *sdkws.MsgData) (err error) { |
| 154 | log.ZInfo(ctx, "Get msg from msg_transfer And push msg", "userIDs", userIDs, "msg", msg.String()) |
| 155 | defer func(duration time.Time) { |
| 156 | t := time.Since(duration) |
| 157 | log.ZInfo(ctx, "Get msg from msg_transfer And push msg end", "msg", msg.String(), "time cost", t) |
| 158 | }(time.Now()) |
| 159 | if err := c.webhookBeforeOnlinePush(ctx, &c.config.WebhooksConfig.BeforeOnlinePush, userIDs, msg); err != nil { |
| 160 | return err |
| 161 | } |
| 162 | |
| 163 | wsResults, err := c.GetConnsAndOnlinePush(ctx, msg, userIDs) |
| 164 | if err != nil { |
| 165 | return err |
| 166 | } |
| 167 | |
| 168 | log.ZDebug(ctx, "single and notification push result", "result", wsResults, "msg", msg, "push_to_userID", userIDs) |
| 169 | log.ZInfo(ctx, "single and notification push end") |
| 170 | |
| 171 | if !c.shouldPushOffline(ctx, msg) { |
| 172 | return nil |
| 173 | } |
| 174 | log.ZInfo(ctx, "pushOffline start") |
| 175 | |
| 176 | for _, v := range wsResults { |
| 177 | //message sender do not need offline push |
| 178 | if msg.SendID == v.UserID { |
| 179 | continue |
| 180 | } |
| 181 | //receiver online push success |
| 182 | if v.OnlinePush { |
| 183 | return nil |
| 184 | } |
| 185 | } |
| 186 | needOfflinePushUserID := []string{msg.RecvID} |
| 187 | var offlinePushUserID []string |
| 188 | |
| 189 | //receiver offline push |
| 190 | if err = c.webhookBeforeOfflinePush(ctx, &c.config.WebhooksConfig.BeforeOfflinePush, needOfflinePushUserID, msg, &offlinePushUserID); err != nil { |
| 191 | return err |
| 192 | } |
| 193 | |
| 194 | if len(offlinePushUserID) > 0 { |
| 195 | needOfflinePushUserID = offlinePushUserID |
| 196 | } |
| 197 | err = c.offlinePushMsg(ctx, msg, needOfflinePushUserID) |
| 198 | if err != nil { |
| 199 | log.ZDebug(ctx, "offlinePushMsg failed", err, "needOfflinePushUserID", needOfflinePushUserID, "msg", msg) |
| 200 | log.ZWarn(ctx, "offlinePushMsg failed", err, "needOfflinePushUserID length", len(needOfflinePushUserID), "msg", msg) |
| 201 | return nil |
| 202 | } |
| 203 | |
| 204 | return nil |
| 205 | } |
| 206 | |
| 207 | func (c *ConsumerHandler) shouldPushOffline(_ context.Context, msg *sdkws.MsgData) bool { |
| 208 | isOfflinePush := datautil.GetSwitchFromOptions(msg.Options, constant.IsOfflinePush) |
no test coverage detected