Exemplo n.º 1
0
    def clean_expired(self):
        limit_timestamp = get_mail_clean_time().timestamp

        doc = MongoMail.db(self.server_id).find_one({'_id': self.char_id}, {'mails': 1})
        expired = []

        total_amount = len(doc['mails'])

        for k, v in doc['mails'].iteritems():
            if v['create_at'] <= limit_timestamp:
                expired.append(k)

        if expired:
            if len(expired) == total_amount:
                MongoMail.db(self.server_id).delete_one({'_id': self.char_id})
            else:
                updater = {"mails.{0}".format(i): 1 for i in expired}

                MongoMail.db(self.server_id).update_one(
                    {'_id': self.char_id},
                    {'$unset': updater}
                )

            self.send_remove_notify(expired)
        return len(expired)
Exemplo n.º 2
0
    def __init__(self, server_id, char_id):
        self.server_id = server_id
        self.char_id = char_id

        doc = MongoMail.db(server_id).find_one({'_id': char_id})
        if not doc:
            doc = MongoMail.document()
            doc['_id'] = char_id
            MongoMail.db(server_id).insert_one(doc)

        # try get shared mail
        with SharedMail(server_id).fetch_and_clean(char_id) as shared_mails:
            for sm in shared_mails:
                if self.is_unique_id_exists(sm['_id'], doc=doc):
                    # 防止因为不确定原因导致的 重复添加邮件问题
                    continue

                self.add(
                    title=sm['title'],
                    content=sm['content'],
                    attachment=sm['attachment'],
                    create_at=sm['create_at'],
                    from_id=sm['from_id'],
                    function=sm['function'],
                    unique_id=sm['_id'],
                    send_notify=False
                )
Exemplo n.º 3
0
    def delete(self, mail_id):
        key = "mails.{0}".format(mail_id)
        MongoMail.db(self.server_id).update_one(
            {'_id': self.char_id},
            {'$unset': {key: 1}}
        )

        self.send_remove_notify([mail_id])
Exemplo n.º 4
0
    def open(self, mail_id):
        from apps.history_record.models import MailHistoryRecord

        if not self.get_mail(mail_id):
            raise GameException(ConfigErrorMessage.get_error_id("MAIL_NOT_EXISTS"))

        MongoMail.db(self.server_id).update_one(
            {'_id': self.char_id},
            {'$set': {'mails.{0}.has_read'.format(mail_id): True}}
        )

        MailHistoryRecord.set_read(mail_id)
        self.send_notify(ids=[mail_id])
Exemplo n.º 5
0
    def get_mail(self, mail_id):
        doc = MongoMail.db(self.server_id).find_one(
            {'_id': self.char_id},
            {'mails.{0}'.format(mail_id): 1}
        )

        return doc['mails'].get(mail_id, None)
Exemplo n.º 6
0
    def cronjob(server_id):
        # 删除过期邮件
        cleaned_amount = 0
        doc = MongoMail.db(server_id).find({}, {'_id': 1})
        for d in doc:
            mm = MailManager(server_id, d['_id'])
            cleaned_amount += mm.clean_expired()

        return cleaned_amount
Exemplo n.º 7
0
    def get_attachment(self, mail_id):
        this_mail = self.get_mail(mail_id)
        if not this_mail:
            raise GameException(ConfigErrorMessage.get_error_id("MAIL_NOT_EXISTS"))

        attachment = this_mail['attachment']
        if not attachment:
            raise GameException(ConfigErrorMessage.get_error_id("MAIL_HAS_NO_ATTACHMENT"))

        rc = ResourceClassification.load_from_json(attachment)
        rc.add(self.server_id, self.char_id, message="MailManager.get_attachment")

        MongoMail.db(self.server_id).update_one(
            {'_id': self.char_id},
            {'$set': {'mails.{0}.attachment'.format(mail_id): ""}}
        )

        self.send_notify(ids=[mail_id])
        return rc
