Skip to content
This repository has been archived by the owner on Jan 1, 2022. It is now read-only.

Commit

Permalink
groupId, username, date, title, description, member_count, type, is_v…
Browse files Browse the repository at this point in the history
…erified, is_scam, last_updated
  • Loading branch information
darmiel committed Nov 28, 2020
1 parent e9d3a09 commit ccebc2c
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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(
Expand All @@ -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())
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
105 changes: 75 additions & 30 deletions apps/rest-api/src/controller/chatsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand All @@ -91,6 +106,7 @@ module.exports.addChat = async (chat) => {

// we'll compare the following mysql & object keys
const fields = [
"groupId",
"username",
"date",
"title",
Expand All @@ -102,7 +118,7 @@ module.exports.addChat = async (chat) => {
"last_updated",
];

let update = {
const update = {
query: "",
params: [],
};
Expand All @@ -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 (?, ?, ?, ?, ?);",
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions apps/rest-api/src/controller/messagesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit ccebc2c

Please sign in to comment.