@headless-lms/core 0.2.0 → 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 (225) hide show
  1. package/dist/assets/events.d.ts +39 -21
  2. package/dist/assets/events.js +1 -1
  3. package/dist/assets/events.js.map +1 -1
  4. package/dist/assets/file-urls.d.ts +20 -0
  5. package/dist/assets/file-urls.js +25 -0
  6. package/dist/assets/file-urls.js.map +1 -0
  7. package/dist/assets/index.d.ts +5 -4
  8. package/dist/assets/index.js +3 -2
  9. package/dist/assets/model.d.ts +2 -2
  10. package/dist/assets/ports.d.ts +45 -15
  11. package/dist/assets/service.d.ts +39 -7
  12. package/dist/assets/service.js +291 -38
  13. package/dist/assets/service.js.map +1 -1
  14. package/dist/automations/actions.d.ts +13 -24
  15. package/dist/automations/actions.js +168 -43
  16. package/dist/automations/actions.js.map +1 -1
  17. package/dist/automations/catalog.d.ts +2 -1
  18. package/dist/automations/catalog.js +229 -13
  19. package/dist/automations/catalog.js.map +1 -1
  20. package/dist/automations/community-derivations.d.ts +14 -0
  21. package/dist/automations/community-derivations.js +439 -0
  22. package/dist/automations/community-derivations.js.map +1 -0
  23. package/dist/automations/community-mail.d.ts +112 -0
  24. package/dist/automations/community-mail.js +90 -0
  25. package/dist/automations/community-mail.js.map +1 -0
  26. package/dist/automations/community-notifications.d.ts +16 -0
  27. package/dist/automations/community-notifications.js +84 -0
  28. package/dist/automations/community-notifications.js.map +1 -0
  29. package/dist/automations/events.d.ts +28 -0
  30. package/dist/automations/index.d.ts +4 -2
  31. package/dist/automations/index.js +3 -2
  32. package/dist/automations/model.d.ts +10 -3
  33. package/dist/automations/model.js +15 -2
  34. package/dist/automations/model.js.map +1 -1
  35. package/dist/automations/ports.d.ts +5 -1
  36. package/dist/automations/schedule.d.ts +6 -0
  37. package/dist/automations/schedule.js +41 -0
  38. package/dist/automations/schedule.js.map +1 -0
  39. package/dist/automations/seeds.d.ts +17 -0
  40. package/dist/automations/seeds.js +235 -0
  41. package/dist/automations/seeds.js.map +1 -0
  42. package/dist/automations/service.d.ts +22 -3
  43. package/dist/automations/service.js +306 -43
  44. package/dist/automations/service.js.map +1 -1
  45. package/dist/content/events.d.ts +631 -19
  46. package/dist/content/events.js +44 -1
  47. package/dist/content/events.js.map +1 -1
  48. package/dist/content/index.d.ts +4 -4
  49. package/dist/content/model.d.ts +2 -2
  50. package/dist/content/ports.d.ts +71 -18
  51. package/dist/content/service.d.ts +57 -18
  52. package/dist/content/service.js +502 -23
  53. package/dist/content/service.js.map +1 -1
  54. package/dist/content/types.d.ts +2 -2
  55. package/dist/discussion/authors.d.ts +10 -0
  56. package/dist/discussion/authors.js +23 -0
  57. package/dist/discussion/authors.js.map +1 -0
  58. package/dist/discussion/chats.d.ts +104 -0
  59. package/dist/discussion/chats.js +798 -0
  60. package/dist/discussion/chats.js.map +1 -0
  61. package/dist/discussion/events.d.ts +619 -56
  62. package/dist/discussion/events.js +106 -3
  63. package/dist/discussion/events.js.map +1 -1
  64. package/dist/discussion/index.d.ts +7 -3
  65. package/dist/discussion/index.js +4 -1
  66. package/dist/discussion/ports.d.ts +288 -27
  67. package/dist/discussion/ports.js +6 -1
  68. package/dist/discussion/ports.js.map +1 -0
  69. package/dist/discussion/posts.d.ts +103 -0
  70. package/dist/discussion/posts.js +723 -0
  71. package/dist/discussion/posts.js.map +1 -0
  72. package/dist/discussion/service.d.ts +32 -10
  73. package/dist/discussion/service.js +293 -78
  74. package/dist/discussion/service.js.map +1 -1
  75. package/dist/entitlements/events.d.ts +178 -11
  76. package/dist/entitlements/events.js +28 -1
  77. package/dist/entitlements/events.js.map +1 -1
  78. package/dist/entitlements/index.d.ts +5 -4
  79. package/dist/entitlements/index.js +2 -1
  80. package/dist/entitlements/model.d.ts +3 -2
  81. package/dist/entitlements/ports.d.ts +124 -11
  82. package/dist/entitlements/rules.d.ts +12 -0
  83. package/dist/entitlements/rules.js +17 -0
  84. package/dist/entitlements/rules.js.map +1 -0
  85. package/dist/entitlements/service.d.ts +54 -3
  86. package/dist/entitlements/service.js +472 -2
  87. package/dist/entitlements/service.js.map +1 -1
  88. package/dist/identity/access.d.ts +6 -0
  89. package/dist/identity/access.js +7 -0
  90. package/dist/identity/access.js.map +1 -0
  91. package/dist/identity/index.d.ts +6 -4
  92. package/dist/identity/index.js +3 -1
  93. package/dist/identity/model.d.ts +2 -1
  94. package/dist/identity/notifications.d.ts +27 -0
  95. package/dist/identity/notifications.js +101 -0
  96. package/dist/identity/notifications.js.map +1 -0
  97. package/dist/identity/ports.d.ts +73 -3
  98. package/dist/identity/service.d.ts +14 -3
  99. package/dist/identity/service.js +62 -5
  100. package/dist/identity/service.js.map +1 -1
  101. package/dist/integrations/model.js +1 -1
  102. package/dist/integrations/model.js.map +1 -1
  103. package/dist/integrations/service.js +2 -2
  104. package/dist/integrations/service.js.map +1 -1
  105. package/dist/organizations/access-scopes.d.ts +19 -0
  106. package/dist/organizations/access-scopes.js +89 -0
  107. package/dist/organizations/access-scopes.js.map +1 -0
  108. package/dist/organizations/events.d.ts +74 -32
  109. package/dist/organizations/events.js +6 -0
  110. package/dist/organizations/events.js.map +1 -1
  111. package/dist/organizations/index.d.ts +7 -6
  112. package/dist/organizations/index.js +4 -3
  113. package/dist/organizations/members.d.ts +8 -5
  114. package/dist/organizations/members.js.map +1 -1
  115. package/dist/organizations/ports.d.ts +14 -11
  116. package/dist/organizations/roles.d.ts +6 -10
  117. package/dist/organizations/roles.js +19 -7
  118. package/dist/organizations/roles.js.map +1 -1
  119. package/dist/organizations/service.d.ts +20 -11
  120. package/dist/organizations/service.js +157 -47
  121. package/dist/organizations/service.js.map +1 -1
  122. package/dist/organizations/types.d.ts +1 -10
  123. package/dist/progress/index.d.ts +2 -2
  124. package/dist/progress/ports.d.ts +6 -1
  125. package/dist/progress/service.d.ts +3 -1
  126. package/dist/progress/service.js +6 -2
  127. package/dist/progress/service.js.map +1 -1
  128. package/dist/reporting/admin/index.d.ts +4 -0
  129. package/dist/reporting/admin/index.js +2 -0
  130. package/dist/reporting/admin/model.d.ts +108 -0
  131. package/dist/reporting/admin/ports.d.ts +27 -0
  132. package/dist/reporting/admin/service.d.ts +22 -0
  133. package/dist/reporting/admin/service.js +38 -0
  134. package/dist/reporting/admin/service.js.map +1 -0
  135. package/dist/reporting/member/index.d.ts +5 -0
  136. package/dist/reporting/member/index.js +2 -0
  137. package/dist/reporting/member/model.d.ts +124 -0
  138. package/dist/reporting/member/ports.d.ts +45 -0
  139. package/dist/reporting/member/service.d.ts +73 -0
  140. package/dist/reporting/member/service.js +286 -0
  141. package/dist/reporting/member/service.js.map +1 -0
  142. package/dist/shared/actor.d.ts +9 -0
  143. package/dist/shared/errors.d.ts +7 -1
  144. package/dist/shared/errors.js +10 -1
  145. package/dist/shared/errors.js.map +1 -1
  146. package/dist/shared/id.d.ts +11 -0
  147. package/dist/shared/id.js +12 -1
  148. package/dist/shared/id.js.map +1 -1
  149. package/dist/shared/mailer.d.ts +4 -1
  150. package/dist/shared/mailer.js.map +1 -1
  151. package/dist/shared/network.d.ts +10 -0
  152. package/dist/shared/network.js +211 -0
  153. package/dist/shared/network.js.map +1 -0
  154. package/dist/types/assets.d.ts +2 -2
  155. package/dist/types/automations.d.ts +10 -2
  156. package/dist/types/content.d.ts +2 -2
  157. package/dist/types/discussion.d.ts +11 -3
  158. package/dist/types/email-templates.d.ts +121 -0
  159. package/dist/types/entitlements.d.ts +2 -2
  160. package/dist/types/identity.d.ts +48 -2
  161. package/dist/types/index.d.ts +9 -9
  162. package/dist/types/ports.d.ts +6 -0
  163. package/dist/types/schemas/assets.d.ts +65 -37
  164. package/dist/types/schemas/assets.js +17 -12
  165. package/dist/types/schemas/assets.js.map +1 -1
  166. package/dist/types/schemas/automations.d.ts +18 -1
  167. package/dist/types/schemas/automations.js +15 -6
  168. package/dist/types/schemas/automations.js.map +1 -1
  169. package/dist/types/schemas/content.d.ts +335 -3
  170. package/dist/types/schemas/content.js +139 -3
  171. package/dist/types/schemas/content.js.map +1 -1
  172. package/dist/types/schemas/discussion.d.ts +370 -3
  173. package/dist/types/schemas/discussion.js +199 -4
  174. package/dist/types/schemas/discussion.js.map +1 -1
  175. package/dist/types/schemas/entitlements.d.ts +36 -2
  176. package/dist/types/schemas/entitlements.js +23 -1
  177. package/dist/types/schemas/entitlements.js.map +1 -1
  178. package/dist/types/schemas/identity.d.ts +111 -1
  179. package/dist/types/schemas/identity.js +55 -2
  180. package/dist/types/schemas/identity.js.map +1 -1
  181. package/dist/types/schemas/index.d.ts +8 -8
  182. package/dist/types/schemas/index.js +8 -8
  183. package/dist/types/schemas/organizations.d.ts +8 -0
  184. package/dist/types/schemas/organizations.js +2 -0
  185. package/dist/types/schemas/organizations.js.map +1 -1
  186. package/dist/types/schemas/shared.d.ts +3 -1
  187. package/dist/types/schemas/shared.js +5 -1
  188. package/dist/types/schemas/shared.js.map +1 -1
  189. package/package.json +2 -2
  190. package/dist/reporting/courses/index.d.ts +0 -4
  191. package/dist/reporting/courses/index.js +0 -2
  192. package/dist/reporting/courses/model.d.ts +0 -24
  193. package/dist/reporting/courses/ports.d.ts +0 -13
  194. package/dist/reporting/courses/service.d.ts +0 -19
  195. package/dist/reporting/courses/service.js +0 -20
  196. package/dist/reporting/courses/service.js.map +0 -1
  197. package/dist/reporting/dashboard/index.d.ts +0 -4
  198. package/dist/reporting/dashboard/index.js +0 -2
  199. package/dist/reporting/dashboard/model.d.ts +0 -14
  200. package/dist/reporting/dashboard/ports.d.ts +0 -13
  201. package/dist/reporting/dashboard/service.d.ts +0 -19
  202. package/dist/reporting/dashboard/service.js +0 -20
  203. package/dist/reporting/dashboard/service.js.map +0 -1
  204. package/dist/reporting/learn/index.d.ts +0 -5
  205. package/dist/reporting/learn/index.js +0 -2
  206. package/dist/reporting/learn/model.d.ts +0 -23
  207. package/dist/reporting/learn/ports.d.ts +0 -43
  208. package/dist/reporting/learn/ports.js +0 -1
  209. package/dist/reporting/learn/service.d.ts +0 -49
  210. package/dist/reporting/learn/service.js +0 -116
  211. package/dist/reporting/learn/service.js.map +0 -1
  212. package/dist/reporting/students/index.d.ts +0 -4
  213. package/dist/reporting/students/index.js +0 -2
  214. package/dist/reporting/students/model.d.ts +0 -51
  215. package/dist/reporting/students/model.js +0 -1
  216. package/dist/reporting/students/ports.d.ts +0 -15
  217. package/dist/reporting/students/ports.js +0 -1
  218. package/dist/reporting/students/service.d.ts +0 -20
  219. package/dist/reporting/students/service.js +0 -23
  220. package/dist/reporting/students/service.js.map +0 -1
  221. /package/dist/reporting/{courses → admin}/model.js +0 -0
  222. /package/dist/reporting/{courses → admin}/ports.js +0 -0
  223. /package/dist/reporting/{dashboard → member}/model.js +0 -0
  224. /package/dist/reporting/{dashboard → member}/ports.js +0 -0
  225. /package/dist/{reporting/learn/model.js → shared/actor.js} +0 -0
