Skip to content

Commit

Permalink
feature works.
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Dec 8, 2018
1 parent 9b89af1 commit 6b46185
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/frontend/src/data/models/message/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ export default class Message extends Model {

@attr() sendError?: string;

/**
* When a user comes online, they dispatch a bunch of pings to their contacts.
* If any of those contacts have queue messages (designated by this boolean)
* the messages will automatically be sent to the user who jest came online
* */
@attr() queueForResend?: boolean;

@belongsTo('identity', { async: false }) sender?: Identity;

// @belongsTo('message', { async: false, inverse: 'deliveryConfirmations' }) confirmationFor?: Message;
Expand Down
14 changes: 14 additions & 0 deletions packages/frontend/src/services/messages/auto-responder.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import Service from '@ember/service';
import { service } from '@ember-decorators/service';
import StoreService from 'ember-data/store';

import MessageDispatcher from 'emberclear/src/services/messages/dispatcher';
import MessageFactory from 'emberclear/src/services/messages/factory';
import Message from 'emberclear/src/data/models/message/model';
import Identity from 'emberclear/src/data/models/identity/model';

/**
* Nothing here should be blocking, as these responses should not matter
Expand All @@ -14,13 +16,25 @@ import Message from 'emberclear/src/data/models/message/model';
export default class MessageAutoResponder extends Service {
@service('messages/dispatcher') dispatcher!: MessageDispatcher;
@service('messages/factory') factory!: MessageFactory;
@service store!: StoreService;

async messageReceived(respondToMessage: Message) {
const sender = await respondToMessage.sender;
const response = this.factory.buildDeliveryConfirmation(respondToMessage);

this.dispatcher.sendToUser.perform(response, sender);
}

async cameOnline(identity: Identity) {
const pendingMessages = await this.store.query('message', {
queueForResend: true,
to: identity.uid,
});

pendingMessages.forEach((message: Message) => {
this.dispatcher.sendToUser.perform(message, identity);
});
}
}

// DO NOT DELETE: this is how TypeScript knows how to look up your services.
Expand Down
7 changes: 7 additions & 0 deletions packages/frontend/src/services/messages/dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ export default class MessageDispatcher extends Service {
this.sendTo(message, to);
}

// there needs to be a polymorphic relationship in order for this to work
// sendMessage(message: Message) {
// return sendTo(message, message.to);
// }

async sendTo(message: Message, to: Identity | Channel) {
message.set('queueForResend', false);

if (to instanceof Identity) {
if (to.id === 'me') return;

Expand Down
1 change: 1 addition & 0 deletions packages/frontend/src/services/messages/handler/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export default class ReceivedMessageHandler extends Service {
const sender = await this.findOrCreateSender(senderInfo);

this.statusManager.markOnline(sender);
this.autoResponder.cameOnline(sender);

const message = this.store.createRecord('message', {
id,
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/src/services/toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default class Toast extends Service {
...options,
message: message || 'status',
type: status,
cssClasses: `notification ${status} p-xs has-shadow height-tall`,
cssClasses: `notification ${status} is-${status} p-xs has-shadow height-tall`,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,13 @@ export default class DeliveryConfirmation extends Component<IArgs> {

yield message.destroyRecord();
}

@dropTask
*resendAutomatically(this: DeliveryConfirmation) {
const { message } = this.args;

message.set('queueForResend', true);

yield message.save();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
{{#if this.resend.isIdle}}
<a {{action (perform this.resend)}}>{{t 'buttons.resend'}}</a>
{{/if}}
|
{{#if this.resendAutomatically.isIdle}}
{{#if @message.queueForResend}}
{{t 'models.message.autosendPending'}}
{{else}}
<a {{action (perform this.resendAutomatically)}}>{{t 'buttons.resendAutomatically'}}</a>
{{/if}}
{{/if}}

{{fa-icon 'exclamation-circle'}}

Expand Down
2 changes: 2 additions & 0 deletions packages/frontend/translations/en-us.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ connection:
buttons:
delete: delete
resend: resend
resendAutomatically: resend automatically
save: Save
next: Next
back: Back
Expand Down Expand Up @@ -89,6 +90,7 @@ models:
publicKey: Public Key

message:
autosendPending: autosend pending
errors:
timeout: 'Sending timed out.'

Expand Down

0 comments on commit 6b46185

Please sign in to comment.