// 删除消息 // msgtype -> system(系统消息)/inbox(outbox)(短消息) func DeleteMessage(id, msgtype string) bool { var err error if msgtype == "system" { err = model.NewSystemMessage().Where("id=" + id).Delete() } else if msgtype == "inbox" { // 打标记 err = model.NewMessage().Set("tdel=" + model.TdelHasDel).Where("id=" + id).Update() } else { err = model.NewMessage().Set("fdel=" + model.FdelHasDel).Where("id=" + id).Update() } if err != nil { logger.Errorln("message service DeleteMessage Error:", err) return false } return true }
// 获得发给某人的短消息(收件箱) func FindToMsgsByUid(uid string) []map[string]interface{} { messages, err := model.NewMessage().Where("to=" + uid + " AND tdel=" + model.TdelNotDel).Order("ctime DESC").FindAll() if err != nil { logger.Errorln("message service FindToMsgsByUid Error:", err) return nil } uids := make(map[int]int) ids := make([]int, 0, len(messages)) for _, message := range messages { uids[message.From] = message.From if message.Hasread == model.NotRead { ids = append(ids, message.Id) } } // 标记已读 go MarkHasRead(ids, false, util.MustInt(uid)) userMap := getUserInfos(uids) result := make([]map[string]interface{}, len(messages)) for i, message := range messages { tmpMap := make(map[string]interface{}) util.Struct2Map(tmpMap, message) tmpMap["user"] = userMap[message.From] // 为了跟系统消息一致 tmpMap["title"] = "发来了一条消息:" result[i] = tmpMap } return result }
// 获得某个用户未读消息数(系统消息和短消息) func FindNotReadMsgNum(uid int) int { condition := "to=" + strconv.Itoa(uid) + " AND hasread=" + model.NotRead sysMsgNum, err := model.NewSystemMessage().Where(condition).Count() if err != nil { logger.Errorln("SystemMessage service FindNotReadMsgNum Error:", err) } msgNum, err := model.NewMessage().Where(condition).Count() if err != nil { logger.Errorln("Message service FindNotReadMsgNum Error:", err) } return sysMsgNum + msgNum }
// from给to发短信息 func SendMessageTo(from, to int, content string) bool { message := model.NewMessage() message.From = from message.To = to message.Content = content if _, err := message.Insert(); err != nil { logger.Errorln("message service SendMessageTo Error:", err) return false } return true }
// from给to发短信息 func SendMessageTo(from, to int, content string) bool { message := model.NewMessage() message.From = from message.To = to message.Content = content if _, err := message.Insert(); err != nil { logger.Errorln("message service SendMessageTo Error:", err) return false } // 通过 WebSocket 通知对方 msg := NewMessage(WsMsgNotify, 1) go Book.PostMessage(to, msg) return true }
// 获取某人发送的消息 func FindFromMsgsByUid(uid string) []map[string]interface{} { messages, err := model.NewMessage().Where("from=" + uid + " AND fdel=" + model.FdelNotDel).Order("ctime DESC").FindAll() if err != nil { logger.Errorln("message service FindFromMsgsByUid Error:", err) return nil } uids := make(map[int]int) for _, message := range messages { uids[message.To] = message.To } userMap := getUserInfos(uids) result := make([]map[string]interface{}, len(messages)) for i, message := range messages { tmpMap := make(map[string]interface{}) util.Struct2Map(tmpMap, message) tmpMap["user"] = userMap[message.To] result[i] = tmpMap } return result }
// 标记消息已读 func MarkHasRead(ids []int, isSysMsg bool, uid int) bool { if len(ids) == 0 { return true } condition := "id=" + strconv.Itoa(ids[0]) if len(ids) > 1 { condition = "id in(" + util.Join(ids, ",") + ")" } var err error if isSysMsg { err = model.NewSystemMessage().Set("hasread=" + model.HasRead).Where(condition).Update() } else { err = model.NewMessage().Set("hasread=" + model.HasRead).Where(condition).Update() } if err != nil { logger.Errorln("message service MarkHasRead Error:", err) return false } return true }