kitten-cloud-function
Version:
用于编程猫源码云功能(云变量、云列表等)的客户端工具
471 lines (470 loc) • 19.3 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CodemaoUserInfo = void 0;
const promise_any_1 = __importDefault(require("@ungap/promise-any"));
const other_1 = require("../../utils/other");
const codemao_community_api_1 = require("../codemao-community-api");
const codemao_user_sex_1 = require("./codemao-user-sex");
const codemao_user_badge_1 = require("./codemao-user-badge");
/**
* 编程猫用户信息类。
*
* - 用于获取编程猫用户信息。
* - 所有属性均为`Promise`对象,当属性获取失败时访问该属性的值会被拒绝。
*
* 提供的用户信息详见类属性。
*
* ### 具有以下特性:
* - 集成多个API接口,以确保在部分API接口信息获取失败时仍能提供尽可能完整的用户信息。
* - 内置懒加载和缓存机制,以减少不必要的请求。
*
* ### 集成API接口
*
* #### 已经集成的API接口
* - {@link getUserProfile}
* - {@link getThisUserDetail}
* - {@link getUserDetail}
* - {@link getUserHonor}
*
* #### 将来可能集成的API接口:
* - {@link searchUserByName}
*
* #### API优先级:
* {@link getUserProfile} > {@link getThisUserDetail} > {@link getUserDetail} > {@link getUserHonor}
*/
class CodemaoUserInfo {
get profile() {
return (async () => {
if (this.__profile == null) {
Object.defineProperty(this, "__profile", {
value: (async () => {
const profile = await (0, codemao_community_api_1.getUserProfile)(await this.authorization);
return {
id: profile.id,
nickname: profile.nickname,
avatarURL: profile.avatar_url,
description: profile.description,
grade: profile.grade,
birthday: new Date(profile.birthday * 1000),
};
})(),
enumerable: false,
configurable: true
});
this.setCache(await this.__profile);
}
return this.__profile;
})();
}
get thisDetail() {
return (async () => {
if (this.__thisDetail == null) {
Object.defineProperty(this, "__thisDetail", {
value: (async () => {
const userDetail = await (0, codemao_community_api_1.getThisUserDetail)(await this.authorization);
return {
id: parseInt(userDetail.id),
username: userDetail.username,
nickname: userDetail.nickname,
realname: userDetail.real_name,
avatarURL: userDetail.avatar_url,
description: userDetail.description,
email: userDetail.email,
badge: codemao_user_badge_1.CodemaoUserBadge.parse(userDetail.author_level),
birthday: new Date(userDetail.birthday * 1000),
sex: codemao_user_sex_1.CodemaoUserSex.from(userDetail.sex),
};
})(),
enumerable: false,
configurable: true
});
this.setCache(await this.__thisDetail);
}
return this.__thisDetail;
})();
}
get detail() {
return (async () => {
if (this.__detail == null) {
Object.defineProperty(this, "__detail", {
value: (async () => {
const userDetail = await (0, codemao_community_api_1.getUserDetail)(await this.id);
return {
id: userDetail.user.id,
nickname: userDetail.user.nickname,
avatarURL: userDetail.user.avatar,
description: userDetail.user.description,
doing: userDetail.user.doing,
sex: codemao_user_sex_1.CodemaoUserSex.from(userDetail.user.sex),
viewTimes: userDetail.viewTimes,
praiseTimes: userDetail.praiseTimes,
forkTimes: userDetail.forkedTimes,
};
})(),
enumerable: false,
configurable: true
});
this.setCache(await this.__detail);
}
return this.__detail;
})();
}
get honor() {
return (async () => {
if (this.__honor == null) {
Object.defineProperty(this, "__honor", {
value: (async () => {
const honor = await (0, codemao_community_api_1.getUserHonor)(await this.id);
return {
id: honor.user_id,
nickname: honor.nickname,
avatarURL: honor.avatar_url,
coverURL: honor.user_cover,
description: honor.user_description,
doing: honor.doing,
badge: codemao_user_badge_1.CodemaoUserBadge.parse(honor.author_level),
viewTimes: honor.view_times,
praiseTimes: honor.liked_total,
collectTimes: honor.collect_times,
forkTimes: honor.re_created_total,
};
})(),
enumerable: false,
configurable: true
});
this.setCache(await this.__honor);
}
return this.__honor;
})();
}
/**
* 身份信息。
*/
get authorization() {
if (this.__authorization == null) {
this.__authorization = Promise.reject(new Error("没有提供身份信息"));
}
return this.__authorization.catch((error) => Promise.reject(["获取用户身份信息失败", error]));
}
/**
* 用户ID。
*/
get id() {
if (this.__id == null) {
this.__id = (0, promise_any_1.default)([
Promise.reject(new Error("没有提供用户ID")),
this.profile
.catch((error0) => this.thisDetail.catch((error1) => Promise.reject([error0, error1])))
.then((info) => info.id),
]).catch(({ errors }) => Promise.reject(["获取用户ID失败", errors[0], ...errors[1]]));
}
return this.__id;
}
/**
* 用户名,用户名可以用于登录编程猫账号。如果用户没有设置用户名,则返回空字符串。
*/
get username() {
if (this.__username == null) {
this.__username = (0, promise_any_1.default)([
Promise.reject(new Error("没有提供用户名")),
this.thisDetail.then((info) => info.username),
]).catch(({ errors }) => Promise.reject(["获取用户名失败", ...errors]));
}
return this.__username;
}
/**
* 用户昵称。
*/
get nickname() {
if (this.__nickname == null) {
this.__nickname = (0, promise_any_1.default)([
Promise.reject(new Error("没有提供用户昵称")),
this.profile
.catch((error0) => this.thisDetail.catch((error1) => this.detail.catch((error2) => this.honor.catch((error3) => Promise.reject([error0, error1, error2, error3])))))
.then((info) => info.nickname),
]).catch(({ errors }) => Promise.reject(["获取用户昵称失败", errors[0], ...errors[1]]));
}
return this.__nickname;
}
/**
* 用户真实姓名。如果用户没有填写真实姓名,则返回空字符串。
*/
get realname() {
if (this.__realname == null) {
this.__realname = (0, promise_any_1.default)([
Promise.reject(new Error("没有提供用户真实姓名")),
this.thisDetail.then((info) => info.realname),
]).catch(({ errors }) => Promise.reject(["获取用户真实姓名失败", ...errors]));
}
return this.__realname;
}
/**
* 用户头像地址。
*/
get avatarURL() {
if (this.__avatarURL == null) {
this.__avatarURL = (0, promise_any_1.default)([
Promise.reject(new Error("没有提供用户头像地址")),
this.profile
.catch((error0) => this.thisDetail.catch((error1) => this.detail.catch((error2) => this.honor.catch((error3) => Promise.reject([error0, error1, error2, error3])))))
.then((info) => info.avatarURL),
]).catch(({ errors }) => Promise.reject(["获取用户头像地址失败", errors[0], ...errors[1]]));
}
return this.__avatarURL;
}
/**
* 用户背景图片地址。
*/
get coverURL() {
if (this.__coverURL == null) {
this.__coverURL = (0, promise_any_1.default)([
Promise.reject(new Error("没有提供用户背景图片地址")),
this.honor.then((info) => info.coverURL)
]).catch(({ errors }) => Promise.reject(["获取用户背景图片地址失败", ...errors]));
}
return this.__coverURL;
}
/**
* 用户描述。
*/
get description() {
if (this.__description == null) {
this.__description = (0, promise_any_1.default)([
Promise.reject(new Error("没有提供用户描述")),
this.profile
.catch((error0) => this.thisDetail.catch((error1) => this.detail.catch((error2) => this.honor.catch((error3) => Promise.reject([error0, error1, error2, error3])))))
.then((info) => info.description),
]).catch(({ errors }) => Promise.reject(["获取用户描述失败", errors[0], ...errors[1]]));
}
return this.__description;
}
/**
* 用户正在做什么。
*/
get doing() {
if (this.__doing == null) {
this.__doing = (0, promise_any_1.default)([
Promise.reject(new Error("没有提供用户正在做什么")),
this.detail.then((info) => info.doing),
]).catch(({ errors }) => Promise.reject(["获取用户正在做什么失败", ...errors]));
}
return this.__doing;
}
/**
* 用户邮箱地址。
*/
get email() {
if (this.__email == null) {
this.__email = (0, promise_any_1.default)([
Promise.reject(new Error("没有提供用户邮箱")),
this.thisDetail.then((info) => info.email),
]).catch(({ errors }) => Promise.reject(["获取用户邮箱失败", ...errors]));
}
return this.__email;
}
/**
* 用户创作者勋章。
*/
get badge() {
if (this.__badge == null) {
this.__badge = (0, promise_any_1.default)([
Promise.reject(new Error("没有提供用户创作者勋章")),
this.thisDetail
.catch((error0) => this.honor
.catch((error1) => Promise.reject([error0, error1])))
.then((info) => info.badge),
]).catch(({ errors }) => Promise.reject(["获取用户创作者勋章", errors[0], ...errors[1]]));
}
return this.__badge;
}
/**
* 用户等级。
*/
get grade() {
if (this.__grade == null) {
this.__grade = (0, promise_any_1.default)([
Promise.reject(new Error("没有提供用户等级")),
this.profile.then((info) => info.grade),
]).catch(({ errors }) => Promise.reject(["获取用户等级失败", ...errors]));
}
return this.__grade;
}
/**
* 用户生日。
*/
get birthday() {
if (this.__birthday == null) {
this.__birthday = (0, promise_any_1.default)([
Promise.reject(new Error("没有提供用户生日")),
this.profile
.catch((error0) => this.thisDetail.catch((error1) => Promise.reject([error0, error1])))
.then((info) => info.birthday),
]).catch(({ errors }) => Promise.reject(["获取用户生日失败", errors[0], ...errors[1]]));
}
return this.__birthday;
}
/**
* 用户性别。详见 {@link CodemaoUserSex}。
*/
get sex() {
if (this.__sex == null) {
this.__sex = (0, promise_any_1.default)([
Promise.reject(new Error("没有提供用户性别")),
this.thisDetail
.catch((error0) => this.detail.catch((error1) => Promise.reject([error0, error1])))
.then((info) => info.sex),
]).catch(({ errors }) => Promise.reject(["获取用户性别失败", errors[0], ...errors[1]]));
}
return this.__sex;
}
/**
* 用户所有作品被浏览的次数总和。
*/
get viewTimes() {
if (this.__viewTimes == null) {
this.__viewTimes = (0, promise_any_1.default)([
Promise.reject(new Error("没有提供用户被浏览次数")),
this.detail
.catch((error0) => this.honor.catch((error1) => Promise.reject([error0, error1])))
.then((info) => info.viewTimes),
]).catch(({ errors }) => Promise.reject(["获取用户被浏览次数失败", errors[0], ...errors[1]]));
}
return this.__viewTimes;
}
/**
* 用户所有作品被点赞的次数总和。
*/
get praiseTimes() {
if (this.__praiseTimes == null) {
this.__praiseTimes = (0, promise_any_1.default)([
Promise.reject(new Error("没有提供用户被点赞次数")),
this.detail
.catch((error0) => this.honor.catch((error1) => Promise.reject([error0, error1])))
.then((info) => info.praiseTimes),
]).catch(({ errors }) => Promise.reject(["获取用户被点赞次数失败", errors[0], ...errors[1]]));
}
return this.__praiseTimes;
}
/**
* 用户所有作品被收藏的次数总和。
*/
get collectTimes() {
if (this.__collectTimes == null) {
this.__collectTimes = (0, promise_any_1.default)([
Promise.reject(new Error("没有提供用户被收藏次数")),
this.honor.then((info) => info.collectTimes),
]).catch(({ errors }) => Promise.reject(["获取用户被收藏次数失败", ...errors]));
}
return this.__collectTimes;
}
/**
* 用户所有作品被再创作的次数总和。
*/
get forkTimes() {
if (this.__forkTimes == null) {
this.__forkTimes = (0, promise_any_1.default)([
Promise.reject(new Error("没有提供用户被再创作次数")),
this.honor
.catch((error0) => this.detail.catch((error1) => Promise.reject([error0, error1])))
.then((info) => info.forkTimes),
]).catch(({ errors }) => Promise.reject(["获取用户被再创作次数失败", errors[0], ...errors[1]]));
}
return this.__forkTimes;
}
/**
* @param info 已知的用户信息,如果什么信息都不提供,则表示表示当前登录的用户。
*/
constructor(info) {
for (const key in this) {
if (key.startsWith("__") && this[key] == Node) {
Object.defineProperty(this, key, {
value: undefined,
enumerable: false,
configurable: true
});
}
}
if (Object.keys(info).length == 0) {
this.__authorization = Promise.resolve(null);
}
else {
this.setCache(info);
}
}
setCache(info) {
for (let key in info) {
let value = info[key];
if (value != null) {
Object.defineProperty(this, `__${key}`, {
value: Promise.resolve(value),
enumerable: false,
configurable: true
});
}
}
}
}
exports.CodemaoUserInfo = CodemaoUserInfo;
__decorate([
(0, other_1.enumerable)(true)
], CodemaoUserInfo.prototype, "authorization", null);
__decorate([
(0, other_1.enumerable)(true)
], CodemaoUserInfo.prototype, "id", null);
__decorate([
(0, other_1.enumerable)(true)
], CodemaoUserInfo.prototype, "username", null);
__decorate([
(0, other_1.enumerable)(true)
], CodemaoUserInfo.prototype, "nickname", null);
__decorate([
(0, other_1.enumerable)(true)
], CodemaoUserInfo.prototype, "realname", null);
__decorate([
(0, other_1.enumerable)(true)
], CodemaoUserInfo.prototype, "avatarURL", null);
__decorate([
(0, other_1.enumerable)(true)
], CodemaoUserInfo.prototype, "coverURL", null);
__decorate([
(0, other_1.enumerable)(true)
], CodemaoUserInfo.prototype, "description", null);
__decorate([
(0, other_1.enumerable)(true)
], CodemaoUserInfo.prototype, "doing", null);
__decorate([
(0, other_1.enumerable)(true)
], CodemaoUserInfo.prototype, "email", null);
__decorate([
(0, other_1.enumerable)(true)
], CodemaoUserInfo.prototype, "badge", null);
__decorate([
(0, other_1.enumerable)(true)
], CodemaoUserInfo.prototype, "grade", null);
__decorate([
(0, other_1.enumerable)(true)
], CodemaoUserInfo.prototype, "birthday", null);
__decorate([
(0, other_1.enumerable)(true)
], CodemaoUserInfo.prototype, "sex", null);
__decorate([
(0, other_1.enumerable)(true)
], CodemaoUserInfo.prototype, "viewTimes", null);
__decorate([
(0, other_1.enumerable)(true)
], CodemaoUserInfo.prototype, "praiseTimes", null);
__decorate([
(0, other_1.enumerable)(true)
], CodemaoUserInfo.prototype, "collectTimes", null);
__decorate([
(0, other_1.enumerable)(true)
], CodemaoUserInfo.prototype, "forkTimes", null);