Exemplo n.º 8
0
    def is_unique_id_exists(self, unique_id, doc=None):
        if not doc:
            doc = MongoMail.db(self.server_id).find_one(
                {'_id': self.char_id},
            )

        for k, v in doc['mails'].iteritems():
            if v.get('unique_id', '') == unique_id:
                return True

        return False
Exemplo n.º 9
0
    def add(self, title, content, attachment="", create_at=None, from_id=0, function=0, unique_id='', send_notify=True):
        if create_at:
            now = arrow.get(create_at)
        else:
            now = arrow.utcnow()

        doc = MongoMail.document_mail()
        doc['from_id'] = from_id
        doc['title'] = title
        doc['content'] = content
        doc['attachment'] = attachment
        doc['create_at'] = now.timestamp
        doc['function'] = function
        doc['unique_id'] = unique_id

        # doc['data'] = base64.b64encode(data)

        mail_id = make_string_id()

        MongoMail.db(self.server_id).update_one(
            {'_id': self.char_id},
            {'$set': {'mails.{0}'.format(mail_id): doc}}
        )

        MailHistoryRecord.create(
            _id=mail_id,
            from_id=from_id,
            to_id=self.char_id,
            title=title,
            content=content,
            attachment=attachment,
            function=function,
            create_at=now.format("YYYY-MM-DD HH:mm:ssZ")
        )

        if send_notify:
            self.send_notify(ids=[mail_id])
Exemplo n.º 10
0
    def send_notify(self, ids=None):
        if ids:
            projection = {"mails.{0}".format(i): 1 for i in ids}
            act = ACT_UPDATE
        else:
            projection = {"mails": 1}
            act = ACT_INIT

        doc = MongoMail.db(self.server_id).find_one({'_id': self.char_id}, projection)
        notify = MailNotify()
        notify.act = act

        limit_timestamp = get_mail_clean_time().timestamp

        for k, v in doc['mails'].iteritems():
            notify_mail = notify.mails.add()
            notify_mail.id = k

            if v['from_id']:
                # char
                notify_mail.mail_from.from_type = MAIL_FROM_USER
                # XXX
                notify_mail.mail_from.club.MergeFrom(Club(self.server_id, v['from_id']).make_protomsg())
            else:
                notify_mail.mail_from.from_type = MAIL_FROM_SYSTEM

            notify_mail.title = v['title']
            notify_mail.content = v['content']
            notify_mail.has_read = v['has_read']
            notify_mail.create_at = v['create_at']
            notify_mail.function = v['function']
            if v.get('data', None):
                notify_mail.data = base64.b64decode(v['data'])

            remained_seconds = v['create_at'] - limit_timestamp
            if remained_seconds < 0:
                remained_seconds = 0

            notify_mail.remained_seconds = remained_seconds

            if v['attachment']:
                notify_mail.attachment.MergeFrom(ResourceClassification.load_from_json(v['attachment']).make_protomsg())

            function = v.get('function', 0)
            if function:
                notify_mail.function = function

        MessagePipe(self.char_id).put(msg=notify)
Exemplo n.º 11
0
    def add(self, for_char_ids, title, content, attachment="", from_id=0, function=0):
        doc = MongoMail.document_mail()

        doc['_id'] = make_string_id()
        doc['for_char_ids'] = for_char_ids

        doc['from_id'] = from_id
        doc['title'] = title
        doc['content'] = content
        doc['attachment'] = attachment
        doc['create_at'] = arrow.utcnow().timestamp
        doc['function'] = function

        MongoSharedMail.db(self.server_id).insert_one(doc)

        # 立即给最近登陆操作的人发送通知
        recent_char_ids = OperationLog.get_recent_action_char_ids(self.server_id, recent_minutes=30)
        for cid in recent_char_ids:
            if cid in for_char_ids:
                MailManager(self.server_id, cid).send_notify()