group.go 1.72 KB
Newer Older
chenweijian's avatar
chenweijian committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
package group_s

import (
	"encoding/json"
	"github.com/sirupsen/logrus"
	"hilo-user/domain"
	"hilo-user/domain/model/group_m"
	"hilo-user/sdk/tencentyun"
	"runtime/debug"
)

// 发送群信令。入参是内部imGroupId,这里做转换
func SendSignalMsg(model *domain.Model, imGroupId, txGroupId string, msg group_m.GroupSystemMsg, async bool) {
	model.Log.WithField("imGroupId:", imGroupId)
	model.Log.WithField("txGroupId:", txGroupId)

	groupId := txGroupId
	var err error
	if len(groupId) == 0 {
		groupId, err = group_m.ToTxGroupId(model, imGroupId)
		if err != nil {
			return
		}
	}

	buffer, err := json.Marshal(msg)
	if err == nil {
		str := string(buffer)
		model.Log.Infof("SendSignalMsg: %s, async = %v", str, async)

		if async {
			go func(logger *logrus.Entry) {
				defer func() {
					if r := recover(); r != nil {
						//打印错误堆栈信息
						logger.Errorf("SendSignalMsg SYSTEM ACTION PANIC: %v, stack: %v", r, string(debug.Stack()))
					}
				}()
				if err = tencentyun.SendSystemMsg(logger, groupId, []string{}, str); err != nil {
					logger.Errorf("SendSignalMsg aync failed for %s, msgId = %d, context:%v, err:%v", groupId, msg.MsgId, str, err)
				} else {
					logger.Infof("SendSignalMsg aync success for %s, msgId = %d, context:%v, err:%v", groupId, msg.MsgId, str)
				}
			}(model.Log)
		} else if err = tencentyun.SendSystemMsg(model.Log, groupId, []string{}, str); err != nil {
			model.Log.Errorf("SendSignalMsg sync failed for %s, msgId = %d, context:%v, err:%v", groupId, msg.MsgId, str, err)
		} else {
			model.Log.Infof("SendSignalMsg sync success for %s, msgId = %d, context:%v", groupId, msg.MsgId, str)
		}
	} else {
		model.Log.Errorln("Marshall failure, msgId = %d : %s", msg.MsgId, err.Error())
	}
}