Skip to content

Commit

Permalink
Added banger SQL query for getting messages
Browse files Browse the repository at this point in the history
  • Loading branch information
f-r00t committed Feb 26, 2024
1 parent 17c6a3e commit 2b6cc92
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 42 deletions.
70 changes: 29 additions & 41 deletions src/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -732,9 +732,6 @@ export async function markGroupConversationAsRead(group) {
});


Globals.unreadMessages = await getUnreadMessages();
Globals.updateGroupsFunction();

}

export async function markBoardsMessageAsRead(hash) {
Expand Down Expand Up @@ -1136,21 +1133,33 @@ export async function getMessages(conversation=false, limit=25) {
export async function getGroupMessages(group=false, limit=25) {

const [data] = await database.executeSql(
`SELECT
nickname,
type,
message,
timestamp,
board,
address,
hash
FROM
privateboards_messages_db
WHERE reply = '' ${group ? ' AND board = "' + group + '"' : ''}
ORDER BY
timestamp
DESC
LIMIT ${limit}`
`
SELECT
pm.nickname,
pm.type,
pm.message,
pm.timestamp,
pm.board,
pm.address,
pm.hash,
pm.reply,
COALESCE(rc.reply_count, 0) AS replies
FROM
privateboards_messages_db pm
LEFT JOIN (
SELECT
reply,
COUNT(*) AS reply_count
FROM
privateboards_messages_db
GROUP BY
reply
) rc ON pm.hash = rc.reply
WHERE
pm.reply = '' ${group ? ' AND pm.board = "' + group + '"' : ''}
ORDER BY
pm.timestamp DESC
LIMIT ${limit}`
);

const [count] = await database.executeSql(
Expand All @@ -1162,7 +1171,7 @@ export async function getGroupMessages(group=false, limit=25) {
let count_raw = 0;

if (count && count.rows && count.rows.length) {
console.log(count);
console.log(count, rand);
const res = [];

for (let i = 0; i < count.rows.length; i++) {
Expand All @@ -1180,27 +1189,6 @@ export async function getGroupMessages(group=false, limit=25) {
for (let i = 0; i < data.rows.length; i++) {
const item = data.rows.item(i);

const [replyCount] = await database.executeSql(
`
SELECT COUNT(*) FROM privateboards_messages_db WHERE reply = "${item.hash}"
`
);

let replyCount_raw = 0;

if (replyCount && replyCount.rows && replyCount.rows.length && item.hash != '') {

const res = [];

for (let i = 0; i < replyCount.rows.length; i++) {

const item = replyCount.rows.item(i);

replyCount_raw = item['COUNT(*)'];

}
};

res.push({
nickname: item.nickname,
type: item.type,
Expand All @@ -1210,7 +1198,7 @@ export async function getGroupMessages(group=false, limit=25) {
address: item.address,
hash: item.hash,
reply: item.reply,
replies: replyCount_raw,
replies: item.replies,
count: count_raw,
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/Groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ export class GroupChatScreenNoTranslation extends React.Component {

await markGroupConversationAsRead(this.state.key);

let messages = await getGroupMessages(this.state.key);
let messages = await getGroupMessages(this.state.key, 25);

if (!messages) {
messages = [];
Expand Down

0 comments on commit 2b6cc92

Please sign in to comment.