({ content, chatId, teamId, userId, id, files, parentMessageId })
| 179 | |
| 180 | // done: add parentMessageId |
| 181 | public static async addOrEdit({ content, chatId, teamId, userId, id, files, parentMessageId }) { |
| 182 | if (!content || !teamId) { |
| 183 | throw new Error('Bad data'); |
| 184 | } |
| 185 | |
| 186 | const { isSubscriptionActiveForAccount, isTrialPeriodOverForAccount } = |
| 187 | await User.getSubscriptionStatus(userId); |
| 188 | |
| 189 | if (isTrialPeriodOverForAccount && !isSubscriptionActiveForAccount) { |
| 190 | throw new Error('This team is not subscribed to a paid plan and free trial period is over.'); |
| 191 | } |
| 192 | |
| 193 | await Chat.updateOne({ _id: chatId }, { lastUpdatedAt: new Date() }); |
| 194 | |
| 195 | if (id) { |
| 196 | const editedMessage = await this.edit({ |
| 197 | content, |
| 198 | chatId, |
| 199 | userId, |
| 200 | id, |
| 201 | }); |
| 202 | |
| 203 | // no realtime update for editing message |
| 204 | return { message: editedMessage, userIdsToNotify: [] }; |
| 205 | } else { |
| 206 | const { chat } = await this.checkPermission({ userId, chatId }); |
| 207 | |
| 208 | const htmlContent = markdownToHtml(content); |
| 209 | |
| 210 | const newMessage = await this.create({ |
| 211 | createdUserId: userId, |
| 212 | chatId, |
| 213 | content, |
| 214 | htmlContent, |
| 215 | isEdited: false, |
| 216 | createdAt: new Date(), |
| 217 | lastEditedAt: new Date(), |
| 218 | parentMessageId: parentMessageId || null, |
| 219 | }); |
| 220 | |
| 221 | // add messageId to all participants of Chat except actor user |
| 222 | |
| 223 | await User.markMessageAsUnreadForParticipantsOfChatAfterMessageIsAddedByUser({ |
| 224 | userIdsToNotify: chat.chatParticipantIds.filter((id) => id !== userId), |
| 225 | messageId: newMessage._id.toString(), |
| 226 | }); |
| 227 | |
| 228 | // call markMessageAsUneadBySomeoneAfterMessageIsAddedByUser |
| 229 | await User.updateOne( |
| 230 | { _id: userId }, |
| 231 | { |
| 232 | $addToSet: { |
| 233 | unreadBySomeoneMessageIds: newMessage._id.toString(), |
| 234 | }, |
| 235 | }, |
| 236 | ); |
| 237 | |
| 238 | // update moveFile |
nothing calls this directly
no test coverage detected