@live-change/content-service 0.2.52 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +109 -3
  2. package/package.json +4 -4
package/index.js CHANGED
@@ -6,11 +6,12 @@ const definition = require('./definition.js')
6
6
  const config = definition.config
7
7
  const {
8
8
  contentReaderRoles = ['reader'],
9
- contentWriterRoles = ['writer']
9
+ contentWriterRoles = ['writer'],
10
+ contentPublisherRoles = ['writer']
10
11
  } = config
11
12
 
12
13
 
13
- const { Content, Session } = require("./model.js")
14
+ const { Content, Session, schemas, getDocument } = require("./model.js")
14
15
  const Snapshot = definition.foreignModel("prosemirror", "Snapshot")
15
16
 
16
17
  definition.view({
@@ -37,7 +38,7 @@ definition.view({
37
38
  }
38
39
  },
39
40
  accessControl: {
40
- roles: ['reader']
41
+ roles: contentReaderRoles
41
42
  },
42
43
  daoPath({ objectType, object }, { client, service }, method) {
43
44
  const contentId = App.encodeIdentifier([objectType, object])
@@ -64,5 +65,110 @@ definition.view({
64
65
  }
65
66
  })
66
67
 
68
+ const Document = definition.foreignModel("prosemirror", "Document")
69
+
70
+ definition.view({
71
+ name: "contentPreview",
72
+ properties: {
73
+ objectType: {
74
+ type: String,
75
+ validation: ['nonEmpty']
76
+ },
77
+ object: {
78
+ type: String,
79
+ validation: ['nonEmpty']
80
+ }
81
+ },
82
+ returns: {
83
+ type: Object,
84
+ properties: {
85
+ content: {
86
+ type: Object,
87
+ },
88
+ timestamp: {
89
+ type: Date
90
+ }
91
+ }
92
+ },
93
+ accessControl: {
94
+ roles: contentWriterRoles
95
+ },
96
+ daoPath({ objectType, object }, { client, service }, method) {
97
+ const contentId = App.encodeIdentifier([objectType, object])
98
+ return ['database', 'queryObject', app.databaseName, `(${
99
+ async (input, output, { documentTableName, contentId }) => {
100
+ function mapper(obj) {
101
+ return obj && { content: obj.content, timestamp: obj.lastModified }
102
+ }
103
+ await input.table(documentTableName).object(contentId).onChange(async (obj, oldObj) => {
104
+ output.change(mapper(obj), mapper(oldObj))
105
+ })
106
+ }
107
+ })`, { documentTableName: Document.tableName, contentId }]
108
+ }
109
+ })
110
+
111
+ definition.action({
112
+ name: 'publish',
113
+ waitForEvents: true,
114
+ properties: {
115
+ objectType: {
116
+ type: String,
117
+ validation: ['nonEmpty']
118
+ },
119
+ object: {
120
+ type: String,
121
+ validation: ['nonEmpty']
122
+ },
123
+ version: {
124
+ type: Number
125
+ },
126
+ type: {
127
+ type: String
128
+ }
129
+ },
130
+ accessControl: {
131
+ roles: contentPublisherRoles
132
+ },
133
+ async execute({ objectType, object, type, version }, { client, service }, emit) {
134
+ const contentId = App.encodeIdentifier([objectType, object])
135
+ const [contentData, documentData] = await Promise.all([
136
+ Content.get(contentId),
137
+ Document.get(contentId)
138
+ ])
139
+ if(!documentData) throw new Error("Document not found")
140
+ if(version > documentData.version) throw new Error("Version not found")
141
+ await service.triggerService("prosemirror", {
142
+ type: 'takeSnapshot',
143
+ targetType: objectType,
144
+ target: object,
145
+ documentType: type || 'content',
146
+ version
147
+ })
148
+ const snapshotId = App.encodeIdentifier([contentId, version.toFixed().padStart(10, '0')])
149
+ if(contentData) {
150
+ emit({
151
+ type: 'ownerOwnedContentUpdated',
152
+ identifiers: {
153
+ ownerType: objectType, owner: object
154
+ },
155
+ data: {
156
+ snapshot: snapshotId
157
+ }
158
+ })
159
+ } else {
160
+ emit({
161
+ type: 'ownerOwnedContentSet',
162
+ identifiers: {
163
+ ownerType: objectType, owner: object
164
+ },
165
+ data: {
166
+ snapshot: snapshotId
167
+ }
168
+ })
169
+ }
170
+ return 'ok'
171
+ }
172
+ })
67
173
 
68
174
  module.exports = definition
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/content-service",
3
- "version": "0.2.52",
3
+ "version": "0.3.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -21,12 +21,12 @@
21
21
  "url": "https://www.viamage.com/"
22
22
  },
23
23
  "dependencies": {
24
- "@live-change/framework": "0.6.15",
25
- "@live-change/relations-plugin": "0.6.15",
24
+ "@live-change/framework": "0.7.2",
25
+ "@live-change/relations-plugin": "0.7.2",
26
26
  "lru-cache": "^7.12.0",
27
27
  "pluralize": "8.0.0",
28
28
  "progress-stream": "^2.0.0",
29
29
  "prosemirror-model": "^1.18.1"
30
30
  },
31
- "gitHead": "1b64dcc618e1686c98d5e14e128ee40e49cb5a84"
31
+ "gitHead": "9cb5f8b01ecbcf4d841274ae112872370ea9d927"
32
32
  }