/
wall.ts
496 lines (422 loc) · 11.5 KB
/
wall.ts
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
import { Attachment, AttachmentFactoryOptions } from './attachment';
import { ExternalAttachment } from './external';
import { Attachmentable } from '../shared';
// eslint-disable-next-line import/no-cycle
import { transformAttachments } from './helpers';
import { AttachmentType, kSerializeData } from '../../utils/constants';
import { pickProperties, applyMixins } from '../../utils/helpers';
const kAttachments = Symbol('attachments');
const kCopyHistoryAttachments = Symbol('copyHistoryAttachments');
export interface IWallAttachmentPayload {
id: number;
to_id?: number;
owner_id: number;
access_key?: string;
from_id?: number;
created_by?: number;
date?: number;
text?: string;
reply_owner_id?: number;
reply_post_id?: number;
friends_only?: number;
comments?: {
count: number;
can_post: number;
groups_can_post: number;
can_close: number;
can_open: number;
};
copyright?: {
id?: number;
link: string;
name: string;
type: string;
};
likes?: {
count: number;
user_likes: number;
can_like: number;
can_publish: number;
};
reposts?: {
count: number;
user_reposted: number;
};
views?: {
count: number;
};
post_type?: string;
post_source?: {
type: string;
platform: string;
data: string;
url: string;
};
attachments?: object[];
geo?: object;
signer_id?: number;
copy_history?: IWallAttachmentPayload[];
can_pin?: number;
can_delete?: number;
can_edit?: number;
is_pinned?: number;
marked_as_ads?: number;
is_favorite?: number;
donut?: {
is_donut: boolean;
paid_duration?: number;
placeholder?: {
text: string;
};
can_publish_free_copy?: boolean;
edit_mode?: 'all' | 'duration';
durations?: {
id: number;
name: string;
}[];
};
postponed_id?: number;
}
export type WallAttachmentOptions =
AttachmentFactoryOptions<IWallAttachmentPayload>;
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
class WallAttachment extends Attachment<IWallAttachmentPayload, AttachmentType.WALL | 'wall'> {
protected [kAttachments]!: (Attachment | ExternalAttachment)[];
protected [kCopyHistoryAttachments]!: WallAttachment[];
/**
* Constructor
*/
public constructor(options: WallAttachmentOptions) {
super({
...options,
type: AttachmentType.WALL,
});
this.$filled = this.payload.date !== undefined;
this.applyPayload(options.payload);
}
/**
* Load attachment payload
*/
public async loadAttachmentPayload(): Promise<void> {
if (this.$filled) {
return;
}
const { items: [post] } = await this.api.wall.getById({
posts: `${this.ownerId}_${this.id}`,
extended: 0,
});
this.applyPayload(post as unknown as IWallAttachmentPayload);
this.$filled = true;
}
/**
* Checks has comments
*/
public get hasComments(): boolean | undefined {
const { commentsCount } = this;
return commentsCount !== undefined
? commentsCount > 0
: undefined;
}
/**
* Checks has ads in post
*/
public get hasAds(): boolean | undefined {
if (!this.$filled) {
return undefined;
}
return Boolean(this.payload.marked_as_ads);
}
/**
* Checks has this user reposted
*/
public get hasUserReposted(): boolean | undefined {
if (!this.$filled) {
return undefined;
}
return Boolean(this.payload.reposts!.user_reposted);
}
/**
* Checks has this user likes
*/
public get hasUserLike(): boolean | undefined {
if (!this.$filled) {
return undefined;
}
return Boolean(this.payload.likes!.user_likes);
}
/**
* Checks can the current user comment on the entry
*/
public get isCanUserCommented(): boolean | undefined {
if (!this.$filled) {
return undefined;
}
return Boolean(this.payload.comments!.can_post);
}
/**
* Checks if a community can comment on a post
*/
public get isCanGroupsCommented(): boolean | undefined {
if (!this.$filled) {
return undefined;
}
return Boolean(this.payload.comments!.groups_can_post);
}
/**
* Checks if you can comment on a post
*/
public get isCanCommented(): boolean | undefined {
return this.isCanUserCommented || this.isCanGroupsCommented;
}
/**
* Checks if a user can close on a comments
*/
public get isCanCloseComments(): boolean | undefined {
if (!this.$filled) {
return undefined;
}
return Boolean(this.payload.comments!.can_close);
}
/**
* Checks if a user can open on a comments
*/
public get isCanOpenComments(): boolean | undefined {
if (!this.$filled) {
return undefined;
}
return Boolean(this.payload.comments!.can_open);
}
/**
* Checks whether the current user can like the record
*/
public get isCanLike(): boolean | undefined {
if (!this.$filled) {
return undefined;
}
return Boolean(this.payload.likes!.can_like);
}
/**
* hecks whether the current user can repost the record
*/
public get isCanReposted(): boolean | undefined {
if (!this.$filled) {
return undefined;
}
return Boolean(this.payload.likes!.can_publish);
}
/**
* Checks is can this user pin post
*/
public get isCanPin(): boolean | undefined {
if (!this.$filled) {
return undefined;
}
return Boolean(this.payload.can_pin);
}
/**
* Checks is can this user delete post
*/
public get isCanDelete(): boolean | undefined {
if (!this.$filled) {
return undefined;
}
return Boolean(this.payload.can_delete);
}
/**
* Checks is can this user edit post
*/
public get isCanEdit(): boolean | undefined {
if (!this.$filled) {
return undefined;
}
return Boolean(this.payload.can_edit);
}
/**
* Checks is can this user edit post
*/
public get isPinned(): boolean | undefined {
if (!this.$filled) {
return undefined;
}
return Boolean(this.payload.is_pinned);
}
/**
* Checks is post created only by friends
*/
public get isFriendsOnly(): boolean | undefined {
if (!this.$filled) {
return undefined;
}
return Boolean(this.payload.friends_only);
}
/**
* Checks is bookmarked current user
*/
public get isFavorited(): boolean | undefined {
if (!this.$filled) {
return undefined;
}
return Boolean(this.payload.is_favorite);
}
public get ownerId(): number {
return this.payload.owner_id || this.payload.to_id!;
}
/**
* Returns the identifier author
*/
public get authorId(): number | undefined {
return this.payload.from_id;
}
/**
* Returns the administrator identifier that posted the entry
*/
public get createdUserId(): number | undefined {
return this.payload.created_by;
}
/**
* The identifier of the record owner, in response to which the current
*/
public get replyOwnerId(): number | undefined {
return this.payload.reply_owner_id;
}
/**
* The identifier of the record in response to which the current one was left.
*/
public get replyPostId(): number | undefined {
return this.payload.reply_post_id;
}
/**
* Returns author identifier if the entry was published
* on behalf of the community and signed by the user
*/
public get signerId(): number | undefined {
return this.payload.signer_id;
}
/**
* Returns the id of the pending entry. This field is returned when the entry was on the timer.
*/
public get postponedId(): number | undefined {
return this.payload.postponed_id;
}
/**
* Returns the date when this post was created
*/
public get createdAt(): number | undefined {
return this.payload.date;
}
/**
* Returns the post type
*/
public get postType(): string | undefined {
return this.payload.post_type;
}
/**
* Returns the post text
*/
public get text(): string | undefined {
return this.payload.text;
}
/**
* Returns the number of record views
*/
public get viewsCount(): number | undefined {
return this.payload.views?.count;
}
/**
* Returns the likes count
*/
public get likesCount(): number | undefined {
return this.payload.likes?.count;
}
/**
* Returns the reposts count
*/
public get repostsCount(): number | undefined {
return this.payload.reposts?.count;
}
/**
* Returns the comments count
*/
public get commentsCount(): number | undefined {
return this.payload.comments?.count;
}
/**
* Returns the likes info
*/
public get likes(): IWallAttachmentPayload['likes'] | undefined {
return this.payload.likes;
}
/**
* Returns the post source
*/
public get postSource(): IWallAttachmentPayload['post_source'] | undefined {
return this.payload.post_source;
}
/**
* Returns the geolocation
*/
public get geo(): IWallAttachmentPayload['geo'] | undefined {
return this.payload.geo;
}
/**
* Returns the copyright
*/
public get copyright(): IWallAttachmentPayload['copyright'] | undefined {
return this.payload.copyright;
}
/**
* Returns the attachments
*/
public get attachments(): (Attachment | ExternalAttachment)[] {
return this[kAttachments];
}
/**
* Returns the history of reposts for post
*/
public get copyHistory(): WallAttachment[] | undefined {
return this[kCopyHistoryAttachments];
}
/**
* Applies the payload
*/
private applyPayload(payload: IWallAttachmentPayload): void {
this.payload = payload;
this[kAttachments] = transformAttachments(payload.attachments || [], this.api);
this[kCopyHistoryAttachments] = (payload.copy_history || []).map((history): WallAttachment => (
new WallAttachment({
api: this.api,
payload: history,
})
));
}
/**
* Returns the custom data
*/
public [kSerializeData](): object {
return pickProperties(this, [
'authorId',
'createdUserId',
'replyOwnerId',
'replyPostId',
'signerId',
'postponedId',
'createdAt',
'postType',
'text',
'viewsCount',
'likesCount',
'repostsCount',
'commentsCount',
'likes',
'postSource',
'geo',
'copyright',
'copyHistory',
'attachments',
]);
}
}
// eslint-disable-next-line
interface WallAttachment extends Attachmentable {}
applyMixins(WallAttachment, [Attachmentable]);
export { WallAttachment };