Skip to content

Commit

Permalink
Remove macOS inline replies feature.
Browse files Browse the repository at this point in the history
node-mac-notifier no longer builds on macOS with Electron 11 (error:
no template named 'remove_cv_t' in namespace 'std').  It was
previously implicated in crashes on macOS (zulip#1016).  And we no longer
have any macOS developers that seem to be maintaining this
feature (e.g. zulip#1022 is stalled).

Signed-off-by: Anders Kaseorg <[email protected]>
  • Loading branch information
andersk committed Dec 2, 2020
1 parent fe86315 commit 9041b06
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 289 deletions.
15 changes: 2 additions & 13 deletions app/renderer/js/injected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ interface CompatElectronBridge extends ElectronBridge {
(() => {
const zulipWindow = window as typeof window & {
electron_bridge: CompatElectronBridge;
narrow?: {
by_subject?: (target_id: number, options: {trigger?: string}) => void;
by_topic?: (target_id: number, options: {trigger?: string}) => void;
};
page_params?: unknown;
raw_electron_bridge: ElectronBridge;
};
Expand Down Expand Up @@ -48,16 +44,9 @@ interface CompatElectronBridge extends ElectronBridge {
});
}

const {narrow, page_params} = zulipWindow;
const {page_params} = zulipWindow;
if (page_params !== undefined) {
electron_bridge.send_event('zulip-loaded', {
narrow_by_topic: (id: number) => {
const narrowByTopic = narrow?.by_topic ?? narrow?.by_subject;
if (narrowByTopic !== undefined) {
narrowByTopic(id, {trigger: 'notification'});
}
}
});
electron_bridge.send_event('zulip-loaded');
}
})();

Expand Down
113 changes: 0 additions & 113 deletions app/renderer/js/notification/darwin-notifications.ts

This file was deleted.

123 changes: 0 additions & 123 deletions app/renderer/js/notification/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,8 @@
import {remote} from 'electron';

import Logger from '../utils/logger-util';

const logger = new Logger({
file: 'errors.log',
timestamp: true
});

// Do not change this
export const appId = 'org.zulip.zulip-electron';

export type BotListItem = [string, string];
const botsList: BotListItem[] = [];
let botsListLoaded = false;

// This function load list of bots from the server
// in case botsList isn't already completely loaded when required in parseRely
export async function loadBots(): Promise<void> {
botsList.length = 0;
const response = await fetch('/json/users');
if (response.ok) {
const {members} = await response.json();
members.forEach(({is_bot, full_name}: Record<string, unknown>) => {
if (is_bot && typeof full_name === 'string') {
const bot = `@${full_name}`;
const mention = `@**${bot.replace(/^@/, '')}**`;
botsList.push([bot, mention]);
}
});
botsListLoaded = true;
} else {
logger.log('Load bots request failed: ', await response.text());
logger.log('Load bots request status: ', response.status);
}
}

export function checkElements(...elements: unknown[]): boolean {
let status = true;
elements.forEach(element => {
if (element === null || element === undefined) {
status = false;
}
});
return status;
}

export function customReply(reply: string): void {
// Server does not support notification reply yet.
const buttonSelector = '.messagebox #send_controls button[type=submit]';
const messageboxSelector = '.selected_message .messagebox .messagebox-border .messagebox-content';
const textarea: HTMLInputElement = document.querySelector('#compose-textarea');
const messagebox: HTMLButtonElement = document.querySelector(messageboxSelector);
const sendButton: HTMLButtonElement = document.querySelector(buttonSelector);

// Sanity check for old server versions
const elementsExists = checkElements(textarea, messagebox, sendButton);
if (!elementsExists) {
return;
}

textarea.value = reply;
messagebox.click();
sendButton.click();
}

const currentWindow = remote.getCurrentWindow();
const webContents = remote.getCurrentWebContents();
const webContentsId = webContents.id;
Expand All @@ -73,65 +12,3 @@ const webContentsId = webContents.id;
export function focusCurrentServer(): void {
currentWindow.webContents.send('focus-webview-with-id', webContentsId);
}

// This function parses the reply from to notification
// making it easier to reply from notification eg
// @username in reply will be converted to @**username**
// #stream in reply will be converted to #**stream**
// bot mentions are not yet supported
export async function parseReply(reply: string): Promise<string> {
const usersDiv = document.querySelectorAll('#user_presences li');
const streamHolder = document.querySelectorAll('#stream_filters li');

type UsersItem = BotListItem;
type StreamsItem = BotListItem;
const users: UsersItem[] = [];
const streams: StreamsItem[] = [];

usersDiv.forEach(userRow => {
const anchor = userRow.querySelector('span a');
if (anchor !== null) {
const user = `@${anchor.textContent.trim()}`;
const mention = `@**${user.replace(/^@/, '')}**`;
users.push([user, mention]);
}
});

streamHolder.forEach(stream => {
const streamAnchor = stream.querySelector('div a');
if (streamAnchor !== null) {
const streamName = `#${streamAnchor.textContent.trim()}`;
const streamMention = `#**${streamName.replace(/^#/, '')}**`;
streams.push([streamName, streamMention]);
}
});

users.forEach(([user, mention]) => {
if (reply.includes(user)) {
const regex = new RegExp(user, 'g');
reply = reply.replace(regex, mention);
}
});

streams.forEach(([stream, streamMention]) => {
const regex = new RegExp(stream, 'g');
reply = reply.replace(regex, streamMention);
});

// If botsList isn't completely loaded yet, make a request for list
if (!botsListLoaded) {
await loadBots();
}

// Iterate for every bot name and replace in reply
// @botname with @**botname**
botsList.forEach(([bot, mention]) => {
if (reply.includes(bot)) {
const regex = new RegExp(bot, 'g');
reply = reply.replace(regex, mention);
}
});

reply = reply.replace(/\\n/, '\n');
return reply;
}
8 changes: 1 addition & 7 deletions app/renderer/js/notification/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ const {app} = remote;
// On windows 8 we have to explicitly set the appUserModelId otherwise notification won't work.
app.setAppUserModelId(appId);

let Notification = DefaultNotification;

if (process.platform === 'darwin') {
Notification = require('./darwin-notifications');
}

export interface NotificationData {
close: () => void;
title: string;
Expand All @@ -39,7 +33,7 @@ export function newNotification(
options: NotificationOptions | undefined,
dispatch: (type: string, eventInit: EventInit) => boolean
): NotificationData {
const notification = new Notification(title, options);
const notification = new DefaultNotification(title, options);
for (const type of ['click', 'close', 'error', 'show']) {
notification.addEventListener(type, (ev: Event) => {
if (!dispatch(type, ev)) {
Expand Down
9 changes: 1 addition & 8 deletions app/renderer/js/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@ import fs from 'fs';
import isDev from 'electron-is-dev';

import electron_bridge from './electron-bridge';
import {loadBots} from './notification/helpers';
import * as NetworkError from './pages/network';

contextBridge.exposeInMainWorld('raw_electron_bridge', electron_bridge);

electron_bridge.once('zulip-loaded', async () => {
await loadBots();
});

ipcRenderer.on('logout', () => {
// Create the menu for the below
const dropdown: HTMLElement = document.querySelector('.dropdown-toggle');
Expand Down Expand Up @@ -51,16 +46,14 @@ ipcRenderer.on('show-notification-settings', () => {
}, 100);
});

electron_bridge.once('zulip-loaded', ({narrow_by_topic}) => {
electron_bridge.once('zulip-loaded', () => {
// Redirect users to network troubleshooting page
const getRestartButton = document.querySelector('.restart_get_events_button');
if (getRestartButton) {
getRestartButton.addEventListener('click', () => {
ipcRenderer.send('forward-message', 'reload-viewer');
});
}

electron_bridge.on('narrow-by-topic', narrow_by_topic);
});

window.addEventListener('load', (event: any): void => {
Expand Down
Loading

0 comments on commit 9041b06

Please sign in to comment.