---
# System prepended metadata

title: Update API route

---

# Update API route
## GET /messages


#### Params
> **messageId** [messageId]
> to load the search history, where the message with this messageId must be present in the middle of the pack
> *Example: messageId = 10 will return pack [ ..9, { messageId: 10 }, 11...]*

> **nextId** [hash]
> cursor to the edge element in the pack, oldest if isLoadNextMessages = 0, newest if isLoadNextMessages = 1 


> **isLoadNextMessages**(!) [0; 1]
> if 0, you need to return older ones, BEFORE nextId
> if 1, you need to load newer messages, AFTER nextId 

> **limit**(default = 20) [positive number]
> indicates the number of messages in each pack

> **sessionId**(!) [sessionId]
> sessionId of session we want to get messages from
* (!) - required param


#### Functional requirements
1. get the latest history +
2. get previous history
3. get the following history
4. get history that contains the message *(messageId)* inside

#### Usage examples
unreads(4) = [21, 22, 23, 24]
last message = 24
limit = 10
```
response: {
  data: [
     { messageId: 15 }, // 1
     { messageId: 16 }, // 2
     { messageId: 17 }, // 3
     { messageId: 18 }, // 4
     { messageId: 19 }, // 5
     { messageId: 20 }, // 6
     { messageId: 21 }, // 7
     { messageId: 22 }, // 8 
     { messageId: 23 }, // 9
     { messageId: 24 }, // 10
  ],
  meta: {
    cursor: {
        next: null,
        prev: string
    },
    firstUnreadMessageId: 21
  }
}
```

---
unreads(10) = [15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
last message = 24
limit = 10

```
response: {
  data: [
     { messageId: 15 }, // 1
     { messageId: 16 }, // 2
     { messageId: 17 }, // 3
     { messageId: 18 }, // 4
     { messageId: 19 }, // 5
     { messageId: 20 }, // 6
     { messageId: 21 }, // 7
     { messageId: 22 }, // 8 
     { messageId: 23 }, // 9
     { messageId: 24 }, // 10
  ],
  meta: {
    cursor: {
        next: null,
        prev: string
    },
    firstUnreadMessageId: 15
  }
}
```
---
unreads(15) = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
last message = 24
limit = 10
```
response: {
  data: [
     { messageId: 10 }, // 1
     { messageId: 11 }, // 2
     { messageId: 12 }, // 3
     { messageId: 13 }, // 4
     { messageId: 14 }, // 5
     { messageId: 15 }, // 6
     { messageId: 16 }, // 7
     { messageId: 17 }, // 8 
     { messageId: 18 }, // 9
     { messageId: 19 }, // 10
  ],
  meta: {
    cursor: {
        next: string,
        prev: string
    },
    firstUnreadMessageId: 10
  }
}
```


#### Response
```response: { metaData: { nextCursorId: <someHash> }, data: <message>[] }```

GET /messages?
 [R]sessionId=Number
 [P]limit=Number
 [P]sort=JSONString <{ "sentAt": "asc" | "desc" }>
 [P]search=JSONString <{ "messageIds": [34,125], "sentAt": { "gt|lt": ISOString } }>
    <isSkipUnread|isUnread>=Boolean
    <isSelfHistory|isAvaialbleForUser>=Boolean | <entityId>=Number

    if (header.entityId === query.entityId) {
        return next();
    }
    if (headers.include(systemFunction)) {
        return next();
    }
    
Q: Якщо ми ігноруємо анріди, чи маєми їх позначити як прочинатні?
    -- промаркувати все решта прочитаним