refactor(community): add join community functionnality

This commit is contained in:
Jonathan Rainville 2021-12-16 10:24:25 -05:00 committed by Sale Djenic
parent fdca0da77d
commit 0388688e9e
21 changed files with 448 additions and 193 deletions

View File

@ -142,7 +142,7 @@ proc newAppController*(statusFoundation: StatusFoundation): AppController =
result.networkService = network_service.newService()
result.contactsService = contacts_service.newService(statusFoundation.status.events, statusFoundation.threadpool)
result.chatService = chat_service.newService(statusFoundation.status.events, result.contactsService)
result.communityService = community_service.newService(result.chatService)
result.communityService = community_service.newService(statusFoundation.status.events)
result.messageService = message_service.newService(statusFoundation.status.events, statusFoundation.threadpool)
result.activityCenterService = activity_center_service.newService(statusFoundation.status.events, statusFoundation.threadpool, result.chatService)
result.tokenService = token_service.newService(statusFoundation.status.events, statusFoundation.threadpool,

View File

@ -31,3 +31,6 @@ method init*[T](self: Controller[T]) =
let communities = self.communityService.getAllCommunities()
self.delegate.setAllCommunities(communities)
method joinCommunity*[T](self: Controller[T], communityId: string): string =
self.communityService.joinCommunity(communityId)

View File

@ -10,6 +10,9 @@ method delete*(self: AccessInterface) {.base.} =
method init*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")
method joinCommunity*(self: AccessInterface, communityId: string): string {.base.} =
raise newException(ValueError, "No implementation available")
type
## Abstract class (concept) which must be implemented by object/s used in this
## module.

View File

@ -19,6 +19,9 @@ method viewDidLoad*(self: AccessInterface) {.base.} =
method setAllCommunities*(self: AccessInterface, communities: seq[CommunityDto]) {.base.} =
raise newException(ValueError, "No implementation available")
method joinCommunity*(self: AccessInterface, communityId: string): string {.base.} =
raise newException(ValueError, "No implementation available")
type
## Abstract class (concept) which must be implemented by object/s used in this
## module.

View File

@ -2,7 +2,7 @@ import NimQml, sequtils
import eventemitter
import ./io_interface, ./view, ./controller
import ../item
import ../../shared_models/section_item
import ../../../global/global_singleton
import ../../../../app_service/service/community/service as community_service
@ -56,5 +56,18 @@ method setAllCommunities*[T](self: Module[T], communities: seq[CommunityDto]) =
c.description,
c.images.thumbnail,
icon = "",
c.color)
c.color,
hasNotification = false,
notificationsCount = 0,
active = false,
enabled = true,
c.joined,
c.canJoin,
c.canRequestAccess,
c.isMember,
c.permissions.access,
c.permissions.ensOnly)
self.view.addItem(communityItem)
method joinCommunity*[T](self: Module[T], communityId: string): string =
self.controller.joinCommunity(communityId)

View File

@ -1,18 +1,24 @@
import NimQml, json, strutils, json_serialization, sequtils
import ./io_interface
import ../model, ../item
import ../../shared_models/section_model
import ../../shared_models/section_item
import ../../shared_models/active_section
QtObject:
type
View* = ref object of QObject
delegate: io_interface.AccessInterface
model: Model
model: SectionModel
modelVariant: QVariant
observedItem: ActiveSection
observedItemVariant: QVariant
proc delete*(self: View) =
self.model.delete
self.modelVariant.delete
self.observedItem.delete
self.observedItemVariant.delete
self.QObject.delete
proc newView*(delegate: io_interface.AccessInterface): View =
@ -21,11 +27,13 @@ QtObject:
result.delegate = delegate
result.model = newModel()
result.modelVariant = newQVariant(result.model)
result.observedItem = newActiveSection()
result.observedItemVariant = newQVariant(result.observedItem)
proc load*(self: View) =
self.delegate.viewDidLoad()
proc addItem*(self: View, item: Item) =
proc addItem*(self: View, item: SectionItem) =
self.model.addItem(item)
proc getModel(self: View): QVariant {.slot.} =
@ -33,4 +41,22 @@ QtObject:
QtProperty[QVariant] model:
read = getModel
proc observedItemChanged*(self:View) {.signal.}
proc getObservedItem(self: View): QVariant {.slot.} =
return self.observedItemVariant
QtProperty[QVariant] observedCommunity:
read = getObservedItem
notify = observedItemChanged
proc setObservedCommunity*(self: View, itemId: string) {.slot.} =
let item = self.model.getItemById(itemId)
if (item.id == ""):
return
self.observedItem.setActiveSectionData(item)
proc joinCommunity*(self: View, communityId: string): string {.slot.} =
result = self.delegate.joinCommunity(communityId)

