Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle switch from buddies to participants in 14.0+ #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ const macosVersion = require('macos-version')

const versions = require('./macos_versions')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MacMijabr
R

const currentVersion = macosVersion()
const isParticipant = (macosVersion.is('>=14.0')) ? true : false

const messagesDb = require('./lib/messages-db.js')

@@ -75,33 +76,33 @@ function packTimeConditionally(ts) {
// Gets the proper handle string for a contact with the given name
function handleForName(name) {
assert(typeof name == 'string', 'name must be a string')
return osa(name => {
return osa((name, isParticipant) => {
const Messages = Application('Messages')
return Messages.buddies.whose({ name: name })[0].handle()
})(name)
return (isParticipant ? Messages.participants.whose({ name: name })[0].handle() : Messages.buddies.whose({ name: name })[0].handle())
})(name, isParticipant)
}

// Gets the display name for a given handle
// TODO: support group chats
function nameForHandle(handle) {
assert(typeof handle == 'string', 'handle must be a string')
return osa(handle => {
return osa((handle, isParticipant) => {
const Messages = Application('Messages')
return Messages.buddies.whose({ handle: handle }).name()[0]
})(handle)
return (isParticipant ? Messages.participants.whose({ handle: handle })[0].name() : Messages.buddies.whose({ handle: handle })[0].name())
})(handle, isParticipant)
}

// Sends a message to the given handle
function send(handle, message) {
assert(typeof handle == 'string', 'handle must be a string')
assert(typeof message == 'string', 'message must be a string')
return osa((handle, message) => {
return osa((handle, message, isParticipant) => {
const Messages = Application('Messages')

let target

try {
target = Messages.buddies.whose({ handle: handle })[0]
target = isParticipant ? Messages.participants.whose({ handle: handle })[0] : Messages.buddies.whose({ handle: handle })[0]
} catch (e) {}

try {
@@ -113,19 +114,20 @@ function send(handle, message) {
} catch (e) {
throw new Error(`no thread with handle '${handle}'`)
}
})(handle, message)
})(handle, message, isParticipant)
}

let emitter = null
let emittedMsgs = []

function listen() {
// If listen has already been run, return the existing emitter
if (emitter != null) {
return emitter
}

// Create an EventEmitter
emitter = new (require('events')).EventEmitter()
emitter = new(require('events')).EventEmitter()

let last = packTimeConditionally(appleTimeNow() - 5)
let bail = false
@@ -208,4 +210,4 @@ module.exports = {
nameForHandle,
getRecentChats,
SUPPRESS_WARNINGS: false,
}
}