|
@@ -1,55 +1,65 @@
|
|
|
-import { pubClient } from "../connection/redis.js";
|
|
|
-
|
|
|
-const prefix = process.env.REDIS_PREFIX || "chat";
|
|
|
-
|
|
|
-const getInKey = (realKey) => {
|
|
|
- return `${prefix}:${realKey}`;
|
|
|
-};
|
|
|
-const getCurrentUser = async (userId, key) => {
|
|
|
- try {
|
|
|
- if (userId) {
|
|
|
- console.log("getCurrentUser", userId, key);
|
|
|
- const user = await pubClient.hGet(getInKey(userId), String(key));
|
|
|
- return Promise.resolve(user);
|
|
|
- } else {
|
|
|
- return Promise.resolve(false);
|
|
|
- }
|
|
|
- } catch (error) {
|
|
|
- return Promise.resolve(false);
|
|
|
- }
|
|
|
-};
|
|
|
-
|
|
|
-const updateUser = async (userId, userObj) => {
|
|
|
- await pubClient.hSet(getInKey(userId), userObj.from, JSON.stringify(userObj));
|
|
|
- // await pubClient.expire(userId, 60 * 60 * 24);
|
|
|
-};
|
|
|
-
|
|
|
-const updateRoomUser = async (roomId, userId, userObj) => {
|
|
|
- try {
|
|
|
- await pubClient.hSet(getInKey(userId), userObj.from, JSON.stringify(userObj));
|
|
|
- await pubClient.hSet(getInKey(roomId), userId, JSON.stringify(userObj));
|
|
|
- return Promise.resolve(true);
|
|
|
- } catch (error) {
|
|
|
- return Promise.resolve(false);
|
|
|
- }
|
|
|
-};
|
|
|
-
|
|
|
-const removeRoomAllUsers = (roomId) => {
|
|
|
- return pubClient.del(getInKey(roomId));
|
|
|
-};
|
|
|
-
|
|
|
-const removeRoomUser = (roomId, useId) => {
|
|
|
- return pubClient.hDel(getInKey(roomId), useId);
|
|
|
-};
|
|
|
-
|
|
|
-const getAllRoomUsers = async (roomId) => {
|
|
|
- return new Promise(async (resolve) => {
|
|
|
- const AllRoomUsers = await pubClient.hVals(getInKey(roomId));
|
|
|
- const allUsers = Array.from(AllRoomUsers)
|
|
|
- .map((i) => JSON.parse(i))
|
|
|
- .sort((a, b) => parseInt(a.order) - parseInt(b.order));
|
|
|
- return resolve(allUsers);
|
|
|
- });
|
|
|
-};
|
|
|
-
|
|
|
-export { getCurrentUser, updateUser, updateRoomUser, removeRoomAllUsers, removeRoomUser, getAllRoomUsers };
|
|
|
+import { pubClient } from "../connection/redis.js";
|
|
|
+
|
|
|
+const prefix = process.env.REDIS_PREFIX || "chat";
|
|
|
+
|
|
|
+const getInKey = (realKey) => {
|
|
|
+ return `${prefix}:${realKey}`;
|
|
|
+};
|
|
|
+const getCurrentUser = async (userId, key) => {
|
|
|
+ try {
|
|
|
+ if (userId) {
|
|
|
+ console.log("getCurrentUser", userId, key);
|
|
|
+ const user = await pubClient.hGet(getInKey(userId), String(key));
|
|
|
+ return Promise.resolve(user);
|
|
|
+ } else {
|
|
|
+ return Promise.resolve(false);
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ return Promise.resolve(false);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+const updateUser = async (userId, userObj) => {
|
|
|
+ await pubClient.hSet(getInKey(userId), userObj.from, JSON.stringify(userObj));
|
|
|
+ // await pubClient.expire(userId, 60 * 60 * 24);
|
|
|
+};
|
|
|
+
|
|
|
+const updateRoomUser = async (roomId, userId, userObj) => {
|
|
|
+ try {
|
|
|
+ await pubClient.hSet(getInKey(userId), userObj.from, JSON.stringify(userObj));
|
|
|
+ await pubClient.hSet(getInKey(roomId), userId, JSON.stringify(userObj));
|
|
|
+ return Promise.resolve(true);
|
|
|
+ } catch (error) {
|
|
|
+ return Promise.resolve(false);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+const removeRoomAllUsers = (roomId) => {
|
|
|
+ return pubClient.del(getInKey(roomId));
|
|
|
+};
|
|
|
+
|
|
|
+const removeRoomUser = (roomId, useId) => {
|
|
|
+ return pubClient.hDel(getInKey(roomId), useId);
|
|
|
+};
|
|
|
+
|
|
|
+const getAllRoomUsers = async (roomId) => {
|
|
|
+ return new Promise(async (resolve) => {
|
|
|
+ const AllRoomUsers = await pubClient.hVals(getInKey(roomId));
|
|
|
+ const allUsers = Array.from(AllRoomUsers)
|
|
|
+ .map((i) => JSON.parse(i))
|
|
|
+ .sort((a, b) => parseInt(a.order) - parseInt(b.order));
|
|
|
+ return resolve(allUsers);
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
+const isUserInRooms = async (roomId, userId) => {
|
|
|
+ return new Promise(async (resolve) => {
|
|
|
+ const AllRoomUsers = await pubClient.hVals(getInKey(roomId));
|
|
|
+ const hasUser = Array.from(AllRoomUsers)
|
|
|
+ .map((i) => JSON.parse(i))
|
|
|
+ .find((u) => u.userId === userId);
|
|
|
+ return resolve(hasUser || false);
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
+export { getCurrentUser, updateUser, updateRoomUser, removeRoomAllUsers, removeRoomUser, getAllRoomUsers, isUserInRooms };
|