
# Service Library - `lobbyChat`

This document provides a complete reference of the custom code library for the `lobbyChat` service. It includes all library functions, edge functions with their REST endpoints, templates, and assets.


## Library Functions

Library functions are reusable modules available to all business APIs and other custom code within the service via `require("lib/<moduleName>")`.


### `lobbyChatValidateMuteAndContent.js`

```js
module.exports = async function lobbyChatValidateMuteAndContent(context) {
  // context.session.userId is the sender
  const senderId = context.session.userId;
  if (!senderId) throw new Error('You must be logged in to send messages.');

  // Collect active mute(s) for this sender (any lobbyMessage with mutedUntil > now for this sender)
  const { getLobbyMessageListByQuery } = require("dbLayer");
  const now = new Date();
  const mutedMsgs = await getLobbyMessageListByQuery({ senderId, mutedUntil: { $notnull: true } });
  if (mutedMsgs && mutedMsgs.length > 0) {
    // Muted if *any* applicable unexpired mute exists
    const isMuted = mutedMsgs.some(msg => msg.mutedUntil && new Date(msg.mutedUntil) > now);
    if (isMuted) throw new Error('You are currently muted and cannot send lobby messages.');
  }
  // Content validation
  const body = context.content ?? context.request?.body?.content;
  if (!body || typeof body !== 'string' || !body.trim()) {
    throw new Error('Message content cannot be empty.');
  }
  if (body.length > 500) {
    throw new Error('Message content exceeds 500 character limit.');
  }
  // Attach displayName
  context.senderDisplayName = context.session.fullname || 'Anonymous';
  return true;
}
```


### `lobbyChatValidateModerationUpdate.js`

```js
module.exports = function lobbyChatValidateModerationUpdate(context) {
  // Only allowed to update: removed, mutedUntil, reportStatus (never content, senderDisplayName)
  const allowed = ['removed','mutedUntil','reportStatus'];
  const updating = Object.keys(context.updateData || {});
  if (updating.some(f => !allowed.includes(f))) {
    throw new Error('Only moderation fields may be changed: removed, mutedUntil, reportStatus.');
  }
  return true;
}
```





## Edge Functions

Edge functions are custom HTTP endpoint handlers that run outside the standard Business API pipeline. Each edge function is paired with an Edge Controller that defines its REST endpoint.


### `purgeExpiredLobbyMessages.js`


**Edge Controller:**
- **Path:** `/admin/purge-expired-lobby-messages`
- **Method:** `GET`
- **Login Required:** Yes


```js
module.exports = async () => {
  // Physically remove all messages older than 24h
  const { deleteLobbyMessageByQuery } = require("dbLayer");
  const cutoff = new Date(Date.now() - 24*60*60*1000);
  const where = { sentAt: { $lt: cutoff.toISOString() } };
  const deleted = await deleteLobbyMessageByQuery(where);
  return { status: 200, deletedCount: deleted.length, cutoff: cutoff.toISOString() };
}
```





## Edge Controllers Summary

| Function Name | Method | Path | Login Required |
|--------------|--------|------|----------------|
| `purgeExpiredLobbyMessages` | `GET` | `/admin/purge-expired-lobby-messages` | Yes |









---

*This document was generated from the service library configuration and should be kept in sync with design changes.*
