forked from TeamCodeStream/codestream-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost.js
More file actions
47 lines (39 loc) · 1.44 KB
/
Copy pathpost.js
File metadata and controls
47 lines (39 loc) · 1.44 KB
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
// provides the Post model for handling posts
'use strict';
const CodeStreamModel = require(process.env.CSSVC_BACKEND_ROOT + '/api_server/lib/models/codestream_model');
const CodeStreamModelValidator = require(process.env.CSSVC_BACKEND_ROOT + '/api_server/lib/models/codestream_model_validator');
const PostAttributes = require('./post_attributes');
class Post extends CodeStreamModel {
getValidator () {
return new CodeStreamModelValidator(PostAttributes);
}
// right before the post is saved...
async preSave (options) {
// ensure referencing IDs are lower-cased
this.lowerCase('teamId');
this.lowerCase('repoId');
this.lowerCase('streamId');
this.lowerCase('parentPostId');
this.lowerCase('commitHashWhenPosted');
this.lowerCase('markerIds');
this.lowerCase('codemarkIds');
// ensure mentionedUserIds array is sorted
if (this.attributes.mentionedUserIds instanceof Array) {
this.attributes.mentionedUserIds.sort();
}
this.lowerCase('mentionedUserIds');
await super.preSave(options);
}
// does this post mention the current user?
mentionsUser (user) {
const mentionedUserIds = this.get('mentionedUserIds') || [];
return mentionedUserIds.includes(user.id);
}
// get the emote for this post, if it starts with /me (basically the rest of the post)
getEmote () {
const text = this.get('text') || '';
const match = text.match(/^\/me\s+(.*)/);
return match && match.length > 1 && match[1];
}
}
module.exports = Post;