2018-03-07 14:28:48 +01:00
|
|
|
'use strict'
|
|
|
|
|
2019-04-20 08:44:22 +02:00
|
|
|
import {
|
|
|
|
app,
|
|
|
|
ipcMain,
|
|
|
|
shell,
|
2019-10-27 05:29:49 +01:00
|
|
|
session,
|
2019-04-20 08:44:22 +02:00
|
|
|
Menu,
|
|
|
|
Tray,
|
|
|
|
BrowserWindow,
|
|
|
|
BrowserWindowConstructorOptions,
|
|
|
|
MenuItemConstructorOptions,
|
2019-10-23 16:07:20 +02:00
|
|
|
IpcMainEvent,
|
2019-07-21 17:19:23 +02:00
|
|
|
Notification,
|
2020-02-05 17:16:29 +01:00
|
|
|
NotificationConstructorOptions,
|
|
|
|
nativeTheme
|
2019-04-20 08:44:22 +02:00
|
|
|
} from 'electron'
|
2018-03-08 15:08:33 +01:00
|
|
|
import Datastore from 'nedb'
|
2019-04-16 13:38:02 +02:00
|
|
|
import { isEmpty } from 'lodash'
|
2018-03-24 02:57:41 +01:00
|
|
|
import log from 'electron-log'
|
2018-03-24 15:23:25 +01:00
|
|
|
import windowStateKeeper from 'electron-window-state'
|
2018-04-05 02:00:42 +02:00
|
|
|
import simplayer from 'simplayer'
|
|
|
|
import path from 'path'
|
2018-06-01 07:19:56 +02:00
|
|
|
import ContextMenu from 'electron-context-menu'
|
2019-04-15 18:35:20 +02:00
|
|
|
import { initSplashScreen, Config } from '@trodi/electron-splashscreen'
|
2018-08-10 17:40:06 +02:00
|
|
|
import openAboutWindow from 'about-window'
|
2020-03-15 09:47:40 +01:00
|
|
|
import { Entity, detector } from 'megalodon'
|
2019-07-21 17:19:23 +02:00
|
|
|
import sanitizeHtml from 'sanitize-html'
|
2019-09-23 12:31:25 +02:00
|
|
|
import AutoLaunch from 'auto-launch'
|
2018-03-08 15:08:33 +01:00
|
|
|
|
2019-07-23 18:14:43 +02:00
|
|
|
import pkg from '~/package.json'
|
2018-03-08 09:41:39 +01:00
|
|
|
import Authentication from './auth'
|
2018-03-09 09:08:27 +01:00
|
|
|
import Account from './account'
|
2020-03-15 09:47:40 +01:00
|
|
|
import { StreamingURL, UserStreaming, DirectStreaming, LocalStreaming, PublicStreaming, ListStreaming, TagStreaming } from './websocket'
|
2018-04-07 15:40:57 +02:00
|
|
|
import Preferences from './preferences'
|
2018-09-25 18:02:36 +02:00
|
|
|
import Fonts from './fonts'
|
2018-06-01 07:19:56 +02:00
|
|
|
import Hashtags from './hashtags'
|
2019-04-16 16:50:53 +02:00
|
|
|
import UnreadNotification from './unreadNotification'
|
2020-01-11 13:48:22 +01:00
|
|
|
import i18next from '~/src/config/i18n'
|
|
|
|
import { i18n as I18n } from 'i18next'
|
2018-08-13 16:48:13 +02:00
|
|
|
import Language from '../constants/language'
|
2019-06-06 16:44:50 +02:00
|
|
|
import { LocalAccount } from '~/src/types/localAccount'
|
|
|
|
import { LocalTag } from '~/src/types/localTag'
|
2019-04-16 16:50:53 +02:00
|
|
|
import { UnreadNotification as UnreadNotificationConfig } from '~/src/types/unreadNotification'
|
2019-07-21 17:19:23 +02:00
|
|
|
import { Notify } from '~/src/types/notify'
|
2019-06-27 14:58:43 +02:00
|
|
|
import { StreamingError } from '~/src/errors/streamingError'
|
2019-07-30 17:17:30 +02:00
|
|
|
import HashtagCache from './cache/hashtag'
|
2019-08-08 16:39:27 +02:00
|
|
|
import AccountCache from './cache/account'
|
|
|
|
import { InsertAccountCache } from '~/src/types/insertAccountCache'
|
2019-10-22 17:08:56 +02:00
|
|
|
import { Proxy } from '~/src/types/proxy'
|
2019-10-27 05:29:49 +01:00
|
|
|
import ProxyConfiguration from './proxy'
|
2018-05-30 13:54:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Context menu
|
|
|
|
*/
|
2019-04-23 15:36:05 +02:00
|
|
|
ContextMenu({
|
|
|
|
showCopyImageAddress: true,
|
|
|
|
showSaveImageAs: true
|
|
|
|
})
|
2018-03-07 14:28:48 +01:00
|
|
|
|
2018-03-24 02:57:41 +01:00
|
|
|
/**
|
|
|
|
* Set log level
|
|
|
|
*/
|
|
|
|
log.transports.console.level = 'debug'
|
|
|
|
log.transports.file.level = 'info'
|
|
|
|
|
2019-04-15 18:35:20 +02:00
|
|
|
declare namespace global {
|
|
|
|
let __static: string
|
|
|
|
}
|
|
|
|
|
2018-03-07 14:28:48 +01:00
|
|
|
/**
|
|
|
|
* Set `__static` path to static files in production
|
|
|
|
* https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
|
|
|
|
*/
|
|
|
|
if (process.env.NODE_ENV !== 'development') {
|
2018-04-05 02:00:42 +02:00
|
|
|
global.__static = path.join(__dirname, '/static').replace(/\\/g, '\\\\')
|
2018-03-07 14:28:48 +01:00
|
|
|
}
|
|
|
|
|
2019-04-15 18:35:20 +02:00
|
|
|
let mainWindow: BrowserWindow | null
|
2019-04-18 09:15:50 +02:00
|
|
|
let tray: Tray | null
|
2019-04-20 08:44:22 +02:00
|
|
|
const winURL = process.env.NODE_ENV === 'development' ? `http://localhost:9080` : `file://${__dirname}/index.html`
|
2018-03-07 14:28:48 +01:00
|
|
|
|
2019-09-17 18:13:16 +02:00
|
|
|
// MAS build is not allowed requestSingleInstanceLock.
|
|
|
|
// ref: https://github.com/h3poteto/whalebird-desktop/issues/1030
|
|
|
|
// ref: https://github.com/electron/electron-osx-sign/issues/137#issuecomment-307626305
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
// Enforces single instance for linux and windows.
|
|
|
|
const gotTheLock = app.requestSingleInstanceLock()
|
2019-08-25 02:22:50 +02:00
|
|
|
|
2019-09-17 18:13:16 +02:00
|
|
|
if (!gotTheLock) {
|
|
|
|
app.quit()
|
|
|
|
} else {
|
|
|
|
app.on('second-instance', () => {
|
|
|
|
// Someone tried to run a second instance, we should focus our window.
|
|
|
|
if (mainWindow) {
|
|
|
|
if (mainWindow.isMinimized()) mainWindow.restore()
|
|
|
|
if (!mainWindow!.isVisible()) {
|
|
|
|
mainWindow!.show()
|
|
|
|
mainWindow!.setSkipTaskbar(false)
|
|
|
|
}
|
|
|
|
mainWindow.focus()
|
2019-08-25 02:22:50 +02:00
|
|
|
}
|
2019-09-17 18:13:16 +02:00
|
|
|
})
|
|
|
|
}
|
2019-08-25 02:22:50 +02:00
|
|
|
}
|
|
|
|
|
2019-07-23 18:14:43 +02:00
|
|
|
const appId = pkg.build.appId
|
|
|
|
|
2019-04-20 08:44:22 +02:00
|
|
|
const splashURL =
|
|
|
|
process.env.NODE_ENV === 'development'
|
|
|
|
? path.resolve(__dirname, '../../static/splash-screen.html')
|
|
|
|
: `${__dirname}/static/splash-screen.html`
|
2018-07-27 17:49:05 +02:00
|
|
|
|
2018-03-22 08:55:58 +01:00
|
|
|
// https://github.com/louischatriot/nedb/issues/459
|
2018-03-22 08:49:39 +01:00
|
|
|
const userData = app.getPath('userData')
|
2019-09-23 12:31:25 +02:00
|
|
|
const appPath = app.getPath('exe')
|
|
|
|
|
2019-04-20 08:44:22 +02:00
|
|
|
const accountDBPath = process.env.NODE_ENV === 'production' ? userData + '/db/account.db' : 'account.db'
|
2018-04-07 15:40:57 +02:00
|
|
|
let accountDB = new Datastore({
|
|
|
|
filename: accountDBPath,
|
2018-03-08 15:08:33 +01:00
|
|
|
autoload: true
|
|
|
|
})
|
2018-10-11 16:12:22 +02:00
|
|
|
const accountManager = new Account(accountDB)
|
2019-04-20 08:44:22 +02:00
|
|
|
accountManager.initialize().catch((err: Error) => log.error(err))
|
2018-10-11 16:12:22 +02:00
|
|
|
|
2019-04-20 08:44:22 +02:00
|
|
|
const hashtagsDBPath = process.env.NODE_ENV === 'production' ? userData + '/db/hashtags.db' : 'hashtags.db'
|
2018-06-01 07:19:56 +02:00
|
|
|
let hashtagsDB = new Datastore({
|
|
|
|
filename: hashtagsDBPath,
|
|
|
|
autoload: true
|
|
|
|
})
|
|
|
|
|
2019-04-20 08:44:22 +02:00
|
|
|
const unreadNotificationDBPath = process.env.NODE_ENV === 'production' ? userData + '/db/unread_notification.db' : 'unread_notification.db'
|
2018-11-07 14:48:50 +01:00
|
|
|
const unreadNotification = new UnreadNotification(unreadNotificationDBPath)
|
2019-04-20 08:44:22 +02:00
|
|
|
unreadNotification.initialize().catch((err: Error) => log.error(err))
|
2018-11-07 14:48:50 +01:00
|
|
|
|
2019-04-20 08:44:22 +02:00
|
|
|
const preferencesDBPath = process.env.NODE_ENV === 'production' ? userData + './db/preferences.json' : 'preferences.json'
|
2018-03-08 15:08:33 +01:00
|
|
|
|
2019-07-30 17:17:30 +02:00
|
|
|
/**
|
|
|
|
* Cache path
|
|
|
|
*/
|
|
|
|
const hashtagCachePath = process.env.NODE_ENV === 'production' ? userData + '/cache/hashtag.db' : 'cache/hashtag.db'
|
2019-07-31 17:40:28 +02:00
|
|
|
const hashtagCache = new HashtagCache(hashtagCachePath)
|
2019-07-30 17:17:30 +02:00
|
|
|
|
2019-08-08 16:39:27 +02:00
|
|
|
const accountCachePath = process.env.NODE_ENV === 'production' ? userData + '/cache/account.db' : 'cache/account.db'
|
|
|
|
const accountCache = new AccountCache(accountCachePath)
|
|
|
|
|
2019-04-20 08:44:22 +02:00
|
|
|
const soundBasePath =
|
|
|
|
process.env.NODE_ENV === 'development' ? path.join(__dirname, '../../build/sounds/') : path.join(process.resourcesPath!, 'build/sounds/')
|
2018-04-07 16:18:20 +02:00
|
|
|
|
2019-09-25 18:32:52 +02:00
|
|
|
let launcher: AutoLaunch | null = null
|
2019-10-27 05:29:49 +01:00
|
|
|
const proxyConfiguration = new ProxyConfiguration(preferencesDBPath)
|
2019-09-25 18:32:52 +02:00
|
|
|
|
|
|
|
// On MAS build, auto launch is not working.
|
|
|
|
// We have to use Launch Agent: https://github.com/Teamwork/node-auto-launch/issues/43
|
|
|
|
// But it is too difficult to build, and Slack does not provide this function in MAS build.
|
|
|
|
// Therefore I don't provide this function for MacOS.
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
launcher = new AutoLaunch({
|
|
|
|
name: 'Whalebird',
|
|
|
|
path: appPath
|
|
|
|
})
|
|
|
|
}
|
2019-09-24 14:16:55 +02:00
|
|
|
|
2019-04-20 08:44:22 +02:00
|
|
|
async function listAccounts(): Promise<Array<LocalAccount>> {
|
2018-03-21 04:22:45 +01:00
|
|
|
try {
|
2018-10-11 16:12:22 +02:00
|
|
|
const accounts = await accountManager.listAccounts()
|
2018-03-21 04:22:45 +01:00
|
|
|
return accounts
|
|
|
|
} catch (err) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-20 08:44:22 +02:00
|
|
|
async function changeAccount(account: LocalAccount, index: number) {
|
2019-08-18 06:18:40 +02:00
|
|
|
// Sometimes application is closed to tray.
|
2018-07-21 07:13:52 +02:00
|
|
|
// In this time, mainWindow in not exist, so we have to create window.
|
|
|
|
if (mainWindow === null) {
|
|
|
|
await createWindow()
|
|
|
|
// We have to wait the web contents is loaded.
|
2019-04-15 18:35:20 +02:00
|
|
|
mainWindow!.webContents.on('did-finish-load', () => {
|
|
|
|
mainWindow!.webContents.send('change-account', Object.assign(account, { index: index }))
|
2018-07-21 07:13:52 +02:00
|
|
|
})
|
|
|
|
} else {
|
2019-08-18 06:18:40 +02:00
|
|
|
mainWindow.show()
|
2018-07-21 07:13:52 +02:00
|
|
|
mainWindow.webContents.send('change-account', Object.assign(account, { index: index }))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-20 08:44:22 +02:00
|
|
|
async function getLanguage() {
|
2018-08-13 16:48:13 +02:00
|
|
|
try {
|
|
|
|
const preferences = new Preferences(preferencesDBPath)
|
|
|
|
const conf = await preferences.load()
|
|
|
|
return conf.language.language
|
|
|
|
} catch (err) {
|
2019-11-19 14:38:57 +01:00
|
|
|
log.warn(err)
|
2018-08-13 16:48:13 +02:00
|
|
|
return Language.en.key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-20 08:44:22 +02:00
|
|
|
async function createWindow() {
|
2018-03-12 17:00:19 +01:00
|
|
|
/**
|
2018-03-21 04:22:45 +01:00
|
|
|
* List accounts
|
2018-03-12 17:00:19 +01:00
|
|
|
*/
|
2018-07-21 07:13:52 +02:00
|
|
|
const accounts = await listAccounts()
|
2019-04-17 12:52:01 +02:00
|
|
|
const accountsChange: Array<MenuItemConstructorOptions> = accounts.map((a, index) => {
|
2018-07-21 07:13:52 +02:00
|
|
|
return {
|
|
|
|
label: a.domain,
|
|
|
|
accelerator: `CmdOrCtrl+${index + 1}`,
|
|
|
|
click: () => changeAccount(a, index)
|
|
|
|
}
|
|
|
|
})
|
2018-08-10 01:47:29 +02:00
|
|
|
|
2018-08-13 16:48:13 +02:00
|
|
|
/**
|
|
|
|
* Get language
|
|
|
|
*/
|
|
|
|
const language = await getLanguage()
|
2020-01-11 13:48:22 +01:00
|
|
|
i18next.changeLanguage(language)
|
2018-08-13 16:48:13 +02:00
|
|
|
|
2020-02-05 17:16:29 +01:00
|
|
|
/**
|
|
|
|
* Load system theme color for dark mode
|
|
|
|
*/
|
|
|
|
nativeTheme.themeSource = 'system'
|
|
|
|
|
2018-07-21 07:13:52 +02:00
|
|
|
/**
|
|
|
|
* Set application menu
|
|
|
|
*/
|
2020-01-11 13:48:22 +01:00
|
|
|
ApplicationMenu(accountsChange, i18next)
|
2018-03-12 17:00:19 +01:00
|
|
|
|
2018-07-21 07:13:52 +02:00
|
|
|
/**
|
|
|
|
* Set dock menu for mac
|
|
|
|
*/
|
2018-07-21 07:29:52 +02:00
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
const dockMenu = Menu.buildFromTemplate(accountsChange)
|
|
|
|
app.dock.setMenu(dockMenu)
|
|
|
|
}
|
2018-07-21 07:13:52 +02:00
|
|
|
|
2019-07-23 18:14:43 +02:00
|
|
|
/**
|
2019-09-24 14:16:55 +02:00
|
|
|
* Windows10 don't notify, so we have to set appId
|
2019-07-23 18:14:43 +02:00
|
|
|
* https://github.com/electron/electron/issues/10864
|
|
|
|
*/
|
|
|
|
app.setAppUserModelId(appId)
|
|
|
|
|
2019-03-10 13:07:19 +01:00
|
|
|
/**
|
|
|
|
* Enable accessibility
|
|
|
|
*/
|
2019-10-23 16:07:20 +02:00
|
|
|
app.accessibilitySupportEnabled = true
|
2019-03-10 13:07:19 +01:00
|
|
|
|
2018-07-21 07:13:52 +02:00
|
|
|
/**
|
|
|
|
* Initial window options
|
|
|
|
*/
|
|
|
|
let mainWindowState = windowStateKeeper({
|
|
|
|
defaultWidth: 1000,
|
2019-04-15 18:35:20 +02:00
|
|
|
defaultHeight: 563
|
2018-07-21 07:13:52 +02:00
|
|
|
})
|
2019-04-15 18:35:20 +02:00
|
|
|
const mainOpts: BrowserWindowConstructorOptions = {
|
2018-07-21 07:13:52 +02:00
|
|
|
titleBarStyle: 'hidden',
|
|
|
|
x: mainWindowState.x,
|
|
|
|
y: mainWindowState.y,
|
|
|
|
width: mainWindowState.width,
|
|
|
|
height: mainWindowState.height,
|
|
|
|
useContentSize: true,
|
2019-05-06 15:35:09 +02:00
|
|
|
icon: path.resolve(__dirname, '../../build/icons/256x256.png'),
|
|
|
|
webPreferences: {
|
|
|
|
// It is required to use ipcRenderer in renderer process.
|
|
|
|
// But it is not secure, so if you want to disable this option, please use preload script.
|
2019-05-09 16:54:53 +02:00
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: false,
|
|
|
|
preload: path.resolve(__dirname, './preload.js')
|
2019-05-06 15:35:09 +02:00
|
|
|
}
|
2018-07-27 17:49:05 +02:00
|
|
|
}
|
2019-04-15 18:35:20 +02:00
|
|
|
const config: Config = {
|
2018-07-27 17:49:05 +02:00
|
|
|
windowOpts: mainOpts,
|
|
|
|
templateUrl: splashURL,
|
|
|
|
splashScreenOpts: {
|
|
|
|
width: 425,
|
|
|
|
height: 325
|
|
|
|
}
|
|
|
|
}
|
2019-04-15 18:35:20 +02:00
|
|
|
mainWindow = initSplashScreen(config)
|
2018-03-07 14:28:48 +01:00
|
|
|
|
2018-07-27 17:49:05 +02:00
|
|
|
mainWindowState.manage(mainWindow)
|
2018-07-28 13:44:16 +02:00
|
|
|
|
|
|
|
mainWindow.loadURL(winURL)
|
2018-03-07 14:28:48 +01:00
|
|
|
|
2019-04-20 08:44:22 +02:00
|
|
|
mainWindow.webContents.on('will-navigate', event => event.preventDefault())
|
2018-05-27 15:05:29 +02:00
|
|
|
|
2019-10-27 05:29:49 +01:00
|
|
|
/**
|
|
|
|
* Get system proxy configuration.
|
|
|
|
*/
|
|
|
|
if (session && session.defaultSession) {
|
2020-02-05 15:46:17 +01:00
|
|
|
const proxyInfo = await session.defaultSession.resolveProxy('https://mastodon.social')
|
|
|
|
proxyConfiguration.setSystemProxy(proxyInfo)
|
|
|
|
log.info(`System proxy configuration: ${proxyInfo}`)
|
2019-10-27 05:29:49 +01:00
|
|
|
}
|
|
|
|
|
2018-07-21 07:13:52 +02:00
|
|
|
mainWindow.on('closed', () => {
|
|
|
|
mainWindow = null
|
|
|
|
})
|
2018-09-08 08:12:19 +02:00
|
|
|
|
2019-08-18 06:18:40 +02:00
|
|
|
// Show tray icon only linux and windows.
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
// Show tray icon
|
2019-08-18 06:47:09 +02:00
|
|
|
tray = new Tray(path.join(__dirname, '../../build/icons/tray_icon.png'))
|
2020-01-11 13:48:22 +01:00
|
|
|
const trayMenu = TrayMenu(accountsChange, i18next)
|
2019-08-18 06:18:40 +02:00
|
|
|
tray.setContextMenu(trayMenu)
|
|
|
|
|
|
|
|
// For Windows
|
2020-01-11 13:48:22 +01:00
|
|
|
tray.setToolTip(i18next.t('main_menu.application.name'))
|
2019-08-18 06:18:40 +02:00
|
|
|
tray.on('click', () => {
|
|
|
|
if (mainWindow!.isVisible()) {
|
|
|
|
mainWindow!.hide()
|
|
|
|
mainWindow!.setSkipTaskbar(true)
|
|
|
|
} else {
|
|
|
|
mainWindow!.show()
|
|
|
|
mainWindow!.setSkipTaskbar(false)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-08-22 12:56:13 +02:00
|
|
|
// Minimize to tray
|
|
|
|
mainWindow!.on('close', event => {
|
|
|
|
mainWindow!.hide()
|
|
|
|
mainWindow!.setSkipTaskbar(true)
|
|
|
|
event.preventDefault()
|
|
|
|
})
|
|
|
|
}
|
2018-03-07 14:28:48 +01:00
|
|
|
}
|
|
|
|
|
2018-04-19 17:34:24 +02:00
|
|
|
// Do not lower the rendering priority of Chromium when background
|
|
|
|
app.commandLine.appendSwitch('disable-renderer-backgrounding')
|
|
|
|
|
2018-03-07 14:28:48 +01:00
|
|
|
app.on('ready', createWindow)
|
|
|
|
|
|
|
|
app.on('window-all-closed', () => {
|
2018-07-20 15:51:44 +02:00
|
|
|
// this action is called when user click the close button.
|
|
|
|
// In macOS, close button does not shutdown application. It is hide application window.
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
app.quit()
|
2018-07-22 15:25:23 +02:00
|
|
|
} else {
|
|
|
|
// In MacOS, we should change disable some menu items.
|
|
|
|
const menu = Menu.getApplicationMenu()
|
2020-02-05 15:46:17 +01:00
|
|
|
if (menu) {
|
|
|
|
if (menu.items[0].submenu) {
|
|
|
|
// Preferences
|
|
|
|
menu.items[0].submenu.items[2].enabled = false
|
|
|
|
}
|
|
|
|
if (menu.items[1].submenu) {
|
|
|
|
// New Toot
|
|
|
|
menu.items[1].submenu.items[0].enabled = false
|
|
|
|
}
|
|
|
|
if (menu.items[4].submenu) {
|
|
|
|
// Open Window
|
|
|
|
menu.items[4].submenu.items[1].enabled = true
|
|
|
|
// Jump to
|
|
|
|
menu.items[4].submenu.items[4].enabled = false
|
|
|
|
}
|
2019-04-15 18:35:20 +02:00
|
|
|
}
|
2018-07-20 15:51:44 +02:00
|
|
|
}
|
2018-03-07 14:28:48 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
app.on('activate', () => {
|
|
|
|
if (mainWindow === null) {
|
|
|
|
createWindow()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-10-11 16:12:22 +02:00
|
|
|
let auth = new Authentication(accountManager)
|
2018-03-08 09:41:39 +01:00
|
|
|
|
2020-03-17 16:21:57 +01:00
|
|
|
type AuthRequest = {
|
|
|
|
instance: string
|
|
|
|
sns: 'mastodon' | 'pleroma' | 'misskey'
|
|
|
|
}
|
|
|
|
|
|
|
|
ipcMain.on('get-auth-url', async (event: IpcMainEvent, request: AuthRequest) => {
|
2019-10-27 09:22:44 +01:00
|
|
|
const proxy = await proxyConfiguration.forMastodon()
|
2019-04-20 08:44:22 +02:00
|
|
|
auth
|
2020-03-17 16:21:57 +01:00
|
|
|
.getAuthorizationUrl(request.sns, request.instance, proxy)
|
2019-04-20 08:44:22 +02:00
|
|
|
.then(url => {
|
2018-03-24 02:57:41 +01:00
|
|
|
log.debug(url)
|
2018-03-09 09:08:27 +01:00
|
|
|
event.sender.send('response-get-auth-url', url)
|
2018-03-10 15:28:55 +01:00
|
|
|
// Open authorize url in default browser.
|
2018-03-08 09:41:39 +01:00
|
|
|
shell.openExternal(url)
|
|
|
|
})
|
2019-04-20 08:44:22 +02:00
|
|
|
.catch(err => {
|
2018-04-01 14:43:23 +02:00
|
|
|
log.error(err)
|
|
|
|
event.sender.send('error-get-auth-url', err)
|
|
|
|
})
|
2018-03-08 09:41:39 +01:00
|
|
|
})
|
|
|
|
|
2020-03-17 16:21:57 +01:00
|
|
|
type TokenRequest = {
|
|
|
|
code: string | null
|
|
|
|
sns: 'mastodon' | 'pleroma' | 'misskey'
|
|
|
|
}
|
|
|
|
|
|
|
|
ipcMain.on('get-access-token', async (event: IpcMainEvent, request: TokenRequest) => {
|
2019-10-27 09:22:44 +01:00
|
|
|
const proxy = await proxyConfiguration.forMastodon()
|
2019-04-20 08:44:22 +02:00
|
|
|
auth
|
2020-03-17 16:21:57 +01:00
|
|
|
.getAccessToken(request.sns, request.code, proxy)
|
2019-04-20 08:44:22 +02:00
|
|
|
.then(token => {
|
|
|
|
accountDB.findOne(
|
|
|
|
{
|
|
|
|
accessToken: token
|
|
|
|
},
|
|
|
|
(err, doc: any) => {
|
|
|
|
if (err) return event.sender.send('error-get-access-token', err)
|
|
|
|
if (isEmpty(doc)) return event.sender.send('error-get-access-token', 'error document is empty')
|
|
|
|
event.sender.send('response-get-access-token', doc._id)
|
|
|
|
}
|
|
|
|
)
|
2018-03-08 16:24:18 +01:00
|
|
|
})
|
2019-04-20 08:44:22 +02:00
|
|
|
.catch(err => {
|
2018-06-12 01:48:01 +02:00
|
|
|
log.error(err)
|
|
|
|
event.sender.send('error-get-access-token', err)
|
|
|
|
})
|
2018-03-08 09:41:39 +01:00
|
|
|
})
|
|
|
|
|
2018-03-13 07:09:54 +01:00
|
|
|
// environments
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('get-social-token', (event: IpcMainEvent) => {
|
2018-03-13 07:09:54 +01:00
|
|
|
const token = process.env.SOCIAL_TOKEN
|
2019-04-16 13:38:02 +02:00
|
|
|
if (isEmpty(token)) {
|
2018-03-13 07:09:54 +01:00
|
|
|
return event.sender.send('error-get-social-token', new EmptyTokenError())
|
|
|
|
}
|
|
|
|
event.sender.send('response-get-social-token', token)
|
2018-03-08 10:36:09 +01:00
|
|
|
})
|
|
|
|
|
2018-03-09 09:08:27 +01:00
|
|
|
// nedb
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('list-accounts', (event: IpcMainEvent) => {
|
2019-04-20 08:44:22 +02:00
|
|
|
accountManager
|
|
|
|
.listAccounts()
|
|
|
|
.catch(err => {
|
2018-03-24 02:57:41 +01:00
|
|
|
log.error(err)
|
2018-03-10 15:21:09 +01:00
|
|
|
event.sender.send('error-list-accounts', err)
|
2018-03-08 15:08:33 +01:00
|
|
|
})
|
2019-04-20 08:44:22 +02:00
|
|
|
.then(accounts => {
|
2018-03-10 15:21:09 +01:00
|
|
|
event.sender.send('response-list-accounts', accounts)
|
2018-03-08 15:08:33 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('get-local-account', (event: IpcMainEvent, id: string) => {
|
2019-04-20 08:44:22 +02:00
|
|
|
accountManager
|
|
|
|
.getAccount(id)
|
|
|
|
.catch(err => {
|
2018-03-24 02:57:41 +01:00
|
|
|
log.error(err)
|
2018-03-09 09:36:57 +01:00
|
|
|
event.sender.send('error-get-local-account', err)
|
|
|
|
})
|
2019-04-20 08:44:22 +02:00
|
|
|
.then(account => {
|
2018-03-10 17:51:35 +01:00
|
|
|
event.sender.send('response-get-local-account', account)
|
2018-03-09 09:36:57 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-10-27 09:22:44 +01:00
|
|
|
ipcMain.on('update-account', async (event: IpcMainEvent, acct: LocalAccount) => {
|
|
|
|
const proxy = await proxyConfiguration.forMastodon()
|
2019-04-20 08:44:22 +02:00
|
|
|
accountManager
|
2019-10-27 09:22:44 +01:00
|
|
|
.refresh(acct, proxy)
|
2019-04-20 08:44:22 +02:00
|
|
|
.then(ac => {
|
2018-04-01 14:43:23 +02:00
|
|
|
event.sender.send('response-update-account', ac)
|
|
|
|
})
|
2019-04-20 08:44:22 +02:00
|
|
|
.catch(err => {
|
2018-04-01 14:43:23 +02:00
|
|
|
event.sender.send('error-update-account', err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('remove-account', (event: IpcMainEvent, id: string) => {
|
2019-04-20 08:44:22 +02:00
|
|
|
accountManager
|
|
|
|
.removeAccount(id)
|
2019-07-21 17:19:23 +02:00
|
|
|
.then(id => {
|
2019-07-10 17:55:01 +02:00
|
|
|
stopUserStreaming(id)
|
|
|
|
event.sender.send('response-remove-account', id)
|
2018-04-01 12:23:07 +02:00
|
|
|
})
|
2019-04-20 08:44:22 +02:00
|
|
|
.catch(err => {
|
2018-04-01 12:23:07 +02:00
|
|
|
event.sender.send('error-remove-account', err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('forward-account', (event: IpcMainEvent, acct: LocalAccount) => {
|
2019-04-20 08:44:22 +02:00
|
|
|
accountManager
|
|
|
|
.forwardAccount(acct)
|
2018-04-02 02:07:09 +02:00
|
|
|
.then(() => {
|
|
|
|
event.sender.send('response-forward-account')
|
|
|
|
})
|
2019-04-20 08:44:22 +02:00
|
|
|
.catch(err => {
|
2018-10-11 16:12:22 +02:00
|
|
|
log.error(err)
|
2018-04-02 02:07:09 +02:00
|
|
|
event.sender.send('error-forward-account', err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('backward-account', (event: IpcMainEvent, acct: LocalAccount) => {
|
2019-04-20 08:44:22 +02:00
|
|
|
accountManager
|
|
|
|
.backwardAccount(acct)
|
2018-04-02 15:17:08 +02:00
|
|
|
.then(() => {
|
|
|
|
event.sender.send('response-backward-account')
|
|
|
|
})
|
2019-04-20 08:44:22 +02:00
|
|
|
.catch(err => {
|
2018-04-02 15:17:08 +02:00
|
|
|
event.sender.send('error-backward-account', err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-10-27 09:22:44 +01:00
|
|
|
ipcMain.on('refresh-accounts', async (event: IpcMainEvent) => {
|
|
|
|
const proxy = await proxyConfiguration.forMastodon()
|
2019-04-20 08:44:22 +02:00
|
|
|
accountManager
|
2019-10-27 09:22:44 +01:00
|
|
|
.refreshAccounts(proxy)
|
2019-04-20 08:44:22 +02:00
|
|
|
.then(accounts => {
|
2018-05-15 17:19:07 +02:00
|
|
|
event.sender.send('response-refresh-accounts', accounts)
|
|
|
|
})
|
2019-04-20 08:44:22 +02:00
|
|
|
.catch(err => {
|
2018-05-15 17:19:07 +02:00
|
|
|
event.sender.send('error-refresh-accounts', err)
|
|
|
|
})
|
2018-04-22 08:48:20 +02:00
|
|
|
})
|
|
|
|
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('remove-all-accounts', (event: IpcMainEvent) => {
|
2019-04-20 08:44:22 +02:00
|
|
|
accountManager
|
|
|
|
.removeAll()
|
2018-06-13 15:51:41 +02:00
|
|
|
.then(() => {
|
|
|
|
event.sender.send('response-remove-all-accounts')
|
|
|
|
})
|
2019-04-20 08:44:22 +02:00
|
|
|
.catch(err => {
|
2018-06-13 15:51:41 +02:00
|
|
|
log.error(err)
|
|
|
|
event.sender.send('error-remove-all-accounts', err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('change-auto-launch', (event: IpcMainEvent, enable: boolean) => {
|
2019-09-25 18:32:52 +02:00
|
|
|
if (launcher) {
|
|
|
|
launcher.isEnabled().then(enabled => {
|
|
|
|
if (!enabled && enable && launcher) {
|
|
|
|
launcher.enable()
|
|
|
|
} else if (enabled && !enable && launcher) {
|
|
|
|
launcher.disable()
|
|
|
|
}
|
|
|
|
event.sender.send('response-change-auto-launch', enable)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
event.sender.send('response-change-auto-launch', false)
|
|
|
|
}
|
2019-09-23 12:31:25 +02:00
|
|
|
})
|
|
|
|
|
2018-06-14 15:33:44 +02:00
|
|
|
// badge
|
|
|
|
ipcMain.on('reset-badge', () => {
|
2018-06-14 15:40:29 +02:00
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
app.dock.setBadge('')
|
|
|
|
}
|
2018-06-14 15:33:44 +02:00
|
|
|
})
|
|
|
|
|
2019-06-27 16:03:30 +02:00
|
|
|
// user streaming
|
2020-03-15 09:47:40 +01:00
|
|
|
let userStreamings: { [key: string]: UserStreaming | null } = {}
|
2019-06-27 16:03:30 +02:00
|
|
|
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('start-all-user-streamings', (event: IpcMainEvent, accounts: Array<LocalAccount>) => {
|
2019-07-29 16:32:02 +02:00
|
|
|
accounts.map(async account => {
|
2019-06-27 16:03:30 +02:00
|
|
|
const id: string = account._id!
|
2019-07-29 16:32:02 +02:00
|
|
|
try {
|
|
|
|
const acct = await accountManager.getAccount(id)
|
|
|
|
// Stop old user streaming
|
|
|
|
if (userStreamings[id]) {
|
|
|
|
userStreamings[id]!.stop()
|
|
|
|
userStreamings[id] = null
|
|
|
|
}
|
2019-10-27 09:22:44 +01:00
|
|
|
const proxy = await proxyConfiguration.forMastodon()
|
2020-03-15 09:47:40 +01:00
|
|
|
const sns = await detector(acct.baseURL, proxy)
|
|
|
|
const url = await StreamingURL(sns, acct, proxy)
|
|
|
|
userStreamings[id] = new UserStreaming(sns, acct, url, proxy)
|
|
|
|
userStreamings[id]!.start(
|
|
|
|
async (update: Entity.Status) => {
|
2019-07-29 16:32:02 +02:00
|
|
|
if (!event.sender.isDestroyed()) {
|
|
|
|
event.sender.send(`update-start-all-user-streamings-${id}`, update)
|
|
|
|
}
|
2019-07-31 17:40:28 +02:00
|
|
|
// Cache hashtag
|
|
|
|
update.tags.map(async tag => {
|
2019-08-29 15:42:32 +02:00
|
|
|
await hashtagCache.insertHashtag(tag.name).catch(err => console.error(err))
|
2019-07-31 17:40:28 +02:00
|
|
|
})
|
2019-08-10 11:31:50 +02:00
|
|
|
// Cache account
|
2019-08-10 15:30:30 +02:00
|
|
|
await accountCache.insertAccount(id, update.account.acct).catch(err => console.error(err))
|
2019-07-29 16:32:02 +02:00
|
|
|
},
|
2020-03-15 09:47:40 +01:00
|
|
|
(notification: Entity.Notification) => {
|
2019-07-29 16:32:02 +02:00
|
|
|
const preferences = new Preferences(preferencesDBPath)
|
|
|
|
preferences.load().then(conf => {
|
|
|
|
const options = createNotification(notification, conf.notification.notify)
|
|
|
|
if (options !== null) {
|
|
|
|
const notify = new Notification(options)
|
|
|
|
notify.on('click', _ => {
|
|
|
|
if (!event.sender.isDestroyed()) {
|
|
|
|
event.sender.send('open-notification-tab', id)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
notify.show()
|
2019-06-27 16:03:30 +02:00
|
|
|
}
|
2019-07-29 16:32:02 +02:00
|
|
|
})
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
app.dock.setBadge('•')
|
|
|
|
}
|
2019-07-22 16:32:14 +02:00
|
|
|
|
2019-07-29 16:32:02 +02:00
|
|
|
// In macOS and Windows, sometimes window is closed (not quit).
|
|
|
|
// But streamings are always running.
|
|
|
|
// When window is closed, we can not send event to webContents; because it is already destroyed.
|
|
|
|
// So we have to guard it.
|
|
|
|
if (!event.sender.isDestroyed()) {
|
|
|
|
// To update notification timeline
|
|
|
|
event.sender.send(`notification-start-all-user-streamings-${id}`, notification)
|
|
|
|
|
|
|
|
// Does not exist a endpoint for only mention. And mention is a part of notification.
|
|
|
|
// So we have to get mention from notification.
|
|
|
|
if (notification.type === 'mention') {
|
|
|
|
event.sender.send(`mention-start-all-user-streamings-${id}`, notification)
|
2019-06-27 16:03:30 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-29 16:32:02 +02:00
|
|
|
},
|
|
|
|
(statusId: string) => {
|
|
|
|
if (!event.sender.isDestroyed()) {
|
|
|
|
event.sender.send(`delete-start-all-user-streamings-${id}`, statusId)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(err: Error) => {
|
|
|
|
log.error(err)
|
|
|
|
// In macOS, sometimes window is closed (not quit).
|
|
|
|
// When window is closed, we can not send event to webContents; because it is destroyed.
|
|
|
|
// So we have to guard it.
|
|
|
|
if (!event.sender.isDestroyed()) {
|
|
|
|
event.sender.send('error-start-all-user-streamings', err)
|
|
|
|
}
|
2019-07-22 16:32:14 +02:00
|
|
|
}
|
2019-07-29 16:32:02 +02:00
|
|
|
)
|
|
|
|
} catch (err) {
|
|
|
|
log.error(err)
|
|
|
|
const streamingError = new StreamingError(err.message, account.domain)
|
|
|
|
if (!event.sender.isDestroyed()) {
|
|
|
|
event.sender.send('error-start-all-user-streamings', streamingError)
|
|
|
|
}
|
|
|
|
}
|
2019-06-27 16:03:30 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-06-27 15:42:46 +02:00
|
|
|
ipcMain.on('stop-all-user-streamings', () => {
|
|
|
|
Object.keys(userStreamings).map((key: string) => {
|
|
|
|
if (userStreamings[key]) {
|
|
|
|
userStreamings[key]!.stop()
|
|
|
|
userStreamings[key] = null
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-07-10 17:55:01 +02:00
|
|
|
/**
|
|
|
|
* Stop an user streaming in all user streamings.
|
|
|
|
* @param id specified user id in nedb.
|
|
|
|
*/
|
|
|
|
const stopUserStreaming = (id: string) => {
|
|
|
|
Object.keys(userStreamings).map((key: string) => {
|
|
|
|
if (key === id && userStreamings[id]) {
|
|
|
|
userStreamings[id]!.stop()
|
|
|
|
userStreamings[id] = null
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-16 18:09:29 +02:00
|
|
|
type StreamingSetting = {
|
2019-04-20 08:44:22 +02:00
|
|
|
account: LocalAccount
|
2019-04-16 18:09:29 +02:00
|
|
|
}
|
|
|
|
|
2020-03-15 09:47:40 +01:00
|
|
|
let directMessagesStreaming: DirectStreaming | null = null
|
2018-11-03 17:12:36 +01:00
|
|
|
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('start-directmessages-streaming', async (event: IpcMainEvent, obj: StreamingSetting) => {
|
2019-07-28 17:56:55 +02:00
|
|
|
const { account } = obj
|
2019-07-29 16:32:02 +02:00
|
|
|
try {
|
|
|
|
const acct = await accountManager.getAccount(account._id!)
|
2018-11-03 17:12:36 +01:00
|
|
|
|
2019-07-29 16:32:02 +02:00
|
|
|
// Stop old directmessages streaming
|
|
|
|
if (directMessagesStreaming !== null) {
|
|
|
|
directMessagesStreaming.stop()
|
|
|
|
directMessagesStreaming = null
|
|
|
|
}
|
2019-10-27 09:22:44 +01:00
|
|
|
const proxy = await proxyConfiguration.forMastodon()
|
2020-03-15 09:47:40 +01:00
|
|
|
const sns = await detector(acct.baseURL, proxy)
|
|
|
|
const url = await StreamingURL(sns, acct, proxy)
|
|
|
|
directMessagesStreaming = new DirectStreaming(sns, acct, url, proxy)
|
2019-07-29 16:32:02 +02:00
|
|
|
directMessagesStreaming.start(
|
2020-03-15 09:47:40 +01:00
|
|
|
(update: Entity.Status) => {
|
2019-07-29 16:32:02 +02:00
|
|
|
if (!event.sender.isDestroyed()) {
|
|
|
|
event.sender.send('update-start-directmessages-streaming', update)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(id: string) => {
|
|
|
|
if (!event.sender.isDestroyed()) {
|
|
|
|
event.sender.send('delete-start-directmessages-streaming', id)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(err: Error) => {
|
|
|
|
log.error(err)
|
|
|
|
if (!event.sender.isDestroyed()) {
|
|
|
|
event.sender.send('error-start-directmessages-streaming', err)
|
2018-11-03 17:12:36 +01:00
|
|
|
}
|
2019-07-22 16:32:14 +02:00
|
|
|
}
|
2019-07-29 16:32:02 +02:00
|
|
|
)
|
|
|
|
} catch (err) {
|
|
|
|
log.error(err)
|
|
|
|
if (!event.sender.isDestroyed()) {
|
|
|
|
event.sender.send('error-start-directmessages-streaming', err)
|
|
|
|
}
|
|
|
|
}
|
2018-11-03 17:12:36 +01:00
|
|
|
})
|
|
|
|
|
2019-04-17 12:52:01 +02:00
|
|
|
ipcMain.on('stop-directmessages-streaming', () => {
|
2018-11-03 17:37:30 +01:00
|
|
|
if (directMessagesStreaming !== null) {
|
|
|
|
directMessagesStreaming.stop()
|
|
|
|
directMessagesStreaming = null
|
2018-11-03 17:12:36 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-03-15 09:47:40 +01:00
|
|
|
let localStreaming: LocalStreaming | null = null
|
2018-03-14 04:24:54 +01:00
|
|
|
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('start-local-streaming', async (event: IpcMainEvent, obj: StreamingSetting) => {
|
2019-07-28 17:56:55 +02:00
|
|
|
const { account } = obj
|
2019-07-29 16:32:02 +02:00
|
|
|
try {
|
|
|
|
const acct = await accountManager.getAccount(account._id!)
|
2018-03-14 04:24:54 +01:00
|
|
|
|
2019-07-29 16:32:02 +02:00
|
|
|
// Stop old local streaming
|
|
|
|
if (localStreaming !== null) {
|
|
|
|
localStreaming.stop()
|
|
|
|
localStreaming = null
|
|
|
|
}
|
2019-10-27 09:22:44 +01:00
|
|
|
const proxy = await proxyConfiguration.forMastodon()
|
2020-03-15 09:47:40 +01:00
|
|
|
const sns = await detector(acct.baseURL, proxy)
|
|
|
|
const url = await StreamingURL(sns, acct, proxy)
|
|
|
|
localStreaming = new LocalStreaming(sns, acct, url, proxy)
|
2019-07-29 16:32:02 +02:00
|
|
|
localStreaming.start(
|
2020-03-15 09:47:40 +01:00
|
|
|
(update: Entity.Status) => {
|
2019-07-29 16:32:02 +02:00
|
|
|
if (!event.sender.isDestroyed()) {
|
|
|
|
event.sender.send('update-start-local-streaming', update)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(id: string) => {
|
|
|
|
if (!event.sender.isDestroyed()) {
|
|
|
|
event.sender.send('delete-start-local-streaming', id)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(err: Error) => {
|
|
|
|
log.error(err)
|
|
|
|
if (!event.sender.isDestroyed()) {
|
|
|
|
event.sender.send('error-start-local-streaming', err)
|
2018-03-14 04:24:54 +01:00
|
|
|
}
|
2019-07-22 16:32:14 +02:00
|
|
|
}
|
2019-07-29 16:32:02 +02:00
|
|
|
)
|
|
|
|
} catch (err) {
|
|
|
|
log.error(err)
|
|
|
|
if (!event.sender.isDestroyed()) {
|
|
|
|
event.sender.send('error-start-local-streaming', err)
|
|
|
|
}
|
|
|
|
}
|
2018-03-14 04:24:54 +01:00
|
|
|
})
|
|
|
|
|
2019-04-17 12:52:01 +02:00
|
|
|
ipcMain.on('stop-local-streaming', () => {
|
2018-07-01 14:15:54 +02:00
|
|
|
if (localStreaming !== null) {
|
|
|
|
localStreaming.stop()
|
|
|
|
localStreaming = null
|
|
|
|
}
|
2018-03-14 04:24:54 +01:00
|
|
|
})
|
|
|
|
|
2020-03-15 09:47:40 +01:00
|
|
|
let publicStreaming: PublicStreaming | null = null
|
2018-03-14 06:54:20 +01:00
|
|
|
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('start-public-streaming', async (event: IpcMainEvent, obj: StreamingSetting) => {
|
2019-07-28 17:56:55 +02:00
|
|
|
const { account } = obj
|
2019-07-29 16:32:02 +02:00
|
|
|
try {
|
|
|
|
const acct = await accountManager.getAccount(account._id!)
|
2018-03-14 06:54:20 +01:00
|
|
|
|
2019-07-29 16:32:02 +02:00
|
|
|
// Stop old public streaming
|
|
|
|
if (publicStreaming !== null) {
|
|
|
|
publicStreaming.stop()
|
|
|
|
publicStreaming = null
|
|
|
|
}
|
2019-10-27 09:22:44 +01:00
|
|
|
const proxy = await proxyConfiguration.forMastodon()
|
2020-03-15 09:47:40 +01:00
|
|
|
const sns = await detector(acct.baseURL, proxy)
|
|
|
|
const url = await StreamingURL(sns, acct, proxy)
|
|
|
|
publicStreaming = new PublicStreaming(sns, acct, url, proxy)
|
2019-07-29 16:32:02 +02:00
|
|
|
publicStreaming.start(
|
2020-03-15 09:47:40 +01:00
|
|
|
(update: Entity.Status) => {
|
2019-07-29 16:32:02 +02:00
|
|
|
if (!event.sender.isDestroyed()) {
|
|
|
|
event.sender.send('update-start-public-streaming', update)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(id: string) => {
|
|
|
|
if (!event.sender.isDestroyed()) {
|
|
|
|
event.sender.send('delete-start-public-streaming', id)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(err: Error) => {
|
|
|
|
log.error(err)
|
|
|
|
if (!event.sender.isDestroyed()) {
|
|
|
|
event.sender.send('error-start-public-streaming', err)
|
2018-03-14 06:54:20 +01:00
|
|
|
}
|
2019-07-22 16:32:14 +02:00
|
|
|
}
|
2019-07-29 16:32:02 +02:00
|
|
|
)
|
|
|
|
} catch (err) {
|
|
|
|
log.error(err)
|
|
|
|
if (!event.sender.isDestroyed()) {
|
|
|
|
event.sender.send('error-start-public-streaming', err)
|
|
|
|
}
|
|
|
|
}
|
2018-03-14 06:54:20 +01:00
|
|
|
})
|
|
|
|
|
2019-04-17 12:52:01 +02:00
|
|
|
ipcMain.on('stop-public-streaming', () => {
|
2018-07-01 14:15:54 +02:00
|
|
|
if (publicStreaming !== null) {
|
|
|
|
publicStreaming.stop()
|
|
|
|
publicStreaming = null
|
|
|
|
}
|
2018-03-14 06:54:20 +01:00
|
|
|
})
|
|
|
|
|
2020-03-15 09:47:40 +01:00
|
|
|
let listStreaming: ListStreaming | null = null
|
2018-04-09 16:43:36 +02:00
|
|
|
|
2019-04-16 18:09:29 +02:00
|
|
|
type ListID = {
|
2019-05-27 15:56:54 +02:00
|
|
|
listID: string
|
2019-04-16 18:09:29 +02:00
|
|
|
}
|
|
|
|
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('start-list-streaming', async (event: IpcMainEvent, obj: ListID & StreamingSetting) => {
|
2019-07-28 17:56:55 +02:00
|
|
|
const { listID, account } = obj
|
2019-07-29 16:32:02 +02:00
|
|
|
try {
|
|
|
|
const acct = await accountManager.getAccount(account._id!)
|
2018-04-09 16:43:36 +02:00
|
|
|
|
2019-07-29 16:32:02 +02:00
|
|
|
// Stop old list streaming
|
|
|
|
if (listStreaming !== null) {
|
|
|
|
listStreaming.stop()
|
|
|
|
listStreaming = null
|
|
|
|
}
|
2019-10-27 09:22:44 +01:00
|
|
|
const proxy = await proxyConfiguration.forMastodon()
|
2020-03-15 09:47:40 +01:00
|
|
|
const sns = await detector(acct.baseURL, proxy)
|
|
|
|
const url = await StreamingURL(sns, acct, proxy)
|
|
|
|
listStreaming = new ListStreaming(sns, acct, url, proxy)
|
2019-07-29 16:32:02 +02:00
|
|
|
listStreaming.start(
|
2020-03-15 09:47:40 +01:00
|
|
|
listID,
|
|
|
|
(update: Entity.Status) => {
|
2019-07-29 16:32:02 +02:00
|
|
|
if (!event.sender.isDestroyed()) {
|
|
|
|
event.sender.send('update-start-list-streaming', update)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(id: string) => {
|
|
|
|
if (!event.sender.isDestroyed()) {
|
|
|
|
event.sender.send('delete-start-list-streaming', id)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(err: Error) => {
|
|
|
|
log.error(err)
|
|
|
|
if (!event.sender.isDestroyed()) {
|
|
|
|
event.sender.send('error-start-list-streaming', err)
|
2018-04-09 16:43:36 +02:00
|
|
|
}
|
2019-07-22 16:32:14 +02:00
|
|
|
}
|
2019-07-29 16:32:02 +02:00
|
|
|
)
|
|
|
|
} catch (err) {
|
|
|
|
log.error(err)
|
|
|
|
if (!event.sender.isDestroyed()) {
|
|
|
|
event.sender.send('error-start-list-streaming', err)
|
|
|
|
}
|
|
|
|
}
|
2018-04-09 16:43:36 +02:00
|
|
|
})
|
|
|
|
|
2019-04-17 12:52:01 +02:00
|
|
|
ipcMain.on('stop-list-streaming', () => {
|
2018-06-01 06:09:42 +02:00
|
|
|
if (listStreaming !== null) {
|
|
|
|
listStreaming.stop()
|
|
|
|
listStreaming = null
|
|
|
|
}
|
2018-05-31 11:13:38 +02:00
|
|
|
})
|
|
|
|
|
2020-03-15 09:47:40 +01:00
|
|
|
let tagStreaming: TagStreaming | null = null
|
2018-05-31 11:13:38 +02:00
|
|
|
|
2019-04-16 18:09:29 +02:00
|
|
|
type Tag = {
|
|
|
|
tag: string
|
|
|
|
}
|
|
|
|
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('start-tag-streaming', async (event: IpcMainEvent, obj: Tag & StreamingSetting) => {
|
2019-07-28 17:56:55 +02:00
|
|
|
const { tag, account } = obj
|
2019-07-29 16:32:02 +02:00
|
|
|
try {
|
|
|
|
const acct = await accountManager.getAccount(account._id!)
|
2018-05-31 11:13:38 +02:00
|
|
|
|
2019-07-29 16:32:02 +02:00
|
|
|
// Stop old tag streaming
|
|
|
|
if (tagStreaming !== null) {
|
|
|
|
tagStreaming.stop()
|
|
|
|
tagStreaming = null
|
|
|
|
}
|
2019-10-27 09:22:44 +01:00
|
|
|
const proxy = await proxyConfiguration.forMastodon()
|
2020-03-15 09:47:40 +01:00
|
|
|
const sns = await detector(acct.baseURL, proxy)
|
|
|
|
const url = await StreamingURL(sns, acct, proxy)
|
|
|
|
tagStreaming = new TagStreaming(sns, acct, url, proxy)
|
2019-07-29 16:32:02 +02:00
|
|
|
tagStreaming.start(
|
2020-03-15 09:47:40 +01:00
|
|
|
tag,
|
|
|
|
(update: Entity.Status) => {
|
2019-07-29 16:32:02 +02:00
|
|
|
if (!event.sender.isDestroyed()) {
|
|
|
|
event.sender.send('update-start-tag-streaming', update)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(id: string) => {
|
|
|
|
if (!event.sender.isDestroyed()) {
|
|
|
|
event.sender.send('delete-start-tag-streaming', id)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(err: Error) => {
|
|
|
|
log.error(err)
|
|
|
|
if (!event.sender.isDestroyed()) {
|
|
|
|
event.sender.send('error-start-tag-streaming', err)
|
2018-05-31 11:13:38 +02:00
|
|
|
}
|
2019-07-22 16:32:14 +02:00
|
|
|
}
|
2019-07-29 16:32:02 +02:00
|
|
|
)
|
|
|
|
} catch (err) {
|
|
|
|
log.error(err)
|
|
|
|
if (!event.sender.isDestroyed()) {
|
|
|
|
event.sender.send('error-start-tag-streaming', err)
|
|
|
|
}
|
|
|
|
}
|
2018-05-31 11:13:38 +02:00
|
|
|
})
|
|
|
|
|
2019-04-17 12:52:01 +02:00
|
|
|
ipcMain.on('stop-tag-streaming', () => {
|
2018-07-01 14:15:54 +02:00
|
|
|
if (tagStreaming !== null) {
|
|
|
|
tagStreaming.stop()
|
|
|
|
tagStreaming = null
|
|
|
|
}
|
2018-05-31 11:13:38 +02:00
|
|
|
})
|
|
|
|
|
2018-04-05 02:00:42 +02:00
|
|
|
// sounds
|
2019-04-17 12:52:01 +02:00
|
|
|
ipcMain.on('fav-rt-action-sound', () => {
|
2018-04-07 16:18:20 +02:00
|
|
|
const preferences = new Preferences(preferencesDBPath)
|
2019-04-20 08:44:22 +02:00
|
|
|
preferences
|
|
|
|
.load()
|
|
|
|
.then(conf => {
|
2018-04-07 16:18:20 +02:00
|
|
|
if (conf.general.sound.fav_rb) {
|
|
|
|
const sound = path.join(soundBasePath, 'operation_sound01.wav')
|
2019-04-16 16:50:53 +02:00
|
|
|
simplayer(sound, (err: Error) => {
|
2018-04-07 16:18:20 +02:00
|
|
|
if (err) log.error(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(err => log.error(err))
|
2018-04-05 14:04:40 +02:00
|
|
|
})
|
|
|
|
|
2019-04-17 12:52:01 +02:00
|
|
|
ipcMain.on('toot-action-sound', () => {
|
2018-04-07 16:18:20 +02:00
|
|
|
const preferences = new Preferences(preferencesDBPath)
|
2019-04-20 08:44:22 +02:00
|
|
|
preferences
|
|
|
|
.load()
|
|
|
|
.then(conf => {
|
2018-04-07 16:18:20 +02:00
|
|
|
if (conf.general.sound.toot) {
|
|
|
|
const sound = path.join(soundBasePath, 'operation_sound02.wav')
|
2019-04-16 16:50:53 +02:00
|
|
|
simplayer(sound, (err: Error) => {
|
2018-04-07 16:18:20 +02:00
|
|
|
if (err) log.error(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(err => log.error(err))
|
2018-04-05 02:00:42 +02:00
|
|
|
})
|
|
|
|
|
2018-04-07 15:40:57 +02:00
|
|
|
// preferences
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('get-preferences', async (event: IpcMainEvent) => {
|
2018-04-07 15:40:57 +02:00
|
|
|
const preferences = new Preferences(preferencesDBPath)
|
2019-09-25 18:32:52 +02:00
|
|
|
let enabled = false
|
|
|
|
if (launcher) {
|
|
|
|
enabled = await launcher.isEnabled()
|
|
|
|
}
|
2019-09-24 14:16:55 +02:00
|
|
|
await preferences
|
|
|
|
.update({
|
|
|
|
general: {
|
|
|
|
other: enabled
|
|
|
|
}
|
2018-04-07 15:40:57 +02:00
|
|
|
})
|
2019-09-24 14:16:55 +02:00
|
|
|
.catch(err => console.error(err))
|
|
|
|
const conf = await preferences.load().catch(err => {
|
|
|
|
event.sender.send('error-get-preferences', err)
|
|
|
|
})
|
|
|
|
event.sender.send('response-get-preferences', conf)
|
2018-04-07 15:40:57 +02:00
|
|
|
})
|
|
|
|
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('update-preferences', (event: IpcMainEvent, data: any) => {
|
2018-08-29 17:56:56 +02:00
|
|
|
const preferences = new Preferences(preferencesDBPath)
|
2019-04-20 08:44:22 +02:00
|
|
|
preferences
|
|
|
|
.update(data)
|
|
|
|
.then(conf => {
|
2018-08-29 17:56:56 +02:00
|
|
|
event.sender.send('response-update-preferences', conf)
|
|
|
|
})
|
2019-04-20 08:44:22 +02:00
|
|
|
.catch(err => {
|
2018-08-29 17:56:56 +02:00
|
|
|
event.sender.send('error-update-preferences', err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('change-collapse', (_event: IpcMainEvent, value: boolean) => {
|
2018-07-03 16:02:16 +02:00
|
|
|
const preferences = new Preferences(preferencesDBPath)
|
2019-04-20 08:44:22 +02:00
|
|
|
preferences
|
|
|
|
.update({
|
2018-07-03 16:02:16 +02:00
|
|
|
state: {
|
|
|
|
collapse: value
|
|
|
|
}
|
|
|
|
})
|
2019-04-20 08:44:22 +02:00
|
|
|
.catch(err => {
|
2018-07-03 16:02:16 +02:00
|
|
|
log.error(err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('get-collapse', (event: IpcMainEvent) => {
|
2018-07-03 16:02:16 +02:00
|
|
|
const preferences = new Preferences(preferencesDBPath)
|
2019-04-20 08:44:22 +02:00
|
|
|
preferences.load().then(conf => {
|
|
|
|
event.sender.send('response-get-collapse', conf.state.collapse)
|
|
|
|
})
|
2018-10-23 01:49:53 +02:00
|
|
|
})
|
|
|
|
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('change-global-header', (event: IpcMainEvent, value: boolean) => {
|
2018-10-23 01:49:53 +02:00
|
|
|
const preferences = new Preferences(preferencesDBPath)
|
2019-04-20 08:44:22 +02:00
|
|
|
preferences
|
|
|
|
.update({
|
2018-10-23 01:49:53 +02:00
|
|
|
state: {
|
|
|
|
hideGlobalHeader: value
|
|
|
|
}
|
|
|
|
})
|
2019-04-20 08:44:22 +02:00
|
|
|
.then(conf => {
|
2018-10-23 01:49:53 +02:00
|
|
|
event.sender.send('response-change-global-header', conf)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
log.error(err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('get-global-header', (event: IpcMainEvent) => {
|
2018-10-23 01:49:53 +02:00
|
|
|
const preferences = new Preferences(preferencesDBPath)
|
2019-04-20 08:44:22 +02:00
|
|
|
preferences.load().then(conf => {
|
|
|
|
event.sender.send('response-get-global-header', conf.state.hideGlobalHeader)
|
|
|
|
})
|
2018-07-03 16:02:16 +02:00
|
|
|
})
|
|
|
|
|
2019-10-22 17:08:56 +02:00
|
|
|
// proxy
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('update-proxy-config', (event: IpcMainEvent, proxy: Proxy) => {
|
2019-10-22 17:08:56 +02:00
|
|
|
const preferences = new Preferences(preferencesDBPath)
|
|
|
|
preferences
|
|
|
|
.update({
|
|
|
|
proxy: proxy
|
|
|
|
})
|
|
|
|
.then(conf => {
|
|
|
|
event.sender.send('response-update-proxy-config', conf)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
log.error(err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-10-27 05:29:49 +01:00
|
|
|
ipcMain.on('get-proxy-configuration', async (event: IpcMainEvent) => {
|
2019-10-27 09:27:00 +01:00
|
|
|
const proxy = await proxyConfiguration.forMastodon()
|
2019-10-27 05:29:49 +01:00
|
|
|
event.sender.send('response-get-proxy-configuration', proxy)
|
|
|
|
})
|
|
|
|
|
2019-10-22 17:08:56 +02:00
|
|
|
// language
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('change-language', (event: IpcMainEvent, value: string) => {
|
2018-08-13 16:48:13 +02:00
|
|
|
const preferences = new Preferences(preferencesDBPath)
|
2019-04-20 08:44:22 +02:00
|
|
|
preferences
|
|
|
|
.update({
|
2018-08-13 16:48:13 +02:00
|
|
|
language: {
|
|
|
|
language: value
|
|
|
|
}
|
|
|
|
})
|
2019-04-20 08:44:22 +02:00
|
|
|
.then(conf => {
|
2020-01-11 13:48:22 +01:00
|
|
|
i18next.changeLanguage(conf.language.language)
|
2018-08-13 16:48:13 +02:00
|
|
|
event.sender.send('response-change-language', conf.language.language)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-06-01 07:19:56 +02:00
|
|
|
// hashtag
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('save-hashtag', (event: IpcMainEvent, tag: string) => {
|
2018-06-01 07:19:56 +02:00
|
|
|
const hashtags = new Hashtags(hashtagsDB)
|
2019-04-20 08:44:22 +02:00
|
|
|
hashtags
|
|
|
|
.insertTag(tag)
|
2018-09-13 18:21:07 +02:00
|
|
|
.then(() => {
|
|
|
|
event.sender.send('response-save-hashtag')
|
|
|
|
})
|
2019-04-20 08:44:22 +02:00
|
|
|
.catch(err => {
|
2018-06-01 07:19:56 +02:00
|
|
|
log.error(err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('list-hashtags', (event: IpcMainEvent) => {
|
2018-06-01 11:26:52 +02:00
|
|
|
const hashtags = new Hashtags(hashtagsDB)
|
2019-04-20 08:44:22 +02:00
|
|
|
hashtags
|
|
|
|
.listTags()
|
|
|
|
.then(tags => {
|
2018-06-01 11:26:52 +02:00
|
|
|
event.sender.send('response-list-hashtags', tags)
|
|
|
|
})
|
2019-04-20 08:44:22 +02:00
|
|
|
.catch(err => {
|
2018-06-01 11:26:52 +02:00
|
|
|
event.sender.send('error-list-hashtags', err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('remove-hashtag', (event: IpcMainEvent, tag: LocalTag) => {
|
2018-06-02 08:30:20 +02:00
|
|
|
const hashtags = new Hashtags(hashtagsDB)
|
2019-04-20 08:44:22 +02:00
|
|
|
hashtags
|
|
|
|
.removeTag(tag)
|
2018-06-02 08:30:20 +02:00
|
|
|
.then(() => {
|
|
|
|
event.sender.send('response-remove-hashtag')
|
|
|
|
})
|
2019-04-20 08:44:22 +02:00
|
|
|
.catch(err => {
|
2018-06-02 08:30:20 +02:00
|
|
|
event.sender.send('error-remove-hashtag', err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-09-25 18:02:36 +02:00
|
|
|
// Fonts
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('list-fonts', (event: IpcMainEvent) => {
|
2018-09-25 18:02:36 +02:00
|
|
|
Fonts()
|
|
|
|
.then(list => {
|
|
|
|
event.sender.send('response-list-fonts', list)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
event.sender.send('error-list-fonts', err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-11-07 14:48:50 +01:00
|
|
|
// Unread notifications
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('get-unread-notification', (event: IpcMainEvent, accountID: string) => {
|
2019-04-20 08:44:22 +02:00
|
|
|
unreadNotification
|
|
|
|
.findOne({
|
|
|
|
accountID: accountID
|
|
|
|
})
|
2018-11-07 14:48:50 +01:00
|
|
|
.then(doc => {
|
|
|
|
event.sender.send('response-get-unread-notification', doc)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
2018-11-08 14:44:58 +01:00
|
|
|
console.warn(err)
|
2018-11-07 14:48:50 +01:00
|
|
|
event.sender.send('error-get-unread-notification', err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('update-unread-notification', (event: IpcMainEvent, config: UnreadNotificationConfig) => {
|
2019-04-16 16:50:53 +02:00
|
|
|
const { accountID } = config
|
2019-04-20 08:44:22 +02:00
|
|
|
unreadNotification
|
|
|
|
.insertOrUpdate(accountID!, config)
|
2018-11-07 14:48:50 +01:00
|
|
|
.then(_ => {
|
|
|
|
event.sender.send('response-update-unread-notification', true)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
event.sender.send('error-update-unread-notification', err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-07-30 17:17:30 +02:00
|
|
|
// Cache
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('get-cache-hashtags', async (event: IpcMainEvent) => {
|
2019-07-31 17:40:28 +02:00
|
|
|
const tags = await hashtagCache.listTags()
|
|
|
|
event.sender.send('response-get-cache-hashtags', tags)
|
2019-07-30 17:17:30 +02:00
|
|
|
})
|
|
|
|
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('insert-cache-hashtags', (event: IpcMainEvent, tags: Array<string>) => {
|
2019-07-31 17:40:28 +02:00
|
|
|
tags.map(async name => {
|
2019-08-29 15:42:32 +02:00
|
|
|
await hashtagCache.insertHashtag(name).catch(err => console.error(err))
|
2019-07-31 17:40:28 +02:00
|
|
|
})
|
2019-08-08 16:39:27 +02:00
|
|
|
event.sender.send('response-insert-cache-hashtags')
|
|
|
|
})
|
|
|
|
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('get-cache-accounts', async (event: IpcMainEvent, ownerID: string) => {
|
2019-08-08 16:39:27 +02:00
|
|
|
const accounts = await accountCache.listAccounts(ownerID)
|
|
|
|
event.sender.send('response-get-cache-accounts', accounts)
|
|
|
|
})
|
|
|
|
|
2019-10-23 16:07:20 +02:00
|
|
|
ipcMain.on('insert-cache-accounts', (event: IpcMainEvent, obj: InsertAccountCache) => {
|
2019-08-08 16:39:27 +02:00
|
|
|
const { ownerID, accts } = obj
|
|
|
|
accts.map(async acct => {
|
2019-08-10 15:30:30 +02:00
|
|
|
await accountCache.insertAccount(ownerID, acct).catch(err => console.error(err))
|
2019-08-08 16:39:27 +02:00
|
|
|
})
|
|
|
|
event.sender.send('response-insert-cache-accounts')
|
2019-07-30 17:17:30 +02:00
|
|
|
})
|
|
|
|
|
2018-08-14 03:18:30 +02:00
|
|
|
// Application control
|
2019-04-17 12:52:01 +02:00
|
|
|
ipcMain.on('relaunch', () => {
|
2018-08-14 03:18:30 +02:00
|
|
|
app.relaunch()
|
|
|
|
app.exit()
|
|
|
|
})
|
|
|
|
|
2018-03-07 14:28:48 +01:00
|
|
|
/**
|
|
|
|
* Auto Updater
|
|
|
|
*
|
|
|
|
* Uncomment the following code below and install `electron-updater` to
|
|
|
|
* support auto updating. Code Signing with a valid certificate is required.
|
|
|
|
* https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
import { autoUpdater } from 'electron-updater'
|
|
|
|
|
|
|
|
autoUpdater.on('update-downloaded', () => {
|
|
|
|
autoUpdater.quitAndInstall()
|
|
|
|
})
|
|
|
|
|
|
|
|
app.on('ready', () => {
|
|
|
|
if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
|
|
|
|
})
|
|
|
|
*/
|
2018-03-13 07:09:54 +01:00
|
|
|
|
|
|
|
class EmptyTokenError {}
|
2018-08-10 17:40:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set application menu
|
|
|
|
*/
|
2020-01-11 13:48:22 +01:00
|
|
|
const ApplicationMenu = (accountsChange: Array<MenuItemConstructorOptions>, i18n: I18n) => {
|
2018-08-10 17:40:06 +02:00
|
|
|
/**
|
|
|
|
* For mac menu
|
|
|
|
*/
|
2019-04-20 08:44:22 +02:00
|
|
|
const macGeneralMenu: Array<MenuItemConstructorOptions> =
|
|
|
|
process.platform !== 'darwin'
|
|
|
|
? []
|
|
|
|
: [
|
|
|
|
{
|
|
|
|
type: 'separator'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: i18n.t('main_menu.application.services'),
|
2019-10-23 16:07:20 +02:00
|
|
|
role: 'services'
|
2019-04-20 08:44:22 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'separator'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: i18n.t('main_menu.application.hide'),
|
|
|
|
role: 'hide'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: i18n.t('main_menu.application.hide_others'),
|
2019-10-23 16:07:20 +02:00
|
|
|
role: 'hideOthers'
|
2019-04-20 08:44:22 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: i18n.t('main_menu.application.show_all'),
|
|
|
|
role: 'unhide'
|
|
|
|
}
|
|
|
|
]
|
2018-08-10 17:40:06 +02:00
|
|
|
|
2019-04-17 12:52:01 +02:00
|
|
|
const template: Array<MenuItemConstructorOptions> = [
|
2018-08-10 17:40:06 +02:00
|
|
|
{
|
|
|
|
label: i18n.t('main_menu.application.name'),
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
label: i18n.t('main_menu.application.about'),
|
|
|
|
role: 'about',
|
|
|
|
click: () => {
|
|
|
|
openAboutWindow({
|
|
|
|
icon_path: path.resolve(__dirname, '../../build/icons/256x256.png'),
|
|
|
|
copyright: 'Copyright (c) 2018 AkiraFukushima',
|
|
|
|
package_json_dir: path.resolve(__dirname, '../../'),
|
2019-08-23 18:36:33 +02:00
|
|
|
open_devtools: process.env.NODE_ENV !== 'production',
|
|
|
|
win_options: {
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: true
|
|
|
|
}
|
|
|
|
}
|
2018-08-10 17:40:06 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'separator'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: i18n.t('main_menu.application.preferences'),
|
|
|
|
accelerator: 'CmdOrCtrl+,',
|
|
|
|
click: () => {
|
2019-04-15 18:35:20 +02:00
|
|
|
mainWindow!.webContents.send('open-preferences')
|
2018-08-10 17:40:06 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
...macGeneralMenu,
|
|
|
|
{
|
|
|
|
type: 'separator'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: i18n.t('main_menu.application.quit'),
|
|
|
|
accelerator: 'CmdOrCtrl+Q',
|
|
|
|
role: 'quit'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: i18n.t('main_menu.toot.name'),
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
label: i18n.t('main_menu.toot.new'),
|
|
|
|
accelerator: 'CmdOrCtrl+N',
|
|
|
|
click: () => {
|
2019-04-15 18:35:20 +02:00
|
|
|
mainWindow!.webContents.send('CmdOrCtrl+N')
|
2018-08-10 17:40:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: i18n.t('main_menu.edit.name'),
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
label: i18n.t('main_menu.edit.undo'),
|
|
|
|
accelerator: 'CmdOrCtrl+Z',
|
|
|
|
role: 'undo'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: i18n.t('main_menu.edit.redo'),
|
|
|
|
accelerator: 'Shift+CmdOrCtrl+Z',
|
|
|
|
role: 'redo'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'separator'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: i18n.t('main_menu.edit.cut'),
|
|
|
|
accelerator: 'CmdOrCtrl+X',
|
|
|
|
role: 'cut'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: i18n.t('main_menu.edit.copy'),
|
|
|
|
accelerator: 'CmdOrCtrl+C',
|
|
|
|
role: 'copy'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: i18n.t('main_menu.edit.paste'),
|
|
|
|
accelerator: 'CmdOrCtrl+V',
|
|
|
|
role: 'paste'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: i18n.t('main_menu.edit.select_all'),
|
|
|
|
accelerator: 'CmdOrCtrl+A',
|
|
|
|
role: 'selectall'
|
|
|
|
}
|
2019-10-23 16:07:20 +02:00
|
|
|
] as Array<MenuItemConstructorOptions>
|
2018-08-10 17:40:06 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: i18n.t('main_menu.view.name'),
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
label: i18n.t('main_menu.view.toggle_full_screen'),
|
|
|
|
role: 'togglefullscreen'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: i18n.t('main_menu.window.name'),
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
label: i18n.t('main_menu.window.close'),
|
|
|
|
role: 'close'
|
|
|
|
},
|
2018-10-10 02:05:21 +02:00
|
|
|
{
|
|
|
|
label: i18n.t('main_menu.window.open'),
|
|
|
|
enabled: false,
|
|
|
|
click: () => {
|
|
|
|
reopenWindow()
|
|
|
|
}
|
|
|
|
},
|
2018-08-10 17:40:06 +02:00
|
|
|
{
|
|
|
|
label: i18n.t('main_menu.window.minimize'),
|
|
|
|
role: 'minimize'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'separator'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: i18n.t('main_menu.window.jump_to'),
|
|
|
|
accelerator: 'CmdOrCtrl+K',
|
|
|
|
enabled: true,
|
|
|
|
click: () => {
|
2019-04-15 18:35:20 +02:00
|
|
|
mainWindow!.webContents.send('CmdOrCtrl+K')
|
2018-08-10 17:40:06 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'separator'
|
|
|
|
},
|
|
|
|
...accountsChange
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2019-04-17 12:52:01 +02:00
|
|
|
const menu: Menu = Menu.buildFromTemplate(template)
|
2018-08-10 17:40:06 +02:00
|
|
|
Menu.setApplicationMenu(menu)
|
|
|
|
}
|
2018-10-10 02:05:21 +02:00
|
|
|
|
2020-01-11 13:48:22 +01:00
|
|
|
const TrayMenu = (accountsChange: Array<MenuItemConstructorOptions>, i18n: I18n): Menu => {
|
2019-08-18 06:18:40 +02:00
|
|
|
const template: Array<MenuItemConstructorOptions> = [
|
|
|
|
...accountsChange,
|
2019-10-13 09:07:35 +02:00
|
|
|
{
|
|
|
|
label: i18n.t('main_menu.application.open'),
|
|
|
|
click: async () => {
|
|
|
|
if (mainWindow) {
|
|
|
|
mainWindow.show()
|
|
|
|
} else {
|
|
|
|
await createWindow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2019-08-18 06:18:40 +02:00
|
|
|
{
|
|
|
|
label: i18n.t('main_menu.application.quit'),
|
|
|
|
click: () => {
|
|
|
|
mainWindow!.destroy()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
const menu: Menu = Menu.buildFromTemplate(template)
|
|
|
|
return menu
|
|
|
|
}
|
|
|
|
|
2019-04-20 08:44:22 +02:00
|
|
|
async function reopenWindow() {
|
2018-10-10 02:05:21 +02:00
|
|
|
if (mainWindow === null) {
|
|
|
|
await createWindow()
|
|
|
|
return null
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
2019-07-21 17:19:23 +02:00
|
|
|
|
2020-03-15 09:47:40 +01:00
|
|
|
const createNotification = (notification: Entity.Notification, notifyConfig: Notify): NotificationConstructorOptions | null => {
|
2019-07-21 17:19:23 +02:00
|
|
|
switch (notification.type) {
|
|
|
|
case 'favourite':
|
|
|
|
if (notifyConfig.favourite) {
|
|
|
|
return {
|
2020-01-11 13:48:22 +01:00
|
|
|
title: i18next.t('notification.favourite.title'),
|
|
|
|
body: i18next.t('notification.favourite.body', { username: username(notification.account) }),
|
2019-07-21 17:19:23 +02:00
|
|
|
silent: false
|
|
|
|
} as NotificationConstructorOptions
|
|
|
|
}
|
|
|
|
break
|
|
|
|
case 'follow':
|
|
|
|
if (notifyConfig.follow) {
|
|
|
|
return {
|
2020-01-11 13:48:22 +01:00
|
|
|
title: i18next.t('notification.follow.title'),
|
|
|
|
body: i18next.t('notification.follow.body', { username: username(notification.account) }),
|
2019-07-21 17:19:23 +02:00
|
|
|
silent: false
|
|
|
|
} as NotificationConstructorOptions
|
|
|
|
}
|
|
|
|
break
|
|
|
|
case 'mention':
|
|
|
|
if (notifyConfig.reply) {
|
|
|
|
return {
|
|
|
|
title: `${username(notification.status!.account)}`,
|
|
|
|
body: sanitizeHtml(notification.status!.content, {
|
|
|
|
allowedTags: [],
|
|
|
|
allowedAttributes: []
|
|
|
|
}),
|
|
|
|
silent: false
|
|
|
|
} as NotificationConstructorOptions
|
|
|
|
}
|
|
|
|
break
|
|
|
|
case 'reblog':
|
|
|
|
if (notifyConfig.reblog) {
|
|
|
|
return {
|
2020-01-11 13:48:22 +01:00
|
|
|
title: i18next.t('notification.reblog.title'),
|
|
|
|
body: i18next.t('notification.reblog.body', { username: username(notification.account) }),
|
2019-07-21 17:19:23 +02:00
|
|
|
silent: false
|
|
|
|
} as NotificationConstructorOptions
|
|
|
|
}
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2020-03-15 09:47:40 +01:00
|
|
|
const username = (account: Entity.Account): string => {
|
2019-07-21 17:19:23 +02:00
|
|
|
if (account.display_name !== '') {
|
|
|
|
return account.display_name
|
|
|
|
} else {
|
|
|
|
return account.username
|
|
|
|
}
|
|
|
|
}
|