Skip to content

Commit

Permalink
persistent unreads work. Changed query_limit to QUERY_LIMIT to better…
Browse files Browse the repository at this point in the history
… indicate that it's a magic number constant. Added threadUUID and unreads attributes to ThreadPreviewInterface and ThreadPreview component props. added last seen lookup and count since last seen functionality to loadpreviews and searchpreviews.
  • Loading branch information
Ian committed Jun 14, 2024
1 parent 0c80f1b commit 70e13ab
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 16 deletions.
12 changes: 9 additions & 3 deletions frontend/components/ThreadPreview/ThreadPreview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ export type ThreadPreviewInterface = {
},
threadUUID:{
type:String
}
},
unreads:{
type:number
},
}
Expand Down Expand Up @@ -79,12 +82,15 @@ export default {
},
threadUUID:{
type:String
}
},
unreads:{
type:Number
},
},
components: { Conversation },
data() {
return{
newMessages:0
newMessages:this.$props.newMessages
}
},
computed: {
Expand Down
4 changes: 2 additions & 2 deletions frontend/lib/backfillPreviews.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MessageData, emitter, state , addPreview, query_limit} from './global';
import { MessageData, emitter, state , addPreview, QUERY_LIMIT} from './global';


type threadPreviewQuery = {
Expand Down Expand Up @@ -113,7 +113,7 @@ export async function loadPreviews(extensionUUID:string, older_than:string) {
fetching = false;
console.log('load preview error:', e);
}finally{
if(temp.length< query_limit){
if(temp.length< QUERY_LIMIT){
emitter.emit("no-more-previews");
}
//console.log(temp);
Expand Down
4 changes: 2 additions & 2 deletions frontend/lib/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type GlobalState = {
page: number,
oldestMessage: Number,
};
const query_limit = 20; //this limits the nubmer of threadpreview results per load request
const QUERY_LIMIT = 20; //this limits the nubmer of threadpreview results per load request

const state = reactive<GlobalState>({
conversations: {},
Expand Down Expand Up @@ -142,4 +142,4 @@ function updateOldestMessage(newOldestTimestamp: Number){
//console.log(state.oldestMessage)
return state.oldestMessage;
}
export { state, emitter, query_limit, MessageData, GlobalState, ThreadChangePayload, addMessage, addPreview, updatePageNumber }
export { state, emitter, QUERY_LIMIT, MessageData, GlobalState, ThreadChangePayload, addMessage, addPreview, updatePageNumber }
10 changes: 5 additions & 5 deletions last_seen.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@
$row_exists = $database->select($sql, $parameters, 'all');

if($row_exists){
$sql = "UPDATE webtexting_threads_last_seen SET last_seen_timestamp = NOW() WHERE extension_uuid = :extension_uuid AND thread_uuid = :thread_uuid AND domain_uuid = :domain_uuid ;";
$sql = "UPDATE webtexting_threads_last_seen SET timestamp = NOW() WHERE extension_uuid = :extension_uuid AND thread_uuid = :thread_uuid AND domain_uuid = :domain_uuid ;";
$parameters["extension_uuid"] = $extension['extension_uuid'];
$parameters["thread_uuid"] = $_GET['thread_uuid'];
$parameters["domain_uuid"] = $domain_uuid;
$updated_last_seen = $database->execute($sql, $parameters);
if($updated_last_seen){
//succeeded
echo("Update Success");
echo(json_encode(["Update Success",$updated_last_seen]));
}
else{
//failed
echo("Update Failed");
echo(json_encode(["Update Failed",$updated_last_seen]));
}
}
//else
Expand All @@ -53,11 +53,11 @@
$insert_last_seen = $database->execute($sql, $parameters);
if($insert_last_seen){
//succeeded
//echo("New User_Thread Creation Success");
echo(json_encode(["New User_Thread Creation Success",$insert_last_seen]));
}
else{
//failed
//echo("New User_Thread Creation Failed");
echo(json_encode(["New User_Thread Creation Failed",$insert_last_seen]));

}
}
Expand Down
42 changes: 39 additions & 3 deletions loadpreviews.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@
}
//this is for Limiting the list of threads on initial load

$query_limit = 20;
$QUERY_LIMIT = 20;


$sql = 'SELECT * FROM webtexting_threads WHERE domain_uuid = :domain_uuid AND local_number = :local_number ';
if($_GET['older_than'] ){
$sql .= 'AND last_message < to_timestamp(:older_than/1000.00)';
}
$sql.= ' ORDER BY last_message DESC LIMIT :query_limit ;';
$sql.= ' ORDER BY last_message DESC LIMIT :QUERY_LIMIT ;';
$parameters['domain_uuid'] = $domain_uuid;
$parameters['local_number'] = $ownNumber;
if($_GET['older_than'] ){
$parameters['older_than'] = $_GET['older_than'];
}
$parameters['query_limit'] = $query_limit;
$parameters['QUERY_LIMIT'] = $QUERY_LIMIT;

$conversations = $database->select($sql,$parameters, 'all');
unset($parameters);
Expand Down Expand Up @@ -238,6 +238,7 @@
$i++;
}
// //this is where we fetch and attach the bodyPreviews
// this is also where we may want to do our initial unread notification calculation
$i=0;
foreach($threadPreviews as $preview){
$sql = "SELECT * FROM webtexting_messages WHERE extension_uuid = :extension_uuid AND domain_uuid = :domain_uuid AND ";
Expand All @@ -261,6 +262,41 @@

}
$last_message = false;
if($preview['threadUUID'] ){

$sql = "SELECT timestamp from webtexting_threads_last_seen WHERE extension_uuid = :extension_uuid AND domain_uuid = :domain_uuid AND thread_uuid = :thread_uuid;";
$parameters['extension_uuid'] = $extension['extension_uuid'];
$parameters['domain_uuid'] = $domain_uuid;
$parameters['thread_uuid'] = $preview['threadUUID'] ;
$last_seen_stamp = $database->select($sql, $parameters, 'row');
unset($parameters);
if($last_seen_stamp['timestamp']){
//here is where we would want to prepare and execute the count since last seen query
$sql = "SELECT COUNT(*) from webtexting_messages WHERE extension_uuid = :extension_uuid AND domain_uuid = :domain_uuid AND
(start_stamp BETWEEN :last_seen_stamp AND NOW()) AND ";
if ($preview['groupUUID'] != null) {
$sql .= "group_uuid = :group_uuid AND (to_number = :own_number AND NOT (from_number = to_number));";
$parameters['own_number'] = $ownNumber;
$parameters['group_uuid'] = $preview['groupUUID'];
} else {
$sql .= "(to_number = :own_number AND from_number = :remote_number AND group_uuid IS NULL)";
$parameters['own_number'] = $ownNumber;
$parameters['remote_number'] = $preview['remoteNumber'];
}
$parameters['extension_uuid'] = $extension['extension_uuid'];
$parameters['domain_uuid'] = $domain_uuid;
$parameters['last_seen_stamp'] = $last_seen_stamp['timestamp'];
$count_since_last_seen = $database->select($sql, $parameters, 'row');
unset($parameters);

if($count_since_last_seen['count']){
$threadPreviews[$i]['newMessages'] = $count_since_last_seen['count'];
}
$count_since_last_seen = 0;
}
$last_seen_stamp = false;

}
$i++;
}
}
Expand Down
50 changes: 49 additions & 1 deletion searchpreviews.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
$threadPreviews[$z]['remoteNumber'] = $solo['phone_number'];
$threadPreviews[$z]['displayName']= $solo['phone_number'];
$threadPreviews[$z]['contactEditLink'] = "/app/contacts/contact_edit.php?id=".$solo['contact_uuid'];

