@@ -3,7 +3,7 @@ import { User } from "../entities/user.entity.js";
3
3
import { Message } from "../entities/message.entity.js" ;
4
4
import { Chat } from "../entities/chat.entity.js" ;
5
5
import crypto from "crypto" ;
6
- import { createChatKeyHash , encryptMessage } from "../authorization/crypt.js" ;
6
+ import { createChatKeyHash , encryptMessage , decryptMessage } from "../authorization/crypt.js" ;
7
7
export const chat = express . Router ( ) ;
8
8
9
9
////// FUNCTION
@@ -32,7 +32,7 @@ async function CheckUser(userid) {
32
32
chat . post ( "/sendmessage" , async ( req , res ) => {
33
33
// Body{obj.receiverId, obj.content}
34
34
let chatkey = null ;
35
- console . log ( "----------------------------------------------" ) ;
35
+ // console.log("----------------------------------------------");
36
36
try {
37
37
const obj = req . body ;
38
38
const currentUser = await User . findById ( req . user . id ) . populate ( {
@@ -43,34 +43,34 @@ chat.post("/sendmessage", async (req, res) => {
43
43
}
44
44
} ) ;
45
45
46
- console . log ( "\n" ) ;
47
- console . log ( obj . receiverId ) ;
46
+ // console.log("\n");
47
+ // console.log(obj.receiverId);
48
48
const receiverUser = await User . findById ( obj . receiverId ) . populate ( {
49
49
path : 'chatList' ,
50
50
match : {
51
51
usertwo : req . user . id ,
52
52
}
53
53
} ) ;
54
54
55
- console . log ( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ) ;
55
+ // console.log("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
56
56
57
57
if ( ! currentUser ) {
58
- console . log ( "Current User empty" ) ;
58
+ // console.log("Current User empty");
59
59
res . status ( 404 ) . json ( { error : 'currentUser not found' } ) ;
60
60
} else if ( ! receiverUser ) {
61
- console . log ( "Receiver User empty" ) ;
61
+ // console.log("Receiver User empty");
62
62
res . status ( 404 ) . json ( { error : 'currentUser not found' } ) ;
63
63
}
64
64
65
- console . log ( "current user chat list \n" ) ;
66
- console . log ( currentUser . chatList . length ) ;
65
+ // console.log("current user chat list \n");
66
+ // console.log(currentUser.chatList.length);
67
67
68
68
69
69
const createAndSaveChat = async ( user , receiverId ) => {
70
70
if ( chatkey == null ) {
71
- chatkey = await createChatKeyHash ( crypto . randomBytes ( 9 ) . toString ( 'hex' ) ) ;
72
- console . log ( "chatkey \n" )
73
- console . log ( chatkey )
71
+ chatkey = createChatKeyHash ( ) ;
72
+ // console.log("chatkey \n")
73
+ // console.log(chatkey)
74
74
}
75
75
const chatldata = await Chat . create ( {
76
76
usertwo : receiverId ,
@@ -91,8 +91,8 @@ chat.post("/sendmessage", async (req, res) => {
91
91
currentUser . chatList = [ chatListData ] ;
92
92
}
93
93
94
- console . log ( receiverUser . chatList . length ) ;
95
- console . log ( "\nifffffffffffff\n" ) ;
94
+ // console.log(receiverUser.chatList.length);
95
+ // console.log("\nifffffffffffff\n");
96
96
97
97
if ( ! receiverUser . chatList || receiverUser . chatList . length === 0 ) {
98
98
const chatListData = await createAndSaveChat ( receiverUser , req . user . id ) ;
@@ -104,6 +104,13 @@ chat.post("/sendmessage", async (req, res) => {
104
104
const chatIdCurrentUser = currentUser . chatList [ 0 ] . id ;
105
105
const chatIdReceiverUser = receiverUser . chatList [ 0 ] . id ;
106
106
let cryptconent = await encryptMessage ( obj . content , currentUser . chatList [ 0 ] . chatKey ) ;
107
+ // console.log("crtype conetent------------")
108
+ // console.log(cryptconent);
109
+ // console.log('====================================');
110
+ let decrtypy = await decryptMessage ( cryptconent , currentUser . chatList [ 0 ] . chatKey )
111
+ // console.log("decrtypy conetent------------")
112
+ // console.log(decrtypy)
113
+ // console.log('====================================');
107
114
const message = await Message . create ( {
108
115
content : cryptconent ,
109
116
receiverId : obj . receiverId ,
@@ -118,8 +125,8 @@ chat.post("/sendmessage", async (req, res) => {
118
125
chat : chatIdReceiverUser
119
126
} ) ;
120
127
121
- console . log ( "\n " ) ;
122
- console . log ( receiverUser . chatList [ 0 ] ) ;
128
+ // console.log("\n ");
129
+ // console.log(receiverUser.chatList[0]);
123
130
currentUser . chatList [ 0 ] . messages . push ( message . _id ) ;
124
131
receiverUser . chatList [ 0 ] . messages . push ( message2 . _id ) ;
125
132
// await Promise.all([
@@ -129,30 +136,29 @@ chat.post("/sendmessage", async (req, res) => {
129
136
await currentUser . save ( ) ,
130
137
await receiverUser . save ( )
131
138
132
- console . log ( receiverUser . chatList [ 0 ] . messages ) ;
133
- console . log ( "--------- send message section ------" ) ;
139
+ // console.log(receiverUser.chatList[0].messages);
140
+ // console.log("--------- send message section ------");
134
141
135
142
return res . status ( 200 ) . send ( true ) ;
136
143
} catch ( error ) {
137
- console . log ( error ) ;
144
+ // console.log(error);
138
145
return error ;
139
146
}
140
147
} ) ;
141
-
142
-
143
- // Get Methods
144
- ///////////////*FrontendNOT*////////////////////////////////////////////
148
+ // Get Methods m///////////////*FrontendNOT*////////////////////////////////////////////
145
149
// /chatmessages?page=0&receiverId=123
146
150
chat . get ( '/chatmessages' , async ( req , res ) => {
147
151
try {
152
+
153
+ const myid = req . user . id
148
154
const { page, receiverId } = req . query ;
149
- console . log ( "''''''''''''''''''''" )
155
+ // console.log("\n ''''''''''''''''''''")
150
156
151
- const limit = 10
157
+ const limit = 100
152
158
const skipCount = page * limit ;
153
159
154
160
if ( ! await CheckUser ( receiverId ) ) {
155
- console . log ( "not found user" ) ;
161
+ // console.log("not found user");
156
162
return res . status ( 404 ) . json ( { error : "not found" } ) ;
157
163
}
158
164
@@ -168,13 +174,26 @@ chat.get('/chatmessages', async (req, res) => {
168
174
options : {
169
175
limit : limit ,
170
176
skip : skipCount ,
171
- sort : { dateTime : - 1 }
177
+ sort : { dateTime : 1 }
172
178
}
173
179
} ) ;
174
180
175
- const messagesArray = mychat . flatMap ( chat => chat . messages ) ; // create 1 array
181
+ //console.log(mychat)
182
+ const decryptedMessages = [ ] ;
183
+
184
+ mychat . forEach ( chat => {
185
+ chat . messages . forEach ( element => {
186
+ let decrtpycontent = decryptMessage ( element . content , chat . chatKey ) ;
187
+ element . content = decrtpycontent ;
188
+ decryptedMessages . push ( element )
189
+ } ) ;
190
+ } ) ;
191
+
192
+
193
+
194
+
176
195
177
- return res . status ( 200 ) . json ( messagesArray ) ;
196
+ return res . status ( 200 ) . json ( decryptedMessages ) ;
178
197
} catch ( error ) {
179
198
console . error ( error ) ;
180
199
res . status ( 500 ) . json ( { error : 'Internal Server Error' } ) ;
0 commit comments