UNPKG

@ginstone/nga-api

Version:

96 lines (95 loc) 3.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PrivateMessageReply = exports.PmReplyBody = exports.parseStatus = exports.parseUsers = exports.PrivateMessage = exports.PmListBody = void 0; const ObjUtils_1 = require("../../utils/ObjUtils"); const SimpleUserInfo_1 = require("../field/user/SimpleUserInfo"); const PrivateMessageStatus_1 = require("../../enums/PrivateMessageStatus"); const UserContext_1 = require("../field/user/UserContext"); const StrUtils_1 = require("../../utils/StrUtils"); const BbsTagParser_1 = require("../../bbscode/BbsTagParser"); /** * 私信 */ class PmListBody { constructor(raw) { this.page = raw.currentPage; this.hasNext = raw.nextPage; this.size = raw.rowsPerPage; this.data = (0, ObjUtils_1.values)(raw).filter(i => typeof i === 'object').map(i => new PrivateMessage(i)); } } exports.PmListBody = PmListBody; class PrivateMessage { constructor(raw) { this.messageId = raw.mid; this.bit = raw.bit; this.title = raw.subject; this.timeCreated = new Date(raw.time * 1000); this.lastModify = new Date(raw.last_modify * 1000); this.postCount = raw.posts; this.fromUid = raw.from; this.fromUsername = raw.from_username; this.lastReplyUid = raw.last_from; this.lastReplyUsername = raw.last_from_username; this.users = (0, exports.parseUsers)(raw.all_user); this.status = (0, exports.parseStatus)(this.bit); this.unread = this.status.indexOf(PrivateMessageStatus_1.PrivateMessageStatus.UNREAD) > -1; } } exports.PrivateMessage = PrivateMessage; const parseUsers = (s) => { let arr = []; if (s) { const userArray = s.split("\t"); for (let i = 0; i < userArray.length; i += 2) { const uid = Number(userArray[i]); const username = userArray[i + 1]; arr.push(new SimpleUserInfo_1.SimpleUserInfo({ uid, username })); } } return arr; }; exports.parseUsers = parseUsers; const parseStatus = (bit) => { let arr = []; const s = (0, StrUtils_1.parseBit)(bit); for (let key in PrivateMessageStatus_1.PrivateMessageStatus) { const index = Number(key); if (!isNaN(index) && s.charAt(index) === '1') { arr.push(index); } } return arr; }; exports.parseStatus = parseStatus; /** * 私信会话详情 */ class PmReplyBody { constructor(raw) { this.length = raw.length; this.hasNext = raw.nextPage; this.starterUid = raw.starterUid; this.userInfo = new UserContext_1.UserContext(raw.userInfo); this.page = raw.currentPage; this.users = (0, exports.parseUsers)(raw.allUsers); this.bit = raw.subjectBit; this.status = (0, exports.parseStatus)(this.bit); this.data = (0, ObjUtils_1.values)(raw).filter(i => i && typeof i === 'object' && i.hasOwnProperty("from")).map(i => new PrivateMessageReply(i)); } } exports.PmReplyBody = PmReplyBody; /** * 私信回复 */ class PrivateMessageReply { constructor(raw) { this.id = raw.id; this.title = raw.subject; this.content = raw.content; this.userId = raw.from; this.timestamp = new Date(raw.time * 1000); this.contentNodes = BbsTagParser_1.BbsTagParser.parseContent(raw.content); } } exports.PrivateMessageReply = PrivateMessageReply;