feat(community): fetch community info from link

Fixes #2290
This commit is contained in:
Jonathan Rainville 2021-04-29 15:08:57 -04:00 committed by Iuri Matias
parent 7ee11e27e2
commit eac7009807
7 changed files with 77 additions and 19 deletions

View File

@ -169,11 +169,14 @@ QtObject:
result = fmt"Error joining the community: {e.msg}"
proc membershipRequestChanged*(self: CommunitiesView, communityName: string, accepted: bool) {.signal.}
proc communityAdded*(self: CommunitiesView, communityId: string) {.signal.}
proc addCommunityToList*(self: CommunitiesView, community: Community) =
let communityCheck = self.communityList.getCommunityById(community.id)
if (communityCheck.id == ""):
self.communityList.addCommunityItemToList(community)
self.communityAdded(community.id)
else:
self.communityList.replaceCommunity(community)
@ -342,6 +345,12 @@ QtObject:
return "Error declining request to join the community"
return ""
proc requestCommunityInfo*(self: CommunitiesView, communityId: string) {.slot.} =
try:
self.status.chat.requestCommunityInfo(communityId)
except Exception as e:
error "Error fetching community info", msg = e.msg
proc getChannel*(self: CommunitiesView, channelId: string): Chat =
for community in self.joinedCommunityList.communities:
for chat in community.chats:

View File

@ -53,6 +53,8 @@ type
ChatModel* = ref object
publicKey*: string
events*: EventEmitter
communitiesToFetch*: seq[string]
mailserverReady*: bool
contacts*: Table[string, Profile]
channels*: Table[string, Chat]
msgCursor*: Table[string, string]
@ -68,6 +70,8 @@ include chat/utils
proc newChatModel*(events: EventEmitter): ChatModel =
result = ChatModel()
result.events = events
result.mailserverReady = false
result.communitiesToFetch = @[]
result.contacts = initTable[string, Profile]()
result.channels = initTable[string, Chat]()
result.msgCursor = initTable[string, string]()
@ -160,6 +164,11 @@ proc updateContacts*(self: ChatModel, contacts: seq[Profile]) =
self.contacts[c.id] = c
self.events.emit("chatUpdate", ChatUpdateArgs(contacts: contacts))
proc requestMissingCommunityInfos*(self: ChatModel) =
if (self.communitiesToFetch.len == 0):
return
for communityId in self.communitiesToFetch:
status_chat.requestCommunityInfo(communityId)
proc init*(self: ChatModel, pubKey: string) =
self.publicKey = pubKey
@ -228,6 +237,8 @@ proc init*(self: ChatModel, pubKey: string) =
warn "No topics found for chats. Cannot load past messages"
else:
self.events.once("mailserverAvailable") do(a: Args):
self.mailserverReady = true
self.requestMissingCommunityInfos()
self.events.emit("mailserverTopics", TopicArgs(topics: topics));
self.events.on("contactUpdate") do(a: Args):
@ -462,6 +473,10 @@ proc joinCommunity*(self: ChatModel, communityId: string) =
status_chat.joinCommunity(communityId)
proc requestCommunityInfo*(self: ChatModel, communityId: string) =
if (not self.mailserverReady):
self.communitiesToFetch.add(communityId)
self.communitiesToFetch = self.communitiesToFetch.deduplicate()
return
status_chat.requestCommunityInfo(communityId)
proc leaveCommunity*(self: ChatModel, communityId: string) =

View File

@ -29,6 +29,22 @@ Item {
property string communityId: ""
property int stickerPackId: -1
property string displayUserName: {
if (isCurrentUser) {
//% "You"
return qsTrId("You")
}
if (localName !== "") {
return localName
}
if (userName !== "") {
return Utils.removeStatusEns(userName)
}
return Utils.removeStatusEns(alias)
}
property string authorCurrentMsg: "authorCurrentMsg"
property string authorPrevMsg: "authorPrevMsg"

View File

@ -59,7 +59,7 @@ Item {
StyledText {
id: invitedYou
//% "%1 invited you to join a community"
text: qsTrId("-1-invited-you-to-join-a-community").arg(userName)
text: qsTrId("-1-invited-you-to-join-a-community").arg(displayUserName)
anchors.top: title.bottom
anchors.topMargin: 4
anchors.left: parent.left

View File

@ -55,6 +55,8 @@ Column {
}
}
Connections {
id: linkFetchConnections
enabled: false
target: chatsModel
onLinkPreviewDataWasReceived: {
let response
@ -69,6 +71,8 @@ Column {
if (response.uuid !== root.uuid) return
linkFetchConnections.enabled = false
if (!response.success) {
console.error(response.result.error)
return undefined
@ -86,6 +90,27 @@ Column {
}
}
Connections {
id: linkCommunityFetchConnections
enabled: false
target: chatsModel.communities
onCommunityAdded: {
if (communityId !== linkData.communityId) {
return
}
linkCommunityFetchConnections.enabled = false
const data = Utils.getLinkDataForStatusLinks(link)
if (data) {
linkData = data
if (!data.fetching && data.communityId) {
return linkMessageLoader.sourceComponent = invitationBubble
}
return linkMessageLoader.sourceComponent = unfurledLinkComponent
}
}
}
function getSourceComponent() {
// Reset the height in case we set it to 0 below. See note below
// for more information
@ -117,6 +142,10 @@ Column {
const data = Utils.getLinkDataForStatusLinks(link)
if (data) {
linkData = data
if (data.fetching && data.communityId) {
linkCommunityFetchConnections.enabled = true
return
}
if (data.communityId) {
return invitationBubble
}
@ -124,6 +153,7 @@ Column {
return unfurledLinkComponent
}
linkFetchConnections.enabled = true
return chatsModel.getLinkPreviewData(link, root.uuid)
}
// setting the height to 0 allows the "enable link" dialog to

View File

@ -12,21 +12,7 @@ Item {
StyledTextEdit {
id: chatName
text: {
if (isCurrentUser) {
//% "You"
return qsTrId("You")
}
if (localName !== "") {
return localName
}
if (userName !== "") {
return Utils.removeStatusEns(userName)
}
return Utils.removeStatusEns(alias)
}
text: displayUserName
color: text.startsWith("@") || isCurrentUser || localName !== "" ? Style.current.blue : Style.current.secondaryText
font.weight: Font.Medium
font.pixelSize: Style.current.secondaryTextFontSize

View File

@ -371,7 +371,6 @@ QtObject {
}
// Community
// TODO this will probably change
index = link.lastIndexOf("/cc/")
if (index > -1) {
const communityId = link.substring(index + 4)
@ -379,8 +378,10 @@ QtObject {
const communityName = chatsModel.communities.getCommunityNameById(communityId)
if (!communityName) {
// Unknown community
// TODO use a function to fetch that community?
// Unknown community, fetch the info if possible
chatsModel.communities.requestCommunityInfo(communityId)
result.communityId = communityId
result.fetching = true
return result
}
@ -423,6 +424,7 @@ QtObject {
site: qsTr("Status app link"),
title: result.title,
communityId: result.communityId,
fetching: result.fetching,
thumbnailUrl: "../../../../img/status.png",
contentType: "",
height: 0,