MCPcopy
hub / github.com/openimsdk/open-im-server / Push2User

Method Push2User

internal/push/push_handler.go:153–205  ·  view source on GitHub ↗

Push2User Suitable for two types of conversations, one is SingleChatType and the other is NotificationChatType.

(ctx context.Context, userIDs []string, msg *sdkws.MsgData)

Source from the content-addressed store, hash-verified

151
152// Push2User Suitable for two types of conversations, one is SingleChatType and the other is NotificationChatType.
153func (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
207func (c *ConsumerHandler) shouldPushOffline(_ context.Context, msg *sdkws.MsgData) bool {
208 isOfflinePush := datautil.GetSwitchFromOptions(msg.Options, constant.IsOfflinePush)

Callers 1

handleMs2PsChatMethod · 0.95

Calls 6

GetConnsAndOnlinePushMethod · 0.95
shouldPushOfflineMethod · 0.95
offlinePushMsgMethod · 0.95
StringMethod · 0.45

Tested by

no test coverage detected