Files
zulip/static/js/message_flags.js
Anders Kaseorg ac7b09d57e js: Convert _.map(a, …) to a.map(…).
And convert the corresponding function expressions to arrow style
while we’re here.

import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import K from "ast-types/gen/kinds";
import fs from "fs";
import path from "path";
import process from "process";

const checkExpression = (node: n.Node): node is K.ExpressionKind =>
  n.Expression.check(node);

for (const file of process.argv.slice(2)) {
  console.log("Parsing", file);
  const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
    parser: path.extname(file) === ".ts" ? tsParser : babelParser,
  });
  let changed = false;

  recast.visit(ast, {
    visitCallExpression(path) {
      const { callee, arguments: args } = path.node;
      if (
        n.MemberExpression.check(callee) &&
        !callee.computed &&
        n.Identifier.check(callee.object) &&
        callee.object.name === "_" &&
        n.Identifier.check(callee.property) &&
        callee.property.name === "map" &&
        args.length === 2 &&
        checkExpression(args[0]) &&
        checkExpression(args[1])
      ) {
        const [arr, fn] = args;
        path.replace(
          b.callExpression(b.memberExpression(arr, b.identifier("map")), [
            n.FunctionExpression.check(fn) ||
            n.ArrowFunctionExpression.check(fn)
              ? b.arrowFunctionExpression(
                  fn.params,
                  n.BlockStatement.check(fn.body) &&
                    fn.body.body.length === 1 &&
                    n.ReturnStatement.check(fn.body.body[0])
                    ? fn.body.body[0].argument || b.identifier("undefined")
                    : fn.body
                )
              : fn,
          ])
        );
        changed = true;
      }
      this.traverse(path);
    },
  });

  if (changed) {
    console.log("Writing", file);
    fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
  }
}

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-10 14:08:12 -08:00

130 lines
3.8 KiB
JavaScript

function send_flag_update(message, flag, op) {
channel.post({
url: '/json/messages/flags',
idempotent: true,
data: {
messages: JSON.stringify([message.id]),
flag: flag,
op: op,
},
});
}
exports._unread_batch_size = 1000;
exports.send_read = (function () {
let queue = [];
let on_success;
let start;
function server_request() {
// Wait for server IDs before sending flags
const real_msgs = _.filter(queue, function (msg) {
return !msg.locally_echoed;
});
const real_msg_ids = real_msgs.map(msg => msg.id);
if (real_msg_ids.length === 0) {
setTimeout(start, 100);
return;
}
const real_msg_ids_batch = real_msg_ids.slice(0, exports._unread_batch_size);
// We have some real IDs. If there are any left in the queue when this
// call finishes, they will be handled in the success callback.
channel.post({
url: '/json/messages/flags',
idempotent: true,
data: {messages: JSON.stringify(real_msg_ids_batch),
op: 'add',
flag: 'read'},
success: on_success,
});
}
start = _.throttle(server_request, 1000);
on_success = function on_success(data) {
if (data === undefined || data.messages === undefined) {
return;
}
queue = _.filter(queue, function (message) {
return !data.messages.includes(message.id);
});
if (queue.length > 0) {
start();
}
};
function add(messages) {
queue = queue.concat(messages);
start();
}
return add;
}());
exports.save_collapsed = function (message) {
send_flag_update(message, 'collapsed', 'add');
};
exports.save_uncollapsed = function (message) {
send_flag_update(message, 'collapsed', 'remove');
};
// This updates the state of the starred flag in local data
// structures, and triggers a UI rerender.
exports.update_starred_flag = function (message_id, new_value) {
const message = message_store.get(message_id);
if (message === undefined) {
// If we don't have the message locally, do nothing; if later
// we fetch it, it'll come with the correct `starred` state.
return;
}
message.starred = new_value;
ui.update_starred_view(message_id, new_value);
};
exports.toggle_starred_and_update_server = function (message) {
if (message.locally_echoed) {
// This is defensive code for when you hit the "*" key
// before we get a server ack. It's rare that somebody
// can star this quickly, and we don't have a good way
// to tell the server which message was starred.
return;
}
message.starred = !message.starred;
// Unlike most calls to mark messages as read, we don't check
// msg_list.can_mark_messages_read, because starring a message is an
// explicit interaction and we'd like to preserve the user
// expectation invariant that all starred messages are read.
unread_ops.notify_server_message_read(message);
ui.update_starred_view(message.id, message.starred);
if (message.starred) {
send_flag_update(message, 'starred', 'add');
starred_messages.add([message.id]);
} else {
send_flag_update(message, 'starred', 'remove');
starred_messages.remove([message.id]);
}
};
exports.unstar_all_messages = function () {
const starred_msg_ids = starred_messages.get_starred_msg_ids();
channel.post({
url: '/json/messages/flags',
idempotent: true,
data: {
messages: JSON.stringify(starred_msg_ids),
flag: 'starred',
op: 'remove',
},
});
};
window.message_flags = exports;