View File

@ -1,4 +1,4 @@
import item, controller_interface, io_interface, chronicles
import ../shared_models/section_item, controller_interface, io_interface, chronicles
import ../../global/global_singleton
import ../../../app_service/service/settings/service_interface as settings_service
@ -6,6 +6,8 @@ import ../../../app_service/service/keychain/service as keychain_service
import ../../../app_service/service/accounts/service_interface as accounts_service
import ../../../app_service/service/chat/service as chat_service
import ../../../app_service/service/community/service as community_service
import ../../../app_service/service/contacts/service as contacts_service
import ../../../app_service/service/message/service as message_service
import ../../core/signals/types
import eventemitter
@ -24,6 +26,8 @@ type
accountsService: accounts_service.ServiceInterface
chatService: chat_service.Service
communityService: community_service.ServiceInterface
messageService: message_service.Service
contactsService: contacts_service.Service
activeSectionId: string
proc newController*(delegate: io_interface.AccessInterface,
@ -32,7 +36,9 @@ proc newController*(delegate: io_interface.AccessInterface,
keychainService: keychain_service.Service,
accountsService: accounts_service.ServiceInterface,
chatService: chat_service.Service,
communityService: community_service.ServiceInterface):
communityService: community_service.ServiceInterface,
contactsService: contacts_service.Service,
messageService: message_service.Service):
Controller =
result = Controller()
result.delegate = delegate
@ -42,6 +48,8 @@ proc newController*(delegate: io_interface.AccessInterface,
result.accountsService = accountsService
result.chatService = chatService
result.communityService = communityService
result.contactsService = contactsService
result.messageService = messageService
method delete*(self: Controller) =
discard
@ -67,6 +75,17 @@ method init*(self: Controller) =
singletonInstance.localAccountSettings.removeKey(LS_KEY_STORE_TO_KEYCHAIN)
self.delegate.emitStoringPasswordError(args.errDescription)
self.events.on(SIGNAL_COMMUNITY_JOINED) do(e:Args):
let args = CommunityArgs(e)
self.delegate.communityJoined(
args.community,
self.events,
self.contactsService,
self.chatService,
self.communityService,
self.messageService
)
self.events.on("sectionAvailabilityChanged") do(e:Args):
## We will receive here a signal with two fields:
## sectionType: int

View File

@ -1,4 +1,4 @@
import item
import ../shared_models/section_item
import ../../../app_service/service/community/service_interface as community_service
type

View File

@ -1,113 +0,0 @@
import json, strformat
type
SectionType* {.pure.} = enum
Chat = 0
Community,
Wallet,
WalletV2,
Browser,
ProfileSettings,
NodeManagement
type
Item* = object
sectionType: SectionType
id: string
name: string
description: string
image: string
icon: string
color: string
hasNotification: bool
notificationsCount: int
active: bool
enabled: bool
proc initItem*(
id: string,
sectionType: SectionType,
name,
description = "",
image = "",
icon = "",
color = "",
hasNotification = false,
notificationsCount: int = 0,
active = false,
enabled = true
): Item =
result.id = id
result.sectionType = sectionType
result.name = name
result.description = description
result.image = image
result.icon = icon
result.color = color
result.hasNotification = hasNotification
result.notificationsCount = notificationsCount
result.active = active
result.enabled = enabled
proc isEmpty*(self: Item): bool =
return self.id.len == 0
proc `$`*(self: Item): string =
result = fmt"""MainModuleItem(
id: {self.id},
sectionType: {self.sectionType.int},
name: {self.name},
description: {self.description},
image: {self.image},
icon: {self.icon},
color: {self.color},
hasNotification: {self.hasNotification},
notificationsCount:{self.notificationsCount},
active:{self.active},
enabled:{self.enabled}
]"""
proc id*(self: Item): string {.inline.} =
self.id
proc sectionType*(self: Item): SectionType {.inline.} =
self.sectionType
proc name*(self: Item): string {.inline.} =
self.name
proc description*(self: Item): string {.inline.} =
self.description
proc image*(self: Item): string {.inline.} =
self.image
proc icon*(self: Item): string {.inline.} =
self.icon
proc color*(self: Item): string {.inline.} =
self.color
proc hasNotification*(self: Item): bool {.inline.} =
self.hasNotification
proc `hasNotification=`*(self: var Item, value: bool) {.inline.} =
self.hasNotification = value
proc notificationsCount*(self: Item): int {.inline.} =
self.notificationsCount
proc `notificationsCount=`*(self: var Item, value: int) {.inline.} =
self.notificationsCount = value
proc active*(self: Item): bool {.inline.} =
self.active
proc `active=`*(self: var Item, value: bool) {.inline.} =
self.active = value
proc enabled*(self: Item): bool {.inline.} =
self.enabled
proc `enabled=`*(self: var Item, value: bool) {.inline.} =
self.enabled = value

View File

@ -1,6 +1,6 @@
import NimQml, Tables
import io_interface, view, controller, item, model
import io_interface, view, controller, ../shared_models/section_item, ../shared_models/section_model
import ../../global/app_sections_config as conf
import ../../global/global_singleton
@ -88,7 +88,15 @@ proc newModule*[T](
result.view = view.newView(result)
result.viewVariant = newQVariant(result.view)
result.controller = controller.newController(
result, events, settingsService, keychainService, accountsService, chatService, communityService
result,
events,
settingsService,
keychainService,
accountsService,
chatService,
communityService,
contactsService,
messageService
)
result.moduleLoaded = false
@ -127,6 +135,23 @@ method delete*[T](self: Module[T]) =
self.viewVariant.delete
self.controller.delete
method createCommunityItem*[T](self: Module[T], c: CommunityDto): SectionItem =
let (unviewedCount, mentionsCount) = self.controller.getNumOfNotificationsForCommunity(c.id)
let hasNotification = unviewedCount > 0 or mentionsCount > 0
let notificationsCount = mentionsCount # we need to add here number of requests
result = initItem(
c.id,
SectionType.Community,
c.name,
c.description,
c.images.thumbnail,
"",
c.color,
hasNotification,
notificationsCount,
false,
singletonInstance.localAccountSensitiveSettings.getCommunitiesEnabled())
method load*[T](
self: Module[T],
events: EventEmitter,
@ -147,14 +172,14 @@ method load*[T](
self,
events,
c.id,
true,
isCommunity = true,
contactsService,
chatService,
communityService,
messageService
)
var activeSection: Item
var activeSection: SectionItem
var activeSectionId = singletonInstance.localAccountSensitiveSettings.getActiveSection()
# Chat Section
@ -170,11 +195,7 @@ method load*[T](
# Community Section
for c in joinedCommunities:
let (unviewedCount, mentionsCount) = self.controller.getNumOfNotificationsForCommunity(c.id)
let hasNotification = unviewedCount > 0 or mentionsCount > 0
let notificationsCount = mentionsCount # we need to add here number of requests
let communitySectionItem = initItem(c.id, SectionType.Community, c.name, c.description, c.images.thumbnail, "",
c.color, hasNotification, notificationsCount, false, singletonInstance.localAccountSensitiveSettings.getCommunitiesEnabled())
let communitySectionItem = self.createCommunityItem(c)
self.view.addItem(communitySectionItem)
if(activeSectionId == communitySectionItem.id):
activeSection = communitySectionItem
@ -316,7 +337,7 @@ method emitStoringPasswordError*[T](self: Module[T], errorDescription: string) =
method emitStoringPasswordSuccess*[T](self: Module[T]) =
self.view.emitStoringPasswordSuccess()
method setActiveSection*[T](self: Module[T], item: Item) =
method setActiveSection*[T](self: Module[T], item: SectionItem) =
if(item.isEmpty()):
echo "section is empty and cannot be made as active one"
return
@ -367,3 +388,26 @@ method onActiveChatChange*[T](self: Module[T], sectionId: string, chatId: string
method getAppSearchModule*[T](self: Module[T]): QVariant =
self.appSearchModule.getModuleAsVariant()
method communityJoined*[T](
self: Module[T],
community: CommunityDto,
events: EventEmitter,
contactsService: contacts_service.Service,
chatService: chat_service.Service,
communityService: community_service.ServiceInterface,
messageService: message_service.Service
) =
self.communitySectionsModule[community.id] = chat_section_module.newModule(
self,
events,
community.id,
isCommunity = true,
contactsService,
chatService,
communityService,
messageService
)
self.view.addItem(self.createCommunityItem(community))
# TODO do we need to set it as active

View File

@ -1,4 +1,4 @@
import ../item
import ../../shared_models/section_item
method offerToStorePassword*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")
@ -16,4 +16,7 @@ method enableSection*(self: AccessInterface, sectionType: SectionType) {.base.}
raise newException(ValueError, "No implementation available")
method disableSection*(self: AccessInterface, sectionType: SectionType) {.base.} =
raise newException(ValueError, "No implementation available")
method communityJoined*(self: AccessInterface, community: CommunityDto, events: EventEmitter, contactsService: contacts_service.Service, chatService: chat_service.Service, communityService: community_service.ServiceInterface, messageService: message_service.Service) {.base.} =
raise newException(ValueError, "No implementation available")

View File

@ -1,5 +1,5 @@
import NimQml
import ../item
import ../../shared_models/section_item
method viewDidLoad*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")
@ -7,7 +7,7 @@ method viewDidLoad*(self: AccessInterface) {.base.} =
method storePassword*(self: AccessInterface, password: string) {.base.} =
raise newException(ValueError, "No implementation available")
method setActiveSection*(self: AccessInterface, item: Item) {.base.} =
method setActiveSection*(self: AccessInterface, item: SectionItem) {.base.} =
raise newException(ValueError, "No implementation available")
method setUserStatus*(self: AccessInterface, status: bool) {.base.} =

View File

@ -1,12 +1,14 @@
import NimQml
import model, item, active_section
import ../shared_models/section_model
import ../shared_models/section_item
import ../shared_models/active_section
import io_interface
QtObject:
type
View* = ref object of QObject
delegate: io_interface.AccessInterface
model: Model
model: SectionModel
modelVariant: QVariant
activeSection: ActiveSection
activeSectionVariant: QVariant
@ -32,10 +34,10 @@ QtObject:
# In some point, here, we will setup some exposed main module related things.
self.delegate.viewDidLoad()
proc addItem*(self: View, item: Item) =
proc addItem*(self: View, item: SectionItem) =
self.model.addItem(item)
proc model*(self: View): Model =
proc model*(self: View): SectionModel =
return self.model
proc modelChanged*(self: View) {.signal.}
@ -74,7 +76,7 @@ QtObject:
read = getActiveSection
notify = activeSectionChanged
proc activeSectionSet*(self: View, item: Item) =
proc activeSectionSet*(self: View, item: SectionItem) =
self.activeSection.setActiveSectionData(item)
self.activeSectionChanged()

View File

@ -1,9 +1,9 @@
import NimQml
import item
import section_item
QtObject:
type ActiveSection* = ref object of QObject
item: Item
item: SectionItem
proc setup(self: ActiveSection) =
self.QObject.setup
@ -15,7 +15,7 @@ QtObject:
new(result, delete)
result.setup
proc setActiveSectionData*(self: ActiveSection, item: Item) =
proc setActiveSectionData*(self: ActiveSection, item: SectionItem) =
self.item = item
proc getId(self: ActiveSection): string {.slot.} =
@ -36,6 +36,12 @@ QtObject:
QtProperty[string] name:
read = getName
proc description(self: ActiveSection): string {.slot.} =
return self.item.description
QtProperty[string] description:
read = description
proc getImage(self: ActiveSection): string {.slot.} =
return self.item.image
@ -66,4 +72,40 @@ QtObject:
QtProperty[int] notificationCount:
read = getNotificationCount
proc canJoin(self: ActiveSection): bool {.slot.} =
return self.item.canJoin
QtProperty[bool] canJoin:
read = canJoin
proc canRequestAccess(self: ActiveSection): bool {.slot.} =
return self.item.canRequestAccess
QtProperty[bool] canRequestAccess:
read = canRequestAccess
proc getJoined(self: ActiveSection): bool {.slot.} =
return self.item.joined
QtProperty[bool] joined:
read = getJoined
proc getIsMember(self: ActiveSection): bool {.slot.} =
return self.item.isMember
QtProperty[bool] isMember:
read = getIsMember
proc access(self: ActiveSection): int {.slot.} =
return self.item.access
QtProperty[int] access:
read = access
proc ensOnly(self: ActiveSection): bool {.slot.} =
return self.item.ensOnly
QtProperty[bool] ensOnly:
read = ensOnly

View File

@ -0,0 +1,155 @@
import strformat
type
SectionType* {.pure.} = enum
Chat = 0
Community,
Wallet,
WalletV2,
Browser,
ProfileSettings,
NodeManagement
type
SectionItem* = object
sectionType: SectionType
id: string
name: string
description: string
image: string
icon: string
color: string
hasNotification: bool
notificationsCount: int
active: bool
enabled: bool
isMember: bool
joined: bool
canJoin: bool
canRequestAccess: bool
access: int
ensOnly: bool
proc initItem*(
id: string,
sectionType: SectionType,
name,
description = "",
image = "",
icon = "",
color = "",
hasNotification = false,
notificationsCount: int = 0,
active = false,
enabled = true,
joined = false,
canJoin = false,
canRequestAccess = false,
isMember = false,
access: int = 0,
ensOnly = false
): SectionItem =
result.id = id
result.sectionType = sectionType
result.name = name
result.description = description
result.image = image
result.icon = icon
result.color = color
result.hasNotification = hasNotification
result.notificationsCount = notificationsCount
result.active = active
result.enabled = enabled
result.joined = joined
result.canJoin = canJoin
result.canRequestAccess = canRequestAccess
result.isMember = isMember
result.access = access
result.ensOnly = ensOnly
proc isEmpty*(self: SectionItem): bool =
return self.id.len == 0
proc `$`*(self: SectionItem): string =
result = fmt"""MainModuleItem(
id: {self.id},
sectionType: {self.sectionType.int},
name: {self.name},
description: {self.description},
image: {self.image},
icon: {self.icon},
color: {self.color},
hasNotification: {self.hasNotification},
notificationsCount:{self.notificationsCount},
active:{self.active},
enabled:{self.enabled},
joined:{self.joined},
canJoin:{self.canJoin},
canRequestAccess:{self.canRequestAccess},
isMember:{self.isMember},
access:{self.access},
ensOnly:{self.ensOnly}
]"""
proc id*(self: SectionItem): string {.inline.} =
self.id
proc sectionType*(self: SectionItem): SectionType {.inline.} =
self.sectionType
proc name*(self: SectionItem): string {.inline.} =
self.name
proc description*(self: SectionItem): string {.inline.} =
self.description
proc image*(self: SectionItem): string {.inline.} =
self.image
proc icon*(self: SectionItem): string {.inline.} =
self.icon
proc color*(self: SectionItem): string {.inline.} =
self.color
proc hasNotification*(self: SectionItem): bool {.inline.} =
self.hasNotification
proc `hasNotification=`*(self: var SectionItem, value: bool) {.inline.} =
self.hasNotification = value
proc notificationsCount*(self: SectionItem): int {.inline.} =
self.notificationsCount
proc `notificationsCount=`*(self: var SectionItem, value: int) {.inline.} =
self.notificationsCount = value
proc active*(self: SectionItem): bool {.inline.} =
self.active
proc `active=`*(self: var SectionItem, value: bool) {.inline.} =
self.active = value
proc enabled*(self: SectionItem): bool {.inline.} =
self.enabled
proc `enabled=`*(self: var SectionItem, value: bool) {.inline.} =
self.enabled = value
proc joined*(self: SectionItem): bool {.inline.} =
self.joined
proc canJoin*(self: SectionItem): bool {.inline.} =
self.canJoin
proc canRequestAccess*(self: SectionItem): bool {.inline.} =
self.canRequestAccess
proc isMember*(self: SectionItem): bool {.inline.} =
self.isMember
proc access*(self: SectionItem): int {.inline.} =
self.access
proc ensOnly*(self: SectionItem): bool {.inline.} =
self.ensOnly

View File

@ -1,6 +1,6 @@
import NimQml, Tables, strutils, strformat
import item
import section_item
type
ModelRole {.pure.} = enum
@ -15,42 +15,44 @@ type
NotificationsCount
Active
Enabled
Joined
IsMember
QtObject:
type
Model* = ref object of QAbstractListModel
items: seq[Item]
SectionModel* = ref object of QAbstractListModel
items: seq[SectionItem]
proc delete(self: Model) =
proc delete(self: SectionModel) =
self.items = @[]
self.QAbstractListModel.delete
proc setup(self: Model) =
proc setup(self: SectionModel) =
self.QAbstractListModel.setup
proc newModel*(): Model =
proc newModel*(): SectionModel =
new(result, delete)
result.setup
proc `$`*(self: Model): string =
proc `$`*(self: SectionModel): string =
for i in 0 ..< self.items.len:
result &= fmt"""
[{i}]:({$self.items[i]})
"""
proc countChanged(self: Model) {.signal.}
proc countChanged(self: SectionModel) {.signal.}
proc getCount(self: Model): int {.slot.} =
proc getCount(self: SectionModel): int {.slot.} =
self.items.len
QtProperty[int] count:
read = getCount
notify = countChanged
method rowCount(self: Model, index: QModelIndex = nil): int =
method rowCount(self: SectionModel, index: QModelIndex = nil): int =
return self.items.len
method roleNames(self: Model): Table[int, string] =
method roleNames(self: SectionModel): Table[int, string] =
{
ModelRole.Id.int:"id",
ModelRole.SectionType.int:"sectionType",
@ -62,10 +64,12 @@ QtObject:
ModelRole.HasNotification.int:"hasNotification",
ModelRole.NotificationsCount.int:"notificationsCount",
ModelRole.Active.int:"active",
ModelRole.Enabled.int:"enabled"
ModelRole.Enabled.int:"enabled",
ModelRole.Joined.int:"joined",
ModelRole.IsMember.int:"isMember"
}.toTable
method data(self: Model, index: QModelIndex, role: int): QVariant =
method data(self: SectionModel, index: QModelIndex, role: int): QVariant =
if (not index.isValid):
return
@ -98,8 +102,12 @@ QtObject:
result = newQVariant(item.active)
of ModelRole.Enabled:
result = newQVariant(item.enabled)
of ModelRole.Joined:
result = newQVariant(item.joined)
of ModelRole.IsMember:
result = newQVariant(item.isMember)
proc addItem*(self: Model, item: Item) =
proc addItem*(self: SectionModel, item: SectionItem) =
let parentModelIndex = newQModelIndex()
defer: parentModelIndex.delete
@ -109,17 +117,17 @@ QtObject:
self.countChanged()
proc getItemById*(self: Model, id: string): Item =
proc getItemById*(self: SectionModel, id: string): SectionItem =
for it in self.items:
if(it.id == id):
return it
proc getItemBySectionType*(self: Model, sectionType: SectionType): Item =
proc getItemBySectionType*(self: SectionModel, sectionType: SectionType): SectionItem =
for it in self.items:
if(it.sectionType == sectionType):
return it
proc setActiveSection*(self: Model, id: string) =
proc setActiveSection*(self: SectionModel, id: string) =
for i in 0 ..< self.items.len:
if(self.items[i].active):
let index = self.createIndex(i, 0, nil)
@ -131,7 +139,7 @@ QtObject:
self.items[i].active = true
self.dataChanged(index, index, @[ModelRole.Active.int])
proc enableDisableSection(self: Model, sectionType: SectionType, value: bool) =
proc enableDisableSection(self: SectionModel, sectionType: SectionType, value: bool) =
if(sectionType != SectionType.Community):
for i in 0 ..< self.items.len:
if(self.items[i].sectionType == sectionType):
@ -153,8 +161,8 @@ QtObject:
let bottomIndex = self.createIndex(bottomInd, 0, nil)
self.dataChanged(topIndex, bottomIndex, @[ModelRole.Enabled.int])
proc enableSection*(self: Model, sectionType: SectionType) =
proc enableSection*(self: SectionModel, sectionType: SectionType) =
self.enableDisableSection(sectionType, true)
proc disableSection*(self: Model, sectionType: SectionType) =
proc disableSection*(self: SectionModel, sectionType: SectionType) =
self.enableDisableSection(sectionType, false)

View File

@ -8,6 +8,7 @@ include ../../../common/json_utils
type
Permission* = object
access*: int
ensOnly*: bool
type
Images* = object
@ -59,6 +60,7 @@ type CommunityDto* = object
proc toPermission(jsonObj: JsonNode): Permission =
result = Permission()
discard jsonObj.getProp("access", result.access)
discard jsonObj.getProp("ens_only", result.ensOnly)
proc toImages(jsonObj: JsonNode): Images =
result = Images()

View File

@ -1,5 +1,6 @@
import Tables, json, sequtils, std/algorithm, strformat, chronicles
import eventemitter
import service_interface, ./dto/community
import ../chat/service as chat_service
@ -11,11 +12,19 @@ export service_interface
logScope:
topics = "community-service"
type
CommunityArgs* = ref object of Args
community*: CommunityDto
# Signals which may be emitted by this service:
const SIGNAL_COMMUNITY_JOINED* = "SIGNAL_COMMUNITY_JOINED"
type
Service* = ref object of service_interface.ServiceInterface
events: EventEmitter
joinedCommunities: Table[string, CommunityDto] # [community_id, CommunityDto]
allCommunities: Table[string, CommunityDto] # [community_id, CommunityDto]
chatService: chat_service.Service
# Forward declaration
method loadAllCommunities(self: Service): seq[CommunityDto]
@ -24,10 +33,11 @@ method loadJoinedComunities(self: Service): seq[CommunityDto]
method delete*(self: Service) =
discard
proc newService*(chatService: chat_service.Service): Service =
proc newService*(events: EventEmitter): Service =
result = Service()
result.events = events
result.joinedCommunities = initTable[string, CommunityDto]()
result.chatService = chatService
result.allCommunities = initTable[string, CommunityDto]()
method init*(self: Service) =
try:
@ -126,4 +136,28 @@ method getAllChats*(self: Service, communityId: string, order = SortOrder.Ascend
if(order == SortOrder.Ascending):
result.sort(sortAsc[Chat])
else:
result.sort(sortDesc[Chat])
result.sort(sortDesc[Chat])
method isUserMemberOfCommunity*(self: Service, communityId: string): bool =
if(not self.allCommunities.contains(communityId)):
return false
return self.allCommunities[communityId].joined and self.allCommunities[communityId].isMember
method userCanJoin*(self: Service, communityId: string): bool =
if(not self.allCommunities.contains(communityId)):
return false
return self.allCommunities[communityId].canJoin
method joinCommunity*(self: Service, communityId: string): string =
result = ""
try:
if (not self.userCanJoin(communityId) or self.isUserMemberOfCommunity(communityId)):
return
discard status_go.joinCommunity(communityId)
var community = self.allCommunities[communityId]
self.joinedCommunities[communityId] = community
self.events.emit(SIGNAL_COMMUNITY_JOINED, CommunityArgs(community: community))
except Exception as e:
error "Error joining the community", msg = e.msg
result = fmt"Error joining the community: {e.msg}"

View File

@ -37,4 +37,13 @@ method getChats*(self: ServiceInterface, communityId: string, categoryId = "", o
raise newException(ValueError, "No implementation available")
method getAllChats*(self: ServiceInterface, communityId: string, order = SortOrder.Ascending): seq[Chat] {.base.} =
raise newException(ValueError, "No implementation available")
method isUserMemberOfCommunity*(self: ServiceInterface, communityId: string) {.base.} =
raise newException(ValueError, "No implementation available")
method userCanJoin*(self: ServiceInterface, communityId: string) {.base.} =
raise newException(ValueError, "No implementation available")
method joinCommunity*(self: ServiceInterface, communityId: string) {.base.} =
raise newException(ValueError, "No implementation available")

View File

@ -15,19 +15,19 @@ StatusModal {
id: root
property var store
// Not Refactored Yet
property QtObject community: null // root.store.chatsModelInst.communities.observedCommunity
property QtObject community: root.store.communitiesModuleInst.observedCommunity
property string communityId: community.id
property string name: community.name
property string description: community.description
property int access: community.access
property string source: community.thumbnailImage
property int nbMembers: community.nbMembers
property string source: community.image
// TODO members needs to be refactored on the backend
property int nbMembers: 0//community.nbMembers
property bool ensOnly: community.ensOnly
property bool canJoin: community.canJoin
property bool canRequestAccess: community.canRequestAccess
property bool isMember: community.isMember
property string communityColor: community.communityColor || Style.current.blue
property string communityColor: community.color || Style.current.blue
header.title: name
header.subTitle: {
@ -45,8 +45,8 @@ StatusModal {
//% "On request community"
subTitle = qsTrId("on-request-community");
break;
default:
subTitle = qsTrId("Unknown community");
default:
subTitle = qsTr("Unknown community");
break;
}
if (ensOnly) {
@ -202,24 +202,26 @@ StatusModal {
}
onClicked: {
// Not Refactored Yet
// let error
// if (access === Constants.communityChatOnRequestAccess && !root.isMember) {
// error = root.store.chatsModelInst.communities.requestToJoinCommunity(root.communityId, userProfile.name)
// if (!error) {
// enabled = false
// //% "Pending"
// text = qsTrId("invite-chat-pending")
// }
// } else {
// error = root.store.chatsModelInst.communities.joinCommunity(root.communityId, true)
// }
let error
if (access === Constants.communityChatOnRequestAccess && !root.isMember) {
// TODO refactor
return
// error = root.store.chatsModelInst.communities.requestToJoinCommunity(root.communityId, userProfile.name)
// if (!error) {
// enabled = false
// //% "Pending"
// text = qsTrId("invite-chat-pending")
// }
} else {
error = root.store.communitiesModuleInst.joinCommunity(root.communityId)
}
// if (error) {
// joiningError.text = error
// return joiningError.open()
// }
if (error) {
joiningError.text = error
return joiningError.open()
}
// root.close()
root.close()
}
}
]

View File

@ -355,9 +355,9 @@ Item {
// onSetActiveCommunity: {
// root.store.chatsModelInst.communities.setActiveCommunity(id)
// }
// onSetObservedCommunity: {
// root.store.chatsModelInst.communities.setObservedCommunity(id)
// }
onSetObservedCommunity: {
root.store.communitiesModuleInst.setObservedCommunity(id)
}
onClosed: {
destroy()
}