summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/room/show.ts
blob: 85cd57aef460e3e03679854f02a2698f263efb5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import $ from 'cafy';
import define from '../../define';
import { ApiError } from '../../error';
import { Users, UserProfiles } from '../../../../models';
import { ID } from '@/misc/cafy-id';
import { toPunyNullable } from '@/misc/convert-host';

export const meta = {
	tags: ['room'],

	requireCredential: false as const,

	params: {
		userId: {
			validator: $.optional.type(ID),
		},

		username: {
			validator: $.optional.str
		},

		host: {
			validator: $.optional.nullable.str
		},
	},

	errors: {
		noSuchUser: {
			message: 'No such user.',
			code: 'NO_SUCH_USER',
			id: '7ad3fa3e-5e12-42f0-b23a-f3d13f10ee4b'
		}
	},

	res: {
		type: 'object' as const,
		optional: false as const, nullable: false as const,
		properties: {
			roomType: {
				type: 'string' as const,
				optional: false as const, nullable: false as const,
				enum: ['default', 'washitsu']
			},
			furnitures: {
				type: 'array' as const,
				optional: false as const, nullable: false as const,
				items: {
					type: 'object' as const,
					optional: false as const, nullable: false as const,
					properties: {
						id: {
							type: 'string' as const,
							optional: false as const, nullable: false as const
						},
						type: {
							type: 'string' as const,
							optional: false as const, nullable: false as const
						},
						props: {
							type: 'object' as const,
							optional: true as const, nullable: false as const,
						},
						position: {
							type: 'object' as const,
							optional: false as const, nullable: false as const,
							properties: {
								x: {
									type: 'number' as const,
									optional: false as const, nullable: false as const
								},
								y: {
									type: 'number' as const,
									optional: false as const, nullable: false as const
								},
								z: {
									type: 'number' as const,
									optional: false as const, nullable: false as const
								}
							}
						},
						rotation: {
							type: 'object' as const,
							optional: false as const, nullable: false as const,
							properties: {
								x: {
									type: 'number' as const,
									optional: false as const, nullable: false as const
								},
								y: {
									type: 'number' as const,
									optional: false as const, nullable: false as const
								},
								z: {
									type: 'number' as const,
									optional: false as const, nullable: false as const
								}
							}
						}
					}
				}
			},
			carpetColor: {
				type: 'string' as const,
				optional: false as const, nullable: false as const,
				format: 'hex',
				example: '#85CAF0'
			}
		}
	}
};

export default define(meta, async (ps, me) => {
	const user = await Users.findOne(ps.userId != null
		? { id: ps.userId }
		: { usernameLower: ps.username!.toLowerCase(), host: toPunyNullable(ps.host) });

	if (user == null) {
		throw new ApiError(meta.errors.noSuchUser);
	}

	const profile = await UserProfiles.findOneOrFail(user.id);

	if (profile.room.furnitures == null) {
		await UserProfiles.update(user.id, {
			room: {
				furnitures: [],
				...profile.room
			}
		});

		profile.room.furnitures = [];
	}

	if (profile.room.roomType == null) {
		const initialType = 'default';
		await UserProfiles.update(user.id, {
			room: {
				roomType: initialType as any,
				...profile.room
			}
		});

		profile.room.roomType = initialType;
	}

	if (profile.room.carpetColor == null) {
		const initialColor = '#85CAF0';
		await UserProfiles.update(user.id, {
			room: {
				carpetColor: initialColor as any,
				...profile.room
			}
		});

		profile.room.carpetColor = initialColor;
	}

	return profile.room;
});