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

Commit

Permalink
Implements #23
Browse files Browse the repository at this point in the history
  • Loading branch information
darmiel committed Nov 30, 2020
1 parent ccebc2c commit 922da85
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,18 @@ public void onEnable() {
}
}

private void addOrUpdateChat(@Nullable final Chat chat, @Nullable final ApiChat apiChat) {
if (chat != null) {
// Logger.debug(chat);
}
//Logger.debug(apiChat);

SwApi.callDatabaseResult(SwApi.CHAT_SERVICE.addChat(apiChat));
}

@Subscribe
public void onUpdateNewChat(final TdApi.UpdateNewChat event) {
final Chat chat = event.chat;
final ApiChat build = ApiChat.builder()
.chatId(chat.id)
.title(chat.title)
.type(ChatType.getType(chat.type))
.lastUpdated(System.currentTimeMillis())
.build();
addOrUpdateChat(chat, build);

// uppdate new chat
// -> chatId
SwApi.callDatabaseResult(SwApi.CHAT_SERVICE.addChat(build));
}

@Subscribe
Expand All @@ -81,7 +74,10 @@ public void onChatTitleUpdate(final UpdateChatTitle event) {
.chatId(event.chatId)
.title(event.title)
.build();
addOrUpdateChat(null, build);

// uppdate chat title
// -> chatId
SwApi.callDatabaseResult(SwApi.CHAT_SERVICE.addChat(build));
}

@Subscribe
Expand Down Expand Up @@ -111,7 +107,7 @@ public void onNewMessage(final UpdateNewMessage event) {
}

@RequiredArgsConstructor
public class ChatInfoResultHandler implements ResultHandler {
public static class ChatInfoResultHandler implements ResultHandler {

private final Client client;

Expand Down Expand Up @@ -148,10 +144,11 @@ public void onResult(final Object object) {
.groupId(groupId)
.title(chat.title)
.type(ChatType.getType(chat.type))
.lastUpdated(System.currentTimeMillis())
.build();

addOrUpdateChat(null, build);
// uppdate chat title
// -> chatId
SwApi.callDatabaseResult(SwApi.CHAT_SERVICE.addChat(build));
return;
}

Expand Down Expand Up @@ -184,7 +181,9 @@ public void onResult(final Object object) {
}

if (apiChat != null) {
addOrUpdateChat(null, apiChat);
// uppdate chat title
// -> chatId
SwApi.callDatabaseResult(SwApi.CHAT_SERVICE.addChat(apiChat));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class SwApi {

public static final Gson GSON = new GsonBuilder()
.setPrettyPrinting()
.serializeNulls()
// .serializeNulls()
.create();

private static final Retrofit retrofit = new Retrofit.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ApiChat {
public final Byte isScam;

@SerializedName("last_updated")
public final long lastUpdated;
public final Long lastUpdated;

public final Byte monitor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ public interface ChatService {
@GET("chats")
Call<List<ApiChat>> getChats(@Query("offset") long offset);

@POST("chats")
Call<DatabaseResult> addChat(@Body ApiChat chat);
@POST("chats/chatId")
Call<DatabaseResult> addChatByChatId(@Body ApiChat chat);

@POST("chats/groupId")
Call<DatabaseResult> addChatByGroupId(@Body ApiChat chat);

@GET("chats/:id")
Call<ApiChat> getChat(@Path("id") long chatId);
Expand All @@ -27,4 +30,14 @@ public interface ChatService {
@POST("chats/:id/count/:count")
Call<DatabaseResult> updateMemberCount(@Path("id") long chatId, @Path("count") int count);

default Call<DatabaseResult> addChat(ApiChat chat) {
if (chat.chatId != null) {
return addChatByChatId(chat);
} else if (chat.groupId != null) {
return addChatByGroupId(chat);
} else {
return null;
}
}

}
Loading

0 comments on commit 922da85

Please sign in to comment.