refactor(community-service): merge curatedCommunities in communities

Fixes #9752
This commit is contained in:
Jonathan Rainville 2023-03-06 16:06:01 -05:00
parent 33d38a4081
commit 55ad5409f7
7 changed files with 67 additions and 84 deletions

View File

@ -46,8 +46,8 @@ proc init*(self: Controller) =
self.delegate.onImportCommunityErrorOccured(args.community.id, args.error)
self.events.on(SIGNAL_CURATED_COMMUNITY_FOUND) do(e:Args):
let args = CuratedCommunityArgs(e)
self.delegate.curatedCommunityAdded(args.curatedCommunity)
let args = CommunityArgs(e)
self.delegate.curatedCommunityAdded(args.community)
self.events.on(SIGNAL_COMMUNITY_ADDED) do(e:Args):
let args = CommunityArgs(e)
@ -64,7 +64,7 @@ proc init*(self: Controller) =
let args = CommunitiesArgs(e)
for community in args.communities:
self.delegate.communityEdited(community)
self.delegate.curatedCommunityEdited(CuratedCommunity(communityId: community.id, available: true, community:community))
self.delegate.curatedCommunityEdited(community)
self.events.on(SIGNAL_COMMUNITY_MUTED) do(e:Args):
let args = CommunityMutedArgs(e)
@ -97,8 +97,8 @@ proc init*(self: Controller) =
self.delegate.curatedCommunitiesLoadingFailed()
self.events.on(SIGNAL_CURATED_COMMUNITIES_LOADED) do(e:Args):
let args = CuratedCommunitiesArgs(e)
self.delegate.curatedCommunitiesLoaded(args.curatedCommunities)
let args = CommunitiesArgs(e)
self.delegate.curatedCommunitiesLoaded(args.communities)
proc getCommunityTags*(self: Controller): string =
result = self.communityService.getCommunityTags()
@ -106,7 +106,7 @@ proc getCommunityTags*(self: Controller): string =
proc getAllCommunities*(self: Controller): seq[CommunityDto] =
result = self.communityService.getAllCommunities()
proc getCuratedCommunities*(self: Controller): seq[CuratedCommunity] =
proc getCuratedCommunities*(self: Controller): seq[CommunityDto] =
result = self.communityService.getCuratedCommunities()
proc spectateCommunity*(self: Controller, communityId: string): string =

View File

@ -107,10 +107,10 @@ method communityEdited*(self: AccessInterface, community: CommunityDto) {.base.}
method communityAdded*(self: AccessInterface, community: CommunityDto) {.base.} =
raise newException(ValueError, "No implementation available")
method curatedCommunityAdded*(self: AccessInterface, community: CuratedCommunity) {.base.} =
method curatedCommunityAdded*(self: AccessInterface, community: CommunityDto) {.base.} =
raise newException(ValueError, "No implementation available")
method curatedCommunityEdited*(self: AccessInterface, community: CuratedCommunity) {.base.} =
method curatedCommunityEdited*(self: AccessInterface, community: CommunityDto) {.base.} =
raise newException(ValueError, "No implementation available")
method communityImported*(self: AccessInterface, community: CommunityDto) {.base.} =
@ -155,5 +155,5 @@ method curatedCommunitiesLoading*(self: AccessInterface) {.base.} =
method curatedCommunitiesLoadingFailed*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")
method curatedCommunitiesLoaded*(self: AccessInterface, curatedCommunities: seq[CuratedCOmmunity]) {.base.} =
method curatedCommunitiesLoaded*(self: AccessInterface, curatedCommunities: seq[CommunityDto]) {.base.} =
raise newException(ValueError, "No implementation available")

View File

