Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Apr 25, 2025. It is now read-only.
/chat_servicePublic archive

Commit01626ae

Browse files
committed
__ADD__ adding documentation for every event in service
1 parenta618913 commit01626ae

File tree

10 files changed

+2085
-41
lines changed

10 files changed

+2085
-41
lines changed

‎lib/controllers/chat_controller.js‎

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,24 @@ exports.newMessage = async (io, socket, data, callback) => {
6262
message:'message has been sent successfully'
6363
}));
6464
//TODO: brodcast in the future
65+
/**
66+
*@memberof socket_handler - this is a backend event
67+
*@event send_message
68+
*@emits text - message text
69+
*@emits room_id - id of the room
70+
*@emits timestamp - just a fucking timestamp
71+
*@emits moment - jalali momnet
72+
*@emits sender_id - id of the message sender
73+
*@emits message_id - id of the sent message
74+
*@description - sends message to the given room
75+
*/
6576
io.to(room_id).emit('send_message',Object.assign({},config.RESPONSE,{
6677
message:'successful',
6778
data:msg
6879
}));
6980
}
7081
});
71-
82+
7283
}
7384
}catch(e){
7485
callback(Object.assign({},config.RESPONSE,{
@@ -100,22 +111,34 @@ exports.isTyping = async (io, socket, data, callback) => {
100111
}=data;
101112
try{
102113
constvalid=validator.joi.validate(data,validator.isTyping);
103-
if(valid.error){
114+
if(valid.error){
104115
callback(Object.assign({},config.RESPONSE,{
105116
result:false,
106117
message:'input is not valid',
107118
data:valid.error
108119
}));
109-
}else{
120+
}else{
110121
constsender_id=awaitchat_dao.getSenderIdBySocket(room_id,socket.id);
111122
if(typing){
123+
/**
124+
*@memberof socket_handler - this is a backend event
125+
*@event start_typing
126+
*@emits sender_id - typing user id
127+
*@description - this event happens when a user is typing (0_0)
128+
*/
112129
io.to(room_id).emit('start_typing',Object.assign({},config.RESPONSE,{
113130
message:'start_typing',
114131
data:{
115132
sender_id
116133
}
117134
}));
118135
}else
136+
/**
137+
*@memberof socket_handler - this is a backend event
138+
*@event stop_typing
139+
*@emits sender_id - typing user id
140+
*@description - this event happens when a user is not typing (0_0)
141+
*/
119142
io.to(room_id).emit('stop_typing',Object.assign({},config.RESPONSE,{
120143
message:'stop_typing',
121144
data:{
@@ -155,13 +178,13 @@ exports.readMessage = async (io, socket, data, callback) => {
155178
}=data;
156179
try{
157180
constvalid=validator.joi.validate(data,validator.readMessage);
158-
if(valid.error){
181+
if(valid.error){
159182
callback(Object.assign({},config.RESPONSE,{
160183
result:false,
161184
message:'input is not valid',
162185
data:valid.error
163186
}));
164-
}else{
187+
}else{
165188
constsender_sockets=awaitroom_dao.findSocketsById(sender_id,room_id);
166189
constkeyedMessage=newkm('readMessage',JSON.stringify({
167190
sender_id,
@@ -177,8 +200,17 @@ exports.readMessage = async (io, socket, data, callback) => {
177200
throwe;
178201
}else{
179202
kafka_log(result);
180-
203+
181204
for(constsender_socketofsender_sockets){
205+
/**
206+
*@memberof socket_handler - this is a backend event
207+
*@event read_message
208+
*@emits sender_id - id of the user who had sent the message
209+
*@emits receiver_id - id of the user who had read the message
210+
*@emits room_id - id of the room
211+
*@emits message_id - mongo id of the message
212+
*@description - this event happens when a message is read by a user
213+
*/
182214
io.to(sender_socket).emit('read_message',Object.assign({},config.RESPONSE,{
183215
data:{
184216
sender_id,
@@ -188,7 +220,7 @@ exports.readMessage = async (io, socket, data, callback) => {
188220
}
189221
}));
190222
}
191-
223+
192224
}
193225
});
194226
}

‎lib/controllers/room_controller.js‎

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
constkafka_log=require('debug')('goftare:kafka');
22
constwinston=require('../utils/logger');
33
constvalidator=require('../utils/validator');
4-
// const { producer } = require('../utils/kafka');
4+
const{ producer, km}=require('../utils/kafka');
5+
constmongoose=require('mongoose');
6+
constmoment=require('moment-jalaali');
57
constroom_dao=require('../dao/room_dao');
68
constuser_dao=require('../dao/user_dao');
79
constconfig=require('../config/config');
@@ -38,8 +40,15 @@ exports.joinRoom = async (io, socket, data, callback) => {
3840
constmembers=awaitroom_dao.joinSocketToRoom(room_id,socket.id,joined_id);
3941
if(members){
4042
socket.join(room_id);
41-
//also done with broadcast
42-
io.in(room_id).emit('joined_room',Object.assign({},config.RESPONSE,{
43+
/**
44+
*@memberof socket_handler - this is a backend event
45+
*@event joined_room
46+
*@emits joined_id - the joined user id
47+
*@emits members - the array of members objects
48+
*@description the answer of the join room is an event named joined_room
49+
* which gives back the joined id and the "online" members
50+
*/
51+
io.to(room_id).emit('joined_room',Object.assign({},config.RESPONSE,{
4352
message:'successful',
4453
data:{
4554
joined_id,
@@ -83,7 +92,7 @@ exports.createRoom = async (io, socket, data, callback) => {
8392
message:'input is not valid',
8493
data:valid.error
8594
}));
86-
}else{
95+
}else{
8796
constsockets=[];
8897

8998
constsaved=awaitroom_dao.joinSocketToRoom(_id,socket.id,creator_id);
@@ -96,24 +105,54 @@ exports.createRoom = async (io, socket, data, callback) => {
96105
}
97106
}
98107
}
99-
108+
100109
// const created = await room_dao.saveRoom(room_id, sockets);
101110
kafka_log('........................\n'+sockets+'\n.............................');
102-
if(sockets.length!==0){
103-
for(constsocketofsockets){
104-
io.to(socket).emit('new_room',Object.assign({},config.RESPONSE,{
105-
_id,
106-
members
111+
constkeyedMessage=newkm('readMessage',JSON.stringify({
112+
text:'room_created',
113+
room_id:_id,
114+
timestamp:newDate().getTime(),
115+
moment:moment().format('jYYYY/jMM/jDD HH:mm:ss'),
116+
sender_id:creator_id,
117+
message_id:mongoose.Types.ObjectId()
118+
}));
119+
producer.send([Object.assign(config.PAYLOAD,{
120+
messages:[keyedMessage]
121+
})],function(e,result){
122+
if(e){
123+
callback(Object.assign({},config.RESPONSE,{
124+
result:false,
125+
message:'kafka error',
126+
data:e
107127
}));
128+
}else{
129+
if(sockets.length!==0){
130+
for(constsocketofsockets){
131+
/**
132+
*@memberof socket_handler - this is a backend event
133+
*@event new_room
134+
*@emits _id - the room id
135+
*@emits members - new room members
136+
*@description - this event indicates that a new room is
137+
* created and is supposed to emit a "join_room" after reciving
138+
*/
139+
io.to(socket).emit('new_room',Object.assign({},config.RESPONSE,{
140+
_id,
141+
members
142+
}));
143+
}
144+
callback(Object.assign({},config.RESPONSE,{
145+
message:'room_created',
146+
data:result
147+
}));
148+
149+
}else
150+
callback(Object.assign({},config.RESPONSE,{
151+
result:false,
152+
message:'failed to create the room by socket'
153+
}));
108154
}
109-
callback(Object.assign({},config.RESPONSE,{
110-
message:'room_created'
111-
}));
112-
}else
113-
callback(Object.assign({},config.RESPONSE,{
114-
result:false,
115-
message:'failed to create the room by socket'
116-
}));
155+
});
117156
}
118157
}catch(e){
119158
callback(Object.assign({},config.RESPONSE,{
@@ -140,6 +179,12 @@ exports.disconnecting = async (io, socket) => {
140179
try{
141180
constremovedSocket=awaitroom_dao.removeSocketFromRoom(rooms,socket.id);
142181
for(constroomofremovedSocket.rooms){
182+
/**
183+
*@memberof socket_handler
184+
*@event disconnected - this event happens when a user is disconnected from socket_io
185+
*@emits sender_id - id of the user who is disconnected
186+
*@description - user is disconnecting socket_io
187+
*/
143188
io.to(room).emit('disconnected',Object.assign({},config.RESPONSE,{
144189
message:'user disconnected',
145190
data:{

‎lib/utils/validator.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const joi = require('joi');
33

44
exports.message=joi.object().keys({
55
room_id:joi.string().required().min(23).max(25),
6-
text:joi.string().require(),
6+
text:joi.string().required(),
77
time_stamp:joi.date().timestamp()
88
});
99

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp