Skip to content

Commit

Permalink
implemented basic chat service
Browse files Browse the repository at this point in the history
  • Loading branch information
roncodes committed Apr 3, 2024
1 parent 464ecca commit 1198f4a
Showing 1 changed file with 86 additions and 2 deletions.
88 changes: 86 additions & 2 deletions addon/services/chat.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,87 @@
import Service from '@ember/service';
import Service, { inject as service } from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { isArray } from '@ember/array';
import { task } from 'ember-concurrency';

export default class ChatService extends Service {}
export default class ChatService extends Service {
@service store;
@service currentUser;
@tracked channels = [];
@tracked openChannels = [];

openChannel(chatChannelRecord) {
this.openChannels.pushObject(chatChannelRecord);
}

closeChannel(chatChannelRecord) {
const index = this.openChannels.findIndex((_) => _.id === chatChannelRecord.id);
if (index >= 0) {
this.openChannels.removeAt(index);
}
}

getOpenChannels() {
return this.openChannels;
}

createChatChannel(name) {
const chatChannel = this.store.createRecord('chat-channel', { name });
return chatChannel.save();
}

deleteChatChannel(chatChannelRecord) {
return chatChannelRecord.destroyRecord();
}

updateChatChannel(chatChannelRecord, props = {}) {
chatChannelRecord.setProperties(props);
return chatChannelRecord.save();
}

addParticipant(chatChannelRecord, userRecord) {
const chatParticipant = this.store.createRecord('chat-participant', {
chat_channel_uuid: chatChannelRecord.id,
user_uuid: userRecord.id,
});
return chatParticipant.save();
}

removeParticipant(chatParticipant) {
return chatParticipant.destroyRecord();
}

async sendMessage(chatChannelRecord, senderRecord, messageContent = '') {
const chatMessage = this.store.createRecord('chat-message', {
chat_channel_uuid: chatChannelRecord.id,
sender_uuid: senderRecord.id,
content: messageContent,
});
await chatMessage.save();
chatChannelRecord.messages.pushObject(chatMessage);
await this.loadMessages.perform(chatChannelRecord);
}

deleteMessage(chatMessageRecord) {
return chatMessageRecord.destroyRecord();
}

@task *loadMessages(chatChannelRecord) {
const messages = yield this.store.query('chat-message', { chat_channel_uuid: chatChannelRecord.id });
chatChannelRecord.set('messages', messages);
return messages;
}

@task *loadChannels(options = {}) {
const params = options.params || {};
const channels = yield this.store.query('chat-channel', params);
if (isArray(channels)) {
this.channels = channels;
}

if (typeof options.withChannels === 'function') {
options.withChannels(channels);
}

return channels;
}
}

0 comments on commit 1198f4a

Please sign in to comment.