-
Notifications
You must be signed in to change notification settings - Fork 11.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad72919
commit cb2980a
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"$schema": "https://aka.ms/codetour-schema", | ||
"title": "5 - How to create a DB model", | ||
"steps": [ | ||
{ | ||
"directory": "apps/meteor/server/models", | ||
"description": "## How to create a DB Model \n\n### Rocket.Chat server relies on MongoDB as its primary database to store crucial information such as chat messages, user data, system configurations, and other related data. MongoDB plays a vital role in maintaining and organizing the essential information that powers Rocket.Chat.\n\n### Files like assets, user files, images, and other media files are stored locally on the system running the server or over network services like Amazon S3, and WebDAV.\n\n### Let us explore and see how can anyone create a new Database model, Here I will be giving example of Messages model and how things inside it works." | ||
}, | ||
{ | ||
"file": "apps/meteor/server/models/raw/Messages.ts", | ||
"description": "## Messages Model\n\n- **Firstly we start by importing some important components and modules such as BaseRaw which contains Operations related to Database model.**\n\n- **We have *Rooms* model imported from *@rocket.chat/models* which is again a model like we are looking at right now, Checkout [Rooms](./apps/meteor/server/models/raw/Rooms.ts) Model**\n\n- **We also have imports from model-typings such as FindPaginated and IMessageModel which have type definitions for the model**\n\n- **We have multiple imports from mongodb as *AggregationCursor, Collection, FindCursor, UpdateResult, etc.* which help in mongodb operations**", | ||
"line": 31 | ||
}, | ||
{ | ||
"file": "apps/meteor/server/models/raw/Messages.ts", | ||
"description": "## MessageRaw class\n\n- **To register a DB model in Rocket.Chat, it is necessary to create a corresponding class for that model. In our case, as we are creating the Messages Model, we need to define a class specifically for it. This class will serve as the blueprint for the Messages Model and will be used for registering and interacting with the corresponding data in the database.**\n\n- **In order to facilitate the management of the Messages Model in Rocket.Chat's database, we have a class called MessageRaw. This class extends the BaseRaw class and implements the IMessageModel interface, which we discussed earlier. By extending BaseRaw and implementing the IMessageModel interface, MessageRaw inherits necessary functionalities and ensures it adheres to the required structure and behavior of the Messages Model in Rocket.Chat.**", | ||
"line": 42 | ||
}, | ||
{ | ||
"file": "apps/meteor/server/models/raw/Messages.ts", | ||
"description": "## Methods and Operations\n\n### From here onwards there are multiple methods and operations related with Message Model you can go through each of them and try to undestand what are they doing, It is easy to understand them.\n\n- **There are multiple methods such as**\n - ***findStarredByUserAtRoom***\n - ***findLivechatMessages***\n - ***findStarred***\n - ***setMessageAttachments***\n - ***getMessageByFileIdAndUsername***\n - and many more", | ||
"line": 82 | ||
}, | ||
{ | ||
"file": "apps/meteor/server/models/Messages.ts", | ||
"description": "## Registering a DB model\n\n### Now let us see how can we register any DB model\n\n### 1 - First of all we need to import *registeModel* from *@rocket.chat/models*\n\n### 2 - Import MessagesRaw- The DB model Class we created which includes operations, and we are also importing db, trashCollection- It contains deleted messages", | ||
"line": 1 | ||
}, | ||
{ | ||
"file": "apps/meteor/server/models/Messages.ts", | ||
"description": "## Registering\n\n### Here we are using the registerModel import and passing 'IMessageModel' and using class MessagesRaw we pass in db and trashCollection\n\n### The register model function looks something like this -\n```\nfunction registerModel<TModel extends IBaseModel<any, any, any>>(name: string, instance: TModel | (() => TModel)): void;\n```\n\n### And we pass data into it like - \n```\n registerModel('IMessagesModel', new MessagesRaw(db, trashCollection));\n //It becomes something like this, Here IMessageModel is basically implemented in MessagesRaw as we saw in previous steps\n registerModel<MessagesRaw>(name: string, instance: MessagesRaw | (() => MessagesRaw)): void\n```", | ||
"line": 7 | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"$schema": "https://aka.ms/codetour-schema", | ||
"title": "6 - How to use a DB model", | ||
"steps": [ | ||
{ | ||
"directory": "apps/meteor/server/methods", | ||
"description": "## How to use a DB model\n\n### We discussed about how to create a DB model in previous tour, Here we would be learning how to use a DB model\n\n### Let us Learn how to use DB model by taking an example of loading older messages in channels, This is an meteor method, which is called when we scroll up and try to load older messages (/api/v1/method.call/loadHistory)" | ||
}, | ||
{ | ||
"file": "apps/meteor/server/methods/loadHistory.ts", | ||
"description": "## loadHistory Endpoint\n\n### Here We hava an meteor endpoint which takes rid, end, limit, ls, showThreadMessage as argument, This method is responsible for rendering older messages \n\n- You can go through the methods used below they are quiet easy to understand", | ||
"line": 32 | ||
}, | ||
{ | ||
"file": "apps/meteor/server/methods/loadHistory.ts", | ||
"description": "## The LoadMessageHistory \n\n### LoadMessageHistory function is called here and it takes userId, rid, end, limit, ls, showThreadMessages", | ||
"line": 64 | ||
}, | ||
{ | ||
"file": "apps/meteor/app/lib/server/functions/loadMessageHistory.ts", | ||
"description": "## loadMessageHistory function\n\n### Here we import Messages and Rooms DB models from @rocket.chat/models and we will furhter use them in our function\n\n", | ||
"line": 2 | ||
}, | ||
{ | ||
"file": "apps/meteor/app/lib/server/functions/loadMessageHistory.ts", | ||
"description": "## Explanation\n\n- The loadMessageHistory function is responsible for retrieving a history of messages from a specific room in Rocket.Chat. It accepts several parameters including the user ID, room ID, end timestamp, limit, last seen timestamp, whether to show thread messages, and offset.\n\n- First, it fetches the room using the provided room ID and checks if it exists. If the room doesn't exist, an error is thrown.\n\n- Next, it determines the types of hidden system messages for the room.\n\n- Then, it defines options for querying the messages, including sorting by timestamp in descending order, applying the limit and offset.", | ||
"line": 8 | ||
}, | ||
{ | ||
"file": "apps/meteor/app/lib/server/functions/loadMessageHistory.ts", | ||
"description": "## Explanation 2\n\n- Based on the provided end timestamp, it retrieves visible messages before that timestamp, excluding the hidden message types. If no end timestamp is provided, it retrieves all visible messages in the room, excluding hidden message types.\n\n- The retrieved records are then normalized and processed for the specific user, taking into account any restrictions or modifications based on the user's permissions or settings.\n\n- If a last seen timestamp is provided, it checks if there are unread messages after that timestamp. If there are unread messages, it retrieves the first unread message and calculates the total count of unread messages that are not loaded yet.\n\n- Finally, it returns an object containing the retrieved messages, the first unread message, and the count of unread messages that are not loaded.", | ||
"line": 42 | ||
}, | ||
{ | ||
"file": "apps/meteor/app/lib/server/functions/loadMessageHistory.ts", | ||
"description": "## Returns\n\n### In the end it returns messages, firstUnread, unreadNotLoaded you can go through the code above its easy to understand", | ||
"line": 88 | ||
} | ||
] | ||
} |