@@ -0,0 +1,723 @@
1
+ import { genId } from "../shared/id.js";
2
+ import { ConflictError, ForbiddenError, InvalidInputError, NotFoundError } from "../shared/errors.js";
3
+ import { noopLogger } from "../shared/logger.js";
4
+ import { canModerate, isStaffRole } from "../organizations/roles.js";
5
+ import { discussionEvents } from "./events.js";
6
+ import { authorView } from "./authors.js";
7
+ import "./ports.js";
8
+ //#region src/discussion/posts.ts
9
+ const PUBLISHED_ONLY = ["published"];
10
+ const DUE_BATCH_SIZE = 100;
11
+ const MODERATOR_VISIBLE = [
12
+ "published",
13
+ "pending",
14
+ "removed"
15
+ ];
16
+ var PostServiceImpl = class {
17
+ repo;
18
+ access;
19
+ content;
20
+ authors;
21
+ profiles;
22
+ comments;
23
+ images;
24
+ fileUrls;
25
+ uow;
26
+ logger;
27
+ constructor(params) {
28
+ this.repo = params.repo;
29
+ this.access = params.access;
30
+ this.content = params.content;
31
+ this.authors = params.authors;
32
+ this.profiles = params.profiles;
33
+ this.comments = params.comments;
34
+ this.images = params.images;
35
+ this.fileUrls = params.fileUrls;
36
+ this.uow = params.uow;
37
+ this.logger = params.logger ?? noopLogger;
38
+ }
39
+ async visibleTitles(orgId, actor, postIds) {
40
+ const found = await this.repo.findMany(orgId, [...new Set(postIds)]);
41
+ const communityIds = [...new Set(found.map((post) => post.communityId))];
42
+ const open = new Set((await Promise.all(communityIds.map((communityId) => this.access.openSpaceIds(orgId, actor, communityId)))).flat());
43
+ return Object.fromEntries(found.filter((post) => open.has(post.spaceId) && this.canSee(actor, post)).map((post) => [post.id, post.title]));
44
+ }
45
+ async create(orgId, actor, input) {
46
+ const space = await this.requireOpen(orgId, actor, input.spaceId, input.communityId);
47
+ await this.requireOwnedImage(orgId, actor.id, input.coverAssetId);
48
+ this.assertTopic(space, input.topicId);
49
+ const now = /* @__PURE__ */ new Date();
50
+ const landing = this.initial(input, space, actor, now);
51
+ const post = {
52
+ orgId,
53
+ id: genId("post"),
54
+ communityId: space.communityId,
55
+ spaceId: space.id,
56
+ orgUserId: actor.id,
57
+ topicId: input.topicId,
58
+ title: input.title,
59
+ body: input.body,
60
+ extra: input.extra,
61
+ coverAssetId: input.coverAssetId,
62
+ pinned: false,
63
+ commentsClosed: null,
64
+ reactionsEnabled: null,
65
+ createdAt: now,
66
+ updatedAt: now,
67
+ ...landing
68
+ };
69
+ const saved = await this.uow.run(async ({ posts, outbox }) => {
70
+ const created = await posts.insert(orgId, post);
71
+ const events = [discussionEvents.postCreated.make({
72
+ orgId,
73
+ data: created,
74
+ metadata: this.meta(actor, created.status === "pending" ? null : void 0)
75
+ })];
76
+ if (created.status === "published") events.push(discussionEvents.postPublished.make({
77
+ orgId,
78
+ data: created,
79
+ metadata: this.meta(actor, null)
80
+ }));
81
+ await outbox.append(events);
82
+ return created;
83
+ });
84
+ this.logger.info("post created", {
85
+ orgId,
86
+ postId: saved.id,
87
+ status: saved.status,
88
+ actorId: actor.id
89
+ });
90
+ return this.viewOf(orgId, actor, saved);
91
+ }
92
+ async update(orgId, postId, actor, patch) {
93
+ const { post, space } = await this.loadVisible(orgId, actor, postId);
94
+ this.assertAuthor(actor, post);
95
+ if (post.status === "removed") throw new ConflictError("This post was removed");
96
+ if (patch.topicId !== void 0) this.assertTopic(space, patch.topicId);
97
+ if (typeof patch.coverAssetId === "string") await this.requireOwnedImage(orgId, actor.id, patch.coverAssetId);
98
+ if (!this.patchChanges(post, patch)) return this.viewOf(orgId, actor, post);
99
+ const updated = await this.uow.run(async ({ posts, outbox }) => {
100
+ const next = await this.mustUpdate(posts, orgId, postId, patch);
101
+ await outbox.append([discussionEvents.postUpdated.make({
102
+ orgId,
103
+ data: next,
104
+ metadata: this.meta(actor, next.status === "pending" ? post.status : void 0)
105
+ })]);
106
+ return next;
107
+ });
108
+ this.logger.info("post updated", {
109
+ orgId,
110
+ postId,
111
+ fields: Object.keys(patch),
112
+ actorId: actor.id
113
+ });
114
+ return this.viewOf(orgId, actor, updated);
115
+ }
116
+ async submit(orgId, postId, actor) {
117
+ const { post, space } = await this.loadVisible(orgId, actor, postId);
118
+ this.assertAuthor(actor, post);
119
+ if (post.status !== "draft" && post.status !== "scheduled") throw new ConflictError("This post cannot be submitted");
120
+ this.assertTopic(space, post.topicId);
121
+ const saved = await this.commitSubmission(orgId, post, space, actor.role, actor, /* @__PURE__ */ new Date());
122
+ this.logger.info("post submitted", {
123
+ orgId,
124
+ postId,
125
+ status: saved.status,
126
+ actorId: actor.id
127
+ });
128
+ return this.viewOf(orgId, actor, saved);
129
+ }
130
+ async schedule(orgId, postId, actor, scheduledAt) {
131
+ const { post } = await this.loadVisible(orgId, actor, postId);
132
+ this.assertAuthor(actor, post);
133
+ if (post.status === "published" || post.status === "removed" || post.status === "pending") throw new ConflictError("This post cannot be scheduled");
134
+ if (scheduledAt <= /* @__PURE__ */ new Date()) throw new InvalidInputError("A scheduled time must be in the future");
135
+ const updated = await this.uow.run(async ({ posts, outbox }) => {
136
+ const next = await this.mustUpdate(posts, orgId, postId, {
137
+ status: "scheduled",
138
+ scheduledAt,
139
+ publishedAt: null,
140
+ removedBy: null
141
+ });
142
+ await outbox.append([discussionEvents.postUpdated.make({
143
+ orgId,
144
+ data: next,
145
+ metadata: this.meta(actor, post.status)
146
+ })]);
147
+ return next;
148
+ });
149
+ this.logger.info("post scheduled", {
150
+ orgId,
151
+ postId,
152
+ scheduledAt: scheduledAt.toISOString(),
153
+ actorId: actor.id
154
+ });
155
+ return this.viewOf(orgId, actor, updated);
156
+ }
157
+ async approve(orgId, postId, actor, communityId) {
158
+ const { post } = await this.loadVisible(orgId, actor, postId, communityId);
159
+ if (!this.canModerate(actor)) throw new ForbiddenError("You cannot review this post");
160
+ if (post.status !== "pending") throw new ConflictError("This post is not awaiting review");
161
+ const now = /* @__PURE__ */ new Date();
162
+ const updated = await this.uow.run(async ({ posts, outbox }) => {
163
+ const next = await this.mustUpdate(posts, orgId, postId, {
164
+ status: "published",
165
+ publishedAt: now,
166
+ scheduledAt: null,
167
+ removedBy: null
168
+ });
169
+ await outbox.append([discussionEvents.postPublished.make({
170
+ orgId,
171
+ data: next,
172
+ metadata: this.meta(actor, post.status)
173
+ })]);
174
+ return next;
175
+ });
176
+ this.logger.info("post approved", {
177
+ orgId,
178
+ postId,
179
+ actorId: actor.id
180
+ });
181
+ return this.viewOf(orgId, actor, updated);
182
+ }
183
+ async remove(orgId, postId, actor, communityId) {
184
+ const { post } = await this.loadVisible(orgId, actor, postId, communityId);
185
+ if (post.orgUserId !== actor.id && !this.canModerate(actor)) throw new ForbiddenError("You cannot remove this post");
186
+ if (post.status === "removed") throw new ConflictError("This post was removed");
187
+ if (post.status !== "published" && post.status !== "pending") throw new ConflictError("This post cannot be removed");
188
+ const updated = await this.uow.run(async ({ posts, outbox }) => {
189
+ const next = await this.mustUpdate(posts, orgId, postId, {
190
+ status: "removed",
191
+ removedBy: actor.id
192
+ });
193
+ if (post.orgUserId !== actor.id) await posts.resolveFlags(orgId, postId, {
194
+ resolvedAt: /* @__PURE__ */ new Date(),
195
+ resolvedBy: actor.id,
196
+ resolution: "removed"
197
+ });
198
+ await outbox.append([discussionEvents.postRemoved.make({
199
+ orgId,
200
+ data: next,
201
+ metadata: this.meta(actor, post.status)
202
+ })]);
203
+ return next;
204
+ });
205
+ this.logger.info("post removed", {
206
+ orgId,
207
+ postId,
208
+ actorId: actor.id
209
+ });
210
+ return this.viewOf(orgId, actor, updated);
211
+ }
212
+ async restore(orgId, postId, actor, communityId) {
213
+ const { post, space } = await this.loadVisible(orgId, actor, postId, communityId);
214
+ if (!this.canModerate(actor)) throw new ForbiddenError("You cannot moderate this post");
215
+ if (post.status !== "removed") throw new ConflictError("This post was not removed");
216
+ const now = /* @__PURE__ */ new Date();
217
+ const role = await this.authors.roleOf(orgId, post.orgUserId);
218
+ const updated = await this.uow.run(async ({ posts, outbox }) => {
219
+ const next = await this.mustUpdate(posts, orgId, postId, this.submissionOf(space.settings.requireReview, role, now));
220
+ const metadata = this.meta(actor, post.status);
221
+ const event = next.status === "published" ? discussionEvents.postPublished.make({
222
+ orgId,
223
+ data: next,
224
+ metadata
225
+ }) : discussionEvents.postUpdated.make({
226
+ orgId,
227
+ data: next,
228
+ metadata
229
+ });
230
+ await outbox.append([event]);
231
+ return next;
232
+ });
233
+ this.logger.info("post restored", {
234
+ orgId,
235
+ postId,
236
+ status: updated.status,
237
+ actorId: actor.id
238
+ });
239
+ return this.viewOf(orgId, actor, updated);
240
+ }
241
+ pin(orgId, postId, actor) {
242
+ return this.moderate(orgId, postId, actor, { pinned: true }, (post) => post.pinned, "post pinned");
243
+ }
244
+ unpin(orgId, postId, actor) {
245
+ return this.moderate(orgId, postId, actor, { pinned: false }, (post) => !post.pinned, "post unpinned");
246
+ }
247
+ closeComments(orgId, postId, actor) {
248
+ return this.moderate(orgId, postId, actor, { commentsClosed: true }, (post) => post.commentsClosed === true, "post comments closed");
249
+ }
250
+ openComments(orgId, postId, actor) {
251
+ return this.moderate(orgId, postId, actor, { commentsClosed: false }, (post) => post.commentsClosed === false, "post comments opened");
252
+ }
253
+ setReactions(orgId, postId, actor, enabled) {
254
+ return this.moderate(orgId, postId, actor, { reactionsEnabled: enabled }, (post) => post.reactionsEnabled === enabled, "post reactions changed");
255
+ }
256
+ async setReaction(orgId, postId, actor, emoji) {
257
+ const { post, space } = await this.loadVisible(orgId, actor, postId);
258
+ if (!(post.reactionsEnabled ?? space.settings.reactions)) throw new ForbiddenError("Reactions are off on this post");
259
+ await this.repo.setReaction(orgId, postId, actor.id, emoji);
260
+ this.logger.debug("post reaction set", {
261
+ orgId,
262
+ postId,
263
+ orgUserId: actor.id
264
+ });
265
+ return (await this.repo.reactionsOf(orgId, [postId], actor.id))[postId] ?? { reactions: {} };
266
+ }
267
+ async follow(orgId, postId, actor) {
268
+ await this.loadVisible(orgId, actor, postId);
269
+ await this.repo.follow(orgId, postId, actor.id);
270
+ this.logger.debug("post followed", {
271
+ orgId,
272
+ postId,
273
+ orgUserId: actor.id
274
+ });
275
+ }
276
+ async unfollow(orgId, postId, actor) {
277
+ await this.loadVisible(orgId, actor, postId);
278
+ await this.repo.unfollow(orgId, postId, actor.id);
279
+ this.logger.debug("post unfollowed", {
280
+ orgId,
281
+ postId,
282
+ orgUserId: actor.id
283
+ });
284
+ }
285
+ async bookmark(orgId, postId, actor) {
286
+ await this.loadVisible(orgId, actor, postId);
287
+ await this.repo.bookmark(orgId, postId, actor.id);
288
+ this.logger.debug("post bookmarked", {
289
+ orgId,
290
+ postId,
291
+ orgUserId: actor.id
292
+ });
293
+ }
294
+ async unbookmark(orgId, postId, actor) {
295
+ await this.loadVisible(orgId, actor, postId);
296
+ await this.repo.unbookmark(orgId, postId, actor.id);
297
+ this.logger.debug("post unbookmarked", {
298
+ orgId,
299
+ postId,
300
+ orgUserId: actor.id
301
+ });
302
+ }
303
+ async flag(orgId, postId, actor, reason) {
304
+ const { post } = await this.loadVisible(orgId, actor, postId);
305
+ const now = /* @__PURE__ */ new Date();
306
+ const flag = {
307
+ orgId,
308
+ id: genId("postFlag"),
309
+ postId,
310
+ orgUserId: actor.id,
311
+ reason,
312
+ resolvedAt: null,
313
+ resolution: null,
314
+ resolvedBy: null,
315
+ createdAt: now,
316
+ updatedAt: now
317
+ };
318
+ const saved = await this.uow.run(async ({ posts, outbox }) => {
319
+ const created = await posts.insertFlag(orgId, flag);
320
+ if (!created) {
321
+ const existing = await posts.findFlag(orgId, postId, actor.id);
322
+ if (!existing) throw new NotFoundError("Flag", postId);
323
+ return existing;
324
+ }
325
+ await outbox.append([discussionEvents.postReported.make({
326
+ orgId,
327
+ data: {
328
+ ...created,
329
+ communityId: post.communityId
330
+ },
331
+ metadata: this.meta(actor)
332
+ })]);
333
+ return created;
334
+ });
335
+ this.logger.info("post flagged", {
336
+ orgId,
337
+ postId,
338
+ flagId: saved.id,
339
+ actorId: actor.id
340
+ });
341
+ return saved;
342
+ }
343
+ async resolveFlags(orgId, postId, actor, resolution, communityId) {
344
+ await this.loadVisible(orgId, actor, postId, communityId);
345
+ if (!this.canModerate(actor)) throw new ForbiddenError("You cannot work this queue");
346
+ await this.repo.resolveFlags(orgId, postId, {
347
+ resolvedAt: /* @__PURE__ */ new Date(),
348
+ resolvedBy: actor.id,
349
+ resolution
350
+ });
351
+ this.logger.info("post flags resolved", {
352
+ orgId,
353
+ postId,
354
+ resolution,
355
+ actorId: actor.id
356
+ });
357
+ }
358
+ async spaceFeed(orgId, actor, communityId, spaceId, query) {
359
+ const space = await this.requireOpen(orgId, actor, spaceId, communityId);
360
+ if (query.topicId && !space.topics.some((topic) => topic.id === query.topicId)) throw new NotFoundError("Topic", query.topicId);
361
+ const page = await this.feedPage(orgId, actor, [spaceId], query);
362
+ this.logger.debug("space feed read", {
363
+ orgId,
364
+ spaceId,
365
+ rows: page.rows.length
366
+ });
367
+ return page;
368
+ }
369
+ async memberFeed(orgId, actor, communityId, query) {
370
+ const spaceIds = await this.openSpaceIdsIn(orgId, actor, communityId);
371
+ const page = await this.feedPage(orgId, actor, spaceIds, query);
372
+ this.logger.debug("member feed read", {
373
+ orgId,
374
+ communityId,
375
+ spaces: spaceIds.length,
376
+ rows: page.rows.length
377
+ });
378
+ return page;
379
+ }
380
+ async openSpaceIdsIn(orgId, actor, communityId) {
381
+ if (!await this.access.isCommunityMember(orgId, actor, communityId)) throw new NotFoundError("Community", communityId);
382
+ return this.access.openSpaceIds(orgId, actor, communityId);
383
+ }
384
+ async listOwn(orgId, actor, communityId, query) {
385
+ const spaceIds = await this.openSpaceIdsIn(orgId, actor, communityId);
386
+ const page = await this.repo.listByAuthor(orgId, actor.id, spaceIds, query);
387
+ this.logger.debug("own posts read", {
388
+ orgId,
389
+ communityId,
390
+ rows: page.rows.length
391
+ });
392
+ return {
393
+ ...page,
394
+ rows: await this.viewsOf(orgId, actor, page.rows)
395
+ };
396
+ }
397
+ async listBookmarked(orgId, actor, communityId, query) {
398
+ const spaceIds = await this.openSpaceIdsIn(orgId, actor, communityId);
399
+ const page = await this.repo.listBookmarked(orgId, actor.id, spaceIds, {
400
+ ...query,
401
+ visibleStatuses: this.visibleStatuses(actor)
402
+ });
403
+ this.logger.debug("bookmarked posts read", {
404
+ orgId,
405
+ communityId,
406
+ rows: page.rows.length
407
+ });
408
+ return {
409
+ ...page,
410
+ rows: await this.viewsOf(orgId, actor, page.rows)
411
+ };
412
+ }
413
+ async coverFile(orgId, actor, postId) {
414
+ const post = await this.repo.find(orgId, postId);
415
+ if (!post?.coverAssetId || !this.canSee(actor, post)) return null;
416
+ const space = await this.content.getSpace(orgId, post.spaceId);
417
+ if (!space || !await this.access.canOpen(orgId, actor, space)) return null;
418
+ return this.images.signFile(orgId, post.coverAssetId);
419
+ }
420
+ async getPost(orgId, actor, postId) {
421
+ const { post } = await this.loadVisible(orgId, actor, postId);
422
+ this.logger.debug("post read", {
423
+ orgId,
424
+ postId
425
+ });
426
+ return this.viewOf(orgId, actor, post);
427
+ }
428
+ async moderationQueue(orgId, actor, communityId, spaceId) {
429
+ await this.requireOpen(orgId, actor, spaceId, communityId);
430
+ if (!this.canModerate(actor)) throw new ForbiddenError("You cannot work this queue");
431
+ const [pending, flags, openCommentFlags] = await Promise.all([
432
+ this.repo.listPending(orgId, spaceId, 100),
433
+ this.repo.listOpenFlags(orgId, spaceId, 100),
434
+ this.comments.listOpenFlagsForSpace(orgId, spaceId, 100)
435
+ ]);
436
+ this.logger.debug("space queue read", {
437
+ orgId,
438
+ spaceId,
439
+ pending: pending.length,
440
+ flags: flags.length,
441
+ commentFlags: openCommentFlags.length
442
+ });
443
+ return {
444
+ pending: await this.viewsOf(orgId, actor, pending),
445
+ flags,
446
+ commentFlags: openCommentFlags
447
+ };
448
+ }
449
+ visible(orgId, actor, postId) {
450
+ return this.loadVisible(orgId, actor, postId);
451
+ }
452
+ find(orgId, postId) {
453
+ return this.repo.find(orgId, postId);
454
+ }
455
+ followerIds(orgId, postId) {
456
+ return this.repo.followerIds(orgId, postId);
457
+ }
458
+ hasDue(orgId, now) {
459
+ return this.repo.hasDue(orgId, now);
460
+ }
461
+ async publishDue(orgId, now) {
462
+ let afterId;
463
+ let count = 0;
464
+ let refused = 0;
465
+ for (;;) {
466
+ const due = await this.repo.listDue(orgId, now, DUE_BATCH_SIZE, afterId);
467
+ if (due.length === 0) break;
468
+ const batch = await this.submitDue(orgId, now, due);
469
+ count += batch.count;
470
+ refused += batch.refused;
471
+ if (due.length < DUE_BATCH_SIZE) break;
472
+ afterId = due[due.length - 1].id;
473
+ }
474
+ if (count > 0) this.logger.info("due posts submitted", {
475
+ orgId,
476
+ count,
477
+ refused
478
+ });
479
+ else this.logger.debug("no posts due", { orgId });
480
+ }
481
+ async submitDue(orgId, now, due) {
482
+ const spaces = /* @__PURE__ */ new Map();
483
+ const roles = /* @__PURE__ */ new Map();
484
+ const refused = /* @__PURE__ */ new Set();
485
+ for (const post of due) {
486
+ if (!spaces.has(post.spaceId)) spaces.set(post.spaceId, await this.content.getSpace(orgId, post.spaceId));
487
+ if (!roles.has(post.orgUserId)) roles.set(post.orgUserId, await this.authors.roleOf(orgId, post.orgUserId));
488
+ const space = spaces.get(post.spaceId);
489
+ const role = roles.get(post.orgUserId) ?? null;
490
+ if (space && !await this.submittable(orgId, post, space, role)) refused.add(post.id);
491
+ }
492
+ return {
493
+ count: await this.uow.run(async ({ posts, outbox }) => {
494
+ const events = [];
495
+ for (const post of due) {
496
+ const current = await posts.find(orgId, post.id);
497
+ if (!current || current.status !== "scheduled" || current.scheduledAt === null || current.scheduledAt > now) continue;
498
+ const space = spaces.get(current.spaceId);
499
+ if (!space) {
500
+ this.logger.error("scheduled post has no space", {
501
+ orgId,
502
+ postId: current.id,
503
+ spaceId: current.spaceId
504
+ });
505
+ continue;
506
+ }
507
+ const next = await posts.update(orgId, current.id, refused.has(current.id) ? {
508
+ status: "draft",
509
+ scheduledAt: null,
510
+ publishedAt: null,
511
+ removedBy: null
512
+ } : this.submissionOf(space.settings.requireReview, roles.get(current.orgUserId) ?? null, now));
513
+ if (!next) continue;
514
+ const metadata = this.meta(null, current.status);
515
+ events.push(next.status === "published" ? discussionEvents.postPublished.make({
516
+ orgId,
517
+ data: next,
518
+ metadata
519
+ }) : discussionEvents.postUpdated.make({
520
+ orgId,
521
+ data: next,
522
+ metadata
523
+ }));
524
+ }
525
+ if (events.length > 0) await outbox.append(events);
526
+ return events.length;
527
+ }),
528
+ refused: refused.size
529
+ };
530
+ }
531
+ async submittable(orgId, post, space, role) {
532
+ if (role === null || !this.topicFits(space, post.topicId)) return false;
533
+ return this.access.canOpen(orgId, {
534
+ id: post.orgUserId,
535
+ role
536
+ }, space);
537
+ }
538
+ async feedPage(orgId, actor, spaceIds, query) {
539
+ const page = await this.repo.listPublished(orgId, spaceIds, query);
540
+ return {
541
+ ...page,
542
+ rows: await this.viewsOf(orgId, actor, page.rows)
543
+ };
544
+ }
545
+ async viewOf(orgId, actor, post) {
546
+ const [view] = await this.viewsOf(orgId, actor, [post]);
547
+ return view;
548
+ }
549
+ async viewsOf(orgId, actor, posts) {
550
+ if (posts.length === 0) return [];
551
+ const postIds = posts.map((post) => post.id);
552
+ const [records, reactions, commentCounts, followed, bookmarked] = await Promise.all([
553
+ this.profiles.authorsOf(orgId, [...new Set(posts.map((post) => post.orgUserId))]),
554
+ this.repo.reactionsOf(orgId, postIds, actor.id),
555
+ this.comments.publishedCommentCounts(orgId, postIds),
556
+ this.repo.followedAmong(orgId, postIds, actor.id),
557
+ this.repo.bookmarkedAmong(orgId, postIds, actor.id)
558
+ ]);
559
+ const following = new Set(followed);
560
+ const saved = new Set(bookmarked);
561
+ return posts.map((post) => {
562
+ const record = records[post.orgUserId];
563
+ if (!record) throw new NotFoundError("Member", post.orgUserId);
564
+ const counts = reactions[post.id];
565
+ return {
566
+ ...post,
567
+ coverUrl: post.coverAssetId ? this.fileUrls.postCover(post.id) : null,
568
+ author: authorView(orgId, record, this.fileUrls),
569
+ reactions: counts?.reactions ?? {},
570
+ viewerReaction: counts?.viewerReaction ?? null,
571
+ commentCount: commentCounts[post.id] ?? 0,
572
+ following: following.has(post.id),
573
+ bookmarked: saved.has(post.id)
574
+ };
575
+ });
576
+ }
577
+ async commitSubmission(orgId, post, space, role, actor, now) {
578
+ return this.uow.run(async ({ posts, outbox }) => {
579
+ const next = await this.mustUpdate(posts, orgId, post.id, this.submissionOf(space.settings.requireReview, role, now));
580
+ const metadata = this.meta(actor, post.status);
581
+ const event = next.status === "published" ? discussionEvents.postPublished.make({
582
+ orgId,
583
+ data: next,
584
+ metadata
585
+ }) : discussionEvents.postUpdated.make({
586
+ orgId,
587
+ data: next,
588
+ metadata
589
+ });
590
+ await outbox.append([event]);
591
+ return next;
592
+ });
593
+ }
594
+ async moderate(orgId, postId, actor, patch, unchanged, log) {
595
+ const { post } = await this.loadVisible(orgId, actor, postId);
596
+ if (!this.canModerate(actor)) throw new ForbiddenError("You cannot moderate this post");
597
+ if (post.status === "removed") throw new ConflictError("This post was removed");
598
+ if (unchanged(post)) return this.viewOf(orgId, actor, post);
599
+ const updated = await this.uow.run(async ({ posts, outbox }) => {
600
+ const next = await this.mustUpdate(posts, orgId, postId, patch);
601
+ await outbox.append([discussionEvents.postUpdated.make({
602
+ orgId,
603
+ data: next,
604
+ metadata: this.meta(actor, next.status === "pending" ? post.status : void 0)
605
+ })]);
606
+ return next;
607
+ });
608
+ this.logger.info(log, {
609
+ orgId,
610
+ postId,
611
+ actorId: actor.id
612
+ });
613
+ return this.viewOf(orgId, actor, updated);
614
+ }
615
+ async mustUpdate(posts, orgId, postId, patch) {
616
+ const next = await posts.update(orgId, postId, patch);
617
+ if (!next) throw new NotFoundError("Post", postId);
618
+ return next;
619
+ }
620
+ initial(input, space, actor, now) {
621
+ if (input.mode === "draft") return {
622
+ status: "draft",
623
+ scheduledAt: null,
624
+ publishedAt: null,
625
+ removedBy: null
626
+ };
627
+ if (input.mode === "schedule") {
628
+ if (!input.scheduledAt) throw new InvalidInputError("A scheduled post needs a time");
629
+ if (input.scheduledAt <= now) throw new InvalidInputError("A scheduled time must be in the future");
630
+ return {
631
+ status: "scheduled",
632
+ scheduledAt: input.scheduledAt,
633
+ publishedAt: null,
634
+ removedBy: null
635
+ };
636
+ }
637
+ return this.submissionOf(space.settings.requireReview, actor.role, now);
638
+ }
639
+ submissionOf(requireReview, role, now) {
640
+ if (requireReview && !(role && isStaffRole(role))) return {
641
+ status: "pending",
642
+ scheduledAt: null,
643
+ publishedAt: null,
644
+ removedBy: null
645
+ };
646
+ return {
647
+ status: "published",
648
+ scheduledAt: null,
649
+ publishedAt: now,
650
+ removedBy: null
651
+ };
652
+ }
653
+ async requireOwnedImage(orgId, orgUserId, assetId) {
654
+ if (assetId === null) return;
655
+ if (!await this.images.ownsImage(orgId, assetId, orgUserId)) {
656
+ this.logger.debug("cover refused", {
657
+ orgId,
658
+ orgUserId,
659
+ assetId
660
+ });
661
+ throw new NotFoundError("Asset", assetId);
662
+ }
663
+ }
664
+ async openSpace(orgId, actor, spaceId, communityId) {
665
+ const space = await this.content.getSpace(orgId, spaceId);
666
+ if (!space || communityId !== void 0 && space.communityId !== communityId) return null;
667
+ return await this.access.canOpen(orgId, actor, space) ? space : null;
668
+ }
669
+ async requireOpen(orgId, actor, spaceId, communityId) {
670
+ const space = await this.openSpace(orgId, actor, spaceId, communityId);
671
+ if (!space) throw new NotFoundError("Space", spaceId);
672
+ return space;
673
+ }
674
+ async loadVisible(orgId, actor, postId, communityId) {
675
+ const post = await this.repo.find(orgId, postId);
676
+ if (!post || communityId !== void 0 && post.communityId !== communityId) throw new NotFoundError("Post", postId);
677
+ const space = await this.openSpace(orgId, actor, post.spaceId);
678
+ if (!space || !this.canSee(actor, post)) throw new NotFoundError("Post", postId);
679
+ return {
680
+ post,
681
+ space
682
+ };
683
+ }
684
+ canSee(actor, post) {
685
+ return post.orgUserId === actor.id || this.visibleStatuses(actor).includes(post.status);
686
+ }
687
+ visibleStatuses(actor) {
688
+ return this.canModerate(actor) ? MODERATOR_VISIBLE : PUBLISHED_ONLY;
689
+ }
690
+ canModerate(actor) {
691
+ return canModerate(actor.role);
692
+ }
693
+ assertAuthor(actor, post) {
694
+ if (post.orgUserId !== actor.id) throw new ForbiddenError("You can change only your own post");
695
+ }
696
+ topicFits(space, topicId) {
697
+ if (topicId === null) return space.topics.length === 0;
698
+ return space.topics.some((topic) => topic.id === topicId);
699
+ }
700
+ assertTopic(space, topicId) {
701
+ if (this.topicFits(space, topicId)) return;
702
+ if (topicId === null) throw new InvalidInputError("This space requires a topic");
703
+ throw new NotFoundError("Topic", topicId);
704
+ }
705
+ patchChanges(post, patch) {
706
+ return Object.keys(patch).some((key) => {
707
+ const value = patch[key];
708
+ if (value === void 0) return false;
709
+ if (key === "extra") return JSON.stringify(post.extra) !== JSON.stringify(value);
710
+ return post[key] !== value;
711
+ });
712
+ }
713
+ meta(actor, previousStatus) {
714
+ return {
715
+ ...actor ? { actorOrgUserId: actor.id } : {},
716
+ ...previousStatus !== void 0 ? { previousStatus } : {}
717
+ };
718
+ }
719
+ };
720
+ //#endregion
721
+ export { PostServiceImpl };
722
+
723
+ //# sourceMappingURL=posts.js.map