Skip to content

Commit c99b4df

Browse files
committed
room topic announce and qrCode method
1 parent 73dcc44 commit c99b4df

File tree

2 files changed

+78
-3
lines changed

2 files changed

+78
-3
lines changed

wechaty/src/main/kotlin/io/github/wechaty/user/Room.kt

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import io.github.wechaty.filebox.FileBox
1313
import io.github.wechaty.schemas.RoomMemberQueryFilter
1414
import io.github.wechaty.schemas.RoomPayload
1515
import io.github.wechaty.type.Sayable
16+
import io.github.wechaty.utils.QrcodeUtils
1617
import org.apache.commons.collections4.CollectionUtils
1718
import org.apache.commons.lang3.StringUtils
1819
import org.slf4j.LoggerFactory
@@ -31,6 +32,10 @@ class Room(wechaty: Wechaty, val id: String) : Accessory(wechaty), Sayable {
3132
return ready(true)
3233
}
3334

35+
fun isReady(): Boolean {
36+
return this.payload != null
37+
}
38+
3439
override fun say(something: Any, contact: Contact): Future<Any> {
3540

3641
var msgId: String? = null
@@ -75,8 +80,7 @@ class Room(wechaty: Wechaty, val id: String) : Accessory(wechaty), Sayable {
7580
}
7681
mentionList = varList.toList()
7782

78-
val mentionAlias = mentionList.map {
79-
contact ->
83+
val mentionAlias = mentionList.map { contact ->
8084
val alias = alias(contact as Contact)
8185
val concatText = if (StringUtils.isNotBlank(alias)) {
8286
alias!!
@@ -213,6 +217,66 @@ class Room(wechaty: Wechaty, val id: String) : Accessory(wechaty), Sayable {
213217
}
214218
}
215219

220+
fun topic(newTopic: String?): Future<Any> {
221+
if (!isReady()) {
222+
log.warn("Room topic() room not ready")
223+
throw Exception("not ready")
224+
}
225+
226+
if (newTopic == null) {
227+
if (payload != null && payload!!.topic != null) {
228+
return CompletableFuture.supplyAsync {
229+
return@supplyAsync payload!!.topic
230+
}
231+
} else {
232+
val memberIdList = puppet.roomMemberList(id).get()
233+
val memberList = memberIdList.filter { it != puppet.selfId() }
234+
.map { wechaty.contactManager.load(it) }
235+
236+
var defaultTopic = ""
237+
if (memberList.isNotEmpty()) {
238+
defaultTopic = memberList[0].name()
239+
}
240+
241+
if (memberList.size >= 2) {
242+
for (index in 1..2) {
243+
defaultTopic += ",${memberList[index].name()}"
244+
}
245+
}
246+
return CompletableFuture.supplyAsync {
247+
return@supplyAsync defaultTopic
248+
}
249+
}
250+
}
251+
252+
return CompletableFuture.supplyAsync {
253+
try {
254+
return@supplyAsync puppet.roomTopic(id, newTopic).get()
255+
} catch (e: Exception) {
256+
log.warn("Room topic(newTopic=$newTopic) exception:$e")
257+
throw Exception(e)
258+
}
259+
}
260+
261+
}
262+
263+
fun announce(text: String?): Future<Any> {
264+
return CompletableFuture.supplyAsync {
265+
if (text == null) {
266+
return@supplyAsync puppet.getRoomAnnounce(id).get()
267+
} else {
268+
return@supplyAsync puppet.setRoomAnnounce(id, text)
269+
}
270+
}
271+
}
272+
273+
fun qrCode(): Future<String> {
274+
return CompletableFuture.supplyAsync {
275+
val qrCodeValue = puppet.roomQRCode(id).get()
276+
return@supplyAsync QrcodeUtils.guardQrCodeValue(qrCodeValue)
277+
}
278+
}
279+
216280
fun memberAll(query: RoomMemberQueryFilter?): List<Contact> {
217281

218282
if (query == null) {

wechaty/src/main/kotlin/io/github/wechaty/utils/QrcodeUtils.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ import com.google.zxing.common.BitMatrix
88
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel
99
import java.util.*
1010

11+
const val MAX_LEN = 7089
12+
1113
class QrcodeUtils {
1214

15+
1316
companion object {
1417

1518
@JvmStatic
@@ -48,6 +51,14 @@ class QrcodeUtils {
4851
}
4952
return sb.toString()
5053
}
54+
55+
@JvmStatic
56+
fun guardQrCodeValue(value: String): String {
57+
if (value.length > MAX_LEN) {
58+
throw Exception("QR Code Value is larger then the max len. Did you return the image base64 text by mistake? See: https://github.com/wechaty/wechaty/issues/1889")
59+
}
60+
return value
61+
}
5162
}
5263

53-
}
64+
}

0 commit comments

Comments
 (0)