fix: code review

This commit is contained in:
Richard Ramos 2021-04-06 13:29:36 -04:00 committed by Jonathan Rainville
parent 61555d610b
commit 8543bf04d4
1 changed files with 15 additions and 26 deletions

View File

@ -77,11 +77,9 @@ proc newChatModel*(events: EventEmitter): ChatModel =
proc delete*(self: ChatModel) = proc delete*(self: ChatModel) =
discard discard
proc update*(self: ChatModel, chats: seq[Chat], messages: seq[Message], emojiReactions: seq[Reaction], communities: seq[Community], communityMembershipRequests: seq[CommunityMembershipRequest]) = proc cleanSpamChatGroups(self: ChatModel, chats: seq[Chat], contacts: seq[Profile]): seq[Chat] =
var contacts = getAddedContacts()
# Automatically decline chat group invitations if admin is not a contact
for chat in chats: for chat in chats:
if not chat.isActive: continue
if chat.chatType == ChatType.PrivateGroupChat: if chat.chatType == ChatType.PrivateGroupChat:
var isContact = false var isContact = false
var joined = false var joined = false
@ -94,8 +92,18 @@ proc update*(self: ChatModel, chats: seq[Chat], messages: seq[Message], emojiRea
isContact = true isContact = true
if not isContact and not joined: if not isContact and not joined:
status_chat.deactivateChat(chat) status_chat.deactivateChat(chat)
continue else:
result.add(chat)
else:
result.add(chat)
proc update*(self: ChatModel, chats: seq[Chat], messages: seq[Message], emojiReactions: seq[Reaction], communities: seq[Community], communityMembershipRequests: seq[CommunityMembershipRequest]) =
var contacts = getAddedContacts()
# Automatically decline chat group invitations if admin is not a contact
var chatList = self.cleanSpamChatGroups(chats, contacts)
for chat in chatList:
if chat.isActive: if chat.isActive:
self.channels[chat.id] = chat self.channels[chat.id] = chat
@ -108,7 +116,7 @@ proc update*(self: ChatModel, chats: seq[Chat], messages: seq[Message], emojiRea
if self.lastMessageTimestamps[chatId] > ts: if self.lastMessageTimestamps[chatId] > ts:
self.lastMessageTimestamps[chatId] = ts self.lastMessageTimestamps[chatId] = ts
self.events.emit("chatUpdate", ChatUpdateArgs(messages: messages, chats: chats, contacts: @[], emojiReactions: emojiReactions, communities: communities, communityMembershipRequests: communityMembershipRequests)) self.events.emit("chatUpdate", ChatUpdateArgs(messages: messages, chats: chatList, contacts: @[], emojiReactions: emojiReactions, communities: communities, communityMembershipRequests: communityMembershipRequests))
proc hasChannel*(self: ChatModel, chatId: string): bool = proc hasChannel*(self: ChatModel, chatId: string): bool =
self.channels.hasKey(chatId) self.channels.hasKey(chatId)
@ -153,31 +161,12 @@ proc updateContacts*(self: ChatModel, contacts: seq[Profile]) =
self.events.emit("chatUpdate", ChatUpdateArgs(contacts: contacts)) self.events.emit("chatUpdate", ChatUpdateArgs(contacts: contacts))
proc deleteSpamGroupChats(self: ChatModel, contacts: seq[Profile]) =
var chats = status_chat.loadChats()
for chat in chats:
if chat.chatType == ChatType.PrivateGroupChat:
var isContact = false
var joined = false
for member in chat.members:
if member.id == self.publicKey and member.joined:
joined = true
if member.admin and member.joined:
for contact in contacts:
if contact.address == member.id:
isContact = true
if not isContact and not joined:
status_chat.deactivateChat(chat)
proc init*(self: ChatModel, pubKey: string) = proc init*(self: ChatModel, pubKey: string) =
self.publicKey = pubKey self.publicKey = pubKey
var contacts = getAddedContacts() var contacts = getAddedContacts()
self.deleteSpamGroupChats(contacts) var chatList = self.cleanSpamChatGroups(status_chat.loadChats(), contacts)
var chatList = status_chat.loadChats()
let profileUpdatesChatIds = chatList.filter(c => c.chatType == ChatType.Profile).map(c => c.id) let profileUpdatesChatIds = chatList.filter(c => c.chatType == ChatType.Profile).map(c => c.id)