diff --git a/apps/message-crawler/telegram-crawler-core/src/main/java/io/d2a/schwurbelwatch/mods/updaters/ChatsUpdateModule.java b/apps/message-crawler/telegram-crawler-core/src/main/java/io/d2a/schwurbelwatch/mods/updaters/ChatsUpdateModule.java index 207e765..f093e52 100644 --- a/apps/message-crawler/telegram-crawler-core/src/main/java/io/d2a/schwurbelwatch/mods/updaters/ChatsUpdateModule.java +++ b/apps/message-crawler/telegram-crawler-core/src/main/java/io/d2a/schwurbelwatch/mods/updaters/ChatsUpdateModule.java @@ -19,6 +19,8 @@ import org.drinkless.tdlib.TdApi; import org.drinkless.tdlib.TdApi.BasicGroup; import org.drinkless.tdlib.TdApi.Chat; +import org.drinkless.tdlib.TdApi.ChatTypeBasicGroup; +import org.drinkless.tdlib.TdApi.ChatTypeSupergroup; import org.drinkless.tdlib.TdApi.Error; import org.drinkless.tdlib.TdApi.GetBasicGroup; import org.drinkless.tdlib.TdApi.GetSupergroup; @@ -54,9 +56,9 @@ public void onEnable() { private void addOrUpdateChat(@Nullable final Chat chat, @Nullable final ApiChat apiChat) { if (chat != null) { - Logger.debug(chat); + // Logger.debug(chat); } - Logger.debug(apiChat); + //Logger.debug(apiChat); SwApi.callDatabaseResult(SwApi.CHAT_SERVICE.addChat(apiChat)); } @@ -99,6 +101,7 @@ public void onNewMessage(final UpdateNewMessage event) { if (System.currentTimeMillis() - lastUpdated <= 300_000) { return; } + this.chatInfoCache.put(chatId, System.currentTimeMillis()); Logger.info("Updating chat " + chatId); this.client.getClient().send( @@ -123,18 +126,26 @@ public void onResult(final Object object) { if (object.getConstructor() == Chat.CONSTRUCTOR) { final Chat chat = (Chat) object; final long chatId = chat.id; + int groupId = 0; switch (ChatType.getType(chat.type)) { case BASIC: - this.client.send(new GetBasicGroup((int) chatId), this); + final ChatTypeBasicGroup chatTypeBasicGroup = (ChatTypeBasicGroup) chat.type; + groupId = chatTypeBasicGroup.basicGroupId; + + this.client.send(new GetBasicGroup(groupId), this); break; case SUPER: - this.client.send(new GetSupergroup((int) chatId), this); + final ChatTypeSupergroup chatTypeSupergroup = (ChatTypeSupergroup) chat.type; + groupId = chatTypeSupergroup.supergroupId; + + this.client.send(new GetSupergroup(groupId), this); break; } final ApiChat build = ApiChat.builder() .chatId(chat.id) + .groupId(groupId) .title(chat.title) .type(ChatType.getType(chat.type)) .lastUpdated(System.currentTimeMillis()) @@ -151,16 +162,19 @@ public void onResult(final Object object) { Logger.debug("Retrieved a basic group"); final BasicGroup group = (BasicGroup) object; + System.out.println(group); apiChat = ApiChat.builder() - .chatId(group.id) + .groupId(group.id) .memberCount(group.memberCount) .build(); } else if (object.getConstructor() == Supergroup.CONSTRUCTOR) { Logger.debug("Retrieved a super group"); final Supergroup group = (Supergroup) object; + System.out.println(group); + apiChat = ApiChat.builder() - .chatId(group.id) + .groupId(group.id) .username(group.username) .date(group.date) .memberCount(group.memberCount) diff --git a/apps/message-crawler/telegram-crawler-core/src/main/java/io/d2a/schwurbelwatch/tgcrawler/api/routes/chats/ApiChat.java b/apps/message-crawler/telegram-crawler-core/src/main/java/io/d2a/schwurbelwatch/tgcrawler/api/routes/chats/ApiChat.java index 22a1ac1..cbe5488 100644 --- a/apps/message-crawler/telegram-crawler-core/src/main/java/io/d2a/schwurbelwatch/tgcrawler/api/routes/chats/ApiChat.java +++ b/apps/message-crawler/telegram-crawler-core/src/main/java/io/d2a/schwurbelwatch/tgcrawler/api/routes/chats/ApiChat.java @@ -8,7 +8,9 @@ @ToString public class ApiChat { - public final long chatId; + public final Long chatId; + public final Integer groupId; + public final String username; public final Integer date; public final String title; diff --git a/apps/rest-api/src/controller/chatsController.js b/apps/rest-api/src/controller/chatsController.js index 624fd5a..b4251ab 100644 --- a/apps/rest-api/src/controller/chatsController.js +++ b/apps/rest-api/src/controller/chatsController.js @@ -7,17 +7,15 @@ const Joi = require("joi"); * Schemas */ const chatSchema = Joi.object({ - chatId: Joi.number().required(), + chatId: Joi.number().default(0).allow("").allow(null).optional(), + groupId: Joi.number().default(0).allow("").allow(null).optional(), + username: Joi.string().default(null).allow("").allow(null).optional(), date: Joi.number().default(0).allow("").allow(null).optional(), title: Joi.string().default(null).allow("").allow(null).optional(), description: Joi.string().default(null).allow("").allow(null).optional(), member_count: Joi.number().min(0).default(0).allow("").allow(null).optional(), - type: Joi.string() - .default("unknown") - .allow("") - .allow(null) - .optional(), + type: Joi.string().default("unknown").allow("").allow(null).optional(), is_verified: Joi.number() .min(0) .max(1) @@ -61,8 +59,16 @@ module.exports.addChat = async (chat) => { }; } + if (value.chatId == null && value.groupId == null) { + return { + error: true, + message: "chatId or groupId missing.", + }; + } + // get chat id and check - const chatId = parseInt(value.chatId); + const chatId = value.chatId; + const groupId = value.groupId; // current date in milliseconds const date = Date.now(); @@ -72,14 +78,23 @@ module.exports.addChat = async (chat) => { try { // get existing chats if any exists - const rows = await connection.query( - "SELECT * FROM chats WHERE chatId = ? LIMIT 1;", - [chatId] - ); + let rows = []; + if (value.chatId != null) { + rows = await connection.query( + "SELECT * FROM chats WHERE chatId = ? LIMIT 1;", + [parseInt(chatId)] + ); + } else if (value.groupId != null) { + rows = await connection.query( + "SELECT * FROM chats WHERE groupId = ? LIMIT 1;", + [groupId] + ); + } // there is an chat existing with the same chatId if (rows.length >= 1) { const oldChat = rows[0]; + console.log(oldChat); // check if we monitor this channel, if not, return with an error if (oldChat.monitor == 0) { @@ -91,6 +106,7 @@ module.exports.addChat = async (chat) => { // we'll compare the following mysql & object keys const fields = [ + "groupId", "username", "date", "title", @@ -102,7 +118,7 @@ module.exports.addChat = async (chat) => { "last_updated", ]; - let update = { + const update = { query: "", params: [], }; @@ -117,22 +133,23 @@ module.exports.addChat = async (chat) => { const _old = oldChat[field]; const _new = value[field]; - if (_old != _new) { + if (_new == null) { + continue; + } + + if (_old !== _new) { console.log( - "[Chat Update | " + - chatId + - "] Updating field " + - field + - " from " + - _old + - " to " + - _new + `[Chat Update | ${chatId}] Updating field ${field} from ${_old} to ${_new}` ); update.query += (update.query.length == 0 ? "" : ", ") + field + " = ?"; update.params.push(_new); + if (field == "last_updated") { + continue; + } + // add to updates await connection.query( "INSERT INTO chats_updates (`chatId`, `key`, `old_value`, `new_value`, `date`) VALUES (?, ?, ?, ?, ?);", @@ -143,27 +160,55 @@ module.exports.addChat = async (chat) => { // update ?! if (update.query.length > 0) { - update.query += ", last_updated = ?"; - update.params.push(date); + console.log(value); - update.params.push(chatId); // this is used for the where clause and should be here. + if (!update.query.includes("last_updated")) { + update.query += ", last_updated = ?"; + update.params.push(date); + } else if (update.query.length == 1) { + return { + error: true, + message: "Only updating last_updated.", + }; + } - console.log("[Chat Update | " + chatId + "] Updated chat"); + console.log( + "[Chat Update | " + + chatId + + "] Update chat " + + chatId + + " | " + + groupId + ); // update chat - return await connection.query( - "UPDATE chats SET " + update.query + " WHERE chatId = ?;", - update.params - ); + if (value.chatId != null) { + console.log("by chat id"); + console.log(update); + update.params.push(chatId); // this is used for the where clause and should be here. + return await connection.query( + "UPDATE chats SET " + update.query + " WHERE chatId = ?;", + update.params + ); + } else if (value.groupId != null) { + console.log("by group id"); + console.log(update); + update.params.push(groupId); // this is used for the where clause and should be here. + return await connection.query( + "UPDATE chats SET " + update.query + " WHERE groupId = ?;", + update.params + ); + } } else { console.log("[Chat Update | " + chatId + "] No need to update"); } } else { // insert new chat return await connection.query( - "INSERT INTO chats VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + "INSERT INTO chats VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", [ value.chatId, + value.groupId, value.username, value.date, value.title, diff --git a/apps/rest-api/src/controller/messagesController.js b/apps/rest-api/src/controller/messagesController.js index 1708c45..e040e7e 100644 --- a/apps/rest-api/src/controller/messagesController.js +++ b/apps/rest-api/src/controller/messagesController.js @@ -78,8 +78,8 @@ module.exports.addMessage = async (message) => { const _new = value.content; if (_old == _new) { - console.log(oldMessage); - console.log(value); + // TODO: Uncomment | console.log(oldMessage); + // TODO: Uncomment | console.log(value); return { error: true,