Implemented adaptive fetching using IDEC extensions

This commit is contained in:
Luxferre
2024-10-25 16:12:04 +03:00
parent 7f8b07a9e5
commit dc2df5265f
+30 -2
View File
@@ -154,7 +154,9 @@ proc getfile {url} {
} }
https - http { https - http {
set hs [::http::geturl $url -binary 1 -keepalive 1 -timeout 5000] set hs [::http::geturl $url -binary 1 -keepalive 1 -timeout 5000]
if {[::http::ncode $hs] eq "200"} {
return [::http::data $hs] return [::http::data $hs]
} else {return {}}
} }
default {return {}} default {return {}}
} }
@@ -211,16 +213,41 @@ proc fetchiidb {url echos dbfile dolog maxids} {
set echos [split $echos "/,;"] set echos [split $echos "/,;"]
} }
if {$dolog eq 1} {puts "Echos to fetch: $echos"} if {$dolog eq 1} {puts "Echos to fetch: $echos"}
# pass the echo list and fetch the message IDs
set echos [lmap s $echos {string trim $s}] set echos [lmap s $echos {string trim $s}]
# get the IDEC extended feature list
set featurelist ""
catch {set featurelist [getfile [string cat $url "/x/features"]]}
set featurelist [lmap s [split $featurelist \n] {string trim $s}]
set datalines ""
if {([lsearch $featurelist x/c] > -1 ) && ([lsearch $featurelist u/e] > -1)} {
# get message count in every echo of our choice
set countdata [getfile [string cat $url "/x/c/" [join $echos "/"]]]
foreach cd $countdata {
set cdparts [split $cd :]
if {[llength $cdparts] > 1} {
set ename [lindex $cdparts 0]
set rcount [lindex $cdparts 1]
set lcount [msgdb eval {SELECT COUNT (DISTINCT `id`) FROM `msg` WHERE `echoname` = $ename;}]
# get the difference between remote and local counts
set diff [expr {$rcount - $lcount}]
if {$diff > 0} { # we have something to fetch
set localdata [getfile [string cat $url "/u/e/" $ename "/-$diff:$diff"]]
append datalines [string trim $localdata] \n
}
}
}
} else { # no extended feature support, pass the echo list and fetch the message IDs
set echodata [getfile [string cat $url "/u/e/" [join $echos "/"]]] set echodata [getfile [string cat $url "/u/e/" [join $echos "/"]]]
set datalines [split $echodata \n]
}
# iterate over the fetched data and fetch corresponding messages # iterate over the fetched data and fetch corresponding messages
set curecho "" set curecho ""
set datalines [split $echodata "\n"]
set echomap "" set echomap ""
# build the map of lists of message IDs # build the map of lists of message IDs
foreach line $datalines { foreach line $datalines {
set line [string trim $line] set line [string trim $line]
if {$line ne ""} {
# detect if the line is related to echo name or message ID # detect if the line is related to echo name or message ID
if {[string first "." $line] eq -1} { # message ID if {[string first "." $line] eq -1} { # message ID
if {[string length $line] == 20} { # filter out invalid IDs if {[string length $line] == 20} { # filter out invalid IDs
@@ -233,6 +260,7 @@ proc fetchiidb {url echos dbfile dolog maxids} {
dict set echomap $curecho "" dict set echomap $curecho ""
} }
} }
}
if {$dolog eq 1} {puts "Echomap built"} if {$dolog eq 1} {puts "Echomap built"}
# pass the echo list and fetch the message IDs # pass the echo list and fetch the message IDs
# now, process the map we've built # now, process the map we've built