@ -46,7 +46,7 @@ type
# Forward declaration
method setCommunityTags*(self: Module, communityTags: string)
method setAllCommunities*(self: Module, communities: seq[CommunityDto])
method setCuratedCommunities*(self: Module, curatedCommunities: seq[CuratedCommunity])
method setCuratedCommunities*(self: Module, curatedCommunities: seq[CommunityDto])
proc newModule*(
delegate: delegate_interface.AccessInterface,
@ -89,7 +89,6 @@ method viewDidLoad*(self: Module) =
self.setCommunityTags(self.controller.getCommunityTags())
self.setAllCommunities(self.controller.getAllCommunities())
self.setCuratedCommunities(self.controller.getCuratedCommunities())
self.delegate.communitiesModuleDidLoad()
@ -98,7 +97,7 @@ method onActivated*(self: Module) =
return
self.controller.asyncLoadCuratedCommunities()
method curatedCommunitiesLoaded*(self: Module, curatedCommunities: seq[CuratedCOmmunity]) =
method curatedCommunitiesLoaded*(self: Module, curatedCommunities: seq[CommunityDto]) =
self.curatedCommunitiesLoaded = true
self.setCuratedCommunities(curatedCommunities)
self.view.setCuratedCommunitiesLoading(false)
@ -167,17 +166,17 @@ method getCommunityItem(self: Module, c: CommunityDto): SectionItem =
communityTokens = self.controller.getCommunityTokens(c.id)
)
method getCuratedCommunityItem(self: Module, c: CuratedCommunity): CuratedCommunityItem =
method getCuratedCommunityItem(self: Module, c: CommunityDto): CuratedCommunityItem =
return initCuratedCommunityItem(
c.communityId,
c.community.name,
c.community.description,
c.available,
c.community.images.thumbnail,
c.community.images.banner,
c.community.color,
c.community.tags,
len(c.community.members))
c.id,
c.name,
c.description,
c.isAvailable,
c.images.thumbnail,
c.images.banner,
c.color,
c.tags,
len(c.members))
method getDiscordCategoryItem(self: Module, c: DiscordCategoryDto): DiscordCategoryItem =
return initDiscordCategoryItem(
@ -218,14 +217,14 @@ method communityEdited*(self: Module, community: CommunityDto) =
self.view.model().editItem(self.getCommunityItem(community))
self.view.communityChanged(community.id)
method setCuratedCommunities*(self: Module, curatedCommunities: seq[CuratedCommunity]) =
method setCuratedCommunities*(self: Module, curatedCommunities: seq[CommunityDto]) =
for community in curatedCommunities:
self.view.curatedCommunitiesModel().addItem(self.getCuratedCommunityItem(community))
method curatedCommunityAdded*(self: Module, community: CuratedCommunity) =
method curatedCommunityAdded*(self: Module, community: CommunityDto) =
self.view.curatedCommunitiesModel().addItem(self.getCuratedCommunityItem(community))
method curatedCommunityEdited*(self: Module, community: CuratedCommunity) =
method curatedCommunityEdited*(self: Module, community: CommunityDto) =
self.view.curatedCommunitiesModel().addItem(self.getCuratedCommunityItem(community))
method requestAdded*(self: Module) =

View File

@ -19,9 +19,12 @@ const asyncLoadCuratedCommunitiesTask: Task = proc(argEncoded: string) {.gcsafe,
let arg = decode[AsyncLoadCuratedCommunitiesTaskArg](argEncoded)
try:
let response = status_go.getCuratedCommunities()
arg.finish(response)
arg.finish(%* {
"response": response,
"error": "",
})
except Exception as e:
arg.finish(%* {
"error": RpcError(message: e.msg),
"error": e.msg,
})

View File

@ -91,6 +91,7 @@ type CommunityDto* = object
requestedToJoinAt*: int64
isMember*: bool
muted*: bool
listedInDirectory*: bool
pendingRequestsToJoin*: seq[CommunityMembershipRequestDto]
settings*: CommunitySettingsDto
adminSettings*: CommunityAdminSettingsDto
@ -101,10 +102,8 @@ type CommunityDto* = object
tokenPermissions*: Table[string, CommunityTokenPermissionDto]
communityTokensMetadata*: seq[CommunityTokensMetadataDto]
type CuratedCommunity* = object
available*: bool
communityId*: string
community*: CommunityDto
proc isAvailable*(communityDto: CommunityDto): bool =
return communityDto.name != "" and communityDto.description != ""
type DiscordCategoryDto* = object
id*: string
@ -336,35 +335,24 @@ proc parseCommunities*(response: RpcResponse[JsonNode]): seq[CommunityDto] =
result = map(response.result.getElems(),
proc(x: JsonNode): CommunityDto = x.toCommunityDto())
proc parseCuratedCommunities*(response: RpcResponse[JsonNode]): seq[CuratedCommunity] =
if (response.result["communities"].kind == JObject):
for (communityId, communityJson) in response.result["communities"].pairs():
result.add(CuratedCommunity(
available: true,
communityId: communityId,
community: communityJson.toCommunityDto()
))
if (response.result["unknownCommunities"].kind == JArray):
for communityId in response.result["unknownCommunities"].items():
result.add(CuratedCommunity(
available: false,
communityId: communityId.getStr()
))
proc parseKnownCuratedCommunities(jsonCommunities: JsonNode): seq[CommunityDto] =
for _, communityJson in jsonCommunities.pairs():
var community = communityJson.toCommunityDto()
community.listedInDirectory = true
result.add(community)
proc parseCuratedCommunities*(response: JsonNode): seq[CuratedCommunity] =
proc parseUnknownCuratedCommunities(jsonCommunities: JsonNode): seq[CommunityDto] =
for communityId in jsonCommunities.items():
var community = CommunityDto()
community.id = communityId.getStr
community.listedInDirectory = true
result.add(community)
proc parseCuratedCommunities*(response: JsonNode): seq[CommunityDto] =
if (response["communities"].kind == JObject):
for (communityId, communityJson) in response["communities"].pairs():
result.add(CuratedCommunity(
available: true,
communityId: communityId,
community: communityJson.toCommunityDto()
))
result = parseKnownCuratedCommunities(response["communities"])
if (response["unknownCommunities"].kind == JArray):
for communityId in response["unknownCommunities"].items():
result.add(CuratedCommunity(
available: false,
communityId: communityId.getStr()
))
result = concat(result, parseUnknownCuratedCommunities(response["unknownCommunities"]))
proc contains(arrayToSearch: seq[int], searched: int): bool =
for element in arrayToSearch:

View File

@ -27,12 +27,6 @@ type
error*: string
fromUserAction*: bool
CuratedCommunityArgs* = ref object of Args
curatedCommunity*: CuratedCommunity
CuratedCommunitiesArgs* = ref object of Args
curatedCommunities*: seq[CuratedCommunity]
CommunitiesArgs* = ref object of Args
communities*: seq[CommunityDto]
@ -176,7 +170,6 @@ QtObject:
activityCenterService: activity_center_service.Service
messageService: message_service.Service
communityTags: string # JSON string contraining tags map
curatedCommunities: Table[string, CuratedCommunity] # [community_id, CuratedCommunity]
communities: Table[string, CommunityDto] # [community_id, CommunityDto]
myCommunityRequests*: seq[CommunityMembershipRequestDto]
historyArchiveDownloadTaskCommunityIds*: HashSet[string]
@ -213,7 +206,6 @@ QtObject:
result.activityCenterService = activityCenterService
result.messageService = messageService
result.communityTags = newString(0)
result.curatedCommunities = initTable[string, CuratedCommunity]()
result.communities = initTable[string, CommunityDto]()
result.myCommunityRequests = @[]
result.historyArchiveDownloadTaskCommunityIds = initHashSet[string]()
@ -224,18 +216,22 @@ QtObject:
if community.joined:
result[communityId] = community
proc getFilteredCuratedCommunities(self: Service): Table[string, CommunityDto] =
result = initTable[string, CommunityDto]()
for communityId, community in self.communities.pairs:
if community.listedInDirectory:
result[communityId] = community
proc doConnect(self: Service) =
self.events.on(SignalType.CommunityFound.event) do(e: Args):
var receivedData = CommunitySignal(e)
self.communities[receivedData.community.id] = receivedData.community
self.events.emit(SIGNAL_COMMUNITY_DATA_IMPORTED, CommunityArgs(community: receivedData.community))
if self.curatedCommunities.contains(receivedData.community.id) and not self.curatedCommunities[receivedData.community.id].available:
let curatedCommunity = CuratedCommunity(available: true,
communityId: receivedData.community.id,
community: receivedData.community)
self.curatedCommunities[receivedData.community.id] = curatedCommunity
self.events.emit(SIGNAL_CURATED_COMMUNITY_FOUND, CuratedCommunityArgs(curatedCommunity: curatedCommunity))
if self.communities.contains(receivedData.community.id) and
self.communities[receivedData.community.id].listedInDirectory and not
self.communities[receivedData.community.id].isAvailable:
self.events.emit(SIGNAL_CURATED_COMMUNITY_FOUND, CommunityArgs(community: self.communities[receivedData.community.id]))
self.events.on(SignalType.Message.event) do(e: Args):
var receivedData = MessageSignal(e)
@ -427,10 +423,6 @@ QtObject:
return
if(self.curatedCommunities.hasKey(community.id)):
self.curatedCommunities[community.id].available = true
self.curatedCommunities[community.id].community = community
let prev_community = self.communities[community.id]
# If there's settings without `id` it means the original
@ -640,8 +632,8 @@ QtObject:
proc getAllCommunities*(self: Service): seq[CommunityDto] =
return toSeq(self.communities.values)
proc getCuratedCommunities*(self: Service): seq[CuratedCommunity] =
return toSeq(self.curatedCommunities.values)
proc getCuratedCommunities*(self: Service): seq[CommunityDto] =
return toSeq(self.getFilteredCuratedCommunities.values)
proc getCommunityById*(self: Service, communityId: string): CommunityDto =
if(not self.communities.hasKey(communityId)):
@ -1351,16 +1343,14 @@ QtObject:
proc onAsyncLoadCuratedCommunitiesDone*(self: Service, response: string) {.slot.} =
try:
let rpcResponseObj = response.parseJson
if (rpcResponseObj{"error"}.kind != JNull):
let error = Json.decode($rpcResponseObj["error"], RpcError)
error "Error requesting community info", msg = error.message
if (rpcResponseObj{"error"}.kind != JNull and rpcResponseObj{"error"}.getStr != ""):
error "Error requesting community info", msg = rpcResponseObj{"error"}.getStr
self.events.emit(SIGNAL_CURATED_COMMUNITIES_LOADING_FAILED, Args())
return
let curatedCommunities = parseCuratedCommunities(rpcResponseObj{"result"})
let curatedCommunities = parseCuratedCommunities(rpcResponseObj["response"]["result"])
for curatedCommunity in curatedCommunities:
self.curatedCommunities[curatedCommunity.communityId] = curatedCommunity
self.events.emit(SIGNAL_CURATED_COMMUNITIES_LOADED, CuratedCommunitiesArgs(curatedCommunities: self.getCuratedCommunities()))
self.communities[curatedCommunity.id] = curatedCommunity
self.events.emit(SIGNAL_CURATED_COMMUNITIES_LOADED, CommunitiesArgs(communities: self.getCuratedCommunities()))
except Exception as e:
let errMsg = e.msg
error "error loading curated communities: ", errMsg

View File

@ -57,6 +57,9 @@ StatusSectionLayout {
id: filteredCommunitiesModel
function selectedTagsPredicate(selectedTagsNames, tagsJSON) {
if (!tagsJSON) {
return true
}
const tags = JSON.parse(tagsJSON)
for (const i in tags) {
selectedTagsNames = selectedTagsNames.filter(name => name !== tags[i].name)