/
posts.js
127 lines (112 loc) · 4.85 KB
/
posts.js
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
const _ = require('lodash');
const debug = require('ghost-ignition').debug('api:v2:utils:serializers:input:posts');
const url = require('./utils/url');
const utils = require('../../index');
function removeMobiledocFormat(frame) {
if (frame.options.formats && frame.options.formats.includes('mobiledoc')) {
frame.options.formats = frame.options.formats.filter((format) => {
return (format !== 'mobiledoc');
});
}
}
module.exports = {
browse(apiConfig, frame) {
debug('browse');
// @TODO: `api_key_id` does not work long term, because it can be either a content or an admin api key?
/**
* ## current cases:
* - context object is empty (functional call, content api access)
* - api_key_id exists? content api access
* - user exists? admin api access
*/
if (utils.isContentAPI(frame)) {
// CASE: the content api endpoints for posts should only return non page type resources
if (frame.options.filter) {
if (frame.options.filter.match(/page:\w+\+?/)) {
frame.options.filter = frame.options.filter.replace(/page:\w+\+?/, '');
}
if (frame.options.filter) {
frame.options.filter = frame.options.filter + '+page:false';
} else {
frame.options.filter = 'page:false';
}
} else {
frame.options.filter = 'page:false';
}
// CASE: the content api endpoint for posts should not return mobiledoc
removeMobiledocFormat(frame);
}
debug(frame.options);
},
read(apiConfig, frame) {
debug('read');
// @TODO: `api_key_id` does not work long term, because it can be either a content or an admin api key?
/**
* ## current cases:
* - context object is empty (functional call, content api access)
* - api_key_id exists? content api access
* - user exists? admin api access
*/
if (utils.isContentAPI(frame)) {
frame.data.page = false;
// CASE: the content api endpoint for posts should not return mobiledoc
removeMobiledocFormat(frame);
}
debug(frame.options);
},
add(apiConfig, frame) {
debug('add');
/**
* Convert author property to author_id to match the name in the database.
*
* @deprecated: `author`, might be removed in Ghost 3.0
*/
if (frame.data.posts[0].hasOwnProperty('author')) {
frame.data.posts[0].author_id = frame.data.posts[0].author;
delete frame.data.posts[0].author;
}
/**
* CASE: we don't support updating nested-nested relations e.g. `post.authors[*].roles` yet.
*
* Bookshelf-relations supports this feature, BUT bookshelf's `hasChanged` fn will currently
* clash with this, because `hasChanged` won't be able to tell if relations have changed or not.
* It would always return `changed.roles = [....]`. It would always throw a model event that relations
* were updated, which is not true.
*
* Bookshelf-relations can tell us if a relation has changed, it knows that.
* But the connection between our model layer, Bookshelf's `hasChanged` fn and Bookshelf-relations
* is not present. As long as we don't support this case, we have to ignore this.
*/
if (frame.data.posts[0].authors && frame.data.posts[0].authors.length) {
_.each(frame.data.posts[0].authors, (author, index) => {
if (author.hasOwnProperty('roles')) {
delete frame.data.posts[0].authors[index].roles;
}
if (author.hasOwnProperty('permissions')) {
delete frame.data.posts[0].authors[index].permissions;
}
});
}
/**
* Model notation is: `tag.parent_id`.
* The API notation is `tag.parent`.
*/
if (frame.data.posts[0].hasOwnProperty('tags')) {
if (_.isArray(frame.data.posts[0].tags) && frame.data.posts[0].tags.length) {
_.each(frame.data.posts[0].tags, (tag, index) => {
if (tag.hasOwnProperty('parent')) {
frame.data.posts[0].tags[index].parent_id = tag.parent;
delete frame.data.posts[0].tags[index].parent;
}
if (tag.hasOwnProperty('posts')) {
delete frame.data.posts[0].tags[index].posts;
}
});
}
}
frame.data.posts[0] = url.forPost(Object.assign({}, frame.data.posts[0]), frame.options);
},
edit(apiConfig, frame) {
this.add(apiConfig, frame);
}
};