$threadPreviews[$z]['threadUUID'] = $solo['thread_uuid'];

$threadPreviews[$z]['link'] = "thread.php?extension_uuid=".$extension['extension_uuid']."&number=".$solo['phone_number'];
$threadPreviews[$z]['ownNumber'] = $ownNumber;
$threadPreviews[$z]['timestamp'] = $solo['last_message'];
Expand Down Expand Up @@ -126,6 +127,8 @@
foreach ($groups as $group) {
$threadPreviews[$z]['groupUUID'] = $group['group_uuid'];
$threadPreviews[$z]['groupMembers'] = $group['members'];
$threadPreviews[$z]['threadUUID'] = $group['thread_uuid'];

if ($group['name'] != null) {
$display_name = $group['name'];
} else {
Expand Down Expand Up @@ -209,6 +212,8 @@
$usedNumbers[$local_to_remote['remote_number']] = true;
$threadPreviews[$z]['remoteNumber'] = $local_to_remote['remote_number'];
$threadPreviews[$z]['displayName']= $local_to_remote['remote_number'];
$threadPreviews[$z]['threadUUID'] = $local_to_remote['thread_uuid'];

$threadPreviews[$z]['contactEditLink'] = "/app/contacts/contact_edit.php?id=".$local_to_remote['contact_uuid'];
$threadPreviews[$z]['link'] = "thread.php?extension_uuid=".$extension['extension_uuid']."&number=".$local_to_remote['remote_number'];
$threadPreviews[$z]['ownNumber'] = $ownNumber;
Expand All @@ -221,6 +226,7 @@


//this is where we fetch and attach the bodyPreviews
//we could modify this to provide our initial unread message count as well
$z=0;
foreach($threadPreviews as $preview){
$sql = "SELECT * FROM webtexting_messages WHERE extension_uuid = :extension_uuid AND domain_uuid = :domain_uuid AND ";
Expand All @@ -244,6 +250,48 @@

}
$last_message = false;
//this is where we find out when the user last accessed each thread
//If they've opened a thread before they have a thread_uuid
//else they haven't and we don't need to calculate unreads
if($threadPreviews[$z]['threadUUID']){

$sql = "SELECT timestamp from webtexting_threads_last_seen WHERE extension_uuid = :extension_uuid AND domain_uuid = :domain_uuid AND thread_uuid = :thread_uuid;";
$parameters['extension_uuid'] = $extension['extension_uuid'];
$parameters['domain_uuid'] = $domain_uuid;
$parameters['thread_uuid'] = $threadPreviews[$z]['threadUUID'];
$last_seen_stamp = $database->select($sql, $parameters, 'row');
unset($parameters);

if($last_seen_stamp['timestamp']){
//here is where we would want to prepare and execute the count since last seen query
$sql = "SELECT COUNT(*) from webtexting_messages WHERE extension_uuid = :extension_uuid AND domain_uuid = :domain_uuid AND
(start_stamp BETWEEN :last_seen_stamp AND NOW()) AND ";
if ($preview['groupUUID'] != null) {
$sql .= "group_uuid = :group_uuid AND (to_number = :own_number AND NOT (from_number = to_number));";
$parameters['own_number'] = $ownNumber;
$parameters['group_uuid'] = $preview['groupUUID'];
} else {
$sql .= "(to_number = :own_number AND from_number = :remote_number AND group_uuid IS NULL)";
$parameters['own_number'] = $ownNumber;
$parameters['remote_number'] = $preview['remoteNumber'];
}
$parameters['extension_uuid'] = $extension['extension_uuid'];
$parameters['domain_uuid'] = $domain_uuid;
$parameters['last_seen_stamp'] = $last_seen_stamp['timestamp'];
$count_since_last_seen = $database->select($sql, $parameters, 'row');
unset($parameters);

if($count_since_last_seen['count']){
$threadPreviews[$z]['newMessages'] = $count_since_last_seen['count'];
}
$count_since_last_seen = 0;
}
$last_seen_stamp = false;

}



$z++;
}
echo(json_encode($threadPreviews));
Expand Down

0 comments on commit 70e13ab

Please sign in to comment.