Skip to content

Commit

Permalink
#3 - Add datetime in logs; Add method to get subscription for unsubsc…
Browse files Browse the repository at this point in the history
…ribe.
  • Loading branch information
Felipe Tomm committed Nov 27, 2020
1 parent 654e80a commit 54acc59
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions dist/lib/driver.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { IMessage } from '../config/messageInterfaces';
import { IMessageReceiptAPI } from '../utils/interfaces';
/** Internal for comparing message update timestamps */
export declare let lastReadTime: Date;
export declare const eventMessages: string[];
/**
* The integration property is applied as an ID on sent messages `bot.i` param
* Should be replaced when connection is invoked by a package using the SDK
Expand Down
3 changes: 3 additions & 0 deletions dist/lib/driver.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/lib/driver.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rocketchat-js-sdk",
"version": "0.2.9-7",
"version": "0.2.9-8",
"description": "Node.js Forked SDK for Rocket.Chat. Application interface for server methods and message streams.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
21 changes: 21 additions & 0 deletions src/lib/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,17 @@ export function unsubscribe (subscription: ISubscription): void {
logger.info(`[${subscription.id}] Unsubscribed`)
}

/**
* Get Subscription by room id
* @param rid Room id for filter subscriptions
*/
export function getSubscription (rid: string): ISubscription[] {
const response = subscriptions.filter(function (sub) {
return sub.id = rid
})
return response
}

/** Unsubscribe from all subscriptions in collection */
export function unsubscribeAll (): void {
subscriptions.map((s: ISubscription) => unsubscribe(s))
Expand Down Expand Up @@ -398,6 +409,16 @@ export function respondToMessages (
// In RocketChat Server ^3.8.0, the message has changed for Object to an Array. Get only first pos of array
message = Array.isArray(message) ? message[0] : message

let { t } = message
const doUnsubscribe = ((t === 'ul' && (message.u._id === userId)) || (t === 'uj' && meta.roomParticipant))
if (doUnsubscribe) {
const subscription = getSubscription(message.rid)
subscription.forEach(sub => {
unsubscribe(sub)
})
return
}

// Ignore bot's own messages
if (message.u._id === userId) return

Expand Down
8 changes: 4 additions & 4 deletions src/lib/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import { ILogger } from '../config/driverInterfaces'
/** Temp logging, should override form adapter's log */
class InternalLog implements ILogger {
debug (...args: any[]) {
console.log(...args)
console.log([`[debug] ${Date.now()}: `,...args])
}
info (...args: any[]) {
console.log(...args)
console.log([`[info] ${Date.now()}: `,...args])
}
warning (...args: any[]) {
console.warn(...args)
console.warn([`[warning] ${Date.now()}: `,...args])
}
warn (...args: any[]) { // legacy method
return this.warning(...args)
}
error (...args: any[]) {
console.error(...args)
console.error([`[error] ${Date.now()}: `,...args])
}
}

Expand Down
2 changes: 1 addition & 1 deletion target/npmlist.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rocketchat-js-sdk",
"version": "0.2.9-7",
"version": "0.2.9-8",
"dependencies": {
"@types/lru-cache": {
"version": "4.1.0"
Expand Down

0 comments on commit 54acc59

Please sign in to comment.