try to optimise routeAPI

This commit is contained in:
Azareal 2020-03-09 15:16:44 +10:00
parent b04d77d7b6
commit 28f210371e
18 changed files with 75 additions and 63 deletions

View File

@ -192,7 +192,7 @@ func buildAlertSb(sb *strings.Builder, msg string, sub []string, path, avatar st
sb.WriteRune('}')
}
func BuildAlertSb(sb *strings.Builder, a Alert, user User /* The current user */) (err error) {
func BuildAlertSb(sb *strings.Builder, a *Alert, user User /* The current user */) (err error) {
var targetUser *User
if a.Actor == nil {
a.Actor, err = Users.Get(a.ActorID)

View File

@ -46,7 +46,7 @@ function bindToAlerts() {
ev.stopPropagation();
ev.preventDefault();
$.ajax({
url: "/api/?action=set&module=dismiss-alert",
url: "/api/?a=set&m=dismiss-alert",
type: "POST",
dataType: "json",
data: { id: $(this).attr("data-asid") },
@ -137,7 +137,7 @@ function loadAlerts(menuAlerts, eTc = false) {
$.ajax({
type: 'get',
dataType: 'json',
url:'/api/?module=alerts' + tc,
url:'/api/?m=alerts' + tc,
success: (data) => {
if("errmsg" in data) {
setAlertError(menuAlerts,data.errmsg)
@ -150,7 +150,7 @@ function loadAlerts(menuAlerts, eTc = false) {
if(eTc && lastTc != 0) {
for(var i in data.msgs) wsAlertEvent(data.msgs[i]);
} else {*/
console.log("data:",data.count);
console.log("data",data);
for(var i in data.msgs) addAlert(data.msgs[i]);
alertCount = data.count;
updateAlertList(menuAlerts);
@ -311,7 +311,7 @@ function runWebSockets(resume = false) {
moreTopicBlock.classList.remove("more_topic_block_initial");
moreTopicBlock.classList.add("more_topic_block_active");
console.log("phraseBox:",phraseBox);
console.log("phraseBox",phraseBox);
let msgBox = moreTopicBlock.getElementsByClassName("more_topics")[0];
msgBox.innerText = phraseBox["topic_list"]["topic_list.changed_topics"].replace("%d",moreTopicCount);
}
@ -347,7 +347,7 @@ function runWebSockets(resume = false) {
console.log("before notify on alert")
// We can only get away with this because template_alert has no phrases, otherwise it too would have to be part of the "dance", I miss Go concurrency :(
if(!noAlerts) {
notifyOnScriptW("template_alert", (e) => {
notifyOnScriptW("template_alert", e => {
if(e!=undefined) console.log("failed alert? why?", e)
}, () => {
if(!Template_alert) throw("template function not found");
@ -424,8 +424,8 @@ function mainInit(){
moreTopicCount = 0;
})
$(".add_like,.remove_like").click(function(event) {
event.preventDefault();
$(".add_like,.remove_like").click(function(ev) {
ev.preventDefault();
//$(this).unbind("click");
let target = this.closest("a").getAttribute("href");
console.log("target", target);
@ -467,11 +467,11 @@ function mainInit(){
});
});
$(".link_label").click(function(event) {
event.preventDefault();
$(".link_label").click(function(ev) {
ev.preventDefault();
let linkSelect = $('#'+$(this).attr("data-for"));
if(!linkSelect.hasClass("link_opened")) {
event.stopPropagation();
ev.stopPropagation();
linkSelect.addClass("link_opened");
}
});
@ -902,10 +902,10 @@ function mainInit(){
$(".unix_to_24_hour_time").each(function(){
let unixTime = this.innerText;
let date = new Date(unixTime*1000);
console.log("date: ", date);
console.log("date", date);
let minutes = "0" + date.getMinutes();
let formattedTime = date.getHours() + ":" + minutes.substr(-2);
console.log("formattedTime:", formattedTime);
console.log("formattedTime", formattedTime);
this.innerText = formattedTime;
});
@ -913,10 +913,10 @@ function mainInit(){
// TODO: Localise this
let monthList = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
let date = new Date(this.innerText * 1000);
console.log("date: ", date);
console.log("date", date);
let day = "0" + date.getDate();
let formattedTime = monthList[date.getMonth()] + " " + day.substr(-2) + " " + date.getFullYear();
console.log("formattedTime:", formattedTime);
console.log("formattedTime", formattedTime);
this.innerText = formattedTime;
});
@ -939,7 +939,7 @@ function mainInit(){
$("#poll_results_" + pollID).removeClass("auto_hide");
fetch("/poll/results/" + pollID, {
credentials: 'same-origin'
}).then((response) => response.text()).catch((error) => console.error("Error:",error)).then((rawData) => {
}).then(resp => resp.text()).catch(err => console.error("err:",err)).then((rawData) => {
// TODO: Make sure the received data is actually a list of integers
let data = JSON.parse(rawData);
let allZero = true;

View File

@ -102,7 +102,7 @@ var imageExts = ["png", "jpg", "jpe","jpeg","jif","jfi","jfif", "svg", "bmp", "g
});
} catch(e) {
// TODO: Use a notice instead
console.log("e:",e);
console.log("e",e);
alert(e);
}
}
@ -142,7 +142,7 @@ var imageExts = ["png", "jpg", "jpe","jpeg","jif","jfi","jfif", "svg", "bmp", "g
});
} catch(e) {
// TODO: Use a notice instead
console.log("e:",e);
console.log("e",e);
alert(e);
}
}
@ -248,8 +248,8 @@ var imageExts = ["png", "jpg", "jpe","jpeg","jif","jfi","jfif", "svg", "bmp", "g
}
});
};
$(".mod_floater_submit").click(function(event){
event.preventDefault();
$(".mod_floater_submit").click(function(ev){
ev.preventDefault();
let selectNode = this.form.querySelector(".mod_floater_options");
let optionNode = selectNode.options[selectNode.selectedIndex];
let action = optionNode.getAttribute("value");
@ -265,12 +265,12 @@ var imageExts = ["png", "jpg", "jpe","jpeg","jif","jfi","jfif", "svg", "bmp", "g
let fid = this.getAttribute("data-fid");
if (fid == null) return;
this.classList.add("pane_selected");
console.log("fid:" + fid);
console.log("fid", fid);
forumToMoveTo = fid;
$("#mover_submit").unbind("click");
$("#mover_submit").click(function(event){
event.preventDefault();
$("#mover_submit").click(ev => {
ev.preventDefault();
bulkActionSender("move",selectedTopics,forumToMoveTo);
});
});
@ -284,7 +284,7 @@ var imageExts = ["png", "jpg", "jpe","jpeg","jif","jfi","jfif", "svg", "bmp", "g
function addPollInput() {
console.log("clicked on pollinputinput");
let dataPollInput = $(this).parent().attr("data-pollinput");
console.log("dataPollInput:", dataPollInput);
console.log("dataPollInput", dataPollInput);
if(dataPollInput == undefined) return;
if(dataPollInput != (pollInputIndex-1)) return;
$(".poll_content_row .formitem").append(Template_topic_c_poll_input({
@ -292,13 +292,13 @@ var imageExts = ["png", "jpg", "jpe","jpeg","jif","jfi","jfif", "svg", "bmp", "g
Place: phraseBox["topic"]["topic.reply_add_poll_option"].replace("%d",pollInputIndex),
}));
pollInputIndex++;
console.log("new pollInputIndex: ", pollInputIndex);
console.log("new pollInputIndex", pollInputIndex);
$(".pollinputinput").off("click");
$(".pollinputinput").click(addPollInput);
}
var pollInputIndex = 1;
$("#add_poll_button").click((ev) => {
$("#add_poll_button").click(ev => {
ev.preventDefault();
$(".poll_content_row").removeClass("auto_hide");
$("#has_poll_input").val("1");

View File

@ -140,13 +140,13 @@ func UploadAction(fname, path string, args ...string) *uploadAction {
return &uploadAction{route}
}
func (action *uploadAction) MaxSizeVar(varName string) *RouteImpl {
action.Route.LitBeforeMultiline(`err = c.HandleUploadRoute(w,req,user,` + varName + `)
func (a *uploadAction) MaxSizeVar(varName string) *RouteImpl {
a.Route.LitBeforeMultiline(`err = c.HandleUploadRoute(w,req,user,` + varName + `)
if err != nil {
return err
}`)
action.Route.Before("NoUploadSessionMismatch")
return action.Route
a.Route.Before("NoUploadSessionMismatch")
return a.Route
}
type RouteSet struct {

View File

@ -41,7 +41,7 @@ func routeAPI(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError
return c.PreErrorJS("Bad Form", w, r)
}
action := r.FormValue("action")
action := r.FormValue("a")
if action == "" {
action = "get"
}
@ -49,7 +49,7 @@ func routeAPI(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError
return c.PreErrorJS("Invalid Action", w, r)
}
switch r.FormValue("module") {
switch r.FormValue("m") {
// TODO: Split this into it's own function
case "dismiss-alert":
id, err := strconv.Atoi(r.FormValue("id"))
@ -104,12 +104,24 @@ func routeAPI(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError
return c.InternalErrorJS(err, w, r)
}
if count == 0 {
gzw, ok := w.(c.GzipResponseWriter)
if ok {
w = gzw.ResponseWriter
h := w.Header()
h.Del("Content-Type")
h.Del("Content-Encoding")
}
_, _ = io.WriteString(w, `{}`)
return nil
}
rCreatedAt, _ := strconv.ParseInt(r.FormValue("t"), 10, 64)
rCount, _ := strconv.Atoi(r.FormValue("c"))
//log.Print("rCreatedAt:", rCreatedAt)
//log.Print("rCount:", rCount)
var actors []int
var alerts []c.Alert
var alerts []*c.Alert
var createdAt time.Time
var topCreatedAt int64
@ -121,7 +133,7 @@ func routeAPI(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError
defer rows.Close()
for rows.Next() {
var al c.Alert
al := &c.Alert{}
err = rows.Scan(&al.ASID, &al.ActorID, &al.TargetUserID, &al.Event, &al.ElementType, &al.ElementID, &createdAt)
if err != nil {
return c.InternalErrorJS(err, w, r)
@ -142,7 +154,7 @@ func routeAPI(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError
}
}
if count == 0 || len(alerts) == 0 || (rCreatedAt != 0 && rCreatedAt >= topCreatedAt && count == rCount) {
if len(alerts) == 0 || (rCreatedAt != 0 && rCreatedAt >= topCreatedAt && count == rCount) {
gzw, ok := w.(c.GzipResponseWriter)
if ok {
w = gzw.ResponseWriter