Skip to content

Commit

Permalink
farm head chat feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielzxccc committed May 1, 2024
1 parent 8fd10ae commit e00d3aa
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 22 deletions.
4 changes: 3 additions & 1 deletion src/modules/Forums/ForumsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export async function listQuestions(req: SessionRequest, res: Response) {
const searchKey = String(query.search)
const filterKey = query.filter
const tagKey = query.tag
const privateForum = query.privateForum

const questions = await Interactor.listQuestions(
offset,
Expand All @@ -57,7 +58,8 @@ export async function listQuestions(req: SessionRequest, res: Response) {
perPage,
req.session.userid || '00',
query.profile,
tagKey
tagKey,
privateForum
)
const totalPages = Math.ceil(Number(questions.total.count) / perPage)
res.status(200).json({
Expand Down
17 changes: 12 additions & 5 deletions src/modules/Forums/ForumsInteractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { deleteFile } from '../../utils/file'
import { viewsLimitter } from '../../middleware/ViewsLimitter'
import { findUser } from '../Users/UserService'
import { emitPushNotification } from '../Notifications/NotificationInteractor'
import { emitNotificationToFarmHeads } from '../Socket/SocketController'

export async function viewQuestion(
id: string,
Expand Down Expand Up @@ -66,7 +67,8 @@ export async function listQuestions(
perpage: number,
userid: string,
profile?: string,
tag?: string
tag?: string,
privateForum?: boolean
) {
const [data, total] = await Promise.all([
Service.findQuestions(
Expand All @@ -76,9 +78,10 @@ export async function listQuestions(
perpage,
userid,
profile,
tag
tag,
privateForum
),
Service.getTotalCount(profile, searchKey, tag),
Service.getTotalCount(profile, searchKey, tag, privateForum),
])

for (let question of data) {
Expand Down Expand Up @@ -131,10 +134,14 @@ export async function createNewQuestion(
if (!userid) {
throw new HttpError('Session Expired', 401)
}
const { title, question, tags } = questions.body
const content = { userid, title, question, imagesrc }
const { title, question, tags, privateForum } = questions.body
const content = { userid, title, question, imagesrc, private: privateForum }
const newQuestion = await Service.createQuestion(content, tags)

if (newQuestion.private) {
emitNotificationToFarmHeads()
}

await uploadFiles(uploadedFiles)
for (const image of uploadedFiles) {
deleteFile(image.filename)
Expand Down
5 changes: 5 additions & 0 deletions src/modules/Forums/ForumsOpenApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
* schema:
* type: string
* description: tag query
* - in: query
* name: privateForum
* schema:
* type: boolean
* description: tag query
* responses:
* "200":
* description: Success
Expand Down
38 changes: 23 additions & 15 deletions src/modules/Forums/ForumsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export async function findQuestions(
perpage: number,
userid: string,
profile?: string,
tag?: string
tag?: string,
privateForum?: boolean
) {
let query = db
.selectFrom('forums')
Expand All @@ -38,14 +39,17 @@ export async function findQuestions(
'forums.id',
jsonObjectFrom(
eb
.selectFrom('users')
.selectFrom('users as u')
.leftJoin('community_farms as cf', 'cf.id', 'u.farm_id')
.select([
'avatar',
'username',
'role',
sql<string>`CAST(id AS TEXT)`.as('id'),
'u.avatar',
'u.username',
'u.role',
'u.district',
'cf.farm_name',
sql<string>`CAST(u.id AS TEXT)`.as('id'),
])
.whereRef('forums.userid', '=', 'users.id')
.whereRef('forums.userid', '=', 'u.id')
).as('user'),
jsonArrayFrom(
eb
Expand All @@ -63,11 +67,6 @@ export async function findQuestions(
'forums.updatedat',
'forums.views',
sql<string>`COUNT(DISTINCT forums_answers.id)`.as('answer_count'),
// fn
// .count<number>('forums_ratings.id')
// .filterWhere('type', '=', 'upvote')
// .distinct()
// .as('vote_count'),
fn.count<number>('forums_ratings.id').distinct().as('vote_count'),
fn
.count<number>('forums_ratings.id')
Expand All @@ -79,8 +78,6 @@ export async function findQuestions(
.distinct()
.filterWhere('type', '=', 'upvote')
.as('upvote'),
// fn.count<number>('DISTINCT forums_answers.id').as('answer_count'),
// fn.count<number>('forums_ratings.id').as('vote_count'),
fn.max('forums_answers.createdat').as('latest_answer_createdat'),
jsonObjectFrom(
eb
Expand All @@ -99,6 +96,12 @@ export async function findQuestions(
query = query.orderBy('latest_answer_createdat', 'desc')
if (filterKey === 'trending') query = query.orderBy('upvote', 'desc')

if (privateForum) {
query = query.where('forums.private', '=', true)
} else {
query = query.where('forums.private', '=', false)
}

if (searchQuery.length) {
query = query.where((eb) =>
eb.or([
Expand Down Expand Up @@ -369,7 +372,8 @@ export async function getTotalAnswers(id: string) {
export async function getTotalCount(
id: string,
searchKey: string,
tag?: string
tag?: string,
privateForum?: boolean
) {
let query = db
.selectFrom('forums')
Expand All @@ -387,6 +391,10 @@ export async function getTotalCount(
)
}

if (privateForum) {
query = query.where('forums.private', '=', true)
}

if (tag.length) {
query = query.where(({ selectFrom, exists }) =>
exists(
Expand Down
4 changes: 4 additions & 0 deletions src/modules/Socket/SocketController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ export function emitNotification(id: string, payload: string) {
export function emitNotificationToAdmin(payload: string) {
io.emit('admin', payload)
}

export function emitNotificationToFarmHeads() {
io.emit('farm_head', 'CHAT_EVENT_TRIGGER')
}
17 changes: 16 additions & 1 deletion src/schema/ForumsSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import { z } from 'zod'
* type: string
* role:
* type: string
* district:
* type: string
* farm_name:
* type: string
* tags:
* type: array
* items:
Expand Down Expand Up @@ -79,14 +83,16 @@ import { z } from 'zod'
* required:
* - title
* - question
* - imagesrc
* properties:
* title:
* type: string
* description: The title of the forum entry
* question:
* type: string
* description: The question in the forum entry
* privateForum:
* type: boolean
* description: The question in the forum entry
* imagesrc:
* type: array
* items:
Expand Down Expand Up @@ -292,6 +298,11 @@ export const SearchForums = z.object({
filter: z.string().optional().default('newest'),
profile: z.string().optional().default(''),
tag: z.string().optional().default(''),
privateForum: z
.string()
.transform((arg) => Boolean(arg))
.optional()
.default(''),
}),
})
/**
Expand Down Expand Up @@ -346,6 +357,10 @@ export const ForumsSchema = z.object({
question: z
.string({ required_error: 'Question is required' })
.min(1, 'Question must not be empty'),
privateForum: z
.string()
.transform((arg) => Boolean(arg))
.optional(),
tags: z.union([z.array(z.string()), z.string()]).optional(),
}),
})
Expand Down
1 change: 1 addition & 0 deletions tables/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ CREATE TABLE forums(
question TEXT NOT NULL,
views INT DEFAULT 0,
imagesrc TEXT[],
private BOOLEAN default false,
createdAt timestamp DEFAULT CURRENT_TIMESTAMP,
updatedAt timestamp DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (userid) REFERENCES users(id) ON DELETE CASCADE
Expand Down

0 comments on commit e00d3aa

Please sign in to comment.