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

@ -170,10 +170,13 @@ QtObject:
proc membershipRequestChanged*(self: CommunitiesView, communityName: string, accepted: bool) {.signal.} proc membershipRequestChanged*(self: CommunitiesView, communityName: string, accepted: bool) {.signal.}
proc communityAdded*(self: CommunitiesView, communityId: string) {.signal.}
proc addCommunityToList*(self: CommunitiesView, community: Community) = proc addCommunityToList*(self: CommunitiesView, community: Community) =
let communityCheck = self.communityList.getCommunityById(community.id) let communityCheck = self.communityList.getCommunityById(community.id)
if (communityCheck.id == ""): if (communityCheck.id == ""):
self.communityList.addCommunityItemToList(community) self.communityList.addCommunityItemToList(community)
self.communityAdded(community.id)
else: else:
self.communityList.replaceCommunity(community) self.communityList.replaceCommunity(community)
@ -342,6 +345,12 @@ QtObject:
return "Error declining request to join the community" return "Error declining request to join the community"
return "" 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 = proc getChannel*(self: CommunitiesView, channelId: string): Chat =
for community in self.joinedCommunityList.communities: for community in self.joinedCommunityList.communities:
for chat in community.chats: for chat in community.chats:

View File

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

View File

@ -29,6 +29,22 @@ Item {
property string communityId: "" property string communityId: ""
property int stickerPackId: -1 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 authorCurrentMsg: "authorCurrentMsg"
property string authorPrevMsg: "authorPrevMsg" property string authorPrevMsg: "authorPrevMsg"

View File

@ -59,7 +59,7 @@ Item {
StyledText { StyledText {
id: invitedYou id: invitedYou
//% "%1 invited you to join a community" //% "%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.top: title.bottom
anchors.topMargin: 4 anchors.topMargin: 4
anchors.left: parent.left anchors.left: parent.left

View File

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

View File

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

View File

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