1
This commit is contained in:
parent
fb27e0e9f9
commit
7579af6485
@ -2,10 +2,7 @@ package app
|
||||
|
||||
import (
|
||||
"f5"
|
||||
"main/constant"
|
||||
"mt"
|
||||
"main/middleware"
|
||||
. "main/global"
|
||||
)
|
||||
|
||||
type app struct {
|
||||
@ -31,7 +28,6 @@ func (this *app) Init() {
|
||||
f5.LoadMetaTable(mt.Table)
|
||||
this.registerDataSources()
|
||||
this.initCb()
|
||||
f5.GetApp().GetGinEngine().Use(middleware.CaForward, middleware.CaAuth)
|
||||
}
|
||||
|
||||
func (this *app) UnInit() {
|
||||
@ -42,24 +38,8 @@ func (this *app) Update() {
|
||||
}
|
||||
|
||||
func (this *app) registerDataSources() {
|
||||
f5.GetGoStyleDb().RegisterDataSource(
|
||||
constant.MAIL_DB,
|
||||
mt.Table.MailDb.GetById(0).GetHost(),
|
||||
mt.Table.MailDb.GetById(0).GetPort(),
|
||||
mt.Table.MailDb.GetById(0).GetUser(),
|
||||
mt.Table.MailDb.GetById(0).GetPasswd(),
|
||||
mt.Table.MailDb.GetById(0).GetDatabase(),
|
||||
3)
|
||||
f5.GetJsStyleDb().RegisterDataSource(
|
||||
constant.MAIL_DB,
|
||||
mt.Table.MailDb.GetById(0).GetHost(),
|
||||
mt.Table.MailDb.GetById(0).GetPort(),
|
||||
mt.Table.MailDb.GetById(0).GetUser(),
|
||||
mt.Table.MailDb.GetById(0).GetPasswd(),
|
||||
mt.Table.MailDb.GetById(0).GetDatabase(),
|
||||
1)
|
||||
}
|
||||
|
||||
func (this *app) HasTask() bool {
|
||||
return GetMailMgr().HasTask()
|
||||
return false
|
||||
}
|
||||
|
@ -1,29 +1,8 @@
|
||||
package constant
|
||||
|
||||
const (
|
||||
MAIL_DB = "maildb"
|
||||
)
|
||||
|
||||
const (
|
||||
APP_MODULE_IDX = iota
|
||||
PLAYER_MGR_MODULE_IDX
|
||||
MAIL_MGR_MODULE_IDX
|
||||
CONTROLLER_MGR_MODULE_IDX
|
||||
MAX_MODULE_IDX
|
||||
)
|
||||
|
||||
const (
|
||||
MAIL_TYPE_GROUP = 1
|
||||
MAIL_TYPE_ALL = 2
|
||||
)
|
||||
|
||||
const (
|
||||
INBOX_STATE_NONE = 0
|
||||
INBOX_STATE_READ = 1
|
||||
INBOX_STATE_DELETED = 2
|
||||
)
|
||||
|
||||
const (
|
||||
EVENT_MAIL_UPDATE = "mail.update"
|
||||
EVENT_UPSER_GROUP_UPDATE = "user_group.update"
|
||||
)
|
||||
|
@ -9,12 +9,10 @@ import (
|
||||
|
||||
var modules [constant.MAX_MODULE_IDX]q5.Module
|
||||
var initOrders = []int32{
|
||||
constant.PLAYER_MGR_MODULE_IDX,
|
||||
constant.MAIL_MGR_MODULE_IDX,
|
||||
}
|
||||
|
||||
var app common.App
|
||||
var playerMgr common.PlayerMgr
|
||||
var mailMgr common.MailMgr
|
||||
|
||||
func GetApp() common.App {
|
||||
@ -25,9 +23,6 @@ func GetMailMgr() common.MailMgr {
|
||||
return mailMgr
|
||||
}
|
||||
|
||||
func GetPlayerMgr() common.PlayerMgr {
|
||||
return playerMgr
|
||||
}
|
||||
|
||||
func RegModule(idx int32, m q5.Module) {
|
||||
fmt.Printf("RegModule module %d\n", idx)
|
||||
@ -37,10 +32,6 @@ func RegModule(idx int32, m q5.Module) {
|
||||
{
|
||||
app = m.(common.App)
|
||||
}
|
||||
case constant.PLAYER_MGR_MODULE_IDX:
|
||||
{
|
||||
playerMgr = m.(common.PlayerMgr)
|
||||
}
|
||||
case constant.MAIL_MGR_MODULE_IDX:
|
||||
{
|
||||
mailMgr = m.(common.MailMgr)
|
||||
|
@ -3,8 +3,6 @@ package initialize
|
||||
import (
|
||||
_ "main/app"
|
||||
. "main/global"
|
||||
_ "main/mail"
|
||||
_ "main/player"
|
||||
_ "main/controller"
|
||||
)
|
||||
|
||||
|
@ -1,12 +0,0 @@
|
||||
package mail
|
||||
|
||||
import (
|
||||
"main/constant"
|
||||
"main/global"
|
||||
)
|
||||
|
||||
var _mailMgr = new(mailMgr)
|
||||
|
||||
func init() {
|
||||
global.RegModule(constant.MAIL_MGR_MODULE_IDX, _mailMgr)
|
||||
}
|
@ -1,177 +0,0 @@
|
||||
package mail
|
||||
|
||||
import (
|
||||
"q5"
|
||||
"f5"
|
||||
"sync"
|
||||
"main/common"
|
||||
"main/constant"
|
||||
)
|
||||
|
||||
type attachment struct {
|
||||
itemId int32 `json:"itemid"`
|
||||
itemNum int32 `json:"itemnum"`
|
||||
}
|
||||
|
||||
type mail struct {
|
||||
mailId int64
|
||||
mailType int32
|
||||
subject string
|
||||
content string
|
||||
sendTime int32
|
||||
expireTime int32
|
||||
userRegStartTime int32
|
||||
userRegEndTime int32
|
||||
attachments []*attachment
|
||||
recipients *sync.Map //account_id, int
|
||||
userGroups *sync.Map //group_id, *userGroup
|
||||
}
|
||||
|
||||
func (this *mail) init() {
|
||||
this.attachments = []*attachment{}
|
||||
}
|
||||
|
||||
func (this *mail) loadFromDb(ds *f5.DataSet) {
|
||||
this.mailId = q5.ToInt64(ds.GetByName("mail_id"))
|
||||
this.mailType = q5.ToInt32(ds.GetByName("mail_type"))
|
||||
this.subject = ds.GetByName("subject")
|
||||
this.content = ds.GetByName("content")
|
||||
this.sendTime = q5.ToInt32(ds.GetByName("sendtime"))
|
||||
this.expireTime = q5.ToInt32(ds.GetByName("expiretime"))
|
||||
this.userRegStartTime = q5.ToInt32(ds.GetByName("user_reg_start_time"))
|
||||
this.userRegEndTime = q5.ToInt32(ds.GetByName("user_reg_end_time"))
|
||||
{
|
||||
attachmentsStr := ds.GetByName("attachments")
|
||||
if attachmentsStr != "" {
|
||||
if err := q5.DecodeJson(attachmentsStr, &this.attachments); err != nil {
|
||||
panic("mail.loadFromDb parse attachments error " + q5.ToString(this.mailId))
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
recipientsStr := ds.GetByName("recipients")
|
||||
if recipientsStr != "" {
|
||||
recipientsList := []string{}
|
||||
if err := q5.DecodeJson(recipientsStr, recipientsList); err != nil {
|
||||
panic("mail.loadFromDb parse recipients error " + q5.ToString(this.mailId))
|
||||
}
|
||||
/*
|
||||
for _, recipient := range(recipientsList) {
|
||||
//this.recipients[recipient] = 1
|
||||
}*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (this *mail) isType(mailType int32) bool {
|
||||
return this.mailType == mailType
|
||||
}
|
||||
|
||||
func (this *mail) IsValid(hum common.Player) bool {
|
||||
if f5.GetApp().GetRealSeconds() < int64(this.expireTime) &&
|
||||
hum.GetRegisterTime() >= this.userRegStartTime &&
|
||||
hum.GetRegisterTime() <= this.userRegEndTime {
|
||||
if this.mailType == constant.MAIL_TYPE_GROUP {
|
||||
return this.isMailUser(hum.GetAccountId())
|
||||
} else if this.mailType == constant.MAIL_TYPE_ALL {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (this *mail) GetMailId() int64 {
|
||||
return this.mailId
|
||||
}
|
||||
|
||||
func (this *mail) GetExpireTime() int32 {
|
||||
return this.expireTime
|
||||
}
|
||||
|
||||
func (this *mail) fillMailDto(hum common.Player, p *common.MailDto) bool {
|
||||
p.MailId = q5.ToString(this.mailId)
|
||||
p.From = ""
|
||||
p.To = ""
|
||||
p.Subject = this.subject
|
||||
p.Content = this.content
|
||||
//p.Flags
|
||||
if !hum.IsUnread(this) {
|
||||
p.Flags = 1 << 0
|
||||
} else {
|
||||
p.Flags = 0
|
||||
}
|
||||
p.SendTime = this.sendTime
|
||||
p.ExpireTime = this.expireTime
|
||||
p.MailType = this.mailType
|
||||
p.MailSubType = 0
|
||||
p.Ext = ""
|
||||
p.Attachments = make([]*common.AttachmentDto, 0)
|
||||
for _, ele := range(this.attachments) {
|
||||
attachment := new(common.AttachmentDto)
|
||||
attachment.ItemId = ele.itemId
|
||||
attachment.ItemNum = ele.itemNum
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (this* mail) isMailUser(accountId string) bool {
|
||||
{
|
||||
p := this.recipients
|
||||
if p != nil {
|
||||
if _, ok := p.Load(accountId); ok {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
found := false
|
||||
this.traverseUserGroup(
|
||||
func (groupId int64, group *userGroup) bool {
|
||||
if group != nil {
|
||||
if _, ok := group.userHash.Load(accountId); ok {
|
||||
found = true
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
if found {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (this *mail) traverseRecipients(cb func(string) bool) {
|
||||
p := this.recipients
|
||||
if p != nil {
|
||||
p.Range(func (k, v interface{}) bool {
|
||||
return cb(k.(string))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (this *mail) traverseUserGroup(cb func(int64, *userGroup) bool) {
|
||||
p := this.userGroups
|
||||
if p != nil {
|
||||
p.Range(func (k, v interface{}) bool {
|
||||
if v != nil {
|
||||
return cb(k.(int64), v.(*userGroup))
|
||||
} else {
|
||||
g := _mailMgr.getGroup(k.(int64))
|
||||
if g != nil {
|
||||
p.Store(k, g)
|
||||
return cb(k.(int64), g)
|
||||
} else {
|
||||
return cb(k.(int64), nil)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func newMail() *mail {
|
||||
p := new(mail)
|
||||
p.init()
|
||||
return p
|
||||
}
|
@ -1,508 +0,0 @@
|
||||
package mail
|
||||
|
||||
import (
|
||||
"q5"
|
||||
"f5"
|
||||
"main/common"
|
||||
"main/constant"
|
||||
"sync"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type dbEvent struct {
|
||||
idx int64
|
||||
eventName string
|
||||
param1 string
|
||||
entry q5.ListHead
|
||||
}
|
||||
|
||||
type userGroup struct {
|
||||
groupId int64
|
||||
userHash *sync.Map
|
||||
}
|
||||
|
||||
type mailMgr struct {
|
||||
idHash sync.Map //int64 => *mail
|
||||
wholeMails sync.Map //int64 => *mail
|
||||
groupMails sync.Map //int64 => *mail
|
||||
personalMails sync.Map //string => sync.Map<int64, *mail>
|
||||
groupHash sync.Map //int64 => *userGroup
|
||||
lastSyncEventIdx int64
|
||||
pullingEvent bool
|
||||
procingEvent bool
|
||||
eventQueue q5.Queue
|
||||
timerWp *f5.TimerWp
|
||||
}
|
||||
|
||||
func (this *mailMgr) Init() {
|
||||
this.eventQueue.Init()
|
||||
this.syncEvent()
|
||||
this.loadMails()
|
||||
this.loadGroups()
|
||||
this.loadGroupMembers()
|
||||
f5.GetApp().RegisterCaHandle("Mail", "getMailList", this.CaGetMailList)
|
||||
f5.GetApp().RegisterCaHandle("Mail", "markMail", this.CaMarkMail)
|
||||
f5.GetApp().RegisterCaHandle("Mail", "getUnreadMailCnt", this.CaGetUnreadMailCnt)
|
||||
f5.GetApp().RegisterCaHandle("Mail", "getAttachment", this.CaGetAttachment)
|
||||
f5.GetApp().RegisterCaHandle("Mail", "deleteMails", this.CaDeleteMails)
|
||||
this.timerWp = f5.GetTimer().SetIntervalWp(
|
||||
300,
|
||||
func (e int32, args* q5.Args) {
|
||||
if e == q5.TIMER_EXEC_EVENT {
|
||||
this.updateDbEvent()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (this *mailMgr) UnInit() {
|
||||
f5.GetTimer().Delete(this.timerWp)
|
||||
this.timerWp = nil
|
||||
}
|
||||
|
||||
func (this *mailMgr) loadMails() {
|
||||
f5.GetSysLog().Info("mailMgr.loadMails begin")
|
||||
nowTime := f5.GetApp().GetNowSeconds()
|
||||
lastIdx := f5.GetGoStyleDb().SyncBatchLoadFullTable(
|
||||
constant.MAIL_DB,
|
||||
"SELECT * FROM t_mail WHERE idx > %d AND deleted = 0 AND expiretime > " + q5.ToString(nowTime),
|
||||
func (ds *f5.DataSet) {
|
||||
p := newMail()
|
||||
p.loadFromDb(ds)
|
||||
this.addMail(p)
|
||||
},
|
||||
func (err error) {
|
||||
panic(fmt.Sprintf("mailMgr.loadMails dberror:%s", err))
|
||||
})
|
||||
f5.GetSysLog().Info("mailMgr.loadMails end lastIdx:%d mailNum:%d",
|
||||
lastIdx,
|
||||
q5.GetSyncMapSize(this.idHash))
|
||||
}
|
||||
|
||||
func (this *mailMgr) loadGroups() {
|
||||
f5.GetSysLog().Info("mailMgr.loadGroups begin")
|
||||
lastIdx := f5.GetGoStyleDb().SyncBatchLoadFullTable(
|
||||
constant.MAIL_DB,
|
||||
"SELECT * FROM t_group WHERE idx > %d AND deleted = 0",
|
||||
func (ds *f5.DataSet) {
|
||||
groupId := q5.ToInt64(ds.GetByName("group_id"))
|
||||
if this.getGroup(groupId) != nil {
|
||||
panic(fmt.Sprintf("mailMgr.loadGroups group_id error"))
|
||||
}
|
||||
p := newUserGroup()
|
||||
p.groupId = groupId
|
||||
this.addGroup(p)
|
||||
},
|
||||
func (err error) {
|
||||
panic(fmt.Sprintf("mailMgr.loadGroups dberror:%s", err))
|
||||
})
|
||||
f5.GetSysLog().Info("mailMgr.loadGroups end lastIdx:%d mailNum:%d",
|
||||
lastIdx,
|
||||
q5.GetSyncMapSize(this.idHash))
|
||||
}
|
||||
|
||||
func (this *mailMgr) loadGroupMembers() {
|
||||
f5.GetSysLog().Info("mailMgr.loadGroupMembers begin")
|
||||
lastIdx := f5.GetGoStyleDb().SyncBatchLoadFullTable(
|
||||
constant.MAIL_DB,
|
||||
"SELECT * FROM t_member WHERE idx > %d AND deleted = 0",
|
||||
func (ds *f5.DataSet) {
|
||||
groupId := q5.ToInt64(ds.GetByName("group_id"))
|
||||
memberId := ds.GetByName("member_id")
|
||||
p := this.getGroup(groupId)
|
||||
if p != nil {
|
||||
p.userHash.Store(memberId, q5.ToInt64(ds.GetByName("idx")))
|
||||
}
|
||||
},
|
||||
func (err error) {
|
||||
panic(fmt.Sprintf("mailMgr.loadGroupMembers dberror:%s", err))
|
||||
})
|
||||
f5.GetSysLog().Info("mailMgr.loadGroupMembers end lastIdx:%d mailNum:%d",
|
||||
lastIdx,
|
||||
q5.GetSyncMapSize(this.idHash))
|
||||
}
|
||||
|
||||
func (this *mailMgr) CaGetMailList(c *gin.Context) {
|
||||
hum := c.MustGet("hum").(common.Player)
|
||||
rspObj := struct {
|
||||
ErrCode int32 `json:"errcode"`
|
||||
ErrMsg string `json:"errmsg"`
|
||||
MailList []*common.MailDto `json:"mail_list"`
|
||||
}{
|
||||
MailList: []*common.MailDto{},
|
||||
}
|
||||
this.traversePlayerMail(
|
||||
hum,
|
||||
func (m *mail) bool {
|
||||
if m.IsValid(hum) {
|
||||
if hum.IsReadable(m) {
|
||||
mailDto := new(common.MailDto)
|
||||
if m.fillMailDto(hum, mailDto) {
|
||||
q5.AppendSlice(&rspObj.MailList, mailDto)
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
c.JSON(200, rspObj)
|
||||
}
|
||||
|
||||
func (this *mailMgr) CaMarkMail(c *gin.Context) {
|
||||
hum := c.MustGet("hum").(common.Player)
|
||||
mailIds := q5.StrSplit(c.DefaultQuery("mail_ids", ""), ",")
|
||||
mails := []common.Mail{}
|
||||
for _, str := range(mailIds) {
|
||||
m := this.getMail(str)
|
||||
if m != nil && m.IsValid(hum) {
|
||||
q5.AppendSlice(&mails, m)
|
||||
}
|
||||
}
|
||||
hum.MarkMails(mails)
|
||||
rspObj := struct {
|
||||
ErrCode int32 `json:"errcode"`
|
||||
ErrMsg string `json:"errmsg"`
|
||||
}{}
|
||||
c.JSON(200, rspObj)
|
||||
}
|
||||
|
||||
func (this *mailMgr) CaGetUnreadMailCnt(c *gin.Context) {
|
||||
hum := c.MustGet("hum").(common.Player)
|
||||
rspObj := struct {
|
||||
ErrCode int32 `json:"errcode"`
|
||||
ErrMsg string `json:"errmsg"`
|
||||
UnreadMailCnt int32 `json:"unread_mail_cnt"`
|
||||
}{}
|
||||
this.traversePlayerMail(
|
||||
hum,
|
||||
func (m *mail) bool {
|
||||
if m.IsValid(hum) && hum.IsUnread(m) {
|
||||
rspObj.UnreadMailCnt++
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
c.JSON(200, rspObj)
|
||||
}
|
||||
|
||||
func (this *mailMgr) CaGetAttachment(c *gin.Context) {
|
||||
hum := c.MustGet("hum").(common.Player)
|
||||
mailIds := q5.StrSplit(c.DefaultQuery("mail_ids", ""), ",")
|
||||
mails := []common.Mail{}
|
||||
for _, str := range(mailIds) {
|
||||
m := this.getMail(str)
|
||||
if m != nil && m.IsValid(hum) {
|
||||
q5.AppendSlice(&mails, m)
|
||||
}
|
||||
}
|
||||
hum.GetAttachment(mails)
|
||||
rspObj := struct {
|
||||
ErrCode int32 `json:"errcode"`
|
||||
ErrMsg string `json:"errmsg"`
|
||||
}{}
|
||||
c.JSON(200, rspObj)
|
||||
}
|
||||
|
||||
func (this *mailMgr) CaDeleteMails(c *gin.Context) {
|
||||
hum := c.MustGet("hum").(common.Player)
|
||||
mailIds := q5.StrSplit(c.DefaultQuery("mail_ids", ""), ",")
|
||||
mails := []common.Mail{}
|
||||
for _, str := range(mailIds) {
|
||||
m := this.getMail(str)
|
||||
if m != nil && m.IsValid(hum) {
|
||||
q5.AppendSlice(&mails, m)
|
||||
}
|
||||
}
|
||||
hum.DeleteMails(mails)
|
||||
rspObj := struct {
|
||||
ErrCode int32 `json:"errcode"`
|
||||
ErrMsg string `json:"errmsg"`
|
||||
}{}
|
||||
c.JSON(200, rspObj)
|
||||
}
|
||||
|
||||
func (this *mailMgr) traversePlayerMail(hum common.Player, cb func(*mail) bool) {
|
||||
stop := false
|
||||
traversedMails := make(map[int64]*mail, 10)
|
||||
traversFunc := func (k, v interface{}) bool {
|
||||
m := v.(*mail)
|
||||
if m.IsValid(hum) {
|
||||
if _, ok := traversedMails[m.mailId]; ok {
|
||||
return true
|
||||
}
|
||||
if !(cb(m)) {
|
||||
stop = true
|
||||
return false
|
||||
}
|
||||
traversedMails[m.mailId] = m
|
||||
}
|
||||
return true
|
||||
}
|
||||
this.wholeMails.Range(traversFunc)
|
||||
if stop {
|
||||
return
|
||||
}
|
||||
this.groupMails.Range(traversFunc)
|
||||
if stop {
|
||||
return
|
||||
}
|
||||
if p, ok := this.personalMails.Load(hum.GetAccountId()); ok {
|
||||
(p.(*sync.Map)).Range(traversFunc)
|
||||
}
|
||||
}
|
||||
|
||||
func (this *mailMgr) removeMail(m *mail) {
|
||||
this.idHash.Delete(m.mailId)
|
||||
if m.isType(constant.MAIL_TYPE_ALL) {
|
||||
this.wholeMails.Delete(m.mailId)
|
||||
} else if m.isType(constant.MAIL_TYPE_GROUP) {
|
||||
m.traverseUserGroup(
|
||||
func (int64, *userGroup) bool {
|
||||
this.groupMails.Delete(m.mailId)
|
||||
return false
|
||||
})
|
||||
m.traverseRecipients(
|
||||
func (accountId string) bool {
|
||||
if p, ok := this.personalMails.Load(accountId); ok {
|
||||
(p.(*sync.Map)).Delete(m.mailId)
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (this *mailMgr) addMail(m *mail) {
|
||||
this.idHash.Store(m.mailId, m)
|
||||
if m.isType(constant.MAIL_TYPE_ALL) {
|
||||
this.wholeMails.Store(m.mailId, m)
|
||||
} else if m.isType(constant.MAIL_TYPE_GROUP) {
|
||||
m.traverseUserGroup(
|
||||
func (int64, *userGroup) bool {
|
||||
this.groupMails.Store(m.mailId, m)
|
||||
return false
|
||||
})
|
||||
m.traverseRecipients(
|
||||
func (accountId string) bool {
|
||||
if p, ok := this.personalMails.Load(accountId); ok {
|
||||
(p.(*sync.Map)).Store(m.mailId, m)
|
||||
} else {
|
||||
p := new(sync.Map)
|
||||
p.Store(m.mailId, m)
|
||||
this.personalMails.Store(accountId, p)
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (this *mailMgr) updateMail(m *mail) {
|
||||
oldM := this.internalGetMail(m.mailId)
|
||||
if oldM == nil {
|
||||
this.addMail(m)
|
||||
return
|
||||
}
|
||||
if oldM.mailType != m.mailType {
|
||||
panic(fmt.Sprintf("updateMail mailType error"));
|
||||
return
|
||||
}
|
||||
this.removeMail(oldM)
|
||||
this.addMail(m)
|
||||
}
|
||||
|
||||
func (this *mailMgr) addGroup(g *userGroup) {
|
||||
this.groupHash.Store(g.groupId, g)
|
||||
}
|
||||
|
||||
func (this *mailMgr) getGroup(groupId int64) *userGroup {
|
||||
if p, ok := this.groupHash.Load(groupId); ok {
|
||||
return p.(*userGroup)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (this *mailMgr) getMail(mailId string) common.Mail {
|
||||
if m := this.internalGetMail(q5.ToInt64(mailId)); m != nil {
|
||||
return m
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (this *mailMgr) internalGetMail(mailId int64) *mail {
|
||||
if p, ok := this.idHash.Load(mailId); ok {
|
||||
return p.(*mail)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (this *mailMgr) syncEvent() {
|
||||
f5.GetGoStyleDb().SyncSelectCustomQuery(
|
||||
constant.MAIL_DB,
|
||||
"SELECT MAX(idx) FROM t_event;",
|
||||
func(err error, ds *f5.DataSet) {
|
||||
if err != nil {
|
||||
panic("sync event error")
|
||||
return
|
||||
}
|
||||
if ds.Next() {
|
||||
this.lastSyncEventIdx = q5.ToInt64(ds.GetByIndex(0))
|
||||
} else {
|
||||
panic("sync event error")
|
||||
}
|
||||
});
|
||||
f5.GetTimer().SetInterval(
|
||||
1000 * 2,
|
||||
func (e int32, args *q5.Args) {
|
||||
if e == q5.TIMER_EXEC_EVENT {
|
||||
this.pullEvent()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (this *mailMgr) addDbEvent(e *dbEvent) {
|
||||
this.eventQueue.Push(&e.entry)
|
||||
}
|
||||
|
||||
func (this *mailMgr) updateDbEvent() {
|
||||
if this.eventQueue.IsEmpty() {
|
||||
return
|
||||
}
|
||||
if this.procingEvent {
|
||||
return
|
||||
}
|
||||
this.eventQueue.Fetch()
|
||||
for !this.eventQueue.WorkList.Empty() {
|
||||
e := this.eventQueue.WorkList.FirstEntry().(*dbEvent)
|
||||
if e.eventName == constant.EVENT_MAIL_UPDATE {
|
||||
this.procMailUpdate(e)
|
||||
break
|
||||
} else if e.eventName == constant.EVENT_UPSER_GROUP_UPDATE {
|
||||
this.procGroupUpdate(e)
|
||||
break
|
||||
} else {
|
||||
e.entry.DelInit()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (this *mailMgr) pullEvent() {
|
||||
if this.pullingEvent {
|
||||
return
|
||||
}
|
||||
this.pullingEvent = true
|
||||
f5.GetJsStyleDb().SelectCustomQuery(
|
||||
constant.MAIL_DB,
|
||||
fmt.Sprintf("SELECT * FROM t_event WHERE idx > %d;", this.lastSyncEventIdx),
|
||||
func(err error, ds *f5.DataSet) {
|
||||
if err != nil {
|
||||
this.pullingEvent = false
|
||||
return
|
||||
}
|
||||
for ds.Next() {
|
||||
e := new(dbEvent)
|
||||
e.idx = q5.ToInt64(ds.GetByName("idx"))
|
||||
e.eventName = ds.GetByName("event_name")
|
||||
e.param1 = ds.GetByName("param1")
|
||||
e.entry.Init(e)
|
||||
_mailMgr.addDbEvent(e)
|
||||
this.lastSyncEventIdx = q5.ToInt64(ds.GetByName("idx"))
|
||||
}
|
||||
this.pullingEvent = false
|
||||
})
|
||||
}
|
||||
|
||||
func (this* mailMgr) HasTask() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (this* mailMgr) procMailUpdate(e *dbEvent) {
|
||||
this.procingEvent = true
|
||||
f5.GetJsStyleDb().OrmSelectOne(
|
||||
constant.MAIL_DB,
|
||||
"t_mail",
|
||||
[][]string{
|
||||
{"mail_id", e.param1},
|
||||
},
|
||||
func (err error, ds *f5.DataSet) {
|
||||
this.procingEvent = false
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if ds.Next() {
|
||||
p := newMail()
|
||||
p.loadFromDb(ds)
|
||||
this.updateMail(p)
|
||||
} else {
|
||||
panic(fmt.Sprintf("procMailUpdate error:%s", err));
|
||||
}
|
||||
e.entry.DelInit()
|
||||
this.updateDbEvent()
|
||||
})
|
||||
}
|
||||
|
||||
func (this* mailMgr) procGroupUpdate(e *dbEvent) {
|
||||
this.procingEvent = true
|
||||
fetchMemberFunc := func () {
|
||||
f5.GetJsStyleDb().OrmSelect(
|
||||
constant.MAIL_DB,
|
||||
"t_member",
|
||||
[][]string{
|
||||
{"group_id", e.param1},
|
||||
{"deleted", "0"},
|
||||
},
|
||||
func (err error, ds *f5.DataSet) {
|
||||
this.procingEvent = false
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
groupId := q5.ToInt64(ds.GetByName("group_id"))
|
||||
g := this.getGroup(groupId)
|
||||
if g == nil {
|
||||
p := newUserGroup()
|
||||
p.groupId = groupId
|
||||
this.addGroup(g)
|
||||
} else {
|
||||
g.userHash = new(sync.Map)
|
||||
}
|
||||
if ds.Next() {
|
||||
memberId := ds.GetByName("member_id")
|
||||
g.userHash.Store(memberId, q5.ToInt64(ds.GetByName("idx")))
|
||||
return
|
||||
} else {
|
||||
panic(fmt.Sprintf("procGroupUpdate1 error:%s", err));
|
||||
}
|
||||
})
|
||||
}
|
||||
f5.GetJsStyleDb().OrmSelectOne(
|
||||
constant.MAIL_DB,
|
||||
"t_group",
|
||||
[][]string{
|
||||
{"group_id", e.param1},
|
||||
},
|
||||
func (err error, ds *f5.DataSet) {
|
||||
if err != nil {
|
||||
this.procingEvent = false
|
||||
return
|
||||
}
|
||||
if ds.Next() {
|
||||
deleted := q5.ToInt32(ds.GetByName("deleted"))
|
||||
if deleted != 0 {
|
||||
this.procingEvent = false
|
||||
this.groupHash.Delete(q5.ToInt64(e.param1))
|
||||
return
|
||||
}
|
||||
fetchMemberFunc()
|
||||
return
|
||||
} else {
|
||||
this.procingEvent = false
|
||||
panic(fmt.Sprintf("procGroupUpdate1 error:%s", err));
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func newUserGroup() *userGroup {
|
||||
g := new(userGroup)
|
||||
g.userHash = new(sync.Map)
|
||||
return g
|
||||
}
|
@ -2,26 +2,9 @@ package middleware
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
. "main/global"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func CaAuth(c *gin.Context) {
|
||||
accountId := c.DefaultQuery("account_id", "")
|
||||
sessionId := c.DefaultQuery("session_id", "")
|
||||
hum := GetPlayerMgr().GetPlayerByAccountId(accountId)
|
||||
if hum == nil {
|
||||
hum = GetPlayerMgr().ForceCreatePlayer(accountId, sessionId)
|
||||
}
|
||||
if !hum.Lock() {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"errcode": 500,
|
||||
"errmsg": "server internal error",
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
c.Set("hum", hum)
|
||||
c.Next()
|
||||
hum.UnLock()
|
||||
//_ := c.DefaultQuery("account_id", "")
|
||||
//_ := c.DefaultQuery("session_id", "")
|
||||
}
|
||||
|
@ -1,12 +0,0 @@
|
||||
package player
|
||||
|
||||
import (
|
||||
"main/constant"
|
||||
"main/global"
|
||||
)
|
||||
|
||||
var _playerMgr = new(playerMgr)
|
||||
|
||||
func init() {
|
||||
global.RegModule(constant.PLAYER_MGR_MODULE_IDX, _playerMgr)
|
||||
}
|
@ -1,180 +0,0 @@
|
||||
package player
|
||||
|
||||
import (
|
||||
"q5"
|
||||
"f5"
|
||||
"sync"
|
||||
"main/common"
|
||||
"main/constant"
|
||||
"main/model"
|
||||
)
|
||||
|
||||
type inbox struct {
|
||||
mailId int64
|
||||
state int32
|
||||
expireTime int32
|
||||
}
|
||||
|
||||
type player struct {
|
||||
lock sync.Mutex
|
||||
accountId string
|
||||
sessionId string
|
||||
registerTime int32
|
||||
loaded bool
|
||||
inboxHash map[int64]*inbox
|
||||
}
|
||||
|
||||
func (this *player) init() {
|
||||
this.inboxHash = make(map[int64]*inbox)
|
||||
}
|
||||
|
||||
func (this *player) Lock() bool {
|
||||
this.lock.Lock()
|
||||
if !this.loaded {
|
||||
this.load()
|
||||
}
|
||||
ok := this.loaded
|
||||
if !ok {
|
||||
this.UnLock()
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
func (this *player) UnLock() {
|
||||
this.lock.Unlock()
|
||||
}
|
||||
|
||||
func (this *player) GetAccountId() string {
|
||||
return this.accountId
|
||||
}
|
||||
|
||||
func (this *player) GetRegisterTime() int32 {
|
||||
return this.registerTime
|
||||
}
|
||||
|
||||
func (this *player) MarkMails(mails []common.Mail) error {
|
||||
this.checkLock()
|
||||
var resultErr error
|
||||
var nowTime = f5.GetApp().GetRealSeconds()
|
||||
for _, m := range(mails) {
|
||||
if m.IsValid(this) {
|
||||
mi := this.getInbox(m.GetMailId())
|
||||
if mi == nil {
|
||||
err := model.Inbox.Mark(this.GetAccountId(), m.GetMailId(), nowTime, m.GetExpireTime())
|
||||
if err != nil {
|
||||
resultErr = err
|
||||
break
|
||||
}
|
||||
mi = new(inbox)
|
||||
mi.mailId = m.GetMailId()
|
||||
mi.state = constant.INBOX_STATE_READ
|
||||
mi.expireTime = m.GetExpireTime()
|
||||
this.inboxHash[mi.mailId] = mi
|
||||
} else if mi.state != constant.INBOX_STATE_NONE {
|
||||
err := model.Inbox.Mark(this.GetAccountId(), m.GetMailId(), nowTime, m.GetExpireTime())
|
||||
if err != nil {
|
||||
resultErr = err
|
||||
break
|
||||
}
|
||||
mi.state = constant.INBOX_STATE_READ
|
||||
mi.expireTime = m.GetExpireTime()
|
||||
}
|
||||
}
|
||||
}
|
||||
return resultErr
|
||||
}
|
||||
|
||||
func (this *player) GetAttachment(mails []common.Mail) error {
|
||||
return this.DeleteMails(mails)
|
||||
}
|
||||
|
||||
func (this *player) DeleteMails(mails []common.Mail) error {
|
||||
this.checkLock()
|
||||
var resultErr error
|
||||
var nowTime int64
|
||||
for _, m := range(mails) {
|
||||
if m.IsValid(this) {
|
||||
mi := this.getInbox(m.GetMailId())
|
||||
if mi == nil {
|
||||
err := model.Inbox.Delete(this.GetAccountId(), m.GetMailId(), nowTime, m.GetExpireTime())
|
||||
if err != nil {
|
||||
resultErr = err
|
||||
break
|
||||
}
|
||||
mi = new(inbox)
|
||||
mi.mailId = m.GetMailId()
|
||||
mi.state = constant.INBOX_STATE_DELETED
|
||||
mi.expireTime = m.GetExpireTime()
|
||||
this.inboxHash[mi.mailId] = mi
|
||||
} else if mi.state != constant.INBOX_STATE_DELETED {
|
||||
err := model.Inbox.Delete(this.GetAccountId(), m.GetMailId(), nowTime, m.GetExpireTime())
|
||||
if err != nil {
|
||||
resultErr = err
|
||||
break
|
||||
}
|
||||
mi.state = constant.INBOX_STATE_DELETED
|
||||
mi.expireTime = m.GetExpireTime()
|
||||
}
|
||||
}
|
||||
}
|
||||
return resultErr
|
||||
}
|
||||
|
||||
func (this *player) checkLock() {
|
||||
if this.lock.TryLock() {
|
||||
panic("player checkLock error")
|
||||
}
|
||||
}
|
||||
|
||||
func (this *player) load() {
|
||||
f5.GetGoStyleDb().RawQuery(
|
||||
constant.MAIL_DB,
|
||||
"SELECT * FROM t_inbox WHERE account_id=? AND expiretime<?",
|
||||
[]string{
|
||||
this.GetAccountId(),
|
||||
q5.ToString(f5.GetApp().GetRealSeconds() + 3600 * 24 * 7),
|
||||
},
|
||||
func (err error, ds *f5.DataSet) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for ds.Next() {
|
||||
p := new(inbox)
|
||||
p.mailId = q5.ToInt64(ds.GetByName("mail_id"))
|
||||
p.state = q5.ToInt32(ds.GetByName("state"))
|
||||
p.expireTime = q5.ToInt32(ds.GetByName("expiretime"))
|
||||
this.inboxHash[p.mailId] = p
|
||||
}
|
||||
this.loaded = true
|
||||
})
|
||||
}
|
||||
|
||||
func (this *player) getInbox(mailId int64) *inbox {
|
||||
if m, ok := this.inboxHash[mailId]; ok {
|
||||
return m
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (this* player) IsReadable(m common.Mail) bool {
|
||||
mi := this.getInbox(m.GetMailId())
|
||||
if mi != nil {
|
||||
return mi.state != constant.INBOX_STATE_DELETED
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (this* player) IsUnread(m common.Mail) bool {
|
||||
mi := this.getInbox(m.GetMailId())
|
||||
if mi != nil {
|
||||
return mi.state == constant.INBOX_STATE_NONE
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func newPlayer() *player {
|
||||
p := new(player)
|
||||
p.init()
|
||||
return p
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
package player
|
||||
|
||||
import (
|
||||
"f5"
|
||||
"sync"
|
||||
"main/common"
|
||||
)
|
||||
|
||||
type playerMgr struct {
|
||||
accountIdHash sync.Map
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
func (this *playerMgr) Init() {
|
||||
}
|
||||
|
||||
func (this *playerMgr) UnInit() {
|
||||
}
|
||||
|
||||
func (this *playerMgr) GetPlayerByAccountId(accountId string) common.Player {
|
||||
if p, ok := this.accountIdHash.Load(accountId); ok {
|
||||
return p.(*player)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (this *playerMgr) ForceCreatePlayer(accountId string, sessionId string) common.Player {
|
||||
defer this.lock.Unlock()
|
||||
this.lock.Lock()
|
||||
hum := this.GetPlayerByAccountId(accountId)
|
||||
if hum == nil {
|
||||
p := newPlayer()
|
||||
p.accountId = accountId
|
||||
p.sessionId = sessionId
|
||||
p.registerTime = f5.ExtractRegisterTimeFromSessionId(sessionId)
|
||||
hum = p
|
||||
this.accountIdHash.Store(p.accountId, p)
|
||||
}
|
||||
return hum
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user