rbs@u2
Home
Php_u2
Html #1
Html #2
Html #3
Html #4
JS #1
JS #2
JS #3
JS #4
JsApi #1
JsApi #2
JsApi #3
JsApi #4
JsApi_u4
all-files
ori-v2
master
user
Logout
dsg
tool css
bootstrtap 5.3
auth
profile
waste
waste_group
venue
vehicle
event
comment
notification
discover
post
scanner
to_delete
const fetch = require('node-fetch'); async function post(v) { let url=`${v.api}/comment/add-post-comment/${v.param}`; console.log(url) const res = await fetch(url,{ method: 'POST', body: JSON.stringify(v.body), headers: { 'Content-Type': 'application/json', } }); return res } async function post(v) { let url=`${v.api}/comment/delete-comment/${v.param}`; console.log(url) const res = await fetch(url,{ method: 'POST', body: JSON.stringify(v.body), headers: { 'Content-Type': 'application/json', } }); return res } async function post(v) { let url=`${v.api}/comment/get-comments/${v.param}`; console.log(url) const res = await fetch(url,{ method: 'POST', body: JSON.stringify(v.body), headers: { 'Content-Type': 'application/json', } }); return res } async function post(v) { let url=`${v.api}/comment/toggle-like-comment/${v.param}`; console.log(url) const res = await fetch(url,{ method: 'POST', body: JSON.stringify(v.body), headers: { 'Content-Type': 'application/json', } }); return res } const comment = { post, post, post, post, } module.exports = comment
Save
post_id; $res=mysqli_query($db,$sql); $sql4 = "select id from `comments` order by id desc limit 1"; $res4 = mysqli_query($db, $sql4); $rw4 = mysqli_fetch_assoc($res4); $id2=$rw4['id']+1; $idt=array( 'id'=>$id2, 'post_id'=>$js->post_id, 'user_id'=>$js->user_id, 'comment'=>$js->comment, 'comment_date'=>date("Y-m-d H:i:s"), 'user_id'=>$js->user_id, ); if($js->parent_id>0) { $idt['parent_id']=$js->parent_id; } Insert($db,"comments",$idt,0); //$dt['success']=true; $dt['message']="Comment added"; return $dt; } function delete_comment($js){ global $ar,$db; $dt=array(); //{"user_id":"3","comment_id":"5"}; $sql="SELECT `post_id` from `post_comments` where id=".$js->comment_id; //$dt['sql']=$sql; $res=mysqli_query($db,$sql); if(mysqli_num_rows($res)>0) { $r=mysqli_fetch_assoc($res); $sql2="UPDATE posts set comments_count=comments_count-1 where id=".$r['post_id']; $res2=mysqli_query($db,$sql2); Delete($db,"post_comments","id=".$js->comment_id,0); Delete($db,"user_comment_liked","comment_id=".$js->comment_id,0); $dt['success']=true; $dt['message']="Comment deleted successfully"; } return $dt; } function get_comments($js){ global $ar,$db; $dt=array(); //{"user_id":67032,"type":"posts","item_id":"181"} $sql="SELECT id,comment,likes_count,create_at from `comments` where modul='".$js->type."' and item_id=".$js->item_id; //echo $sql8."
"; $res=mysqli_query($db,$sql); $c=0; //$dt['sql'][1]=$sql; //$dt['jum'][1]=mysqli_num_rows($res); while($r1 = mysqli_fetch_assoc($res)) { $sql2="SELECT email,first_name,id,last_name,profile_image,username,name,create_at from `users` where id=".$r1['user_id']; $res2=mysqli_query($db,$sql2); $r2 = mysqli_fetch_assoc($res2); //$dt['sql'][2]=$sql2; //$dt['jum'][2]=mysqli_num_rows($res2); $dt[$c]=$r1; $dt[$c]['create_at']=date("Y-m-d H:i:s",$r1['create_at']); $dt[$c]['display_name']=$r2['name']; $dt[$c]['user_email']=$r2['email']; $dt[$c]['user_login']=$r2['username']; $dt[$c]['user_registered']=date("Y-m-d H:i:s",$r2['create_at']); $dt[$c]['profile_image']=$r2['profile_image']; $dt[$c]['replies']=[]; $c++; } return $dt; } function toggle_like_comment($js){ global $ar,$db; $dt=array(); //{"user_id":67032,"comment_id":"57","owner_id":"4072"} $sql="UPDATE post_commments set likes_count=likes_count+1 where id=".$js->comment_id; $res=mysqli_query($db,$sql); $idt=array( 'comment_id'=>$js->comment_id, 'user_id'=>$js->user_id, 'owner_id'=>$js->owner_id, ); $udt=array( 'user_id'=>$js->user_id, ); InsertUpdate($db,"user_comment_liked",$idt,$udt,0); //$dt['success']=true; $dt['message']="Comment liked"; return $dt; } ?>
$id2, 'item_id'=>$js->item_id, 'user_id'=>$js->user_id, 'comment'=>$js->comment, 'create_at'=>time(), 'modul'=>$js->modul, ); if(isset($js->parent_id) && $js->parent_id>0) { $idt['parent_id']=$js->parent_id; } Insert($db,"comments",$idt,0); //$dt['success']=true; $dt['message']="Comment added"; return $dt; } function delete_comment($js){ global $ar,$db; $dt=array(); //{"user_id":"3","comment_id":"5"}; $sql="SELECT * from `comments` where id=".$js->comment_id." and user_id=".$js->user_id; //$dt['sql']=$sql; $res=mysqli_query($db,$sql); if(mysqli_num_rows($res)>0) { $r=mysqli_fetch_assoc($res); $sql2="UPDATE `".$ar['modul_table'][$r['modul']]."` set comments_count=comments_count-1 where id=".$r['item_id']; $res2=mysqli_query($db,$sql2); Delete($db,"comments","id=".$js->comment_id,0); Delete($db,"user_comment_liked","comment_id=".$js->comment_id,0); $dt['success']=true; $dt['message']="Comment deleted successfully"; } return $dt; } function get_comments($js){ global $ar,$db; $dt=array(); //{"user_id":67032,"modul":"posts","item_id":"181"} $sql="SELECT * from `comments` where `modul`='".$js->modul."' and item_id=".$js->item_id; //echo $sql."
"; $res=mysqli_query($db,$sql); $c=0; //$dt['sql'][1]=$sql; //$dt['jum'][1]=mysqli_num_rows($res); while($r1 = mysqli_fetch_assoc($res)) { $sql2="SELECT email,first_name,id,last_name,profile_image,username,name,create_at,id from `users` where id=".$r1['user_id']; $res2=mysqli_query($db,$sql2); $r2 = mysqli_fetch_assoc($res2); //$dt['sql'][2]=$sql2; //$dt['jum'][2]=mysqli_num_rows($res2); $dt[$c]=$r1; $dt[$c]['create_at']=date("Y-m-d H:i:s",$r1['create_at']); $dt[$c]['display_name']=$r2['name']; $dt[$c]['user_email']=$r2['email']; $dt[$c]['user_login']=$r2['username']; $dt[$c]['user_registered']=date("Y-m-d H:i:s",$r2['create_at']); $dt[$c]['profile_image']=$r2['profile_image']; $dt[$c]['replies']=[]; $c++; } return $dt; } function like_comment($js){ global $ar,$db; $dt=array(); //{"user_id":67032,"comment_id":"57","owner_id":"4072"} $sql="SELECT * from user_comment_liked where comment_id=".$js->comment_id." and user_id=".$js->user_id; $res=mysqli_query($db,$sql); if(mysqli_num_rows($res)>0) { Delete($db,"user_comment_liked","comment_id=".$js->comment_id." and user_id=".$js->user_id,0); $sql3="UPDATE `comments` set likes_count=likes_count-1 where id=".$js->comment_id; $res3=mysqli_query($db,$sql3); $dt['message']="Comment un-liked"; } else { $idt=array( 'comment_id'=>$js->comment_id, 'user_id'=>$js->user_id, ); Insert($db,"user_comment_liked",$idt,0); $sql3="UPDATE `comments` set likes_count=likes_count+1 where id=".$js->comment_id; $res3=mysqli_query($db,$sql3); $dt['message']="Comment liked"; } return $dt; } ?>
Save
export const addComment = async (postId, comment, comment_id = null) => { const controller = new AbortController() const signal = controller.signal try { const user = await getSessionUser() if (!user) return setTimeout(() => { controller.abort() }, TIMEOUT_MS_LOW) const response = await fetch(`${API_URL}/wp-json/app/v1/add-post-comment`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ user_id: user.id, post_id: postId, comment, parent_id: comment_id }), signal }) const data = await response.json() return data } catch (error) { if (error.name === 'AbortError') { throw { message: "Failed to add comment, your connection timed out", name: "TimeOutError" }; } else { throw error; // Rethrow any other errors } } } export const deleteComment = async (commentId) => { const user = await getSessionUser() if (!user) return const response = await fetch(`${API_URL}/wp-json/app/v1/delete-post-comment`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ user_id: user.id, comment_id: commentId }), }) const data = await response.json() return data } export async function fetchComments(postId) { const controller = new AbortController() const signal = controller.signal try { const user = await getSessionUser() if (!user) return setTimeout(() => { controller.abort() }, TIMEOUT_MS_LOW) const response = await fetch(`${API_URL}/wp-json/app/v1/get-post-comments`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ user_id: user.id, post_id: postId }), signal }) const data = await response.json() return data } catch (error) { if (error.name === 'AbortError') { throw { message: "Failed to fetch comments, your connection timed out", name: "TimeOutError" }; } else { throw error; // Rethrow any other errors } } } export const maybeLikeComment = async (commentId, ownerId) => { const user = await getSessionUser() if (!user) return try { const response = await fetch(`${API_URL}/wp-json/app/v1/toggle-like-comment`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ user_id: user.id, comment_id: commentId, owner_id: ownerId }), }) const data = await response.json() if (!response.ok || response.status !== 200) { throw new Error(data.message) } return data } catch (e) { console.error("Error liking comment") throw new Error(e.message) } }
import { API_URL, TIMEOUT_MS_HIGHER } from "./consts.js" import { getSessionUser } from "./auth.js"; export const addComment = async (itemtId, modul, comment, comment_id = null) => { try { const user = await getSessionUser() if (!user) return setTimeout(() => { controller.abort() }, TIMEOUT_MS_LOW) const response = await fetch(`${API_URL}/comment/add-post-comment`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ user_id: user.id, item_id: itemId, modul: modul, comment, parent_id: comment_id }) }) const data = await response.json() return data } catch (error) { if (error.name === 'AbortError') { throw { message: "Failed to add comment, your connection timed out", name: "TimeOutError" }; } else { throw error; // Rethrow any other errors } } }; export const deleteComment = async (commentId) => { const user = await getSessionUser() if (!user) return const response = await fetch(`${API_URL}/comment/delete-comment`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ user_id: user.id, comment_id: commentId }), }) const data = await response.json() return data }; export async function getComments(itemId, modul) { try { const user = await getSessionUser() if (!user) return setTimeout(() => { controller.abort() }, TIMEOUT_MS_LOW) const response = await fetch(`${API_URL}/comment/get-comments`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ user_id: user.id, modul: modul, item_id: itemId }) }) const data = await response.json() return data } catch (error) { if (error.name === 'AbortError') { throw { message: "Failed to fetch comments, your connection timed out", name: "TimeOutError" }; } else { throw error; // Rethrow any other errors } } }; export const LikeComment = async (commentId, ownerId) => { const user = await getSessionUser() if (!user) return try { const response = await fetch(`${API_URL}/comment/like-comment`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ user_id: user.id, comment_id: commentId, owner_id: ownerId }), }) const data = await response.json() if (!response.ok || response.status !== 200) { throw new Error(data.message) } return data } catch (e) { console.error("Error liking comment") throw new Error(e.message) } };
Save
comment
-- add-post-comment
-- delete-comment
-- get-comments
-- toggle-like-comment